/** * FormTools::WriteOptionsFromObject() * Prend en paramètres un nom d'objet et retourne un tableau * d'options de type: * <code> * [0=>'<option value="1">Label1</option>', * 1=>'<option value="2">Label2</option>'] * </code> * * @static * @param string $objname le nom de l'objet * @param mixed $selID la ou les valeurs selectionnées par défaut * @param mixed array or Filter: un filtre optionnel à appliquer * @param array $sort un tableau optionnel pour le tri * @param string $labelMethod la méthode à utiliser pour le label * @param array $fields les attributs à récupérer. * @param boolean $addNullEntry true pour ajouter une option 'Sélectionnez * un élément' * @return array the options array */ static function writeOptionsFromObject($objname, $selID = 0, $filter = array(), $sort = array(), $labelmethod = 'toString', $fields = array(), $addNullEntry = false) { $options = array(); $mapper = Mapper::singleton($objname); if (Tools::isException($mapper)) { return $mapper; } if ($labelmethod == 'toString') { $toStringAttribute = Tools::getToStringAttribute($objname); $fields = is_array($toStringAttribute) ? $toStringAttribute : array($toStringAttribute); } $col = $mapper->loadCollection($filter, $sort, $fields); if ($col instanceof Collection) { $dataArray = array(); $count = $col->getCount(); for ($i = 0; $i < $count; $i++) { $item = $col->getItem($i); if (method_exists($item, 'getId') && method_exists($item, $labelmethod)) { $dataArray[$item->getId()] = $item->{$labelmethod}(); } unset($item); } $options = FormTools::writeOptionsFromArray($dataArray, $selID, $addNullEntry); } return $options; }
function getDataCollection($entityName, $ordre, $filtre = array()) { $PageIndex = isset($_REQUEST['PageIndex']) ? $_REQUEST['PageIndex'] : 0; if ($entityName instanceof Collection) { //On a deja une collection, on l'utilise tel qu'elle. return $entityName; } if ($entityName instanceof Mapper) { $aMapper = $entityName; } elseif (is_string($entityName)) { /** * On a un nom d'objet, on cree le mapper associé et on * l'utilise pour charger une collection d'objets */ $aMapper = Mapper::singleton($entityName); } else { return new Exception('$aMapper is not OK'); } $aCollection = $aMapper->loadCollection($filtre, $ordre); return $aCollection; }
/** * Retourne l'Actor qui est DataBaseOwner, oubien une exception * * @access public * @static * @return object Actor */ function getDataBaseOwner() { $actorMapper = Mapper::singleton('Actor'); $dbo = $actorMapper->load(array('DataBaseOwner' => 1)); return $dbo; }
/** * Retourne une collection d'objets pour les propriétés *..* * * @access public * @param int $id l'id de l'objet en cours * @param string $attr le nom de la propriété * @param mixed $attributeFilters un tableau ou un objet filtre * @param array $sortOrder un tableau pour les tris * @param array $fields un tableau de chaines pour les champs à charger * @return object Collection une collection d'objets */ public function getManyToMany($id, $attr, $filter = array(), $sortOrder = array(), $fields = array()) { if ($id == 0) { // on ne doit pas retourner les objets qui ont une fkey à 0 return new Collection(); } $data = $this->_links[$attr]; $ids = array('Id' => $this->getManyToManyIds($id, $attr)); if (empty($ids)) { return new Collection(); } if ($filter instanceof FilterComponent) { $filter = SearchTools::buildFilterFromArray($ids, $filter); } else { $filter = array_merge($ids, $filter); } $mapper = Mapper::singleton($data['linkClass']); return $mapper->loadCollection($filter, $sortOrder, $fields); }
/** * Grid::_getDataCollection() * * @param $entityName * @param $ordre * @param array $filtre * @return object Collection **/ private function _getDataCollection($entityName, $ordre, $filtre = array()) { $pageIndex = isset($_REQUEST['PageIndex']) ? $_REQUEST['PageIndex'] : 0; if ($entityName instanceof Collection) { // On a déjà une collection, on l'utilise tel $ordre = isset($_REQUEST['order']) ? $this->_getOrderArray($_REQUEST['order']) : $ordre; foreach ($ordre as $key => $value) { $entityName->sort($key, $value); } return $entityName; } if (is_string($entityName)) { $entityName = Mapper::singleton($entityName); } $rows = $this->paged ? $this->itemPerPage : 0; $ret = $entityName->loadCollection($filtre, $ordre, array(), $rows, $pageIndex); return $ret; }
/** * Charge une collection d'objet en fonction du filtre et de l'ordre et * récupère pour chacun d'eux les valeurs en fonction de $toString * * @param string $Entity nom de l'entité * @param mixed $Filter filtre (array or Filter) * @param string $toString nom de l'attribut à récupérer * @param array $order ordre de tri * @static * @return array */ static function toStringArray($Entity, $Filter = array(), $toString = 'toString', $order = array()) { $ValuesArray = array(); $mapper = Mapper::singleton($Entity); if ($toString != 'toString' && $toString != 'toStringLite') { if (property_exists($Entity, '_' . $toString)) { $fields = array($toString); } else { // methode addon $fields = array(); } } else { $toStringAttribute = Tools::getToStringAttribute($Entity); $fields = is_array($toStringAttribute) ? $toStringAttribute : array($toStringAttribute); } $Collection = $mapper->loadCollection($Filter, $order, $fields); if (!Tools::isEmptyObject($Collection)) { $count = $Collection->getCount(); $getter = $toString == 'toString' || $toString == 'toStringLite' ? $toString : 'get' . $toString; for ($i = 0; $i < $count; $i++) { $item = $Collection->getItem($i); $ValuesArray[$item->getId()] = $item->{$getter}(); } } return $ValuesArray; }
/** * Affiche le resultat de la recherche * * @param object $grid objet de type Grid * @param boolean $pager : pagination * @param array or object $filter * @param array $order * @param string $title titre de la page * @param array $JSRequirements * @param string $addContent Contenu html à ajouter avant ou apres le Grid * de la forme: array('beforeForm' => '...', // avant le SearchForm * 'between' => '...', // entre le SearchForm et le Grid... * 'afterGrid' => '...') * @return string */ public function displayResult($grid, $pager = false, $filter = array(), $order = array(), $title = '', $JSRequirements = array(), $addContent = array(), $renderFunc = 'page') { // Si on ne passe pas une Collection directemt au Grid::render() if ($this->getItemsCollection() === false) { $mapper = Mapper::singleton($this->entity); if (!$grid->getMapper() instanceof Mapper) { $grid->setMapper($mapper); } } if ($grid->isPendingAction()) { $dispatchResult = $grid->dispatchAction($this->getItemsCollection()); if (Tools::isException($dispatchResult)) { $urlVarArray = array(); // On passe ds l'url les valeurs des hidden s'il y en a $hiddenFields = $this->getHiddenFields(); foreach ($hiddenFields as $key => $value) { if ($key == 'formSubmitted') { continue; } $urlVarArray[] = $key . '=' . $value; } $urlComplement = empty($urlVarArray) ? '' : '?' . implode('&', $urlVarArray); // L'action est-elle de type Popup: $triggeredAction = $grid->getAction($_REQUEST['actionId']); if ($triggeredAction->targetPopup === true) { $tpl = BASE_POPUP_TEMPLATE; $returnURL = 'javascript:window.close()'; } else { $tpl = BASE_TEMPLATE; $returnURL = basename($_SERVER['PHP_SELF']) . $urlComplement; } Template::errorDialog($dispatchResult->getMessage(), $returnURL, $tpl); exit; } } else { if ($this->getItemsCollection() !== false) { $mapper = $this->getItemsCollection(); } $result = $grid->render($mapper, $pager, $filter, $order); $addContent['beforeForm'] = isset($addContent['beforeForm']) ? $addContent['beforeForm'] : ''; $addContent['between'] = isset($addContent['between']) ? $addContent['between'] : ''; $addContent['afterGrid'] = isset($addContent['afterGrid']) ? $addContent['afterGrid'] : ''; $pageContent = $addContent['beforeForm'] . $this->render() . $addContent['between'] . $result . $addContent['afterGrid']; if (isset($_REQUEST['formSubmitted'])) { SearchTools::saveLastEntitySearched(); } Template::$renderFunc($title, $pageContent . '</form>', $JSRequirements); exit; } }
/** * Retourne l'element à l'index $index * * @access public * @param integer $index * @return mixed un objet de la collection ou false */ public function getItem($index) { if (isset($this->_items[$index])) { if (is_numeric($this->_items[$index])) { $mapper = Mapper::singleton($this->entityName); return $mapper->load(array("Id" => $this->_items[$index])); } return $this->_items[$index]; } $return = false; return $return; }
/** * Retourne une collection d'objets de la classe $name. * Voir Core/Mapper.php (loadObjectCollection) pour plus d'infos. * * @access public * @static * @see Mapper::loadObjectCollection() * @param mixed $filter un tableau ou un objet filtre * @param array $order un tableau pour les tris * @param array $fields un tableau de chaines pour les champs à charger * @param integer $rows le nombre de lignes à charger (pagination) * @param integer $page l'index de la page en cours (pagination) * @param integer $limit le nombre d'enregistrements à charger (LIMIT) * @return object Collection */ public static function loadCollection($name, $filter = array(), $order = array(), $fields = array(), $rows = 0, $page = 1, $limit = false, $noCache = false) { $mapper = Mapper::singleton($name); return $mapper->loadCollection($filter, $order, $fields, $rows, $page, $limit, $noCache); }
/** * Méthode qui gère l'action delete, supprime l'objet dans une transaction. * * @access protected * @return void */ protected function delete() { $this->onBeforeDelete(); Database::connection()->startTrans(); $mapper = Mapper::singleton($this->clsname); $emptyForDeleteProperties = call_user_func(array($this->clsname, 'getEmptyForDeleteProperties')); $notDeletedObjects = array(); // il y a des check auto on supprime un à un car les verif ne sont // pas faites par Mapper::delete() mais par Object::delete() $col = $mapper->loadCollection(array('Id' => $this->objID)); $count = $col->getCount(); for ($i = 0; $i < $count; $i++) { $o = $col->getItem($i); try { $o->delete(); } catch (Exception $exc) { $notDeletedObjects[] = $o->toString(); //. ': ' . $exc->getMessage(); } } if (Database::connection()->hasFailedTrans()) { $err = Database::connection()->errorMsg(); trigger_error($err, E_USER_WARNING); Database::connection()->rollbackTrans(); Template::errorDialog(E_ERROR_SQL . '.<br/>' . $err, $this->guessReturnURL()); exit; } Database::connection()->completeTrans(); if (!empty($notDeletedObjects)) { Template::infoDialog(sprintf(I_NOT_DELETED_WITH_LIST, implode('</li><li>', $notDeletedObjects)), $this->guessReturnURL()); exit; } $this->onAfterDelete(); }
/** * AbstractGrid::getMapper() * * @return object Mapper */ public function getMapper() { if ($this->_Mapper instanceof Mapper) { return $this->_Mapper; } if (is_string($this->_Mapper)) { // On a un nom d'objet, on crée le mapper associé $return = Mapper::singleton($this->_Mapper); } else { $return = new Exception('Grid->_Mapper have to be defined.'); } return $return; }