--- /dev/null
+<?php
+ class AppliancesController extends AppController {
+ public $helpers = array('Html', 'Form');
+ public $components = array('RequestHandler');
+ public $uses = array('Appliance', 'Changelog');
+
+ public function index() {
+
+ // GETTING CONTROLLERS
+ App::import('Controller', 'Warnings');
+ $warningsController = new WarningsController;
+ App::import('Controller', 'Changelogs');
+ $changelogsController = new ChangelogsController;
+ App::import('Controller', 'Customers');
+ $customersController = new CustomersController;
+
+ // GETTING ALL APPLIANCES
+ $appliances = $this->Appliance->find('all');
+
+ // GETTING ALL CUSTOMERS
+ $customers = $customersController->Customer->find('all');
+
+ // GETTING INFORMATION FOR EACH APPLIANCE
+ $changelogsArray = array();
+ $warningsArray = array();
+ $customersArray = array();
+ $lastUpdates = array();
+ foreach($appliances as $appliance) {
+
+ // GETTING CHANGELOG
+ $changelog = $changelogsController->Changelog->findById($appliance['Appliance']['changelog']);
+ if ($changelog) {
+
+ // GETTING WARNINGS OF CHANGELOG
+ if($changelog['Changelog']['warnings']) {
+ $warnings_id = explode('|', $changelog['Changelog']['warnings']);
+ $warnings = array();
+ foreach ($warnings_id as $warning_id) {
+ $warnings[] = $warningsController->Warning->findById($warning_id);
+ }
+
+ // ADDING WARNINGS TO LIST
+ $warningsArray[$changelog['Changelog']['id']] = $warnings;
+ }
+
+ // GETTING LAST DECLARED VERSION IN CHANGELOG
+ if($changelog['Changelog']['versions']) {
+ $changelogVersions = explode('|', $changelog['Changelog']['versions']);
+ foreach ($changelogVersions as $changelogVersion) {
+ $changelogVersionArray = explode(';', $changelogVersion);
+ if(isset($changelogVersionArray[1])) {
+ $update = $changelogVersionArray[1];
+ if(!isset($lastUpdate) || $update > $lastUpdate) {
+ $lastUpdate = $update;
+ }
+ }
+ }
+
+ // ADDING UPDATE DATE TO LIST
+ if(isset($lastUpdate)) {
+ $lastUpdates[$appliance['Appliance']['id']] = $lastUpdate;
+ }
+ }
+
+ // ADDING CHANGELOG TO LIST
+ $changelogsArray[$appliance['Appliance']['id']] = $changelog;
+ }
+
+ // GETTING CUSTOMER
+ $applianceReferenceArray = explode("/",$appliance['Appliance']['reference']);
+ $customerAPId = substr($applianceReferenceArray[1],0,-11);
+ foreach($customers as $customer) {
+
+ // ADDING CUSTOMER TO LIST
+ if($customer['Customer']['ap_id'] === $customerAPId) {
+ $customersArray[$appliance['Appliance']['id']] = $customer;
+ }
+ }
+ }
+
+ // SENDING DATA TO VIEW
+ $this->set(array(
+ 'appliances' => $appliances,
+ '_serialize' => array('appliances')
+ ));
+ $this->set(array(
+ 'changelogsArray' => $changelogsArray,
+ '_serialize' => array('changelogsArray')
+ ));
+ $this->set(array(
+ 'customersArray' => $customersArray,
+ '_serialize' => array('customersArray')
+ ));
+ $this->set(array(
+ 'warningsArray' => $warningsArray,
+ '_serialize' => array('warningsArray')
+ ));
+ $this->set(array(
+ 'lastUpdates' => $lastUpdates,
+ '_serialize' => array('lastUpdates')
+ ));
+ }
+
+ public function view($id = null) {
+ if (!$id) {
+ throw new NotFoundException(__('Mauvaise instance applicative'));
+ }
+
+ $appliance = $this->Appliance->findById($id);
+ if (!$appliance) {
+ throw new NotFoundException(__('Mauvaise instance applicative'));
+ }
+ $this->set('appliance', $appliance);
+ }
+
+ public function add() {
+ if ($this->request->is('POST')) {
+ $this->Appliance->create();
+ if ($this->Appliance->save($this->request->data)) {
+ $this->Session->setFlash(__("L'instance applicative a été enregistrée."));
+ $this->redirect(array('action' => 'index'));
+ }
+ $this->Session->setFlash(__("Enregistrement de l'instance applicative impossible."));
+ }
+ }
+
+ public function edit($id = null) {
+ if (!$id) {
+ throw new NotFoundException(__('Instance applicative introuvable'));
+ }
+
+ $appliance = $this->Appliance->findById($id);
+ if (!$appliance) {
+ throw new NotFoundException(__('Instance applicative introuvable'));
+ }
+ $this->Appliance->id = $id;
+
+ // GETTING VERSION CONTROLLER AND VERSIONS
+ App::import('Controller', 'Versions');
+ $versionsController = new VersionsController;
+ $versions = $versionsController->Version->find('all');
+
+ // GETTING ALL AVAILABLE VERSIONS FOR THIS PRODUCT
+ $possibleVersions = array();
+ foreach($versions as $version) {
+ if($version['Version']['product'] == $this->Appliance->value('product')) {
+ $possibleVersions[$version['Version']['id']] = $version['Version']['value'];
+ }
+ }
+
+ // SENDING DATA TO VIEW
+ $this->set(array(
+ 'possibleVersions' => $possibleVersions,
+ '_serialize' => array('possibleVersions')
+ ));
+
+ // IN CASE OF A PUT REQUEST
+ if ($this->request->is(array('appliance', 'put'))) {
+
+ // GETTING CHANGELOG
+ $changelog = $this->Changelog->findById($this->Appliance->value('changelog'));
+ if (!$changelog) {
+ throw new NotFoundException(__('Changelog introuvable'));
+ }
+ $this->Changelog->id = $this->Appliance->value('changelog');
+
+ // GETTING VERSIONS OF APPLIANCE
+ if($this->Changelog->value('versions')) {
+ $changelogVersions = explode('|', $this->Changelog->value('versions'));
+ } else {
+ $changelogVersions = array();
+ }
+
+ // SAVING APPLIANCE
+ if ($this->Appliance->save($this->request->data)) {
+
+ // CHECKING VERSION ID TO ADD TO CHANGELOG
+ $possibleVersionsId = array_keys($possibleVersions);
+ if(in_array($this->request->data['Appliance']['version'], $possibleVersionsId)) {
+ $versionId = $this->request->data['Appliance']['version'];
+ }
+
+ // CHANGING AND SAVING CHANGELOG
+ if(isset($versionId)) {
+ $changelogVersions[] = $versionId.";".date('Y-m-d');
+ $this->Changelog->value('versions', implode('|', $changelogVersions));
+ $this->Changelog->save();
+ }
+
+ // REDIRECTION
+ $this->Session->setFlash(__("L'instance applicative a été mise à jour."));
+ $this->redirect(array('action' => 'index'));
+ }
+ $this->Session->setFlash(__("Mise à jour de l'instance applicative impossible."));
+ }
+
+ if (!$this->request->data) {
+ $this->request->data = $appliance;
+ }
+ }
+
+ public function delete($id) {
+
+ // DISABLE GET ACCESS ?
+ /*if ($this->request->is('get')) {
+ throw new MethodNotAllowedException();
+ }*/
+ $appliance = $this->Appliance->findById($id);
+ if (!$appliance) {
+ throw new NotFoundException(__('Instance applicative introuvable'));
+ }
+ if ($this->Appliance->delete($id)) {
+ $this->Session->setFlash(
+ __("L'instance applicative %s a été supprimée.", h($appliance['Appliance']['reference']))
+ );
+ $this->redirect(array('action' => 'index'));
+ }
+ }
+
+ // REST API
+ // TO-DO : DELETE OR USE, BUT DON'T LET IT ROT THERE
+
+ /*public function rest_get_all() {
+ $appliances = $this->Appliance->find('all');
+ $this->set(array(
+ 'appliances' => $appliances,
+ '_serialize' => array('appliances')
+ ));
+ }
+
+ public function rest_get($reference) {
+ $appliance = $this->Appliance->findByReference($reference);
+ $this->set(array(
+ 'appliance' => $appliance,
+ '_serialize' => array('appliance')
+ ));
+ }
+
+ public function rest_create() {
+ if ($this->Appliance->save($this->request->data)) {
+ $message = array(
+ 'text' => __('Saved'),
+ 'type' => 'success'
+ );
+ } else {
+ $message = array(
+ 'text' => __('Error'),
+ 'type' => 'error'
+ );
+ }
+ $this->set(array(
+ 'message' => $message,
+ '_serialize' => array('message')
+ ));
+ }
+
+ public function rest_update($reference) {
+ $this->Appliance->reference = $reference;
+ if ($this->Appliance->save($this->request->data)) {
+ $message = array(
+ 'text' => __('Saved'),
+ 'type' => 'success'
+ );
+ } else {
+ $message = array(
+ 'text' => __('Error'),
+ 'type' => 'error'
+ );
+ }
+ $this->set(array(
+ 'message' => $message,
+ '_serialize' => array('message')
+ ));
+ }
+
+ public function rest_delete($reference) {
+ if ($this->Appliance->delete($reference)) {
+ $message = array(
+ 'text' => __('Deleted'),
+ 'type' => 'success'
+ );
+ } else {
+ $message = array(
+ 'text' => __('Error'),
+ 'type' => 'error'
+ );
+ }
+ $this->set(array(
+ 'message' => $message,
+ '_serialize' => array('message')
+ ));
+ }*/
+ }