From c6900c634c789a97025d63e2a036346e7b9086a2 Mon Sep 17 00:00:00 2001 From: Etienne Pallier Date: Mon, 5 Oct 2020 23:26:30 +0200 Subject: [PATCH] refactorisation de la vue 'view' pour toutes les 'autres' entités --- CHANGES.txt | 4 ++++ src/Controller/AppController.php | 177 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------ src/Controller/CategoriesController.php | 16 ++++++++++++++++ src/Controller/MaterielsController.php | 9 +++++++-- src/Controller/SurCategoriesController.php | 57 +++++++++++++++++++++++++++++++++++---------------------- src/Controller/UsersController.php | 10 ++++++++++ src/Model/Table/SurCategoriesTable.php | 19 +++++++++++++++++-- src/Template/Categories/view.ctp | 157 ++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------------------------- src/Template/Common/view.ctp | 53 +++++++++++++++++++++++++++++++---------------------- src/Template/Materiels/view.ctp | 4 ++-- src/View/Helper/MyHelperHelper.php | 2 +- 11 files changed, 324 insertions(+), 184 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 7026f07..ff783d5 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -119,6 +119,10 @@ Outre ces changements, voici d'autres changements importants : ======= CHANGES ======= +refactorisation de la vue 'view' pour toutes les 'autres' entités + +=> dans AppController:view_generic() + ------- 05/10/2020 v4.104.5-3.7.9 (EP) - (b) bugfix UPDATE script => les requetes sql n'étaient tout simplement PAS exécutées !!! diff --git a/src/Controller/AppController.php b/src/Controller/AppController.php index 86c8353..becc0ba 100755 --- a/src/Controller/AppController.php +++ b/src/Controller/AppController.php @@ -32,6 +32,7 @@ use Cake\Log\Log; use Cake\Http\Exception\NotImplementedException; use Cake\Controller\Component\AuthComponent; use Cake\ORM\Entity; +use Cake\Utility\Inflector; @@ -68,8 +69,14 @@ class AppController extends Controller // - ATTRIBUTS VARIABLES - // Par défaut, genre masculin - protected $is_masculin = true; + // Genre + // Par défaut : masculin + public $is_masculin = true; + /* + public function isMasculin() { + return isset($this->is_masculin) ? $this->is_masculin ; true; + } + */ // (EP) Seulement pour les tests // Permet à un test de forcer (si true) le re-chargement d'une entité (car modifiée en BD) @@ -404,7 +411,6 @@ class AppController extends Controller } - protected function is_vowel($char) { return in_array(strtolower($char), ['a','e','i','o','u','y']); } // $form=1 => 'le', 'la', ou "l'" @@ -470,12 +476,39 @@ class AppController extends Controller * - User => 'utilisateur' * ... */ - public function getNiceTypeName() { + //public static function getNiceTypeName() { + public function getTypeNameSingular() { //return strtolower($this->name); - return strtolower($this->getName()); + //return __CLASS__; + //return strtolower( substr($this->getName(),0,-1) ); + return substr($this->getTypeNamePlural(),0,-1); } //@deprecated - public function getNiceName() { return $this->getNiceTypeName(); } + public function getNiceName() { return $this->getTypeNameSingular(); } + + // Nom pluriel affichable pour cette entité + // ex: 'suivis', 'matériels', 'catégories', ... + public function getTypeNamePlural() { + //return strtolower($this->name); + //return __CLASS__; + return strtolower( $this->getName() ); + } + + // ex: 'sur_categorie_id' + public function getFkName() { + // ex: SurCategories => sur_categories + //debug($this->getName()); + //debug($this->name); + //$fkname = Inflector::tableize($this->getName()); + //$fkname = Inflector::humanize($this->getName()); + //$fkname = Inflector::camelize($this->getName()); + $fkname = Inflector::underscore($this->getName()); + //debug($fkname); + //exit; + // ex: sur_categorie + $fkname = substr($fkname,0,-1); + return $fkname.'_id'; + } @@ -2087,8 +2120,126 @@ class AppController extends Controller } // beforeFilter() + public function view_generic($id, $associated_entity_types) { + + // ex: SurCategories + $controller_name = $this->getName(); + /* + $entity = $this->SurCategories->get($id, [ + 'contain' => [] + ]); + */ + $entity = $this->$controller_name->get($id, [ + 'contain' => [] + ]); + $entities_list = []; + // ex: 'sur_categorie_id' + $fk_name = $this->getFkName(); + //debug($fk_name);exit; + //debug($fk_name); + //$fk_name = 'sur_categorie_id'; + /* + $categories = TableRegistry::get('Categories')->find()->where([ + 'sur_categorie_id =' => $id + ]); + $materiels = TableRegistry::get('Materiels')->find('all')->where([ + 'sur_categorie_id =' => $id + ]); + */ + foreach ($associated_entity_types as $entity_type) { + $entities_list[$entity_type] = []; + // shortcut + $et = &$entities_list[$entity_type]; + $et['controller_name'] = $entity_type; + // ex: 'App\Controller\CategoriesController' + $c = 'App\\Controller\\'.$entity_type.'Controller'; + // instance du controleur + $c = new $c(); + // ex: 'catégorie' + $et['entity_type_name_singular'] = $c->getTypeNameSingular(); + // ex: 'catégories' + $et['entity_title'] = $c->getTypeNamePlural(); + $et['is_masculin'] = $c->is_masculin; + $et['name_field_label'] = $c->getNameFieldLabel(); + //$et['entities'] = $this->SurCategories->$entity_type->find()->where([ + //$et['entities'] = $this->$controller_name->$entity_type->find()->where([ + //$table = $this->$controller_name; + //$table = $this->getTableLocator()->get($controller_name); + //$table = $this->getTableLocator()->get($controller_name); + /* + $et['entities'] = $table->$entity_type->find()->where([ + "$fk_name =" => $id + ]); + */ + $table = TableRegistry::getTableLocator()->get($entity_type); + //$table = TableRegistry::getTableLocator()->get('SurCategories'); + //debug($table); exit; + $et['entities'] = $table->find()->where([ + "$fk_name =" => $id + ]); + } + //debug($entities_list); exit; + + //$controller_name = $this->SurCategories->get($id, [ + + /* + $categories = [ + 'controller_name' => 'Categories', + 'entity_type_name' => 'catégorie', + 'title' => 'Catégories', + //'id_name' => 'categs', + 'is_masculine' => false, + 'name_field_name' => 'nom', + 'entities' => $categories, + ]; + $materiels = [ + 'controller_name' => 'Materiels', + 'entity_type_name' => 'matériel', + 'title' => 'Matériels', + //'id_name' => 'categs', + 'is_masculine' => true, + 'name_field_name' => 'designation', + 'entities' => $materiels, + ]; + */ + + // On passe ces variables à la vue + //$entities_list = [$categories, $materiels]; + //$entity_article = 'le '; + $entity_article = $this->getMyArticle(); + //$entity_type_name = 'domaine'; + $entity_type_name_singular = $this->getTypeNameSingular(); + //$entity_name = $entity->getMyName(); + //$entity_name = $entity->nom; + $entity_name_field_label = $this->getNameFieldLabel(); + $entity_name = $entity->$entity_name_field_label; + $this->set(compact( + + // Infos sur l'entité courante + 'entity_article', + 'entity_type_name_singular', + 'entity_name', + 'entity', + + // Infos sur les entités associées + 'entities_list' + )); + //debug($this->viewVars);exit; + //$this->set('surCategory', $surCategory); + //$this->set('categories', $categories); + //$this->set('materiels', $materiels); + + // (EP) Automatique depuis 3.1 (et inutile pour nous) + /* + // Spécifie quelles variables de vues JsonView doit sérialiser + $this->set('_serialize', [ + 'surCategory' + ]); + */ + } // view() + /** - * Add or Edit method (do either add() or edit()) + * Add or Edit generic method (do either add() or edit()) * => Factorisation de add() et edit() * (voir aussi https://book.cakephp.org/3.0/en/orm.html) * @@ -3076,7 +3227,9 @@ class AppController extends Controller //debug($control); exit; // Entité (Entity) - $entity_nice_name = $this->getNiceName(); // matériel, domaine, utilisateur... + //$entity_nice_name = $this->getNiceName(); // matériel, domaine, utilisateur... + $entity_nice_type_name = $this->getTypeNameSingular(); // matériel, domaine, utilisateur... + // Si pas d'entité définie => return ////$entity = $this->e; $entity = $this->getCurrentEntity(); @@ -3127,7 +3280,7 @@ class AppController extends Controller $action_verb = $action_noun_and_verb[1]; */ //$entity_type = $this->getEntityTypeName(); - $entity_type = strtolower( substr($control,0,-1) ); + //$entity_type = strtolower( substr($control,0,-1) ); $article_le = $action_attrs['verb_article'] ? $action_attrs['verb_article'] : $this->getMyArticle(1); $article_dun = $action_attrs['noun_article'] ? $action_attrs['noun_article'] : $this->getMyArticle(3); $id = $entity ? $entity->id : null; @@ -3185,7 +3338,7 @@ class AppController extends Controller //$msglog = "$entity_type {$action_attrs['past_verb']} = '$entity' (id=$id)"; //$msglog = "$entity_type {$action_attrs['past_verb']} = '$entity_name' (id=$id)"; //$msglog = ucfirst($entity_type).' '.$action_attrs['past_verb_conj']; - $msglog = ucfirst($entity_nice_name).' '.$action_attrs['past_verb_conj']; + $msglog = ucfirst($entity_nice_type_name).' '.$action_attrs['past_verb_conj']; if ($entity) $msglog .= " = '$entity_name' (id=$id)"; // Cas particulier d'un Document : on affiche son type if ($entity instanceof Document) $msglog .= " (type={$entity->type_doc})"; @@ -3228,7 +3381,7 @@ class AppController extends Controller // (1) Sujet : //$subject = $subject ? $subject : "{$action_attrs['noun']} $article_dun$entity_type"; - $subject = $subject ? $subject : "{$action_attrs['noun']} $article_dun$entity_nice_name"; + $subject = $subject ? $subject : "{$action_attrs['noun']} $article_dun$entity_nice_type_name"; $DEBUG && debug($subject); // (2) Message (body) : @@ -3244,7 +3397,7 @@ class AppController extends Controller $msg_mail .= "$user a ".$action_attrs['past_verb']; if ($entity) { //$msg_mail .= " $article_le$entity_type '$entity_name'"; - $msg_mail .= " $article_le$entity_nice_name '$entity_name'"; + $msg_mail .= " $article_le$entity_nice_type_name '$entity_name'"; // Cas particulier d'un Document : on affiche son type if ($entity instanceof Document) $msg_mail .= " (type={$entity->type_doc})"; // (EP) Ajout de l'url (ou id) du materiel diff --git a/src/Controller/CategoriesController.php b/src/Controller/CategoriesController.php index f22002d..7ff09f5 100755 --- a/src/Controller/CategoriesController.php +++ b/src/Controller/CategoriesController.php @@ -12,6 +12,13 @@ use Cake\ORM\TableRegistry; class CategoriesController extends AppController { + // Genre + public $is_masculin = false; + + // Nom pluriel affichable pour cette entité + //@Override + public function getTypeNamePlural() { return 'catégories'; } + public function getArticle() { return "La"; @@ -110,6 +117,14 @@ class CategoriesController extends AppController */ public function view($id = null) { + $associated_entity_types = [ + 'SousCategories', + 'Materiels' + ]; + //return parent::view($id, $associated_entity_types); + return $this->view_generic($id, $associated_entity_types); + + /* $category = $this->Categories->get($id, [ 'contain' => [ 'SurCategories' @@ -129,6 +144,7 @@ class CategoriesController extends AppController $this->set('_serialize', [ 'category' ]); + */ } /** diff --git a/src/Controller/MaterielsController.php b/src/Controller/MaterielsController.php index 5b1ebfa..12347c4 100755 --- a/src/Controller/MaterielsController.php +++ b/src/Controller/MaterielsController.php @@ -38,9 +38,14 @@ class MaterielsController extends AppController { // - ATTRIBUTS VARIABLES - // Nom affichable pour cette entité + /* + // Nom singulier affichable pour cette entité // @Override - public function getNiceName() { return "matériel"; } + public function getTypeNameSingular() { return "matériel"; } + */ + // Nom pluriel affichable pour cette entité + //@Override + public function getTypeNamePlural() { return 'matériels'; } private $NOTARCHIVED = [ diff --git a/src/Controller/SurCategoriesController.php b/src/Controller/SurCategoriesController.php index a316081..03ce137 100755 --- a/src/Controller/SurCategoriesController.php +++ b/src/Controller/SurCategoriesController.php @@ -11,17 +11,38 @@ use Cake\ORM\TableRegistry; */ class SurCategoriesController extends AppController { + + /* + // Nom singulier affichable pour cette entité + // @Override parent + public function getTypeNameSingular() { + //return strtolower($this->name); + //return strtolower($this->getName()); + return 'domaine'; + } + */ + // Surcharge pour corriger bug cakephp qui renvoie 'Surcategories' au lieu de 'SurCategories' !! + //@Override + public function getName() { + //return strtolower($this->name); + //return strtolower($this->getName()); + return 'SurCategories'; + } + // Nom pluriel affichable pour cette entité + //@Override + public function getTypeNamePlural() { + //return strtolower($this->name); + //return strtolower($this->getName()); + return 'domaines'; + } + // Genre // @Override - protected $is_masculin = false; + //public $is_masculin = false; // @deprected public function getArticle() { return "La "; } - // Nom affichable pour cette entité - // @Override - public function getNiceName() { return "domaine"; } - /* static public function getActions() { // Toutes les actions CRUD @@ -134,6 +155,7 @@ class SurCategoriesController extends AppController */ } + /** * View method * @@ -144,24 +166,15 @@ class SurCategoriesController extends AppController */ public function view($id = null) { - $surCategory = $this->SurCategories->get($id, [ - 'contain' => [] - ]); - $categories = TableRegistry::get('Categories')->find()->where([ - 'sur_categorie_id =' => $id - ]); - $this->set('categories', $categories); - - $materiels = TableRegistry::get('Materiels')->find('all')->where([ - 'sur_categorie_id =' => $id - ]); - $this->set('materiels', $materiels); - - $this->set('surCategory', $surCategory); - $this->set('_serialize', [ - 'surCategory' - ]); + $associated_entity_types = [ + 'Categories', + 'Users', + 'Materiels' + ]; + //return parent::view($id, $associated_entity_types); + return $this->view_generic($id, $associated_entity_types); } + /** * Add method diff --git a/src/Controller/UsersController.php b/src/Controller/UsersController.php index 6e9a7e5..3040b8a 100755 --- a/src/Controller/UsersController.php +++ b/src/Controller/UsersController.php @@ -14,9 +14,19 @@ class UsersController extends AppController { + /* // Nom affichable pour cette entité // @Override public function getNiceName() { return "utilisateur"; } + */ + // Nom pluriel affichable pour cette entité + //@Override + public function getTypeNamePlural() { + //return strtolower($this->name); + //return strtolower($this->getName()); + return 'utilisateurs'; + } + // "l'" utilisateur (et non pas "le utilisateur") // @deprecated diff --git a/src/Model/Table/SurCategoriesTable.php b/src/Model/Table/SurCategoriesTable.php index 1ff5613..239e109 100755 --- a/src/Model/Table/SurCategoriesTable.php +++ b/src/Model/Table/SurCategoriesTable.php @@ -25,12 +25,27 @@ class SurCategoriesTable extends AppTable parent::initialize($config); $this->setTable('sur_categories'); - $this->setDisplayField('id'); + //$this->setDisplayField('id'); + $this->setDisplayField('nom'); $this->setPrimaryKey('id'); - + + /* C'EST FAUX $this->hasMany('Users', [ 'foreignKey' => 'groupes_thematique_id' ]); + */ + $this->hasMany('Users', [ + 'foreignKey' => 'sur_categorie_id' + ]); + + $this->hasMany('Categories') + ->setForeignKey('sur_categorie_id') + ->setDependent(true); // si le domaine est supprimé, les catégories liées le seront aussi + + $this->hasMany('Materiels') + ->setForeignKey('sur_categorie_id') + ->setDependent(false); // si le domaine est supprimé, les matos liés eux NE LE SERONT PAS !! + } /** diff --git a/src/Template/Categories/view.ctp b/src/Template/Categories/view.ctp index 67e8ec9..da8d19d 100755 --- a/src/Template/Categories/view.ctp +++ b/src/Template/Categories/view.ctp @@ -1,121 +1,36 @@ - -
-

