MyHelperHelper.php 15.9 KB
<?php

/* src/View/Helper/ButtonHelper.php */
namespace App\View\Helper;

use Cake\View\Helper;
use phpDocumentor\Reflection\Types\Void_;

//class MyButtonHelper extends Helper {
class MyHelperHelper extends Helper {
        
    // On a besoin du HtmlHelper
    public $helpers = ['Html', 'Form'];
    
    public function initialize(array $config) { 
        //debug($config);
    }

    
    function getHourMnSecForDuration($duration_sec) {
        //debug($duration_sec);
        // PHP7 only (intdiv)
        //$h = intdiv($duration_sec,3600);
        // PHP5 !!!
        $h = (int)($duration_sec/3600);
        $m = (int)( ($duration_sec - $h*3600) / 60 );
        $s = $duration_sec - $h*3600 - $m*60;
        //return [$h,$m,$s];
        return "$h h $m mn $s sec";
    }
    
    
    // (EP) Fonction utilisée dans la vue Materiels/add_or_edit
    function isReadonlyField ($fieldName, $readonlyFields) {
        
        // - Fonctionnement inversé : TOUS readonly (*) SAUF certains champs (listés)
        //if (!empty($myReadonlyFields) && $myReadonlyFields[0]=='*') {
        if (!empty($readonlyFields) && array_key_exists('*', $readonlyFields)) {
            /*
            $modifiableFields = $readonlyFields;
            array_shift($modifiableFields);
            */
            $modifiableFields = $readonlyFields['*'];
            return ! in_array($fieldName, $modifiableFields);
        }
        
        // - Fonctionnement normal :
        //return ( !empty($materiel->$fieldName) && in_array($fieldName, $myReadonlyFields) );
        //return ( in_array($fieldName, $readonlyFields) );
        return ( array_key_exists($fieldName, $readonlyFields) );
        
    }

    /*
    private function getLabelForFieldName ($fieldName) {
        if ( substr($fieldName, -3) === '_id' ) $fieldName = substr($fieldName,0,-3);
        return ucfirst($fieldName);
    }
    */
        
    // (EP) Fonction utilisée dans la vue Materiels/add_or_edit
    function control(
            $IS_ADD, $readonlyFields, $fieldName, $label=null, 
            $empty=null, $default=null, $type=null, $class=null, $placeholder=null, $value=null
        ) {

        $fieldValue = $this->_View->viewVars['entity']->$fieldName;
            
        $options = [
            //'type' => 'text',
            //'label' => $label ? $label : $this->getLabelForFieldName($fieldName),
            'label' => $label, // si null, c'est le framework qui génère un label auto
            //'empty' => 'Choisir un domaine',
            'empty' => $empty,
            'default' => $default,
            
            'type' => $type,
            'class' => $class,
            'placeholder' => $placeholder,
            //'value' => $value,
            
            //'style' => 'width: 260px',
            // inutile car automatique si le nom de la variable passée par le controleur respecte la norme
            //'options' => $surCategories,
            //'default' => $Designation,
            // ADD only
            /////'default' => $materiel->designation,
            //'default' => $designations,
            //'default' => $designations->toArray(),
            // EDIT only
            
            // disabled ssi EDIT && readonly && pas vide (jamais disabled si mode ADD)
            'disabled' => !$IS_ADD && $this->isReadonlyField($fieldName, $readonlyFields) && !empty($fieldValue)
            //'disabled' => $IS_ADD ? false : $this->isReadonlyField($fieldName, $readonlyFields) && !empty($this->Form->getData($fieldName))
        ];
        //if (!is_null($default)) $options['default'] = $default;
        if (!is_null($value)) $options['value'] = $value;
        
        return $this->Form->control($fieldName, $options);
        
    }
    
    
    function echoListToManageOrViewWithIcon($can_manage=false, $list_name, $controller_name, $action_name='index') {
        //function echo_list($html, $verb, $list_name, $controller_name, $action_name) {
        $verb = $can_manage ? 'Gérer' : 'Voir';
        $icon_name = $can_manage ? 'config2.png' : 'see2.png';
        $this->echoMenuItemWithIcon($icon_name, "$verb les $list_name", $controller_name, $action_name, 'nom');
    }
    
    
    function echoMenuItemPostLinkWithIcon($icon_name, $title, $controller_name, $action_name) {
        $this->echoMenuItemLinkOrPostLinkWithIcon(true, $icon_name, $title, $controller_name, $action_name);
    }
    
