Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.Collection.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 Object Definition
9  *
10  * @author Anakeen 2002
11  * @version $Id: $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  */
15 /**
16  */
17 include_once ("DATA/Class.Document.php");
18 include_once ("DATA/Lib.FullSearch.php");
19 /**
20  * Document Class
21  *
22  */
24 {
25  private $completeProperties = false;
26  private $contentOnlyValue = true;
27  private $contentOrderBy = '';
28  private $contentSlice = 'ALL';
29  private $contentStart = 0;
30  private $contentKey = '';
31  private $contentKeyMode = '';
32  private $contentSearchProperty = '';
33  private $contentFilter = '';
34  private $contentVerifyHasChild = false;
35  private $contentRecursiveLevel = 0;
36  private $contentMap = null;
37  /**
38  * Internal document list
39  * @var DocumentList
40  */
41  private $documentList = null;
42 
44  {
45  $this->completeProperties = $value;
46  }
47  public function setContentOnlyValue($value)
48  {
49  $this->contentOnlyValue = $value;
50  }
51  public function setContentOrderBy($value)
52  {
53  $this->contentOrderBy = $value;
54  }
55  public function setContentSlice($value)
56  {
57  $this->contentSlice = $value;
58  }
59  public function setContentStart($value)
60  {
61  $this->contentStart = $value;
62  }
63  public function setContentKey($value)
64  {
65  $this->contentKey = $value;
66  }
67  public function setContentKeyMode($value)
68  {
69  $this->contentKeyMode = $value;
70  }
72  {
73  $this->contentSearchProperty = $value;
74  }
75  public function setContentFilter($value)
76  {
77  $this->contentFilter = $value;
78  }
80  {
81  $this->contentVerifyHasChild = $value;
82  }
84  {
85  $this->contentRecursiveLevel = intval($value);
86  }
87  public function setContentMap($callback)
88  {
89  $this->contentMap = $callback;
90  }
91  /**
92  * return documents list
93  */
94  public function getContent()
95  {
96  if ($this->documentList) return $this->getDocumentListContent();
97  else return $this->getCollectionContent();
98  }
99  /**
100  * return documents list
101  * @param boolean $onlyvalues
102  * @param boolean $completeprop
103  * @param string $filter
104  * @param int $start
105  * @param int $slice
106  * @param string $orderby
107  * @param boolean $verifyhaschild
108  * @return array of raw documents
109  */
110  public function getCollectionContent()
111  {
112  include_once ("FDL/Class.SearchDoc.php");
113  $s = new SearchDoc($this->dbaccess);
114  $s->useCollection($this->getProperty('initid'));
115  if ($this->contentOrderBy) $s->orderby = $this->contentOrderBy;
116  $s->setSlice($this->contentSlice);
117  $s->setStart($this->contentStart);
118  $s->excludeConfidential();
119  $s->recursiveSearch = ($this->contentRecursiveLevel > 0);
120  $s->folderRecursiveLevel = $this->contentRecursiveLevel;
121 
122  $out = false;
123  $content = array();
124  if ($s->dirid > 0) {
125  $s->setObjectReturn();
126  $key = $this->contentKey;
127  if ($key) {
128  if ($this->contentKeyMode == "word") {
129  $sqlfilters = array();
130  $fullorderby = '';
131  $keyword = '';
132  DocSearch::getFullSqlFilters($key, $sqlfilters, $fullorderby, $keyword);
133  foreach ($sqlfilters as $vfilter) $s->addFilter($vfilter);
134  if (!$s->orderby) $s->orderby = $fullorderby;
135  } else {
136  $s->addFilter("%s ~* '%s'", ($this->contentSearchProperty ? $this->contentSearchProperty : "svalues") , $key);
137  }
138  }
139  if ($this->contentFilter) {
140  if (is_string($this->contentFilter)) {
141  $lfilter = strtolower($this->contentFilter);
142  if ((!strstr($lfilter, '--')) && (!strstr($lfilter, ';')) && (!strstr($lfilter, 'insert')) && (!strstr($lfilter, 'alter')) && (!strstr($lfilter, 'delete')) && (!strstr($lfilter, 'update'))) {
143  // try to prevent sql injection
144  $s->addFilter($this->contentFilter);
145  }
146  } elseif (is_object($this->contentFilter)) {
147  $ofamid = 0;
148  $sfilter = '';
149  $err = $this->doc->object2SqlFilter($this->contentFilter, $ofamid, $sfilter);
150  $this->setError($err);
151  if ($ofamid) $s->fromid = $ofamid;
152  $s->addFilter($sfilter);
153  }
154  }
155  if ($err == "") {
156  $s->search();
157  $out->info = $s->getSearchInfo();
158  $err = $s->getError();
159  if ($err) {
160  $this->setError($out->info["error"]);
161  } else {
162  $this->useDocumentList($s->getDocumentList());
163  return $this->getDocumentListContent();
164  }
165  }
166  } else $this->error = sprintf(_("document not initialized"));
167  $out->error = $this->error;
168 
169  foreach ($content as & $v) {
170  $out->content[] = $v;
171  }
172  $out->slice = $s->slice;
173  $out->start = $s->start;
174  $out->date = date('Y-m-d H:i:s');
175 
176  return $out;
177  }
178  /**
179  * use DocumentList instead a folder
180  * @param DocumentList $dl
181  * @return void
182  */
183  public function useDocumentList(DocumentList & $dl)
184  {
185  $this->documentList = $dl;
186  }
187  /**
188  * return documents list
189  * @param boolean $onlyvalues
190  * @param boolean $completeprop
191  * @return array of raw documents
192  */
193  private function getDocumentListContent()
194  {
195  $dl = $this->documentList;
196  $content = null;
197  if (!$dl) {
198  $this->setError("document list uninitialized");
199  } else {
200  $content = array();
201  $s = $dl->getSearchDocument();
202  if ($this->contentMap) $dl->listMap($this->contentMap);
203  $out = null;
204  $out->info = $s->getSearchInfo();
205  $out->slice = $s->slice;
206  $out->start = $s->start;
207  $this->setError($out->info["error"]);
208  $tmpdoc = new Fdl_Document();
209  $kd = 0;
210  $verifyhaschild = $this->contentVerifyHasChild;
211  foreach ($dl as $doc) {
212  $tmpdoc->affect($doc);
213  if (!$doc->isConfidential()) {
214  if ($verifyhaschild) {
215  $tmpdoc->setVolatileProperty("haschildfolder", hasChildFld($this->dbaccess, $tmpdoc->getProperty('initid') , ($doc->doctype == 'S')));
216  }
217  $content[$kd] = $tmpdoc->getDocument($this->contentOnlyValue, $this->completeProperties);
218  $kd++;
219  }
220  }
221 
222  $out->totalCount = $s->count();
223  if (($out->totalCount == $s->slice) || ($s->start > 0)) {
224  $s->slice = 'ALL';
225  $s->start = 0;
226  $s->reset();
227  $oc = $s->onlyCount();
228  $out->info["totalCount"] = $s->getSearchInfo();
229  if ($oc) $out->totalCount = $oc;
230  }
231  }
232  $out->error = $this->error;
233  $out->content = $content;
234  $out->date = date('Y-m-d H:i:s');
235  return $out;
236  }
237  /**
238  * return document list from a keyword and optionnaly family identificator
239  * @param string $key
240  * @param string $mode search method : regexp or word
241  * @param int $famid filter on family
242  * @param object $filter additionnal filter
243  * @param int $start offset to start search (default is 0)
244  * @param int $slice number of document returned
245  * @param string $orderby order by property or attribute
246  * @param boolean $onlyvalues set to true is want return attribute definition also
247  * @param string $searchproperty property use where key is applied
248  * @param boolean $whl with highlight return also text including keyword. the keyword is between HTML B tag.
249  * @param boolean $verifyhaschild
250  * @return array of raw document
251  */
252  public function simpleSearch($key, $mode = "word", $famid = 0, $filter = "", $start = 0, $slice = 100, $orderby = "", $onlyvalues = true, $searchproperty = "svalues", $whl = false, $verifyhaschild = false)
253  {
254  include_once ("FDL/Class.SearchDoc.php");
255  static $sw = null;
256  $out = '';
257  $tfid = array();
258  if (strstr($famid, '|')) {
259  // multi family search
260  $tfamids = explode('|', $famid);
261  foreach ($tfamids as $fid) {
262  if (!is_numeric($fid)) $fid = getFamidFromName($this->dbaccess, $fid);
263  if ($fid > 0) $tfid[] = $fid;
264  }
265 
266  $famid = 0;
267  }
268  if (preg_match('/([\w:]*)\s?strict/', trim($famid) , $reg)) {
269  if (!is_numeric($reg[1])) $reg[1] = getFamIdFromName($this->dbaccess, $reg[1]);
270  $famid = '-' . $reg[1];
271  }
272  $s = new SearchDoc($this->dbaccess, $famid);
273  if ($key) {
274  if ($mode == "word") {
275  $sqlfilters = array();
276  $fullorderby = '';
277  $keyword = '';
278  DocSearch::getFullSqlFilters($key, $sqlfilters, $fullorderby, $keyword);
279  foreach ($sqlfilters as $vfilter) $s->addFilter($vfilter);
280  if (!$orderby) $orderby = $fullorderby;
281  } else {
282  $s->addFilter(sprintf("%s ~* '%s'", $searchproperty, $key));
283  }
284  }
285  if ($filter) {
286  if (is_string($filter)) {
287  $lfilter = strtolower($filter);
288  if ((!strstr($lfilter, '--')) && (!strstr($lfilter, ';')) && (!stristr($lfilter, 'insert')) && (!stristr($lfilter, 'alter')) && (!stristr($lfilter, 'delete')) && (!stristr($lfilter, 'update'))) {
289  // try to prevent sql injection
290  $s->addFilter($filter);
291  }
292  } elseif (is_object($filter)) {
293  if (!$sw) {
294  $sw = createTmpDoc($this->dbaccess, "DSEARCH");
295  $sw->setValue("se_famid", $famid);
296  }
297  $ofamid = 0;
298  $sfilter = '';
299  $err = $sw->object2SqlFilter($filter, $ofamid, $sfilter);
300  $this->setError($err);
301  if ($ofamid) {
302  $s->fromid = $ofamid;
303  }
304  $s->addFilter($sfilter);
305  }
306  }
307  if ($err == "") {
308  if (count($tfid) > 0) $s->addFilter(getSqlCond($tfid, 'fromid', true));
309  $completeprop = false;
310  $content = array();
311  $s->slice = $slice;
312  $s->start = $start;
313  if ($orderby) $s->orderby = $orderby;
314  $s->setDebugMode();
315  $s->setObjectReturn();
316  $s->excludeConfidential();
317  $s->search();
318  $info = $s->getDebugInfo();
319  $out->error = $info["error"];
320  $out->info = $info;
321 
322  if (!$out->error) {
323  $ws = createTmpDoc($this->dbaccess, "DSEARCH");
324  $ws->setValue("ba_title", sprintf(_("search %s") , $key));
325  $ws->add();
326  $ws->addStaticQuery($s->getOriginalQuery());
327  $tmpdoc = new Fdl_Document($ws->id);
328  $out->document = $tmpdoc->getDocument(true, false);
329  $idx = 0;
330  if (!$keyword) $keyword = str_replace(" ", "|", $key);
331  while ($doc = $s->nextDoc()) {
332  $tmpdoc->affect($doc);
333  if ($verifyhaschild) {
334  $tmpdoc->setVolatileProperty("haschildfolder", hasChildFld($this->dbaccess, $tmpdoc->getProperty('initid') , ($doc->doctype == 'S')));
335  }
336  $content[$idx] = $tmpdoc->getDocument($onlyvalues, $completeprop);
337  if ($whl) $content[$idx]['highlight'] = getHighlight($doc, $keyword);
338  $idx++;
339  }
340 
341  $out->totalCount = $s->count();
342  if (($out->totalCount == $slice) || ($start > 0)) {
343  $s->slice = 'ALL';
344  $s->start = 0;
345  $s->setDebugMode();
346  $s->reset();
347  $out->totalCount = $s->onlyCount();
348  $info = $s->getDebugInfo();
349 
350  $out->delay.= ' count:' . $info["delay"];
351  }
352  $out->content = $content;
353  }
354  } else {
355  $out->error = $err;
356  }
357  $out->slice = $slice;
358  $out->start = $start;
359  $out->date = date('Y-m-d H:i:s');
360  return $out;
361  }
362  /**
363  * return child families
364  * @param int $famid the family root
365  * @return array of families where $famid is an ancestor
366  */
367  public function getSubFamilies($famid, $controlcreate = false)
368  {
369  $out = '';
370  $fam = new_doc($this->dbaccess, $famid);
371  if (!$fam->isAlive()) {
372  $out->error = sprintf(_("data:family %s not alive") , $famid);
373  } elseif ($fam->doctype != 'C') {
374  $out->error = sprintf(_("data:document %s is not a family") , $famid);
375  } else {
376 
377  $content = array();
378  $fld = new Dir($this->dbaccess);
379  if (!is_numeric($famid)) $famid = getFamIdFromName($this->dbaccess, $famid);
380  $tfam = $fld->GetChildFam($famid, $controlcreate);
381  if (count($tfam) > 0) {
382  $tmpdoc = new Fdl_Document();
383  $onlyvalues = true;
384  $completeprop = false;
385  foreach ($tfam as $id => $rawfam) {
386  $fam->affect($rawfam);
387  $tmpdoc->affect($fam);
388  if (!$tmpdoc->error) {
389  $content[] = $tmpdoc->getDocument($onlyvalues, $completeprop);
390  }
391  }
392  }
393  $out->content = $content;
394  $out->totalCount = count($content);
395  }
396  return $out;
397  }
398  /**
399  * insert a document into folder
400  * @param int $docid the document identificator to insert to
401  * @return object with error or message field
402  */
403  public function insertDocument($docid)
404  {
405  $out = '';
406  if ($this->docisset()) {
407  $err = $this->doc->addFile($docid);
408  if ($err != "") {
409  $this->setError($err);
410  $out->error = $err;
411  } else {
412  $out->message = sprintf(_("document %d inserted") , $docid);
413  }
414  } else $out->error = sprintf(_("document not set"));
415  return $out;
416  }
417  /**
418  * unlink a document from folder
419  * @param int $docid the document identificator to unlink
420  * @return object with error or message field
421  */
423  {
424  $out = '';
425  if ($this->docisset()) {
426  $err = $this->doc->delFile($docid);
427  if ($err != "") {
428  $this->setError($err);
429  $out->error = $err;
430  } else {
431  $out->message = sprintf(_("document %d deleted") , $docid);
432  }
433  } else $out->error = sprintf(_("document not set"));
434  return $out;
435  }
436  /**
437  * unlink several documents from folder
438  * @param object $selection selection of document
439  * @return object with error or message field
440  */
441  function unlinkDocuments($selection)
442  {
443  $out = '';
444  include_once ("DATA/Class.DocumentSelection.php");
445  $os = new Fdl_DocumentSelection($selection);
446  $ids = $os->getIdentificators();
447 
448  if ($this->docisset()) {
449  $out->notunlinked = array();
450  $out->unlinked = array();
451  $err = $this->doc->canModify();
452  if ($err != "") {
453  $out->error = $err;
454  } else {
455  foreach ($ids as $docid) {
456  $err = $this->doc->delFile($docid);
457  if ($err != "") {
458  $out->notunlinked[$docid] = $err;
459  } else {
460  $out->unlinked[$docid] = sprintf(_("document %d unlinked") , $docid) . "\n";
461  }
462  }
463  $out->unlinkedCount = count($out->unlinked);
464  $out->notUnlinkedCount = count($out->notunlinked);
465  }
466  } else $out->error = sprintf(_("document not set"));
467  return $out;
468  }
469  /**
470  * unlink all documents from folder
471  * @param object $selection selection of document
472  * @return object with error or message field
473  */
475  {
476  $out = '';
477  if ($this->docisset()) {
478  $out->error = "";
479  $err = $this->doc->canModify();
480  if ($err != "") {
481  $out->error = $err;
482  } else {
483  $out->error = $this->doc->clear();
484  }
485  } else $out->error = sprintf(_("document not set"));
486  return $out;
487  }
488  /**
489  * move several documents from folder
490  * @param object $selection selection of document
491  * @return object with error or message field
492  */
493  function moveDocuments($selection, $targetId)
494  {
495  $out = '';
496  include_once ("DATA/Class.DocumentSelection.php");
497  $os = new Fdl_DocumentSelection($selection);
498  $ids = $os->getIdentificators();
499 
500  if ($this->docisset()) {
501  $out->notmoved = array();
502  $out->moved = array();
503  $err = $this->doc->canModify();
504  if ($err == "") {
505  $targetDoc = new_doc($this->dbaccess, $targetId);
506  if ($targetDoc->isAlive()) {
507  if ($targetDoc->defDoctype != 'D') $err = sprintf(_("target folder [%s] is not a folder") , $targetDoc->getTitle());
508  else {
509  $err = $targetDoc->canModify();
510  }
511  } else {
512  $err = sprintf(_("target folder [%s] is not set") , $targetId);
513  }
514  if ($err != "") {
515  $out->error = $err;
516  } else {
517  foreach ($ids as $docid) {
518  $err = $this->doc->moveDocument($docid, $targetDoc->initid);
519  if ($err != "") {
520  $out->notmoved[$docid] = $err;
521  } else {
522  $out->moved[$docid] = sprintf(_("document %d moved") , $docid) . "\n";
523  }
524  }
525  $out->movedCount = count($out->moved);
526  $out->notMovedCount = count($out->notmoved);
527  }
528  } else {
529  $out->error = $err;
530  }
531  } else $out->error = sprintf(_("document not set"));
532  return $out;
533  }
534  /**
535  * insert several documents to folder
536  * @param object $selection selection of document
537  * @return object with error or message field
538  */
539  function insertDocuments($selection)
540  {
541  $out = '';
542  include_once ("DATA/Class.DocumentSelection.php");
543  $os = new Fdl_DocumentSelection($selection);
544  if ($this->docisset()) {
545  $tdocs = $os->getRawDocuments();
546  $out->notinserted = array();
547  $out->inserted = array();
548  $err = $this->doc->insertMDoc($tdocs, "latest", false, $out->inserted, $out->notinserted);
549  $out->insertedCount = count($out->inserted);
550  $out->notInsertedCount = count($out->notinserted);
551  $out->error = $err;
552  } else $out->error = sprintf(_("document not set"));
553  return $out;
554  }
555 
557  {
558  if ($this->docisset()) {
559  if (method_exists($this->doc, "getAuthorizedFamilies")) {
560 
561  return array(
562  "restriction" => $this->doc->hasNoRestriction() ? false : true,
563  "families" => $this->doc->getAuthorizedFamilies()
564  );
565  }
566  }
567  return null;
568  }
569 }
570 ?>
← centre documentaire © anakeen - published under CC License - Dynacase