source: sandbox/workflow/trunk/inc/engine/src/API/BaseActivity.php @ 2372

Revision 2372, 12.8 KB checked in by pedroerp, 14 years ago (diff)

Ticket #609 - Merged 2197:2356 /sandbox/workflow/branches/609/ em /sandbox/workflow/trunk.

  • 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()
100  {
101    $this->type='base';
102    $this->child_name = 'BaseActivity';
103    parent::Base();
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
125    switch($res['wf_type']) {
126      case 'start':
127        $act = &Factory::newInstance('Start');
128        break;
129
130      case 'end':
131        $act = &Factory::newInstance('End');
132        break;
133
134      case 'join':
135        $act = &Factory::newInstance('Join');
136        break;
137
138      case 'split':
139        $act = &Factory::newInstance('Split');
140        break;
141
142      case 'standalone':
143        $act = &Factory::newInstance('Standalone');
144        break;
145
146      case 'view':
147        $act = &Factory::newInstance('View');
148        break;
149
150      case 'switch':
151        $act = &Factory::newInstance('SwitchActivity');
152        break;
153
154      case 'activity':
155        $act = &Factory::newInstance('Activity');
156        break;
157
158      default:
159        trigger_error('Unknown activity type:'.$res['wf_type'],E_USER_WARNING);
160    }
161
162    $act->setName($res['wf_name']);
163    $act->setProcessId($res['wf_p_id']);
164    $act->setNormalizedName($res['wf_normalized_name']);
165    $act->setDescription($res['wf_description']);
166    $act->setMenuPath($res['wf_menu_path']);
167    $act->setIsInteractive($res['wf_is_interactive']);
168    $act->setIsAutoRouted($res['wf_is_autorouted']);
169    $act->setActivityId($res['wf_activity_id']);
170    $act->setType($res['wf_type']);
171    $act->setDefaultUser($res['wf_default_user']);
172   
173    //Now get forward transitions
174   
175    //Now get backward transitions
176   
177    //Now get roles
178    if ($with_roles)
179    {
180      $query = "select `wf_role_id` from `".GALAXIA_TABLE_PREFIX."activity_roles` where `wf_activity_id`=?";
181      $result=$this->query($query,array($activityId));
182      if (!(empty($result)))
183      {
184        while($res = $result->fetchRow())
185        {
186          $this->roles[] = $res['wf_role_id'];
187        }
188      }
189      $act->setRoles($this->roles);
190    }
191   
192    //Now get agents if asked so
193    if ($with_agents)
194    {
195      $query = "select wf_agent_id, wf_agent_type from ".GALAXIA_TABLE_PREFIX."activity_agents where wf_activity_id=?";
196      $result=$this->query($query,array($activityId));
197      if (!(empty($result)))
198      {
199        while($res = $result->fetchRow())
200        {
201          $this->agents[] = array(
202              'wf_agent_id'     => $res['wf_agent_id'],
203              'wf_agent_type'   => $res['wf_agent_type'],
204            );
205        }
206      }
207      $act->setAgents($this->agents);
208    }
209
210    if ($as_array)
211    {//we wont return the object but an associative array instead
212       $res['wf_name']=$act->getName();
213       $res['wf_normalized_name']=$act->getNormalizedName();
214       $res['wf_description']=$act->getDescription();
215       $res['wf_menu_path']=$act->getMenuPath();
216       $res['wf_is_interactive']=$act->isInteractive();
217       $res['wf_is_autorouted']=$act->isAutoRouted();
218       $res['wf_roles']=$act->getRoles();
219       //$res['outbound']=$act->get();
220       //$res['inbound']=$act->get();
221       $res['wf_p_id']=$act->getProcessId();
222       $res['wf_activity_id']=$act->getActivityId();
223       $res['wf_type']=$act->getType();
224       $res['wf_default_user']=$act->getDefaultUser();
225       $res['wf_agents']= $act->getAgents();
226       return $res;
227    }
228    else
229    {
230      return $act;
231    }
232  }
233 
234  /**
235   * Gets performed roles for a given user
236   *
237   * @param array $user
238   * @return array RoleIds for the given user
239   * @access public
240   */
241  function getUserRoles($user) {
242   
243    // retrieve user_groups information in an array containing all groups for this user
244    $user_groups = galaxia_retrieve_user_groups($GLOBALS['phpgw_info']['user']['account_id'] );
245    // and append it to query                     
246    $query = 'select `wf_role_id` from `'.GALAXIA_TABLE_PREFIX."user_roles`
247          where (
248            (wf_user=? and wf_account_type='u')";
249    if (is_array($groups))
250    {
251      foreach ($groups as &$group)
252        $group = "'{$group}'";
253      $mid .= ' or (wf_user in ('.implode(',',$groups).") and wf_account_type='g')";
254    }
255    $mid .= ')';
256
257    $result=$this->query($query,array($user));
258    $ret = Array();
259    while($res = $result->fetchRow())
260    {
261      $ret[] = $res['wf_role_id'];
262    }
263    return $ret;
264  }
265
266  /**
267   * Gets activity's roleId and name
268   *
269   * @return array $ret Array of associative arrays with roleId and name
270   * @access public
271   */
272  function getActivityRoleNames() {
273    $aid = $this->activityId;
274    $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`=?";
275    $result=$this->query($query,array($aid));
276    $ret = Array();
277    while($res = $result->fetchRow()) {
278      $ret[] = $res;
279    }
280    return $ret;
281  }
282 
283  /**
284   * Returns the normalized name for the activity
285   *
286   * @return string
287   * @access public
288   */
289  function getNormalizedName() {
290    return $this->normalizedName;
291  }
292
293  /**
294   * Sets normalized name for the activity
295   *
296   * @return void
297   * @access public
298   */ 
299  function setNormalizedName($name) {
300    $this->normalizedName=$name;
301  }
302 
303  /**
304   * Sets the name for the activity
305   *
306   * @param string New desired activity's name
307   * @return void
308   * @access public
309   */
310  function setName($name) {
311    $this->name=$name;
312  }
313 
314  /**
315   * Gets the activity name
316   *
317   * @return void
318   * @access public
319   */
320  function getName() {
321    return $this->name;
322  }
323
324  /**
325   * Sets the agents for the activity object (no save)
326   *
327   * @param array $agents Has 'wf_agent_id' and 'wf_agent_type' as keys
328   * @return bool False if any problem is detected
329   * @access public
330   */
331  function setAgents($agents)
332  {
333    if (!(is_array($agents)))
334    {
335      $this->error[] = tra('bad parameter for setAgents, the parameter should be an array');
336      return false;
337    }
338    $this->agents = $agents;
339  }
340 
341  /**
342   * Gets the activity agents
343   *
344   * @return array Basic agents informations (id an type) or false if no agent is defined for this activity
345   * @access public
346   */
347  function getAgents()
348  {
349    if (empty($this->agents)) return false;
350    return $this->agents;
351  }
352 
353  /**
354   * Sets the activity description
355   *
356   * @param string $desc Activity description
357   * @return void
358   * @access public
359   */
360  function setDescription($desc) {
361    $this->description=$desc;
362  }
363
364  /**
365   * Gets the activity description
366   *
367   * @return string
368   * @access public
369   */ 
370  function getDescription() {
371    return $this->description;
372  }
373
374  /**
375   * Sets the activity menu path
376   *
377   * @param string $mp Menu path
378   * @return void
379   * @access public
380   */
381  function setMenuPath($mp) {
382    $this->menuPath=$mp;
383  }
384
385  /**
386   * Gets the activity menu path
387   *
388   * @return string
389   * @access public
390   */ 
391  function getMenuPath() {
392    return $this->menuPath;
393  }
394 
395  /**
396   * Sets the type for the activity although it does NOT allow you to change the current type
397   *
398   * @param string $type
399   * @return void
400   * @access public
401   */
402  function setType($type) {
403    $this->type=$type;
404  }
405 
406  /**
407   * Gets the activity type
408   *
409   * @return string
410   * @access public
411   */
412  function getType() {
413    return $this->type;
414  }
415
416  /**
417   * Sets if the activity is interactive
418   *
419   * @param bool $is
420   * @return void
421   * @access public
422   */
423  function setIsInteractive($is) {
424    $this->isInteractive=$is;
425  }
426 
427  /**
428   * Returns if the activity is interactive
429   *
430   * @return string
431   * @access public
432   */
433  function isInteractive() {
434    return $this->isInteractive == 'y';
435  }
436 
437  /**
438   * Sets if the activity is auto-routed
439   *
440   * @param boolean $is
441   * @return void
442   * @access public
443   */
444  function setIsAutoRouted($is) {
445    $this->isAutoRouted = $is;
446  }
447 
448  /**
449   * Gets if the activity is auto routed
450   *
451   * @return string
452   * @access public
453   */
454  function isAutoRouted() {
455    return $this->isAutoRouted == 'y';
456  }
457
458  /**
459   * Sets the processId for this activity
460   *
461   * @param int $pid
462   * @return void
463   * @access public
464   */
465  function setProcessId($pid) {
466    $this->pId=$pid;
467  }
468 
469  /**
470   * Gets the processId for this activity
471   *
472   * @return int
473   * @access public
474   */
475  function getProcessId() {
476    return $this->pId;
477  }
478
479  /**
480   * Gets the activityId
481   *
482   * @return int
483   * @access public
484   */
485  function getActivityId() {
486    return $this->activityId;
487  } 
488 
489  /**
490   * Sets the activityId
491   *
492   * @param int $id
493   * @return void
494   * @access public
495   */
496  function setActivityId($id) {
497    $this->activityId=$id;
498  }
499 
500  /**
501   * Gets array with roleIds asociated to this activity
502   *
503   * @return array
504   * @access public
505   */
506  function getRoles() {
507    return $this->roles;
508  }
509 
510  /**
511   * Sets roles for this activities, should receive an array of roleIds
512   *
513   * @param array $roles
514   * @return void
515   * @access public
516   */
517  function setRoles($roles) {
518    $this->roles = $roles;
519  }
520
521  /**
522   * Gets default user id associated with this activity as he's recorded
523   * there's no check about validity of this user
524   *
525   * @return string
526   * @access protected
527   */
528  function getDefaultUser() {
529    return $this->defaultUser;
530  }
531
532  /**
533   * Sets the default user for an activity
534   *
535   * @param string $default_user
536   * @return void
537   * @access public
538   */
539  function setDefaultUser($default_user)
540  {
541    if ((!isset($default_user)) || ($default_user=='') || ($default_user==false))
542    {
543      $default_user='*';
544    }
545    $this->defaultUser = $default_user;
546  }
547
548   /**
549   * Checks if a user has a certain role (by name) for this activity,
550   * e.g. $isadmin = $activity->checkUserRole($user,'admin')
551   *
552   * @deprecated Unused function. Old API, do not use it. Return always false
553   */
554  function checkUserRole($user,$rolename)
555  {
556    $this->error[] = 'use of an old deprecated function checkUserRole, return always false';
557    return false;
558  }
559
560}
561?>
Note: See TracBrowser for help on using the repository browser.