Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.DocTitle.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Get Title from ids
8  * @class DocTitle
9  *
10  */
11 class DocTitle
12 {
13  private static $viewUserVector = array();
14  private static $relationCache = array();
15  /**
16  * return title of relation
17  * return false if access deny
18  * return null if relation not found
19  * @param int $docid relation documentg id
20  * @param bool $latest true if it is latest
21  * @param Doc $doc document where comes from relation
22  * @param string $docrevOption docrev attribute option
23  * @param array $info more information about document target (revision, initid)
24  * @return bool|null|string
25  */
26  public static function getRelationTitle($docid, $latest = true, Doc $doc, $docrevOption = "", array & $info = array())
27  {
28  $uid = getCurrentUser()->id; // index by uid in case of sudo
29  $isAdmin = ($uid == 1);
30 
31  if ($docid && !is_numeric($docid)) {
33  }
34  if (!$docid) return null;
35  if ($docrevOption === "") {
36  $docrevOption = $latest ? "latest" : "fixed";
37  }
38  $keyCache = intval($docid) . '-' . $docrevOption;
39 
40  if (!isset(self::$relationCache[$uid][$keyCache])) {
41  self::setRelationCache($doc, $uid);
42  }
43 
44  if (isset(self::$relationCache[$uid][$keyCache])) {
45  } else {
46  $keyCache = intval($docid) . '-' . $docrevOption;
47  }
48  if (isset(self::$relationCache[$uid][$keyCache])) {
49  $relCache = self::$relationCache[$uid][$keyCache];
50  if (!isset($relCache["rid"]) || !isset($relCache["title"])) {
51  return null;
52  } // unknow document
53  if ($relCache["canaccess"] === 'f' && (!$isAdmin)) {
54  return false; //_("information access deny");
55 
56  } else {
57  $info = $relCache;
58  return $relCache["title"];
59  }
60  }
61  $title = self::getTitle($docid, $latest, $docrevOption, $info);
62  return $title;
63  }
64  /**
65  * get all title and access of document's relations
66  * @static
67  * @param Doc $doc
68  * @param int $uid user identifier
69  * @return void
70  */
71  protected static function setRelationCache(Doc $doc, $uid)
72  {
73  $la = $doc->getNormalAttributes();
74  $relationIds = array();
75  foreach ($la as $oa) {
76  $type = $oa->type;
77  $docRevOption = $oa->getOption("docrev", "latest");
78  $latest = $docRevOption === "latest";
79  $revState = "";
80  if (preg_match('/^state\(([^\)]+)\)/', $docRevOption, $matches)) {
81  $revState = $matches[1];
82  }
83  if ($type == "docid" || $type == "account" || $type == "thesaurus") {
84  $ids = $doc->getMultipleRawValues($oa->id);
85  $realId = array();
86  foreach ($ids as $rid) {
87  if (is_numeric($rid)) $realId[] = intval($rid);
88  elseif (strpos($rid, '<BR>') !== false) {
89  $tt = explode('<BR>', $rid);
90  foreach ($tt as $brelid) {
91  if (is_numeric($brelid)) $realId[] = intval($brelid);
92  }
93  }
94  }
95  foreach ($realId as $did) {
96  $relationIds[$did . '-' . $docRevOption] = array(
97  "docid" => $did,
98  "rid" => $did,
99  "latest" => $latest,
100  "state" => $revState
101  );
102  }
103  }
104  }
105  $latestId = array();
106  foreach ($relationIds as $k => $relid) {
107  if ($relid["latest"]) $latestId[] = $relid["docid"];
108  elseif ($relid["state"]) {
109 
110  simpleQuery($doc->dbaccess, sprintf("select id from docread where initid=(select initid from docread where id=%d) and state = '%s' and locked = -1 order by id desc limit 1", $relid["docid"], pg_escape_string($relid["state"])) , $stateId, true, true);
111  if ($stateId) {
112  $relationIds[$k]["rid"] = $stateId;
113  }
114  }
115  }
116  if ($latestId) {
117  $sql = sprintf("select id,initid from docread where initid in (%s) and locked != -1", implode(',', $latestId));
118  //$sql = sprintf("select id,initid from docread where initid in (select initid from docread where id in (%s)) and locked != -1", implode(',', $getLatestId));
119  simpleQuery($doc->dbaccess, $sql, $result);
120  $tInitid = array();
121  foreach ($result as $aRow) {
122  $tInitid[$aRow["initid"]] = $aRow["id"];
123  }
124  foreach ($relationIds as $k => $relid) {
125  if ($relid["latest"]) {
126  $relationIds[$k]["rid"] = empty($tInitid[$relid["docid"]]) ? $relid["docid"] : $tInitid[$relid["docid"]];
127  } else {
128  if (!$relid["state"]) {
129  $relationIds[$k]["rid"] = $relid["docid"];
130  }
131  }
132  }
133  }
134  $realIds = array();
135 
136  foreach ($relationIds as $relid) {
137  if (!empty($relid["rid"])) $realIds[] = $relid["rid"];
138  }
139  if ($realIds) {
140  $sql = sprintf("select id,initid,title,name,doctype,revision,icon,fromid,views && '%s' as canaccess from docread where id in (%s)", self::getUserVector() , implode(',', $realIds));
141  simpleQuery($doc->dbaccess, $sql, $result);
142  $accesses = array();
143  foreach ($result as $access) {
144  $accesses[$access["id"]] = $access;
145  }
146 
147  foreach ($relationIds as $k => $relid) {
148  $rid = $relid["rid"];
149  if ($rid && isset($accesses[$rid])) {
150 
151  if ($accesses[$rid]["doctype"] === "C") {
152  $relationIds[$k]["title"] = DocFam::getLangTitle(array(
153  "name" => $accesses[$rid]["name"],
154  "title" => $accesses[$rid]["title"]
155  ));
156  } else {
157  $relationIds[$k]["title"] = $accesses[$rid]["title"];
158  }
159  $relationIds[$k]["canaccess"] = $accesses[$rid]["canaccess"];
160  $relationIds[$k]["revision"] = $accesses[$rid]["revision"];
161  $relationIds[$k]["initid"] = $accesses[$rid]["initid"];
162  $relationIds[$k]["fromid"] = $accesses[$rid]["fromid"];
163  $relationIds[$k]["icon"] = $accesses[$rid]["icon"];
164  }
165  }
166  }
167  if (!empty(self::$relationCache[$uid])) self::$relationCache[$uid] = array_merge($relationIds, self::$relationCache[$uid]);
168  else self::$relationCache[$uid] = $relationIds;
169  }
170  /**
171  * Get title from database if not found in cache
172  * @param int $docid Document identifier
173  * @param bool $latest
174  * @param string $docrevOption
175  * @param array $info
176  * @return bool|null
177  * @throws \Dcp\Db\Exception
178  */
179  public static function getTitle($docid, $latest = true, $docrevOption = "latest", array & $info = array())
180  {
181 
182  if ($latest || $docrevOption === "latest") {
183  $sql = sprintf("select id,initid,title,revision,name,doctype,fromid,icon,views && '%s' as canaccess from docread where initid = %d and locked != -1", self::getUserVector() , $docid);
184  } else {
185  if (preg_match('/^state\(([^\)]+)\)/', $docrevOption, $matches)) {
186  $revState = $matches[1];
187  $sql = sprintf("select id,initid,revision,title,name,doctype,fromid,icon,views && '%s' as canaccess from docread where initid=(select initid from docread where id=%d) and state = '%s' and locked = -1 order by id desc limit 1", self::getUserVector() , $docid, pg_escape_string($revState));
188  } else {
189  $sql = sprintf("select id,initid,revision,title,name,doctype,fromid,icon,views && '%s' as canaccess from docread where id = %d", self::getUserVector() , $docid);
190  }
191  }
192  simpleQuery('', $sql, $result, false, true);
193  if ($result) {
194  $uid = getCurrentUser()->id;
195  $keyCache = $result["id"] . '-' . intval($latest);
196  self::$relationCache[$uid][$keyCache] = array(
197  "docid" => $result["id"],
198  "rid" => $result["id"],
199  "initid" => $result["initid"],
200  "latest" => $latest,
201  "revision" => $result["revision"],
202  "title" => $result["title"],
203  "fromid" => $result["fromid"],
204  "icon" => $result["icon"],
205  "canaccess" => $result["canaccess"]
206  );
207 
208  if ($result["canaccess"] === 't' || $uid === "1") {
209  $info = $result;
210  return $result["title"];
211  } else {
212  return false; //_("information access deny");
213 
214  }
215  }
216  return null;
217  }
218  /**
219  * get user vector of current user
220  * @static
221  * @return string
222  */
223  private static function getUserVector()
224  {
225  $uid = getCurrentUser()->id;
226  if (!isset(self::$viewUserVector[$uid])) {
227  self::$viewUserVector[$uid] = SearchDoc::getUserViewVector($uid);
228  }
229  return self::$viewUserVector[$uid];
230  }
231 }
static setRelationCache(Doc $doc, $uid)
getNormalAttributes($onlyopt=false)
Definition: Class.Doc.php:2387
static getRelationTitle($docid, $latest=true, Doc $doc, $docrevOption="", array &$info=array())
$docid
Definition: cleanFamily.php:13
getMultipleRawValues($idAttr, $def="", $index=-1)
Definition: Class.Doc.php:3240
getCurrentUser()
Definition: Lib.Common.php:250
getDbAccess()
Definition: Lib.Common.php:368
static getLangTitle($values)
static getUserViewVector($uid)
$info
Definition: geticon.php:30
getIdFromName($dbaccess, $name)
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
Definition: Lib.Common.php:484
$latest
static getTitle($docid, $latest=true, $docrevOption="latest", array &$info=array())
← centre documentaire © anakeen