--- /dev/null
+<?php
+ class InstancesController extends AppController {
+ public $helpers = array('Html', 'Form');
+ public $components = array('RequestHandler');
+
+ public function index() {
+ // IMPORT DES CONTROLLEURS
+ App::import('Controller', 'Alertes');
+ $alertesController = new AlertesController;
+ App::import('Controller', 'Historiques');
+ $historiquesController = new HistoriquesController;
+
+ // RÉCUPÉRATION DES INSTANCES
+ $instances = $this->Instance->find('all');
+
+ // RECUPERATION DE L'HISTORIQUE DE CHAQUE INSTANCE
+ $historiques = array();
+ $totalAlertes = array();
+ foreach($instances as $instance) {
+ $historique = $historiquesController->Historique->findById($instance['Instance']['historique']);
+ if (!$historique) {
+ throw new NotFoundException(__('Historique introuvable'));
+ }
+ // RÉCUPÉRATION DES ALERTES DE L'HISTORIQUE
+ if($historique['Historique']['alertes']) {
+ $alertes_id = explode('|', $historique['Historique']['alertes']);
+ $alertes = array();
+ foreach ($alertes_id as $alerte_id) {
+ $alertes[] = $alertesController->Alerte->findById($alerte_id);
+ }
+ // AJOUT DANS LA LISTE DES ALERTES
+ $totalAlertes[$historique['Historique']['id']] = $alertes;
+ }
+ // AJOUT DANS LA LISTE DES HISTORIQUES
+ $historiques[$instance['Instance']['id']] = $historique;
+
+ }
+
+ // ENVOI DES DONNÉES À LA VUE
+ $this->set(array(
+ 'instances' => $instances,
+ '_serialize' => array('instances')
+ ));
+ $this->set(array(
+ 'historiques' => $historiques,
+ '_serialize' => array('historiques')
+ ));
+ $this->set(array(
+ 'totalAlertes' => $totalAlertes,
+ '_serialize' => array('totalAlertes')
+ ));
+ }
+
+ 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('instance')) {
+ $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'));
+ }
+
+ if ($this->request->is(array('instance', 'put'))) {
+ $this->Instance->id = $id;
+ if ($this->Instance->save($this->request->data)) {
+ $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();
+ }
+
+ if ($this->Instance->delete($id)) {
+ $this->Session->setFlash(
+ __("L'instance d'id: %s a été supprimée.", h($id))
+ );
+ return $this->redirect(array('action' => 'index'));
+ }
+ }
+
+ // API REST
+
+ /*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')
+ ));
+ }*/
+ }
+?>