Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.DocTimer.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * to record timer attached to documents
8  *
9  * @author Anakeen
10  * @version $Id: Class.DocTimer.php,v 1.7 2009/01/07 18:04:27 eric Exp $
11  * @package FDL
12  */
13 /**
14  */
15 
16 include_once ("Class.DbObj.php");
17 class DocTimer extends DbObj
18 {
19  public $fields = array(
20  "timerid", // timer id
21  "level", // current level
22  "originid", // doc which create attach
23  "docid", // document attached
24  "title", // title document attached
25  "fromid", // fromid of docid
26  "attachdate", // date of attachement
27  "referencedate", // reference date
28  "tododate", // date to execute
29  "donedate", // executed date
30  "actions", // actions to execute
31  "result"
32  // result text
33 
34  );
35  public $sup_fields = array(
36  "id"
37  ); // not be in fields auto computed
38 
39  /**
40  * identifier of timer
41  * @public int
42  */
43  public $id;
44  /**
45  * comment date to execute
46  * @public date
47  */
48  public $tododate;
49  /**
50  * level of timer (number of iterations)
51  * @public int
52  */
53  public $level;
54  /**
55  * Timer identifier
56  * @public int $timerid
57  */
58  public $timerid;
59  /**
60  * Document identifier
61  * @public int $docid
62  */
63  public $docid;
64  /**
65  * Reference date to compute process execution date
66  * @public string $referencedate
67  */
69  /**
70  * Executed date
71  * @public string $donedate
72  */
73  public $donedate;
74  /**
75  * Attach date to document
76  * @public string $attachdate
77  */
78  public $attachdate;
79  /**
80  * Action result
81  * @public string $result
82  */
83  public $result;
84  /**
85  * Actions to be executed
86  * @public string $actions
87  */
88  public $actions;
89  /**
90  * Timer title
91  * @public string title
92  */
93  public $title;
94  public $id_fields = array(
95  "id"
96  );
97 
98  public $dbtable = "doctimer";
99 
100  public $sqlcreate = "
101 create table doctimer ( id serial,
102  timerid int not null,
103  level int not null default 0,
104  originid int,
105  docid int not null,
106  title text,
107  fromid int not null,
108  attachdate timestamp,
109  referencedate timestamp,
110  tododate timestamp,
111  donedate timestamp,
112  actions text,
113  result text );
114 ";
115 
116  function preInsert()
117  {
118  include_once ("Class.QueryDb.php");
119  $docid = intval($this->docid);
120  $timerid = intval($this->timerid);
121  $q = new QueryDb($this->dbaccess, $this->dbtable);
122  $q->addQuery("docid=$docid");
123  $q->addQuery("tododate is not null");
124  $q->addQuery("timerid=$timerid");
125  $c = $q->count();
126 
127  if ($c > 0) return _("timer already set");
128  return "";
129  }
130  /**
131  * delete all timers which comes from same origin
132  * @param int $docid initial doc identifier to detach
133  * @param int $originid initial origin id
134  * @param int &$c count of deletion
135  * @return string error - empty if no error -
136  */
137  function unattachFromOrigin($docid, $originid, &$c = 0)
138  {
139  $docid = intval($docid);
140  $originid = intval($originid);
141  $err = "";
142  if ($docid == 0) $err = _("cannot detach : document id is not set");
143  if ($originid == 0) $err.= _("cannot detach : origin id is not set");
144  if ($err == "") {
145  $q = new QueryDb($this->dbaccess, $this->dbtable);
146  $q->addQuery("docid=$docid");
147  $q->addQuery("tododate is not null");
148  $q->addQuery("originid=$originid");
149  $c = $q->count();
150 
151  $err = $this->exec_query("delete from doctimer where docid=$docid and originid=$originid and tododate is not null");
152  }
153  return $err;
154  }
155  /**
156  * delete all timers for a document
157  * @param int $docid initial doc identifier to detach
158  * @param int &$c count of deletion
159  * @return string error - empty if no error -
160  */
161  function unattachAll($docid, &$c)
162  {
163  $docid = intval($docid);
164  $err = "";
165  if ($docid == 0) $err = _("cannot detach : document id is not set");
166  if ($err == "") {
167  $q = new QueryDb($this->dbaccess, $this->dbtable);
168  $q->addQuery("docid=$docid");
169  $q->addQuery("tododate is not null");
170  $c = $q->count();
171 
172  $err = $this->exec_query("delete from doctimer where docid=$docid and tododate is not null");
173  }
174  return $err;
175  }
176  /**
177  * delete a specific timer for a document
178  * @param int $docid initial doc identifier to detach
179  * @param int $timerid timerc identifier to detach
180  * @return string error - empty if no error -
181  */
183  {
184  $docid = intval($docid);
185  $timerid = intval($timerid);
186  $err = "";
187  if ($docid == 0) $err = _("cannot detach : document id is not set");
188  if ($timerid == 0) $err = _("cannot detach : timer id is not set");
189  if ($err == "") $err = $this->exec_query("delete from doctimer where docid=$docid and tododate is not null and timerid=$timerid");
190  return $err;
191  }
192  /**
193  * get all actions need to be executed now
194  */
196  {
197  $q = new QueryDb($this->dbaccess, "DocTimer");
198  $q->addQuery("tododate is not null");
199  $q->addQuery("tododate < now()");
200  $timerhourlimit = getParam("FDL_TIMERHOURLIMIT", 2);
201  if ((int)$timerhourlimit <= 0) {
202  $timerhourlimit = 2;
203  }
204  $q->addQuery(sprintf("tododate > now() - interval '%d hour'", $timerhourlimit));
205  $l = $q->Query(0, 0, "TABLE");
206  if ($q->nb > 0) return $l;
207  return array();
208  }
209 
210  function executeTimerNow()
211  {
212  $timer = new_doc($this->dbaccess, $this->timerid);
213  /**
214  * @var \Dcp\Family\TIMER $timer
215  */
216  if (!$timer->isAlive()) return sprintf(_("cannot execute timer : timer %s is not found") , $this->timerid);
217 
218  $err = $timer->executeLevel($this->level, $this->docid, $msg, $gonextlevel);
219  if ($gonextlevel) {
220  $yetalivetimer = new DocTimer($this->dbaccess, $this->id);
221  if ($yetalivetimer->isAffected()) {
222  $this->donedate = $timer->getTimeDate();
223  $this->tododate = "";
224  $this->result = $msg;
225  $err = $this->modify();
226  $this->id = "";
227  $this->level++;
228  $acts = $timer->getPrevisions($this->referencedate ? $this->referencedate : $this->attachdate, false, $this->level, 1);
229  if (count($acts) == 1) {
230  $act = current($acts);
231  if ($act["execdate"]) {
232  $this->donedate = '';
233  $this->result = '';
234  $this->tododate = $act["execdate"];
235  $this->actions = serialize($act["actions"]);
236  $err = $this->Add();
237  }
238  }
239  }
240  }
241  return $err;
242  }
243 }
244 ?>
exec_query($sql, $lvl=0, $prepare=false)
Add($nopost=false, $nopre=false)
unattachFromOrigin($docid, $originid, &$c=0)
print docid
Definition: migrEnum.php:33
modify($nopost=false, $sfields="", $nopre=false)
unattachDocument($docid, $timerid)
getParam($name, $def="")
must be in core or global type
Definition: Lib.Common.php:193
unattachAll($docid, &$c)
if($file) if($subject==""&&$file) if($subject=="") $err
← centre documentaire © anakeen