* Copyright 2016 Nicolas Goaziou * * This file is part of CHD Gestion. * * CHD Gestion is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * CHD Gestion is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with CHD Gestion. If not, see . **/ namespace App\Controller; use App\Controller\AppController; /** * Equipements Controller * * @property \App\Model\Table\EquipementsTable $Equipements */ class EquipementsController extends AppController { public function initialize() { parent::initialize(); if ($this->request->action === 'index') { $this->loadComponent('Paginator', [ 'limit' => 15, 'order' => [ 'Equipements.id' => 'DESC' ] ]); $this->loadComponent('Search.Prg'); } } /** * Index method * * @return void */ public function index() { $this->paginate = [ 'contain' => ['Services', 'Ipmgmt', 'EquipementModeles', 'EquipementModes', 'Relais'] ]; $query = $this->Equipements ->find('search', $this->Equipements->filterParams($this->request->query)); $this->set('equipements', $this->paginate($query)); $this->set('_serialize', ['equipements']); $this->loadModel('EquipementModeles'); $this->set('equipementModeles', $this->EquipementModeles->find('list')->toArray()); $this->loadModel('Relais'); $this->set('relais', $this->Relais->find('list')->toArray()); } /** * View method * * @param string|null $id Equipement id. * @return \Cake\Network\Response|null * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. */ public function view($id = null) { $equipement = $this->Equipements->get($id, [ 'contain' => ['Services', 'Ipmgmt', 'EquipementModeles', 'EquipementModes', 'Relais'] ]); $this->set('equipement', $equipement); $this->set('_serialize', ['equipement']); } /** * Add method * * @return void Redirects on successful add, renders view otherwise. */ public function add() { $equipement = $this->Equipements->newEntity(); if ($this->request->is('post')) { $equipement = $this->Equipements->patchEntity($equipement, $this->request->data); if ($this->Equipements->save($equipement)) { $this->Flash->success(__('The equipement has been saved.')); return $this->redirect(['action' => 'index']); } else { $this->Flash->error(__('The equipement could not be saved. Please, try again.')); } } $equipements = $this->Equipements->Equipements->find('list')->order('mac'); $services = $this->Equipements->Services->find('list')->order(['service_type_id', 'adherent_id', 'id']); $ipmgmt = $this->Equipements->Ipmgmt->find('list')->notMatching('Equipements'); $ipmgmt->order($ipmgmt->newExpr()->add(['INET_ATON(ip4)'])); $equipementModeles = $this->Equipements->EquipementModeles->find('list'); $equipementModes = $this->Equipements->EquipementModes->find('list'); $relais = $this->Equipements->Relais->find('list'); $this->set(compact('equipement', 'equipements', 'services', 'ipmgmt', 'equipementModeles', 'equipementModes', 'relais')); $this->set('_serialize', ['equipement']); } /** * Edit method * * @param string|null $id Equipement id. * @return void Redirects on successful edit, renders view otherwise. * @throws \Cake\Network\Exception\NotFoundException When record not found. */ public function edit($id = null) { $equipement = $this->Equipements->get($id, [ 'contain' => [] ]); if ($this->request->is(['patch', 'post', 'put'])) { $equipement = $this->Equipements->patchEntity($equipement, $this->request->data); if ($this->Equipements->save($equipement)) { $this->Flash->success(__('The equipement has been saved.')); return $this->redirect(['action' => 'index']); } else { $this->Flash->error(__('The equipement could not be saved. Please, try again.')); } } $equipements = $this->Equipements->Equipements->find('list', array('order' => array('mac' => 'asc'))); $services = $this->Equipements->Services->find('list'); // IPmgmt list : own IP + unused IP // XXX Cake bug ->orWhere(['Ipmgmt.ip4' => $equipement->ipmgmt_id]) does a AND WHERE after ->notMatching('Equipements') so forced to use leftJoinWith() $ipmgmt = $this->Equipements->Ipmgmt->find('list')->distinct()->leftJoinWith('Equipements'); $ipmgmt->where(['Equipements.id IS' => NULL])->orWhere(['Ipmgmt.ip4' => $equipement->ipmgmt_id]); $ipmgmt->order($ipmgmt->newExpr()->add(['INET_ATON(ip4)'])); $equipementModeles = $this->Equipements->EquipementModeles->find('list'); $equipementModes = $this->Equipements->EquipementModes->find('list'); $relais = $this->Equipements->Relais->find('list'); $this->set(compact('equipement', 'equipements', 'services', 'ipmgmt', 'equipementModeles', 'equipementModes', 'relais')); $this->set('_serialize', ['equipement']); } }