Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Method.DetailSearch.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  * Detailled search
9  *
10  * @author Anakeen 2000
11  * @version $Id: Method.DetailSearch.php,v 1.73 2009/01/08 17:52:54 eric Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  * @subpackage GED
15  */
16 /**
17  */
18 /**
19  * @begin-method-ignore
20  * this part will be deleted when construct document class until end-method-ignore
21  */
22 class _DSEARCH extends DocSearch
23 {
24  /*
25  * @end-method-ignore
26  */
27  var $defaultedit = "FREEDOM:EDITDSEARCH"; #N_("include") N_("equal") N_("equal") _("not equal") N_("is empty") N_("is not empty") N_("one value equal")
28  var $defaultview = "FREEDOM:VIEWDSEARCH"; #N_("not include") N_("begin by") N_("not equal") N_("&gt; or equal") N_("&lt; or equal") N_("one word equal") N_("content file word") N_("content file expression")
29 
30  /**
31  * return sql query to search wanted document
32  */
33  function ComputeQuery($keyword = "", $famid = - 1, $latest = "yes", $sensitive = false, $dirid = - 1, $subfolder = true)
34  {
35 
36  if ($dirid > 0) {
37 
38  if ($subfolder) $cdirid = getRChildDirId($this->dbaccess, $dirid);
39  else $cdirid = $dirid;
40  } else $cdirid = 0;;
41 
42  $filters = $this->getSqlGeneralFilters($keyword, $latest, $sensitive);
43 
44  $cond = $this->getSqlDetailFilter();
45 
46  if ($cond === false) return array(
47  false
48  );
49  $distinct = false;
50  if ($latest == "lastfixed") $distinct = true;
51  if ($cond != "") $filters[] = $cond;
52  if ($this->getValue("se_famonly") == "yes") {
53  if (!is_numeric($famid)) $famid = getFamIdFromName($this->dbaccess, $famid);
54  $famid = - abs($famid);
55  }
56  $query = getSqlSearchDoc($this->dbaccess, $cdirid, $famid, $filters, $distinct, $latest == "yes", $this->getValue("se_trash") , false);
57 
58  return $query;
59  }
60  /**
61  * Change queries when use filters objects instead of declarative criteria
62  * @see DocSearch#getQuery()
63  */
64  function getQuery()
65  {
66  $filtersType = $this->getTValue("se_typefilter");
67  if ((count($this->getTvalue("se_filter")) > 0) && ($filtersType[0] != "generated")) {
68  $queries = array();
69  $filters = $this->getTValue("se_filter");
70  foreach ($filters as $filter) {
71  $q = $this->getSqlXmlFilter($filter);
72  if ($q) $queries[] = $q;
73  }
74  return $queries;
75  } else {
76  return parent::getQuery();
77  }
78  }
79 
80  function postModify()
81  {
82  $err = parent::postModify();
83  $err.= $this->updateFromXmlFilter();
84  $err.= $this->updateXmlFilter();
85  if ((!$err) && ($this->isChanged())) $err = $this->modify();
86  return $err;
87  }
88  /**
89  * update somes attributes from Xml filter
90  * @return string error message
91  */
92  public function updateFromXmlFilter()
93  {
94  // update only if one filter
95  $err = '';
96  if (count($this->getTvalue("se_filter")) == 1) {
97  // try to update se_famid
98  $filters = $this->getTValue("se_filter");
99  $filtersType = $this->getTValue("se_typefilter");
100  $filter = $filters[0];
101  $filterType = $filtersType[0];
102  if ($filterType != "generated") {
103  $root = simplexml_load_string($filter);
104  $std = $this->simpleXml2StdClass($root);
105  if ($std->family) {
106  if (!is_numeric($std->family)) {
107  if (preg_match("/([\w:]*)\s?(strict)?/", trim($std->family) , $reg)) {
108  if (!is_numeric($reg[1])) $reg[1] = getFamIdFromName($this->dbaccess, $reg[1]);
109  if ($reg[2] == "strict") $famid = '-' . $reg[1];
110  else $famid = $reg[1];
111  }
112  } else {
113  $famid = ($std->family);
114  }
115  if ($famid) {
116  $err = $this->setValue("se_famid", abs($famid));
117  $err.= $this->setValue("se_famonly", ($famid > 0) ? "no" : "yes");
118  }
119  }
120  }
121  }
122  return $err;
123  }
124  /**
125  * update somes attributes from Xml filter
126  * @return string error message
127  */
128  public function updateXmlFilter()
129  {
130  // update only if one filter
131  $err = '';
132  if (count($this->getTvalue("se_filter")) < 2) {
133  // try to update se_famid
134  $filters = $this->getTValue("se_filter");
135  $typeFilters = $this->getTValue("se_typefilter");
136  if (count($this->getTvalue("se_filter")) == 1) {
137  if ($typeFilters[0] != "generated") return ''; // don't update specified filter created by data API
138 
139  }
140  if ($this->getValue("se_famid")) {
141  $filterXml = sprintf("<filter><family>%s%s</family>", $this->getValue("se_famid") , ($this->getValue("se_famonly") == "yes" ? " strict" : ""));
142  // <criteria><lp></lp><rp></rp><ol></ol><left>an_espece_title</left><operator>~*</operator><right>baleine</right></criteria>
143  $details = $this->getAValues("se_t_detail");
144  foreach ($details as $k => $v) {
145  // need linearize Filter
146  /*$filterXml.=sprintf("<criteria><lp></lp><rp></rp><ol></ol><left>an_espece_title</left><operator>~*</operator><right>baleine</right></criteria>",
147  $v["se_leftp"]);
148  */
149  }
150 
151  $filterXml.= "</filter>";
152  $this->setValue("se_typefilter", "generated"); // only one
153  $this->setValue("se_filter", $filterXml);
154  }
155  }
156  return $err;
157  }
158  /**
159  * return a query from on filter object
160  * @param string $xml xml filter object
161  * @return string the query
162  */
163  function getSqlXmlFilter($xml)
164  {
165  $root = simplexml_load_string($xml);
166  // trasnform XmlObject to StdClass object
167  $std = $this->simpleXml2StdClass($root);
168  $this->object2SqlFilter($std, $famid, $sql);
169 
170  $filters[] = $sql;
171  $cdirid = 0;
172  $q = getSqlSearchDoc($this->dbaccess, $cdirid, $famid, $filters);
173  if (count($q) == 1) {
174  $q0 = $q[0]; // need a tempo variable : don't know why
175  return ($q0);
176  }
177 
178  return false;
179  }
180  /**
181  * cast SimpleXMLElment to stdClass
182  * @param SimpleXMLElement $xml
183  * @return stdClass return object or value if it is a leaf
184  */
185  public function simpleXml2StdClass(SimpleXMLElement $xml)
186  {
187  $std = null;
188  if ($xml->count() == 0) {
189  return current($xml);
190  } else {
191  foreach ($xml as $k => $se) {
192  if (isset($std->$k)) {
193  if (!is_array($std->$k)) $std->$k = array(
194  $std->$k
195  );
196  array_push($std->$k, $this->simpleXml2StdClass($se));
197  } else {
198  $std->$k = $this->simpleXml2StdClass($se);
199  }
200  }
201  }
202  return $std;
203  }
204 
205  function preConsultation()
206  {
207  if (count($this->getTvalue("se_filter")) > 0) {
208  if ($this->defaultview == "FREEDOM:VIEWDSEARCH") {
209  $type = $this->getTvalue("se_typefilter");
210  if ($type[0] != "generated") {
211  $this->defaultview = "FDL:VIEWBODYCARD";
212  }
213  }
214  }
215  }
216 
217  function preEdition()
218  {
219  if (count($this->getTvalue("se_filter")) > 0) {
220  $type = $this->getTvalue("se_typefilter");
221  if ($type[0] != "generated") {
222  $this->defaultedit = "FDL:EDITBODYCARD";
223  $this->getAttribute('se_t_detail', $oa);
224  $oa->setVisibility('R');
225  $this->getAttribute('se_t_filters', $oa);
226  $oa->setVisibility('W');
227 
228  $this->getAttribute('se_filter', $oa);
229  $oa->setVisibility('W');
230  }
231  }
232  }
233  /**
234  * return error if query filters are not compatibles
235  * verify parenthesis
236  * @return string error message , empty if no errors
237  */
238  function getSqlParseError()
239  {
240  $err = "";
241  $tlp = $this->getTValue("SE_LEFTP");
242  $tlr = $this->getTValue("SE_RIGHTP");
243  $clp = 0;
244  $clr = 0;
245  //if (count($tlp) > count($tlr)) $err=sprintf(_("left parenthesis is not closed"));
246  if ($err == "") {
247  foreach ($tlp as $lp) if ($lp == "yes") $clp++;
248  foreach ($tlr as $lr) if ($lr == "yes") $clr++;
249  if ($clp != $clr) $err = sprintf(_("parenthesis number mismatch : %d left, %d right") , $clp, $clr);
250  }
251  return $err;
252  }
253  /**
254  * return sql part from operator
255  * @param string $col a column : property or attribute name
256  * @param string $op one of this ::top keys : =, !=, >, ....
257  * @param string $val value use for test
258  * @param string $val2 second value use for test with >< operator
259  * @return string the sql query part
260  */
261  function getSqlCond($col, $op, $val = "", $val2 = "", &$err = "")
262  {
263 
264  if ((!$this->searchfam) || ($this->searchfam->id != $this->getValue("se_famid"))) {
265  $this->searchfam = new_doc($this->dbaccess, $this->getValue("se_famid"));
266  }
267  $col = trim(strtok($col, ' ')); // a col is one word only (prevent injection)
268  // because for historic reason revdate is not a date type
269  if (($col == "revdate") && ($val != '') && (!is_numeric($val))) {
270  $val = stringdatetounixts($val);
271  }
272  $atype = '';
273  /*
274  * @var NormalAttribute $oa
275  */
276  $oa = $this->searchfam->getAttribute($col);
277 
278  if ($oa) $atype = $oa->type;
279  else if ($this->infofields[$col]) $atype = $this->infofields[$col]["type"];
280  if (($atype == "date" || $atype == "timestamp")) {
281  if ($col == 'revdate') {
282  if ($op == "=") {
283  $val2 = $val + 85399; // tonight
284  $op = "><";
285  }
286  } else {
287  if (($atype == "timestamp")) {
288  $pos = strpos($val, ' ');
289  $hms = '';
290  if ($pos != false) {
291  $hms = substr($val, $pos + 1);
292  }
293  }
294 
295  $cfgdate = getLocaleConfig();
296  if ($val) $val = stringDateToIso($val, $cfgdate['dateFormat']);
297  if ($val2) $val2 = stringDateToIso($val2, $cfgdate['dateFormat']);
298 
299  if ($oa->isMultiple() && getLcdate() != 'iso') {
300  $val = stringDateToLocaleDate($val, $cfgdate['dateFormat']);
301  $val2 = stringDateToLocaleDate($val2, $cfgdate['dateFormat']);
302  }
303 
304  if (($atype == "timestamp") && ($op == "=")) {
305 
306  $val = trim($val);
307  if (strlen($val) == 10) {
308  if ($hms == '') {
309  $val2 = $val . " 23:59:59";
310  $val.= " 00:00:00";
311  $op = "><";
312  } elseif (strlen($hms) == 2) {
313  $val2 = $val . ' ' . $hms . ":59:59";
314  $val.= ' ' . $hms . ":00:00";
315  $op = "><";
316  } elseif (strlen($hms) == 5) {
317  $val2 = $val . ' ' . $hms . ":59";
318  $val.= ' ' . $hms . ":00";
319  $op = "><";
320  } else {
321  $val.= ' ' . $hms;
322  }
323  }
324  }
325  }
326  }
327  switch ($op) {
328  case "is null":
329 
330  switch ($atype) {
331  case "int":
332  case "uid":
333  case "double":
334  case "money":
335  $cond = sprintf(" (%s is null or %s = 0) ", $col, $col);
336  break;
337 
338  case "date":
339  case "time":
340  $cond = sprintf(" (%s is null) ", $col);
341  break;
342 
343  default:
344  $cond = sprintf(" (%s is null or %s = '') ", $col, $col);
345  }
346 
347  break;
348 
349  case "is not null":
350  $cond = " " . $col . " " . trim($op) . " ";
351  break;
352 
353  case "~*":
354  if (trim($val) != "") $cond = " " . $col . " " . trim($op) . " " . $this->_pg_val($val) . " ";
355  break;
356 
357  case "~^":
358  if (trim($val) != "") $cond = " " . $col . "~* '^" . pg_escape_string(trim($val)) . "' ";
359  break;
360 
361  case "~y":
362  if (!is_array($val)) $val = $this->_val2array($val);
363  if (count($val) > 0) $cond = " " . $col . " ~ '\\\\y(" . pg_escape_string(implode('|', $val)) . ")\\\\y' ";
364 
365  break;
366 
367  case "><":
368  if ((trim($val) != "") && (trim($val2) != "")) {
369  $cond = sprintf("%s >= %s and %s <= %s", $col, $this->_pg_val($val) , $col, $this->_pg_val($val2));
370  }
371  break;
372 
373  case "=~*":
374  switch ($atype) {
375  case "uid":
376  $err = simpleQuery(getDbAccessCore() , sprintf("select id from users where firstname ~* '%s' or lastname ~* '%s'", pg_escape_string($val) , pg_escape_string($val)) , $ids, true);
377  if ($err == "") {
378  if (count($ids) == 0) $cond = "false";
379  elseif (count($ids) == 1) {
380  $cond = " " . $col . " = " . intval($ids[0]) . " ";
381  } else {
382  $cond = " " . $col . " in (" . implode(',', $ids) . ") ";
383  }
384  }
385  break;
386 
387  case "docid":
388  if ($oa) {
389  $otitle = $oa->getOption("doctitle");
390  if (!$otitle) {
391  $fid = $oa->format;
392  if (!$fid) $err = sprintf(_("no compatible type with operator %s") , $op);
393  else {
394  if (!is_numeric($fid)) $fid = getFamidFromName($this->dbaccess, $fid);
395  $err = simpleQuery($this->dbaccess, sprintf("select id from doc%d where title ~* '%s'", $fid, pg_escape_string($val)) , $ids, true);
396  if ($err == "") {
397  if (count($ids) == 0) $cond = "false";
398  elseif (count($ids) == 1) {
399  $cond = " " . $col . " = '" . intval($ids[0]) . "' ";
400  } else {
401  $cond = " " . $col . " in ('" . implode("','", $ids) . "') ";
402  }
403  }
404  }
405  } else {
406  if ($otitle == "auto") $otitle = $oa->id . "_title";
407  $oat = $this->searchfam->getAttribute($otitle);
408  if ($oat) {
409  $cond = " " . $oat->id . " ~* '" . pg_escape_string(trim($val)) . "' ";
410  } else {
411  $err = sprintf(_("attribute %s : cannot detect title attribute") , $col);
412  }
413  }
414  } elseif ($col == "fromid") {
415  $err = simpleQuery($this->dbaccess, sprintf("select id from docfam where title ~* '%s'", pg_escape_string($val)) , $ids, true);
416  if ($err == "") {
417  if (count($ids) == 0) $cond = "false";
418  elseif (count($ids) == 1) {
419  $cond = " " . $col . " = " . intval($ids[0]) . " ";
420  } else {
421  $cond = " " . $col . " in (" . implode(",", $ids) . ") ";
422  }
423  }
424  }
425  break;
426 
427  default:
428  if ($atype) $err = sprintf(_("attribute %s : %s type is not allowed with %s operator") , $col, $atype, $op);
429  else $err = sprintf(_("attribute %s not found [%s]") , $col, $atype);
430  }
431  break;
432 
433  case "~@":
434  if (trim($val) != "") {
435  $cond = " " . $col . '_txt' . " ~ '" . strtolower($val) . "' ";
436  }
437  break;
438 
439  case "=@":
440  case "@@":
441  if (trim($val) != "") {
442  $tstatickeys = explode(' ', $val);
443  if (count($tstatickeys) > 1) {
444  $keyword.= str_replace(" ", "&", trim($val));
445  } else {
446  $keyword = trim($val);
447  }
448  if ($op == "@@") $cond = " " . $col . '_vec' . " @@ to_tsquery('french','." . unaccent(strtolower($keyword)) . "') ";
449  else if ($op == "=@") $cond = "fulltext @@ to_tsquery('french','" . unaccent(strtolower($keyword)) . "') ";
450  }
451  break;
452 
453  default:
454 
455  switch ($atype) {
456  case "enum":
457  $enum = $oa->getEnum();
458  if (strrpos($val, '.') !== false) $val = substr($val, strrpos($val, '.') + 1);
459  $tkids = array();;
460  foreach ($enum as $k => $v) {
461  if (in_array($val, explode(".", $k))) {
462  $tkids[] = substr($k, strrpos("." . $k, '.'));
463  }
464  }
465 
466  if ($op == '=') {
467  if ($oa->repeat) {
468  $cond = " " . $col . " ~ '\\\\y(" . pg_escape_string(implode('|', $tkids)) . ")\\\\y' ";
469  } else {
470  $cond = " $col='" . implode("' or $col='", $tkids) . "'";
471  }
472  } elseif ($op == '!=') {
473  if ($oa->repeat) {
474  $cond1 = " " . $col . " !~ '\\\\y(" . pg_escape_string(implode('|', $tkids)) . ")\\\\y' ";
475  } else {
476  $cond1 = " $col !='" . implode("' and $col != '", $tkids) . "'";
477  }
478  $cond = " (($cond1) or ($col is null))";
479  } elseif ($op == '!~*') {
480  $cond = sprintf("( (%s is null) or (%s %s %s) )", $col, $col, trim($op) , $this->_pg_val($val));
481  }
482 
483  break;
484 
485  case "docid":
486  if (!is_numeric($val)) $val = getIdFromName($this->dbaccess, $val);
487  default:
488  $cond1 = " " . $col . " " . trim($op) . $this->_pg_val($val) . " ";
489  if (($op == '!=') || ($op == '!~*')) {
490  $cond = "(($cond1) or ($col is null))";
491  } else $cond = $cond1;
492  }
493  }
494 
495  if (!$cond) $cond = "true";
496  return $cond;
497  }
498 
499  private static function _pg_val($s)
500  {
501  if (substr($s, 0, 2) == ':@') {
502  return " " . trim(strtok(substr($s, 2) , " \t")) . " ";
503  } else return " '" . pg_escape_string(trim($s)) . "' ";
504  }
505  /**
506  * return array of sql filter needed to search wanted document
507  */
509  {
510  $ol = $this->getValue("SE_OL");
511  $tkey = $this->getTValue("SE_KEYS");
512  $taid = $this->getTValue("SE_ATTRIDS");
513  $tf = $this->getTValue("SE_FUNCS");
514  $tlp = $this->getTValue("SE_LEFTP");
515  $tlr = $this->getTValue("SE_RIGHTP");
516  $tols = $this->getTValue("SE_OLS");
517 
518  if ($ol == "") {
519  // try in old version
520  $ols = $this->getTValue("SE_OLS");
521  $ol = $ols[1];
522  if ($ol) {
523  $this->setValue("SE_OL", $ol);
524  $this->modify();
525  }
526  }
527  if ($ol == "") $ol = "and";
528  $cond = "";
529  if (!$this->searchfam) {
530  $this->searchfam = new_doc($this->dbaccess, $this->getValue("se_famid"));
531  }
532  if ((count($taid) > 1) || ($taid[0] != "")) {
533  // special loop for revdate
534  foreach ($tkey as $k => $v) {
535  if (strtolower(substr($v, 0, 5)) == "::get") { // only get method allowed
536  // it's method call
537  $workdoc = $this->getSearchFamilyDocument();
538  if ($workdoc) $rv = $workdoc->ApplyMethod($v);
539  else $rv = $this->ApplyMethod($v);
540  $tkey[$k] = $rv;
541  }
542  if (substr($v, 0, 1) == "?") {
543  // it's a parameter
544  $rv = getHttpVars(substr($v, 1) , "-");
545  if ($rv == "-") return (false);
546  if ($rv === "" || $rv === " ") unset($taid[$k]);
547  else $tkey[$k] = $rv;
548  }
549  if ($taid[$k] == "revdate") {
550  list($dd, $mm, $yyyy) = explode("/", $tkey[$k]);
551  if ($yyyy > 0) $tkey[$k] = mktime(0, 0, 0, $mm, $dd, $yyyy);
552  }
553  }
554  foreach ($taid as $k => $v) {
555  $cond1 = $this->getSqlCond($taid[$k], trim($tf[$k]) , $tkey[$k]);
556  if ($cond == "") {
557  if ($tlp[$k] == "yes") $cond = '(' . $cond1 . " ";
558  else $cond = $cond1 . " ";
559  if ($tlr[$k] == "yes") $cond.= ')';
560  } elseif ($cond1 != "") {
561  if ($tols[$k] != "") $ol1 = $tols[$k];
562  else $ol1 = $ol;
563  if ($tlp[$k] == "yes") $cond.= $ol1 . ' (' . $cond1 . " ";
564  else $cond.= $ol1 . " " . $cond1 . " ";
565  if ($tlr[$k] == "yes") $cond.= ') ';
566  }
567  }
568  }
569  if (trim($cond) == "") $cond = "true";
570  return $cond;
571  }
572  /**
573  * return true if the search has parameters
574  */
575  function isParameterizable()
576  {
577  $tkey = $this->getTValue("SE_KEYS");
578 
579  if ((count($tkey) > 1) || ($tkey[0] != "")) {
580 
581  foreach ($tkey as $k => $v) {
582 
583  if ($v[0] == '?') {
584  return true;
585  //if (getHttpVars(substr($v,1),"-") == "-") return true;
586 
587  }
588  }
589  }
590  return false;
591  }
592  /**
593  * return true if the search need parameters
594  */
595  function needParameters()
596  {
597  $tkey = $this->getTValue("SE_KEYS");
598 
599  if ((count($tkey) > 1) || ($tkey[0] != "")) {
600 
601  foreach ($tkey as $k => $v) {
602 
603  if ($v[0] == '?') {
604  if (getHttpVars(substr($v, 1) , "-") == "-") return true;
605  }
606  }
607  }
608  return false;
609  }
610  /**
611  * Add parameters
612  */
613  function urlWhatEncodeSpec($l)
614  {
615  $tkey = $this->getTValue("SE_KEYS");
616 
617  if ((count($tkey) > 1) || ($tkey[0] != "")) {
618 
619  foreach ($tkey as $k => $v) {
620 
621  if ($v[0] == '?') {
622  if (getHttpVars(substr($v, 1) , "-") != "-") {
623  $l.= '&' . substr($v, 1) . "=" . getHttpVars(substr($v, 1));
624  }
625  }
626  }
627  }
628 
629  return $l;
630  }
631  /**
632  * add parameters in title
633  */
634  function getSpecTitle()
635  {
636  $tkey = $this->getTValue("SE_KEYS");
637  $taid = $this->getTValue("SE_ATTRIDS");
638  $l = "";
639  if ((count($tkey) > 1) || ($tkey[0] != "")) {
640  $tl = array();
641  foreach ($tkey as $k => $v) {
642 
643  if ($v[0] == '?') {
644  $vh = getHttpVars(substr($v, 1) , "-");
645  if (($vh != "-") && ($vh != "")) {
646 
647  if (is_numeric($vh)) {
648  $fam = $this->getSearchFamilyDocument();
649  if ($fam) {
650  $oa = $fam->getAttribute($taid[$k]);
651  if ($oa && $oa->type == "docid") {
652  $vh = $this->getTitle($vh);
653  }
654  }
655  }
656  $tl[] = $vh;
657  }
658  }
659  }
660  if (count($tl) > 0) {
661  $l = " (" . implode(", ", $tl) . ")";
662  }
663  }
664  return $this->getValue("ba_title") . $l;
665  }
666 
667  function viewdsearch($target = "_self", $ulink = true, $abstract = false)
668  {
669  // Compute value to be inserted in a layout
670  $this->viewattr();
671  //-----------------------------------------------
672  // display already condition written
673  $tkey = $this->getTValue("SE_KEYS");
674  $taid = $this->getTValue("SE_ATTRIDS");
675  $tf = $this->getTValue("SE_FUNCS");
676 
677  if ((count($taid) > 1) || ($taid[0] != "")) {
678 
679  $fdoc = new_Doc($this->dbaccess, $this->getValue("SE_FAMID", 1));
680  $zpi = $fdoc->GetNormalAttributes();
681  $zpi["state"] = new BasicAttribute("state", $this->fromid, _("state"));
682  $zpi["title"] = new BasicAttribute("title", $this->fromid, _("doctitle"));
683  $zpi["revdate"] = new BasicAttribute("revdate", $this->fromid, _("revdate"));
684  $zpi["cdate"] = new BasicAttribute("cdate", $this->fromid, _("cdate") , 'W', '', '', 'date');
685  $zpi["revision"] = new BasicAttribute("cdate", $this->fromid, _("revision"));
686  $zpi["owner"] = new BasicAttribute("owner", $this->fromid, _("owner"));
687  $zpi["locked"] = new BasicAttribute("owner", $this->fromid, _("locked"));
688  $zpi["allocated"] = new BasicAttribute("owner", $this->fromid, _("allocated"));
689  $zpi["svalues"] = new BasicAttribute("svalues", $this->fromid, _("any values"));
690 
691  foreach ($taid as $k => $v) {
692  $label = $zpi[$taid[$k]]->getLabel();
693  if ($label == "") $label = $taid[$k];
694  $tcond[]["condition"] = sprintf("%s %s %s", $label, $this->getOperatorLabel($tf[$k], $zpi[$taid[$k]]->type) , ($tkey[$k] != "") ? _($tkey[$k]) : $tkey[$k]);
695  if ($tkey[$k][0] == '?') {
696  $tparm[substr($tkey[$k], 1) ] = $taid[$k];
697  }
698  }
699  $this->lay->SetBlockData("COND", $tcond);
700  }
701  $this->lay->Set("ddetail", "");
702  }
703  /**
704  * return true if the sqlselect is writted by hand
705  * @return bool
706  */
707  function isStaticSql()
708  {
709  return ($this->getValue("se_static") != "");
710  }
711  /**
712  * return family use for search
713  * @return Doc
714  */
715  private function getSearchFamilyDocument()
716  {
717  static $fam = null;
718  if (!$fam) $fam = createTmpDoc($this->dbaccess, $this->getValue("SE_FAMID", 1));
719  return $fam;
720  }
721 
722  function paramdsearch($target = "_self", $ulink = true, $abstract = false)
723  {
724  // Compute value to be inserted in a layout
725  $this->viewattr();
726  //-----------------------------------------------
727  // display already condition written
728  $tkey = $this->getTValue("SE_KEYS");
729  $taid = $this->getTValue("SE_ATTRIDS");
730  $tf = $this->getTValue("SE_FUNCS");
731 
732  if ((count($taid) > 1) || ($taid[0] != "")) {
733 
734  $fdoc = new_Doc($this->dbaccess, $this->getValue("SE_FAMID", 1));
735  $zpi = $fdoc->GetNormalAttributes();
736  $zpi["state"] = new BasicAttribute("state", $this->fromid, _("state"));
737  $zpi["title"] = new BasicAttribute("title", $this->fromid, _("doctitle"));
738  $zpi["revdate"] = new BasicAttribute("revdate", $this->fromid, _("revdate"));
739  $zpi["cdate"] = new BasicAttribute("cdate", $this->fromid, _("cdate") , 'W', '', '', 'date');
740  $zpi["revision"] = new BasicAttribute("cdate", $this->fromid, _("revision"));
741  $zpi["owner"] = new BasicAttribute("owner", $this->fromid, _("owner"));
742  $zpi["locked"] = new BasicAttribute("owner", $this->fromid, _("locked"));
743  $zpi["allocated"] = new BasicAttribute("owner", $this->fromid, _("allocated"));
744  $zpi["svalues"] = new BasicAttribute("svalues", $this->fromid, _("any values"));
745 
746  foreach ($taid as $k => $v) {
747  if ($tkey[$k][0] == '?') {
748  $tparm[substr($tkey[$k], 1) ] = $taid[$k];
749  $toperator[substr($tkey[$k], 1) ] = $tf[$k];
750  }
751  }
752  $this->lay->SetBlockData("COND", $tcond);
753  }
754 
755  $this->lay->Set("ddetail", "");
756  if (count($tparm) > 0) {
757  include_once ("FDL/editutil.php");
758  global $action;
759  editmode($action);
760 
761  $doc = $this->getSearchFamilyDocument();
762  $inputset = array();
763  $ki = 0; // index numeric
764  foreach ($tparm as $k => $v) {
765  if (isset($inputset[$v])) {
766  // need clone when use several times the same attribute
767  $vz = $v . "Z" . $ki;
768  $zpi[$vz] = $zpi[$v];
769  $zpi[$vz]->id = $vz;
770  $v = $vz;
771  }
772  if ($zpi[$v]->fieldSet->type == 'array') $zpi[$v]->fieldSet->type = 'frame'; // no use array configuration for help input
773  $ki++;
774  $inputset[$v] = true;
775 
776  $ttransfert[] = array(
777  "idi" => $v,
778  "idp" => $k,
779  "value" => getHttpVars($k)
780  );
781  $tinputs[$k]["label"] = $zpi[$v]->getLabel();
782  $tinputs[$k]["operator"] = $this->getOperatorLabel($toperator[$k], $zpi[$v]->type);
783  if (($toperator[$k] == "=~*" || $toperator[$k] == "~*") && $zpi[$v]->type == "docid") $zpi[$v]->type = "text"; // present like a search when operator is text search
784  if ($zpi[$v]->visibility == 'R') $zpi[$v]->mvisibility = 'W';
785  if ($zpi[$v]->visibility == 'S') $zpi[$v]->mvisibility = 'W';
786  if (isset($zpi[$v]->id)) {
787  $zpi[$v]->isAlone = true;
788  $tinputs[$k]["inputs"] = getHtmlInput($doc, $zpi[$v], getHttpVars($k));
789  } else {
790  $aotxt = new BasicAttribute($v, $doc->id, "eou");
791  if ($v == "revdate") $aotxt->type = "date";
792  $tinputs[$k]["inputs"] = getHtmlInput($doc, $aotxt, getHttpVars($k));
793  }
794  }
795  $this->lay->setBlockData("PARAM", $tinputs);
796  $this->lay->setBlockData("TRANSFERT", $ttransfert);
797  $this->lay->setBlockData("PINPUTS", $ttransfert);
798  $this->lay->Set("ddetail", "none");
799  $this->lay->set("stext", _("send search"));
800  $this->lay->set("saction", getHttpVars("saction", "FREEDOM_VIEW"));
801  $this->lay->set("sapp", getHttpVars("sapp", "FREEDOM"));
802  $this->lay->set("sid", getHttpVars("sid", "dirid"));
803  $this->lay->set("starget", getHttpVars("starget", ""));
804  $this->lay->set("icon", $this->getIcon());
805  }
806  }
807  // -----------------------------------
808  function editdsearch()
809  {
810  global $action;
811 
812  $famid = GetHttpVars("sfamid", $this->getValue("SE_FAMID", 1));
813  $onlysubfam = GetHttpVars("onlysubfam"); // restricy to sub fam of
814  $dirid = GetHttpVars("dirid");
815  $this->lay->set("ACTION", $action->name);
816  $tclassdoc = array();
817  $action->parent->AddJsRef($action->GetParam("CORE_PUBURL") . "/FDL/Layout/edittable.js");
818  $action->parent->AddJsRef($action->GetParam("CORE_PUBURL") . "/FREEDOM/Layout/editdsearch.js");
819 
820  if ($dirid > 0) {
821  $dir = new_Doc($this->dbaccess, $dirid);
822  if (method_exists($dir, "isAuthorized")) {
823  if ($dir->isAuthorized($classid)) {
824  // verify if classid is possible
825  if ($dir->hasNoRestriction()) {
826  $tclassdoc = GetClassesDoc($this->dbaccess, $action->user->id, $classid, "TABLE");
827  $tclassdoc[] = array(
828  "id" => 0,
829  "title" => _("any families")
830  );
831  } else {
832  $tclassdoc = $dir->getAuthorizedFamilies();
833  $this->lay->set("restrict", true);
834  }
835  } else {
836  $tclassdoc = $dir->getAuthorizedFamilies();
837  $first = current($tclassdoc);
838  $famid1 = ($first["id"]);
839  $this->lay->set("restrict", true);
840  $tfamids = array_keys($tclassdoc);
841  if (!in_array($famid, $tfamids)) $famid = $famid1;
842  }
843  } else {
844  $tclassdoc = GetClassesDoc($this->dbaccess, $action->user->id, $classid, "TABLE");
845  }
846  } else {
847  if ($onlysubfam) {
848  $alsosub = true;
849  if (!is_numeric($onlysubfam)) $onlysubfam = getFamIdFromName($this->dbaccess, $onlysubfam);
850  $cdoc = new_Doc($this->dbaccess, $onlysubfam);
851  $tsub = $cdoc->GetChildFam($cdoc->id, false);
852  if ($alsosub) {
853  $tclassdoc[$classid] = array(
854  "id" => $cdoc->id,
855  "title" => $cdoc->title
856  );
857  $tclassdoc = array_merge($tclassdoc, $tsub);
858  } else {
859  $tclassdoc = $tsub;
860  }
861  $first = current($tclassdoc);
862  if ($classid == "") $classid = $first["id"];
863  } else {
864  $tclassdoc = GetClassesDoc($this->dbaccess, $action->user->id, $classid, "TABLE");
865  $tclassdoc[] = array(
866  "id" => 0,
867  "title" => _("any families")
868  );
869  }
870  }
871 
872  $this->lay->set("onlysubfam", $onlysubfam);
873  $selfam = false;
874  foreach ($tclassdoc as $k => $cdoc) {
875  $selectclass[$k]["idcdoc"] = $cdoc["id"];
876  $selectclass[$k]["classname"] = $cdoc["title"];
877  if (abs($cdoc["id"]) == abs($famid)) {
878  $selfam = true;
879  $selectclass[$k]["selected"] = "selected";
880  if ($famid < 0) $this->lay->set("selfam", $cdoc["title"] . " " . _("(only)"));
881  else $this->lay->set("selfam", $cdoc["title"]);
882  } else $selectclass[$k]["selected"] = "";
883  }
884  if (!$selfam) {
885  $famid = abs($this->getValue("se_famid"));
886  if ($this->id && $famid) {
887  $selectclass[] = array(
888  "idcdoc" => $famid,
889  "classname" => $this->getTitle($famid) ,
890  "selected" => "selected"
891  );
892  } else {
893  reset($tclassdoc);
894  $first = current($tclassdoc);
895  $famid = $first["id"];
896  }
897  }
898  $this->lay->Set("dirid", $dirid);
899  $this->lay->Set("classid", $this->fromid);
900  $this->lay->SetBlockData("SELECTCLASS", $selectclass);
901  $this->setFamidInLayout();
902  // display attributes
903  $tattr = array();
904  $internals = array(
905  "title" => _("doctitle") ,
906  "revdate" => _("revdate") ,
907  "cdate" => _("cdate") ,
908  "revision" => _("revision") ,
909  "owner" => _("id owner") ,
910  "locked" => _("id locked") ,
911  "allocated" => _("id allocated") ,
912  "svalues" => _("any values")
913  );
914 
915  $tattr[] = array(
916  "attrid" => "_prop",
917  "attrtype" => "set",
918  "attrdisabled" => "disabled",
919  "attrname" => _("DocProperties") ,
920  "ismultiple" => 'no'
921  );
922 
923  foreach ($internals as $k => $v) {
924  if ($k == "revdate") $type = "date";
925  else if ($k == "owner") $type = "docid";
926  else if ($k == "locked") $type = "docid";
927  else if ($k == "allocated") $type = "docid";
928  else if ($k == "cdate") $type = "date";
929  else if ($k == "revision") $type = "int";
930  else if ($k == "state") $type = "docid";
931  else $type = "text";
932 
933  $tattr[] = array(
934  "attrid" => $k,
935  "ismultiple" => 'no',
936  "attrtype" => $type,
937  "attrdisabled" => "",
938  "attrname" => $v
939  );
940  }
941 
942  $fdoc = new_Doc($this->dbaccess, abs($famid));
943  $zpi = $fdoc->GetNormalAttributes();
944  $lastSet = array();
945  foreach ($zpi as $k => $v) {
946  if ($v->type == "array") continue;
947  $type = $v->type;
948  if ($lastSet[0] != $v->fieldSet->id) {
949  $tset = $this->editGetSetAttribute($v->fieldSet);
950  if (count($tset) > 0) $tattr = array_merge($tattr, array_reverse($tset));
951  }
952 
953  $tattr[] = array(
954  "attrid" => $v->id,
955  "ismultiple" => ($v->isMultiple()) ? 'yes' : 'no',
956  "attrtype" => $type,
957  "attrdisabled" => "",
958  "attrname" => $v->getLabel()
959  );
960  }
961 
962  $this->lay->SetBlockData("ATTR", $tattr);
963 
964  foreach ($this->top as $k => $v) {
965  $display = '';
966  if (isset($v["type"])) {
967  $ctype = implode(",", $v["type"]);
968  if (!in_array('text', $v["type"])) $display = 'none'; // first is title
969 
970  } else $ctype = "";
971 
972  $tfunc[] = array(
973  "funcid" => $k,
974  "functype" => $ctype,
975  "funcdisplay" => $display,
976  "funcname" => _($v["label"])
977  );
978  }
979  $this->lay->SetBlockData("FUNC", $tfunc);
980  foreach ($tfunc as $k => $v) {
981  if (($v["functype"] != "") && (strpos($v["functype"], "enum") === false)) unset($tfunc[$k]);
982  }
983  $this->lay->SetBlockData("FUNCSTATE", $tfunc);
984  $this->lay->Set("icon", $fdoc->getIcon());
985 
986  if ($this->getValue("SE_LATEST") == "no") $this->lay->Set("select_all", "selected");
987  else $this->lay->Set("select_all", "");
988  //-----------------------------------------------
989  // display state
990  if ($fdoc->wid > 0) {
991  $wdoc = new_Doc($this->dbaccess, $fdoc->wid);
992  $states = $wdoc->getStates();
993 
994  $tstates = array();
995  while (list($k, $v) = each($states)) {
996  $tstates[] = array(
997  "stateid" => $v,
998  "statename" => _($v)
999  );
1000  }
1001  $this->lay->SetBlockData("STATE", $tstates);
1002  $this->lay->Set("dstate", "inline");
1003  } else {
1004  $this->lay->Set("dstate", "none");
1005  }
1006  //-----------------------------------------------
1007  // display already condition written
1008  $tol = $this->getTValue("SE_OLS");
1009  $tkey = $this->getTValue("SE_KEYS");
1010  $taid = $this->getTValue("SE_ATTRIDS");
1011  $tf = $this->getTValue("SE_FUNCS");
1012  $tlp = $this->getTValue("SE_LEFTP");
1013  $trp = $this->getTValue("SE_RIGHTP");
1014 
1015  $cond = "";
1016  $tcond = array();
1017 
1018  if ((count($taid) > 1) || ($taid[0] != "")) {
1019  foreach ($taid as $k => $va) {
1020  $docid_aid = 0;
1021  $v = $tkey[$k];
1022  $oa = $fdoc->getAttribute($taid[$k]);
1023  $tcond[$k] = array(
1024  "OLCOND" => "olcond$k",
1025  "ATTRCOND" => "attrcond$k",
1026  "FUNCCOND" => "funccond$k",
1027  "ISENUM" => (($taid[$k] == "state") || ($oa->type == "enum")) ,
1028  "SSTATE" => "sstate$k",
1029  "ols_and_selected" => ($tol[$k] == "and") ? "selected" : "",
1030  "ols_or_selected" => ($tol[$k] == "or") ? "selected" : "",
1031  "leftp_none_selected" => ($tlp[$k] != "yes") ? "selected" : "",
1032  "leftp_open_selected" => ($tlp[$k] == "yes") ? "selected" : "",
1033  "rightp_none_selected" => ($trp[$k] != "yes") ? "selected" : "",
1034  "rightp_open_selected" => ($trp[$k] == "yes") ? "selected" : "",
1035  "key" => $v
1036  );
1037 
1038  $tattr = array();
1039  if ($taid[$k] == "state") {
1040  $tstates = array();
1041  $stateselected = false;
1042  foreach ($states as $ks => $vs) {
1043  $tstates[] = array(
1044  "sstateid" => $vs,
1045  "sstate_selected" => ($vs == $v) ? "selected" : "",
1046  "sstatename" => _($vs)
1047  );
1048  if ($vs == $v) $stateselected = true;
1049  }
1050  if (!$stateselected) $tcond[$k]["ISENUM"] = false;
1051  $this->lay->SetBlockData("sstate$k", $tstates);
1052 
1053  $tattr[] = array(
1054  "attrid" => $taid[$k],
1055  "ismultiple" => 'no',
1056  "attrtype" => "docid",
1057  "attrselected" => "selected",
1058  "attrname" => _("state")
1059  );
1060  } else {
1061  if ($oa->type == "enum") {
1062  $te = $oa->getEnum();
1063  $tstates = array();
1064  $enumselected = false;
1065  foreach ($te as $ks => $vs) {
1066  $tstates[] = array(
1067  "sstateid" => $ks,
1068  "sstate_selected" => ($ks == $v) ? "selected" : "",
1069  "sstatename" => $vs
1070  );
1071  if ($ks == $v) $enumselected = true;
1072  }
1073  $this->lay->SetBlockData("sstate$k", $tstates);
1074  if (!$enumselected) $tcond[$k]["ISENUM"] = false;
1075  }
1076 
1077  $tattr[] = array(
1078  "attrid" => "_prop",
1079  "ismultiple" => 'no',
1080  "attrtype" => "set",
1081  "attrdisabled" => "disabled",
1082  "attrselected" => "",
1083  "attrname" => _("DocProperties")
1084  );
1085 
1086  foreach ($internals as $ki => $vi) {
1087  if ($ki == "revdate") $type = "date";
1088  else if ($ki == "owner") $type = "docid";
1089  else $type = "text";
1090 
1091  $tattr[] = array(
1092  "attrid" => $ki,
1093  "ismultiple" => 'no',
1094  "attrtype" => $type,
1095  "attrselected" => ($taid[$k] == $ki) ? "selected" : "",
1096  "attrdisabled" => "",
1097  "attrname" => $vi
1098  );
1099  }
1100  $lastSet = array();
1101  $this->editGetSetAttribute(null, true);
1102  foreach ($zpi as $ki => $vi) {
1103  $type = $vi->type;
1104  if ($lastSet[0] != $vi->fieldSet->id) {
1105 
1106  $tset = $this->editGetSetAttribute($vi->fieldSet);
1107  if (count($tset) > 0) $tattr = array_merge($tattr, array_reverse($tset));
1108  }
1109 
1110  $tattr[] = array(
1111  "attrid" => $vi->id,
1112  "ismultiple" => ($vi->isMultiple()) ? 'yes' : 'no',
1113  "attrtype" => $type,
1114  "attrselected" => ($taid[$k] == $vi->id) ? "selected" : "",
1115  "attrdisabled" => "",
1116  "attrname" => $vi->getLabel()
1117  );
1118  }
1119  }
1120  $this->lay->SetBlockData("attrcond$k", $tattr);
1121 
1122  $tfunc = array();
1123 
1124  foreach ($this->top as $ki => $vi) {
1125  $oa = $fdoc->getAttribute($taid[$k]);
1126  $type = $oa->type;
1127  if ($type == "") {
1128  if ($taid[$k] == "title") $type = "text";
1129  elseif ($taid[$k] == "cdate") $type = "date";
1130  elseif ($taid[$k] == "revision") $type = "int";
1131  elseif ($taid[$k] == "allocated") $type = "docid";
1132  elseif ($taid[$k] == "locked") $type = "docid";
1133  elseif ($taid[$k] == "revdate") $type = "date";
1134  elseif ($taid[$k] == "owner") $type = "docid";
1135  elseif ($taid[$k] == "svalues") $type = "text";
1136  elseif ($taid[$k] == "state") $type = "enum";
1137  } else {
1138  if ($oa->inArray() && ($oa->type != 'file')) $type = "array";
1139  }
1140  $display = '';
1141  $ctype = '';
1142  if (isset($vi["type"])) {
1143  if (!in_array($type, $vi["type"])) $display = 'none';
1144  $ctype = implode(",", $vi["type"]);
1145  }
1146  if ($tf[$k] == $ki && $type == 'docid' && $display == '' && ($ki == '=' || $ki == '!=')) {
1147  $docid_aid = $taid[$k];
1148  }
1149  $tfunc[] = array(
1150  "func_id" => $ki,
1151  "func_selected" => ($tf[$k] == $ki) ? "selected" : "",
1152  "func_display" => $display,
1153  "func_type" => $ctype,
1154  "func_name" => _($vi["label"])
1155  );
1156  }
1157  $this->lay->SetBlockData("funccond$k", $tfunc);
1158 
1159  $tols = array();
1160  foreach ($this->tol as $ki => $vi) {
1161  $tols[] = array(
1162  "ol_id" => $ki,
1163  "ol_selected" => ($tol[$k] == $ki) ? "selected" : "",
1164  "ol_name" => _($vi)
1165  );
1166  }
1167  $this->lay->SetBlockData("olcond$k", $tols);
1168  if (is_numeric($v) && (!empty($docid_aid))) {
1169  $tcond[$k]["ISENUM"] = false;
1170  $tcond[$k]["ISDOCID"] = true;
1171  $tcond[$k]["DOCID_AID"] = $docid_aid;
1172  $tcond[$k]["DOCID_AIDINDEX"] = $docid_aid . $k;
1173  $tcond[$k]["DOCID_TITLE"] = $this->getTitle($v);
1174  $tcond[$k]["FAMID"] = abs($famid);
1175  } else {
1176  $tcond[$k]["ISDOCID"] = false;
1177  $tcond[$k]["DOCID_AID"] = 0;
1178  $tcond[$k]["DOCID_AIDINDEX"] = 0;
1179  $tcond[$k]["DOCID_TITLE"] = '';
1180  $tcond[$k]["FAMID"] = abs($famid);
1181  }
1182  }
1183  }
1184  if (count($tcond) > 0) $this->lay->SetBlockData("CONDITIONS", $tcond);
1185  // Add select for enum attributes
1186  $tenums = array();
1187  foreach ($zpi as $k => $v) {
1188  if (($v->type == "enum") || ($v->type == "enumlist")) {
1189  $tenums[] = array(
1190  "SELENUM" => "ENUM$k",
1191  "attrid" => $v->id
1192  );
1193  $tenum = $v->getEnum();
1194  $te = array();
1195  foreach ($tenum as $ke => $ve) {
1196  $te[] = array(
1197  "enumkey" => $ke,
1198  "enumlabel" => $ve
1199  );
1200  }
1201  $this->lay->setBlockData("ENUM$k", $te);
1202  }
1203  }
1204 
1205  $this->lay->setBlockData("ENUMS", $tenums);
1206 
1207  $this->lay->Set("id", $this->id);
1208  $this->editattr();
1209  }
1210 
1211  private function editGetSetAttribute($fs, $reset = false)
1212  {
1213  static $setAttribute = array();
1214  $level = 0;
1215  $tset = array();
1216  if ($reset) $setAttribute = array();
1217  while ($fs && $fs->id != 'FIELD_HIDDENS') {
1218  if (!in_array($fs->id, $setAttribute)) {
1219  $tset[] = array(
1220  "attrid" => $fs->id,
1221  "attrtype" => "set",
1222  "attrdisabled" => "disabled",
1223  "attrselected" => "",
1224  "attrname" => $fs->getLabel()
1225  );
1226  $setAttribute[] = $fs->id;
1227  $level++;
1228  $fs = $fs->fieldSet;
1229  } else {
1230  break;
1231  }
1232  }
1233  return $tset;
1234  }
1235  /**
1236  * @begin-method-ignore
1237  * this part will be deleted when construct document class until end-method-ignore
1238  */
1239  }
1240  /*
1241  * @end-method-ignore
1242  */
1243 ?>
← centre documentaire © anakeen - published under CC License - Dynacase