/** * Grid::Render() * * @param mixed $aMapper: * - Collection: on utilise la collection pour le rendu, * - Mapper: on effectue un LoadObjectCollection pour récupérer * une collection qui sera utilisée pour le rendu, * - string: on crée le mapper correspondant au nom de l'objet * donné (Ex: ActivatedChainTask) qui est utilisé comme en 2. * @param boolean $pager * @param array $filtre * @param array $ordre * @param string $templateName * @return string */ public function render($aMapper, $pager = false, $filtre = array(), $ordre = array(), $templateName = GRID_TEMPLATE) { // Pas besoin de customisation dans ce cas if (count($this->columns) == 1) { $this->customizationEnabled = false; } // Prend en compte les preferences pour l'user connecte, si besoin $this->checkPreferences(); if (isset($_REQUEST['export'])) { // si export demande! $pager = false; $this->itemPerPage = 1000000; $_REQUEST['PageIndex'] = 0; } $smarty = new Template(); $gridJS = ''; // pour le comportement "sortable des lignes de grid" // ne marche que si 1 seul grid dans la page et grid paramétré pour if ($this->dndSortable && $this->dndSortableField !== NULL && $this->NbGridsInPage == 1) { // on désactive toute possibilité de tris $this->withNoSortableColumn = true; // l'ordre doit être forcément sur le dndSortableField SORT_ASC $ordre = array($this->dndSortableField => SORT_ASC); // initialise ajax $cli = new AjaxClient(); $smarty->assign('AJAXJavascript', $cli->initialize()); // initialisation du comportement sortable $gridJS .= "connect(window, 'onload', fw.grid.sortableInit);\n"; $smarty->assign('GridDndSortable', true); $smarty->assign('GridDndSortableField', $this->dndSortableField); } $ordre = isset($_REQUEST['order']) ? $this->_getOrderArray($_REQUEST['order']) : $ordre; $aCollection = $this->_getDataCollection($aMapper, $ordre, $filtre); $gridHeader = array(); $gridHeaderGroups = array(); $gridHeaderGroupItemsCount = 0; $tab_ordre = isset($_REQUEST['order']) ? $_REQUEST['order'] : array(); $processedGroups = array(); $ordermap = array(0 => 'NONE', SORT_ASC => 'ASC', SORT_DESC => 'DESC'); foreach ($this->columns as $colIndex => $column) { if (!$column->enabled) { unset($this->columns[$colIndex]); continue; // On n'affiche que les column actives } if (in_array($colIndex, $this->hiddenColumnsByUser)) { continue; // On n'affiche que les column non cachees par le user } $colSortLink = $this->withNoSortableColumn ? '' : $column->getSortLink($tab_ordre); $colSortOrder = $this->withNoSortableColumn ? false : $ordermap[$column->getSortOrder($tab_ordre)]; $gridHeaderItem = array(); if ($column->groupCount) { $gridHeaderItem['GroupCount'] = $column->groupCount; $gridHeaderItem['GroupCaption'] = $column->groupCaption; $gridHeaderGroups[] = array('Caption' => $column->title, 'Link' => $colSortLink, 'SortOrder' => $colSortOrder); if (!isset($processedGroups[$column->groupCaption])) { $gridHeader[] = $gridHeaderItem; $processedGroups[$column->groupCaption] = true; } $gridHeaderGroupItemsCount++; } else { $gridHeaderItem['Caption'] = $column->title; $gridHeaderItem['Link'] = $colSortLink; $gridHeaderItem['SortOrder'] = $colSortOrder; // Pour l'alignement a droite ou a gauche switch ($column->datatype) { case Object::TYPE_INT: case Object::TYPE_FLOAT: case Object::TYPE_DECIMAL: case 'numeric': $gridHeaderItem['DataType'] = 'numeric'; break; default: // alphanumeric $gridHeaderItem['DataType'] = 'alphanumeric'; } $gridHeader[] = $gridHeaderItem; } } if ($this->hasInlineActions) { $gridHeader[] = array('Caption' => ' '); } $gridObjectIds = array(); $highlightedRows = array(); // les lignes qui seront affichees en vert $gridObjectIdsChecked = array(); $gridContent = array(); $gridItemsChecked = $this->_getItemIds(); $rowActions = array(); $count = $aCollection->getCount(); for ($i = 0; $i < $count; $i++) { $gridContent[$i] = array(); $objectInstance = $aCollection->getItem($i); $instanceID = $objectInstance->_Id; if ($instanceID) { $gridObjectIds[$i] = $instanceID; $gridObjectIdsChecked[$i] = in_array($instanceID, $gridItemsChecked) ? 'checked="checked"' : ''; } else { $gridObjectIds[$i] = $i; } // highlighted rows $highlightedRows[$i] = 0; $cond = $this->highlightCondition; if (!empty($cond)) { $objValue = Tools::getValueFromMacro($objectInstance, $cond['Macro']); $cond['Operator'] = $cond['Operator'] == '=' ? '==' : $cond['Operator']; $instr = '$isHighlighted = ($objValue ' . $cond['Operator'] . ' "' . $cond['Value'] . '");'; eval($instr); if ($isHighlighted == true) { $highlightedRows[$i] = 1; } } foreach ($this->columns as $column) { if (!$column->enabled) { continue; // On n'affiche que les column actives } if (in_array($column->index, $this->hiddenColumnsByUser)) { continue; // On n'affiche que les column non cachees par le user } $cellContent = $column->render($objectInstance); $gridContent[$i][] = @get_class($cellContent) == 'exception' ? 'N/A' : $cellContent; } if ($this->hasInlineActions) { $renderer = new GridActionRendererInline($this->javascriptFormOwnerName, $gridObjectIds[$i]); $rowActions[$i] = $renderer->render($this->_Action); } } // Si EXPORT demande if (isset($_REQUEST['export'])) { // On memorise le gridcontent $this->gridContent = $gridContent; $GridExport = $this->_CSVExport(); exit; } $rendererCls = 'GridActionRenderer'; $actionRenderer = new $rendererCls($this->javascriptFormOwnerName); $actions = $actionRenderer->render($this->_Action); $smarty->assign('RowActions', $rowActions); $smarty->assign('GridEntityName', $aCollection->entityName); $smarty->assign('SortDescImage', parent::SORT_DESC_IMAGE); $smarty->assign('SortAscImage', parent::SORT_ASC_IMAGE); $smarty->assign('CancelFilterImage', parent::CANCEL_FILTER_IMAGE); $smarty->assign('CustomDisplayImage', parent::CUSTOM_DISPLAY_IMAGE); $smarty->assign('CustomDisplayBarImage', parent::CUSTOM_DISPLAY_BAR_IMAGE); // Assignation des eventuelles variables supplementaires if (is_array($this->_extraVars)) { foreach ($this->_extraVars as $key => $val) { $smarty->assign($key, $val); } } $items = isset($_REQUEST['items']) ? $_REQUEST['items'] : array(); $smarty->assign('items', $items); $smarty->assign('Pager', $pager); // pour le rowspan $gridHeaderGroupsCount = count($gridHeaderGroups); if ($gridHeaderGroupsCount > 0) { $smarty->assign('RowSpan', 2); } $smarty->assign('GridHeader', $gridHeader); $smarty->assign('GridHeaderCount', count($gridHeader)); $smarty->assign('GridHeaderGroupItemsCount', $gridHeaderGroupItemsCount); $smarty->assign('GridHeaderGroupsCount', $gridHeaderGroupsCount); $smarty->assign('GridHeaderGroups', $gridHeaderGroups); $CurrentPage = $aCollection->currentPage == 0 ? 1 : abs($aCollection->currentPage); $smarty->assign('CurrentPageIndex', $CurrentPage); $PageTotal = $aCollection->lastPageNo == 0 ? 1 : abs($aCollection->lastPageNo); $smarty->assign('PageTotal', $PageTotal); $TotalRowCount = !$pager ? $count : $aCollection->totalCount; $smarty->assign('GridTotalRowCount', $TotalRowCount); $smarty->assign('GridRow', $gridContent); $smarty->assign('GridHighlightedRows', $highlightedRows); // highlighted rows $smarty->assign('GridObjectIds', $gridObjectIds); $smarty->assign('GridObjectIdsChecked', $gridObjectIdsChecked); $smarty->assign('Actions', $actions); // + 1 pour les colspan... $smarty->assign('NbSubColumn', $this->_NbSubGridColumns == 0 ? 0 : $this->_NbSubGridColumns + 1); $smarty->assign('DisplayCancelFilter', $this->displayCancelFilter); $smarty->assign('WithCheckBox', !$this->withNoCheckBox); $smarty->assign('firstGridInPage', $this->NbGridsInPage); $smarty->assign('withMultipleGridsInForm', $this->_withMultipleGridsInForm); $smarty->assign('customizationEnabled', $this->customizationEnabled); if ($pager != false) { $smarty->assign('paging', $this->pagingRender($aCollection->currentPage, $aCollection->lastPageNo)); $smarty->assign('dropfiltertarget', $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING']); } // Gestion du js pour les tris multiples, si 1 seul grid if ($this->NbGridsInPage == 1) { $gridJS .= "// Objet contenant les ordre précédemment sélectionnés pour le grid courant\n"; $gridJS .= "function gridSortOrderItem(filterOrder, columnIndex, sortOrder){\n"; $gridJS .= "\tthis.filterOrder = filterOrder;\n"; $gridJS .= "\tthis.columnIndex = columnIndex;\n"; $gridJS .= "\tthis.sortOrder = sortOrder;\n"; $gridJS .= "}\n"; $gridJS .= "// Rempli lors de désélection d'un item du grid pour \n"; $gridJS .= "// le supprimer en session aussi\n"; $gridJS .= "var toRemove = new Array();\n"; $gridJS .= "var gridSortOrderList = new Array();\n"; if (isset($_REQUEST['order']) && is_array($_REQUEST['order'])) { foreach ($_REQUEST['order'] as $filterOrder => $filterData) { $column = key($filterData); $sortOrder = current($filterData); $gridJS .= "gridSortOrderList[" . $filterOrder . "] = new gridSortOrderItem(" . (int) $filterOrder . ", " . (int) $column . ", " . (int) $sortOrder . ");\n"; } } // Pour gestion des tris lors de la pagination $gridJS .= "connect(window, 'onload', fw.grid.populateGridSortLayer);"; } $smarty->assign('gridJS', $gridJS); $divAddon = $this->NbGridsInPage > 1 ? '' : '<div id="GridSortAddon"></div>'; // Gestion du customize grid: il est possible que precedemment dans // render(), on ait supprime 1 ou n colonnes (enabled=false) if ($this->customizationEnabled && count($this->columns) > 1) { $this->_customDisplay($smarty); } return $smarty->fetch($templateName) . $divAddon; }
/** * Affiche une page utilisant ajax/json. * (@see Template::page() pour la doc et l'exemple). * * @access public * @param string $title * @param string $body * @param array $js un tableau de fichiers js * @param array $css un tableau de fichiers css ('media'=>'fichier') * @return void */ public static function ajaxPage($title, $body = '', $js = array(), $css = array(), $tpl = BASE_TEMPLATE) { $ajaxClient = new AjaxClient(); self::engine()->assign('AJAXJavascript', $ajaxClient->initialize()); self::page($title, $body, $js, $css, $tpl); }