DocumentsController.php 3.73 KB
<?php
namespace App\Controller;

use App\Controller\AppController;

/**
 * Documents Controller
 *
 * @property \App\Model\Table\DocumentsTable $Documents
 */
class DocumentsController extends AppController
{

    /**
     * Index method
     *
     * @return \Cake\Network\Response|null
     */
    public function index()
    {
        $this->paginate = [
            'contain' => ['Materiels', 'Suivis']
        ];
        $documents = $this->paginate($this->Documents);

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

    /**
     * View method
     *
     * @param string|null $id Document id.
     * @return \Cake\Network\Response|null
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null)
    {
        $document = $this->Documents->get($id, [
            'contain' => ['Materiels', 'Suivis']
        ]);

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

    /**
     * Add method
     *
     * @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $document = $this->Documents->newEntity();
        if ($this->request->is('post')) {
            $document = $this->Documents->patchEntity($document, $this->request->data);
            if ($this->Documents->save($document)) {
                $this->Flash->success(__('Le fichier a bien été ajouté.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('Le fichier n\'a pas pu être ajouté.'));
            }
        }
        $materiels = $this->Documents->Materiels->find('list', ['limit' => 200]);
        $suivis = $this->Documents->Suivis->find('list', ['limit' => 200]);
        $this->set(compact('document', 'materiels', 'suivis'));
        $this->set('_serialize', ['document']);
    }

    /**
     * Edit method
     *
     * @param string|null $id Document 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)
    {
        $document = $this->Documents->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $document = $this->Documents->patchEntity($document, $this->request->data);
            if ($this->Documents->save($document)) {
                $this->Flash->success(__('Le fichier a bien été édité.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('Le fichier n\'a pas pu être édité.'));
            }
        }
        $materiels = $this->Documents->Materiels->find('list', ['limit' => 200]);
        $suivis = $this->Documents->Suivis->find('list', ['limit' => 200]);
        $this->set(compact('document', 'materiels', 'suivis'));
        $this->set('_serialize', ['document']);
    }

    /**
     * Delete method
     *
     * @param string|null $id Document 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']);
        $document = $this->Documents->get($id);
        if ($this->Documents->delete($document)) {
            $this->Flash->success(__('Le fichier a bien été supprimé.'));
        } else {
            $this->Flash->error(__('Le fichier n\'a pas pu être supprimé.'));
        }
        return $this->redirect(['action' => 'index']);
    }
}