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