Platform  3.1
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  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * Document Relation Class
9  *
10  * @author Anakeen
11  * @version $Id: Class.DocRel.php,v 1.13 2008/12/03 13:55:14 eric 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 ("Class.DbObj.php");
19 class DocRel extends DbObj
20 {
21  public $fields = array(
22  "sinitid", // source id
23  "cinitid", // cible id
24  "ctitle", // title of cible
25  "cicon", // icon of cible
26  "stitle", // title of source
27  "sicon", // icon of source
28  "type", // relation kind
29  "doctype"
30  );
31  /**
32  * identifier of the source document
33  * @public int
34  */
35  public $sinitid;
36  /**
37  * identifier of the cible document
38  * @public int
39  */
40  public $cinitid;
41  /**
42  * title of the cible document
43  * @public int
44  */
45  public $title;
46  /**
47  * relation kind
48  * @public int
49  */
50  public $type;
51 
52  public $id_fields = array(
53  "sinitid"
54  );
55 
56  public $dbtable = "docrel";
57  public $ctitle;
58  public $cicon;
59  public $stitle;
60  public $sicon;
61  public $doctype;
62 
63  public $sqlcreate = "
64 create table docrel ( sinitid int not null,
65  cinitid int not null,
66  stitle text,
67  ctitle text,
68  sicon text,
69  cicon text,
70  type text,
71  doctype text );
72 create index i_docrelc on docrel(cinitid);
73 create index i_docrels on docrel(sinitid);
74 create unique index docrel_u on docrel(sinitid,cinitid,type);
75 ";
76 
77  public function getRelations($reltype = "", $doctype = "", $limit = 0)
78  {
79  global $action;
80  include_once ("Class.QueryDb.php");
81  $q = new QueryDb($this->dbaccess, get_class($this));
82  $q->AddQuery("sinitid=" . $this->sinitid);
83  if ($reltype != "") $q->AddQuery("type='$reltype'");
84  if ($doctype != "") $q->AddQuery("doctype='$doctype'");
85  $userid = $action->user->id;
86  // if ($userid!=1) $q->AddQuery("(profid <= 0 or hasviewprivilege($userid, profid))");
87  $l = $q->Query(0, $limit, "TABLE");
88  if (is_array($l)) return $l;
89  return array();
90  }
91  public function getIRelations($reltype = "", $doctype = "", $limit = 0)
92  {
93  include_once ("Class.QueryDb.php");
94  $q = new QueryDb($this->dbaccess, get_class($this));
95  $q->AddQuery("cinitid=" . $this->sinitid);
96  if ($reltype != "") $q->AddQuery("type='$reltype'");
97  if ($doctype != "") $q->AddQuery("doctype='$doctype'");
98  $l = $q->Query(0, $limit, "TABLE");
99  if (is_array($l)) return $l;
100  return array();
101  }
102  /**
103  * Delete document relations
104  * @param string $type a special type of relation . Empty means all relations
105  * @param int $sinitid document identifier of relation (initid)
106  * @return void
107  */
108  public function resetRelations($type = "", $sinitid = 0)
109  {
110  if ($sinitid == 0) $sinitid = $this->sinitid;
111  if ($sinitid > 0) {
112  if ($type != "") $this->exec_query("delete from docrel where sinitid=" . $sinitid . " and type='$type'");
113  else $this->exec_query("delete from docrel where sinitid=" . $sinitid . " and type != 'folder'");
114  }
115  }
116  /**
117  * Update document relations
118  * @param Doc &$doc document to initialize relations
119  * @param bool $force if force recomputing
120  * @return void
121  */
122  function initRelations(&$doc, $force = false)
123  {
124  $nattr = $doc->GetNormalAttributes();
125 
126  $savePoint = uniqid("initrelation");
127  $this->savePoint($savePoint);
128  pg_query($this->dbid, "lock table docrel in exclusive mode"); // need to avoid conflict in docrel index
129  foreach ($nattr as $k => $v) {
130  if (isset($doc->$k) && ($v->type == "docid" || $v->type == "account")) {
131 
132  if (!$force) {
133  if ($doc->getOldValue($v->id) === false) {
134  continue;
135  }
136  }
137  // reset old relations
138  pg_query($this->dbid, sprintf("delete from docrel where sinitid=%d and type='%s'", $doc->initid, pg_escape_string($v->id)));
139  if ($v->inArray()) $tv = array_unique($doc->getTValue($v->id));
140  else $tv = array(
141  $doc->$k
142  );
143  $tvrel = array();
144  foreach ($tv as $relid) {
145  if (strpos($relid, '<BR>') !== false) {
146  $tt = explode('<BR>', $relid);
147  foreach ($tt as $brelid) {
148  if (is_numeric($brelid)) $tvrel[] = intval($brelid);
149  }
150  } elseif (is_numeric($relid)) {
151  $tvrel[] = intval($relid);
152  }
153  }
154  $tvrel = array_unique($tvrel);
155  $this->copyRelations($tvrel, $doc, $v->id);
156  }
157  }
158  $this->commitPoint($savePoint);
159  }
160  /**
161  * copy in db document relations
162  * @param array &$tv array of docid
163  * @param Doc &$doc document source
164  * @return void
165  */
166  function copyRelations(&$tv, &$doc, $reltype)
167  {
168  $tv = array_filter($tv, function ($a)
169  {
170  return (!empty($a));
171  });
172  if (count($tv) > 0) {
173  // increase speed using pg_copy
174  $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));
175 
176  $t = $this->exec_query($sql);
177  if ($this->numrows() > 0) {
178  $c = 0;
179  $tin = array();
180  while ($row = @pg_fetch_array($this->res, $c, PGSQL_ASSOC)) {
181  $tin[] = sprintf("%d\t%d\t%s\t%s\t%s\t%s\t%s\t%s", $doc->initid, $row["initid"], str_replace("\t", " ", $doc->title) , str_replace("\t", " ", $row["title"]) , $doc->icon, $row["icon"], $reltype, $doc->doctype);
182  $c++;
183  }
184 
185  pg_copy_from($this->dbid, "docrel", $tin);
186  }
187  }
188  }
189 }
190 ?>
← centre documentaire © anakeen - published under CC License - Dynacase