index_gen.ctp 9.4 KB
<?php

/*
 * ***************************
 * VUE LISTE GÉNÉRIQUE (index)
 * ***************************
 */


$DEBUG=false;
//$DEBUG=true;

// Variables passées par le contrôleur

// pas bien, mais pratique
$controller = $controller;

$CAN_ADD = $CAN_ADD;
$role = $role;
$nb_entities = $nb_entities;
$entities = $entities;
$entity_plural = $entity_plural;
$fields_names = $fields_names;
$HAS_ADD_BUTTON = $HAS_ADD_BUTTON;
$HAS_EDIT_DEL_BUTTONS = $HAS_EDIT_DEL_BUTTONS;
$sortWhitelist = $sortWhitelist;


/*
// Ajout du champ "nom" automatique
foreach ($fields_names as $f=>$infos) $new_fields_names[$f] = $infos;
$fields_names = $new_fields_names;
// Si champ 'nom', ajout auto du nice name
debug($fields_names);
if (empty($fields_names['nom'])) $fields_names['nom'] = ['nice_name'=>'Nom'];
*/
//debug($fields_names);

//<div class="stats index large-9 medium-8 columns content">
?>




<div class="entities index">

	<?php 
	echo "<h2><i class='icon-list'></i> Liste des $entity_plural ($nb_entities)</h2>"; 
	
	if ($this->fetch('list_header')): ?>
        <div class="list_header">
            <?= $this->fetch('list_header') ?>
        </div>
        <?php 
    endif;

	if ($HAS_ADD_BUTTON && $CAN_ADD) echo $this->element('button_add');
	
    //<table cellpadding="0" cellspacing="0" 
	?>
    <table style="border-collapse: separate; border-spacing: 0;width: 270px;">
		
		<!-- EN-TETES -->
		<thead>
			<tr>
				<!-- Boutons action -->
				<th class="actions"><?= __('') ?></th>

				<!--  Noms des champs -->
				<?php 
				//foreach ($fields_names as $fname=>$fnicename) echo "<th>".$this->Paginator->sort($fname, $fnicename)."</th>";
				//foreach ($fields_names as $fname=>$infos) {
			    foreach ($fields_names as $fname=>$infos) {
			        // Joli nom pour le titre de la colonne (nice name)
			        $nice_name = isset($infos['nice_name']) ? $infos['nice_name'] : $fname;
			        if ( in_array($fname, array_keys($sortWhitelist)) ) $fname = $sortWhitelist[$fname];
			        /*
			        $sort_name = isset($infos['sort_name']) ? $infos['sort_name'] : $fname; 
			        echo "<th>".$this->Paginator->sort($sort_name, ucfirst($nice_name))."</th>";
			        */
			        echo "<th>".$this->Paginator->sort($fname, ucfirst($nice_name))."</th>";
			    }
		        ?>
			</tr>
		</thead>
		
		<!--  DATA -->
		<tbody>
            <?php foreach ($entities as $e): 
            //debug($e);
            ?>
            <tr>
            
            	<!-- 
            	Boutons edit / delete 
            	-->
				<td class="actions" style="padding: 6px 0; text-align: left;">   
                	<?php
                	if ($HAS_EDIT_DEL_BUTTONS) {
                	//debug(compact('id','CAN_EDIT','CAN_DELETE'));
                    $id = $e->id;
                    //$CAN_EDIT = in_array($role, ['Administration', 'Super Administrateur']);
                    $CAN_EDIT = $controller->isAuthorizedAction('edit', $id);
                    $CAN_DELETE = $controller->isAuthorizedAction('delete', $id);
                    //$CAN_DELETE=true;
                    $style_num=1;
                    $WITH_DIV = false;
                	echo $this->element('buttons_edit_del', compact('id','CAN_EDIT','CAN_DELETE', 'style_num', 'WITH_DIV'));
                	}
               		?>
                </td>
				
				<!--
				DATA : affichage de chaque champ dans une colonne
				<td class="smallText"><= $this->Html->link($e->nom, ['action' => 'view', $e->id]) ?></td>
				<td class="smallText"><= $e->has('sur_category') ? $this->Html->link($e->sur_category->nom, ['controller' => 'SurCategories', 'action' => 'view', $e->sur_category->id]) : '' ?></td>
				 -->
				<?php
				$entity = $e->toArray();
				//debug($entity);
				//foreach ($e->toArray() as $fname=>$fval) {
				foreach ($fields_names as $fname=>$infos) {
				    // $fval est soit un champ physique soit un champ virtuel (calculé par le Model)
				    $fval = isset($entity[$fname]) ? $entity[$fname] : $e->connexDurAvg;
		            //debug($fname);
		            //if (! in_array($fname, array_keys($fields_names)) ) continue;
		            //debug($fname);
		            //<td class="smallText"><?= $this->Html->link($e->nom, ['action' => 'view', $e->id]) ></td>
		            echo "<td class='smallText'>";
		                
		                // - S'il y a une fonction de transformation à appliquer, ben... on l'applique
		                //var_dump($fname);
		                if ( isset($fields_names[$fname]['f']) ) {
		                    $f = $fields_names[$fname]['f'];
		                    //debug($f); debug($fval); exit;
		                    //var_dump("Y");
		                    echo $this->MyHelper->$f($fval);
		                }
		                
		                // - Si c'est le champ "nom" ou "id", on l'affiche comme un lien
		                elseif ($fname == 'nom' || $fname == 'id') 
		                    echo $this->Html->link($fval, ['action' => 'view', $e->id]);
		                
		                // - Si le champ est boolean => on affiche oui/non
	                    //elseif ( (substr($fname,0,3)=='is_') && ($fval===false || $fval===true) )
	                    elseif (
        	                       ( substr($fname,0,3)=='is_' || isset($infos['bool_values']) )
        	                       && 
        	                       ( $fval==false || $fval==true )
                        ) {
                           $bool_values = isset($infos['bool_values']) ? $infos['bool_values'] : ['Oui','Non'];
                           //echo $fval ? "Oui" : "Non";
                           echo $fval ? $bool_values[0] : $bool_values[1];
                        }
		                
		                // - Si le champ est une FK => on récupère la valeur du 'contained'
		                //elseif ( strpos($fname,'_id') !== strlen($fname)-3 )
		                elseif ( substr($fname,-3) == '_id' ) {
		                    
		                    //var_dump($fname);
    	                    //$fval = $e->has('sur_category') ? $e->sur_category->nom : $fval;
    	                    // ex: materiel
    	                    $contained_entity_name = $fields_names[$fname]['contained_entity_name'];
    	                    // ex: Materiels
    	                    $controller_name = $fields_names[$fname]['controller_name'];
    	                    //$linkval = $e->has($contained_entity_name) ? $e->$contained_entity_name->nom : $fval;
    	                    $name_field_label = $infos['name_field_label']; // 'nom' ou 'designation' ou ...

		                    //$projet->has('chef_science_id') ? $this->Html->link($projet->chef_science->nom, ['controller' => 'Users', 'action' => 'view', $projet->chef_science_id]) : '' ></td>
    	                    //$linkval = $e->has($contained_entity_name) ? $e->$contained_entity_name->$name_field_label : $fval;
    	                    if ($e->has($contained_entity_name)) {
    	                        if ( isset($fields_names[$fname]['improved']) ) {
    	                            $fk_field = $fields_names[$fname]['improved'];
    	                            $linkval = $e->$fk_field;
    	                        }
    	                        else 
    	                            $linkval = $e->$contained_entity_name->$name_field_label;
    	                    }
    	                    else $linkval = $fval;
    	                    //echo $this->Html->link($fval, ['controller' => $controller_name, 'action' => 'view', $e->sur_category->id]);
    	                    echo $this->Html->link($linkval, ['controller' => $controller_name, 'action' => 'view', $fval]);
    	                    /*
    	                    //debug($fname);
    	                    $parent_entity_infos = &$parent_entities_infos[$fname];
    	                    //debug($parent_entity_infos);
    	                    $fk_contained_name = $parent_entity_infos['fk_contained_name'];
    	                    //debug($fk_contained_name);
    	                    //$fvalue = $associated_entity_infos['name'];
    	                    $fvalue = $entity->$fk_contained_name->nom;
    	                    $this->MyHelper->displayElement(
    	                        __(ucfirst($parent_entity_infos['type_name'])),
    	                        //$category->has('sur_category') ? $this->Html->link($category->sur_category->nom, [
    	                        $this->Html->link($fvalue, [
    	                            'controller' => $parent_entity_infos['controller_name'],
    	                            //'controller' => 'SurCategories',
    	                            'action' => 'view',
    	                            //$associated_entity_infos['id']
    	                            $fval
    	                            //$category->sur_category->id
    	                        ])
	                        );
    	                    */    
    	                } // FK
    	                // - Sinon, champ quelconque => on l'affiche directement
    	                else
    	                    echo $fval;
    	                    
	                echo "</td>";
		        } // foreach entity fields
				?>
			
			</tr>
            <?php 
            //break;
            endforeach; // foreach entities ?>
        </tbody>
        
	</table>
	
	<?php echo $this->element('pagination_with_first_and_last'); ?> 
	

</div>

<!--
<div class="actions">
			<php echo $this->element('menu') ?>
			<php

echo $this->element('menu_index', [
    'pluralHumanName' => 'Catégories',
    'singularHumanName' => 'Catégorie'
])?>
		</div>
-->