13-Cases à cocher multiple et tables annexes
Admettons que vous ayez un comité de lecture (soyons fous) composé de 3 personnes:
- Pierre
- Paul
- Jacques
Un membre peut lire plusieurs livres et donner son avis, mais un livre peut-être lu par plusieurs membres. Pour gérer cela nous allons nous servir de la table Livres, de la table Users où sont enregistrés les membres du comité et d'un nouvelle table Livreusers.
Dans chaque enregistrement nous aurons l'id du livre, l'id du membre, une note ainsi qu'un commentaire. Pour un fonctionnement plus simple, les membres lisent le livre et vous envoient leurs impressions sur les livres qu'ils ont lu. C'est à vous de renseigner dans la fiche d'un livre les notes et commentaire des membres.
Création de la table #__livreusers
Nous allons nous servir du chapitre 4 tuto Cas pratiques sur Joomla - Partie 02 pour créer la table mais cette fois à partir de xxufl_auteurs .
Nous allons insérer 2 champs juste après le champ id :
- id_livre
- id_user
La structure de la table ressemble à cela :
Les champs qui vont nous intéressé sont :
- id unique et auto-incrément
- id_livre id du livre concerné
- id_user id su user concerné
- title il faut mettre un titre=id_livre+"-"+id_auteur doit être unique
- alias doit correspondre au titre
- snippet nous servira pour insérer les commentaires du membre du comité.
Nous devons créer un groupe comme ceci:
Nous devons créer les 3 utilisateurs:
Bon, les bases étant posées, nous allons commencer à coder.
administrator\components\com_livres\views\livre\view.html.php
La fonction display est :
public function display($tpl = null) { // Initialiase variables. $this->form = $this->get('Form'); $this->item = $this->get('Item'); $this->state = $this->get('State'); $this->canDo = LivresHelper::getActions($this->state->get('filter.category_id')); // Check for errors. if (count($errors = $this->get('Errors'))) { JError::raiseError(500, implode("\n", $errors)); return false; } $listeetatcomplete=$this->get('Listeetatcomplete'); $this->assignRef('listeetatcomplete', $listeetatcomplete); $this->addToolbar(); parent::display($tpl); }
devient
public function display($tpl = null) { // Initialiase variables. $this->form = $this->get('Form'); $this->item = $this->get('Item'); $this->state = $this->get('State'); $this->canDo = LivresHelper::getActions($this->state->get('filter.category_id')); // Check for errors. if (count($errors = $this->get('Errors'))) { JError::raiseError(500, implode("\n", $errors)); return false; } $listeetatcomplete=$this->get('Listeetatcomplete'); $this->assignRef('listeetatcomplete', $listeetatcomplete); $listecomscomplete=$this->get('Listecomscomplete'); $this->assignRef('listecomscomplete', $listecomscomplete); $this->addToolbar(); parent::display($tpl); }
Nous remplissons la variable $listecomscomplete pour l'affichage des commentaires déjà renseignés ou non.
administrator\components\com_livres\models\livre.php
La fonction getlistecomscomplete se comporte de la manière suivante:
- On récupère la variable $item
$item = parent::getItem();
- remplir une array avec la liste des utilisateurs du groupe 9
$db = $this->getDbo(); $query = $db->getQuery(true); $query->select('u.id, u.name'); $query->from('#__users AS u, #__user_usergroup_map AS g'); $query->where('u.id = g.user_id AND g.group_id=9'); $db->setQuery($query->__toString()); $result = $db->loadObjectList();
- remplir une autre array avec le contenu de la table livresusers
$db = $this->getDbo(); $query = $db->getQuery(true); $query->select('id,title,id_livre,id_user,snippet'); $query->from('#__livreusers AS lu'); $query->where('lu.id_livre = '.$item->id); $db->setQuery($query->__toString()); $result2 = $db->loadObjectList();
- imbrication du parcours de la deuxième array dans le parcours de la première pour tester les commentaires déjà renseignés du livre en cours
for ($j=0, $m=count( $result ); $j < $m; $j++) { $result[$j]->Checked=0; for ($k=0, $o=count( $result2 ); $k < $o; $k++) { if ($result[$j]->id==$result2[$k]->id_user ) { $result[$j]->Checked="CHECKED"; $result[$j]->snippet=$result2[$k]->snippet; } } }
- transfert de l'array
return $result;
La fonction complète est la suivante :
function getListecomscomplete() { $item = parent::getItem(); $db = $this->getDbo(); $query = $db->getQuery(true); $query->select('u.id, u.name'); $query->from('#__users AS u, #__user_usergroup_map AS g'); $query->where('u.id = g.user_id AND g.group_id=9'); $db->setQuery($query->__toString()); $result = $db->loadObjectList(); $db = $this->getDbo(); $query = $db->getQuery(true); $query->select('id,title,id_livre,id_user,snippet'); $query->from('#__livreusers AS lu'); $query->where('lu.id_livre = '.$item->id); $db->setQuery($query->__toString()); $result2 = $db->loadObjectList(); for ($j=0, $m=count( $result ); $j < $m; $j++) { $result[$j]->Checked=0; for ($k=0, $o=count( $result2 ); $k < $o; $k++) { if ($result[$j]->id==$result2[$k]->id_user ) { $result[$j]->Checked="CHECKED"; $result[$j]->snippet=$result2[$k]->snippet; } } } return $result; }
Nous allons maintenant créer un bouveau fichier edit_commentaires.php dans administrator\components\com_livres\views\livre\tmpl
administrator\components\com_livres\views\livre\tmpl\edit_commentaires.php
Dans ce fichier nous lançons une boucle pour afficher les différentes valeurs contenu dans l'array $listecomscomplete
<?php /** * @version 1.0.0 * @package com_livres * @copyright Copyright (C) 2011 Amy Stephen. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ defined('_JEXEC') or die; echo JHtml::_('sliders.panel',JText::_('COM_LIVRES_FIELDSET_COMMENTAIRES'), 'commentaires-details'); ?> <fieldset class="panelform"> <table class="adminformlist"> <?php for ($j=0, $m=count( $this->listecomscomplete ); $j < $m; $j++) {?> <tr><td><?php echo $this->listecomscomplete[$j]->name; ?></td> <td><INPUT type="checkbox" size="1" id="com" name="com<?php echo $j+1;?>" value="<?php echo $this->listecomscomplete[$j]->id;?>" <?php echo $this->listecomscomplete[$j]->Checked;?>></td> <td><INPUT type="inputbox" size="150" id="com" name="comtexte<?php echo $j+1;?>" value="<?php echo $this->listecomscomplete[$j]->snippet;?>" ></td> <?php }?> <INPUT type="hidden" id="com" name="com0" value="<?php echo $m;?>"> </table> </fieldset>
administrator\components\com_livres\views\livre\tmpl\edit.php
Nous allons modifier ce fichier pour qu'il appelle le fichier ci-dessus :
<div class="width-40 fltrt"> <?php echo JHtml::_('sliders.start','content-sliders-'.$this->item->id, array('useCookie'=>1)); ?> <?php echo $this->loadTemplate('commentaires'); ?> <?php echo $this->loadTemplate('publishing'); ?> <?php echo $this->loadTemplate('custom_fields'); ?> <?php echo $this->loadTemplate('parameters'); ?> <?php echo $this->loadTemplate('metadata'); ?> <?php echo JHtml::_('sliders.end'); ?> </div>
devient
<div class="width-40 fltrt"> <?php echo JHtml::_('sliders.start','content-sliders-'.$this->item->id, array('useCookie'=>1)); ?> <?php echo $this->loadTemplate('commentaires'); ?> <?php echo $this->loadTemplate('publishing'); ?> <?php echo $this->loadTemplate('custom_fields'); ?> <?php echo $this->loadTemplate('parameters'); ?> <?php echo $this->loadTemplate('metadata'); ?> <?php echo JHtml::_('sliders.end'); ?> </div>
Il faut rajouter dans le fichier de langue:
COM_LIVRES_FIELDSET_COMMENTAIRES="Comité de lecture"
Cela nous donne comme affichage, lors d'une première utilisation:
Pour insérer un nouveau commentaire, il suffit de cocher un membre du comité et de saisir le commentaire de celui-ci puis d'enregistrer la fiche.
Il faut modifier la routine de sauvegarde pour enregistrer dans la table xxufl_livreusers les nouvelles données.
administrator\components\com_livres\models\livre.php
La fonction save est :
public function save($data) { $jinput = JFactory::getApplication()->input; $data['etat']=$jinput->get('etat', '', ''); // Alter the title for save as copy if (JRequest::getVar('task') == 'save2copy') { list($title,$alias) = $this->generateNewTitle($data['catid'], $data['alias'], $data['title']); $data['title'] = $title; $data['alias'] = $alias; } if ( array_key_exists('pret', $data)) { $data['pret']='1'; } else { $data['pret']='0'; } if (parent::save($data)) { return true; } return false; }
Nous allons lui rajouter quelques lignes.
Pour récupérer les valeurs du formulaire :
$data['com0']=$jinput->get('com0', '', ''); for ($k=1, $o=$jinput->get('com0', '', ''); $k < $o+1; $k++) { $var_com="com".$k; $var_comtexte="comtexte".$k; $recup=$jinput->get($var_com, '', ''); if (!$recup=="") { $data[$var_com]=$jinput->get($var_com, '', ''); $data[$var_comtexte]=$jinput->get($var_comtexte, '', ''); } else { $data[$var_com]=0; $data[$var_comtexte]=""; } }
Copie des données de $data dans une autre variable $datas pour les transmettre à une routine de sauvegarde.
$datas=$data;
On appelle une routine de sauvegarde :
$result=$this->SaveData($datas);
On crée en fin de fichier la fonction SaveData :
Elle se compose de plusieurs parties. On parcours d'abord les variables transmises et l'on compare avec les valeurs de la table et l'on voit si il faut créer ou mettre à jour le contenu de la table.
for ($k=1, $o=$option['com0']; $k < $o+1; $k++) { $var_com="com".$k; $var_comtexte="comtexte".$k; if (isset($option[$var_com])) { $db = $this->getDbo(); $query = $db->getQuery(true); $query->select('*'); $query->from('#__livreusers AS lu'); $query->where('lu.id_livre = ' . $option['id'].' AND lu.id_user = '.$option[$var_com].''); $db->setQuery($query->__toString()); $result = $db->loadObject(); if (!$option[$var_com] == 0) { if (empty($result)) { $db = JFactory::getDBO(); if (!$option[$var_comtexte]=="") { $query = "INSERT INTO #__livreusers (id_user,id_livre,snippet) VALUES ('" .$option[$var_com]."' , '".$option['id']."','".$option[$var_comtexte]."')"; } else { $query = "INSERT INTO #__livreusers (id_user,id_livre) VALUES ('" .$option[$var_com]."' , '".$option['id']."')"; } $db->setQuery($query); $result = $db->query(); } else { $db = JFactory::getDBO(); $query = "UPDATE #__livreusers SET snippet='".$option[$var_comtexte]."'"; $db->setQuery($query); $result = $db->query(); } } } }
Puis on parcours la table et l'on compare avec les variables transmises pour supprimer les enregistrements non souhaités.
$db = $this->getDbo(); $query = $db->getQuery(true); $query->select('id, id_livre, id_user, snippet'); $query->from('#__livreusers AS lu'); $query->where('lu.id_livre = ' . $option['id'].''); $db->setQuery($query->__toString()); $result = $db->loadObjectList(); for ($j=0, $m=count( $result ); $j < $m; $j++) { $delete="0"; for ($k=1, $o=$option['com0']; $k < $o+1; $k++) { $var_com="com".$k; if (isset($option[$var_com])) { if ($result[$j]->id_user == $option[$var_com]){ $delete="1"; } } } if ($delete=="0"){ $db = JFactory::getDBO(); $query = "DELETE FROM #__livreusers WHERE id_livre = " . $result[$j]->id_livre." AND id_user=".$result[$j]->id_user; $db->setQuery($query); $result2 = $db->query(); } }
Cela nous donne au final :
function SaveData($option) { //Save table #__b_partint_bien for ($k=1, $o=$option['com0']; $k < $o+1; $k++) { $var_com="com".$k; $var_comtexte="comtexte".$k; if (isset($option[$var_com])) { $db = $this->getDbo(); $query = $db->getQuery(true); $query->select('*'); $query->from('#__livreusers AS lu'); $query->where('lu.id_livre = ' . $option['id'].' AND lu.id_user = '.$option[$var_com].''); $db->setQuery($query->__toString()); $result = $db->loadObject(); if (!$option[$var_com] == 0) { if (empty($result)) { $db = JFactory::getDBO(); if (!$option[$var_comtexte]=="") { $query = "INSERT INTO #__livreusers (id_user,id_livre,snippet) VALUES ('" .$option[$var_com]."' , '".$option['id']."','".$option[$var_comtexte]."')"; } else { $query = "INSERT INTO #__livreusers (id_user,id_livre) VALUES ('" .$option[$var_com]."' , '".$option['id']."')"; } $db->setQuery($query); $result = $db->query(); } else { $db = JFactory::getDBO(); $query = "UPDATE #__livreusers SET snippet='".$option[$var_comtexte]."'"; $db->setQuery($query); $result = $db->query(); } } } } $db = $this->getDbo(); $query = $db->getQuery(true); $query->select('id, id_livre, id_user, snippet'); $query->from('#__livreusers AS lu'); $query->where('lu.id_livre = ' . $option['id'].''); $db->setQuery($query->__toString()); $result = $db->loadObjectList(); for ($j=0, $m=count( $result ); $j < $m; $j++) { $delete="0"; for ($k=1, $o=$option['com0']; $k < $o+1; $k++) { $var_com="com".$k; if (isset($option[$var_com])) { if ($result[$j]->id_user == $option[$var_com]){ $delete="1"; } } } if ($delete=="0"){ $db = JFactory::getDBO(); $query = "DELETE FROM #__livreusers WHERE id_livre = " . $result[$j]->id_livre." AND id_user=".$result[$j]->id_user; $db->setQuery($query); $result2 = $db->query(); } } }
Voila vous pouvez gérer les commentaires des membres de votre comité.
12-Bouton radio version dynamique
Nous devons donc créer une nouvelle table pour gérer les différents état d'un livre.
Pour cela nous allons suivre Cas pratiques sur Joomla - Partie 02 en choisissant xxufl_etat comme nom de table. Nous allons faire un peu de ménage dans cette nouvelle table car les nouveaux champs de la table xxufl_livres ne sont pas nécessaire.
Nous supprimons donc les champs :
- id_auteur
- pret
- etat
Voici la structure de la table :
Je ne vais pas revenir sur la création et modification des répertoires et fichiers à effectuer pour gérer cela dans l'interface. Il faut s'inspirer de l'article Cas pratiques sur Joomla - Partie 03 pour cela.
Une fois cela fait, il faut saisir tous les états de conservation des livres que vous souhaitez. Pour moi cela donne comme contenu de table :
Il faut faire correspondre les données de la table avec les boutons radio.
administrator\components\com_livres\models\livre.php
Ajouter à la fin une fonction :
function getListeetatcomplete() { $db = $this->getDbo(); $query = $db->getQuery(true); $query->select(' id, title'); $query->from('#__etat'); $db->setQuery($query->__toString()); $listeetatcomplete = $db->loadObjectList(); return $listeetatcomplete; }
Il faut modifier la fonction save pour intégrer le changement de valeur.
public function save($data) { // Alter the title for save as copy
devient
public function save($data) { $jinput = JFactory::getApplication()->input; $data['etat']=$jinput->get('etat', '', ''); // Alter the title for save as copy
administrator\components\com_livres\views\livre\view.html.php
La fonction display est :
public function display($tpl = null) { // Initialiase variables. $this->form = $this->get('Form'); $this->item = $this->get('Item'); $this->state = $this->get('State'); $this->canDo = LivresHelper::getActions($this->state->get('filter.category_id')); // Check for errors. if (count($errors = $this->get('Errors'))) { JError::raiseError(500, implode("\n", $errors)); return false; } $this->addToolbar(); parent::display($tpl); }
devient
public function display($tpl = null) { // Initialiase variables. $this->form = $this->get('Form'); $this->item = $this->get('Item'); $this->state = $this->get('State'); $this->canDo = LivresHelper::getActions($this->state->get('filter.category_id')); // Check for errors. if (count($errors = $this->get('Errors'))) { JError::raiseError(500, implode("\n", $errors)); return false; } $listeetatcomplete=$this->get('Listeetatcomplete'); $this->assignRef('listeetatcomplete', $listeetatcomplete); $this->addToolbar(); parent::display($tpl); }
Nous remplissons une array en nous servant de la fonction getListeetatcomplete.
administrator\components\com_livres\views\livre\tmpl\edit.php
Nous supprimons les 2 lignes ajoutées dans l'article Cas pratiques sur Joomla - Partie 07.
Donc de :
<li><?php echo $this->form->getLabel('pret'); ?> <?php echo $this->form->getInput('pret'); ?></li> <li><?php echo $this->form->getLabel('etat'); ?> <?php echo $this->form->getInput('etat'); ?></li> <li><?php echo $this->form->getLabel('subtitle'); ?> <?php echo $this->form->getInput('subtitle'); ?></li>
nous passons à
<li><?php echo $this->form->getLabel('pret'); ?> <?php echo $this->form->getInput('pret'); ?></li> <li><?php echo $this->form->getLabel('subtitle'); ?> <?php echo $this->form->getInput('subtitle'); ?></li>
Puis juste après :
<li><?php echo $this->form->getLabel('subtitle'); ?> <?php echo $this->form->getInput('subtitle'); ?></li> </ul>
nous rajoutons :
<div class="clr"></div> <table width="100%" border="0" class="admintable"> <div> <?php for ($j=0, $m=count( $this->listeetatcomplete ); $j < $m; $j++) { $checked=""; if ($this->item->etat == $j) { $checked="checked"; }?> <td><input type="radio" name="TYPMAId" value="<?php echo $j;?>" <?php echo $checked;?>> <?php echo $this->listeetatcomplete[$j]->title;?></td> <?php };?> </div> </table>
Je vous laisse régler à votre guise les problèmes d'affichage.
Cela donne :
La modification d'état est bien sauvegardé. Choisissons Couverture déchirée et sauvegardons.
11-Bouton radio version statique
Pour voir comment utiliser les boutons radio, nous allons gérer l'état des livres.
- 0 - Etat inconnu
- 1 - Etat abîmé
- 2 - Etat bon
- 3 - Comme neuf
Pour cela nous allons rajouter un champ Etat dans la table Livres.
Pour la vue liste nous allons réaliser les modifications suivantes :
administrator\components\com_livres\models\livres.php
La fonction __construct est:
public function __construct($config = array()) { if (empty($config['filter_fields'])) { $config['filter_fields'] = array( 'id', 'a.id', 'title', 'a.title', 'alias', 'a.alias', 'checked_out', 'a.checked_out', 'checked_out_time', 'a.checked_out_time', 'catid', 'a.catid', 'category_title', 'state', 'a.state', 'access', 'a.access', 'access_level', 'created', 'a.created', 'created_by', 'a.created_by', 'ordering', 'a.ordering', 'publish_up', 'a.publish_up', 'publish_down', 'a.publish_down', 'id_auteur', 'a.id_auteur', 'auteur_title', 'pret', 'a.pret' ); } parent::__construct($config); }
devient
public function __construct($config = array()) { if (empty($config['filter_fields'])) { $config['filter_fields'] = array( 'id', 'a.id', 'title', 'a.title', 'alias', 'a.alias', 'checked_out', 'a.checked_out', 'checked_out_time', 'a.checked_out_time', 'catid', 'a.catid', 'category_title', 'state', 'a.state', 'access', 'a.access', 'access_level', 'created', 'a.created', 'created_by', 'a.created_by', 'ordering', 'a.ordering', 'publish_up', 'a.publish_up', 'publish_down', 'a.publish_down', 'id_auteur', 'a.id_auteur', 'auteur_title', 'pret', 'a.pret', 'etat', 'a.etat' ); } parent::__construct($config); }
La ligne 140 est :
'a.parameters, a.custom_fields, a.id_auteur, a.pret'
devient
'a.parameters, a.custom_fields, a.id_auteur, a.pret, a.etat'
administrator\components\com_livres\views\livres\tmpl\default.php
Les lignes 70 à 75 sont :
<th width="5%"> <?php echo JHtml::_('grid.sort', 'COM_LIVRES_PRET_TITLE', 'pret', $listDirn, $listOrder); ?> </th> <th width="5%"> <?php echo JHtml::_('grid.sort', 'JSTATUS', 'a.state', $listDirn, $listOrder); ?> </th>
deviennent les lignes 70 à 78 :
<th width="5%"> <?php echo JHtml::_('grid.sort', 'COM_LIVRES_PRET_TITLE', 'pret', $listDirn, $listOrder); ?> </th> <th width="5%"> <?php echo JHtml::_('grid.sort', 'COM_LIVRES_ETAT_TITLE', 'etat', $listDirn, $listOrder); ?> </th> <th width="5%"> <?php echo JHtml::_('grid.sort', 'JSTATUS', 'a.state', $listDirn, $listOrder); ?> </th>
Insérer en ligne 146 :
<td class="center"> <?php echo JHtml::_('jgrid.published', $item->state, $i, 'livres.', $canChange, 'cb', $item->publish_up, $item->publish_down); ?> </td>
pour avoir cela
<td class="center"> <?php echo $this->escape($item->etat); ?> </td> <td class="center"> <?php echo JHtml::_('jgrid.published', $item->state, $i, 'livres.', $canChange, 'cb', $item->publish_up, $item->publish_down); ?> </td>
Dans le fichier de langue, ajouter la définition suuivante :
COM_LIVRES_ETAT_TITLE="Etat"
Cela donne comme affichage :
Nous allons gérer la fiche d'un livre.
administrator\components\com_livres\models\forms\livre.xml
Ajouter la description de champ suivante :
<field name="etat" type="radio" label="COM_LIVRES_FIELD_ETAT_LABEL" description="COM_LIVRES_FIELD_ETAT_DESC" class="inputbox" filter="intval" size="1" default="1" > <option value="0"> COM_LIVRES_FIELD_ETAT_INCONNU</option> <option value="1"> COM_LIVRES_FIELD_ETAT_ABIME</option> <option value="2"> COM_LIVRES_FIELD_ETAT_BON</option> <option value="3"> COM_LIVRES_FIELD_ETAT_NEUF</option> </field>
administrator\components\com_livres\views\livre\tmpl\edit.php
Modifier les lignes 45 à 48 :
<li><?php echo $this->form->getLabel('pret'); ?> <?php echo $this->form->getInput('pret'); ?></li> <li><?php echo $this->form->getLabel('subtitle'); ?> <?php echo $this->form->getInput('subtitle'); ?></li>
pour devenir les lignes 45 à 50 :
<li><?php echo $this->form->getLabel('pret'); ?> <?php echo $this->form->getInput('pret'); ?></li> <li><?php echo $this->form->getLabel('etat'); ?> <?php echo $this->form->getInput('etat'); ?></li> <li><?php echo $this->form->getLabel('subtitle'); ?> <?php echo $this->form->getInput('subtitle'); ?></li>
Dans le fichier de langue, ajouter les définitions suivantes:
COM_LIVRES_FIELD_ETAT_LABEL="Etat du livre"
COM_LIVRES_FIELD_ETAT_DESC="Choisir un état"
COM_LIVRES_FIELD_ETAT_INCONNU="Inconnu"
COM_LIVRES_FIELD_ETAT_ABIME="Abîmé"
COM_LIVRES_FIELD_ETAT_BON="Bon"
COM_LIVRES_FIELD_ETAT_NEUF="Comme neuf"
Cela nous donne comme affichage :
Si nous choisissons Comme neuf et que nous sauvegardons, l'affichage liste sera :
Tout cela c'est bien joli mais les différents sont statiques, il faut entrer dans le code pour rajouter un état et la liste des livres affiche une valeur et non le texte d'un état. Nous allons voir dans le prochain article comment gérer cela avec une nouvelle table.
10-Case à cocher
Nous allons voir ici le cas des cases à cocher.
Je n'ai jamais réussi à les faire fonctionner (il semble que je ne sois pas le seul), j'ai cherché sur les forums français et US sans succès.
La case se coche bien et sauvegarde la valeur mais elle ne se décoche pas donc la valeur est toujours à 1.
J'ai donc choisi de coder quelque chose pour corriger cela.
Nous modifions la table Livres pour ajouter un champ pour gérer le prêt de livres.
Les valeurs seront: 1 pour prêter et 0 pour en stock.
Les modifications du code sont les suivantes:
administrator\components\com_livres\models\livres.php
La fonction __construct est :
public function __construct($config = array()) { if (empty($config['filter_fields'])) { $config['filter_fields'] = array( 'id', 'a.id', 'title', 'a.title', 'alias', 'a.alias', 'checked_out', 'a.checked_out', 'checked_out_time', 'a.checked_out_time', 'catid', 'a.catid', 'category_title', 'state', 'a.state', 'access', 'a.access', 'access_level', 'created', 'a.created', 'created_by', 'a.created_by', 'ordering', 'a.ordering', 'publish_up', 'a.publish_up', 'publish_down', 'a.publish_down', 'id_auteur', 'a.id_auteur', 'auteur_title' ); } parent::__construct($config); }
devient
public function __construct($config = array()) { if (empty($config['filter_fields'])) { $config['filter_fields'] = array( 'id', 'a.id', 'title', 'a.title', 'alias', 'a.alias', 'checked_out', 'a.checked_out', 'checked_out_time', 'a.checked_out_time', 'catid', 'a.catid', 'category_title', 'state', 'a.state', 'access', 'a.access', 'access_level', 'created', 'a.created', 'created_by', 'a.created_by', 'ordering', 'a.ordering', 'publish_up', 'a.publish_up', 'publish_down', 'a.publish_down', 'id_auteur', 'a.id_auteur', 'auteur_title', 'pret', 'a.pret' ); }
Ligne 139 :
'a.parameters, a.custom_fields, a.id_auteur'
==>
'a.parameters, a.custom_fields, a.id_auteur, a.pret'
administrator\components\com_livres\models\forms\livre.xml
Ajouter la déclaration suivante :
<field name="pret" type="checkbox" label="COM_LIVRES_FIELD_PRET_LABEL" description="COM_LIVRES_FIELD_PRET_DESC" class="inputbox" size="1" value="1" checked="checked" />
administrator\components\com_livres\views\livres\tmpl\default.php
Ajouter à la ligne 70 :
<th width="5%"> <?php echo JHtml::_('grid.sort', 'COM_LIVRES_PRET_TITLE', 'a.pret', $listDirn, $listOrder); ?> </th>
puis à la ligne 135 :
<td class="center"> <?php $checked=""; if ($item->pret == 1){ $checked="checked"; ?> <input type="checkbox" name="pret[]" id="pret" value="<?php echo $item->pret;?>" <?php echo $checked;?> disabled="disabled" > <?php };?> </td>
Dans le fichier de langue ajouter les déclarations suivantes :
COM_LIVRES_PRET_TITLE="Prêt"
COM_LIVRES_FIELD_PRET_LABEL="Livre en prêt"
COM_LIVRES_FIELD_PRET_DESC="Cocher si livre prété"
administrator\components\com_livres\views\livre\tmpl\edit.php
Ajouter à la ligne 45 :
<li><?php echo $this->form->getLabel('pret'); ?> <?php echo $this->form->getInput('pret'); ?></li>
administrator\components\com_livres\models\livre.php
La fonction save est :
public function save($data) { // Alter the title for save as copy if (JRequest::getVar('task') == 'save2copy') { list($title,$alias) = $this->generateNewTitle($data['catid'], $data['alias'], $data['title']); $data['title'] = $title; $data['alias'] = $alias; } if (parent::save($data)) { return true; } return false; }
devient
public function save($data) { // Alter the title for save as copy if (JRequest::getVar('task') == 'save2copy') { list($title,$alias) = $this->generateNewTitle($data['catid'], $data['alias'], $data['title']); $data['title'] = $title; $data['alias'] = $alias; } if ( array_key_exists('pret', $data)) { $data['pret']='1'; } else { $data['pret']='0'; } if (parent::save($data)) { return true; } return false; }
Cela nous donne :
Comme aucun livre n'est prêté nous n'avons rien dans la colonne Prêt.
Si nous allons sur un livre :
Cochons (roses) Livre en prêt et sauvegardons.
Si nous changeons dans :
administrator\components\com_livres\views\livres\tmpl\default.php
<td class="center"> <?php $checked=""; if ($item->pret == 1){ $checked="checked"; ?> <input type="checkbox" name="pret[]" id="pret" value="<?php echo $item->pret;?>" <?php echo $checked;?> disabled="disabled" > <?php };?> </td>
par
<td class="center"> <?php $checked=""; if ($item->pret == 1){ $checked="checked"; ?> <?php };?> <input type="checkbox" name="pret[]" id="pret" value="<?php echo $item->pret;?>" <?php echo $checked;?> disabled="disabled" > </td>
Cela donne :
9-Liaison livre-auteur (vue fiche - liste modal)
Nous allons maintenant voir comment appeler la page "Modal" de la table Auteurs.
Il faut d'abord modifier le fichier :
administrator\components\com_livres\models\fields\selectauteur.php
Pour cela nous nous inspirons de jFormFieldUser servi par le fichier :
libraries\cms\form\field\user.php
Donc notre fichier contient :
<?php /** * @package Joomla.Libraries * @subpackage Form * * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE */ defined('JPATH_PLATFORM') or die; /** * Field to select a user id from a modal list. * * @package Joomla.Libraries * @subpackage Form * @since 1.6.0 */ class JFormFieldSelectAuteur extends JFormField { /** * The form field type. * * @var string * @since 1.6.0 */ public $type = 'SelectAuteur'; /** * Method to get the user field input markup. * * @return string The field input markup. * * @since 1.6.0 */ protected function getInput() { // Initialize variables. $html = array(); $link = 'index.php?option=com_livres&view=auteurs&layout=modal&tmpl=component&field=' . $this->id; // Initialize some field attributes. $attr = $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : ''; $attr .= $this->element['size'] ? ' size="' . (int) $this->element['size'] . '"' : ''; // Initialize JavaScript field attributes. $onchange = (string) $this->element['onchange']; // Load the modal behavior script. JHtml::_('behavior.modal', 'a.modal_' . $this->id); // Build the script. $script = array(); $script[] = ' function jSelectAuteur_' . $this->id . '(id, title) {'; $script[] = ' var old_id = document.getElementById("' . $this->id . '_id").value;'; $script[] = ' if (old_id != id) {'; $script[] = ' document.getElementById("' . $this->id . '_id").value = id;'; $script[] = ' document.getElementById("' . $this->id . '_name").value = title;'; $script[] = ' ' . $onchange; $script[] = ' }'; $script[] = ' SqueezeBox.close();'; $script[] = ' }'; // Add the script to the document head. JFactory::getDocument()->addScriptDeclaration(implode("\n", $script)); $table = JTable::getInstance('auteur','livrestable'); if ($this->value) { $table->load($this->value); } else { $table->username = JText::_('JLIB_FORM_SELECT_USER'); } // Create a dummy text field with the user name. $html[] = '<div class="fltlft">'; $html[] = ' <input size="40" type="text" id="' . $this->id . '_name"' . ' value="' . htmlspecialchars($table->title, ENT_COMPAT, 'UTF-8') . '"' . ' disabled="disabled"' . $attr . ' />'; $html[] = '</div>'; // Create the user select button. $html[] = '<div class="button2-left">'; $html[] = ' <div class="blank">'; if ($this->element['readonly'] != 'true') { $html[] = ' <a class="modal_' . $this->id . '" title="' . JText::_('COM_LIVRES_CHANGE_AUTEUR') . '"' . ' href="' . $link . '"' . ' rel="{handler: \'iframe\', size: {x: 800, y: 500}}">'; $html[] = ' ' . JText::_('COM_LIVRES_CHANGE_AUTEUR') . '</a>'; } $html[] = ' </div>'; $html[] = '</div>'; // Create the real field, hidden, that stored the user id. $html[] = '<input type="hidden" id="' . $this->id . '_id" name="' . $this->name . '" value="' . (int) $this->value . '" />'; return implode("\n", $html); } /** * Method to get the filtering groups (null means no filtering) * * @return mixed array of filtering groups or null. * * @since 1.6.0 */ protected function getGroups() { return null; } /** * Method to get the users to exclude from the list of users * * @return mixed Array of users to exclude or null to to not exclude them * * @since 1.6.0 */ protected function getExcluded() { return null; } }
Il faut rajouter dans le fichier de langue :
COM_LIVRES_CHANGE_AUTEUR="Choisir un auteur"
Cela donne :
L'auteur est renseigné avec sa valeur actuelle et un bouton appelle :
Il suffit de cliquer sur un nom d'auteur pour l'attribuer à ce livre.