+++ /dev/null
-<?php
- class InstancesController extends AppController {
- public $helpers = array('Html', 'Form');
- public $components = array('RequestHandler');
- public $uses = array('Instance', 'Historique');
-
- public function index() {
-
- // GETTING CONTROLLERS
- App::import('Controller', 'Alertes');
- $alertesController = new AlertesController;
- App::import('Controller', 'Historiques');
- $historiquesController = new HistoriquesController;
- App::import('Controller', 'Clients');
- $clientsController = new ClientsController;
-
- // GETTING ALL APPLIANCES
- $instances = $this->Instance->find('all');
-
- // GETTING ALL CLIENTS
- $totalClients = $clientsController->Client->find('all');
-
- // GETTING INFORMATION FOR EACH APPLIANCE
- $historiques = array();
- $totalAlertes = array();
- $clients = array();
- $last_updates = array();
- foreach($instances as $instance) {
- // GETTING CHANGELOG
- $historique = $historiquesController->Historique->findById($instance['Instance']['historique']);
- if ($historique) {
- // GETTING WARNINGS OF CHANGELOG
- if($historique['Historique']['alertes']) {
- $alertes_id = explode('|', $historique['Historique']['alertes']);
- $alertes = array();
- foreach ($alertes_id as $alerte_id) {
- $alertes[] = $alertesController->Alerte->findById($alerte_id);
- }
- // ADDING WARNING TO LIST
- $totalAlertes[$historique['Historique']['id']] = $alertes;
- }
- // GETTING LAST VERSION DECLARED OF CHANGELOG
- if($historique['Historique']['versions']) {
- $infos_versions = explode('|', $historique['Historique']['versions']);
- $last_update = $historique['Historique']['installation'];
- foreach ($infos_versions as $infos_version) {
- $infos_version_array = explode(';', $infos_version);
- if(isset($infos_version_array[1])) {
- $update = $infos_version_array[1];
- if($update > $last_update) {
- $last_update = $update;
- }
- }
- }
- // ADDING UPDATE DATE TO LIST
- $last_updates[$instance['Instance']['id']] = $last_update;
- }
- // ADDING CHANGELOG TO LIST
- $historiques[$instance['Instance']['id']] = $historique;
- }
-
- // GETTING CUSTOMER
- $refInstanceSansProduit = explode("/",$instance['Instance']['reference']);
- $idAPClient = substr($refInstanceSansProduit[1],0,-11);
- foreach($totalClients as $client) {
- // ADDING CUSTOMER TO LIST
- if($client['Client']['identifiant_AP'] === $idAPClient) {
- $clients[$instance['Instance']['id']] = $client;
- }
- }
- }
-
- // SENDING DATA TO VIEW
- $this->set(array(
- 'instances' => $instances,
- '_serialize' => array('instances')
- ));
- $this->set(array(
- 'historiques' => $historiques,
- '_serialize' => array('historiques')
- ));
- $this->set(array(
- 'clients' => $clients,
- '_serialize' => array('clients')
- ));
- $this->set(array(
- 'totalAlertes' => $totalAlertes,
- '_serialize' => array('totalAlertes')
- ));
- $this->set(array(
- 'last_updates' => $last_updates,
- '_serialize' => array('last_updates')
- ));
- }
-
- public function view($id = null) {
- if (!$id) {
- throw new NotFoundException(__('Mauvaise instance'));
- }
-
- $instance = $this->Instance->findById($id);
- if (!$instance) {
- throw new NotFoundException(__('Mauvaise instance'));
- }
- $this->set('instance', $instance);
- }
-
- public function add() {
- if ($this->request->is('POST')) {
- $this->Instance->create();
- if ($this->Instance->save($this->request->data)) {
- $this->Session->setFlash(__("L'instance a été enregistrée."));
- return $this->redirect(array('action' => 'index'));
- }
- $this->Session->setFlash(__("Enregistrement de l'instance impossible."));
- }
- }
-
- public function edit($id = null) {
- if (!$id) {
- throw new NotFoundException(__('Instance introuvable'));
- }
-
- $instance = $this->Instance->findById($id);
- if (!$instance) {
- throw new NotFoundException(__('Instance introuvable'));
- }
- $this->Instance->id = $id;
-
- // GETTING PRODUCT CONTROLLER AND PRODUCTS
- App::import('Controller', 'Produits');
- $produitsController = new ProduitsController;
- $produits = $produitsController->Produit->find('all');
-
- // GETTING VERSION CONTROLLER AND VERSIONS
- App::import('Controller', 'Versions');
- $versionsController = new VersionsController;
- $versions = $versionsController->Version->find('all');
-
- // GETTING PRODUCT ID FOR THIS APPLIANCE
- foreach($produits as $produit) {
- if($produit['Produit']['nom'] == $this->Instance->value('nom')) {
- $idProduit = $produit['Produit']['id'];
- break;
- }
- }
-
- // GETTING ALL AVAILABLE VERSIONS FOR THIS PRODUCT
- $versionsPossibles = array();
- foreach($versions as $version) {
- if($version['Version']['produit'] == $idProduit) {
- $versionsPossibles[] = $version['Version']['valeur'];
- }
- }
-
- // SENDING DATA TO VIEW
- $this->set(array(
- 'versionsPossibles' => $versionsPossibles,
- '_serialize' => array('versionsPossibles')
- ));
-
- // IN CASE OF A PUT REQUEST
- if ($this->request->is(array('instance', 'put'))) {
-
- // GETTING CHANGELOG
- $historique = $this->Historique->findById($this->Instance->value('historique'));
- if (!$historique) {
- throw new NotFoundException(__('Historique introuvable'));
- }
- $this->Historique->id = $this->Instance->value('historique');
-
- // GETTING VERSIONS OF APPLIANCE
- if($this->Historique->value('versions')) {
- $historiqueVersions = explode('|', $this->Historique->value('versions'));
- } else {
- $historiqueVersions = array();
- }
-
- // SAVING APPLIANCE
- if ($this->Instance->save($this->request->data)) {
-
- // GETTING VERSION TO ADD TO CHANGELOG
- foreach($versionsPossibles as $versionPossible) {
- if($versionPossible['Version']['valeur'] == $this->Instance->value('version')) {
- $idVersion = $versionPossible['Version']['id'];
- break;
- }
- }
-
- // CHANGING AND SAVING CHANGELOG
- $historiqueVersions[] = $idVersion.";".date('Y-m-d');
- $this->Historique->value('versions', implode('|', $historiqueVersions));
- $this->Historique->save();
- $this->Session->setFlash(__("L'instance a été mise à jour."));
- return $this->redirect(array('action' => 'index'));
- }
- $this->Session->setFlash(__("Mise à jour de l'instance impossible."));
- }
-
- if (!$this->request->data) {
- $this->request->data = $instance;
- }
- }
-
- public function delete($id) {
- /*if ($this->request->is('get')) {
- throw new MethodNotAllowedException();
- }*/
- $instance = $this->Instance->findById($id);
- if (!$instance) {
- throw new NotFoundException(__('Instance introuvable'));
- }
- if ($this->Instance->delete($id)) {
- $this->Session->setFlash(
- __("L'instance %s a été supprimée.", h($instance['Instance']['reference']))
- );
- return $this->redirect(array('action' => 'index'));
- }
- }
-
- // REST API
-
- /*public function rest_get_all() {
- $instances = $this->Instance->find('all');
- $this->set(array(
- 'instances' => $instances,
- '_serialize' => array('instances')
- ));
- }
-
- public function rest_get($reference) {
- $instance = $this->Instance->findByReference($reference);
- $this->set(array(
- 'instance' => $instance,
- '_serialize' => array('instance')
- ));
- }
-
- public function rest_create() {
- if ($this->Instance->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->Instance->reference = $reference;
- if ($this->Instance->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->Instance->delete($reference)) {
- $message = array(
- 'text' => __('Deleted'),
- 'type' => 'success'
- );
- } else {
- $message = array(
- 'text' => __('Error'),
- 'type' => 'error'
- );
- }
- $this->set(array(
- 'message' => $message,
- '_serialize' => array('message')
- ));
- }*/
- }
-?>