CategoriesController.php 7.4 KB
<?php
namespace App\Controller;

use App\Controller\AppController;
use Cake\ORM\TableRegistry;

/**
 * Categories Controller
 *
 * @property \App\Model\Table\CategoriesTable $Categories
 */
class CategoriesController extends AppController
{

    protected function getArticle()
    {
        return "La";
    }

    /**
     *
     * @param
     *            $user
     * @return boolean Give authorization for categories
     *        
     */
    public function isAuthorized($user)
    {
        // $configuration = $this->confLabinvent;
        // $role = TableRegistry::get('Users')->find()->where(['username' => $user[$configuration->authentificationType_ldap][0]])->first()['role'];
        // $action = $this->request->getAttribute('params')['action'];
        $action = $this->getActionPassed();
        $role = $this->getUserRole($user);
        
        // Actions autorisées pour tous les profils
        // if (in_array($action, ['getBySurCategorie', 'getAll', 'view', 'index'])) {
        if (in_array($action, [
            'getBySurCategorie',
            'getAll'
        ])) {
            return true;
        }
        
        /*
         * // Super-Admin peut accéder à chaque action
         * if($role == 'Super Administrateur') return true;
         */
        
        /*
         * // Administration peut ajouter, supprimer ou modifier une categorie
         * //if($role == 'Administration' && in_array($action,['add','delete','edit'])) return true;
         * if( in_array($action,['add','delete','edit'])) {
         * //if ($role == 'Administration') return true;
         * if ($this->USER_IS_ADMIN_AT_LEAST()) return true;
         * // Les autres n'y ont pas accès
         * return false;
         * }
         */
        
        // Par défaut
        // return false;
        // return parent::isAuthorized($user);
        
        return $this->isAuthorizedCommons($user);
    }

    /**
     * Index method
     *
     * @return \Cake\Network\Response|null
     */
    public function index()
    {
        $this->paginate = [
            'contain' => [
                'SurCategories'
            ]
        ];
        $categories = $this->paginate($this->Categories);
        
        $this->set(compact('categories'));
        $this->set('_serialize', [
            'categories'
        ]);
    }

    /**
     * View method
     *
     * @param string|null $id
     *            Category id.
     * @return \Cake\Network\Response|null
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null)
    {
        $category = $this->Categories->get($id, [
            'contain' => [
                'SurCategories'
            ]
        ]);
        $sousCategories = TableRegistry::get('SousCategories')->find('all')->where([
            'categorie_id =' => $id
        ]);
        $this->set('sousCategories', $sousCategories);
        
        $materiels = TableRegistry::get('Materiels')->find('all')->where([
            'categorie_id =' => $id
        ]);
        $this->set('materiels', $materiels);
        
        $this->set('category', $category);
        $this->set('_serialize', [
            'category'
        ]);
    }

    /**
     * Add method
     *
     * @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $category = $this->Categories->newEntity();
        if ($this->request->is('post')) {
            $category = $this->Categories->patchEntity($category, $this->request->getData());
            if ($this->Categories->save($category)) {
                $this->Flash->success(__('La catégorie a bien été ajouté.'));
                return $this->redirect([
                    'action' => 'view',
                    $category->id
                ]);
            } else {
                $this->Flash->error(__('La catégorie n\'a pas pu être ajouté.'));
            }
        }
        $surCategories = $this->Categories->SurCategories->find('list', [
            'keyField' => 'id',
            'valueField' => 'nom'
        ]);
        
        $this->set(compact('category', 'surCategories'));
        $this->set('_serialize', [
            'category'
        ]);
    }

    /**
     * Edit method
     *
     * @param string|null $id
     *            Category 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)
    {
        $category = $this->Categories->get($id, [
            'contain' => []
        ]);
        if ($this->request->is([
            'patch',
            'post',
            'put'
        ])) {
            $category = $this->Categories->patchEntity($category, $this->request->getData());
            if ($this->Categories->save($category)) {
                $this->Flash->success(__('La catégorie a bien été édité.'));
                return $this->redirect([
                    'action' => 'view',
                    $id
                ]);
            } else {
                $this->Flash->error(__('La catégorie n\'a pas pu être édité.'));
            }
        }
        $surCategories = $this->Categories->SurCategories->find('list', [
            'keyField' => 'id',
            'valueField' => 'nom'
        ]);
        
        $this->set(compact('category', 'surCategories'));
        $this->set('_serialize', [
            'category'
        ]);
    }

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

    public function getBySurCategorie()
    {
        if ($this->request->getData('s_sur_categorie_id') !== null)
            $sur_categorie_id = $this->request->getData('s_sur_categorie_id');
        else
            $sur_categorie_id = $this->request->getData('sur_categorie_id');
        $categories = $this->Categories->find('list', [
            'conditions' => [
                'Categories.sur_categorie_id' => $sur_categorie_id
            ],
            'order' => [
                'Categories.nom'
            ],
            'recursive' => - 1,
            'keyField' => 'id',
            'valueField' => 'nom'
        ]);
        $this->set('categories', $categories);
        $this->viewBuilder()->layout = 'ajax';
    }

    // called from view ADD/EDIT (scaffold.form.ctp) on domain select change (javascript event)
    public function getAll()
    {
        $categories = $this->Categories->find('list', [
            'order' => [
                'Categories.nom'
            ],
            'recursive' => - 1,
            'keyField' => 'id',
            'valueField' => 'nom'
        ]);
        $this->set('categories', $categories);
        $this->viewBuilder()->layout = 'ajax';
    }
}