/** * Retourne un FilterComponent * * @param string $Attribute nom de l'attribut sur lequel s'effectue le filtre * @param string $DbTablesPath chemin en cas de jointure, ou '' si pas de jointure * Exple: ''ActivatedOperation.ActivatedChain.CommandItem().Command.CommandNo' * les () expriment une relation 1..* ou *..* * Autre type de path autorise: Command@ProductCommand.CommandItem().Product.BaseReference * le "@" permet de gerer le pb de l'heritage: on force la classe ProductCommand ici * @param string $Operator "Like" ou "Equal" ou "NotLike", ...: * cf ds SearchTools::getToSqlOperator() tous les operateurs disponibles * @param string $Value valeur du champs: facultatif * @param int $force (facultatif): si vaut 1 (0 par defaut), force la * construction du FilterComponent, * même si pas de var REQUEST ou SESSION reçue * @param string $startEntity nom de l'entité cherché dans le cas où il y a * une relation 1..* ou *..* * @static * @return object FilterComponent ou false */ static function newFilterComponent($Attribute, $DbTablesPath, $Operator = 'Equals', $Value = '', $force = 0, $startEntity = '') { $OperatorArray = array('Like', 'NotLike', 'Equals', 'NotEquals', 'GreaterThan', 'GreaterThanOrEquals', 'LowerThan', 'LowerThanOrEquals', 'In', 'NotIn', 'IsNull', 'IsNotNull'); if (!in_array($Operator, $OperatorArray)) { Template::errorDialog('Bad operator. Filter can\'t be builded.', $_SERVER['PHP_SELF']); exit; } unset($FilterComponent); $FilterComponent = new FilterComponent(); $FilterRuleOperator = SearchTools::getToSqlOperator($Operator); $pos = strpos($DbTablesPath, "()"); if ($pos === false) { // Pas de relation * ds la jointure eventuelle if (isset($_REQUEST[$Attribute]) && $_REQUEST[$Attribute] != '' && $_REQUEST[$Attribute] != '##' || isset($_SESSION[$Attribute]) && $_SESSION[$Attribute] != '' && $_SESSION[$Attribute] != '##' || $force == 1) { // || !(is_string($Value) && $Value == '') // selon si jointure ou pas $DbTablesPath = $DbTablesPath == "" ? $Attribute : $DbTablesPath; /* si c'est c'est un tab de valeurs, par exple un select multiple */ if (isset($_REQUEST[$Attribute]) && is_array($_REQUEST[$Attribute]) || isset($_SESSION[$Attribute]) && is_array($_SESSION[$Attribute]) || is_array($Value)) { if ($force == 1) { $Array = $Value; } else { $Array = isset($_REQUEST[$Attribute]) ? $_REQUEST[$Attribute] : $_SESSION[$Attribute]; } // pour une raison que j'ignore, $Array n'est dans certains // cas pas un array :/ on fait ce truc ici donc pour s'en // assurer: if (!is_array($Array)) { $Array = array($Array); } if (!in_array('##', $Array, true)) { // si la bonne selection foreach ($Array as $key => $val) { $Array[$key] = $Operator == 'Like' || $Operator == 'NotLike' ? str_replace('*', "%", $val) : $val; } if (in_array($Operator, array('Equals', 'In'))) { $ope = FilterRule::OPERATOR_IN; } elseif (in_array($Operator, array('NotEquals', 'NotIn'))) { $ope = FilterRule::OPERATOR_NOT_IN; } elseif (in_array($Operator, array('Like', 'NotLike'))) { foreach ($Array as $key => $val) { $FilterComponent->setItem(new FilterRule($DbTablesPath, $FilterRuleOperator, $Array[$key])); // c'est un OR ds ce cas!! $FilterComponent->operator = FilterComponent::OPERATOR_OR; } return $FilterComponent; } else { return null; } // Pas de sens dans ce cas (autres operateurs) $FilterComponent->setItem(new FilterRule($DbTablesPath, $ope, $Array)); } else { return false; } // si '##' selectionne } else { if (is_string($Value) && $Value == "") { // !empty($Value) if ($force == 0) { if (isset($_REQUEST[$Attribute]) && ($_REQUEST[$Attribute] == '' || $_REQUEST[$Attribute] == '##')) { return false; // pas de filtre dans ce cas } $Value = isset($_REQUEST[$Attribute]) ? strtoupper($_REQUEST[$Attribute]) : strtoupper($_SESSION[$Attribute]); } } else { $Value = is_string($Value) ? strtoupper($Value) : $Value; } $AttributeValue = $Operator == 'Like' || $Operator == 'NotLike' ? str_replace('*', "%", $Value) : $Value; $FilterComponent->setItem(new FilterRule($DbTablesPath, $FilterRuleOperator, $AttributeValue)); } return $FilterComponent; } } else { // Au moins une relation 1..* ou *..* ds la jointure eventuelle if (isset($_REQUEST[$Attribute]) && $_REQUEST[$Attribute] != '' && $_REQUEST[$Attribute] != '##' || isset($_SESSION[$Attribute]) && $_SESSION[$Attribute] != '' && $_SESSION[$Attribute] != '##' || $force == 1 || !empty($Value)) { // ne pas oublier de preciser ce parametre pour les relation 1..* if ($startEntity == '') { trigger_error('There is a 1..* relation in search criterions: you have to define EntitySearched in SearchForm constructor call !!', E_USER_ERROR); exit; } if ($Value == '' && SearchTools::requestOrSessionExist($Attribute) !== false) { $Value = SearchTools::requestOrSessionExist($Attribute); } if (is_string($Value)) { $Value = strtoupper($Value); } elseif (is_array($Value) && in_array('##', $Value)) { return false; } // Recuperation des Id qui conviennent $sql = SearchTools::getSQLfromMacro($startEntity, $DbTablesPath, $FilterRuleOperator, $Value); $result = Database::connection()->execute($sql); if (false === $result) { // si erreur sql if (DEV_VERSION) { echo $sql . '<br />'; } trigger_error(Database::connection()->ErrorMsg(), E_USER_ERROR); } if ($result->_numOfRows == 0) { // si 0 resultat $FilterComponent->setItem(new FilterRule('Id', FilterRule::OPERATOR_EQUALS, 0)); return $FilterComponent; } $valueArray = array(); while (!$result->EOF) { $valueArray[] = (int) $result->fields['_Id']; $result->moveNext(); } $FilterComponent->setItem(new FilterRule('Id', FilterRule::OPERATOR_IN, $valueArray)); return $FilterComponent; } } return false; }