Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.Workflow.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  * Workflow Object Definition
9  *
10  * @author Anakeen 2009
11  * @version $Id: $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  */
15 /**
16  */
17 include_once ("DATA/Class.Document.php");
18 /**
19  * Workflow Class
20  *
21  */
23 {
24  /**
25  * return properties, values and attributes definition
26  */
27  function getDocument($onlyvalues = false, $completeprop = true)
28  {
29  $out = parent::getDocument($onlyvalues, $completeprop);
30  $out["workflow"] = $this->getWorflowInformations();
31 
32  return $out;
33  }
34 
36  {
37  if ($this->doc->doctype != 'W') return null;
38  $states = $this->doc->getStates();
39  foreach ($states as $v) {
40  $out["states"][$v] = array(
41  "key" => $v,
42  "activity" => $this->doc->getActivity($v) ,
43  "label" => _($v)
44  );
45  }
46 
47  foreach ($this->doc->cycle as $v) {
48  $out["transitions"][] = array(
49  "start" => $v["e1"],
50  "finish" => $v["e2"],
51  "transitionType" => $v["t"]
52  );
53  }
54  foreach ($this->doc->transitions as $k => $v) {
55  $out["transitionTypes"][$k] = array(
56  "key" => $k,
57  "label" => _($k) ,
58  "ask" => $v["ask"],
59  "preMethod" => $v["m1"],
60  "postMethod" => $v["m2"],
61  "noComment" => $v["nr"] ? true : false
62  );
63  }
64 
65  return $out;
66  }
67 
68  function addTransition($start, $finish, $transitionType)
69  {
70  return $this->modifyTransition("add", $start, $finish, $transitionType);
71  }
72 
73  function removeTransition($start, $finish)
74  {
75  return $this->modifyTransition("remove", $start, $finish);
76  }
77 
78  private function modifyTransition($addorrem, $start, $finish, $transitionType = '')
79  {
80  if ($this->doc) {
81  if ($start && $finish && ($addorrem == "remove" || $transitionType)) {
82  if ($this->doc->doctype != 'W') return null;
83  $err = $this->doc->lock(true);
84  if ($err) {
85  $this->setError($err);
86  return null;
87  }
88 
89  $cycles = $this->doc->cycle;
90  foreach ($cycles as $k => $v) {
91  if ($v["e1"] == $start && $v["e2"] == $finish) unset($cycles[$k]);
92  }
93  if ($addorrem == "add") {
94  $cycles[] = array(
95  "e1" => $start,
96  "e2" => $finish,
97  "t" => $transitionType
98  );
99  }
100  $fam = $this->doc->getFamDoc();
101  $classname = $fam->classname;
102  $file = sprintf("%s/FDL/Class.%s.php", DEFAULT_PUBDIR, $classname);
103  if (file_exists($file)) {
104  if (!is_writable($file)) {
105  $this->doc->unlock(true);
106  $this->setError(sprintf(_("workflow file %s is not writable") , $file));
107  return null;
108  }
109  $na = "array(";
110  foreach ($cycles as $k => $v) {
111  $na.= sprintf('array("e1"=>"%s", "e2"=>"%s", "t"=>"%s"),' . "\n", $v["e1"], $v["e2"], $v["t"]);
112  }
113  $na = substr($na, 0, -2) . ")"; //delete end comma and cr
114  $content = file_get_contents($file);
115  $nc = preg_replace('/\$cycle\s*=([^;]*);/i', "\$cycle=$na;", $content);
116 
117  file_put_contents($file, $nc);
118  $this->doc->cycle = $cycles;
119  //print "<pre>".htmlentities($nc)."</pre>";
120 
121  }
122 
123  $err = $this->doc->unlock(true);
124  } else {
125  $this->setError(_("modifyTransition: missing parameter"));
126  return null;
127  }
128  }
129  }
130 
131  function addTraduction($lang, $key, $text)
132  {
133  if ($key && $text && $lang) {
134  $mo = sprintf('msgid ""
135 msgstr ""
136 "Content-Type: text/plain; charset=UTF-8\n"
137 "Content-Transfer-Encoding: 8bit\n"
138 
139 msgid "%s"
140 msgstr "%s"', $key, str_replace('"', '\"', $text));
141 
142  $mofile = sprintf("%s/locale/%s/LC_MESSAGES/w%s.po", DEFAULT_PUBDIR, $lang, $this->doc->fromid);
143  file_put_contents($mofile, $mo);
144 
145  if (file_exists(sprintf("%s/locale/%s/LC_MESSAGES/0w.mo", DEFAULT_PUBDIR, $lang))) {
146 
147  $cmd = sprintf('cd %s/locale/%s/LC_MESSAGES;msgunfmt 0w.mo > _w.po && msgcat --use-first %s _w.po > __w.po && msgfmt __w.po -o 0w.mo && %s/whattext', DEFAULT_PUBDIR, $lang, $mofile, DEFAULT_PUBDIR);
148  } else {
149  $cmd = sprintf('cd %s/locale/%s/LC_MESSAGES;msgfmt %s -o 0w.mo && %s/whattext', DEFAULT_PUBDIR, $lang, $mofile, DEFAULT_PUBDIR);
150  }
151  //print $cmd;
152  $log = exec($cmd, $out, $ret);
153  // print "<hr>$log : $ret";
154  return ($ret == 0);
155  }
156  }
157 
158  function addState($key, $label = '', $activity = '')
159  {
160  // need control not exists
161  $this->alterState("add", $key, $label, $activity);
162  }
163 
164  function modifyState($key, $label = '', $activity = '')
165  {
166  // need control already exists
167  $this->alterState("modify", $key, $label, $activity);
168  }
169  function removeState($key)
170  {
171  // need control already exists
172  $this->alterState("remove", $key);
173  }
174 
175  private function alterState($fn, $key, $label = '', $activity = '')
176  {
177  if ($this->doc) {
178  if ($key) {
179  if ($this->doc->doctype != 'W') return null;
180  $err = $this->doc->lock(true);
181  if ($err) {
182  $this->setError($err);
183  return null;
184  }
185 
186  $states = $this->doc->getStates();
187  if ($fn == "remove") {
188  unset($states[$key]);
189  $cycles = $this->doc->cycle; // delete also transition which use these transition type
190  foreach ($cycles as $k => $v) {
191  if (($v["e1"] == $key) || ($v["e2"] == $key)) unset($cycles[$k]);
192  }
193  $ncy = "array(";
194  foreach ($cycles as $k => $v) {
195  $ncy.= sprintf('array("e1"=>"%s", "e2"=>"%s", "t"=>"%s"),' . "\n", $v["e1"], $v["e2"], $v["t"]);
196  }
197  $ncy = substr($ncy, 0, -2) . ")"; //delete end comma and cr
198  $this->doc->cycle = $cycles;
199  } else {
200  $states[$key] = $key;
201  }
202  $fam = $this->doc->getFamDoc();
203  $classname = $fam->classname;
204  $file = sprintf("%s/FDL/Class.%s.php", DEFAULT_PUBDIR, $classname);
205  if (file_exists($file)) {
206  if (!is_writable($file)) {
207  $this->doc->unlock(true);
208  $this->setError(sprintf(_("workflow file %s is not writable") , $file));
209  return null;
210  }
211  $na = "array(";;
212  foreach ($states as $k => $v) {
213  $na.= sprintf('"%s"=>"%s",' . "\n", $k, $v);
214  }
215  $na = substr($na, 0, -2) . ")"; //delete end comma and cr
216  $content = file_get_contents($file);
217  $nc = preg_replace('/public \$states\s*=([^;]*);/i', "public \$states=$na;", $content, 1, $count);
218  if ($count == 0) {
219  $nc = preg_replace("/(extends WDoc {\s*)/i", "extends WDoc {\n\tpublic \$states=$na;\n", $content, 1, $count);
220  }
221 
222  if ($ncy) $nc = preg_replace('/\$cycle\s*=([^;]*);/i', "\$cycle=$ncy;", $nc);
223  if ($label) {
224  $this->addTraduction('fr', $key, $label);
225  }
226 
227  if ($activity) {
228  $na = "array(";
229  $na.= sprintf('"%s"=>"%s",' . "\n", $key, $activity);
230  foreach ($states as $k => $v) {
231  $actid = $this->doc->attrPrefix . "_ACTIVITYLABEL" . $k;
232  $actv = $this->getValue($actid);
233  if ($k != $key) {
234  if ($actv) {
235  $na.= sprintf('"%s"=>"%s",' . "\n", $k, $actv);
236  }
237  }
238  }
239  $na = substr($na, 0, -2) . ")"; //delete end comma and cr
240  $nc = preg_replace('/public \$stateactivity\s*=([^;]*);/i', "public \$stateactivity=$na;", $nc, 1, $count);
241  if ($count == 0) {
242  $nc = preg_replace("/(extends WDoc {\s*)/i", "extends WDoc {\n\tpublic \$stateactivity=$na;\n", $nc, 1, $count);
243  }
244  $this->doc->stateactivity[$key] = $activity;
245  $this->doc->postModify();
246  }
247 
248  file_put_contents($file, $nc);
249  $this->doc->states = $states;
250  $wsh = getWshCmd();
251  $cmd = $wsh . " --api=fdl_adoc --docid=" . intval($this->doc->fromid);
252  $log = exec($cmd, $out, $ret);
253  if ($err) $this->setError($err);
254  // print "<pre>".htmlentities($nc)."</pre>";
255 
256  }
257 
258  $err = $this->doc->unlock(true);
259  } else {
260  $this->setError(_("addState: missing parameter"));
261  return null;
262  }
263  }
264  }
265 
266  function addTransitiontype($key, $label = '', $ask = null, $preMethod = null, $postmethod = null, $noComment = false, $autoNext = null)
267  {
268  return $this->alterTransitiontype("add", $key, $label, $ask, $preMethod, $postmethod, $noComment, $autoNext);
269  }
270 
271  function modifyTransitiontype($key, $label = '', $ask = null, $preMethod = null, $postmethod = null, $noComment = false, $autoNext = null)
272  {
273  return $this->alterTransitiontype("modify", $key, $label, $ask, $preMethod, $postmethod, $noComment, $autoNext);
274  }
275  function removeTransitiontype($key)
276  {
277  return $this->alterTransitiontype("remove", $key);
278  }
279 
280  private function alterTransitiontype($addorrem, $key, $label = '', $ask = null, $preMethod = null, $postmethod = null, $noComment = null, $autoNext = null)
281  {
282  if ($this->doc) {
283  if (true) {
284  if ($this->doc->doctype != 'W') return null;
285  $err = $this->doc->lock(true);
286  if ($err) {
287  $this->setError($err);
288  return null;
289  }
290 
291  $transitions = $this->doc->transitions;
292  if ($addorrem == "remove") {
293  unset($transitions[$key]);
294  $cycles = $this->doc->cycle; // delete also transition which use these transition type
295  foreach ($cycles as $k => $v) {
296  if ($v["t"] == $key) unset($cycles[$k]);
297  }
298  $ncy = "array(";
299  foreach ($cycles as $k => $v) {
300  $ncy.= sprintf('array("e1"=>"%s", "e2"=>"%s", "t"=>"%s"),' . "\n", $v["e1"], $v["e2"], $v["t"]);
301  }
302  $ncy = substr($ncy, 0, -2) . ")"; //delete end comma and cr
303  $this->doc->cycle = $cycles;
304  } else {
305  if ($addorrem == "modify") {
306  $tkey = $transitions[$key]; // conserve last values is not new set
307  if ($ask === null) $ask = $tkey["ask"];
308  if ($preMethod === null) $preMethod = $tkey["m1"];
309  if ($postMethod === null) $postMethod = $tkey["m2"];
310  if ($noComment === null) $noComment = $tkey["nr"];
311  }
312  $transitions[$key] = array(
313  "ask" => $ask,
314  "m1" => $preMethod,
315  "m2" => $postmethod,
316  "nr" => $noComment
317  );
318  }
319  if ($label) {
320  $this->addTraduction('fr', $key, $label);
321  }
322  $fam = $this->doc->getFamDoc();
323  $classname = $fam->classname;
324  $file = sprintf("%s/FDL/Class.%s.php", DEFAULT_PUBDIR, $classname);
325  if (file_exists($file)) {
326  if (!is_writable($file)) {
327  $this->doc->unlock(true);
328  $this->setError(sprintf(_("workflow file %s is not writable") , $file));
329  return null;
330  }
331  $na = "array(";
332  foreach ($transitions as $k => $v) {
333  if (is_array($v["ask"])) {
334  $sax = $v["ask"];
335  $v["ask"] = 'array(';
336  foreach ($sax as $ax) {
337  $v["ask"].= "'" . $ax . "',";
338  }
339  $v["ask"] = substr($v["ask"], 0, -1) . ")"; //delete end comma
340 
341  } else $v["ask"] = "null";
342  $na.= sprintf('"%s"=>array("m1"=>"%s", "m2"=>"%s", "ask"=>%s, "nr"=>%s),' . "\n", $k, $v["m1"], $v["m2"], $v["ask"], $v["nr"] ? "true" : "false");
343  }
344  $na = substr($na, 0, -2) . ")"; //delete end comma and cr
345  $content = file_get_contents($file);
346  $nc = preg_replace('/\$transitions\s*=([^;]*);/i', "\$transitions=$na;", $content);
347  if ($ncy) $nc = preg_replace('/\$cycle\s*=([^;]*);/i', "\$cycle=$ncy;", $nc);
348 
349  file_put_contents($file, $nc);
350  $this->doc->transitions = $transitions;
351  $wsh = getWshCmd();
352  $cmd = $wsh . " --api=fdl_adoc --docid=" . intval($this->doc->fromid);
353  $log = exec($cmd, $out, $ret);
354  if ($err) $this->setError($err);
355  //print "<pre>".htmlentities($nc)."</pre>";
356 
357  }
358 
359  $err = $this->doc->unlock(true);
360  } else {
361  $this->setError(_("alterTransitiontype: missing parameter"));
362  return null;
363  }
364  }
365  }
366 }
367 ?>
← centre documentaire © anakeen - published under CC License - Dynacase