Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.DocWait.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 
7 require_once "Class.DbObj.php";
8 /**
9  * Waiting document
10  *
11  * @brief Temporary saving
12  * @class DocWait
13  * @package FDL
14  */
15 class DocWait extends DbObj
16 {
17  /**
18  * database field
19  *
20  * @var array
21  */
22  public $fields = array(
23  "refererid", // doc id
24  "refererinitid", // doc initid
25  "localid", // temporary id in case of creation
26  "title", // doc title
27  "fromid", // family
28  "values", // values of document (serialized object)
29  "orivalues", // original values of document (serialized object)
30  "uid", // user id
31  "domain", // domain id
32  "status", // status code
33  "statusmessage", // status message
34  "transaction", // transaction id
35  "date",
36  "extradata"
37  );
38  /**
39  * identifier of referer document
40  *
41  * @var integer
42  */
43  public $refererid;
44  /**
45  * initial identifier of referer document
46  *
47  * @var integer
48  */
50  /**
51  * temporary identifier use before creation
52  *
53  * @var integer
54  */
55  public $localid;
56  /**
57  * original values (serialized)
58  *
59  * @var string
60  */
61  public $orivalues;
62  /**
63  * extra data (json)
64  *
65  * @var string
66  */
67  public $extradata;
68  /**
69  * family identifier
70  *
71  * @var int
72  */
73  public $fromid;
74  /**
75  * new values (serialized)
76  *
77  * @var string
78  */
79  public $values;
80  /**
81  * transaction identifier
82  *
83  * @var int
84  */
85  public $transaction;
86  /**
87  * identifier system of the user
88  *
89  * @var integer
90  */
91  public $uid;
92  /**
93  * document title
94  *
95  * @var string
96  */
97  public $title;
98  /**
99  * date record
100  *
101  * @var string
102  */
103  public $date;
104  /**
105  * document status : ok|constraint|obsolete
106  *
107  * @var string
108  */
109  public $status;
110  /**
111  * document status message
112  *
113  * @var string
114  */
116  /**
117  * arg of code
118  *
119  * @var string serialize object
120  */
121  public $arg;
122  /**
123  * fields primary key
124  *
125  * @var array fields primary key
126  */
127  public $id_fields = array(
128  "refererinitid",
129  "uid"
130  );
131  /**
132  * database table name
133  *
134  * @var array
135  */
136  public $dbtable = "docwait";
137  /**
138  * sql create table
139  *
140  * @var array
141  */
142  public $sqlcreate = "
143 create table docwait ( refererid int not null,
144  refererinitid int not null,
145  localid text,
146  fromid int,
147  title text,
148  uid int not null,
149  values text,
150  orivalues text,
151  date timestamp default now(),
152  domain int,
153  transaction int,
154  statusmessage text,
155  status text,
156  extradata text );
157 create index i_docwait on docwait(transaction);
158 create unique index iu_docwait on docwait(refererinitid, uid);
159 create sequence seq_waittransaction start 1;
160 ";
161  /**#@+
162  * constant for waiting status"
163  */
164  const newDocument = "new";
165  const upToDate = "uptodate";
166  const modified = "modified";
167  const conflict = "conflict";
168  const constraint = "constraint";
169  const invalid = "invalid";
170  const recording = "recording";
171  /**
172  * referer document
173  *
174  * @var Doc
175  */
176  private $refererDoc = null;
177  /**
178  * referer document identifier
179  *
180  * @var integer
181  */
182  private $refererDocId = null;
183  /**
184  * waiting document
185  *
186  * @var Doc
187  */
188  private $waitingDoc = null;
189  /**
190  * save waiting document
191  *
192  * @param mixed &$info informations for save
193  *
194  * @return string error message
195  */
196  public function save(&$info = null)
197  {
198  $err = '';
199  $this->status = $this->computeStatus();
200  if ($this->status == self::conflict) $err = $this->statusmessage;
201  else {
202  $wdoc = $this->getWaitingDocument();
203  $wdoc->doctype = $wdoc->defDoctype; // become consistent
204  if ($this->localid) {
205  // create it
206  $err = $wdoc->add();
207  if (!$err) {
208  if ($this->localid) {
209  $this->refererid = $wdoc->id;
210  $this->refererinitid = $wdoc->initid;
211  // change primary key
212  //$this->exec_query(sprintf("delete from docwait where localid='%s'", pg_escape_string($this->localid)));
213  $this->exec_query(sprintf("update docwait set refererid=%d, refererinitid=%d, localid='' where localid='%s'", $this->refererid, $this->refererinitid, pg_escape_string($this->localid)));
214  }
215  }
216  }
217  if (!$err) {
218  $info = null;
219  $err = $wdoc->save($info);
220  clearCacheDoc($this->refererid);
221  $this->refererDoc=$wdoc;
222  }
223  if ($err) {
224  $this->status = self::constraint;
225  $this->statusmessage = (!empty($info->error)) ? $info->label . ' : ' . $info->error : $err;
226  // $this->statusmessage=json_encode($info->error);
227  $this->modify();
228  } else {
229  $this->resetWaitingDocument();
230  }
231  }
232  // error_log("try create $err".$this->localid."::".$wdoc->id);
233  //print "save [$this->status]" . $this->title;
234  return $err;
235  }
236  /**
237  * get waiting document from database
238  *
239  * @return string error message
240  */
241  public function resetWaitingDocument()
242  {
243  $doc = $this->getRefererDocument(true);
244  $err = '';
245  if ($doc) {
246  $this->refererinitid = $doc->initid;
247  $this->refererid = $doc->id;
248  $this->orivalues = serialize($doc->getValues());
249  $this->status = self::upToDate;
250  $this->statusmessage = '';
251  $this->transaction = 0;
252  $this->date = date('Y-m-d H:i:s.u');
253  $err = $this->modify();
254  }
255  return $err;
256  }
257  /**
258  * get write attribute of a doc
259  *
260  * @param Doc &$doc the doc
261  *
262  * @return array of docAttribute
263  */
264  private function getWriteAttribute(Doc & $doc)
265  {
266  $attrs = $doc->getNormalAttributes();
267  $wattr = array();
268  foreach ($attrs as $aid => $oa) {
269  if (($oa->mvisibility == 'W') || ($oa->mvisibility == 'O') || $oa->getOption("writted") == "yes") {
270  $wattr[$aid] = $oa;
271  }
272  }
273  return $wattr;
274  }
275  /**
276  * complete
277  *
278  * @return void
279  */
280  public function complete()
281  {
282  $this->refererDoc = null;
283  $this->waitingDoc = null;
284  }
285  /**
286  * the referer (null if new document)
287  *
288  * @param boolean $reset set to true to force update from database
289  *
290  * @return Doc the referer
291  */
292  public function getRefererDocument($reset = false)
293  {
294  if ($reset) $this->refererDoc = null;
295  if ($this->refererid <= 0) return null;
296  if (!$this->refererDoc) {
297  $this->refererDoc = new_doc($this->dbaccess, $this->refererid, true);
298  $this->refererDocId = $this->refererDoc->id;
299  if ($this->waitingDoc) {
300  $this->values = serialize($this->waitingDoc->getValues());
301  $this->waitingDoc = null;
302  }
303  } else {
304  if (($this->refererDoc->id != $this->refererDocId) || ($fix = $this->refererDoc->isFixed()) || ($fix === null)) {
305  $this->refererDoc = new_doc($this->dbaccess, $this->refererid, true);
306  $this->refererDocId = $this->refererDoc->id;
307  if ($this->waitingDoc) {
308  $this->values = serialize($this->waitingDoc->getValues());
309  $this->waitingDoc = null;
310  }
311  }
312  }
313  return $this->refererDoc;
314  }
315  /**
316  * the referer with new values, document ready to update original
317  *
318  * @return Doc the document
319  */
320  public function getWaitingDocument()
321  {
322  $cdoc = $this->getRefererDocument(); // refresh referer if needed
323  if (!$this->waitingDoc) {
324  if (!$cdoc) {
325  $cdoc = createDoc($this->dbaccess, $this->fromid, false, false);
326  }
327  $this->waitingDoc = clone $cdoc;
328  $waitValues = unserialize($this->values);
329  foreach ($waitValues as $aid => $v) {
330  if ($v == '') $v = ' ';
331  $this->waitingDoc->setValue($aid, $v);
332  }
333  $this->waitingDoc->doctype = 'I';
334  }
335  return $this->waitingDoc;
336  }
337  /**
338  * verify if waiting document status is valid
339  *
340  * @return boolean true if valid
341  */
342  public function isValid()
343  {
344  return ($this->status == self::newDocument || $this->status == self::modified || $this->status == self::upToDate);
345  }
346  /**
347  * verify if origin values are same as current alive values
348  *
349  * @param integer $mask identifier to use specific mask to detect W attributes
350  *
351  * @brief restrict to W/O visibilities values
352  * @return integer ths status code
353  */
354  public function computeStatus($mask = 0)
355  {
356  if ($this->status != self::invalid) {
357  if ($this->refererid) {
358  $originValues = unserialize($this->orivalues);
359  $currentDoc = $this->getRefererDocument();
360  if ($currentDoc->isAlive()) {
361  $err = $currentDoc->canEdit(false);
362  if ($err) {
363  $this->statusmessage = $err;
364  $this->status = self::conflict;
365  } else {
366  if ($currentDoc->locked != $currentDoc->getSystemUserId()) {
367  $this->statusmessage = sprintf("document %s [%d] not locked", $currentDoc->getTitle() , $currentDoc->id);
368  $this->status = self::conflict;
369  } else {
370  if ($mask) $currentDoc->ApplyMask($mask);
371  $attrs = $this->getWriteAttribute($currentDoc);
372  $this->status = self::upToDate;
373  $this->statusmessage = '';
374  /*print_r2(array(
375  "cur" => $currentDoc->getValues(),
376  "wai" => $this->getWaitingDocument()->getValues(),
377  "ori" => $originValues
378  ));
379  */
380  $waitingDoc = $this->getWaitingDocument();
381  /*
382  * @var NormalAttribute $oa
383  */
384  foreach ($attrs as $aid => $oa) {
385  $ovalue = $originValues[$oa->id];
386  $cvalue = $currentDoc->getRawValue($oa->id);
387  if ($ovalue != $cvalue) {
388  $this->status = self::conflict;
389  $this->statusmessage.= sprintf(_("conflict %s [%s]: referer=%s, modified=%s") , $oa->getLabel() , $oa->id, $cvalue, $ovalue) . "\n";
390  }
391  }
392  $this->statusmessage = substr($this->statusmessage, 0, -1);
393  }
394  }
395  } else {
396  $this->statusmessage = sprintf("document with id %d does not exists", $this->refererid);
397  $this->status = self::conflict;
398  }
399  } else {
400  $this->status = self::newDocument;
401  }
402  $this->modify();
403  }
404  return $this->status;
405  }
406  /**
407  * get extrat data of waiting document
408  *
409  * @return string the data
410  */
411  public function getExtraData()
412  {
413  return ($this->extradata) ? json_decode($this->extradata) : null;
414  }
415 }
416 ?>
getNormalAttributes($onlyopt=false)
Definition: Class.Doc.php:2387
computeStatus($mask=0)
restrict to W/O visibilities values
Temporary saving.
exec_query($sql, $lvl=0, $prepare=false)
clearCacheDoc($id=0)
getWaitingDocument()
save(&$info=null)
resetWaitingDocument()
const constraint
modify($nopost=false, $sfields="", $nopre=false)
getRefererDocument($reset=false)
const conflict
const modified
createDoc($dbaccess, $fromid, $control=true, $defaultvalues=true, $temporary=false)
const newDocument
const upToDate
$info
Definition: geticon.php:30
if($file) if($subject==""&&$file) if($subject=="") $err
const recording
const invalid
← centre documentaire © anakeen