ProjetsController.php 10.4 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 ( in_array($alias_name, ['ChefSciences','ChefProjets']) ) return 'Users';
        //if ($alias_name == 'ChefProjets') return 'Users';
        return $alias_name;
    }
    
    /**
     * Index method
     *
     * @return \Cake\Http\Response|null
     */
    public function index()
    {
        $lab_website_urls = ($this->confLabinvent->labNameShort != 'IRAP') ?
        []
             :
        [
            'Projets Instrumentaux' => 'https://www.irap.omp.eu/sedoo-project-tag/projets_instrumentaux',
            'Projets Scientifiques' => 'https://www.irap.omp.eu/sedoo-project-tag/projets-scientifiques',
            'Projets R&T' => 'https://www.irap.omp.eu/sedoo-project-tag/rt',
            'Projets financés (ANR, Europe...)' => 'https://www.irap.omp.eu/observation-instrumentation/projets/projets-finances',
        ];
        
        $this->set(compact('lab_website_urls'));
        
        $sortWhitelist = [
            'nom' => 'nom',
            'groupes_thematique_id' => 'GroupesThematiques.nom',
            'chef_science_id' => 'ChefSciences.nom',
            'chef_projet_id' => 'ChefProjets.nom',
            'date_start' => 'date_start',
            'date_stop' => 'date_stop',
        ];
        
        $contains = ['GroupesThematiques', 'ChefSciences', 'ChefProjets'];
        
        $this->index_generic(
            'projets',
            [
                'nom'=>[],
                //'sur_categorie_id'=>['nice_name'=>'Domaine', 'contained_entity_name'=>'sur_category', 'controller_name'=>'SurCategories'],
                'groupes_thematique_id'=>[
                    //'nice_name'=>'Groupe thématique', 
                    'contained_entity_name'=>'groupes_thematique', 
                    'controller_name'=>'GroupesThematiques',
                    //'sort_name' => 'GroupesThematiques.nom'
                ],
                //'user_id'=>[
                'chef_science_id'=>[
                    'nice_name'=>'Responsable Scientifique',
                    'contained_entity_name'=>'chef_science',
                    //'controller_name'=>'ChefSciences',
                    'controller_name'=>'Users',
                    //'sort_name' => 'ChefSciences.nom'
                ],
                'chef_projet_id'=>[
                    'nice_name'=>'Chef Projet',
                    'contained_entity_name'=>'chef_projet',
                    //'controller_name'=>'ChefSciences',
                    'controller_name'=>'Users',
                    //'sort_name' => 'ChefProjets.nom'
                ],
                'date_start'=>['nice_name'=>'Date début'],
                'date_stop'=>['nice_name'=>'Date fin'],
                
            ],
            $contains,
            true, true,
            'nom',
            [],
            $sortWhitelist 
        );

        /*
        // https://book.cakephp.org/3/fr/controllers/components/pagination.html
        $this->paginate = [
            //'contain' => ['GroupesThematiques', 'Pis', 'Pms']
            //'contain' => ['GroupesThematiques', 'Users']
            'contain' => ['GroupesThematiques', 'ChefSciences', 'ChefProjets'],
            'sortWhitelist' => [
                'nom',
                'GroupesThematiques.nom',
                'ChefSciences.nom',
                'ChefProjets.nom',
                'date_start',
                'date_stop',
            ],
            'limit' => 20,
            /S
             'order' => [
             'Users.nom' => 'asc'
             ]
             S/
        ];
        
        $projets = $this->paginate();
        // ce qui équivaut à :
        //s$projets = $this->paginate($this->Projets);
        
        // Si on est sur l'instance de l'IRAP (InventIrap)
        // on affiche l'url vers la page des Groupes métiers sur le site web de l'IRAP
        */
    }

    /**
     * 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->delete_generic($id);
        /*
        $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']);
        */
    }
    
    
}