    function echoMenuItemWithIcon($img_name, $title, $controller_name, $action_name=null, $sort_field=null) {
        $this->echoMenuItemLinkOrPostLinkWithIcon(false, $img_name, $title, $controller_name, $action_name, $sort_field);
    }
    
    function echoMenuItemLinkOrPostLinkWithIcon($postlink, $img_name, $title, $controller_name, $action_name=null, $sort_field=null) {
        $icon_size=40;
        echo '<tr>';
        
            echo "<td width=$icon_size>";
                echo $this->Html->image("icons/$img_name", [
                    "alt" => $title,
                    'height' => $icon_size, 'width' => $icon_size,
                    'url' => ['controller' => $controller_name, 'action' => $action_name]
                ]);
            echo '</td>';
        
            echo '<td>';
            if (! $postlink) {
                echo $this->Html->link($title, [
                    'controller' => $controller_name,
                    'action' => $action_name,
                    'sort' => $sort_field,
                ]);
            }
            else {
                echo $this->Form->postlink($title, [
                    'controller' => $controller_name,
                    'action' => $action_name,
                ], [
                    'confirm' => __('Êtes-vous sur ?')
                ]);
            }
            echo '</td>';
            
        echo '</tr>';
    }
    
    public function getActionButton($icon_class, $buttonStyle, $title, $controller, $action, $id, $other_args=[], $tip='', $confirmMessage='', $moreButtonStyle='') {
        if ($controller=='') $controller='materiels';
        $controllerArgs = [];
        $controllerArgs['controller'] = $controller;
        $controllerArgs['action'] = $action;
        $controllerArgs[] = $id;
        foreach ($other_args as $other_arg) $controllerArgs[] = $other_arg;
        return $this->Html->link(
            __("<i class=$icon_class></i>$title"),
            $controllerArgs,
            /*
             [
             'controller' => $controller,
             'action' => $action,
             $id,
             $other_args
             ],
             */
            [
                'title' => $tip,
                'escape' => false,
                'onclick' => 'return true;',
                //'style' => 'margin-right: 10px'.$moreButtonStyle,
                'style' => $buttonStyle,
                'confirm' => $confirmMessage
            ]
        );
    }
    
