Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.DocCollection.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Document searches classes
8  */
9 require_once "FDL/Class.Doc.php";
10 /**
11  * Document searches classes
12  *
13  * @brief class use to search documents
14  * @class DocCollection
15  * @package FDL
16  */
17 class DocCollection extends Doc
18 {
19  /**
20  * conditionnal operator compatibilities
21  *
22  * @var array
23  */
24  public $top = array(
25  "~*" => array(
26  "label" => "include",
27  "operand" => array(
28  "left",
29  "right"
30  ) ,
31  "dynlabel" => "{left} include {right}", # _("{left} include {right}")
32  "slabel" => array(
33  "file" => "filename or type include", #_("filename or type include")
34  "image" => "filename or type include",
35  "array" => "one value include", #_("one value include")
36 
37  ) ,
38  "sdynlabel" => array(
39  "file" => "{left} filename or type include {right}", #_("{left} filename or type include {right}")
40  "image" => "{left} filename or type include {right}",
41  "array" => "one value of {left} include {right}", #_("one value of {left} include {right}")
42 
43  ) ,
44  "type" => array(
45  "text",
46  "longtext",
47  "htmltext",
48  "ifile",
49  "array",
50  "file",
51  "image",
52  "fulltext"
53  )
54  ) ,
55  "=~*" => array(
56  "label" => "title include",
57  "operand" => array(
58  "left",
59  "right"
60  ) ,
61  "dynlabel" => "{left} title include {right}", # _("{left} last or first name include {right}")
62  "slabel" => array(
63  "uid" => "last or first name include",
64  "docidtitle[]" => "one of the titles include"
65  ) , #_("title include") _("last or first name include") _("one of the titles include")
66  "sdynlabel" => array(
67  "uid" => "{left} last or first name include {right}",
68  "docidtitle[]" => "one of the titles {left} include {right}"
69  ) , #_("{left} title include {right}") _("one of the titles {left} include {right}")
70  "type" => array(
71  "uid",
72  "docid",
73  "account",
74  "docidtitle[]"
75  )
76  ) ,
77  "@@" => array(
78  "label" => "content file word",
79  "operand" => array(
80  "left",
81  "right"
82  ) ,
83  "dynlabel" => "file {left} contain the word {right}", # _("file {left} contain the word {right}")
84  "type" => array(
85  "file"
86  )
87  ) ,
88  "~@" => array(
89  "label" => "content file expression",
90  "operand" => array(
91  "left",
92  "right"
93  ) ,
94  "dynlabel" => "file {left} contain the expression {right}", # _("file {left} contain the expression {right}")
95  "type" => array(
96  "file"
97  )
98  ) ,
99  "=" => array(
100  "label" => "equal",
101  "operand" => array(
102  "left",
103  "right"
104  ) ,
105  "dynlabel" => "{left} equal {right}", # _("{left} equal {right}")
106  "slabel" => array(
107  "docid" => "identificator equal",
108  "account" => "identificator equal",
109  "uid" => "system identifiant equal"
110  ) , #_("identificator equal") _("system identifiant equal")
111  "sdynlabel" => array(
112  "docid" => "{left} identifier equal {right}",
113  "account" => "{left} identifier equal {right}",
114  "uid" => "{left} system identifier equal {right}"
115  ) , #_("{left} identifier equal {right}") _("{left} system identifier equal {right}")
116  "type" => array(
117  "text",
118  "integer",
119  "int",
120  "double",
121  "enum",
122  "date",
123  "time",
124  "timestamp",
125  "money",
126  "color",
127  "docid",
128  "account",
129  "uid"
130  )
131  ) ,
132  "~^" => array(
133  "label" => "begin by",
134  "operand" => array(
135  "left",
136  "right"
137  ) ,
138  "dynlabel" => "{left} begin by {right}", # _("{left} begin by {right}")
139  "type" => array(
140  "text",
141  "longtext"
142  )
143  ) ,
144  "!=" => array(
145  "label" => "not equal",
146  "operand" => array(
147  "left",
148  "right"
149  ) ,
150  "dynlabel" => "{left} is not equal {right}", # _("{left} is not equal {right}")
151  "sdynlabel" => array(
152  "docid" => "{left} identifier not equal {right}",
153  "account" => "{left} identifier not equal {right}",
154  "uid" => "{left} system identifier not equal {right}"
155  ) , #_("{left} identifier not equal {right}") _("{left} system identifier not equal {right}")
156  "slabel" => array(
157  "docid" => "identificator not equal",
158  "account" => "identificator not equal",
159  "uid" => "system identifier not equal"
160  ) , #_("identificator not equal") _("system identifier not equal")
161  "type" => array(
162  "text",
163  "integer",
164  "int",
165  "double",
166  "enum",
167  "date",
168  "time",
169  "timestamp",
170  "money",
171  "color",
172  "docid",
173  "account",
174  "uid"
175  )
176  ) ,
177  "!~*" => array(
178  "label" => "not include",
179  "operand" => array(
180  "left",
181  "right"
182  ) ,
183  "dynlabel" => "{left} not include {right}", # _("{left} not include {right}")
184  "slabel" => array(
185  "file" => "filename or type not include",
186  "fulltext" => "any value include", #_("any value include")
187  "image" => "filename or type not include", #_("filename or type not include")
188  "array" => "no value include", #_("no value include")
189 
190  ) ,
191  "sdynlabel" => array(
192  "file" => "{left} filename or type not include {right}",
193  "fulltext" => "any values include {right}", #_("any values include {right}")
194  "image" => "{left} filename or type not include {right}", #_("{left} filename or type not include {right}")
195  "array" => "{left} include no value of {right}"
196  ) , #_("{left} include no value of {right}")
197  "type" => array(
198  "text",
199  "longtext",
200  "htmltext",
201  "ifile",
202  "array",
203  "file",
204  "image",
205  "fulltext"
206  )
207  ) ,
208  ">" => array(
209  "label" => "&gt;",
210  "operand" => array(
211  "left",
212  "right"
213  ) ,
214  "dynlabel" => "{left} greater than {right}", # _("{left} greater than {right}")
215  "type" => array(
216  "int",
217  "integer",
218  "double",
219  "date",
220  "time",
221  "timestamp",
222  "money"
223  )
224  ) ,
225  "<" => array(
226  "label" => "&lt;",
227  "operand" => array(
228  "left",
229  "right"
230  ) ,
231  "dynlabel" => "{left} lesser than {right}", # _("{left} lesser than {right}")
232  "type" => array(
233  "int",
234  "integer",
235  "double",
236  "date",
237  "time",
238  "timestamp",
239  "money"
240  )
241  ) ,
242  ">=" => array(
243  "label" => "&gt; or equal",
244  "operand" => array(
245  "left",
246  "right"
247  ) ,
248  "dynlabel" => "{left} greater or equal to {right}", # _("{left} greater or equal to {right}")
249  "type" => array(
250  "int",
251  "integer",
252  "double",
253  "date",
254  "time",
255  "timestamp",
256  "money"
257  )
258  ) ,
259  "<=" => array(
260  "label" => "&lt; or equal",
261  "operand" => array(
262  "left",
263  "right"
264  ) ,
265  "dynlabel" => "{left} lesser or equal to {right}", # _("{left} lesser or equal to {right}")
266  "type" => array(
267  "int",
268  "integer",
269  "double",
270  "date",
271  "time",
272  "timestamp",
273  "money"
274  )
275  ) ,
276  "is null" => array(
277  "label" => "is empty",
278  "operand" => array(
279  "left"
280  ) ,
281  "dynlabel" => "{left} is null"
282  ) , # _("{left} is null"),
283  "is not null" => array(
284  "label" => "is not empty",
285  "operand" => array(
286  "left"
287  ) ,
288  "dynlabel" => "{left} is not empty"
289  ) , # _("{left} is not empty"),
290  "><" => array(
291  "label" => "between", #_("between")
292  "operand" => array(
293  "left",
294  "min",
295  "max"
296  ) ,
297  "dynlabel" => "{left} is between {min} and {max}", # _("{left} is between {min} and {max}")
298  "type" => array(
299  "int",
300  "integer",
301  "double",
302  "date",
303  "time",
304  "timestamp",
305  "money"
306  )
307  ) ,
308  "~y" => array(
309  "label" => "one value equal", #("one value equal")
310  "operand" => array(
311  "left",
312  "right"
313  ) ,
314  "dynlabel" => "{left} one value equal {right}", # _("{left} one value equal {right}")
315  "slabel" => array(
316  "docid[]" => "one id equal", # _("one id equal")
317  "account[]" => "one id equal"
318  ) ,
319  "sdynlabel" => array(
320  "docid[]" => "{left} one id equal {right}", #_("{left} one id equal {right}")
321  "account[]" => "{left} one id equal {right}"
322  ) ,
323  "type" => array(
324  "array",
325  "docid[]",
326  "account[]"
327  )
328  )
329  );
330  /**
331  * get label forom operatore code
332  *
333  * @param string $operator operator code
334  * @param string $attributeType sttribute type
335  *
336  * @return string
337  */
338  public function getOperatorLabel($operator, $attributeType)
339  {
340  $op = $this->top[$operator];
341  if (!$op) return _("unknow operator") . " : $operator"; // TODO set exception
342  if ($attributeType) {
343  if (isset($op["slabel"]) && is_array($op["slabel"])) {
344  foreach ($op["slabel"] as $type => $label) {
345  if ($type == $attributeType) return _($label);
346  }
347  }
348  }
349  if ($op["label"]) return _($op["label"]);
350  return $operator; // no label found
351 
352  }
353  /**
354  * return document includes in search folder
355  *
356  * @param bool $controlview if false all document are returned else only visible for current user document are return
357  * @param array $filter to add list sql filter for selected document
358  * @param int|string $famid family identifier to restrict search
359  *
360  * @return array array of document array
361  */
362  public function getContent($controlview = true, array $filter = array() , $famid = "", $qtype = "TABLE", $trash = "")
363  {
364  return array();
365  }
366  /**
367  * return sql filter from object filter
368  *
369  * @param object $of the object filter
370  * @param integer &$famid return the family filter
371  * @param string &$fsql return the sql filter
372  *
373  * @return string error message if incorrect filter, empty if no errors
374  */
375  public function object2SqlFilter($of, &$famid, &$fsql)
376  {
377  if (!empty($of->family)) {
378  if (preg_match('/([\w:]*)\s?(strict)?/', trim($of->family) , $reg)) {
379  if (!is_numeric($reg[1])) $reg[1] = getFamIdFromName($this->dbaccess, $reg[1]);
380  if (isset($reg[2]) && ($reg[2] == "strict")) $famid = '-' . $reg[1];
381  else $famid = $reg[1];
382  }
383  }
384  if (!$famid) {
385  $famid = $this->getRawValue("se_famid");
386  if (!$famid) { // search in origin filter
387  $filter = $this->getMultipleRawValues("se_filter", '', 0);
388  if ($filter) {
389  $xfilter = simplexml_load_string($filter);
390  $famid = trim($xfilter->family);
391  }
392  }
393  }
394  $sql = array();
395  if (!empty($of->sql)) $of->sql = trim($of->sql);
396  if (!empty($of->sql)) {
397  if ((!strstr($of->sql, '--')) && (!strstr($of->sql, ';')) && (!stristr($of->sql, 'insert')) && (!stristr($of->sql, 'alter')) && (!stristr($of->sql, 'delete')) && (!stristr($of->sql, 'update'))) {
398  // try to prevent sql injection
399  $sql[] = $of->sql;
400  }
401  }
402  if ((!empty($of->criteria)) && (!is_array($of->criteria))) {
403  if ($of->criteria->operator) $of->criteria = array(
404  $of->criteria
405  );
406  if ($of->criteria->or) $of->criteria = array(
407  $of->criteria
408  );
409  if ($of->criteria->and) $of->criteria = array(
410  $of->criteria
411  );
412  }
413  $err = '';
414  if ((!empty($of->criteria)) && is_array($of->criteria)) {
415  foreach ($of->criteria as $c) {
416  $sqlone = '';
417  if (!empty($c->operator)) {
418  $err.= $this->_1object2SqlFilter($c, $sqlone, $famid);
419  if ($err == "") $sql[] = $sqlone;
420  } elseif ($c->or && is_array($c->or)) {
421  $sqlor = array();
422  foreach ($c->or as $cor) {
423  if ($cor->operator) $err.= $this->_1object2SqlFilter($cor, $sqlone, $famid);
424  else {
425  $oone = new stdClass();
426  $oone->criteria = $cor;
427  $sqlone = '';
428  $_f = '';
429  $this->object2SqlFilter($oone, $_f, $sqlone);
430  }
431  if ($err == "") $sqlor[] = $sqlone;
432  }
433  if (count($sqlor) > 0) {
434  $sql[] = '(' . implode(') or (', $sqlor) . ')';
435  }
436  } elseif ($c->and && is_array($c->and)) {
437  $sqlor = array();
438  foreach ($c->and as $cor) {
439  if ($cor->operator) $err.= $this->_1object2SqlFilter($cor, $sqlone, $famid);
440  else {
441  $oone = new stdClass();
442  $oone->criteria = $cor;
443  $_f = '';
444  $this->object2SqlFilter($oone, $_f, $sqlone);
445  }
446  if ($err == "") $sqlor[] = $sqlone;
447  }
448  if (count($sqlor) > 0) {
449  $sql[] = '(' . implode(') and (', $sqlor) . ')';
450  }
451  }
452  }
453  }
454  if (count($sql) > 0) {
455  $fsql = '(' . implode(') and (', $sql) . ')';
456  } else {
457  $fsql = "true";
458  }
459  return $err;
460  }
461  /**
462  * return sql from a single object filter
463  *
464  * @param stdClass $c the filter object
465  * @param string &$sql return the sql where clause
466  * @param string $famid family identifier
467  *
468  * @return string error message. Empty is no errors
469  */
470  private function _1object2SqlFilter($c, &$sql, $famid = "")
471  {
472  static $sw = false;
473  $err = '';
474  if ($c->operator) {
475  $top = $this->top[$c->operator];
476  if ($top) {
477  $toperand = $top["operand"];
478  $first = $toperand[0];
479  $second = isset($toperand[1]) ? $toperand[1] : null;
480  $third = isset($toperand[2]) ? $toperand[2] : null;
481  $col = $c->$first;
482  if ($second) {
483  $val1 = (isset($c->$second)) ? $c->$second : null;
484  if (!$val1) {
485  $sql = "true"; // incomplete request parameters
486  return "";
487  }
488  if (strtolower(substr($val1, 0, 5)) == "::get") { // only get method allowed
489  // it's method call
490  $val1 = $this->ApplyMethod($val1);
491  }
492  } else $val1 = "";
493  if ($third) {
494  $val2 = $c->$third;
495  if (!$val2) {
496  $sql = "true"; // incomplete request parameters
497  return "";
498  }
499  if (strtolower(substr($val2, 0, 5)) == "::get") { // only get method allowed
500  // it's method call
501  $val2 = $this->ApplyMethod($val2);
502  }
503  } else $val2 = "";
504  if (!$sw) {
505  $sw = createTmpDoc($this->dbaccess, "DSEARCH");
506  }
507  /**
508  * @var \Dcp\Family\DSEARCH $sw
509  */
510  $sw->setValue("se_famid", $famid);
511  $sql = $sw->getSqlCond($col, $c->operator, $val1, $val2, $err);
512  } else {
513  $err = sprintf(_("operator [%s] not allowed") , $c->operator);
514  }
515  } else {
516  $err = sprintf(_("no operator detected"));
517  }
518  return $err;
519  }
520  /**
521  * return specfic filters instead of normal content
522  *
523  * @return array of sql filters
524  */
525  public function getSpecificFilters()
526  {
527  return array();
528  }
529  /**
530  * test if document has specific filters
531  *
532  * @return boolean true if has filter
533  */
534  public function hasSpecificFilters()
535  {
536  return (count($this->getSpecificFilters()) > 0);
537  }
538  /**
539  * return content of collection
540  *
541  * @return DocumentList
542  */
543  public function getDocumentList()
544  {
545  $s = new SearchDoc($this->dbaccess);
546  $s->useCollection($this->initid);
547  $s->setObjectReturn();
548  $s->excludeConfidential();
549  $s->setOrder("fromid, title, id desc");
550  return $s->search()->getDocumentList();
551  }
552 }
553 
if($_POST["login"]=="")
Definition: chgpasswd.php:19
getContent($controlview=true, array $filter=array(), $famid="", $qtype="TABLE", $trash="")
if($famId) $s
getOperatorLabel($operator, $attributeType)
getMultipleRawValues($idAttr, $def="", $index=-1)
Definition: Class.Doc.php:3240
getFamIdFromName($dbaccess, $name)
object2SqlFilter($of, &$famid, &$fsql)
createTmpDoc($dbaccess, $fromid, $defaultvalue=true)
if($file) if($subject==""&&$file) if($subject=="") $err
getRawValue($idAttr, $def="")
Definition: Class.Doc.php:3117
class use to search documents
← centre documentaire © anakeen