Platform  3.1
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  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * to record timer attached to documents
9  *
10  * @author Anakeen 2008
11  * @version $Id: Class.DocTimer.php,v 1.7 2009/01/07 18:04:27 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 DocTimer extends DbObj
20 {
21  public $fields = array(
22  "timerid", // timer id
23  "level", // current level
24  "originid", // doc which create attach
25  "docid", // document attached
26  "title", // title document attached
27  "fromid", // fromid of docid
28  "attachdate", // date of attachement
29  "tododate", // date to execute
30  "donedate", // executed date
31  "actions", // actions to execute
32  "result"
33  // result text
34 
35  );
36  public $sup_fields = array(
37  "id"
38  ); // not be in fields auto computed
39 
40  /**
41  * identificator of timer
42  * @public int
43  */
44  public $id;
45  /**
46  * comment date to execute
47  * @public date
48  */
49  public $tododate;
50  /**
51  * level of timer (number of iterations)
52  * @public int
53  */
54  public $level;
55 
56  public $id_fields = array(
57  "id"
58  );
59 
60  public $dbtable = "doctimer";
61 
62  public $sqlcreate = "
63 create table doctimer ( id serial,
64  timerid int not null,
65  level int not null default 0,
66  originid int,
67  docid int not null,
68  title text,
69  fromid int not null,
70  attachdate timestamp,
71  tododate timestamp,
72  donedate timestamp,
73  actions text,
74  result text );
75 ";
76 
77  function preInsert()
78  {
79  include_once ("Class.QueryDb.php");
80  $docid = intval($this->docid);
81  $timerid = intval($this->timerid);
82  $q = new QueryDb($this->dbaccess, $this->dbtable);
83  $q->addQuery("docid=$docid");
84  $q->addQuery("tododate is not null");
85  $q->addQuery("timerid=$timerid");
86  $c = $q->count();
87 
88  if ($c > 0) return _("timer already set");
89  }
90  /**
91  * delete all timers which comes from same origin
92  * @param int $docid initial doc identificator to detach
93  * @param int $originid initial origin id
94  * @param int &$c count of deletion
95  * @return string error - empty if no error -
96  */
97  function unattachFromOrigin($docid, $originid, &$c = 0)
98  {
99  $docid = intval($docid);
100  $originid = intval($originid);
101  $err = "";
102  if ($docid == 0) $err = _("cannot detach : document id is not set");
103  if ($originid == 0) $err.= _("cannot detach : origin id is not set");
104  if ($err == "") {
105  $q = new QueryDb($this->dbaccess, $this->dbtable);
106  $q->addQuery("docid=$docid");
107  $q->addQuery("tododate is not null");
108  $q->addQuery("originid=$originid");
109  $c = $q->count();
110 
111  $err = $this->exec_query("delete from doctimer where docid=$docid and originid=$originid and tododate is not null");
112  }
113  return $err;
114  }
115  /**
116  * delete all timers for a document
117  * @param int $docid initial doc identificator to detach
118  * @param int &$c count of deletion
119  * @return string error - empty if no error -
120  */
121  function unattachAll($docid, &$c)
122  {
123  $docid = intval($docid);
124  $err = "";
125  if ($docid == 0) $err = _("cannot detach : document id is not set");
126  if ($err == "") {
127  $q = new QueryDb($this->dbaccess, $this->dbtable);
128  $q->addQuery("docid=$docid");
129  $q->addQuery("tododate is not null");
130  $c = $q->count();
131 
132  $err = $this->exec_query("delete from doctimer where docid=$docid and tododate is not null");
133  }
134  return $err;
135  }
136  /**
137  * delete a specific timer for a document
138  * @param int $docid initial doc identificator to detach
139  * @param int $timerid timerc identificator to detach
140  * @return string error - empty if no error -
141  */
142  function unattachDocument($docid, $timerid)
143  {
144  $docid = intval($docid);
145  $timerid = intval($timerid);
146  $err = "";
147  if ($docid == 0) $err = _("cannot detach : document id is not set");
148  if ($timerid == 0) $err = _("cannot detach : timer id is not set");
149  if ($err == "") $err = $this->exec_query("delete from doctimer where docid=$docid and tododate is not null and timerid=$timerid");
150  return $err;
151  }
152  /**
153  * get all actions need to be executed now
154  */
156  {
157  $q = new QueryDb($this->dbaccess, "DocTimer");
158  $q->addQuery("tododate is not null");
159  $q->addQuery("tododate < now()");
160  $timerhourlimit = getParam("FDL_TIMERHOURLIMIT", 2);
161  $q->addQuery("tododate > now() - interval '$timerhourlimit hour'");
162  $l = $q->Query(0, 0, "TABLE");
163  if ($q->nb > 0) return $l;
164  return array();
165  }
166 
167  function executeTimerNow()
168  {
169  $timer = new_doc($this->dbaccess, $this->timerid);
170  if (!$timer->isAlive()) return sprintf(_("cannot execute timer : timer %s is not found") , $timerid);
171 
172  $err = $timer->executeLevel($this->level, $this->docid, $msg, $gonextlevel);
173  if ($gonextlevel) {
174  $yetalivetimer = new DocTimer($this->dbaccess, $this->id);
175  if ($yetalivetimer->isAffected()) {
176  $this->donedate = $timer->getTimeDate();
177  $this->tododate = "";
178  $this->result = $msg;
179  $err = $this->modify();
180  $this->id = "";
181  $this->level++;
182  $acts = $timer->getPrevisions($this->attachdate, false, $this->level, 1);
183  if (count($acts) == 1) {
184  $act = current($acts);
185  if ($act["execdate"]) {
186  $this->donedate = '';
187  $this->result = '';
188  $this->tododate = $act["execdate"];
189  $this->actions = serialize($act["actions"]);
190  $err = $this->Add();
191  }
192  }
193  }
194  }
195  }
196 }
197 ?>
← centre documentaire © anakeen - published under CC License - Dynacase