Détail catégorie

- - - - - - - - Html->link(__(' Editer cette catégorie'), [ - 'action' => 'edit', - $category->id - ], [ - 'style' => 'margin-right: 40px; display: inline-block', - 'escape' => false, - 'onclick' => 'return true;' - ]); - } - - if (in_array($role, [ - 'Super Administrateur' - ])) { - echo $this->Form->postLink(__(' Supprimer cette catégorie'), [ - 'action' => 'delete', - $category->id - ], [ - 'style' => 'display: inline-block', - 'escape' => false, - 'confirm' => __('Êtes-vous sur de vouloir supprimer # {0}?', $category->id) - ]); - } - - $displayElement(__('Nom'), h($category->nom)); - $displayElement(__('Domaine'), $category->has('sur_category') ? $this->Html->link($category->sur_category->nom, [ - 'controller' => 'SurCategories', - 'action' => 'view', - $category->sur_category->id - ]) : ''); - - ?> - -
- - -

- - Sous-Catégories associées (count()?>) -

-
- - - - - - - - - - - - - - - -
Html->link(h($sous_category->nom), ['controller' => 'SousCategories', 'action' => 'view', h($sous_category->id)]); ?> - Html->link(__(''), ['controller' => 'SousCategories', 'action' => 'view', $sous_category->id], ['escape' => false, 'style' => 'margin:0']) ?> -
- -
- - -

