Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.Dir.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  * Folder document definition
9  *
10  * @author Anakeen 2000
11  * @version $Id: Class.Dir.php,v 1.81 2008/09/03 08:35:24 marc Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  */
15 /**
16  */
17 
18 include_once ("FDL/Class.PDir.php");
19 include_once ("FDL/Class.QueryDir.php");
20 /**
21  * default folder to place document
22  */
23 define("UNCLASS_FLD", 10);
24 /**
25  * Folder document Class
26  *
27  */
28 class Dir extends PDir
29 {
30 
31  var $defDoctype = 'D';
32  private $authfam = false;
33  private $norestrict = false;
34 
35  public $eviews = array(
36  "FDL:EDITBODYCARD",
37  "FDL:EDITRESTRICTION"
38  );
39  function Dir($dbaccess = '', $id = '', $res = '', $dbid = 0)
40  {
41  parent::__construct($dbaccess, $id, $res, $dbid);
42  if ($this->fromid == "") $this->fromid = FAM_DIR;
43  }
44  /**
45  * get the home and basket folder
46  * @param bool $create set tu false if no want aute creation
47  */
48  function GetHome($create = true)
49  {
50  global $action;
51 
52  include_once ("FDL/freedom_util.php");
53  include_once ("FDL/Lib.Dir.php");
54  $rq = getChildDoc($this->dbaccess, 0, 0, 1, array(
55  "owner = -" . $this->userid
56  ) , $this->userid, "LIST", "DIR");
57 
58  if (count($rq) > 0) $home = $rq[0];
59  else {
60  if (!$create) return false;
61  $home = createDoc($this->dbaccess, "DIR");
62 
63  if (!$home) $action->exitError(sprintf(_("no privilege to create this kind (%d) of document") , getFamIdFromName($dbaccess, "DIR")));
64 
65  $home->owner = - $this->userid;
66  include_once ("Class.User.php");
67  $user = new User("", $this->userid);
68  $home->title = $user->firstname . " " . $user->lastname;
69  $home->setTitle($home->title);
70  $home->icon = 'fldhome.gif';
71  $home->name = 'FLDHOME_' . $this->getWhatUserId();
72  $home->Add();
73 
74  $privlocked = createDoc($this->dbaccess, "SEARCH");
75  if (!$privlocked) $action->exitError(sprintf(_("no privilege to create this kind (%d) of document") , getFamIdFromName($dbaccess, "SEARCH")));
76 
77  $privlocked->title = (_("locked document of ") . $home->title);
78  $privlocked->Add();
79  $privlocked->AddQuery("select * from doc where (doctype!='Z') and" . " (locked=" . $this->userid . ") ");
80  $home->AddFile($privlocked->id);
81  }
82  // add basket in home
83  if (getParam("FREEDOM_IDBASKET") == "") {
84 
85  $bas = createDoc($this->dbaccess, "BASKET");
86  if (!$bas) $action->exitError(sprintf(_("no privilege to create this kind (%d) of document") , getFamIdFromName($dbaccess, "BASKET")));
87 
88  $query = new QueryDb($this->dbaccess, "_BASKET");
89  $query->AddQuery("owner = " . $this->userid);
90  $rq = $query->Query();
91  if ($query->nb == 0) {
92  $bas->setvalue("ba_title", _("Document basket"));
93  $bas->setvalue("ba_desc", sprintf(_("basket of %s") , $home->title));
94  $home->name = 'FLDBASKET_' + $this->getWhatUserId();
95  $bas->Add();
96  $home->AddFile($bas->id);
97  $basid = $bas->id;
98  } else {
99  $basid = $rq[0]->id;
100  }
101  global $action;
102  $action->parent->param->Set("FREEDOM_IDBASKET", $basid, PARAM_USER . $this->userid, $action->parent->GetIdFromName("FREEDOM"));
103  $action->parent->session->close();
104  }
105 
106  return $home;
107  }
108  /**
109  * clear containt of this folder
110  *
111  * @return string error message, if no error empty string
112  */
113  function Clear()
114  {
115  if ($this->isLocked(true)) return sprintf(_("folder is locked. Cannot containt modification"));
116  // need this privilege
117  $err = $this->Control("modify");
118  if ($err != "") return $err;
119  $this->AddComment(_("Folder cleared"));
120  $this->addLog('clearcontent');
121  $err = $this->exec_query("delete from fld where dirid=" . $this->initid);
122  $this->updateFldRelations();
123  return $err;
124  }
125  /**
126  * virtual method use before insert document in folder
127  * @param int $docid document identificator to insert
128  * @param bool $multiple flag to indicate if the insertion is a part of grouped insertion
129  * @return string error message if not empty the insert will be aborted
130  */
131  function preInsertDoc($docid, $multiple = false)
132  {
133  }
134  /**
135  * virtual method use after insert document in folder
136  * @param int $docid document identificator to insert
137  * @param bool $multiple flag to indicate if the insertion is a part of grouped insertion
138  * @return string error message
139  */
140  function postInsertDoc($docid, $multiple = false)
141  {
142  }
143  /**
144  * virtual method use after insert multiple document in this folder
145  * must be redefined to optimize algorithm
146  * @param array $tdocid array of document identificator to insert
147  * @return string error message
148  */
149  function postMInsertDoc($tdocid)
150  {
151  foreach ($tdocid as $docid) {
152  $this->postInsertDoc($docid);
153  }
154  }
155  /**
156  * virtual method use after unlink document in folder
157  * @param int $docid document identificator to unlink
158  * @param bool $multiple flag to indicate if the insertion is a part of grouped insertion
159  * @return string error message if not empty the insert will be aborted
160  */
161  function preUnlinkDoc($docid, $multiple = false)
162  {
163  }
164  /**
165  * virtual method use after unlink document in folder
166  * @param int $docid document identificator to unlink
167  * @param bool $multiple flag to indicate if the insertion is a part of grouped insertion
168  * @return string error message
169  */
170  function postUnlinkDoc($docid, $multiple = false)
171  {
172  }
173  /**
174  * Test if current user can add or delete document in this folder
175  *
176  * @return string error message, if no error empty string
177  */
178  function canModify()
179  {
180  if ($this->isLocked(true)) return sprintf(_("folder is locked. Cannot containt modification"));
181  // need this privilege
182  $err = $this->Control("modify");
183  return $err;
184  }
185  /**
186  * add a document reference in this folder
187  *
188  * if mode is latest the user always see latest revision
189  * if mode is static the user see the revision which has been inserted
190  * @param int $docid document ident for the insertion
191  * @param string $mode latest|static
192  * @param bool $noprepost if true if the virtuals methods {@link preInsertDoc()} and {@link postInsertDoc()} are not called
193  * @param bool $forcerestrict if true don't test restriction (if have)
194  * @param bool $nocontrol if true no test acl "modify"
195  * @return string error message, if no error empty string
196  */
197  function AddFile($docid, $mode = "latest", $noprepost = false, $forcerestrict = false, $nocontrol = false)
198  {
199  if (!$nocontrol) {
200  $err = $this->canModify();
201  if ($err != "") return $err;
202  }
203 
204  $doc = new_Doc($this->dbaccess, $docid);
205  $qf = new QueryDir($this->dbaccess);
206  switch ($mode) {
207  case "static":
208  $qf->qtype = 'F'; // fixed document
209  $qf->childid = $docid; // initial doc
210  break;
211 
212  case "latest":
213  default:
214  if (!$doc->isAffected()) return sprintf(_("Cannot add in %s folder, doc id (%d) unknown") , $this->title, $docid);
215  $qf->qtype = 'S'; // single user query
216  $qf->childid = $doc->initid; // initial doc
217  break;
218  }
219  $qf->dirid = $this->initid; // the reference folder is the initial id
220  $qf->query = "";
221  if (!$qf->Exists()) {
222  // use pre virtual method
223  if (!$noprepost) $err = $this->preInsertDoc($docid);
224  if ($err != "") return $err;
225  // verify if doc family is autorized
226  if ((!$forcerestrict) && (!$this->isAuthorized($doc->fromid))) return sprintf(_("Cannot add %s in %s folder, restriction set to add this kind of document") , $doc->title, $this->title);
227 
228  $err = $qf->Add();
229  if ($err == "") {
230  AddLogMsg(sprintf(_("Add %s in %s folder") , $doc->title, $this->title));
231  $this->AddComment(sprintf(_("Document %s inserted") , $doc->title));
232  $doc->addComment(sprintf(_("Document inserted in %s folder") , $this->title, HISTO_INFO, "MOVEADD"));
233 
234  $this->addLog('addcontent', array(
235  "insert" => array(
236  "id" => $doc->id,
237  "title" => $doc->title
238  )
239  ));
240  // add default folder privilege to the doc
241  if ($doc->profid == 0) { // only if no privilege yet
242  switch ($doc->defProfFamId) {
243  case FAM_ACCESSDOC:
244  $profid = $this->getValue("FLD_PDOCID", 0);
245  if ($profid > 0) {
246  $doc->setProfil($profid);
247  $err = $doc->modify(true, array(
248  "profid",
249  "dprofid"
250  ) , true);
251  if ($err == "") $doc->addComment(sprintf(_("Change profil to default document profil : %d") , $profid));
252  }
253  break;
254 
255  case FAM_ACCESSDIR:
256  $profid = $this->getValue("FLD_PDIRID", 0);
257  if ($profid > 0) {
258  $doc->setProfil($profid);
259  // copy default privilege if not set
260  if ($doc->getValue("FLD_PDIRID") == "") {
261  $doc->setValue("FLD_PDIRID", $this->getValue("FLD_PDIRID"));
262  $doc->setValue("FLD_PDIR", $this->getValue("FLD_PDIR"));
263  }
264  if ($doc->getValue("FLD_PDOCID") == "") {
265  $doc->setValue("FLD_PDOCID", $this->getValue("FLD_PDOCID"));
266  $doc->setValue("FLD_PDOC", $this->getValue("FLD_PDOC"));
267  }
268  $err = $doc->modify();
269  if ($err == "") $doc->addComment(sprintf(_("Change profil to default subfolder profil : %d") , $profid));
270  }
271  break;
272  }
273  }
274  }
275  if ($doc->prelid == "") {
276  $doc->prelid = $this->initid;
277  $doc->modify(true, array(
278  "prelid"
279  ) , true);
280  }
281 
282  if ($err == "") {
283  global $action;
284  $action->AddActionDone("ADDFILE", $this->initid);
285 
286  $this->updateFldRelations();
287  // use post virtual method
288  if (!$noprepost) $err = $this->postInsertDoc($docid, false);
289  }
290  }
291  return $err;
292  }
293  // --------------------------------------------------------------------
294 
295  /**
296  * insert multiple document reference in this folder
297  *
298  * if mode is latest the user always see latest revision
299  * if mode is static the user see the revision which has been inserted
300  * @param array doc array document for the insertion
301  * @param string $mode latest|static
302  * @param boolean $noprepost not call preInsert and postInsert method (default if false)
303  * @return string error message, if no error empty string
304  */
305  function InsertMDoc($tdocs, $mode = "latest", $noprepost = false, &$tinserted = array() , &$twarning = array())
306  {
307 
308  $err = $this->canModify();
309  if ($err != "") return $err;
310  $tAddeddocids = array();
311  // verify if doc family is autorized
312  $qf = new QueryDir($this->dbaccess);
313  foreach ($tdocs as $k => $tdoc) {
314 
315  if (!$this->isAuthorized($tdoc["fromid"])) {
316  $warn = sprintf(_("Cannot add %s in %s folder, restriction set to add this kind of document") , $tdoc["title"], $this->title);
317  $twarning[$docid] = $warn;
318  } else {
319  switch ($mode) {
320  case "static":
321 
322  $qf->qtype = 'F'; // fixed document
323  $docid = $tdoc["id"];
324  $qf->childid = $tdoc["id"]; // initial doc
325  break;
326 
327  case "latest":
328  default:
329 
330  $qf->qtype = 'S'; // single user query
331  $docid = $tdoc["initid"];
332  $qf->childid = $tdoc["initid"]; // initial doc
333  break;
334  }
335 
336  $err = "";
337  $qf->id = "";
338  $qf->dirid = $this->initid; // the reference folder is the initial id
339  $qf->query = "";
340  // use post virtual method
341  if (!$noprepost) $err = $this->preInsertDoc($tdoc["initid"], true);
342 
343  if ($err == "") {
344  $err = $qf->Add();
345  if ($err == "") {
346  AddLogMsg(sprintf(_("Add %s in %s folder") , $tdoc["title"], $this->title));
347  $this->AddComment(sprintf(_("Document %s inserted") , $tdoc["title"]) , HISTO_INFO, "MODCONTAIN");
348 
349  $this->addLog('addcontent', array(
350  "insert" => array(
351  "id" => $tdoc["id"],
352  "title" => $tdoc["title"]
353  )
354  ));
355  $tAddeddocids[] = $docid;
356  $tinserted[$docid] = sprintf(_("Document %s inserted") , $tdoc["title"]);
357  // use post virtual method
358  // if (!$noprepost) $err=$this->postInsertDoc($tdoc["initid"],true);
359 
360  }
361  } else {
362  $twarning[$docid] = $err;
363  }
364  }
365  }
366  // use post virtual method
367  if (!$noprepost) {
368  $this->updateFldRelations();
369  $err.= $this->postMInsertDoc($tAddeddocids);
370  }
371 
372  return $err;
373  }
374  /**
375  * insert multiple static document reference in this folder
376  * be carreful : not verify restriction folders
377  * to be use when many include (verification constraint must ne set before by caller)
378  *
379  * @param array $tdocids identificator documents for the insertion
380  * @return string error message, if no error empty string
381  */
382  function QuickInsertMSDocId($tdocids)
383  {
384 
385  $err = $this->canModify();
386  if ($err != "") return $err;
387  $qf = new QueryDir($this->dbaccess);
388  $qf->qtype = 'S'; // single user query
389  $qf->dirid = $this->initid; // the reference folder is the initial id
390  $qf->query = "";
391  foreach ($tdocids as $k => $docid) {
392  $tcopy[$docid]["childid"] = $docid;
393  }
394 
395  $err = $qf->Adds($tcopy, true);
396  $this->updateFldRelations();
397 
398  return $err;
399  }
400  /**
401  * insert all static document which are included in $docid in this folder
402  * be carreful : not verify restriction folders
403  * to be use when many include (verification constraint must ne set before by caller)
404  *
405  * @param int $docid identificator document for the insertion (must be initial id)
406  * @return string error message, if no error empty string
407  */
409  {
410  if (!is_numeric($docid)) return sprintf(_("Dir::insertFolder identificator [%s] must be numeric") , $docid);
411  if ($this->isLocked(true)) return sprintf(_("folder is locked. Cannot containt modification"));
412  // need this privilege
413  $err = $this->Control("modify");
414  if ($err != "") return $err;
415 
416  $err = $this->exec_Query(sprintf("insert INTO fld (select %d,query,childid,qtype from fld where dirid=%d);", $this->initid, $docid));
417 
418  $this->updateFldRelations();
419  return $err;
420  }
421  // --------------------------------------------------------------------
422  function getQids($docid)
423  {
424  // return array of queries id includes in a directory
425  // --------------------------------------------------------------------
426  $tableid = array();
427 
428  $doc = new_Doc($this->dbaccess, $docid);
429  $query = new QueryDb($this->dbaccess, "QueryDir");
430  $query->AddQuery("dirid=" . $this->id);
431  $query->AddQuery("((childid=$docid) and (qtype='F')) OR ((childid={$doc->initid}) and (qtype='S'))");
432  $tableq = $query->Query();
433 
434  if ($query->nb > 0) {
435  while (list($k, $v) = each($tableq)) {
436  $tableid[$k] = $v->id;
437  }
438  unset($tableq);
439  }
440 
441  return ($tableid);
442  }
443  // --------------------------------------------------------------------
444 
445  /**
446  * delete a document reference in this folder
447  *
448  * @param int $docid document ident for the deletion
449  * @param bool $noprepost if true then the virtuals methods {@link preUnlinkDoc()} and {@link postUnlinkDoc()} are not called
450  * @param bool $nocontrol if true no test acl "modify"
451  * @return string error message, if no error empty string
452  */
453  function DelFile($docid, $noprepost = false, $nocontrol = false)
454  {
455  if (!$nocontrol) {
456  $err = $this->canModify();
457  if ($err != "") return $err;
458  }
459  // use pre virtual method
460  if (!$noprepost) $err = $this->preUnlinkDoc($docid);
461  if ($err != "") return $err;
462 
463  $doc = new_Doc($this->dbaccess, $docid);
464  $docid = $doc->initid;
465  //if (count($qids) == 0) $err = sprintf(_("cannot delete link : link not found for doc %d in folder %d"),$docid, $this->initid);
466  if ($err != "") return $err;
467  // search original query
468  $qf = new QueryDir($this->dbaccess, array(
469  $this->initid,
470  $docid
471  ));
472  if (!($qf->isAffected())) $err = sprintf(_("cannot delete link : initial query not found for doc %d in folder %d") , $docid, $this->initid);
473 
474  if ($err != "") return $err;
475 
476  if ($qf->qtype == "M") $err = sprintf(_("cannot delete link for doc %d in folder %d : the document comes from a user query. Delete initial query if you want delete this document") , $docid, $this->initid);
477 
478  if ($err != "") return $err;
479  $qf->Delete();
480 
481  if ($doc->prelid == $this->initid) {
482  $doc->prelid = "";
483  $doc->modify(true, array(
484  "prelid"
485  ) , true);
486  }
487 
488  AddLogMsg(sprintf(_("Delete %d in %s folder") , $docid, $this->title));
489 
490  $this->addLog('delcontent', array(
491  "insert" => array(
492  "id" => $doc->id,
493  "title" => $doc->title
494  )
495  ));
496  $this->AddComment(sprintf(_("Document %s umounted") , $doc->title) , HISTO_INFO, "MODCONTAIN");
497  $doc->addComment(sprintf(_("Document unlinked of %s folder") , $this->title, HISTO_INFO, "MOVEUNLINK"));
498  // use post virtual method
499  if (!$noprepost) {
500  $this->updateFldRelations();
501  $err = $this->postUnlinkDoc($docid);
502  }
503 
504  global $action;
505  $action->AddActionDone("DELFILE", $this->initid);
506 
507  return $err;
508  }
509  /**
510  * move a document from me to a folder
511  * @param integer $docid the document identificator to move
512  * @param integer $movetoid target destination
513  * @return string error message (empty if null)
514  */
515  function moveDocument($docid, $movetoid)
516  {
517  $err = $this->canModify();
518  if ($err == "") {
519  $fromtoid = $this->initid;
520  $da = new_doc($this->dbaccess, $movetoid);
521  if ($da->isAlive()) {
522  if (method_exists($da, "addFile")) {
523  $err = $da->addFile($docid);
524  if ($err == "") {
525  if (($fromtoid) && ($fromtoid != $movetoid)) {
526  if ($this->isAlive()) {
527  if (method_exists($this, "delFile")) {
528  $err = $this->delFile($docid);
529  if ($err == "") {
530  $doc = new_doc($this->dbaccess, $docid, true);
531  if ($doc->isAlive()) {
532  $doc->prelid = $da->initid;
533  $err = $doc->modify(true, array(
534  "prelid"
535  ) , true);
536  }
537  }
538  } else $err = sprintf(_("document %s is not a folder") , $this->getTitle());
539  }
540  } else {
541  if ($err == "") {
542  $doc = new_doc($this->dbaccess, $docid, true);
543  if ($doc->isAlive()) {
544  $doc->prelid = $da->initid;
545  $err = $doc->modify(true, array(
546  "prelid"
547  ) , true);
548  }
549  }
550  }
551  }
552  } else $err = sprintf(_("document %s is not a folder") , $da->getTitle());
553  }
554  }
555  return $err;
556  }
557  // --------------------------------------------------------------------
558  function postModify()
559  {
560  // don't see restriction frame is not needed
561  $allbut = $this->getValue("FLD_ALLBUT");
562  $tfamid = $this->getTValue("FLD_FAMIDS");
563 
564  if (($allbut === "0") && ((count($tfamid) == 0) || ((count($tfamid) == 1) && ($tfamid[0] == 0)))) {
565 
566  $this->deleteValue("FLD_ALLBUT");
567  $this->modify();
568  }
569  }
570  function hasNoRestriction()
571  {
572  if (!$this->authfam) {
573  $this->getAuthorizedFamilies();
574  }
575  return ($this->norestrict);
576  }
577  /**
578  * return families that can be use in insertion
579  * @param int $classid : restrict for same usefor families
580  */
581  function getAuthorizedFamilies($classid = 0, $verifyCreate = false)
582  {
583 
584  if (!$this->authfam) {
585 
586  $tfamid = $this->getTValue("FLD_FAMIDS");
587  $tfam = $this->getTValue("FLD_FAM");
588  $tsubfam = $this->getTValue("FLD_SUBFAM");
589  $allbut = $this->getValue("FLD_ALLBUT");
590 
591  if (($allbut != "1") && ((count($tfamid) == 0) || ((count($tfamid) == 1) && ($tfamid[0] == 0)))) {
592  $this->norestrict = true;
593  return;
594  }
595 
596  $this->norestrict = false;;
597  $tclassdoc = array();
598  if ($allbut != "1") {
599  include_once ("FDL/Lib.Dir.php");
600  $tallfam = GetClassesDoc($this->dbaccess, $this->userid, $classid, "TABLE");
601 
602  foreach ($tallfam as $k => $cdoc) {
603  $tclassdoc[$cdoc["id"]] = $cdoc;
604  // $tclassdoc += $this->GetChildFam($cdoc["id"]);
605 
606  }
607  // suppress undesirable families
608  reset($tfamid);
609  while (list($k, $famid) = each($tfamid)) {
610 
611  unset($tclassdoc[intval($famid) ]);
612  if ($tsubfam[$k] != "yes") {
613  $tnofam = $this->GetChildFam(intval($famid));
614  foreach ($tnofam as $ka => $va) {
615  unset($tclassdoc[intval($ka) ]);
616  }
617  }
618  }
619  } else {
620  //add families
621  foreach ($tfamid as $k => $famid) {
622  $tfdoc = getTDoc($this->dbaccess, $famid);
623  if ($tfdoc && ((!$verifyCreate) || controlTdoc($tfdoc, 'icreate'))) {
624  $tclassdoc[intval($famid) ] = array(
625  "id" => ($tsubfam[$k] == "no") ? (-intval($famid)) : intval($famid) ,
626  "title" => $tfam[$k]
627  );
628  }
629  if ($tsubfam[$k] != "no") $tclassdoc+= $this->GetChildFam(intval($famid));
630  }
631  }
632  $this->authfam = $tclassdoc;
633  }
634  $this->kauthfam = array_keys($this->authfam);
635  return $this->authfam;
636  }
637  /**
638  * return families that can be use in insertion
639  * @param int $classid : restrict for same usefor families
640  */
641  public function isAuthorized($classid)
642  {
643  if (!$this->authfam) {
644  $this->getAuthorizedFamilies();
645  }
646  if ($this->norestrict) return true;
647  if (!$classid) return true;
648 
649  if (isset($this->authfam[$classid])) return true;
650 
651  return false;
652  }
653  /**
654  * return document includes in folder
655  * @param bool $controlview if false all document are returned else only visible for current user document are return
656  * @param array $filter to add list sql filter for selected document
657  * @param int $famid family identificator to restrict search
658  * @param string $qtype type os result TABLE|LIST|ITEM
659  * @return array array of document array
660  */
661  public function getContent($controlview = true, $filter = array() , $famid = "", $qtype = "TABLE", $trash = "")
662  {
663  include_once ("FDL/Lib.Dir.php");
664  if ($controlview) $uid = $this->userid;
665  else $uid = 1;
666  $tdoc = getChildDoc($this->dbaccess, $this->initid, 0, "ALL", $filter, $uid, $qtype, $famid, false, "title", true, $trash);
667  return $tdoc;
668  }
669  /**
670  * update folder relations
671  */
672  public function updateFldRelations()
673  {
674  return; //inhibit folder relation (too slow for great folder)
675  if ($this->doctype == 'T') return;
676  include_once ("FDL/Class.DocRel.php");
677  $nattr = $this->GetNormalAttributes();
678  $or = new DocRel($this->dbaccess);
679  $or->sinitid = $this->initid;
680  $or->resetRelations("folder");
681  $q = new QueryDb($this->dbaccess, "QueryDir");
682  $tv = $q->Query(0, 0, "TABLE", "select childid from fld where dirid=" . $this->initid . " and qtype='S'");
683  if (is_array($tv)) {
684  $tid = array();
685  foreach ($tv as $tq) {
686  $tid[] = $tq["childid"];
687  }
688  $or->copyRelations($tid, $this, 'folder');
689  }
690  }
691  /**
692  * return number of item in the static folder
693  * @param bool $onlyprimary set to true if you wnat only document linked by primary relation
694  * @return int -1 if it is not a static folder
695  */
696  public function count($onlyprimary = false)
697  {
698  if ($onlyprimary) {
699  $tdoc = $this->getPrimaryChild();
700  if ($tdoc) return count($tdoc);
701  } else {
702  $q = new QueryDb($this->dbaccess, "QueryDir");
703  $tv = $q->Query(0, 0, "TABLE", "select childid from fld where dirid=" . $this->initid . " and qtype='S'");
704  if (is_array($tv)) return count($tv);
705  }
706  return -1;
707  }
708  /**
709  * return array of document identificators included in folder
710  * @return array of initial identificators (initid)
711  */
712  public function getContentInitid()
713  {
714  $query = sprintf("select childid from fld where dirid=%d and qtype='S'", $this->initid);
715  $initids = array();
716  $err = simpleQuery($this->dbaccess, $query, $initids, true, false);
717  if ($err == "") return $initids;
718 
719  return array();
720  }
721  /**
722  * get document which primary relation is this folder
723  *
724  *
725  * @return array of doc (array document)
726  */
727  public function getPrimaryChild()
728  {
729  $filter[] = "prelid=" . $this->initid;
730  return $this->getContent(true, $filter);
731  }
732  function Complete()
733  {
734  $this->authfam = false;
735  $this->norestrict = false;
736  }
737  /**
738  * delete all document which primary relation is the folder (recurively)
739  * different of {@see Clear()}
740  * all document are put in the trash (zombie mode)
741  * @return array of possible errors. Empty array means no errors
742  */
743  public function deleteItems()
744  {
745  $filter[] = "prelid=" . $this->initid;
746  $lpdoc = $this->getContent(false, $filter, "", "ITEM");
747 
748  $terr = array();
749  while ($doc = getNextDoc($this->dbaccess, $lpdoc)) {
750  $coulddelete = true;
751  if ($doc->doctype == 'D') {
752  $terr = array_merge($terr, $doc->deleteItems());
753  foreach ($terr as $id => $err) {
754  if ($err != "") $coulddelete = false;
755  }
756  }
757  if ($coulddelete) $terr[$doc->id] = $doc->delete();
758  }
759  $this->AddComment(_("Folder cleared") , HISTO_INFO, "MODCONTAIN");
760  return $terr;
761  }
762  /**
763  * copy (clone) all documents which primary relation is the folder (recurively)
764  * the others documents are just linked
765  * all document are put in $indirid folder id
766  * @param int $indirid the folder where put the copies
767  * @return array of possible errors. Empty array means no errors
768  */
769  public function copyItems($indirid)
770  {
771  $filter = array();
772  $lpdoc = $this->getContent(false, $filter, "", "ITEM");
773 
774  $terr = array();
775  $fld = new_doc($this->dbaccess, $indirid);
776  if ($fld->doctype == 'D') {
777  $err = $fld->control("modify");
778  if ($err == "") {
779  while ($doc = getNextDoc($this->dbaccess, $lpdoc)) {
780  if ($doc->prelid == $this->initid) {
781  // copy
782  $copy = $doc->copy();
783  if (is_object($copy)) {
784  $fld->addFile($copy->initid);
785 
786  if ($doc->doctype == 'D') {
787  $terr = array_merge($terr, $doc->copyItems($copy->id));
788  }
789  }
790  } else {
791  // link
792  $fld->addFile($doc->initid);
793  }
794  }
795  }
796  }
797  return $terr;
798  }
799  /**
800  * delete the folder and its containt
801  * different of {@see Clear()}
802  * all document are put in the trash (zombie mode)
803  * @return string error message, if no error empty string
804  */
805  public function deleteRecursive()
806  {
807  $err = $this->predocdelete(); // test before try recursive deletion
808  if ($err != "") return $err;
809  $coulddelete = true;
810  $terr = $this->deleteItems();
811  $err = "";
812  foreach ($terr as $id => $err1) {
813  if ($err1 != "") {
814  $coulddelete = false;
815  $err.= "\n$err1";
816  }
817  }
818  if ($coulddelete) $err = $this->delete();
819  return $err;
820  }
821  /**
822  * restore all document which primary relation is the folder (recurively)
823  *
824  *
825  * @return int -1 if it is not a static folder
826  */
827  function reviveItems()
828  {
829  $filter[] = "prelid=" . $this->initid;
830  $lpdoc = $this->getContent(true, $filter, "", "ITEM", "only");
831  $terr = array();
832  while ($doc = getNextDoc($this->dbaccess, $lpdoc)) {
833  if ($doc->defDoctype == 'D') $terr = array_merge($terr, $doc->reviveItems());
834  $terr[$doc->id] = $doc->revive();
835  }
836  return $terr;
837  }
838  }
839 ?>
← centre documentaire © anakeen - published under CC License - Dynacase