source: branches/1.2/workflow/inc/engine/src/API/BaseActivity.php @ 1349

Revision 1349, 13.5 KB checked in by niltonneto, 15 years ago (diff)

Ticket #561 - Inclusão do módulo Workflow faltante nessa versão.

  • Property svn:executable set to *
Line 
1<?php
2require_once (GALAXIA_LIBRARY.SEP.'src'.SEP.'common'.SEP.'Base.php');
3/**
4 * Represents activities, and must be derived for each activity type supported in the system.
5 * Derived activities extending this class can be found in the activities subfolder.
6 * This class is observable.
7 *
8 * @package Galaxia
9 * @license http://www.gnu.org/copyleft/gpl.html GPL
10 */
11class BaseActivity extends Base {
12  /**
13   * @var string $name Activity's name
14   * @access public
15   */
16  var $name;
17  /**
18   * @var string $normalizedName Activity's normalized (follows a pattern) name
19   * @access public
20   */ 
21  var $normalizedName;
22  /**
23   * @var string $description Activity's description
24   * @access public
25   */ 
26  var $description;
27  /**
28   * @var string $menuPath Activity's menu path
29   * @access public
30   */ 
31  var $menuPath;
32  /**
33   * @var bool $isInteractive
34   * @access public
35   */ 
36  var $isInteractive;
37  /**
38   * @var bool $isAutoRouted
39   * @access public
40   */ 
41  var $isAutoRouted;
42  /**
43   * @var array $roles
44   * @access public
45   */
46  var $roles=Array();
47  /**
48   * @var array $outbound
49   * @access public
50   */
51  var $outbound=Array();
52  /**
53   * @var array $inbound
54   * @access public
55   */
56  var $inbound=Array();
57  /**
58   * @var int $pId Process's identification number
59   * @access public
60   */
61  var $pId;
62  /**
63   * @var int $activityId Activity's identification number
64   * @access public
65   */ 
66  var $activityId;
67  /**
68   * @var string $type Activity's type
69   * @access public
70   */
71  var $type;
72  /**
73   * @var string  $defaultUser
74   * @access public
75   */
76  var $defaultUser='*';
77  /**
78   * @var array $agents
79   * @access public
80   */ 
81  var $agents=Array();
82 
83  /**
84   * Seems to be the rest of a bad object architecture
85   *
86   * @deprecated
87   */
88  function setDb(&$db)
89  {
90    $this->db =& $db;
91  }
92 
93  /**
94   * Constructor of the BaseActivity Object
95   *
96   * @param object $db ADODB object
97   * @access public
98   */
99  function BaseActivity(&$db)
100  {
101    $this->type='base';
102    $this->child_name = 'BaseActivity';
103    parent::Base($db);
104  }
105
106  /**
107   * Factory method returning an activity of the desired type, loading the information from the database
108   * and populating the activity object with datas related to his activity type (being more than a BaseActivity then
109   *
110   * @param int $activityId it is the id of the wanted activity
111   * @param bool $with_roles true by default, gives you the basic roles information in the result
112   * @param bool $with_agents false by default, gives you the basic agents information in the result
113   * @param bool $as_array false by default, if true the function will return an array instead of an object
114   * @return object Activity of the right class (Child class) or an associative array containing the activity
115   * information if $as_array is set to true
116   * @access public 
117   */
118  function &getActivity($activityId, $with_roles= true,$with_agents=false,$as_array=false)
119  {
120    $query = "select * from `".GALAXIA_TABLE_PREFIX."activities` where `wf_activity_id`=?";
121    $result = $this->query($query,array($activityId));
122    if(!$result || !$result->numRows() ) return false;
123    $res = $result->fetchRow();
124    switch($res['wf_type']) {
125      case 'start':
126        require_once(GALAXIA_LIBRARY.SEP.'src'.SEP.'API'.SEP.'activities'.SEP.'Start.php');
127        $act = new Start($this->db); 
128        break;
129      case 'end':
130        require_once(GALAXIA_LIBRARY.SEP.'src'.SEP.'API'.SEP.'activities'.SEP.'End.php');
131        $act = new End($this->db);
132        break;
133      case 'join':
134        require_once(GALAXIA_LIBRARY.SEP.'src'.SEP.'API'.SEP.'activities'.SEP.'Join.php');
135        $act = new Join($this->db);
136        break;
137      case 'split':
138        require_once(GALAXIA_LIBRARY.SEP.'src'.SEP.'API'.SEP.'activities'.SEP.'Split.php');
139        $act = new Split($this->db);
140        break;
141      case 'standalone':
142        require_once(GALAXIA_LIBRARY.SEP.'src'.SEP.'API'.SEP.'activities'.SEP.'Standalone.php');
143        $act = new Standalone($this->db);
144        break;
145      case 'view':
146        require_once(GALAXIA_LIBRARY.SEP.'src'.SEP.'API'.SEP.'activities'.SEP.'View.php');
147        $act = new View($this->db);
148        break;
149      case 'switch':
150        require_once(GALAXIA_LIBRARY.SEP.'src'.SEP.'API'.SEP.'activities'.SEP.'SwitchActivity.php');
151        $act = new SwitchActivity($this->db);
152        break;
153      case 'activity':
154        require_once(GALAXIA_LIBRARY.SEP.'src'.SEP.'API'.SEP.'activities'.SEP.'Activity.php');
155        $act = new Activity($this->db);
156        break;
157      default:
158        trigger_error('Unknown activity type:'.$res['wf_type'],E_USER_WARNING);
159    }
160   
161    $act->setName($res['wf_name']);
162    $act->setProcessId($res['wf_p_id']);
163    $act->setNormalizedName($res['wf_normalized_name']);
164    $act->setDescription($res['wf_description']);
165    $act->setMenuPath($res['wf_menu_path']);
166    $act->setIsInteractive($res['wf_is_interactive']);
167    $act->setIsAutoRouted($res['wf_is_autorouted']);
168    $act->setActivityId($res['wf_activity_id']);
169    $act->setType($res['wf_type']);
170    $act->setDefaultUser($res['wf_default_user']);
171   
172    //Now get forward transitions
173   
174    //Now get backward transitions
175   
176    //Now get roles
177    if ($with_roles)
178    {
179      $query = "select `wf_role_id` from `".GALAXIA_TABLE_PREFIX."activity_roles` where `wf_activity_id`=?";
180      $result=$this->query($query,array($activityId));
181      if (!(empty($result)))
182      {
183        while($res = $result->fetchRow())
184        {
185          $this->roles[] = $res['wf_role_id'];
186        }
187      }
188      $act->setRoles($this->roles);
189    }
190   
191    //Now get agents if asked so
192    if ($with_agents)
193    {
194      $query = "select wf_agent_id, wf_agent_type from ".GALAXIA_TABLE_PREFIX."activity_agents where wf_activity_id=?";
195      $result=$this->query($query,array($activityId));
196      if (!(empty($result)))
197      {
198        while($res = $result->fetchRow())
199        {
200          $this->agents[] = array(
201              'wf_agent_id'     => $res['wf_agent_id'],
202              'wf_agent_type'   => $res['wf_agent_type'],
203            );
204        }
205      }
206      $act->setAgents($this->agents);
207    }
208
209    if ($as_array)
210    {//we wont return the object but an associative array instead
211       $res['wf_name']=$act->getName();
212       $res['wf_normalized_name']=$act->getNormalizedName();
213       $res['wf_description']=$act->getDescription();
214       $res['wf_menu_path']=$act->getMenuPath();
215       $res['wf_is_interactive']=$act->isInteractive();
216       $res['wf_is_autorouted']=$act->isAutoRouted();
217       $res['wf_roles']=$act->getRoles();
218       //$res['outbound']=$act->get();
219       //$res['inbound']=$act->get();
220       $res['wf_p_id']=$act->getProcessId();
221       $res['wf_activity_id']=$act->getActivityId();
222       $res['wf_type']=$act->getType();
223       $res['wf_default_user']=$act->getDefaultUser();
224       $res['wf_agents']= $act->getAgents();
225       return $res;
226    }
227    else
228    {
229      return $act;
230    }
231  }
232 
233  /**
234   * Gets performed roles for a given user
235   *
236   * @param array $user
237   * @return array RoleIds for the given user
238   * @access public
239   */
240  function getUserRoles($user) {
241   
242    // retrieve user_groups information in an array containing all groups for this user
243    $user_groups = galaxia_retrieve_user_groups($GLOBALS['phpgw_info']['user']['account_id'] );
244    // and append it to query                     
245    $query = 'select `wf_role_id` from `'.GALAXIA_TABLE_PREFIX."user_roles`
246          where (
247            (wf_user=? and wf_account_type='u')";
248    if (is_array($groups))
249    {
250      foreach ($groups as &$group)
251        $group = "'{$group}'";
252      $mid .= ' or (wf_user in ('.implode(',',$groups).") and wf_account_type='g')";
253    }
254    $mid .= ')';
255
256    $result=$this->query($query,array($user));
257    $ret = Array();
258    while($res = $result->fetchRow())
259    {
260      $ret[] = $res['wf_role_id'];
261    }
262    return $ret;
263  }
264
265  /**
266   * Gets activity's roleId and name
267   *
268   * @return array $ret Array of associative arrays with roleId and name
269   * @access public
270   */
271  function getActivityRoleNames() {
272    $aid = $this->activityId;
273    $query = "select gr.`wf_role_id`, `wf_name` from `".GALAXIA_TABLE_PREFIX."activity_roles` gar, `".GALAXIA_TABLE_PREFIX."roles` gr where gar.`wf_role_id`=gr.`wf_role_id` and gar.`wf_activity_id`=?";
274    $result=$this->query($query,array($aid));
275    $ret = Array();
276    while($res = $result->fetchRow()) {
277      $ret[] = $res;
278    }
279    return $ret;
280  }
281 
282  /**
283   * Returns the normalized name for the activity
284   *
285   * @return string
286   * @access public
287   */
288  function getNormalizedName() {
289    return $this->normalizedName;
290  }
291
292  /**
293   * Sets normalized name for the activity
294   *
295   * @return void
296   * @access public
297   */ 
298  function setNormalizedName($name) {
299    $this->normalizedName=$name;
300  }
301 
302  /**
303   * Sets the name for the activity
304   *
305   * @param string New desired activity's name
306   * @return void
307   * @access public
308   */
309  function setName($name) {
310    $this->name=$name;
311  }
312 
313  /**
314   * Gets the activity name
315   *
316   * @return void
317   * @access public
318   */
319  function getName() {
320    return $this->name;
321  }
322
323  /**
324   * Sets the agents for the activity object (no save)
325   *
326   * @param array $agents Has 'wf_agent_id' and 'wf_agent_type' as keys
327   * @return bool False if any problem is detected
328   * @access public
329   */
330  function setAgents($agents)
331  {
332    if (!(is_array($agents)))
333    {
334      $this->error[] = tra('bad parameter for setAgents, the parameter should be an array');
335      return false;
336    }
337    $this->agents = $agents;
338  }
339 
340  /**
341   * Gets the activity agents
342   *
343   * @return array Basic agents informations (id an type) or false if no agent is defined for this activity
344   * @access public
345   */
346  function getAgents()
347  {
348    if (empty($this->agents)) return false;
349    return $this->agents;
350  }
351 
352  /**
353   * Sets the activity description
354   *
355   * @param string $desc Activity description
356   * @return void
357   * @access public
358   */
359  function setDescription($desc) {
360    $this->description=$desc;
361  }
362
363  /**
364   * Gets the activity description
365   *
366   * @return string
367   * @access public
368   */ 
369  function getDescription() {
370    return $this->description;
371  }
372
373  /**
374   * Sets the activity menu path
375   *
376   * @param string $mp Menu path
377   * @return void
378   * @access public
379   */
380  function setMenuPath($mp) {
381    $this->menuPath=$mp;
382  }
383
384  /**
385   * Gets the activity menu path
386   *
387   * @return string
388   * @access public
389   */ 
390  function getMenuPath() {
391    return $this->menuPath;
392  }
393 
394  /**
395   * Sets the type for the activity although it does NOT allow you to change the current type
396   *
397   * @param string $type
398   * @return void
399   * @access public
400   */
401  function setType($type) {
402    $this->type=$type;
403  }
404 
405  /**
406   * Gets the activity type
407   *
408   * @return string
409   * @access public
410   */
411  function getType() {
412    return $this->type;
413  }
414
415  /**
416   * Sets if the activity is interactive
417   *
418   * @param bool $is
419   * @return void
420   * @access public
421   */
422  function setIsInteractive($is) {
423    $this->isInteractive=$is;
424  }
425 
426  /**
427   * Returns if the activity is interactive
428   *
429   * @return string
430   * @access public
431   */
432  function isInteractive() {
433    return $this->isInteractive == 'y';
434  }
435 
436  /**
437   * Sets if the activity is auto-routed
438   *
439   * @param boolean $is
440   * @return void
441   * @access public
442   */
443  function setIsAutoRouted($is) {
444    $this->isAutoRouted = $is;
445  }
446 
447  /**
448   * Gets if the activity is auto routed
449   *
450   * @return string
451   * @access public
452   */
453  function isAutoRouted() {
454    return $this->isAutoRouted == 'y';
455  }
456
457  /**
458   * Sets the processId for this activity
459   *
460   * @param int $pid
461   * @return void
462   * @access public
463   */
464  function setProcessId($pid) {
465    $this->pId=$pid;
466  }
467 
468  /**
469   * Gets the processId for this activity
470   *
471   * @return int
472   * @access public
473   */
474  function getProcessId() {
475    return $this->pId;
476  }
477
478  /**
479   * Gets the activityId
480   *
481   * @return int
482   * @access public
483   */
484  function getActivityId() {
485    return $this->activityId;
486  } 
487 
488  /**
489   * Sets the activityId
490   *
491   * @param int $id
492   * @return void
493   * @access public
494   */
495  function setActivityId($id) {
496    $this->activityId=$id;
497  }
498 
499  /**
500   * Gets array with roleIds asociated to this activity
501   *
502   * @return array
503   * @access public
504   */
505  function getRoles() {
506    return $this->roles;
507  }
508 
509  /**
510   * Sets roles for this activities, should receive an array of roleIds
511   *
512   * @param array $roles
513   * @return void
514   * @access public
515   */
516  function setRoles($roles) {
517    $this->roles = $roles;
518  }
519
520  /**
521   * Gets default user id associated with this activity as he's recorded
522   * there's no check about validity of this user
523   *
524   * @return string
525   * @access protected
526   */
527  function getDefaultUser() {
528    return $this->defaultUser;
529  }
530
531  /**
532   * Sets the default user for an activity
533   *
534   * @param string $default_user
535   * @return void
536   * @access public
537   */
538  function setDefaultUser($default_user)
539  {
540    if ((!isset($default_user)) || ($default_user=='') || ($default_user==false))
541    {
542      $default_user='*';
543    }
544    $this->defaultUser = $default_user;
545  }
546
547   /**
548   * Checks if a user has a certain role (by name) for this activity,
549   * e.g. $isadmin = $activity->checkUserRole($user,'admin')
550   *
551   * @deprecated Unused function. Old API, do not use it. Return always false
552   */
553  function checkUserRole($user,$rolename)
554  {
555    $this->error[] = 'use of an old deprecated function checkUserRole, return always false';
556    return false;
557  }
558
559}
560?>
Note: See TracBrowser for help on using the repository browser.