Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.DocRel.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Document Relation Class
8  *
9  * @author Anakeen
10  * @version $Id: Class.DocRel.php,v 1.13 2008/12/03 13:55:14 eric Exp $
11  * @package FDL
12  */
13 /**
14  */
15 
16 include_once ("Class.DbObj.php");
17 class DocRel extends DbObj
18 {
19  public $fields = array(
20  "sinitid", // source id
21  "cinitid", // cible id
22  "ctitle", // title of cible
23  "cicon", // icon of cible
24  "stitle", // title of source
25  "sicon", // icon of source
26  "type", // relation kind
27  "doctype"
28  );
29  /**
30  * identifier of the source document
31  * @public int
32  */
33  public $sinitid;
34  /**
35  * identifier of the cible document
36  * @public int
37  */
38  public $cinitid;
39  /**
40  * title of the cible document
41  * @public int
42  */
43  public $title;
44  /**
45  * relation kind
46  * @public int
47  */
48  public $type;
49 
50  public $id_fields = array(
51  "sinitid"
52  );
53 
54  public $dbtable = "docrel";
55  public $ctitle;
56  public $cicon;
57  public $stitle;
58  public $sicon;
59  public $doctype;
60 
61  public $sqlcreate = "
62 create table docrel ( sinitid int not null,
63  cinitid int not null,
64  stitle text,
65  ctitle text,
66  sicon text,
67  cicon text,
68  type text,
69  doctype text );
70 create index i_docrelc on docrel(cinitid);
71 create index i_docrels on docrel(sinitid);
72 create unique index docrel_u on docrel(sinitid,cinitid,type);
73 ";
74 
75  public function getRelations($reltype = "", $doctype = "", $limit = 0)
76  {
77  global $action;
78  include_once ("Class.QueryDb.php");
79  if (empty($this->sinitid)) {
80  return array();
81  }
82  $q = new QueryDb($this->dbaccess, get_class($this));
83  $q->AddQuery(sprintf("sinitid = %d", $this->sinitid));
84  if ($reltype != "") $q->AddQuery("type='$reltype'");
85  if ($doctype != "") $q->AddQuery("doctype='$doctype'");
86  $userid = $action->user->id;
87  // if ($userid!=1) $q->AddQuery("(profid <= 0 or hasviewprivilege($userid, profid))");
88  $l = $q->Query(0, $limit, "TABLE");
89  if (is_array($l)) return $l;
90  return array();
91  }
92  public function getIRelations($reltype = "", $doctype = "", $limit = 0)
93  {
94  include_once ("Class.QueryDb.php");
95  $q = new QueryDb($this->dbaccess, get_class($this));
96  $q->AddQuery("cinitid=" . $this->sinitid);
97  if ($reltype != "") $q->AddQuery("type='$reltype'");
98  if ($doctype != "") $q->AddQuery("doctype='$doctype'");
99  $l = $q->Query(0, $limit, "TABLE");
100  if (is_array($l)) return $l;
101  return array();
102  }
103  /**
104  * Delete document relations
105  * @param string $type a special type of relation . Empty means all relations
106  * @param int $sinitid document identifier of relation (initid)
107  * @return void
108  */
109  public function resetRelations($type = "", $sinitid = 0)
110  {
111  if ($sinitid == 0) $sinitid = $this->sinitid;
112  if ($sinitid > 0) {
113  if ($type != "") $this->exec_query("delete from docrel where sinitid=" . $sinitid . " and type='$type'");
114  else $this->exec_query("delete from docrel where sinitid=" . $sinitid . " and type != 'folder'");
115  }
116  }
117  /**
118  * Update document relations
119  * @param Doc &$doc document to initialize relations
120  * @param bool $force if force recomputing
121  * @return void
122  */
123  function initRelations(&$doc, $force = false)
124  {
125  $nattr = $doc->GetNormalAttributes();
126 
127  $savePoint = uniqid("dcp:initrelation");
128  $this->savePoint($savePoint);
129  $this->lockPoint($doc->initid, "IREL"); // need to avoid conflict in docrel index
130  foreach ($nattr as $k => $v) {
131  if (isset($doc->$k) && ($v->type == "docid" || $v->type == "account")) {
132 
133  if (!$force) {
134  if ($doc->getOldRawValue($v->id) === false) {
135  continue;
136  }
137  }
138  // reset old relations
139  pg_query($this->dbid, sprintf("delete from docrel where sinitid=%d and type='%s'", $doc->initid, pg_escape_string($v->id)));
140  if ($v->inArray()) $tv = array_unique($doc->getMultipleRawValues($v->id));
141  else $tv = array(
142  $doc->$k
143  );
144  $tvrel = array();
145  foreach ($tv as $relid) {
146  if (strpos($relid, '<BR>') !== false) {
147  $tt = explode('<BR>', $relid);
148  foreach ($tt as $brelid) {
149  if (is_numeric($brelid)) $tvrel[] = intval($brelid);
150  }
151  } elseif (is_numeric($relid)) {
152  $tvrel[] = intval($relid);
153  }
154  }
155  $tvrel = array_unique($tvrel);
156  $this->copyRelations($tvrel, $doc, $v->id);
157  }
158  }
159  $this->commitPoint($savePoint);
160  }
161  /**
162  * copy in db document relations
163  * @param array &$tv array of docid
164  * @param Doc &$doc document source
165  * @return void
166  */
167  function copyRelations(&$tv, &$doc, $reltype)
168  {
169  $tv = array_filter($tv, function ($a)
170  {
171  return (!empty($a));
172  });
173  if (count($tv) > 0) {
174  // increase speed using pg_copy
175  $sql = sprintf("select distinct on (initid, icon, title) initid, icon, title from docread where initid in (SELECT initid from docread where %s) and locked != -1;", getsqlcond($tv, 'id', true));
176 
177  $t = $this->exec_query($sql);
178  if ($this->numrows() > 0) {
179  $c = 0;
180  $tin = array();
181  while ($row = @pg_fetch_array($this->res, $c, PGSQL_ASSOC)) {
182  $tin[] = sprintf("%d\t%d\t%s\t%s\t%s\t%s\t%s\t%s", $doc->initid, $row["initid"], $this->escapePgCopyChars($doc->title) , $this->escapePgCopyChars($row["title"]) , $doc->icon, $row["icon"], $reltype, $doc->doctype);
183  $c++;
184  }
185 
186  pg_copy_from($this->dbid, "docrel", $tin);
187  }
188  }
189  }
190 
191  protected function escapePgCopyChars($str)
192  {
193  /* Escape literal backslash chars */
194  $str = str_replace("\\", "\\\\", $str);
195  /* These characters should also be escaped, but for compatibility reasons we will simply replace them with a space char */
196  $str = str_replace("\r", " ", $str);
197  $str = str_replace("\n", " ", $str);
198  $str = str_replace("\t", " ", $str);
199  return $str;
200  }
201 }
global $action
print< H1 > Check Database< i > $dbaccess</i ></H1 > $a
Definition: checklist.php:45
exec_query($sql, $lvl=0, $prepare=false)
savePoint($point)
initRelations(&$doc, $force=false)
resetRelations($type="", $sinitid=0)
lockPoint($exclusiveLock, $exclusiveLockPrefix= '')
escapePgCopyChars($str)
$force
commitPoint($point)
getIRelations($reltype="", $doctype="", $limit=0)
getRelations($reltype="", $doctype="", $limit=0)
copyRelations(&$tv, &$doc, $reltype)
← centre documentaire © anakeen