Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Method.Timer.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  * Mail template document
9  *
10  * @author Anakeen 2009
11  * @version $Id: Method.Timer.php,v 1.8 2009/01/16 12:53:10 eric Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  * @subpackage
15  */
16 /**
17  */
18 /**
19  * @begin-method-ignore
20  * this part will be deleted when construct document class until end-method-ignore
21  */
22 class _TIMER extends Doc
23 {
24  /*
25  * @end-method-ignore
26  */
27  private $lineActions;
28  /**
29  * attach timer to a document
30  * @param _TIMER &$timer the timer document
31  * @param Doc &$origin the document which comes from the attachement
32  * @param date $tododate special date to trigger the actions
33  * @return string error - empty if no error -
34  */
35  function attachDocument(&$doc, &$origin, $tododate = null)
36  {
37  include_once ("FDL/Class.DocTimer.php");
38 
39  $dt = new DocTimer($this->dbaccess);
40  $dt->timerid = $this->id;
41  $dt->docid = $doc->initid;
42  $dt->title = $doc->title;
43  $dt->attachdate = $doc->getTimeDate(); // now
44  $dt->level = 0;
45  if ($origin) $dt->originid = $origin->id;
46  $dt->fromid = $doc->fromid;
47 
48  $dates = $this->getTValue("tm_delay");
49  $hours = $this->getTValue("tm_hdelay");
50 
51  if (((count($dates) == 0) || $dates[0] + $hours[0] == 0) && ($tododate == null)) {
52  $err = sprintf(_("no delay specified in timer %s [%d]") , $this->title, $this->id);
53  } else {
54 
55  $acts = $this->getPrevisions($dt->attachdate, false, 0, 1);
56  if (count($acts) == 1) {
57  $act = current($acts);
58  $dt->actions = serialize($act["actions"]);
59 
60  $day = doubleval($dates[0]);
61  $hour = doubleval($hours[0]);
62  if ($tododate) $dt->tododate = $tododate;
63  else $dt->tododate = $this->getTimeDate(24 * $day + $hour);
64  $err = $dt->Add();
65  } else $err = sprintf(_("no level 0 specified in timer %s [%d]") , $this->title, $this->id);
66  }
67  return $err;
68  }
69  /**
70  * unattach timer to a document
71  * @param _TIMER &$timer the timer document
72  * @param Doc &$origin the document which comes from the attachement
73  * @return string error - empty if no error -
74  */
75  function unattachAllDocument(&$doc, &$origin = null, &$c = 0)
76  {
77  include_once ("FDL/Class.DocTimer.php");
78 
79  $dt = new DocTimer($this->dbaccess);
80  if ($origin) $err = $dt->unattachFromOrigin($doc->initid, $origin->initid, $c);
81  else $err = $dt->unattachAll($doc->initid, $c);
82 
83  return $err;
84  }
85  /**
86  * unattach timer to a document
87  * @param _TIMER &$timer the timer document
88  * @param Doc &$origin the document which comes from the attachement
89  * @return string error - empty if no error -
90  */
92  {
93  include_once ("FDL/Class.DocTimer.php");
94 
95  $dt = new DocTimer($this->dbaccess);
96  $err = $dt->unattachDocument($doc->initid, $this->id);
97 
98  return $err;
99  }
100  /**
101  * get prevision for an activate timer
102  * @param date $adate attach date
103  * @param date $tododate todo date may be false if not an already attached timer
104  * @param int $level from level
105  * @param int $maxOccur slice level (since level+maxOccur)
106  * @return array array of prevision
107  */
108  function getPrevisions($adate, $tododate = false, $level = 0, $maxOccur = 10)
109  {
110  $this->linearizeActions();
111 
112  $jdnow = StringDateToJD($this->getTimeDate());
113  $jdattach = StringDateToJD($adate);
114  $spentDelay = $jdnow - $jdattach;
115 
116  $first = true;
117  $tprev = array();
118  $jdstart = $jdattach; //$jdnow-$spentDelay;
119  //compute jdstart first
120  $max = min(($level + $maxOccur) , count($this->lineActions));
121  for ($clevel = 0; $clevel < $level; $clevel++) {
122  $prev[$clevel] = $this->lineActions[$clevel];
123  if ($first && $tododate) { // add delta when timer is modify after attachement
124  /*$jdtodo=StringDateToJD($tododate);
125  $execdate=$jdstart+$prev[$clevel]["delay"];
126  $delta=$jdtodo - $execdate;
127  $first=false;
128  $jdstart += $delta;*/
129  }
130  $ldelay = $prev[$clevel]["delay"];
131  $jdstart+= $ldelay;
132  }
133  for ($clevel = $level; $clevel < $max; $clevel++) {
134  $tprev[$clevel] = $this->lineActions[$clevel];
135  if ($first && $tododate) { // add delta when timer is modify after attachement
136  $jdtodo = StringDateToJD($tododate);
137  $execdate = $jdstart + $tprev[$clevel]["delay"];
138  $delta = $jdtodo - $execdate;
139  $first = false;
140  $jdstart+= $delta;
141  }
142  $ldelay = $tprev[$clevel]["delay"];
143  // print "$clevel)jdstart:$jdstart".jd2cal($jdstart)."[$ldelay] --".jd2cal($jdstart+$ldelay)."--\n";
144  $tprev[$clevel]["execdate"] = jd2cal($jdstart + $ldelay);
145  $tprev[$clevel]["execdelay"] = ($jdstart + $ldelay) - $jdnow;
146  $jdstart+= $ldelay;
147  }
148  return ($tprev);
149  }
150 
151  private function linearizeActions()
152  {
153  $this->lineActions = array();
154  $tactions = $this->getAvalues("tm_t_config");
155  $level = 0;
156  foreach ($tactions as $k => $v) {
157  $repeat = intval($v["tm_iteration"]);
158  if ($repeat <= 0) $repeat = 1;
159 
160  for ($i = 0; $i < $repeat; $i++) {
161  $this->lineActions[$level] = array(
162  "level" => $level,
163  "delay" => $v["tm_delay"] + ($v["tm_hdelay"] / 24) ,
164  "actions" => array(
165  "state" => $v["tm_state"],
166  "tmail" => $v["tm_tmail"],
167  "method" => $v["tm_method"]
168  )
169  );
170  $level++;
171  }
172  }
173  ksort($this->lineActions);
174  }
175  /**
176  * execute a level for a document
177  * @param int $level level af action to execute
178  * @param int $docid document to apply action
179  * @return string error - empty if no error -
180  */
181  function executeLevel($level, $docid, &$msg = null, &$nextlevel = true)
182  {
183  $msg = '';
184  $nextlevel = true;
185  $doc = new_doc($this->dbaccess, $docid, true);
186  if (!$doc->isAlive()) return sprintf(_("cannot execute : document %s is not found") , $docid);
187  $acts = $this->getPrevisions($this->getTimeDate() , false, $level, 1);
188 
189  $gerr = "";
190  $tmsg = array();
191  if (count($acts) > 0) {
192  foreach ($acts as $k => $v) {
193  foreach ($v["actions"] as $ka => $va) {
194  if ($va) {
195  $err = "";
196  switch ($ka) {
197  case "tmail":
198  $tva = $this->_val2array(str_replace('<BR>', "\n", $va));
199  foreach ($tva as $idmail) {
200  $tm = new_doc($this->dbaccess, $idmail);
201  if ($tm->isAlive()) {
202  $msg = sprintf(_("send mail with template %s [%d]") , $tm->title, $tm->id);
203  $doc->addComment(sprintf(_("execute timer %s (level %d) : %s") , $this->title, $this->level, $msg));
204  $err = $tm->sendDocument($doc);
205  $tmsg[] = $msg;
206  }
207  }
208  break;
209 
210  case "state":
211  $msg = sprintf(_("change state to %s") , _($va));
212  $doc->addComment(sprintf(_("execute timer %s (level %d) : %s") , $this->title, $this->level, $msg));
213  $err = $doc->setState($va);
214  $tmsg[] = $msg;
215  break;
216 
217  case "method":
218  $msg = sprintf(_("apply method %s") , $va);
219  $doc->addComment(sprintf(_("execute timer %s (level %d) : %s") , $this->title, $this->level, $msg));
220  $err = $doc->applyMethod($va);
221  $tmsg[] = $msg;
222  break;
223  }
224 
225  if ($err) {
226  $gerr.= "$err\n";
227  $doc->addComment(sprintf(_("execute timer %s (level %d) : %s") , $this->title, $this->level, $err) , HISTO_ERROR);
228  }
229  }
230  }
231  }
232  } else {
233  $nextlevel = false; // this is the end level
234 
235  }
236 
237  $msg = implode(".\n", $tmsg);
238  return $gerr;
239  }
240  /**
241  * @begin-method-ignore
242  * this part will be deleted when construct document class until end-method-ignore
243  */
244 }
245 /*
246  * @end-method-ignore
247 */
248 ?>
← centre documentaire © anakeen - published under CC License - Dynacase