- - Materiels associés (count()?>) -

-
- - - - - - - - - - - - - - - - -
Html->link($materiel->designation, ['controller' => 'Materiels', 'action' => 'view', h($materiel->id)]); ?> - Html->link(__(''), ['controller' => 'Materiels', 'action' => 'view', $materiel->id], ['escape' => false, 'style' => 'margin:0']) ?> -
- -
- - -
- - \ No newline at end of file + + +extend('/Common/view'); + +/* +//Vous pouvez récupérer la liste de tous blocks existants en utilisant la méthode blocks(): +$list = $this->blocks(); + +(En admettant que notre fichier de vue a une variable $post avec les données sur notre post) + +$this->assign('title', $post->title); +$this->start('sidebar'); +?> +
  • + Html->link('edit', [ + 'action' => 'edit', + $post->id + ]); ?> +
  • +end(); +?> + +// The remaining content will be available as the 'content' block +// In the parent view. +body) ?> +*/ diff --git a/src/Template/Common/view.ctp b/src/Template/Common/view.ctp index 3d8d6d8..e2e7db6 100755 --- a/src/Template/Common/view.ctp +++ b/src/Template/Common/view.ctp @@ -1,22 +1,32 @@ getSource()); - -$DEBUG=false; -//$DEBUG=true; +//exit; if ($DEBUG) { - $article = 'le '; - $entity_type_name = 'domaine'; + $entity_article = 'le '; + $entity_type_name_singular = 'domaine'; //$entity_name = $entity->getMyName(); $entity_name = $entity->nom; @@ -45,15 +55,9 @@ if ($DEBUG) { $entities_list = [$categories, $materiels]; } -$entity_name = $entity_name; - - - -// Liste des entités à afficher (en tant qu'entité associée) : -// En général, par défaut : "Matériels associés" et "Utilisateurs associés" -$entities_list = $entities_list; ?> + @@ -62,8 +66,13 @@ $entities_list = $entities_list; -

    Informations sur

    + +

    Informations sur

    + Html->link("Voir toutes les entités (liste)", ['action'=>'index']) ?> +
    +
    + @@ -121,11 +130,11 @@ $entities_list = $entities_list; // function displaySectionShowHide($controller_name, $entity_type_name, $title, $id_name, $is_masculine, $name_field_name, $entities) { $this->MyHelper->displayAssociatedEntitiesAsSectionShowHide( $es['controller_name'], - $es['entity_type_name'], - $es['title'], + $es['entity_type_name_singular'], + $es['entity_title'], //$es['id_name'], - $es['is_masculine'], - $es['name_field_name'], + $es['is_masculin'], + $es['name_field_label'], $es['entities'] ); //displaySectionShowHide($this->Html, false, 'Aucune catégorie liée', 'categs', 'Catégories', $categories, 'nom', 'Categories', 'catégorie'); diff --git a/src/Template/Materiels/view.ctp b/src/Template/Materiels/view.ctp index b8ac0f7..0ecd414 100755 --- a/src/Template/Materiels/view.ctp +++ b/src/Template/Materiels/view.ctp @@ -582,12 +582,12 @@ else if (h($entity->materiel_technique) == 1) //$displayElement(__('Nom du materiel'),$entity->designation); //$displayElement(__('Description'),$entity->description); //$displayElement(__('Materiel inventorié'), $type); -$this->MyButton->displayElement('Nom du materiel',$entity->designation); +$this->MyHelper->displayElement('Nom du materiel',$entity->designation); // (EP202009) Deplacé tout en bas du tableau et affiché avec Text->autoParagraph() //$this->MyButton->displayElement('Description',$entity->description); -$this->MyButton->displayElement('Materiel inventorié',$type); +$this->MyHelper->displayElement('Materiel inventorié',$type); $displayElement(__('Domaine'), $entity->has('sur_category') ? h($entity->sur_category->nom) : ''); $displayElement(__('Catégorie'), $entity->has('category') ? h($entity->category->nom) : ''); $displayElement(__('Sous-Catégorie'), $entity->has('sous_category') ? h($entity->sous_category->nom) : ''); diff --git a/src/View/Helper/MyHelperHelper.php b/src/View/Helper/MyHelperHelper.php index c81df1e..f5b9cbe 100644 --- a/src/View/Helper/MyHelperHelper.php +++ b/src/View/Helper/MyHelperHelper.php @@ -62,7 +62,7 @@ class MyHelperHelper extends Helper { $id_name = $controller_name; $id_h3 = 't_'.$id_name; $id_i = 'i_'.$id_name; - $title = $title.' associé'. ($is_masculine ? 's' : 'es'); + $title = ucfirst($title).' associé'. ($is_masculine ? 's' : 'es'); echo "

    "; echo ""; -- libgit2 0.21.2