Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
freedom_util.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Function Utilities for freedom
8  *
9  * @author Anakeen
10  * @version $Id: freedom_util.php,v 1.119 2009/01/20 14:30:39 eric Exp $
11  * @package FDL
12  * @subpackage
13  */
14 /**
15  */
16 
17 include_once ("FDL/Lib.Util.php");
18 //
19 // ------------------------------------------------------
20 // construction of a sql disjonction
21 // ------------------------------------------------------
22 function GetSqlCond2($Table, $column)
23 // ------------------------------------------------------
24 
25 {
26  $sql_cond = "";
27  if (count($Table) > 0) {
28  $sql_cond = "(($column = '$Table[0]') ";
29  for ($i = 1; $i < count($Table); $i++) {
30  $sql_cond = $sql_cond . "OR ($column = '$Table[$i]') ";
31  }
32  $sql_cond = $sql_cond . ")";
33  }
34 
35  return $sql_cond;
36 }
37 
38 function GetSqlCond($Table, $column, $integer = false)
39 // ------------------------------------------------------
40 
41 {
42  $sql_cond = "";
43  if (count($Table) > 0) {
44  if ($integer) { // for integer type
45  $sql_cond = "$column in (";
46  $sql_cond.= implode(",", $Table);
47  $sql_cond.= ")";
48  } else { // for text type
49  foreach ($Table as & $v) $v = pg_escape_string($v);
50  $sql_cond = "$column in ('";
51  $sql_cond.= implode("','", $Table);
52  $sql_cond.= "')";
53  }
54  }
55 
56  return $sql_cond;
57 }
58 /**
59  * return first element of array
60  * @param array $a
61  * @return string the first, false is empty
62  */
63 function first($a)
64 {
65  if (count($a) == 0) return false;
66  reset($a);
67  return current($a);
68 }
69 
70 function notEmpty($a)
71 {
72  return (!empty($a));
73 }
74 /**
75  * function use by Doc::getOOoValue()
76  * use to trap XML parsing error : raise exception
77  * @param int $errno error number
78  * @param string $errstr error message
79  * @param string $errfile
80  * @param string $errline error line
81  * @return bool
82  */
83 function HandleXmlError($errno, $errstr, $errfile, $errline)
84 {
85  if ($errno == E_WARNING && (substr_count($errstr, "DOMDocument::loadXML()") > 0)) {
86  throw new DOMException($errstr);
87  } else return false;
88 }
89 /**
90  * clear all cache used by new_doc function
91  * @param int $id document identifier : limit to destroy cache of only this document
92  * @return void
93  */
94 function clearCacheDoc($id = 0)
95 {
96  if ($id == 0) {
98  } else {
100  }
101 }
102 /**
103  * return document object in type concordance
104  * @param string $dbaccess database specification
105  * @param int|string $id identifier of the object
106  * @param bool $latest if true set to latest revision of doc
107  *
108  * @code
109  * $myDoc=new_doc("", $myIdentifier);
110  * if ($myDoc->isAlive()) {
111  * print $myDoc->getTitle();
112  * } else {
113  * printf("%s not found",$myIdentifier);
114  * }
115  * @endcode
116  *
117  * @return Doc object
118  */
119 function new_Doc($dbaccess, $id = '', $latest = false)
120 {
121  if ($dbaccess == "") {
122  // don't test if file exist or must be searched in include_path
124  }
125  // print("doctype:".$res["doctype"]);
126  $classname = "";
127  if (($id == '')) {
128  $doc = new \Dcp\Family\Document($dbaccess);
129 
130  return ($doc);
131  }
132  $fromid = "";
133  $gen = ""; // path GEN or not
134  if (!is_numeric($id)) $id = getIdFromName($dbaccess, $id);
135  elseif ($latest) {
136  $lid = getLatestDocId($dbaccess, $id);
137  if ($lid > 0) {
138  $id = $lid;
139  $latest = false;
140  }
141  }
142  $id = intval($id);
143  if ($id > 0) {
144  $sharedDoc = Dcp\Core\SharedDocuments::get($id);
145  if (isset($sharedDoc) && ((!$latest) || ($sharedDoc->locked != - 1))) {
146  if (($sharedDoc->doctype != 'W') || (!isset($sharedDoc->doc))) {
147  if ($sharedDoc->id == $id) {
148  $sharedDoc->cached = 1;
149  return $sharedDoc;
150  } else {
152  }
153  }
154  }
155 
156  $fromid = getFromId($dbaccess, $id);
157  if ($fromid > 0) {
158  $classname = "Doc$fromid";
159  $gen = getGen($dbaccess);
160  } else if ($fromid == - 1) $classname = "DocFam";
161  }
162 
163  if ($classname != "") {
164  if (!include_once ("FDL$gen/Class.$classname.php")) {
165  AddWarningMsg(sprintf("cannot include %s class", $classname));
166  return null;
167  }
168  /* @var Doc $doc */
169  $doc = new $classname($dbaccess, $id);
170 
171  if ($latest && $doc->locked == - 1) {
172  $tl = getLatestTDoc($dbaccess, $doc->initid);
173  $doc->affect($tl);
174  $id = $doc->id;
175  }
176 
177  if ($id > 0) {
178  if (($doc->doctype != 'C') || (count($doc->attributes->attr) > 0)) {
180  $doc->iscached = 1;
181  }
182  //print_r2("<b>use cache $id /".$doc->id."</b>");
183 
184  }
185  return ($doc);
186  } else {
187  $doc = new \Dcp\Family\Document($dbaccess, $id);
188 
189  return ($doc);
190  }
191 }
192 /**
193  * create a new document object in type concordance
194  *
195  * the document is set with default values and default profil of the family
196  * @param string $dbaccess database specification
197  * @param string $fromid identifier of the family document (the number or internal name)
198  * @param bool $control if false don't control the user hability to create this kind of document
199  * @param bool $defaultvalues if false not affect default values
200  * @param bool $temporary if true create document as temporary doc (use Doc::createTmpDoc instead)
201  * @see createTmpDoc to create temporary/working document
202  * @code
203  * $myDoc=createDoc("", "SOCIETY");
204  * if ($myDoc) {
205  * $myDoc->setValue("si_name", "my company");
206  * $err=$myDoc->store();
207  * }
208  * @endcode
209  * @return Doc may be return false if no hability to create the document
210  */
211 function createDoc($dbaccess, $fromid, $control = true, $defaultvalues = true, $temporary = false)
212 {
213 
214  if (!is_numeric($fromid)) $fromid = getFamIdFromName($dbaccess, $fromid);
215  if ($fromid > 0) {
216  include_once ("FDL/Class.DocFam.php");
217  /*
218  * @var DocFam $cdoc
219  */
220  $cdoc = new_doc($dbaccess, $fromid);
221 
222  if (!$cdoc->isAffected()) return false;
223  if ($control) {
224  $err = $cdoc->control('create');
225  if ($err != "") return false;
226  }
227 
228  $classname = "Doc" . $fromid;
229  $GEN = getGen($dbaccess);
230  include_once ("FDL$GEN/Class.$classname.php");
231  /* @var DocFam $doc */
232  $doc = new $classname($dbaccess);
233 
234  $doc->revision = "0";
235  $doc->doctype = $doc->defDoctype; // it is a new document (not a familly)
236  $doc->cprofid = "0"; // NO CREATION PROFILE ACCESS
237  $doc->fromid = $fromid;
238  if (!$temporary) {
239  $err = $doc->setProfil($cdoc->cprofid); // inherit from its familly
240  $doc->setCvid($cdoc->ccvid); // inherit from its familly
241  $doc->wid = $cdoc->wid;
242  }
243  $doc->icon = $cdoc->icon; // inherit from its familly
244  $doc->usefor = $cdoc->usefor; // inherit from its familly
245  $doc->atags = $cdoc->atags;
246  if ($defaultvalues) $doc->setDefaultValues($cdoc->getDefValues());
247  $doc->ApplyMask();
248  return ($doc);
249  }
250  return new_Doc($dbaccess);
251 }
252 /**
253  * create a temporary document object in type concordance
254  *
255  * the document is set with default values and has no profil
256  * the create privilege is not tested in this case
257  * @param string $dbaccess database specification
258  * @param string $fromid identifier of the family document (the number or internal name)
259  * @param bool $defaultvalue set to false to not set default values
260  * @return Doc may be return false if no hability to create the document
261  */
262 function createTmpDoc($dbaccess, $fromid, $defaultvalue = true)
263 {
264  $d = createDoc($dbaccess, $fromid, false, $defaultvalue, true);
265  if ($d) {
266  $d->doctype = 'T'; // tag has temporary document
267  $d->profid = 0; // no privilege
268 
269  }
270  return $d;
271 }
272 /**
273  * return from id for document (not for family (use @see getFamFromId() instead)
274  * @param string $dbaccess database specification
275  * @param int $id identifier of the object
276  *
277  * @return int false if error occured (return -1 if family document )
278  */
279 function getFromId($dbaccess, $id)
280 {
281  if (!($id > 0)) return false;
282  if (!is_numeric($id)) return false;
283  $dbid = getDbid($dbaccess);
284  $fromid = false;
285 
286  $result = pg_query($dbid, sprintf("select fromid from docfrom where id=%d", $id));
287  if ($result) {
288  if (pg_num_rows($result) > 0) {
289  $arr = pg_fetch_array($result, 0, PGSQL_ASSOC);
290  $fromid = $arr["fromid"];
291  }
292  }
293 
294  return $fromid;
295 }
296 /**
297  * return from name for document (not for family (use @see getFamFromId() instead)
298  * @param string $dbaccess database specification
299  * @param int $id identifier of the object
300  *
301  * @return string false if error occured (return -1 if family document )
302  */
303 function getFromName($dbaccess, $id)
304 {
305 
306  if (!($id > 0)) return false;
307  if (!is_numeric($id)) return false;
308  $dbid = getDbid($dbaccess);
309  $fromname = false;
310  $result = pg_query($dbid, sprintf("SELECT name from docfam where id=(select fromid from docfrom where id=%d)", $id));
311 
312  if (pg_num_rows($result) > 0) {
313  $arr = pg_fetch_array($result, 0, PGSQL_ASSOC);
314  $fromname = $arr["name"];
315  }
316 
317  return $fromname;
318 }
319 /**
320  * return from id for family document
321  * @param string $dbaccess database specification
322  * @param int $id identifier of the object
323  *
324  * @return int false if error occured
325  */
326 function getFamFromId($dbaccess, $id)
327 {
328 
329  if (!($id > 0)) return false;
330  if (!is_numeric($id)) return false;
331  $dbid = getDbid($dbaccess);
332  $fromid = false;
333  $result = pg_query($dbid, "select fromid from docfam where id=$id;");
334 
335  if (pg_num_rows($result) > 0) {
336  $arr = pg_fetch_array($result, 0, PGSQL_ASSOC);
337  $fromid = intval($arr["fromid"]);
338  }
339 
340  return $fromid;
341 }
342 /**
343  * get document title from document identifier
344  * @param int|string $id document identifier
345  * @param bool $latest set to false for a fixed id or true for latest
346  * @return string
347  */
348 function getDocTitle($id, $latest = true)
349 {
351  if (!is_numeric($id)) $id = getIdFromName($dbaccess, $id);
352  if ($id > 0) {
353 
354  if (!$latest) $sql = sprintf("select title, doctype, locked, initid, name from docread where id=%d", $id);
355  else $sql = sprintf("select title, doctype, locked, initid, name from docread where initid=(select initid from docread where id=%d) order by id desc limit 1", $id);
356  simpleQuery($dbaccess, $sql, $t, false, true);
357 
358  if (!$t) return '';
359  if ($t["doctype"] == 'C') return getFamTitle($t);
360  // TODO confidential property
361  return $t["title"];
362  }
363  return '';
364 }
365 /**
366  * get some properties for a document
367  * @param $id
368  * @param bool $latest
369  * @param array $prop properties list to retrieve
370  * @return array|null of indexed properties's values - empty array if not found
371  */
372 function getDocProperties($id, $latest = true, array $prop = array(
373  "title"
374 ))
375 {
377  if (!is_numeric($id)) $id = getIdFromName($dbaccess, $id);
378  if (($id > 0) && count($prop) > 0) {
379  $sProps = implode(',', $prop);
380  if (!$latest) $sql = sprintf("select %s, doctype, locked, initid from docread where id=%d", $sProps, $id);
381  else $sql = sprintf("select %s, doctype, locked, initid from docread where initid=(select initid from docread where id=%d) order by id desc limit 1", $sProps, $id);
382  simpleQuery($dbaccess, $sql, $t, false, true);
383 
384  if (!$t) return null;
385  return $t;
386  }
387  return null;
388 }
389 /**
390  * return document table value
391  * @param string $dbaccess database specification
392  * @param int $id identifier of the object
393  * @param array $sqlfilters add sql supply condition
394  *
395  * @param array $result
396  * @return array false if error occured or if cocument not found
397  */
398 function getTDoc($dbaccess, $id, $sqlfilters = array() , $result = array())
399 {
400  global $action;
401  global $SQLDELAY, $SQLDEBUG;
402 
403  if (!is_numeric($id)) $id = getIdFromName($dbaccess, $id);
404  if (!($id > 0)) return false;
405  $dbid = getDbid($dbaccess);
406  $table = "doc";
407  $fromid = getFromId($dbaccess, $id);
408  if ($fromid > 0) $table = "doc$fromid";
409  else if ($fromid == - 1) $table = "docfam";
410  if ($fromid == 0) return false; // no document can be found
411  $sqlcond = "";
412  if (count($sqlfilters) > 0) $sqlcond = "and (" . implode(") and (", $sqlfilters) . ")";
413  if (count($result) == 0) {
414  $userMemberOf = DocPerm::getMemberOfVector();
415  $sql = sprintf("select *,getaperm('%s',profid) as uperm from only %s where id=%d %s", $userMemberOf, $table, $id, $sqlcond);
416  } else {
417 
418  $scol = implode($result, ",");
419  $sql = "select $scol from only $table where id=$id $sqlcond;";
420  }
421  $sqlt1 = 0;
422  if ($SQLDEBUG) $sqlt1 = microtime(); // to test delay of request
423  $result = pg_query($dbid, $sql);
424  if ($SQLDEBUG) {
425  global $TSQLDELAY;
426  $SQLDELAY+= microtime_diff(microtime() , $sqlt1); // to test delay of request
427  $TSQLDELAY[] = array(
428  "t" => sprintf("%.04f", microtime_diff(microtime() , $sqlt1)) ,
429  "s" => $sql
430  );
431  }
432  if (($result) && (pg_num_rows($result) > 0)) {
433  $arr = pg_fetch_array($result, 0, PGSQL_ASSOC);
434 
435  return $arr;
436  }
437  return false;
438 }
439 /**
440  * return the value of an doc array item
441  *
442  * @param array &$t the array where get value
443  * @param string $k the index of the value
444  * @param string $d default value if not found or if it is empty
445  * @return string
446  */
447 function getv(&$t, $k, $d = "")
448 {
449  if (isset($t[$k]) && ($t[$k] != "")) return $t[$k];
450  if (strpos($t["attrids"], "£$k") !== 0) {
451 
452  $tvalues = explode("£", $t["values"]);
453  $tattrids = explode("£", $t["attrids"]);
454  foreach ($tattrids as $ka => $va) {
455  if ($va != "") {
456  if (!isset($t[$va])) $t[$va] = $tvalues[$ka];
457  if ($va == $k) {
458  if ($tvalues[$ka] != "") return $tvalues[$ka];
459  break;
460  }
461  }
462  }
463  }
464  return $d;
465 }
466 /**
467  * complete all values of an doc array item
468  *
469  * @param array &$t the array where get value
470  * @return string
471  */
472 function getvs(&$t)
473 {
474  $tvalues = explode("£", $t["values"]);
475  $tattrids = explode("£", $t["attrids"]);
476  foreach ($tattrids as $ka => $va) {
477  if ($va != "") {
478  if (!isset($t[$va])) $t[$va] = $tvalues[$ka];
479  }
480  }
481  return $t;
482 }
483 /**
484  * use to usort attributes
485  * @param BasicAttribute $a
486  * @param BasicAttribute $b
487  */
488 function tordered($a, $b)
489 {
490  if (isset($a->ordered) && isset($b->ordered)) {
491  if ($a->ordered == $b->ordered) return 0;
492  if ($a->ordered > $b->ordered) return 1;
493  return -1;
494  }
495  if (isset($a->ordered)) return 1;
496  if (isset($b->ordered)) return -1;
497  return 0;
498 }
499 
500 function cmp_cvorder3($a, $b)
501 {
502  if ($a["cv_order"] == $b["cv_order"]) {
503  return 0;
504  }
505  return ($a["cv_order"] < $b["cv_order"]) ? -1 : 1;
506 }
507 /**
508  * control privilege for a document in the array form
509  * the array must provide from getTdoc
510  * the function is equivalent of Doc::Control
511  * @param array $tdoc document
512  * @param string $aclname identifier of the privilege to test
513  * @return bool true if current user has privilege
514  */
515 function controlTdoc(&$tdoc, $aclname)
516 {
517  global $action;
518  static $_ODocCtrol = false;
519  static $_memberOf = false; // current user
520  if (!$_ODocCtrol) {
521  $cd = new DocCtrl();
522  $_ODocCtrol = $cd;
523  $_memberOf = DocPerm::getMemberOfVector();
524  }
525 
526  if (($tdoc["profid"] <= 0) || ($action->user->id == 1)) return true;
527  if (!isset($tdoc["uperm"])) {
528  $sql = sprintf("select getaperm('%s',%d) as uperm", $_memberOf, $tdoc['profid']);
529  $err = simpleQuery($action->dbaccess, $sql, $uperm, true, true);
530  if (!$err) $tdoc["uperm"] = $uperm;
531  }
532  $err = $_ODocCtrol->ControlUp($tdoc["uperm"], $aclname);
533 
534  return ($err == "");
535 }
536 /**
537  * get document object from array document values
538  * @param string $dbaccess database specification
539  * @param array $v values of document
540  * @return Doc the document object
541  */
542 function getDocObject($dbaccess, $v, $k = 0)
543 {
544  /* @var Doc[][] $_OgetDocObject */
545  static $_OgetDocObject;
546 
547  if ($v["doctype"] == "C") {
548  if (!isset($_OgetDocObject[$k]["family"])) $_OgetDocObject[$k]["family"] = new DocFam($dbaccess);
549  $_OgetDocObject[$k]["family"]->Affect($v, true);
550  $v["fromid"] = "family";
551  } else {
552  if (!isset($_OgetDocObject[$k][$v["fromid"]])) $_OgetDocObject[$k][$v["fromid"]] = createDoc($dbaccess, $v["fromid"], false, false);
553  }
554 
555  $_OgetDocObject[$k][$v["fromid"]]->Affect($v, true);
556 
557  return $_OgetDocObject[$k][$v["fromid"]];
558 }
559 /**
560  * return the next document in sql select ressources
561  * use with "ITEM" type searches direct in QueryDb
562  * return Doc the next doc (false if the end)
563  */
564 function getNextDbObject($dbaccess, $res)
565 {
566  $tdoc = pg_fetch_array($res, NULL, PGSQL_ASSOC);
567  if ($tdoc === false) return false;
568  return getDocObject($dbaccess, $tdoc, intval($res));
569 }
570 /**
571  * return the next document in sql select ressources
572  * use with "ITEM" type searches with getChildDoc
573  * return Doc the next doc (false if the end)
574  */
575 function getNextDoc($dbaccess, &$tres)
576 {
577  $n = current($tres);
578  if ($n === false) return false;
579  $tdoc = pg_fetch_array($n, NULL, PGSQL_ASSOC);
580  if ($tdoc === false) {
581  $n = next($tres);
582  if ($n === false) return false;
583  $tdoc = pg_fetch_array($n, NULL, PGSQL_ASSOC);
584  if ($tdoc === false) return false;
585  }
586  return getDocObject($dbaccess, $tdoc, intval(current($tres)));
587 }
588 /**
589  * count returned document in sql select ressources
590  * @param array $tres of ressources
591  * return Doc the next doc (false if the end)
592  */
593 function countDocs(&$tres)
594 {
595  $n = 0;
596  foreach ($tres as $res) $n+= pg_num_rows($res);
597  reset($tres);
598  return $n;
599 }
600 /**
601  * return the identifier of a family from internal name
602  *
603  * @param string $dbaccess database specification
604  * @param string $name internal family name
605  * @return int 0 if not found
606  */
607 function getFamIdFromName($dbaccess, $name)
608 {
609  include_once ("FDL/Class.DocFam.php");
610  global $tFamIdName;
611  if (!isset($tFamIdName)) {
612  $tFamIdName = array();
613  $q = new QueryDb($dbaccess, "DocFam");
614  $ql = $q->Query(0, 0, "TABLE");
615  foreach ($ql as $k => $v) {
616  if ($v["name"] != "") $tFamIdName[$v["name"]] = $v["id"];
617  }
618  }
619  if (isset($tFamIdName[$name])) {
620  return $tFamIdName[$name];
621  }
622  if (isset($tFamIdName[strtoupper($name) ])) {
623  return $tFamIdName[strtoupper($name) ];
624  }
625  $name = strtolower($name);
626  foreach ($tFamIdName as $famName => $famId) {
627  if (strtolower($famName) === $name) {
628  return $famId;
629  }
630  }
631  return 0;
632 }
633 /**
634  * return the identifier of a document from a search with title
635  *
636  * @param string $dbaccess database specification
637  * @param string $name logical name
638  * @param string $famid must be set to increase speed search
639  * @param boolean $only set to true to not search in subfamilies
640  * @return int 0 if not found, return negative first id found if multiple (name must be unique)
641  */
642 function getIdFromTitle($dbaccess, $title, $famid = "", $only = false)
643 {
644  if ($famid && (!is_numeric($famid))) $famid = getFamIdFromName($dbaccess, $famid);
645  if ($famid > 0) {
646  $fromonly = ($only) ? "only" : "";
647  $err = simpleQuery($dbaccess, sprintf("select id from $fromonly doc%d where title='%s' and locked != -1", $famid, pg_escape_string($title)) , $id, true, true);
648  } else {
649  $err = simpleQuery($dbaccess, sprintf("select id from docread where title='%s' and locked != -1", pg_escape_string($title)) , $id, true, true);
650  }
651 
652  return $id;
653 }
654 /**
655  * return the latest identifier of a document from its logical name
656  *
657  * @param string $dbaccess database specification
658  * @param string $name logical name
659  * @return int|false return numeric id, false if not found, if revision (name must be unique) return the latest id
660  */
661 function getIdFromName($dbaccess, $name)
662 {
663  static $first = true;
664 
665  $name = trim($name);
666  if (!$name || strpos($name, "\n") !== false) {
667  return false;
668  }
669  $dbid = getDbid($dbaccess);
670  $id = false;
671 
672  if ($first) {
673  @pg_prepare($dbid, "getidfromname", 'select id from docname where name=$1');
674  $first = false;
675  }
676  // $result = pg_query($dbid,"select id from docname where name='$name';");
677  $result = pg_execute($dbid, "getidfromname", array(
678  $name
679  ));
680  $n = pg_num_rows($result);
681  if ($n > 0) {
682  $arr = pg_fetch_array($result, ($n - 1) , PGSQL_ASSOC);
683  $id = $arr["id"];
684  }
685  return $id;
686 }
687 /**
688  * return the initial identifier of a document from its logical name
689  * @param string $name
690  * @return int
691  */
692 function getInitidFromName($name)
693 {
694  simpleQuery(getDbAccess() , sprintf("select initid from docread, docname where docread.id=docname.id and docname.name= '%s';", pg_escape_string($name)) , $initid, true, true);
695  return $initid;
696 }
697 /**
698  * return the logical name of a document from its initial identifier
699  *
700  * @param string $dbaccess database specification
701  * @param string $id initial identifier
702  *
703  * @return string empty if not found
704  */
705 function getNameFromId($dbaccess, $id)
706 {
707  static $first = true;
708  $dbid = getDbid($dbaccess);
709  $id = intval($id);
710  $name = '';
711  // $result = pg_query($dbid,"select name from docname where id=$id;");
712  if ($first) {
713  @pg_prepare($dbid, "getNameFromId", 'select name from docread where id=$1');
714  $first = false;
715  }
716  $result = pg_execute($dbid, "getNameFromId", array(
717  $id
718  ));
719  $n = pg_num_rows($result);
720  if ($n > 0) {
721  $arr = pg_fetch_array($result, ($n - 1) , PGSQL_ASSOC);
722  $name = $arr["name"];
723  }
724  return $name;
725 }
727 {
728 
729  global $tFamIdName;
730 
731  if (!isset($tFamIdName)) getFamIdFromName($action->dbaccess, "-");
732 
733  reset($tFamIdName);
734  foreach ($tFamIdName as $k => $v) {
735  $action->lay->set("IDFAM_$k", $v);
736  }
737 }
738 /**
739  * return freedom user document in concordance with what user id
740  * @param string $dbaccess database specification
741  * @param int $userid what user identifier
742  * @return Doc the user document
743  */
744 function getDocFromUserId($dbaccess, $userid)
745 {
746  if ($userid == "") return false;
747  include_once ("FDL/Lib.Dir.php");
748  $tdoc = array();
749  $user = new Account("", $userid);
750  if (!$user->isAffected()) return false;
751  if ($user->accounttype == Account::GROUP_TYPE) {
752  $filter = array(
753  "us_whatid = '$userid'"
754  );
755  $tdoc = internalGetDocCollection($dbaccess, 0, 0, "ALL", $filter, 1, "LIST", getFamIdFromName($dbaccess, "IGROUP"));
756  } else {
757  $filter = array(
758  "us_whatid = '$userid'"
759  );
760  $tdoc = internalGetDocCollection($dbaccess, 0, 0, "ALL", $filter, 1, "LIST", getFamIdFromName($dbaccess, "IUSER"));
761  }
762  if (count($tdoc) == 0) return false;
763  return $tdoc[0];
764 }
765 
766 function getFamTitle(&$tdoc)
767 {
768  $r = $tdoc["name"] . '#title';
769  $i = _($r);
770  if ($i != $r) return $i;
771  return $tdoc['title'];
772 }
773 /**
774  * verify in database if document is fixed
775  * @return bool
776  */
777 function isFixedDoc($dbaccess, $id)
778 {
779  $tdoc = getTDoc($dbaccess, $id, array() , array(
780  "locked"
781  ));
782  if (!$tdoc) return null;
783  return ($tdoc["locked"] == - 1);
784 }
785 /**
786  * lock oldest alive revision in case of conflict
787  * @param Doc $doc
788  */
790 {
791  if ($doc->id && $doc->fromid > 0) {
792  simpleQuery($doc->dbaccess, sprintf("select id from only doc%d where initid=%d and locked != -1 order by id", $doc->fromid, $doc->initid) , $r);
793  array_pop($r); // last stay alive
794  if (count($r) > 0) {
795  $rid = array();
796  foreach ($r as $docInfo) {
797  simpleQuery($doc->dbaccess, sprintf("update doc set locked= -1 where id=%d", $docInfo["id"]));
798  $rid[] = $docInfo["id"];
799  if ($docInfo["id"] == $doc->id) {
800  $doc->locked = - 1;
801  }
802  }
803  $doc->addHistoryEntry(sprintf(_("Fix multiple alive document #%s") , implode(', ', $rid)) , DocHisto::WARNING);
804  addWarningMsg(sprintf(_("Fix multiple alive revision for \"%s\"") , $doc->getTitle()));
805  global $action;
806  $action->log->warning(sprintf(_("Fix multiple alive document for \"%s\" #%s") , $doc->getTitle() , implode(', ', $rid)));
807  }
808  }
809 }
810 
811 function ComputeVisibility($vis, $fvis, $ffvis = '')
812 {
813  if ($vis == "I") return $vis;
814  if ($fvis == "H") return $fvis;
815  if (($fvis == "R") && (($vis == "W") || ($vis == "U") || ($vis == "S"))) return $fvis;
816  if (($fvis == "R") && ($vis == "O")) return "H";
817  if (($fvis == "O") && ($vis == "W")) return $fvis;
818  if (($fvis == "S") && (($vis == "W") || ($vis == "O"))) return $fvis;
819  if ($fvis == "I") return $fvis;
820  if ($fvis == 'U') {
821  if ($ffvis && ($vis == 'W' || $vis == 'O' || $vis == 'S')) {
822  if ($ffvis == 'S') return 'S';
823  if ($ffvis == 'R') return 'R';
824  }
825  }
826 
827  return $vis;
828 }
829 /**
830  * return doc array of latest revision of initid
831  *
832  * @param string $dbaccess database specification
833  * @param string $initid initial identifier of the document
834  * @param array $sqlfilters add sql supply condition
835  * @return array values array if found. False if initid not avalaible
836  */
837 function getLatestTDoc($dbaccess, $initid, $sqlfilters = array() , $fromid = false)
838 {
839  global $action;
840 
841  if (!($initid > 0)) return false;
843  $table = "doc";
844  if (!$fromid) {
845  simpleQuery($dbaccess, sprintf("select fromid from docread where initid=%d order by id desc limit 1", $initid) , $tf, true);
846  if (count($tf) > 0) {
847  $fromid = $tf[0];
848  }
849  }
850  if ($fromid > 0) $table = "doc$fromid";
851  else if ($fromid == - 1) $table = "docfam";
852 
853  $sqlcond = "";
854  if (count($sqlfilters) > 0) $sqlcond = "and (" . implode(") and (", $sqlfilters) . ")";
855 
856  $userid = $action->user->id;
857  if ($userid) {
858  $userMember = DocPerm::getMemberOfVector();
859  $sql = sprintf("select *,getaperm('%s',profid) as uperm from only %s where initid=%d and doctype != 'T' and locked != -1 %s", $userMember, $table, $initid, $sqlcond);
860  simpleQuery($dbaccess, $sql, $result);
861  if (!$result) {
862  // zombie doc ?
863  $sql = sprintf("select *,getaperm('%s',profid) as uperm from only %s where initid=%d and doctype != 'T' %s order by id desc limit 1", $userMember, $table, $initid, $sqlcond);
864  simpleQuery($dbaccess, $sql, $result);
865  }
866 
867  if ($result && (count($result) > 0)) {
868  if (count($result) > 1) addWarningMsg(sprintf("document %d : multiple alive revision", $initid));
869 
870  $arr = $result[0];
871 
872  return $arr;
873  }
874  }
875  return false;
876 }
877 /**
878  * return identificators according to latest revision
879  * the order is not the same as parameters. The key of result containt initial id
880  *
881  * @param string $dbaccess database specification
882  * @param array $ids array of document identificators
883  * @return array identifier relative to latest revision. if one or several documents document not exists the identifier not appear in result so the array count of result can be lesser than parameter
884  */
885 function getLatestDocIds($dbaccess, $ids)
886 {
887  if (!is_array($ids)) return null;
888 
889  $dbid = getDbid($dbaccess);
890  foreach ($ids as $k => $v) $ids[$k] = intval($v);
891  $sids = implode($ids, ",");
892  $sql = sprintf("SELECT id,initid from docread where initid in (SELECT initid from docread where id in (%s)) and locked != -1;", $sids);
893  $result = @pg_query($dbid, $sql);
894  if ($result) {
895  $arr = pg_fetch_all($result);
896  $tlids = array();
897  foreach ($arr as $v) $tlids[$v["initid"]] = $v["id"];
898  return $tlids;
899  }
900  return null;
901 }
902 /**
903  * return latest id of document from its initid or other id
904  *
905  * @param string $dbaccess database specification
906  * @param int $initid document identificator
907  * @return int identifier relative to latest revision. if one or several documents document not exists the identifier not appear in result so the array count of result can be lesser than parameter
908  */
909 function getLatestDocId($dbaccess, $initid)
910 {
911  if (is_array($initid)) return null;
912  // first more quick if alive
913  simpleQuery($dbaccess, sprintf("select id from docread where initid='%d' and locked != -1", $initid) , $id, true, true);
914  if ($id > 0) return $id;
915  // second for zombie document
916  simpleQuery($dbaccess, sprintf("select id from docread where initid='%d' order by id desc limit 1", $initid) , $id, true, true);
917  if ($id > 0) return $id;
918  // it is not really on initid
919  simpleQuery($dbaccess, sprintf("select id from docread where initid=(select initid from docread where id=%d) and locked != -1", $initid) , $id, true, true);
920  if ($id > 0) return $id;
921  return null;
922 }
923 /**
924  * return doc array of specific revision of document initid
925  *
926  * @param string $dbaccess database specification
927  * @param string $initid initial identifier of the document
928  * @param int $rev revision number
929  * @return array values array if found. False if initid not avalaible
930  */
931 function getRevTDoc($dbaccess, $initid, $rev)
932 {
933  global $action;
934 
935  if (!($initid > 0)) return false;
936  $table = "docread";
937  $fromid = getFromId($dbaccess, $initid);
938  $sql = sprintf("select fromid from docread where initid=%d and revision=%d", $initid, $rev);
939  simpleQuery($dbaccess, $sql, $fromid, true, true);
940  if ($fromid > 0) $table = "doc$fromid";
941  else if ($fromid == - 1) $table = "docfam";
942 
943  $userMember = DocPerm::getMemberOfVector();
944  $sql = sprintf("select *,getaperm('%s',profid) as uperm from only %s where initid=%d and revision=%d ", $userMember, $table, $initid, $rev);
945  simpleQuery($dbaccess, $sql, $result, false, true);
946  if ($result) {
947  return $result;
948  }
949  return false;
950 }
951 /**
952  * return really latest revision number
953  * use only for debug mode
954  *
955  * @param string $dbaccess database specification
956  * @param int $initid initial identifier of the document
957  * @param int $fromid family identicator of document
958  * @return int latest revision if found. False if initid not available
959  */
960 function getLatestRevisionNumber($dbaccess, $initid, $fromid = 0)
961 {
962  global $action;
963 
964  $initid = intval($initid);
965  if (!($initid > 0)) return false;
966  $dbid = getDbid($dbaccess);
967  $table = "docread";
968  if ($fromid == - 1) $table = "docfam";
969 
970  $result = @pg_query($dbid, "SELECT revision from $table where initid=$initid order by revision desc limit 1;");
971  if ($result && (pg_num_rows($result) > 0)) {
972  $arr = pg_fetch_array($result, 0, PGSQL_ASSOC);
973  return $arr['revision'];
974  }
975  return false;
976 }
977 /**
978  * Create default folder for a family with default constraint
979  *
980  * @param Doc $Doc the family object document
981  * @return int id of new folder (false if error)
982  */
984 {
985  $dir = createDoc($doc->dbaccess, getFamIdFromName($doc->dbaccess, "DIR"));
986  $err = $dir->Add();
987  if ($err != "") return false;
988  $dir->setValue("BA_TITLE", sprintf(_("root for %s") , $doc->title));
989  $dir->setValue("BA_DESC", _("default folder"));
990  $dir->setValue("FLD_ALLBUT", "1");
991  $dir->setValue("FLD_FAM", $doc->title . "\n" . _("folder") . "\n" . _("search"));
992  $dir->setValue("FLD_FAMIDS", $doc->id . "\n" . getFamIdFromName($doc->dbaccess, "DIR") . "\n" . getFamIdFromName($doc->dbaccess, "SEARCH"));
993  $dir->setValue("FLD_SUBFAM", "yes\nyes\nyes");
994  $dir->Modify();
995  $fldid = $dir->id;
996  return $fldid;
997 }
998 /**
999  * get personal profil
1000  *
1001  * return the profil named "PERSONAL-PROFIL-<$uid>"
1002  * the document return is a folder profil that can be use also for "normal" documents
1003  * @return PDir may be return false if no hability to create the document
1004  */
1005 function getMyProfil($dbaccess, $create = true)
1006 {
1007  global $action;
1008  $uid = $action->user->id;
1009  $pname = sprintf("PERSONAL-PROFIL-%d", $uid);
1010  $p = new_doc($dbaccess, $pname);
1011  if (!$p->isAffected()) {
1012  if ($create) {
1013  $p = createDoc($dbaccess, "PDIR");
1014  if ($p) {
1015  $p->name = $pname;
1016  $p->setValue("ba_title", sprintf(_("Personal profile for %s %s") , $action->user->firstname, $action->user->lastname));
1017  $p->setValue("prf_desc", sprintf(_("Only %s %s can view and edit") , $action->user->firstname, $action->user->lastname));
1018 
1019  $err = $p->Add();
1020  if ($err == "") {
1021  $err = $p->setControl(); //activate the profile
1022  $p->setProfil($p->id);
1023  }
1024  }
1025  } else {
1026  $p = false;
1027  }
1028  }
1029  return $p;
1030 }
1031 /**
1032  * @param DomElement $node
1033  * @return bool
1034  */
1035 function xt_innerXML(&$node)
1036 {
1037  if (!$node) return false;
1038  $document = $node->ownerDocument;
1039  $nodeAsString = $document->saveXML($node);
1040  preg_match('!<.*?>(.*)</.*?>!s', $nodeAsString, $match);
1041  return $match[1];
1042 }
1043 /**
1044  * @param $html
1045  * @return mixed
1046  * @deprecated
1047  */
1048 function cleanhtml($html)
1049 {
1050  $html = preg_replace("/<\/?span[^>]*>/s", "", $html);
1051  $html = preg_replace("/<\/?font[^>]*>/s", "", $html);
1052  $html = preg_replace("/<\/?meta[^>]*>/s", "", $html);
1053  $html = preg_replace("/<style[^>]*>.*?<\/style>/s", "", $html);
1054  $html = preg_replace("/<([^>]*) style=\"[^\"]*\"/s", "<\\1", $html);
1055  $html = preg_replace("/<([^>]*) class=\"[^\"]*\"/s", "<\\1", $html);
1056  return $html;
1057 }
GetSqlCond2($Table, $column)
tordered($a, $b)
static getMemberOfVector($uid=0, $strict=false)
global $SQLDEBUG
Definition: indexq.php:28
getNextDoc($dbaccess, &$tres)
$tdoc
getTDoc($dbaccess, $id, $sqlfilters=array(), $result=array())
global $action
fixMultipleAliveDocument(Doc &$doc)
static set($key, &$item, $force=false)
print< H1 > Check Database< i > $dbaccess</i ></H1 > $a
Definition: checklist.php:45
notEmpty($a)
clearCacheDoc($id=0)
getDocFromUserId($dbaccess, $userid)
getv(&$t, $k, $d="")
addWarningMsg($msg)
Definition: Lib.Common.php:95
controlTdoc(&$tdoc, $aclname)
getIdFromTitle($dbaccess, $title, $famid="", $only=false)
const GROUP_TYPE
global $TSQLDELAY
Definition: indexq.php:29
$d
Definition: dav.php:77
global $SQLDELAY
Definition: indexq.php:28
getLatestTDoc($dbaccess, $initid, $sqlfilters=array(), $fromid=false)
getDocProperties($id, $latest=true, array $prop=array("title"))
getNextDbObject($dbaccess, $res)
getInitidFromName($name)
getMyProfil($dbaccess, $create=true)
cmp_cvorder3($a, $b)
createAutoFolder(&$doc)
countDocs(&$tres)
getDocTitle($id, $latest=true)
isFixedDoc($dbaccess, $id)
getFromName($dbaccess, $id)
getFamTitle(&$tdoc)
getDbid($dbaccess)
Definition: Lib.Common.php:353
createDoc($dbaccess, $fromid, $control=true, $defaultvalues=true, $temporary=false)
microtime_diff($a, $b)
Definition: Lib.Common.php:302
internalGetDocCollection($dbaccess, $dirid, $start="0", $slice="ALL", $sqlfilters=array(), $userid=1, $qtype="LIST", $fromid="", $distinct=false, $orderby="title", $latest=true, $trash="", &$debug=null, $folderRecursiveLevel=2, $join= '',\SearchDoc &$searchDoc=null)
Definition: Lib.Dir.php:428
getFamIdFromName($dbaccess, $name)
getTitle($id="-1", $def="", $latest=false)
Definition: Class.Doc.php:8715
cleanhtml($html)
xt_innerXML(&$node)
getDbAccess()
Definition: Lib.Common.php:368
new_Doc($dbaccess, $id= '', $latest=false)
$dir
Definition: resizeimg.php:144
GetSqlCond($Table, $column, $integer=false)
getvs(&$t)
getFamFromId($dbaccess, $id)
const WARNING
getNameFromId($dbaccess, $id)
getRevTDoc($dbaccess, $initid, $rev)
ComputeVisibility($vis, $fvis, $ffvis= '')
getFromId($dbaccess, $id)
setFamidInLayout(Action &$action)
getLatestDocIds($dbaccess, $ids)
getLatestRevisionNumber($dbaccess, $initid, $fromid=0)
$dbaccess
Definition: checkVault.php:17
getIdFromName($dbaccess, $name)
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
Definition: Lib.Common.php:484
createTmpDoc($dbaccess, $fromid, $defaultvalue=true)
if($file) if($subject==""&&$file) if($subject=="") $err
addHistoryEntry($comment= '', $level=DocHisto::INFO, $code= '', $uid= '')
Definition: Class.Doc.php:4707
getLatestDocId($dbaccess, $initid)
HandleXmlError($errno, $errstr, $errfile, $errline)
$latest
getDocObject($dbaccess, $v, $k=0)
getGen($dbaccess)
Definition: Lib.Util.php:27
first($a)
← centre documentaire © anakeen