ProjetsController.php 7.14 KB
<?php
namespace App\Controller;

use App\Controller\AppController;

/**
 * Projets Controller
 *
 * @property \App\Model\Table\ProjetsTable $Projets
 *
 * @method \App\Model\Entity\Projet[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
 */
class ProjetsController extends AppController {
    
    // Jolis labels pour chaque champ de l'entité
    //@Override
    protected $nice_field_labels = [
        'groupes_thematique_id' => 'Groupe thématique',
        'chef_science_id' => 'Responsable scientifique',
        'chef_projet_id' => 'Chef de projet',
        'date_start' => 'Date début',
        'date_stop' => 'Date fin',
    ];
    
    /*
    protected function getControllerNameForFK($fk_name) {
        if ($fk_name=='chef_science_id') return 'PIs';
        if ($fk_name=='chef_projet_id') return 'PMs';
        return parent::getControllerNameForFK($fk_name);
    }
    */

    protected function getRealControllerNameForAlias($alias_name) {
        if ($alias_name == 'ChefSciences') return 'Users';
        if ($alias_name == 'ChefProjets') return 'Users';
        return $alias_name;
    }
    
    /**
     * Index method
     *
     * @return \Cake\Http\Response|null
     */
    public function index()
    {
        $this->paginate = [
            //'contain' => ['GroupesThematiques', 'Users']
            //'contain' => ['GroupesThematiques', 'Pis', 'Pms']
            'contain' => ['GroupesThematiques', 'ChefSciences', 'ChefProjets']
        ];
        $projets = $this->paginate($this->Projets);

        $this->set(compact('projets'));
    }

    /**
     * View method
     *
     * @param string|null $id Projet id.
     * @return \Cake\Http\Response|null
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null)
    {
        /*
        $projet = $this->Projets->get($id, [
            //'contain' => ['GroupesThematiques', 'Users', 'Materiels']
            'contain' => ['GroupesThematiques', 'Pis', 'Pms', 'Materiels']
        ]);
        debug($projet); exit;
        */
        
        $child_entity_types = [
            'Materiels',
        ];
        //return parent::view($id, $associated_entity_types);
        return $this->view_generic($id, $child_entity_types);
        
        /*
        $projet = $this->Projets->get($id, [
            //'contain' => ['GroupesThematiques', 'Users', 'Materiels']
            'contain' => ['GroupesThematiques', 'Pis', 'Pms', 'Materiels']
        ]);

        $this->set('projet', $projet);
        */
    }

    /**
     * Add method
     *
     * @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
     */
    public function add() {
        $this->add_or_edit(TRUE, null);
        /*
         * ORIGINAL GÉNÉRÉ PAR CAKE BAKE controller Projets :
        $projet = $this->Projets->newEntity();
        if ($this->request->is('post')) {
            $projet = $this->Projets->patchEntity($projet, $this->request->getData());
            if ($this->Projets->save($projet)) {
                $this->Flash->success(__('The projet has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The projet could not be saved. Please, try again.'));
        }
        $groupesThematiques = $this->Projets->GroupesThematiques->find('list', ['limit' => 200]);
        $users = $this->Projets->Users->find('list', ['limit' => 200]);
        $this->set(compact('projet', 'groupesThematiques', 'users'));
        */
    }

    /**
     * Edit method
     *
     * @param string|null $id Projet id.
     * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise.
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function edit($id = null) {
        $this->add_or_edit(FALSE, $id);
        /*
         * ORIGINAL GÉNÉRÉ PAR CAKE BAKE controller Projets :
        $projet = $this->Projets->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $projet = $this->Projets->patchEntity($projet, $this->request->getData());
            if ($this->Projets->save($projet)) {
                $this->Flash->success(__('The projet has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The projet could not be saved. Please, try again.'));
        }
        $groupesThematiques = $this->Projets->GroupesThematiques->find('list', ['limit' => 200]);
        $users = $this->Projets->Users->find('list', ['limit' => 200]);
        $this->set(compact('projet', 'groupesThematiques', 'users'));
        */
    }
    
    protected function add_or_edit(
        $IS_ADD, $id=null, 
        $errors=null, $entity_name=null, 
        array $associated_entities=[], 
        $with_parent=false) 
    {
        $IS_EDIT = !$IS_ADD;
        $controller = $this->request->getParam('controller'); // 'Projets'
        $this->myDebug("step 3: $controller .add_or_edit()");
        // ORIGINAL GÉNÉRÉ PAR CAKE BAKE controller Projets :
        //$projet = $IS_ADD ? $this->Projets->newEntity() : $this->Projets->get($id, ['contain' => []]);
        $entity = $IS_ADD ? $this->$controller->newEntity() : $this->$controller->get($id, ['contain' => $associated_entities]);
        $allowed_request_types = $IS_ADD ? 'post' : ['patch', 'post', 'put'];
        if ($this->request->is($allowed_request_types)) {
            $entity = $this->$controller->patchEntity($entity, $this->request->getData());
            if ($this->$controller->save($entity)) {
                $this->Flash->success(__('Le projet a bien été sauvegardé'));
                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__("Le projet n'a pas pu être sauvegardé ! Essayez à nouveau"));
        }
        $groupes_thematiques = $this->$controller->GroupesThematiques->find('list', ['limit' => 200]);
        $pis = $this->$controller->ChefSciences->find('list', ['limit' => 2000]);
        //$pms = $pis;
        $pms = $this->$controller->ChefProjets->find('list', ['limit' => 2000]);
        //$pis = $this->$controller->Users->find('list', ['limit' => 200]);
        //$pis = $this->$controller->Pis->find('list', ['limit' => 200]);
        //$pis = $this->$controller->Pis->find('list', ['limit' => 2000]);
        $this->set(compact('IS_ADD', 'entity', 'groupes_thematiques', 'pis', 'pms'));
    }

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

        return $this->redirect(['action' => 'index']);
    }
}