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