    public function displayTableRowLabelAndValue($label, $value, $params = "") { $this->displayElement($label, $value, $params); }
    //@deprecated
    public function displayElement($label, $value, $params = "") {
        $TD = ($params=="") ? 'TD' : "TD $params";
        //$TD = ($params=="") ? '<TD>' : '<TD '.$params.'>';
        //$tdstyle = $params!="" ? $params : '';
        // Ca c'est parce que sinon y'a au moins deux tests qui passent pas, a cause de l'espace dans la balise ...
        //if ($valeur != "") echo '<tr> <td><strong>'.$nom.' </strong></td>' . $TD.$valeur.'</td></tr>';
        //if ($valeur != "") echo '<tr><td><strong>' . $nom . ' </strong></td>' . $balise . $valeur . '</td></tr>';
        //if ($valeur!="") echo "<TR> <TD><strong>".__($nom)."</strong></TD> <$TD>".h($valeur)."</TD> </TR>";
        //if ($valeur!="") echo "<TR> <TD><strong>".__($nom)."</strong></TD> <$TD>".$valeur."</TD> </TR>";
        if ($value!==null && $value!=='') {
            $val = $value;
            if ($value === true) $val = 'Oui';
            if ($value === false) $val = 'Non';
            echo "<TR> <TD><strong>".__($label)."</strong></TD> <$TD>".$val."</TD> </TR>";
        }
        //if ($valeur!="") echo "<TR> <TD><strong>$nom</strong></TD> <$TD>$valeur</TD> </TR>";
    }
    
    
    public function echoActionButton (
        $icon_class, $buttonStyle, $title, 
        $action, $id, $controller=null, // si controleur null =>controleur par défaut
        $other_args=[], $tip='', 
        $confirmMessage='', $moreButtonStyle='',
        $with_confirm=false
        ) {
        
        //if ($controller=='') $controller='materiels';
        $controllerArgs = [];
        if ($controller) $controllerArgs['controller'] = $controller;
        $controllerArgs['action'] = $action;
        $controllerArgs[] = $id;
        foreach ($other_args as $other_arg) $controllerArgs[] = $other_arg;

        // Avec confirmation (avant de déclencher l'action)
        if ($with_confirm) {
            if (!$confirmMessage) $confirmMessage = __("Êtes-vous sur de vouloir $title cette entité ?");
            echo $this->Form->postLink(__("<i class='$icon_class'></i>$title"),
                $controllerArgs,
                [
                    'title' => $title,
                    //'style' => $buttonStyle,
                    'style' => 'margin-left: 10px',
                    'escape' => false,
                    'confirm' => $confirmMessage,
                    //'confirm' => $confirmMessage
                    //'confirm' => __("Êtes-vous sur de vouloir $title cette entité ?"),
                    //'confirm' => __('Êtes-vous sur de vouloir supprimer # {0}?', $suivi->id)
                ]);
        }
        
        // Sans confirmation (action directe)
        else {
            echo $this->Html->link(
                __("<i class=$icon_class></i>$title"),
                $controllerArgs,
                /*
                 [
                 'controller' => $controller,
                 'action' => $action,
                 $id,
                 $other_args
                 ],
                 */
                [
                    'title' => $tip,
                    'escape' => false,
                    'onclick' => 'return true;',
                    //'style' => 'margin-right: 10px'.$moreButtonStyle,
                    'style' => $buttonStyle,
                    //'confirm' => $confirmMessage ? $confirmMessage : $tip,
                ]
            );
        }

    }
 
    
    public function echoEditButton () { $this->echoButtonForAction('icon-pencil', 'edit', 'Editer', 'Modifier'); }
    
    public function echoButtonForAction ($icon_class, $action, $label, $tooltip, $with_confirm=false) {
        //$icon_class = 'icon-pencil';
        $buttonStyle = 'margin-right: 10px';
        echo '<div id="boutons" class="actions" style="margin-bottom:20px; width:100%; float:none; padding:5px 0;">';
        $this->echoActionButton($icon_class, $buttonStyle,
            " $label", $action, null, null, [], $tooltip,
            '', '',
            $with_confirm
            );
        echo "</div>";
    }
    
    
    
    public function echoDeleteButton (
        $title,
        $id, $controller=null, // si controleur null =>controleur par défaut
        $other_args=[], $confirmMessage='', $moreButtonStyle=''
        ) {
            
            //if ($controller=='') $controller='materiels';
            $controllerArgs = [];
            if ($controller) $controllerArgs['controller'] = $controller;
            $controllerArgs['action'] = 'delete';
            $controllerArgs[] = $id;
            foreach ($other_args as $other_arg) $controllerArgs[] = $other_arg;
            echo $this->Form->postLink(__("<i class='icon-trash'></i>$title"),
                $controllerArgs,
                [
                    'title' => 'Supprimer',
                    //'style' => $buttonStyle,
                    'style' => 'margin-left: 10px',
                    'escape' => false,
                    //'confirm' => $confirmMessage
                    'confirm' => __('Êtes-vous sur de vouloir supprimer cette entité ?'),
                    //'confirm' => __('Êtes-vous sur de vouloir supprimer # {0}?', $suivi->id)
                ]);
    }
    
    

        
        
    
    
