2 /*******************************************************************************
3 * Copyright (C) 2007 Easter-eggs
4 * http://ldapsaisie.labs.libre-entreprise.org
6 * Author: See AUTHORS file in top-level directory.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 ******************************************************************************/
26 * @author Benjamin Renard <brenard@easter-eggs.com>
30 // The LdapObject type of search
31 private $LSobject=NULL;
33 // The configuration of search
36 // The context of search
39 // The parameters of the search
40 private $params=array (
44 'predefinedFilter' => false,
49 'attronly' => false, // If true, only attribute names are returned
52 'attributes' => array(),
54 'onlyAccessible' => NULL,
55 'sortDirection' => NULL,
58 'displaySubDn' => NULL,
59 'displayFormat' => NULL,
60 'nbObjectsByPage' => NB_LSOBJECT_LIST,
61 'nbPageLinkByPage' => 10,
62 'customInfos' => array(),
63 'withoutCache' => false,
64 'extraDisplayedColumns' => false,
67 // The cache of search parameters
68 private $_searchParams = NULL;
70 // The cache of the hash of the search parameters
71 private $_hash = NULL;
73 // The result of the search
77 private $_canCopy=NULL;
82 * @param[in] $LSobject string The LdapObject type of search
83 * @param[in] $context string Context of search (LSrelation / LSldapObject/ ...)
84 * @param[in] $params array Parameters of search
85 * @param[in] $purgeParams boolean If params in session have to be purged
88 function LSsearch($LSobject,$context,$params=null,$purgeParams=false) {
89 if (!LSsession :: loadLSobject($LSobject)) {
92 $this -> LSobject = $LSobject;
94 $this -> loadConfig();
96 if (isset($_REQUEST['LSsearchPurgeSession'])) {
97 $this -> purgeSession();
100 $this -> context = $context;
103 if (! $this -> loadParamsFromSession()) {
104 LSdebug('LSsearch : load default parameters');
105 $this -> loadDefaultParameters();
109 $this -> purgeParams();
110 $this -> loadDefaultParameters();
113 if (is_array($params)) {
114 $this -> setParams($params);
120 * Load configuration from LSconfig
124 private function loadConfig() {
125 $this -> config = LSconfig::get("LSobjects.".$this -> LSobject.".LSsearch");
126 if (is_array($this -> config['predefinedFilters'])) {
127 foreach($this -> config['predefinedFilters'] as $filter => $label) {
128 if(!LSldap::isValidFilter($filter)) {
129 LSerror::addErrorCode('LSsearch_15',array('label' => $label, 'filter' => $filter, 'type' => $this -> LSobject));
130 unset($this -> config['predefinedFilters'][$key]);
137 * Load default search parameters from configuration
139 * @retval boolean True on success or False
141 private function loadDefaultParameters() {
142 if (is_array($this -> config['params'])) {
143 return $this -> setParams($this -> config['params']);
149 * Load search parameters from session
151 * @retval boolean True if params has been loaded from session or False
153 private function loadParamsFromSession() {
154 LSdebug('LSsearch : load context params session '.$this -> context);
155 if (isset($_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'][$this -> context]) && is_array($_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'][$this -> context])) {
156 $params = $_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'][$this -> context];
158 if ($params['filter']) {
159 $params['filter'] = Net_LDAP2_Filter::parse($params['filter']);
162 $this -> params = $params;
169 * Save search parameters in session
173 private function saveParamsInSession() {
174 LSdebug('LSsearch : save context params session '.$this -> context);
175 $params = $this -> params;
176 if ($params['filter'] instanceof Net_LDAP2_Filter) {
177 $params['filter'] = $params['filter'] -> asString();
180 foreach ($params as $param => $value) {
181 if ( !isset($_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'][$this -> context][$param]) || $_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'][$this -> context][$param]!=$value) {
182 LSdebug("S: $param => $value");
183 $_SESSION['LSsession']['LSsearch'][$this -> LSobject]['params'][$this -> context][$param]=$value;
189 * Purge parameters in session
191 * @param[in] $LSobject string The LSobject type
195 public function purgeParams($LSobject=NULL) {
196 if (is_null($LSobject)) {
197 $LSobject = $this -> LSobject;
199 unset($_SESSION['LSsession']['LSsearch'][$LSobject]['params']);
207 public function purgeCache($LSobject=NULL) {
208 if (is_null($LSobject))
209 $LSobject = $this -> LSobject;
210 unset($_SESSION['LSsession']['LSsearch'][$LSobject]);
218 private function purgeSession() {
219 unset($_SESSION['LSsession']['LSsearch']);
223 * Define one search parameter
225 * @param[in] $param string The parameter name
226 * @param[in] $value mixed The parameter value
228 * @retval boolean True on success or False
230 public function setParam($param,$value) {
231 return $this -> setParams(array($param => $value));
235 * Define search parameters
237 * @param[in] $params array Parameters of search
239 * @retval boolean True on success or False
241 public function setParams($params) {
245 if (isset($params['filter'])) {
246 if (is_string($params['filter'])) {
247 $filter = Net_LDAP2_Filter::parse($params['filter']);
248 if (!LSerror::isLdapError($filter)) {
249 $this -> params['filter'] = $filter;
252 LSerror :: addErrorCode('LSsearch_01',$params['filter']);
256 elseif($params['filter'] instanceof Net_LDAP2_Filter) {
257 $this -> params['filter'] =& $params['filter'];
262 if (isset($params['approx'])) {
263 if (is_bool($params['approx']) || $params['approx']==0 || $params['approx']==1) {
264 $this -> params['approx'] = (bool)$params['approx'];
267 LSerror :: addErrorCode('LSsearch_05','approx');
273 if (isset($params['withoutCache'])) {
274 if (is_bool($params['withoutCache']) || $params['withoutCache']==0 || $params['withoutCache']==1) {
275 $this -> params['withoutCache'] = (bool)$params['withoutCache'];
278 LSerror :: addErrorCode('LSsearch_05','withoutCache');
284 if (isset($params['pattern'])) {
285 if ($params['pattern']=="") {
286 $this -> params['pattern'] = NULL;
287 $this -> params['filter'] = NULL;
289 elseif (self :: isValidPattern($params['pattern'])) {
290 $this -> params['pattern'] = $params['pattern'];
291 if (!is_string($params['filter'])) {
292 $this -> params['filter']=NULL;
299 if (isset($params['basedn']) && is_string($params['basedn'])) {
300 if (isCompatibleDNs(LSsession :: getRootDn(),$params['basedn'])) {
301 $this -> params['basedn'] = $params['basedn'];
304 LSerror :: addErrorCode('LSsearch_02',$params['basedn']);
310 if (isset($params['subDn']) && is_string($params['subDn'])) {
311 if (LSsession :: validSubDnLdapServer($params['subDn'])) {
312 $this -> params['subDn'] = $params['subDn'];
315 LSerror :: addErrorCode('LSsearch_03','subDn');
321 if (isset($params['scope']) && is_string($params['scope'])) {
322 if (in_array($params['scope'],array('sub','one','base'))) {
323 $this -> params['scope'] = $params['scope'];
326 LSerror :: addErrorCode('LSsearch_03','scope');
332 if (isset($params['nbObjectsByPage'])) {
333 if (((int)$params['nbObjectsByPage'])>1 ) {
334 $this -> params['nbObjectsByPage'] = (int)$params['nbObjectsByPage'];
337 LSerror :: addErrorCode('LSsearch_03','nbObjectsByPage');
343 if (isset($params['sortlimit'])) {
344 if (is_int($params['sortlimit']) && $params['sortlimit']>=0 ) {
345 $this -> params['sortlimit'] = $params['sortlimit'];
347 elseif ((int)$params['sortlimit'] > 0) {
348 $this -> params['sortlimit'] = (int)$params['sortlimit'];
351 LSerror :: addErrorCode('LSsearch_03','sortlimit');
357 if (isset($params['sortDirection']) && is_string($params['sortDirection'])) {
358 if (in_array($params['sortDirection'],array('ASC','DESC'))) {
359 $this -> params['sortDirection'] = $params['sortDirection'];
362 LSerror :: addErrorCode('LSsearch_03','sortDirection');
368 if (isset($params['sortBy']) && is_string($params['sortBy'])) {
369 if (in_array($params['sortBy'],array('displayName','subDn')) || ($this ->extraDisplayedColumns && isset($this ->extraDisplayedColumns[$params['sortBy']]))) {
370 if ($this -> params['sortBy'] == $params['sortBy']) {
371 $this -> toggleSortDirection();
374 $this -> params['sortBy'] = $params['sortBy'];
375 if (!is_string($params['sortDirection'])) {
376 $this -> params['sortDirection']='ASC';
381 LSerror :: addErrorCode('LSsearch_03','sortBy');
387 if (isset($params['sizelimit'])) {
388 if (((int)$params['sizelimit']) >= 0) {
389 $this -> params['sizelimit'] = $params['sizelimit'];
392 LSerror :: addErrorCode('LSsearch_04');
398 if (isset($params['attronly'])) {
399 if (is_bool($params['attronly']) || $params['attronly']==0 || $params['attronly']==1) {
400 $this -> params['attronly'] = (bool)$params['attronly'];
403 LSerror :: addErrorCode('LSsearch_05','attronly');
409 if (isset($params['recursive'])) {
410 if (is_bool($params['recursive']) || $params['recursive']==0 || $params['recursive']==1) {
411 $this -> params['recursive'] = (bool)$params['recursive'];
414 LSerror :: addErrorCode('LSsearch_05','recursive');
420 if (isset($params['displaySubDn'])) {
421 if (! LSsession :: isSubDnLSobject($this -> LSobject) ) {
422 if (is_bool($params['displaySubDn']) || $params['displaySubDn']==0 || $params['displaySubDn']==1) {
423 $this -> params['displaySubDn'] = (bool)$params['displaySubDn'];
426 LSerror :: addErrorCode('LSsearch_05','displaySubDn');
433 if (isset($params['attributes'])) {
434 if (is_string($params['attributes'])) {
435 $this -> params['attributes'] = array($params['attributes']);
437 elseif (is_array($params['attributes'])) {
438 $this -> params['attributes']=array();
439 foreach ($params['attributes'] as $attr) {
440 if (is_string($attr)) {
441 if (LSconfig::get("LSobjects.".$this -> LSobject.".attrs.$attr")) {;
442 $this -> params['attributes'][] = $attr;
445 LSerror :: addErrorCode('LSsearch_11',$attr);
451 LSerror :: addErrorCode('LSsearch_06');
457 if (isset($params['extraDisplayedColumns'])) {
458 $this -> params['extraDisplayedColumns']=(bool)$params['extraDisplayedColumns'];
462 if (isset($params['predefinedFilter'])) {
463 if (is_string($params['predefinedFilter'])) {
464 if (empty($params['predefinedFilter'])) {
465 $this->params['predefinedFilter']=false;
467 elseif(is_array($this -> config['predefinedFilters'])) {
468 if(isset($this->config['predefinedFilters'][$params['predefinedFilter']])) {
469 $this -> params['predefinedFilter'] = $params['predefinedFilter'];
472 LSerror :: addErrorCode('LSsearch_03','predefinedFilter');
478 LSerror :: addErrorCode('LSsearch_03','predefinedFilter');
484 if (isset($params['displayFormat']) && is_string($params['displayFormat'])) {
485 $this -> params['displayFormat'] = $params['displayFormat'];
489 if (isset($params['customInfos']) && is_array($params['customInfos'])) {
490 foreach($params['customInfos'] as $name => $data) {
491 if(is_array($data['function']) && is_string($data['function'][0])) {
492 LSsession::loadLSclass($data['function'][0]);
494 if (is_callable($data['function'])) {
495 $this -> params['customInfos'][$name] = array (
496 'function' => &$data['function'],
497 'args' => $data['args']
501 LSerror :: addErrorCode('LSsearch_14',$name);
506 // Only Accessible objects
507 if (isset($params['onlyAccessible'])) {
508 $this -> params['onlyAccessible'] = (bool)$params['onlyAccessible'];
511 $this -> saveParamsInSession();
516 * Return true only if the form is submited
518 * @retval boolean True only if the is submited
520 private function formIsSubmited() {
521 return isset($_REQUEST['LSsearch_submit']);
525 * Define search parameters by reading Post Data ($_REQUEST)
529 public function setParamsFormPostData() {
532 if (self::formIsSubmited()) {
534 if (is_null($data['recursive'])) {
535 $data['recursive']=false;
538 $data['recursive']=true;
542 if (is_null($data['approx'])) {
543 $data['approx']=false;
546 $data['approx']=true;
549 if (isset($data['ajax']) && !isset($data['pattern'])) {
554 $this -> setParams($data);
558 * Toggle the sort direction
562 private function toggleSortDirection() {
563 if ($this -> params['sortDirection']=="ASC") {
564 $this -> params['sortDirection'] = "DESC";
567 $this -> params['sortDirection'] = "ASC";
572 * Make a filter object with a pattern of search
574 * @param[in] $pattern The pattern of search. If is null, the pattern in params will be used.
576 * @retval mixed Net_LDAP2_Filter on success or False
578 function getFilterFromPattern($pattern=NULL) {
579 if ($pattern==NULL) {
580 $pattern=$this -> params['pattern'];
582 if (self :: isValidPattern($pattern)) {
583 $attrsConfig=LSconfig::get("LSobjects.".$this -> LSobject.".LSsearch.attrs");
585 if (!is_array($attrsConfig)) {
586 foreach(LSconfig::get("LSobjects.".$this -> LSobject.".attrs") as $attr => $config) {
587 $attrsList[$attr]=array();
591 foreach($attrsConfig as $key => $val) {
593 $attrsList[$val]=array();
596 $attrsList[$key]=$val;
601 if (empty($attrsList)) {
602 LSerror :: addErrorCode('LSsearch_07');
607 foreach ($attrsList as $attr => $opts) {
608 if ($params['approx']) {
609 if (isset($opts['approxLSformat'])) {
610 $filter=Net_LDAP2_Filter::parse(getFData($opts['approxLSformat'],array('name'=>$attr,'pattern'=>$pattern)));
613 $filter=Net_LDAP2_Filter::create($attr,'approx',$pattern);
617 if (isset($opts['searchLSformat'])) {
618 $filter=Net_LDAP2_Filter::parse(getFData($opts['searchLSformat'],array('name'=>$attr,'pattern'=>$pattern)));
621 $filter=Net_LDAP2_Filter::create($attr,'contains',$pattern);
625 if (!Net_LDAP2::isError($filter)) {
629 LSerror :: addErrorCode('LSsearch_08',array('attr' => $attr,'pattern' => $pattern));
633 if(!empty($filters)) {
634 $filter=LSldap::combineFilters('or',$filters);
639 LSerror :: addErrorCode('LSsearch_09');
644 LSerror :: addErrorCode('LSsearch_10');
650 * Check if search pattern is valid
652 * @param[in] $pattern string The pattern
654 * @retval boolean True if pattern is valid or False
656 static function isValidPattern($pattern) {
657 return (is_string($pattern) && $pattern!= "" && $pattern!="*");
661 * Check if cache is enabled
663 * @retval boolean True if cache is enabled or False
665 public function cacheIsEnabled() {
666 if (isset($this -> config['cache'])) {
667 $conf=$this -> config['cache'];
668 if (is_bool($conf) || $conf==0 || $conf==1) {
672 LSerror :: addErrorCode('LSsearch_03','cache');
675 return LSsession :: cacheSearch();
679 * Methode for parameters value access
681 * @param[in] $key string The parameter name
683 * @retval mixed The parameter value or NULL
685 public function getParam($key) {
686 if(in_array($key,array_keys($this -> params))) {
687 return $this -> params[$key];
693 * Return hidden fileds to add in search form
695 * @retval array The hield fields whith their values
697 public function getHiddenFieldForm() {
699 'LSobject' => $this -> LSobject
704 * Generate an array with search parameters, only parameters whitch have to be
705 * passed to Net_LDAP2 for the LDAP search. This array will be store in
706 * $this -> _searchParams private variable.
710 private function generateSearchParams() {
711 // Purge the cache of the hash
712 $this -> _hash = NULL;
716 'filter' => $this -> params['filter'],
717 'basedn' => $this -> params['basedn'],
718 'scope' => $this -> params['scope'],
719 'sizelimit' => $this -> params['sizelimit'],
720 'attronly' => $this -> params['attronly'],
721 'attributes' => $this -> params['attributes']
725 if (!is_null($this -> params['pattern'])) {
726 $filter=$this ->getFilterFromPattern();
727 if (is_null($retval['filter'])) {
728 $retval['filter']=$filter;
731 $retval['filter']=LSldap::combineFilters('and',array($retval['filter'],$filter));
736 if (is_string($this -> params['predefinedFilter'])) {
737 if (!is_null($retval['filter'])) {
738 $filter=LSldap::combineFilters('and',array($this -> params['predefinedFilter'],$retval['filter']));
740 $retval['filter']=$filter;
744 $retval['filter']=$this -> params['predefinedFilter'];
749 $objFilter=LSldapObject::getObjectFilter($this -> LSobject);
751 if (!is_null($retval['filter'])) {
752 $filter=LSldap::combineFilters('and',array($objFilter,$retval['filter']));
754 $retval['filter']=$filter;
758 $retval['filter']=$objFilter;
763 if (is_null($retval['basedn'])) {
764 if (!is_null($this -> params['subDn'])) {
765 if ($this -> params['recursive']) {
766 $retval['basedn'] = $this -> params['subDn'];
769 $retval['basedn'] = LSconfig::get("LSobjects.".$this -> LSobject.".container_dn").','.$this -> params['subDn'];
773 if ($this -> params['recursive']) {
774 $retval['basedn'] = LSsession :: getTopDn();
777 $retval['basedn'] = LSconfig::get("LSobjects.".$this -> LSobject.".container_dn").','.LSsession :: getTopDn();
781 if ($this -> params['recursive'] || !isset($retval['scope'])) {
782 $retval['scope'] = 'sub';
785 if (is_null($this -> params['displayFormat'])) {
786 $this -> params['displayFormat']=LSconfig::get("LSobjects.".$this -> LSobject.".display_name_format");
790 $attrs=getFieldInFormat($this -> params['displayFormat']);
791 if(is_array($retval['attributes'])) {
792 $retval['attributes']=array_merge($attrs,$retval['attributes']);
795 $retval['attributes']=$attrs;
799 if ($this -> params['extraDisplayedColumns'] && is_array($this -> config['extraDisplayedColumns'])) {
800 foreach ($this -> config['extraDisplayedColumns'] as $id => $conf) {
801 $attrs=getFieldInFormat($conf['LSformat']);
802 if(is_array($conf['alternativeLSformats'])) {
803 foreach ($conf['alternativeLSformats'] as $format) {
804 $attrs=array_merge($attrs,getFieldInFormat($format));
808 $attrs=array_merge($attrs,getFieldInFormat($conf['alternativeLSformats']));
810 if(is_array($retval['attributes'])) {
811 $retval['attributes']=array_merge($attrs,$retval['attributes']);
814 $retval['attributes']=$attrs;
819 if (is_array($retval['attributes'])) {
820 $retval['attributes']=array_unique($retval['attributes']);
823 $this -> _searchParams = $retval;
829 * @param[in] $cache boolean Define if the cache can be used
831 * @retval boolean True on success or False
833 public function run($cache=true) {
834 $this -> generateSearchParams();
835 if ($this -> _searchParams['filter'] instanceof Net_LDAP2_Filter) {
836 LSdebug('LSsearch : filter : '.$this -> _searchParams['filter']->asString());
838 LSdebug('LSsearch : basedn : '.$this -> _searchParams['basedn'].' - scope : '.$this -> _searchParams['scope']);
840 if( $cache && (!isset($_REQUEST['refresh'])) && (!$this -> params['withoutCache']) ) {
841 LSdebug('LSsearch : with the cache');
842 $this -> result = $this -> getResultFromCache();
845 LSdebug('LSsearch : without the cache');
846 $this -> setParam('withoutCache',false);
849 if (!$this -> result) {
850 LSdebug('LSsearch : Not in cache');
851 $this -> result=array(
853 'sortDirection' => NULL
857 $list = LSldap :: search(
858 $this -> _searchParams['filter'],
859 $this -> _searchParams['basedn'],
860 $this -> _searchParams
864 if ($list === false) {
865 LSerror :: addErrorCode('LSsearch_12');
869 if ($this -> getParam('onlyAccessible') && LSsession :: getLSuserObjectDn()) {
870 $this -> result['list']=array();
872 // Check user rights on objets
873 foreach($list as $id => $obj) {
874 if (LSsession :: canAccess($this -> LSobject,$obj['dn'])) {
875 $this -> result['list'][]=$obj;
880 $this -> result['list']=$list;
883 $this -> addResultToCache();
892 * Return an hash corresponding to the parameters of the search
894 * @param[in] $searchParams array An optional search params array
896 * @retval string The hash of the parameters of the search
898 public function getHash($searchParams=null) {
899 if(is_null($searchParams)) {
900 $searchParams=$this -> _searchParams;
901 if ($this -> _hash) {
902 return $this -> _hash;
905 if ($searchParams['filter'] instanceof Net_LDAP_Filter) {
906 $searchParams['filter']=$searchParams['filter']->asString();
908 return hash('md5',print_r($searchParams,true));
912 * Add the result of the search to cache of the session
916 public function addResultToCache() {
917 if ($this -> cacheIsEnabled()) {
918 LSdebug('LSsearch : Save result in cache.');
919 $hash=$this->getHash();
920 $_SESSION['LSsession']['LSsearch'][$this -> LSobject][$hash]=$this->result;
925 * Get the result of the search from cache of the session
927 * @retval array | False The array of the result of the search or False
929 private function getResultFromCache() {
930 if ($this -> cacheIsEnabled()) {
931 $hash=$this->getHash();
932 if (isset($_SESSION['LSsession']['LSsearch'][$this -> LSobject][$hash])) {
933 LSdebug('LSsearch : Load result from cache.');
934 return $_SESSION['LSsession']['LSsearch'][$this -> LSobject][$hash];
941 * Get page informations to display
943 * @param[in] $page integer The number of the page
945 * @retval array The information of the page
947 public function getPage($page=0) {
948 if (!LSsession::loadLSclass('LSsearchEntry')) {
949 LSerror::addErrorCode('LSsession_05',$this -> LSobject);
958 'total' => $this -> total
961 if ($retval['total']>0) {
962 LSdebug('Total : '.$retval['total']);
964 if (!$this->params['nbObjectsByPage']) {
965 $this->params['nbObjectsByPage']=NB_LSOBJECT_LIST;
967 $retval['nbPages']=ceil($retval['total']/$this->params['nbObjectsByPage']);
969 $sortTable=$this -> getSortTable();
973 ($page * $this->params['nbObjectsByPage']),
974 $this->params['nbObjectsByPage']
977 foreach ($list as $key => $id) {
978 $retval['list'][]=new LSsearchEntry($this,$this -> LSobject,$this -> params,$this -> _hash,$this -> result['list'],$id);
987 * @retval array The entries
989 public function getSearchEntries() {
990 if (!LSsession::loadLSclass('LSsearchEntry')) {
991 LSerror::addErrorCode('LSsession_05',$this -> LSobject);
995 if ($this -> total>0) {
996 $sortTable=$this -> getSortTable();
998 foreach ($sortTable as $key => $id) {
999 $retval[]=new LSsearchEntry($this,$this -> LSobject,$this -> params,$this -> _hash,$this -> result['list'],$id);
1006 * Access to information of this object
1008 * @param[in] $key string The key of the info
1010 * @retval mixed The info
1012 public function __get($key) {
1017 if ($key=='LSobject') {
1018 return $this -> LSobject;
1020 elseif (in_array($key,$params)) {
1021 return $this -> params[$key];
1023 elseif ($key=='label_objectName') {
1024 return LSldapObject::getLabel($this -> LSobject);
1026 elseif ($key=='label_level') {
1027 return LSsession :: getSubDnLabel();
1029 elseif ($key=='label_actions') {
1030 return _('Actions');
1032 elseif ($key=='label_no_result') {
1033 return _("This search didn't get any result.");
1035 elseif ($key=='sort') {
1036 if (isset($this -> params['sortlimit']) && ($this -> params['sortlimit']>0)) {
1037 return ($this -> total < $this -> params['sortlimit']);
1041 elseif ($key=='sortlimit') {
1042 return $this -> params['sortlimit'];
1044 elseif ($key=='total') {
1045 return count($this -> result['list']);
1047 elseif ($key=='label_total') {
1048 return $this -> total." ".$this -> label_objectName;
1050 elseif ($key=='displaySubDn') {
1051 if (LSsession :: subDnIsEnabled()) {
1052 if (!is_null($this -> params[$key])) {
1053 return $this -> params[$key];
1056 return (! LSsession :: isSubDnLSobject($this -> LSobject) );
1061 elseif ($key=='canCopy') {
1062 if (!is_null($this -> _canCopy))
1063 return $this -> _canCopy;
1064 $this -> _canCopy = LSsession :: canCreate($this -> LSobject);
1065 return $this -> _canCopy;
1067 elseif ($key=='predefinedFilters') {
1069 if (is_array($this -> config['predefinedFilters'])) {
1070 foreach($this -> config['predefinedFilters'] as $filter => $label) {
1071 $retval[$filter]=__($label);
1076 elseif ($key=='extraDisplayedColumns') {
1077 if ($this->params['extraDisplayedColumns'] && is_array($this -> config['extraDisplayedColumns'])) {
1078 return $this -> config['extraDisplayedColumns'];
1085 throw new Exception('Incorrect property !');
1090 * Function use with uasort to sort two entry
1092 * @param[in] $a array One line of result
1093 * @param[in] $b array One line of result
1095 * @retval int Value for uasort
1097 private function _sortTwoEntry(&$a,&$b) {
1098 $sortBy = $this -> params['sortBy'];
1099 $sortDirection = $this -> params['sortDirection'];
1100 if ($sortDirection=='ASC') {
1106 $oa = new LSsearchEntry($this,$this -> LSobject,$this -> params,$this -> _hash,$this -> result['list'],$a);
1108 $ob = new LSsearchEntry($this,$this -> LSobject,$this -> params,$this -> _hash,$this -> result['list'],$b);
1111 if ($va == $vb) return 0;
1113 $val = array($va,$vb);
1123 * Function to run after using the result. It's update the cache
1125 * IT'S FUNCTION IS VERY IMPORTANT !!!
1129 function afterUsingResult() {
1130 $this -> addResultToCache();
1134 * Redirect user to object view if the search have only one result
1136 * @retval boolean True only if user have been redirected
1138 function redirectWhenOnlyOneResult() {
1139 if ($this -> total == 1 && $this -> result && self::formIsSubmited()) {
1140 LSsession :: redirect('view.php?LSobject='.$this -> LSobject.'&dn='.urlencode($this -> result['list'][0]['dn']));
1146 * Run the sort if it's enabled and if the result is not in the cache
1148 * @retval boolean True on success or false
1151 if (!$this -> sort) {
1152 LSdebug('doSort : sort is disabled');
1155 if (is_null($this -> params['sortBy'])) {
1158 if (is_null($this -> params['sortDirection'])) {
1159 $this -> params['sortDirection']='ASC';
1162 if ($this->total==0) {
1166 if (isset($this -> result['sort'][$this -> params['sortBy']][$this -> params['sortDirection']])) {
1167 LSdebug('doSort : from cache');
1171 LSdebug('doSort : '.$this -> params['sortBy'].' - '.$this -> params['sortDirection']);
1173 $this -> result['sort'][$this -> params['sortBy']][$this -> params['sortDirection']]=range(0,($this -> total-1));
1175 if (!LSsession :: loadLSClass('LSsearchEntry')) {
1176 LSerror::addErrorCode('LSsession_05','LSsearchEntry');
1181 $this -> result['sort'][$this -> params['sortBy']][$this -> params['sortDirection']],
1182 array($this,'_sortTwoEntry')
1184 LSerror :: addErrorCode('LSsearch_13');
1192 * Returns the id of table rows in the result sorted according to criteria
1193 * defined in the parameters
1195 * @retval array The Table of id lines of results sorted
1197 function getSortTable() {
1198 if (isset($this -> result['sort'][$this -> params['sortBy']][$this -> params['sortDirection']])) {
1199 return $this -> result['sort'][$this -> params['sortBy']][$this -> params['sortDirection']];
1201 return range(0,($this -> total-1));
1207 * @retval Array DN associate with name
1209 public function listObjectsName() {
1210 if (!LSsession::loadLSclass('LSsearchEntry')) {
1211 LSerror::addErrorCode('LSsession_05',$this -> LSobject);
1217 if ($this -> total>0) {
1218 $sortTable=$this -> getSortTable();
1220 foreach ($sortTable as $key => $id) {
1221 $entry=new LSsearchEntry($this,$this -> LSobject,$this -> params,$this -> _hash,$this -> result['list'],$id);
1222 $retval[$entry->dn]=$entry->displayName;
1230 * List LSldapObjects
1232 * @retval Array of LSldapObjects
1234 public function listObjects() {
1237 if ($this -> total>0) {
1238 $sortTable=$this -> getSortTable();
1241 foreach ($sortTable as $key => $id) {
1242 $retval[$c]=new $this -> LSobject();
1243 $retval[$c] -> loadData($this -> result['list'][$id]['dn']);
1254 * @retval Array of DN
1256 public function listObjectsDn() {
1259 if ($this -> total>0) {
1260 $sortTable=$this -> getSortTable();
1263 foreach ($sortTable as $key => $id) {
1264 $retval[$c] = $this -> result['list'][$id]['dn'];
1277 LSerror :: defineError('LSsearch_01',
1278 _("LSsearch : Invalid filter : %{filter}.")
1280 LSerror :: defineError('LSsearch_02',
1281 _("LSsearch : Invalid basedn : %{basedn}.")
1283 LSerror :: defineError('LSsearch_03',
1284 _("LSsearch : Invalid value for %{param} parameter.")
1286 LSerror :: defineError('LSsearch_04',
1287 _("LSsearch : Invalid size limit. Must be an integer greater or equal to 0.")
1289 LSerror :: defineError('LSsearch_05',
1290 _("LSsearch : Invalid parameter %{attr}. Must be an boolean.")
1292 LSerror :: defineError('LSsearch_06',
1293 _("LSsearch : Invalid parameter attributes. Must be an string or an array of strings.")
1295 LSerror :: defineError('LSsearch_07',
1296 _("LSsearch : Can't build attributes list for make filter.")
1298 LSerror :: defineError('LSsearch_08',
1299 _("LSsearch : Error building filter with attribute '%{attr}' and pattern '%{pattern}'")
1301 LSerror :: defineError('LSsearch_09',
1302 _("LSsearch : Error combining filters.")
1304 LSerror :: defineError('LSsearch_10',
1305 _("LSsearch : Invalid pattern.")
1307 LSerror :: defineError('LSsearch_11',
1308 _("LSsearch : Invalid attribute %{attr} in parameters.")
1310 LSerror :: defineError('LSsearch_12',
1311 _("LSsearch : Error during the search.")
1313 LSerror :: defineError('LSsearch_13',
1314 _("LSsearch : Error sorting the search.")
1316 LSerror :: defineError('LSsearch_14',
1317 _("LSsearch : The function of the custum information %{name} is not callable.")
1319 LSerror :: defineError('LSsearch_15',
1320 _("LSsearch : Invalid predefinedFilter for LSobject type %{type} : %{label} (filter : %{filter}).")