/** * AjaxServer::getCollectionForSelect() * * @param string $entity * @param array $filter * @access public * @return string json encoded array */ public function getCollectionForSelect($entity, $filter = array()) { $return = array(); $array = SearchTools::createArrayIDFromCollection($entity, $filter); foreach ($array as $id => $toString) { $return[] = array('id' => $id, 'toString' => utf8_encode($toString)); } return json_encode($return); }
/** * Grid::getItemIds() * retourne un tableau des items checkbox checkés * * @access private * @return array **/ private function _getItemIds() { $sessionVarName = SearchTools::getGridItemsSessionName(); if (!isset($_SESSION[$sessionVarName])) { $_SESSION[$sessionVarName] = array(); } if (!isset($_REQUEST['gridItems'])) { $_REQUEST['gridItems'] = array(); } if (!isset($_REQUEST['toRemove'])) { $_REQUEST['toRemove'] = ''; } $gridItems = array_unique(array_merge($_SESSION[$sessionVarName], $_REQUEST['gridItems'])); $gridItems = array_diff($gridItems, explode('|', $_REQUEST['toRemove'])); $session = Session::Singleton(); $session->register($sessionVarName, $gridItems, 2); unset($_REQUEST['toRemove'], $_REQUEST['gridItems']); $return = array_merge($this->getPreselectedItems(), $gridItems); if (!empty($return)) { $this->_gridItems = $return; } return $this->_gridItems; }
/** * Construit la requête SELECT complète. Cette méthode est utilisé par * Mapper::load et Mapper::loadCollection * * @access private * @param mixed $attributeFilters un tableau ou un objet filtre * @param array $sortOrder un tableau pour les tris * @return string la requête SELECT */ private function _getSQLRequest($attributeFilters = array(), $sortOrder = array(), $fields = array()) { $filteraddon = $params = array(); if (false != call_user_func(array($this->_cls, 'getParentClassName'))) { $filteraddon['ClassName'] = $this->_cls; $params[] = 'ClassName'; } if ($attributeFilters instanceof FilterComponent) { $filter = SearchTools::buildFilterFromArray($filteraddon, $attributeFilters); $filterMacros = $attributeFilters->CollectMacros(); } else { if (is_array($attributeFilters)) { $filter = SearchTools::buildFilterFromArray(array_merge($filteraddon, $attributeFilters), false, $this->_cls); $filterMacros = $filter->CollectMacros(); } } // si fields est un tableau if (empty($fields)) { $params = array('*'); } else { if (!in_array('Id', $fields)) { $params[] = 'Id'; } if (defined('DATABASE_ID') && !in_array('DBId', $fields)) { $params[] = 'DBId'; } if (property_exists($this->_cls, 'lastModified')) { $params[] = 'LastModified'; } $params = array_merge($params, $fields); } // on appelle le state machine pour construire la requête $stateMachine = new StateMachine($this->_cls, $params, $filterMacros, $sortOrder); return $stateMachine->toSQL($filter); }
/** * Appelé entre autres par Mapper::_getSQLRequest() pour construire un objet * Filter si le paramètre $attributeFilters est un tableau * * @access public * @param array $array le tableau filtre * @return object FilterComponent */ public static function buildFilterFromArray($array = array(), $filter = false, $cls = '') { $metafilter = new FilterComponent(); $metafilter->operator = FilterComponent::OPERATOR_AND; if ($filter instanceof FilterComponent) { if (empty($array)) { return $filter; } $metafilter->setItem($filter); } foreach ($array as $filterName => $filterValue) { if (false !== strpos($filterName, '.')) { $operator = is_array($filterValue) ? 'In' : 'Equals'; $component = SearchTools::newFilterComponent($filterName, $filterName, $operator, $filterValue, 1, $cls); $metafilter->setItem($component); } else { $operator = is_array($filterValue) ? FilterRule::OPERATOR_IN : FilterRule::OPERATOR_EQUALS; $rule = new FilterRule($filterName, $operator, $filterValue); $metafilter->setItem($rule); } } return $metafilter; }
/** * 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; } }
/** * Transforme une date issue d'un Widget de date de QuickForm de la forme: * 'dMY H:i' * * @static * @param $fieldName string nom du champs * @access public * @return string */ static function quickFormDateToMySQL($fieldName) { if (SearchTools::requestOrSessionExist($fieldName) == false) { return false; } $date = SearchTools::requestOrSessionExist($fieldName); $month = isset($date['m']) ? $date['m'] : $date['M']; $return = $date['Y'] . '-' . sprintf('%02d', $month) . '-' . sprintf('%02d', $date['d']); if (isset($date['H'])) { $return .= ' ' . sprintf('%02d', $date['H']) . ':' . sprintf('%02d', $date['i']) . ':' . '00'; } return $return; }
/** * _getSearchFormAttributes * * Retourne les attributs à utiliser pour l'ajout de l'élément au * searchform. * * @param string $elmName Nom de la propriété * @access private * @return void */ private function _getSearchFormAttributes($elmName) { $elmType = $this->getElementType($elmName); if ($elmType == Object::TYPE_CONST) { $method = sprintf('get%sConstArray', $elmName); $array = call_user_func(array($this->clsname, $method)); return array(array(GenericController::FAKE_INDEX => MSG_SELECT_AN_ELEMENT) + $array); } if ($elmType == Object::TYPE_FKEY) { $clsName = $this->attrs[$elmName]; $fGetter = 'getFilterFor' . $elmName; $filter = method_exists($this, $fGetter) ? $this->{$fGetter}() : array(); $tGetter = 'getToStringAttributeFor' . $elmName; $toString = method_exists($this, $tGetter) ? $this->{$tGetter}() : 'toString'; return array(SearchTools::CreateArrayIDFromCollection($clsName, $filter, MSG_SELECT_AN_ELEMENT, $toString)); } if ($elmType == Object::TYPE_BOOL) { return array(array('##' => _('Any'), '1' => _('Yes'), '0' => _('No'))); } if ($elmType == Object::TYPE_MANYTOMANY) { $elmName = $this->links[$elmName]['linkClass']; return array(SearchTools::CreateArrayIDFromCollection($elmName, array(), MSG_SELECT_MANY_ELEMENTS, 'toString', array())); } return array(); }
/** * Retourne un objet HTML_QuickForm_Element construit avec les paramètres * passés. * * @access private * @param string $name le nom de l'attribut * @param string $ename le nom de l'élément de formulaire correspondant * @param string $type le type de l'attribut * @param string $label le label de l'élément de formulaire * @param array $opts un tableau d'options pour l'élément de formulaire * @param bool $req determine si l'élément est requis * @param bool $ipe determine si l'élément fkey doit être édité dans le form * @param bool $aeButton determine si l'élément fkey doit utiliser un bouton addedit * @return object HTML_QuickForm_Element */ private function _createElement($name, $ename, $type, $label, $opts, $req, $ipe, $aeButton) { $ret = array(); $opts['id'] = $ename; $labelWithSemiColon = $label . ': '; if ($type == Object::TYPE_FKEY) { // type fkey: on construit un select $objPropertiesArray = $this->object->getProperties(); $className = $objPropertiesArray[$name]; require_once MODELS_DIR . '/' . $className . '.php'; $tmpobject = new $className(); $getter = 'get' . $name . 'Id'; $objID = $this->object->{$getter}(); // denormalized forms if ($ipe) { $customFileName = CUSTOM_CONTROLLER_DIR . '/' . $className . 'AddEdit.php'; if (file_exists(PROJECT_ROOT . '/' . LIB_DIR . '/' . $customFileName)) { require_once $customFileName; $class = $className . 'AddEdit'; } else { $class = 'GenericAddEdit'; } $this->innerForm = new $class(array('clsname' => $className, 'id' => $objID, 'return_url' => $this->guessReturnURL(), 'profiles' => $this->profiles)); $this->innerForm->parentForm = $this; $this->innerForm->initialize(); $this->innerForm->render(); $mapping = array_keys($tmpobject->getMapping()); $this->form->addElement('hidden', $ename . '_ID', $objID); $ret[] = HTML_QuickForm::createElement('header', $ename, $label); foreach ($this->innerForm->formDefaults as $k => $v) { $elt = $this->innerForm->form->getElement($k . '_Group'); if ($elt instanceof HTML_QuickForm_Element) { $ret[] = $elt; } $elt = $this->innerForm->form->getElement($k); if ($elt instanceof HTML_QuickForm_Element) { $ret[] = $elt; } $this->formDefaults[$k] = $v; } } else { $toStringAttribute = call_user_func(array($tmpobject, 'getToStringAttribute')); $sortOrderField = is_array($toStringAttribute) ? $toStringAttribute[0] : $toStringAttribute; $fGetter = 'getFilterFor' . $name; $filter = method_exists($this, $fGetter) ? $this->{$fGetter}() : array(); $arr = SearchTools::createArrayIDFromCollection($className, $filter, $req ? '' : MSG_SELECT_AN_ELEMENT, 'toString', array($sortOrderField => SORT_ASC)); $ename .= '_ID'; $opts['id'] = $ename; if ($aeButton && $this->action != self::FEATURE_VIEW) { $opts['class'] = 'select_with_add_button'; $ret[] = HTML_QuickForm::createElement('select', $ename, $labelWithSemiColon, $arr, $opts); $retURL = urlencode(sprintf('%s?action=%s&entity=%s&objID=%d', $_SERVER['PHP_SELF'], $this->action, $this->altname, $this->objID)); $url = sprintf('dispatcher.php?entity=%s&action=add&retURL=%s&fromEntity=%s', $className, $retURL, $this->clsname . ':' . $name); $ret[] = HTML_QuickForm::createElement('button', 'addButton' . $name, A_ADD, 'id="addButton' . $name . '" class="button" onclick="' . 'this.form.redirectURL.value=\'' . $url . '\';' . 'this.form.fromAddButton.value=1;this.form.submit();"'); } else { $opts['class'] = 'select'; $ret[] = HTML_QuickForm::createElement('select', $ename, $labelWithSemiColon, $arr, $opts); } } } else { if ($type == Object::TYPE_CONST) { // type constante: on construit un select aussi $method = sprintf('get%sConstArray', $name); $arr = call_user_func(array($this->clsname, $method)); $opts['class'] = 'select'; $ret[] = HTML_QuickForm::createElement('select', $ename, $labelWithSemiColon, $arr, $opts); } else { if (in_array($type, array(Object::TYPE_DATE, Object::TYPE_DATETIME))) { // type date ou datetime: un calendrier require_once 'HTML/QuickForm/jscalendar.php'; $options = array('baseURL' => 'js/jscalendar/', 'styleCss' => JSCALENDAR_DEFAULT_CSS, 'language' => I18N::getLocaleCode(true), 'setup' => array('inputField' => $ename, 'button' => $ename . '_calendar_trigger', 'displayArea' => 'displayed' . $ename, 'ifFormat' => '%Y-%m-%d', 'daFormat' => '\'+Calendar._TT["DEF_DATE_FORMAT"]+\'' . ($type == Object::TYPE_DATETIME ? ' %H:%M' : ''), 'showsTime' => $type == Object::TYPE_DATETIME ? true : false, 'showOthers' => true)); $attributes = array('readonly' => 'readonly', 'class' => 'ReadOnlyField', 'rows' => 1, 'style' => 'width:30%;height:14px;', 'id' => 'displayed' . $ename); $ret[] = HTML_QuickForm::createElement('hidden', $ename, null, array('id' => $ename)); // vu que jscalendar utilise innerhtml, on ne peut mettre ici un input text // Du coup, ce 'bidouillage' avec un textarea $ret[] = HTML_QuickForm::createElement('textarea', 'displayed' . $ename, null, $attributes); /*$ret[] = HTML_QuickForm::createElement('static', 'displayed'.$ename, null, '<span id="displayed'. $ename . '" name="displayed'. $ename . '" style="border: 1px dotted rgb(0, 0, 0); margin: 3px; padding: 3px; background-color: rgb(225, 232, 239); text-align: right;">toto</span>' );*/ $ret[] = HTML_QuickForm::createElement('button', $ename . '_calendar_trigger', '...', array('title' => _('Select a date'), 'id' => $ename . '_calendar_trigger', 'class' => 'button')); $ret[] = HTML_QuickForm::createElement('jscalendar', $ename . '_calendar', null, $options); } else { if ($type == Object::TYPE_TIME) { $fmt = array('language' => I18N::getLocaleCode(true), 'format' => 'H:i'); $opts['class'] = 'datetime'; $ret[] = HTML_QuickForm::createElement('date', $ename, $labelWithSemiColon, $fmt, $opts); } else { if (in_array($type, array(Object::TYPE_TEXT, Object::TYPE_I18N_TEXT, Object::TYPE_LONGTEXT))) { // type texte ou longtext: un textarea $opts['rows'] = 10; $opts['cols'] = 40; $opts['class'] = 'textarea'; $ret[] = HTML_QuickForm::createElement('textarea', $ename, $labelWithSemiColon, $opts); } else { if ($type == Object::TYPE_BOOL) { $opts['id'] = $ename . '_0'; $ret[] = HTML_QuickForm::createElement('radio', $ename, $labelWithSemiColon, A_YES, 1, $opts); $opts['id'] = $ename . '_1'; $ret[] = HTML_QuickForm::createElement('radio', $ename, $labelWithSemiColon, A_NO, 0, $opts); } else { if ($type == Object::TYPE_PASSWORD) { $opts['value'] = "this.value='" . self::PW_NOCHG . "';"; $ret[] = HTML_QuickForm::createElement('password', $ename, $labelWithSemiColon, $opts); $opts['id'] = $ename . '_Again'; $label = $label . ' (' . _('confirm') . ')'; $ret[] = HTML_QuickForm::createElement('password', $opts['id'], $labelWithSemiColon, $opts); } elseif ($type == Object::TYPE_MANYTOMANY) { require_once 'HTML/QuickForm/advmultiselect.php'; // type many2many: on construit deux selects avec advmultiselect $objPropertiesArray = $this->object->getProperties(); $className = $this->links[$name]['linkClass']; //objPropertiesArray[$name]; require_once MODELS_DIR . '/' . $className . '.php'; $tmpobject = new $className(); $toStringAttribute = call_user_func(array($tmpobject, 'getToStringAttribute')); $sortOrderField = is_array($toStringAttribute) ? $toStringAttribute[0] : $toStringAttribute; $toString = is_array($toStringAttribute) ? 'toString' : $toStringAttribute; $filterGetter = 'getFilterFor' . $name; if (method_exists($this, $filterGetter)) { $filter = $this->{$filterGetter}(); } else { $filter = array(); } $arr = SearchTools::createArrayIDFromCollection($className, $filter, '', $toString, array($sortOrderField => SORT_ASC)); $opts['size'] = 8; $opts['style'] = 'width:100%;'; $ret[] = HTML_QuickForm::createElement('advmultiselect', $ename, array($label, $label, ''), $arr, $opts); } elseif (in_array($type, array(Object::TYPE_FILE, Object::TYPE_IMAGE, Object::TYPE_FILE_UPLOAD))) { $opts['class'] = 'textfield'; $ret[] = HTML_QuickForm::createElement('file', $ename, $labelWithSemiColon, $opts); } elseif ($type == Object::TYPE_HTML || $type == Object::TYPE_I18N_HTML) { // type html: un editeur wysiwyg // lib: HTML_QuickForm_Element_tinymce require_once 'HTML/QuickForm/tinymce.php'; $options = array('baseURL' => 'js/tinymce/jscripts/tiny_mce/', 'configFile' => 'js/tinymce.inc.js'); $opts['class'] = 'mceEditor'; $ret[] = HTML_QuickForm::createElement('tinymce', $ename, $labelWithSemiColon, $options, $opts); } elseif ($type == Object::TYPE_HEXACOLOR) { // type hexacolor: un colorpicker $this->addJSRequirements('js/colorpicker.js'); require_once 'HTML/QuickForm/colorpicker.php'; $ret[] = HTML_QuickForm::createElement('colorpicker', $ename, $labelWithSemiColon, array(), $opts); } else { // les autres types sont des textfield $opts['class'] = 'textfield'; $ret[] = HTML_QuickForm::createElement('text', $ename, $labelWithSemiColon, $opts); } } } } } } } if (!$ipe) { foreach ($ret as $elt) { if ($req) { $elt->setAttribute('class', $elt->getAttribute('class') . ' required_element'); } } } return $ret; }