    /* Pour src/Template/Configurations/view et edit */
    public function echoSectionStart($title, $nice_title=null) {
        $icon_class = 'icon-chevron-up';
        // 'icon-chevron-down';
        if (!$nice_title) $nice_title = $title;
        $WITH_TABLE=true;
        //echo '<h3 id="t_'.$section.'" style="cursor: pointer;">';
        //echo "<h3 id='t_$section' class='toggle' style='cursor: pointer;'>";
        echo "<h3 id='t_$title' class='toggle' style='cursor: pointer;'>";
            //echo '<i class="icon-chevron-down" style="font-size: 14px;" id="i_'.$section.'"></i>';
            //echo '<i class="icon-chevron-down" style="font-size: 14px;" id="i_'.$title.'"></i>';
            echo "<i id='i_$title' class='$icon_class' style='font-size: 14px;'></i>";
            echo ' '.'<span style="text-decoration: underline;">'.$nice_title.'</span>';
        echo '</h3>';
        //echo '<div id="'.$section.'" style="margin-bottom: 20px;">';
        echo '<div id="'.$title.'" style="margin-bottom: 20px;">';
        if ($WITH_TABLE) {
            echo '<table>';
            echo '<tr><th style="width: 250px;"></th><th></th></tr>';
        }
    }
    public function echoSectionStop() {
        $WITH_TABLE=true;
        if ($WITH_TABLE) echo '</table>';
        echo '</div>';
    }
    
    
    
    
    //function displaySectionShowHide($controller_name, $entity_type_name, $title, $id_name, $is_masculine, $name_field_name, $entities) {
    //function displaySectionShowHide($controller_name, $entity_type_name, $title, $is_masculine, $name_field_name, $entities) {
    function displayAssociatedEntitiesAsSectionShowHide($controller_name, $entity_type_name, $title, $is_masculin, $name_field_name, $entities) {
        $id_name = $controller_name;
        $id_h3 = 't_'.$id_name;
        $id_i = 'i_'.$id_name;
        $title = ucfirst($title).' associé'. ($is_masculin ? 's' : 'es');
        
        echo "<h3 id='$id_h3' class='toggle' style='cursor: pointer;'>";
        //echo "<i class='icon-chevron-down' style='font-size: 14px;' id='$id_i'></i>";
        echo "<i class='icon-chevron-up' style='font-size: 14px;' id='$id_i'></i>";
        //echo " <span style='text-decoration: underline;'>$title (".$entities->count().")</span>";
        echo " <span style='text-decoration: underline;'>$title (".count($entities).")</span>";
        echo "</h3>";
        
        echo "<div id='$id_name' style='margin-bottom: 20px;'>";
        //if (true) {
        $shift3 = '&nbsp;&nbsp;&nbsp;';
        $shift5 = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
        //if ($entities->isEmpty()) {
        if (empty($entities)) {
            //echo "Aucune $entity_name liée à ce domaine.";
            echo $shift5.($is_masculin ? "Aucun $entity_type_name associé" : "Aucune $entity_type_name associée");
        }
        else {
            echo "<table>";
            /*
             <tr>
             <th><= __('Nom') ?></th>
             <th style="width: 50px;"><= __('Détail') ?></th>
             </tr>
             */
            foreach ($entities as $entity) {
                echo "<tr>";
                    echo "<td>";
                    // Nom de l'entité (ou numéro de l'entité si nom vide, comme pour les emprunts par exemple...)
                    $name = $entity->$name_field_name ? $entity->$name_field_name : "$entity_type_name #".$entity->id;
                    echo $shift3.$this->Html->link(h($name), ['controller' => $controller_name, 'action' => 'view', h($entity->id)]);
                    echo "</td>";
                    /*
                     <td class="actions">
                     <= $this->Html->link(__('<i class="icon-search"></i>'), ['controller' => $controller, 'action' => 'view', $entity->id], ['escape' => false, 'style' => 'margin:0']) ?>
                     </td>
                     */
                echo "</tr>";
            }
            echo "</table>";
        }
        echo "</div>";
    } // displaySectionShowHide()
    
    
}