MaterielsController.php 5.04 KB
<?php
namespace App\Controller;

use App\Controller\AppController;

/**
 * Materiels Controller
 *
 * @property \App\Model\Table\MaterielsTable $Materiels
 */
class MaterielsController extends AppController
{

    /**
     * Index method
     *
     * @return \Cake\Network\Response|null
     */
    public function index()
    {
        $this->paginate = [
            'contain' => ['SurCategories', 'Categories', 'SousCategories', 'GroupesThematiques', 'GroupesMetiers', 'Organismes', 'Sites']
        ];
        $materiels = $this->paginate($this->Materiels);

        $this->set(compact('materiels'));
        $this->set('_serialize', ['materiels']);
    }

    /**
     * View method
     *
     * @param string|null $id Materiel id.
     * @return \Cake\Network\Response|null
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null)
    {
        $materiel = $this->Materiels->get($id, [
            'contain' => ['SurCategories', 'Categories', 'SousCategories', 'GroupesThematiques', 'GroupesMetiers', 'Organismes', 'Sites', 'Documents', 'Emprunts', 'Suivis']
        ]);

        $this->set('materiel', $materiel);
        $this->set('_serialize', ['materiel']);
    }

    /**
     * Add method
     *
     * @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $materiel = $this->Materiels->newEntity();
        if ($this->request->is('post')) {
            $materiel = $this->Materiels->patchEntity($materiel, $this->request->data);
            if ($this->Materiels->save($materiel)) {
                $this->Flash->success(__('The materiel has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The materiel could not be saved. Please, try again.'));
            }
        }
        $surCategories = $this->Materiels->SurCategories->find('list', ['limit' => 200]);
        $categories = $this->Materiels->Categories->find('list', ['limit' => 200]);
        $sousCategories = $this->Materiels->SousCategories->find('list', ['limit' => 200]);
        $groupesThematiques = $this->Materiels->GroupesThematiques->find('list', ['limit' => 200]);
        $groupesMetiers = $this->Materiels->GroupesMetiers->find('list', ['limit' => 200]);
        $organismes = $this->Materiels->Organismes->find('list', ['limit' => 200]);
        $sites = $this->Materiels->Sites->find('list', ['limit' => 200]);
        $this->set(compact('materiel', 'surCategories', 'categories', 'sousCategories', 'groupesThematiques', 'groupesMetiers', 'organismes', 'sites'));
        $this->set('_serialize', ['materiel']);
    }

    /**
     * Edit method
     *
     * @param string|null $id Materiel id.
     * @return \Cake\Network\Response|void Redirects on successful edit, renders view otherwise.
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    public function edit($id = null)
    {
        $materiel = $this->Materiels->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $materiel = $this->Materiels->patchEntity($materiel, $this->request->data);
            if ($this->Materiels->save($materiel)) {
                $this->Flash->success(__('The materiel has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The materiel could not be saved. Please, try again.'));
            }
        }
        $surCategories = $this->Materiels->SurCategories->find('list', ['limit' => 200]);
        $categories = $this->Materiels->Categories->find('list', ['limit' => 200]);
        $sousCategories = $this->Materiels->SousCategories->find('list', ['limit' => 200]);
        $groupesThematiques = $this->Materiels->GroupesThematiques->find('list', ['limit' => 200]);
        $groupesMetiers = $this->Materiels->GroupesMetiers->find('list', ['limit' => 200]);
        $organismes = $this->Materiels->Organismes->find('list', ['limit' => 200]);
        $sites = $this->Materiels->Sites->find('list', ['limit' => 200]);
        $this->set(compact('materiel', 'surCategories', 'categories', 'sousCategories', 'groupesThematiques', 'groupesMetiers', 'organismes', 'sites'));
        $this->set('_serialize', ['materiel']);
    }

    /**
     * Delete method
     *
     * @param string|null $id Materiel id.
     * @return \Cake\Network\Response|null Redirects to index.
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $materiel = $this->Materiels->get($id);
        if ($this->Materiels->delete($materiel)) {
            $this->Flash->success(__('The materiel has been deleted.'));
        } else {
            $this->Flash->error(__('The materiel could not be deleted. Please, try again.'));
        }
        return $this->redirect(['action' => 'index']);
    }
}