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

Revision 1349, 50.9 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 require_once(GALAXIA_LIBRARY.SEP.'src'.SEP.'common'.SEP.'Base.php');
2/**
3 * Provides methods for use in typical user interface scripts
4 *
5 * @package Galaxia
6 * @license http://www.gnu.org/copyleft/gpl.html GPL
7 * @todo More options in list_user_instances, they should not be added by the external modules 
8 */
9class GUI extends Base {
10
11  /**
12   * @var object $wf_cecurity Used to obtain access for the user on certain actions from the engine
13   * @access public
14   */
15  var $wf_security;
16  /**
17   * @var object  $pm Process manager object used to retrieve infos from processes
18   * @access public
19   */
20  var $pm;
21  /**
22   * @var array $process_cache Cache to avoid queries
23   * @access public
24   */
25  var $process_cache=Array();
26
27  /**
28   * Constructor
29   *
30   * @param object &$db ADOdb
31   * @return object GUI instance
32   * @access public
33   */
34  function GUI(&$db)
35  {
36    $this->child_name = 'GUI';
37    parent::Base($db);
38    require_once(GALAXIA_LIBRARY.SEP.'src'.SEP.'common'.SEP.'WfSecurity.php');
39    $this->wf_security =& new WfSecurity($this->db);
40  }
41
42  /**
43   * Collects errors from all linked objects which could have been used by this object.
44   * Each child class should instantiate this function with her linked objetcs, calling get_error(true)
45   *
46   * @param bool $debug False by default, if true debug messages can be added to 'normal' messages
47   * @param string $prefix Appended to the debug message
48   * @access public
49   * @return void
50   */
51  function collect_errors($debug=false, $prefix='')
52  {
53    parent::collect_errors($debug, $prefix);
54    $this->error[] = $this->wf_security->get_error(false, $debug, $prefix);
55  }
56
57   /**
58    * List user processes, user processes should follow one of these conditions:
59    * 1) The process has an instance assigned to the user
60    * 2) The process has a begin activity with a role compatible to the user roles
61    * 3) The process has an instance assigned to '*' and the roles for the activity match the roles assigned to the user
62    *
63    * @param int $user Current user id
64    * @param int $offset Current starting point for the query results
65    * @param int $maxRecords Max number of results to return
66    * @param string $sort_mode For sorting
67    * @param string $find Search in activity name or description
68    * @param string $where Deprecated it's a string to add to the query, use with care for SQL injection
69    * @access public
70    * @return array List of processes that match this and it also returns the number of instances that are in the process matching the conditions
71    */
72  function gui_list_user_processes($user,$offset,$maxRecords,$sort_mode,$find,$where='')
73  {
74    // FIXME: this doesn't support multiple sort criteria
75    //$sort_mode = $this->convert_sortmode($sort_mode);
76    $sort_mode = str_replace("__"," ",$sort_mode);
77
78    $mid = "where gp.wf_is_active=?";
79    // add group mapping, warning groups and user can have the same id
80    $groups = galaxia_retrieve_user_groups($user);
81    $mid .= " and ((gur.wf_user=? and gur.wf_account_type='u')";
82    if (is_array($groups))
83    {
84      foreach ($groups as &$group)
85        $group = "'{$group}'";
86      $mid .= ' or (gur.wf_user in ('.implode(',',$groups).") and gur.wf_account_type='g')";
87    }
88    $mid .= ')';
89    $bindvars = array('y',$user);
90    if($find) {
91      $findesc = '%'.$find.'%';
92      $mid .= " and ((gp.wf_name like ?) or (gp.wf_description like ?))";
93      $bindvars[] = $findesc;
94      $bindvars[] = $findesc;
95    }
96    if($where) {
97      $mid.= " and ($where) ";
98    }
99   
100    $query = "select distinct(gp.wf_p_id),
101                     gp.wf_is_active,                   
102                     gp.wf_name as wf_procname,
103                     gp.wf_normalized_name as normalized_name,
104                     gp.wf_version as wf_version,
105                     gp.wf_version as version
106              from ".GALAXIA_TABLE_PREFIX."processes gp
107                INNER JOIN ".GALAXIA_TABLE_PREFIX."activities ga ON gp.wf_p_id=ga.wf_p_id
108                INNER JOIN ".GALAXIA_TABLE_PREFIX."activity_roles gar ON gar.wf_activity_id=ga.wf_activity_id
109                INNER JOIN ".GALAXIA_TABLE_PREFIX."roles gr ON gr.wf_role_id=gar.wf_role_id
110                INNER JOIN ".GALAXIA_TABLE_PREFIX."user_roles gur ON gur.wf_role_id=gr.wf_role_id
111              $mid";
112    $query_cant = "select count(distinct(gp.wf_p_id))
113              from ".GALAXIA_TABLE_PREFIX."processes gp
114                INNER JOIN ".GALAXIA_TABLE_PREFIX."activities ga ON gp.wf_p_id=ga.wf_p_id
115                INNER JOIN ".GALAXIA_TABLE_PREFIX."activity_roles gar ON gar.wf_activity_id=ga.wf_activity_id
116                INNER JOIN ".GALAXIA_TABLE_PREFIX."roles gr ON gr.wf_role_id=gar.wf_role_id
117                INNER JOIN ".GALAXIA_TABLE_PREFIX."user_roles gur ON gur.wf_role_id=gr.wf_role_id
118              $mid";
119    $result = $this->query($query,$bindvars,$maxRecords,$offset, true, $sort_mode);
120    $cant = $this->getOne($query_cant,$bindvars);
121    $ret = Array();
122    if (!(empty($result)))
123    {
124      while($res = $result->fetchRow()) {
125        // Get instances and activities per process,
126        $pId=$res['wf_p_id'];
127        $query_act = 'select count(distinct(ga.wf_activity_id))
128              from '.GALAXIA_TABLE_PREFIX.'processes gp
129                INNER JOIN '.GALAXIA_TABLE_PREFIX.'activities ga ON gp.wf_p_id=ga.wf_p_id
130                INNER JOIN '.GALAXIA_TABLE_PREFIX.'activity_roles gar ON gar.wf_activity_id=ga.wf_activity_id
131                INNER JOIN '.GALAXIA_TABLE_PREFIX.'roles gr ON gr.wf_role_id=gar.wf_role_id
132                INNER JOIN '.GALAXIA_TABLE_PREFIX."user_roles gur ON gur.wf_role_id=gr.wf_role_id
133              where gp.wf_p_id=?
134              and (  ((gur.wf_user=? and gur.wf_account_type='u') ";
135        if (is_array($groups))
136        {
137          $query_act .= ' or (gur.wf_user in ('.implode(',',$groups).") and gur.wf_account_type='g')";
138        }
139        $query_act .= '))';
140         
141        $res['wf_activities']=$this->getOne($query_act,array($pId,$user));
142        //we are counting here instances which are completed/exception or actives
143        // TODO: maybe we should add a second counter with only running instances
144        $query_inst = 'select count(distinct(gi.wf_instance_id))
145              from '.GALAXIA_TABLE_PREFIX.'instances gi
146                INNER JOIN '.GALAXIA_TABLE_PREFIX.'instance_activities gia ON gi.wf_instance_id=gia.wf_instance_id
147                LEFT JOIN '.GALAXIA_TABLE_PREFIX.'activity_roles gar ON gia.wf_activity_id=gar.wf_activity_id
148                LEFT JOIN '.GALAXIA_TABLE_PREFIX."user_roles gur ON gar.wf_role_id=gur.wf_role_id
149              where gi.wf_p_id=?
150              and (";
151        if (is_array($groups))
152        {
153          $query_inst .= "(gur.wf_user in (".implode(",",$groups).") and gur.wf_account_type='g') or ";
154        }
155        $query_inst .= "(gi.wf_owner=?)
156                         or ((gur.wf_user=?) and gur.wf_account_type='u'))";
157        $res['wf_instances']=$this->getOne($query_inst,array($pId,$user,$user));
158        $ret[] = $res;
159      }
160    }
161    $retval = Array();
162    $retval["data"] = $ret;
163    $retval["cant"] = $cant;
164    return $retval;
165  }
166
167  /**
168   * Gets user activities
169   *
170   * @param int $user Current user id
171   * @param int $offset Current starting point for the query results
172   * @param int $maxRecords Max number of results to return
173   * @param string $sort_mode For sorting
174   * @param string $find Search in activity name or description
175   * @param string $where Deprecated it's a string to add to the query, use with care for SQL injection
176   * @param bool $remove_activities_without_instances False by default will remove all activities having no instances related at this time
177   * @param bool $remove_instances_activities False by default, if true then all activities related to instances will be avoided (i.e. activities which are not standalone, start or view). If $remove_activities_without_instances is true you'll obtain nothing :-)
178   * @param bool $add_start False by default, if true start activities are added to the listing, no effect if $remove_activities_without_instances is true
179   * @param bool $add_standalone False by default, if true standalone activities are added to the listing, no effect if $remove_activities_without_instances is true
180   * @param bool $add_view False by default, if true view activities are added to the listing, no effect if $remove_activities_without_instances is true
181   * @return array Associative, key cant gives the number of results, key data is an associative array conteining the results
182   * @access public
183   */
184  function gui_list_user_activities($user,$offset,$maxRecords,$sort_mode,$find,$where='', $remove_activities_without_instances=false, $remove_instances_activities =false, $add_start = false, $add_standalone = false, $add_view = false)
185  {
186    // FIXME: this doesn't support multiple sort criteria
187    //$sort_mode = $this->convert_sortmode($sort_mode);
188    $sort_mode = str_replace("__"," ",$sort_mode);
189    $mid = "where gp.wf_is_active=?";
190    $bindvars = array('y');
191   
192    if ($remove_instances_activities)
193    {
194      $mid .= " and ga.wf_type <> ? and ga.wf_type <> ? and ga.wf_type <> ?  and  ga.wf_type <> ?  and  ga.wf_type <> ? ";
195      $bindvars[] = 'end';
196      $bindvars[] = 'switch';
197      $bindvars[] = 'join';
198      $bindvars[] = 'activity';
199      $bindvars[] = 'split';
200    }
201    if (!($add_start))
202    {
203      $mid .= " and ga.wf_type <> ?";
204      $bindvars[] = 'start';
205    }
206    if (!($add_standalone))
207    {
208      $mid .= " and ga.wf_type <> ?";
209      $bindvars[] = 'standalone';
210    }
211    if (!($add_view))
212    {
213      $mid .= " and ga.wf_type <> ?";
214      $bindvars[] = 'view';
215    }
216
217    // add group mapping, warning groups and user can have the same id
218    $groups = galaxia_retrieve_user_groups($user);
219    if (is_array($groups))
220      foreach ($groups as &$group)
221        $group = "'{$group}'";
222    $mid .= " and ((gur.wf_user=? and gur.wf_account_type='u')";
223    if (is_array($groups))
224    {
225      $mid .= ' or (gur.wf_user in ('.implode(',',$groups).") and gur.wf_account_type='g')";
226    }
227    $mid .= ')';
228    $bindvars[] = $user;
229    if($find) {
230      $findesc = '%'.$find.'%';
231      $mid .= " and ((ga.wf_name like ?) or (ga.wf_description like ?))";
232      $bindvars[] = $findesc;
233      $bindvars[] = $findesc;
234    }
235    if($where) {
236      $mid.= " and ($where) ";
237    }
238    if ($remove_activities_without_instances)
239    {
240      $more_tables = "INNER JOIN ".GALAXIA_TABLE_PREFIX."instance_activities gia ON gia.wf_activity_id=gar.wf_activity_id
241                      INNER JOIN ".GALAXIA_TABLE_PREFIX."instances gi ON gia.wf_instance_id=gi.wf_instance_id";
242    }
243    else
244    {
245        $more_tables = "";
246    }
247    $query = "select distinct(ga.wf_activity_id),
248                     ga.wf_name,
249                     NULLIF(ga.wf_menu_path, '') AS wf_menu_path,
250                     ga.wf_type,
251                     gp.wf_name as wf_procname,
252                     ga.wf_is_interactive,
253                     ga.wf_is_autorouted,
254                     ga.wf_activity_id,
255                     gp.wf_version as wf_version,
256                     gp.wf_p_id,
257                     gp.wf_is_active,
258                     gp.wf_normalized_name
259                from ".GALAXIA_TABLE_PREFIX."processes gp
260                INNER JOIN ".GALAXIA_TABLE_PREFIX."activities ga ON gp.wf_p_id=ga.wf_p_id
261                INNER JOIN ".GALAXIA_TABLE_PREFIX."activity_roles gar ON gar.wf_activity_id=ga.wf_activity_id
262                INNER JOIN ".GALAXIA_TABLE_PREFIX."roles gr ON gr.wf_role_id=gar.wf_role_id
263                INNER JOIN ".GALAXIA_TABLE_PREFIX."user_roles gur ON gur.wf_role_id=gr.wf_role_id
264                $more_tables
265                $mid";
266             
267    $query_cant = "select count(distinct(ga.wf_activity_id))
268              from ".GALAXIA_TABLE_PREFIX."processes gp
269                INNER JOIN ".GALAXIA_TABLE_PREFIX."activities ga ON gp.wf_p_id=ga.wf_p_id
270                INNER JOIN ".GALAXIA_TABLE_PREFIX."activity_roles gar ON gar.wf_activity_id=ga.wf_activity_id
271                INNER JOIN ".GALAXIA_TABLE_PREFIX."roles gr ON gr.wf_role_id=gar.wf_role_id
272                INNER JOIN ".GALAXIA_TABLE_PREFIX."user_roles gur ON gur.wf_role_id=gr.wf_role_id
273                $more_tables
274                $mid ";
275    $result = $this->query($query,$bindvars,$maxRecords,$offset, true, $sort_mode);
276    $cant = (int) $this->getOne($query_cant,$bindvars);
277    $ret = Array();
278
279    if (!empty($result) && ($cant > 0))
280      $ret = $result->getArray(-1);
281
282    $retval = Array();
283    $retval['data'] = $ret;
284    $retval['cant'] = $cant;
285    return $retval;
286  }
287
288  /**
289   * Gets user activities but each activity name (and not id) appears only one time
290   *
291   * @param int $user Current user id
292   * @param int $offset Current starting point for the query results
293   * @param int $maxRecords Max number of results to return
294   * @param string $sort_mode For sorting
295   * @param string $find Search in activity name or description
296   * @param string $where Deprecated it's a string to add to the query, use with care for SQL injection
297   * @param bool $remove_instances_activities False by default, if true then all activities related to instances will be avoided (i.e. activities which are not standalone, start or view).
298   * @param bool $add_start False by default, if true start activities are added to the listing
299   * @param bool $add_standalone False by default, if true standalone activities are added to the listing
300   * @param bool $add_view False by default, if true view activities are added to the listing
301   * @return array Associative, key cant gives the number of results, key data is an associative array conteining the results
302   * @access public
303   */   
304        function gui_list_user_activities_by_unique_name($user,$offset,$maxRecords,$sort_mode,$find,$where='', $remove_instances_activities =false, $add_start = false, $add_standalone = false, $add_view = false)
305        {
306                // FIXME: this doesn't support multiple sort criteria
307                //$sort_mode = $this->convert_sortmode($sort_mode);
308                $sort_mode = str_replace("__"," ",$sort_mode);
309                $mid = "where gp.wf_is_active=?";
310                $bindvars = array('y');
311               
312                if ($remove_instances_activities)
313                {
314                   $mid .= " and ga.wf_type <> ? and ga.wf_type <> ? and ga.wf_type <> ?  and  ga.wf_type <> ?  and  ga.wf_type <> ? ";
315                   $bindvars[] = 'end';
316                   $bindvars[] = 'switch';
317                   $bindvars[] = 'join';
318                   $bindvars[] = 'activity';
319                   $bindvars[] = 'split';
320                }
321                if (!($add_start))
322                {
323                  $mid .= " and ga.wf_type <> ?";
324                  $bindvars[] = 'start';
325                }
326                if (!($add_standalone))
327                {
328                  $mid .= " and ga.wf_type <> ?";
329                  $bindvars[] = 'standalone';
330                }
331                if (!($add_view))
332                {
333                  $mid .= " and ga.wf_type <> ?";
334                  $bindvars[] = 'view';
335                }
336               
337                // add group mapping, warning groups and user can have the same id
338                $groups = galaxia_retrieve_user_groups($user);
339                $mid .= " and ((gur.wf_user=? and gur.wf_account_type='u')";
340                if (is_array($groups))
341                {
342      foreach ($groups as &$group)
343        $group = "'{$group}'";
344                  $mid .= ' or (gur.wf_user in ('.implode(',',$groups).") and gur.wf_account_type='g')";
345                }
346                $mid .= ')';
347
348                $bindvars[] = $user;
349                if($find)
350                {
351                        $findesc = '%'.$find.'%';
352                        $mid .= " and ((ga.wf_name like ?) or (ga.wf_description like ?))";
353                        $bindvars[] = $findesc;
354                        $bindvars[] = $findesc;
355                }
356                if($where)
357                {
358                        $mid.= " and ($where) ";
359                }
360
361                $query = "select distinct(ga.wf_name)
362                        from ".GALAXIA_TABLE_PREFIX."processes gp
363                        INNER JOIN ".GALAXIA_TABLE_PREFIX."activities ga ON gp.wf_p_id=ga.wf_p_id
364                        INNER JOIN ".GALAXIA_TABLE_PREFIX."activity_roles gar ON gar.wf_activity_id=ga.wf_activity_id
365                        INNER JOIN ".GALAXIA_TABLE_PREFIX."roles gr ON gr.wf_role_id=gar.wf_role_id
366                        INNER JOIN ".GALAXIA_TABLE_PREFIX."user_roles gur ON gur.wf_role_id=gr.wf_role_id
367                        $mid";
368
369                $query_cant = "select count(distinct(ga.wf_name))
370                        from ".GALAXIA_TABLE_PREFIX."processes gp
371                        INNER JOIN ".GALAXIA_TABLE_PREFIX."activities ga ON gp.wf_p_id=ga.wf_p_id
372                        INNER JOIN ".GALAXIA_TABLE_PREFIX."activity_roles gar ON gar.wf_activity_id=ga.wf_activity_id
373                        INNER JOIN ".GALAXIA_TABLE_PREFIX."roles gr ON gr.wf_role_id=gar.wf_role_id
374                        INNER JOIN ".GALAXIA_TABLE_PREFIX."user_roles gur ON gur.wf_role_id=gr.wf_role_id
375                        $mid";
376                $result = $this->query($query,$bindvars,$maxRecords,$offset, true, $sort_mode);
377                $cant = $this->getOne($query_cant,$bindvars);
378                $ret = Array();
379                if (!(empty($result)))
380                {
381                  while($res = $result->fetchRow())
382                  {
383                        $ret[] = $res;
384                  }
385                }
386
387                $retval = Array();
388                $retval["data"] = $ret;
389                $retval["cant"] = $cant;
390                return $retval;
391        }
392
393  /**
394   * Gets start activities avaible for a given user
395   *
396   * @param int $user Current user id
397   * @param int $offset Current starting point for the query results
398   * @param int $maxRecords Max number of results to return
399   * @param string $sort_mode For sorting
400   * @param string $find Search in activity name or description
401   * @param string $where Deprecated it's a string to add to the query, use with care for SQL injection
402   * @return array Associative, key cant gives the number of results, key data is an associative array conteining the results
403   * @access public
404   */     
405  function gui_list_user_start_activities($user,$offset,$maxRecords,$sort_mode,$find,$where='')
406  {
407    // FIXME: this doesn't support multiple sort criteria
408    $sort_mode = str_replace("__"," ",$sort_mode);
409
410    $mid = "where gp.wf_is_active=? and ga.wf_type=?";
411    // add group mapping, warning groups and user can have the same id
412    $groups = galaxia_retrieve_user_groups($user);
413    $mid .= " and ((gur.wf_user=? and gur.wf_account_type='u')";
414    if (is_array($groups))
415    {
416      foreach ($groups as &$group)
417        $group = "'{$group}'";
418      $mid .= ' or (gur.wf_user in ('.implode(',',$groups).") and gur.wf_account_type='g')";
419    }
420    $mid .= ')';
421    $bindvars = array('y','start',$user);
422    if($find)
423    {
424      //search on activities and processes
425      $findesc = '%'.$find.'%';
426      $mid .= " and ((ga.wf_name like ?) or (ga.wf_description like ?) or (gp.wf_name like ?) or (gp.wf_description like ?))";
427      $bindvars[] = $findesc;
428      $bindvars[] = $findesc;
429      $bindvars[] = $findesc;
430      $bindvars[] = $findesc;
431    }
432    if($where)
433    {
434      $mid.= " and ($where) ";
435    }
436
437    $query = "select distinct(ga.wf_activity_id),
438                              ga.wf_name,
439                              ga.wf_is_interactive,
440                              ga.wf_is_autorouted,
441                              gp.wf_p_id,
442                              gp.wf_name as wf_procname,
443                              gp.wf_version,
444                              gp.wf_normalized_name
445        from ".GALAXIA_TABLE_PREFIX."processes gp
446        INNER JOIN ".GALAXIA_TABLE_PREFIX."activities ga ON gp.wf_p_id=ga.wf_p_id
447        INNER JOIN ".GALAXIA_TABLE_PREFIX."activity_roles gar ON gar.wf_activity_id=ga.wf_activity_id
448        INNER JOIN ".GALAXIA_TABLE_PREFIX."roles gr ON gr.wf_role_id=gar.wf_role_id
449        INNER JOIN ".GALAXIA_TABLE_PREFIX."user_roles gur ON gur.wf_role_id=gr.wf_role_id
450        $mid";
451    $query_cant = "select count(distinct(ga.wf_activity_id))
452        from ".GALAXIA_TABLE_PREFIX."processes gp
453        INNER JOIN ".GALAXIA_TABLE_PREFIX."activities ga ON gp.wf_p_id=ga.wf_p_id
454        INNER JOIN ".GALAXIA_TABLE_PREFIX."activity_roles gar ON gar.wf_activity_id=ga.wf_activity_id
455        INNER JOIN ".GALAXIA_TABLE_PREFIX."roles gr ON gr.wf_role_id=gar.wf_role_id
456        INNER JOIN ".GALAXIA_TABLE_PREFIX."user_roles gur ON gur.wf_role_id=gr.wf_role_id
457        $mid";
458    $result = $this->query($query,$bindvars,$maxRecords,$offset, true, $sort_mode);
459    $ret = Array();
460    if (!(empty($result)))
461    {
462      while($res = $result->fetchRow())
463      {
464        $ret[] = $res;
465      }
466    }
467    $retval = Array();
468    $retval["data"]= $ret;
469    $retval["cant"]= $this->getOne($query_cant,$bindvars);
470   
471    return $retval;
472  }
473
474  /**
475   * Gets instances avaible for a given user, theses instances are all the instances where the user is able to launch a gui action (could be a run --even a run view activity-- or an advanced action like grab, release, admin, etc)
476   * type of action really avaible are not given by this function
477   *
478   * @see GUI::getUserAction
479   * @access public
480   * @param int $user User id
481   * @param int $offset Starting number for the returned records
482   * @param int $maxRecords Limit of records to return in data (but the 'cant' key count the total number without limits)
483   * @param string $sort_mode Sort mode for the query
484   * @param string $find Look at in activity name, activity description or instance name
485   * @param string $where is an empty string by default, the string let you add a string to the SQL statement -please be carefull with it
486   * @param bool $add_properties False by default, will add properties in the returned instances
487   * @param int $pId Process id, 0 by default, in such case it is ignored
488   * @param bool $add_completed_instances False by default, if true we add completed instances in the result
489   * @param bool $add_exception_instances False by default, if true we add instances in exception in the result
490   * @param bool $add_aborted_instances False by default, if true we add aborted instances in the result
491   * @param bool $restrict_to_owner False by default, if true we restrict to instance for which the user is the owner even if it gives no special rights (that can give more or less results -- you'll have ownership but no rights but you wont get rights without ownership)
492   * @param bool $add_non_interactive_instances_of_the_owner False by default, if true we include the non interactive instances the user owns
493   * @return array Number of records in the 'cant key and instances in the 'data' key.
494   * Each instance is an array containing theses keys: wf_instance_id, wf_started (instance), wf_ended (instance), wf_owner, wf_user, wf_status (instance status),
495   * wf_category, wf_act_status, wf_act_started, wf_name (activity name), wf_type, wf_procname, wf_is_interactive, wf_is_autorouted, wf_activity_id,
496   * wf_version (process version), wf_p_id, insname (instance name), wf_priority and wf_readonly (which is true if the user only have read-only roles associated with this activity)
497   */
498  function gui_list_user_instances($user, $offset, $maxRecords, $sort_mode, $find, $where='', $add_properties=false, $pId=0, $add_active_instances=true, $add_completed_instances=false, $add_exception_instances=false, $add_aborted_instances=false, $restrict_to_owner=false, $add_non_interactive_instances_of_the_owner = false)
499  {
500    // FIXME: this doesn't support multiple sort criteria
501    //$sort_mode = $this->convert_sortmode($sort_mode);
502    $sort_mode = str_replace("__"," ",$sort_mode);
503
504    $mid = 'WHERE (gp.wf_is_active = ?)';
505    $bindvars = array('y');
506
507    /* restrict to process */
508    if ($pId !== 0)
509    {
510        $mid .= ' AND (gp.wf_p_id = ?)';
511        $bindvars[] = $pId;
512    }
513
514    /* look for a owner restriction */
515    if ($restrict_to_owner)
516    {
517        $mid .= ' AND (gi.wf_owner = ?)';
518        $bindvars[] = $user;
519    }
520    else /* no restriction on ownership, look for user and/or owner */
521    {
522      $groups = galaxia_retrieve_user_groups($user);
523      if (is_array($groups))
524        $groups = '{' . implode(', ', $groups) . '}';
525
526      /* selects the instances that belong to a role, which the user mapped to */
527      $mid .= ' AND (';
528      $mid .= 'gia.wf_user IN (SELECT \'p\' || gur.wf_role_id FROM egw_wf_user_roles gur WHERE (gur.wf_user = ? and gur.wf_account_type=\'u\')';
529      $bindvars[] = $user;
530      if ($groups)
531      {
532        $mid .= ' OR (gur.wf_user = ANY (?) AND gur.wf_account_type=\'g\')';
533        $bindvars[] = $groups;
534      }
535      $mid .= ')';
536
537      /* selects the instances that belong to the user or everyone (the user must be mapped to a role that has access to the activity the instance is in) */
538      $mid .= 'OR (((gia.wf_user = \'*\') OR (gia.wf_user = ?)) AND gia.wf_activity_id IN (SELECT gar.wf_activity_id FROM egw_wf_activity_roles gar, egw_wf_user_roles gur WHERE (gur.wf_role_id = gar.wf_role_id) AND ((gur.wf_user = ? AND gur.wf_account_type=\'u\')';
539      $bindvars[] = $user;
540      $bindvars[] = $user;
541      if ($groups)
542      {
543        $mid .= 'OR (gur.wf_user = ANY (?) AND gur.wf_account_type = \'g\')';
544        $bindvars[] = $groups;
545      }
546      $mid .= ')))';
547
548      /* this collect non interactive instances we are owner of */
549      if ($add_non_interactive_instances_of_the_owner)
550      {
551        $mid .= ' OR ((gi.wf_owner = ?) AND ga.wf_is_interactive = \'n\')';
552        $bindvars[] = $user;
553      }
554
555      /* and this collect completed/aborted instances when asked which haven't got any user anymore */
556      if ($add_completed_instances || $add_aborted_instances)
557          $mid .= ' OR (gur.wf_user IS NULL)';
558
559      $mid .= ')';
560    }
561
562    if($find)
563    {
564      $findesc = '%'. $find .'%';
565      $mid .= " AND ((UPPER(ga.wf_name) LIKE UPPER(?))";
566      $mid .= " OR (UPPER(gi.wf_name) LIKE UPPER(?)))";
567      $bindvars[] = $findesc;
568      $bindvars[] = $findesc;
569    }
570
571    if($where)
572      $mid.= " AND ({$where}) ";
573
574    /* instance selection :: instances can be active|exception|aborted|completed */
575    $or_status = Array();
576    if ($add_active_instances)
577      $or_status[] = "(gi.wf_status = 'active')";
578    if ($add_exception_instances)
579      $or_status[] = "(gi.wf_status = 'exception')";
580    if ($add_aborted_instances)
581      $or_status[] = "(gi.wf_status = 'aborted')";
582    if ($add_completed_instances)
583      $or_status[] = "(gi.wf_status = 'completed')";
584    if (!(empty($or_status)))
585        $mid .= ' AND (' . implode(' OR ', $or_status) . ')';
586    else
587      /*special case, we want no active instance, and we do not want exception/aborted and completed, so what?
588       * maybe a special new status or some bad record in database... */
589      $mid .= " AND (gi.wf_status NOT IN ('active','exception','aborted','completed'))";
590
591
592    $selectedColumns = array(
593      'DISTINCT(gi.wf_instance_id)',
594      'gi.wf_started',
595      'gi.wf_ended',
596      'gi.wf_owner',
597      'gia.wf_user',
598      'gi.wf_status',
599      'gi.wf_category',
600      'gia.wf_status AS wf_act_status',
601      'gia.wf_started AS wf_act_started',
602      'ga.wf_name',
603      'ga.wf_type',
604      'gp.wf_name AS wf_procname',
605      'ga.wf_is_interactive',
606      'ga.wf_is_autorouted',
607      'ga.wf_activity_id',
608      'gp.wf_version AS wf_version',
609      'gp.wf_p_id',
610      'gp.wf_normalized_name',
611      'gi.wf_name AS insname',
612      'gi.wf_priority'
613    );
614
615    /* add the read only column */
616    $readOnlyColumn = '(SELECT MIN(gar.wf_readonly) FROM egw_wf_activity_roles gar, egw_wf_user_roles gur WHERE (gar.wf_activity_id = gia.wf_activity_id) AND (gur.wf_role_id=gar.wf_role_id) AND ((gur.wf_user = ? and gur.wf_account_type=\'u\')';
617    if ($groups)
618    {
619      $readOnlyColumn .= ' OR (gur.wf_user = ANY (?) AND gur.wf_account_type = \'g\')';
620      /* add the groups to be the second element of the array (there's another 'array_unshift' in the next lines) */
621      array_unshift($bindvars, $groups);
622    }
623    /* add as the first element of the array */
624    array_unshift($bindvars, $user);
625    $readOnlyColumn .= ')) AS wf_readonly';
626    $selectedColumns[] = $readOnlyColumn;
627
628    /* if requested, retrieve the properties */
629    if ($add_properties)
630      $selectedColumns[] = 'gi.wf_properties';
631
632    /* (regis) we need LEFT JOIN because aborted and completed instances are not showned
633     * in instance_activities, they're only in instances */
634    $query = 'SELECT ' . implode(', ', $selectedColumns) . ' ';
635    $query .= 'FROM egw_wf_instances gi LEFT JOIN egw_wf_instance_activities gia ON gi.wf_instance_id=gia.wf_instance_id ';
636    $query .= 'LEFT JOIN egw_wf_activities ga ON gia.wf_activity_id = ga.wf_activity_id ';
637    $query .= 'INNER JOIN egw_wf_processes gp ON gp.wf_p_id=gi.wf_p_id ';
638    $query .= $mid;
639
640    /* fetch the data (paging, if necessary) and count the records */
641    $result = $this->query($query, $bindvars, -1, 0, true, $sort_mode);
642    $cant = $result->NumRows();
643    $realMaxRecords = ($maxRecords == -1) ? ($cant - $offset) : min($maxRecords, $cant - $offset);
644    $ret = Array();
645    if ($cant > $offset)
646    {
647      $result->Move($offset);
648      for ($i = 0; $i < $realMaxRecords; $i++)
649      {
650        $res = $result->fetchRow();
651        if (substr($res['wf_user'], 0, 1) == 'p')
652          $res['wf_user'] = '*';
653        $ret[] = $res;
654      }
655    }
656
657    $retval = Array();
658    $retval['data'] = $ret;
659    $retval['cant'] = $cant;
660    return $retval;
661  }
662
663 /**
664  * Gets all instances where the user is the owner (active, completed, aborted, exception)
665  *
666  * @access public
667  * @param int $user User id
668  * @param int $offset Starting number for the returned records
669  * @param int $maxRecords Limit of records to return in data (but the 'cant' key count the total number without limits)
670  * @param string $sort_mode Sort mode for the query
671  * @param string $find Look at in activity name, activity description or instance name
672  * @param string $where Empty by default, the string let you add a string to the SQL statement -please be carefull with it
673  * @param bool $add_properties False by default, will add properties in the returned instances
674  * @param int $pId Process id, 0 by default, in such case it is ignored
675  * @param bool $add_completed_instances False by default, if true we add completed instances in the result
676  * @param bool $add_exception_instances False by default, if true we add instances in exception in the result
677  * @param bool $add_aborted_instances False by default, if true we add aborted instances in the result
678  * @param bool $add_non_interactive_instances_of_the_owner True by default, if true we include the non interactive instances the user owns
679  * @return array Associative, key cant gives the number of results, key data is an array of instances and each instance
680  * an array containing theses keys: wf_instance_id, wf_started (instance), wf_ended (instance), wf_owner, wf_user,
681  * wf_status (instance status), wf_category, wf_act_status (activity), wf_act_started (activity), wf_name (activity name),
682  * wf_type, wf_procname, wf_is_interactive, wf_is_autorouted, wf_activity_id, wf_version (process version), wf_p_id,
683  * insname (instance name), wf_priority and wf_readonly (which is true if the user only have read-only roles associated
684  * with this activity)
685  */
686  function gui_list_instances_by_owner($user, $offset, $maxRecords, $sort_mode, $find, $where='', $add_properties=false, $pId=0, $add_active_instances=true, $add_completed_instances=false, $add_exception_instances=false, $add_aborted_instances=false, $add_non_interactive_instances_of_the_owner = true)
687  {
688          return $this->gui_list_user_instances($user,$offset,$maxRecords,$sort_mode,$find,$where,$add_properties, $pId,$add_active_instances,$add_completed_instances,$add_exception_instances, $add_aborted_instances, true, $add_non_interactive_instances_of_the_owner);
689  }
690
691  /**
692   * Gets the view activity id avaible for a given process.
693   * No test is done on real access to this activity for users, this access will be check at runtime (when clicking)
694   *
695   * @param int $pId Process Id
696   * @return mixed View activity id or false if no view activity is present dor this process
697   * @access public
698   */
699  function gui_get_process_view_activity($pId)
700  {
701    if (!(isset($this->process_cache[$pId]['view'])))
702    {
703      if (!(isset($this->pm)))
704      {
705        require_once(GALAXIA_LIBRARY.SEP.'src'.SEP.'ProcessManager'.SEP.'ProcessManager.php');
706        $this->pm =& new ProcessManager($this->db);
707      }
708      $this->process_cache[$pId]['view'] = $this->pm->get_process_view_activity($pId);
709    }
710    return $this->process_cache[$pId]['view'];
711  }
712
713  /**
714   * Gets all informations about a given instance and a given user, list activities and status.
715   * We list activities for which the user is the owner or the actual user or in a role giving him access to the activity
716   * notice that completed and aborted instances aren't associated with activities and that start and standalone activities
717   * aren't associated with an instance ==> if instanceId is 0 you'll get all standalone and start activities in the result.
718   * This is the reason why you can give --if you have it-- the process id, to restrict results to start and standalone
719   * activities to this process
720   *
721   * @access public
722   * @param int $user User id
723   * @param int $instance_id Instance id
724   * @param int $pId Process id, 0 by default, in such case it is ignored
725   * @param bool $add_completed_instances False by default, if true we add completed instances in the result
726   * @param bool $add_exception_instances False by default, if true we add instances in exception in the result
727   * @param bool $add_aborted_instances False by default, if true we add aborted instances in the result
728   * @return array Associative, contains:
729   * ['instance'] =>
730   *    ['instance_id'], ['instance_status'], ['owner'], ['started'],
731   *    ['ended'], ['priority'], ['instance_name'],
732   *    ['process_name'], ['process_version'], ['process_id']
733   * ['activities'] =>
734   *     ['activity'] =>
735   *         ['user']               : current user
736   *         ['id']                     : activity Id
737   *         ['name']
738   *         ['type']
739   *         ['is_interactive'] : 'y' or 'n'
740   *         ['is_autorouted']  : 'y' or 'n'
741   *         ['status']
742   */
743  function gui_get_user_instance_status($user,$instance_id, $pId=0, $add_completed_instances=false,$add_exception_instances=false, $add_aborted_instances=false)
744  {
745    $bindvars =Array();
746    $mid = "\n where gp.wf_is_active=?";
747    $bindvars[] = 'y';
748    if (!($pId==0))
749    {
750      // process restriction
751      $mid.= " and gp.wf_p_id=?";
752      $bindvars[] = $pId;
753    }
754    if (!($instance_id==0))
755    {
756      // instance selection
757      $mid .= " and (gi.wf_instance_id=?)";
758      $bindvars[] = $instance_id;
759      $statuslist[]='active';
760      if ($add_exception_instances) $statuslist[]='exception';
761      if ($add_aborted_instances) $statuslist[]='aborted';
762      if ($add_completed_instances) $statuslist[]='completed';
763      $status_list = implode ($statuslist,',');
764      $mid .= " and (gi.wf_status in ('".implode ("','",$statuslist)."'))\n";
765    }
766    else
767    {
768      // collect NULL instances for start and standalone activities
769      $mid .= " and (gi.wf_instance_id is NULL)";
770    }
771    // add group mapping, warning groups and user can have the same id
772    $groups = galaxia_retrieve_user_groups($user);
773    $mid .= "\n and ( ((gur.wf_user=? and gur.wf_account_type='u')";
774    if (is_array($groups))
775    {
776      foreach ($groups as &$group)
777        $group = "'{$group}'";
778      $mid .= ' or (gur.wf_user in ('.implode(',',$groups).") and gur.wf_account_type='g')";
779    }
780    $mid .= ')';
781    $bindvars[] = $user;
782    // this collect non interactive instances we are owner of
783    $mid .= "\n or (gi.wf_owner=?)";
784    $bindvars[] = $user;
785    // and this collect completed/aborted instances when asked which haven't got any user anymore
786    if (($add_completed_instances) || ($add_aborted_instances))
787    {
788      $mid .= "\n or (gur.wf_user is NULL)";
789    }
790    $mid .= ")";
791   
792    // we need LEFT JOIN because aborted and completed instances are not showned
793    // in instance_activities, they're only in instances
794    $query = 'select distinct(gi.wf_instance_id) as instance_id,
795                     gi.wf_status as instance_status,
796                     gi.wf_owner as owner,
797                     gi.wf_started as started,
798                     gi.wf_ended as ended,
799                     gi.wf_priority as priority,
800                     gi.wf_name as instance_name,
801                     gp.wf_name as process_name,
802                     gp.wf_version as process_version,
803                     gp.wf_p_id as process_id,
804                     gia.wf_user as user,
805                     ga.wf_activity_id as id,
806                     ga.wf_name as name,
807                     ga.wf_type as type,
808                     ga.wf_is_interactive as is_interactive,
809                     ga.wf_is_autorouted as is_autorouted,
810                     gia.wf_status as status';
811    if ($instance_id==0)
812    {//TODO: this gives all activities, rstrict to standalone and start
813      $query.=' from '.GALAXIA_TABLE_PREFIX.'activities ga
814                LEFT JOIN '.GALAXIA_TABLE_PREFIX.'instance_activities gia ON ga.wf_activity_id=gia.wf_activity_id
815                LEFT JOIN '.GALAXIA_TABLE_PREFIX.'instances gi ON gia.wf_activity_id = gi.wf_instance_id
816                LEFT JOIN '.GALAXIA_TABLE_PREFIX.'activity_roles gar ON gia.wf_activity_id=gar.wf_activity_id
817                LEFT JOIN '.GALAXIA_TABLE_PREFIX.'user_roles gur ON gur.wf_role_id=gar.wf_role_id
818                INNER JOIN '.GALAXIA_TABLE_PREFIX.'processes gp ON gp.wf_p_id=ga.wf_p_id '.$mid;
819    }
820    else
821    {
822      $query.=' from '.GALAXIA_TABLE_PREFIX.'instances gi
823                LEFT JOIN '.GALAXIA_TABLE_PREFIX.'instance_activities gia ON gi.wf_instance_id=gia.wf_instance_id
824                LEFT JOIN '.GALAXIA_TABLE_PREFIX.'activities ga ON gia.wf_activity_id = ga.wf_activity_id
825                LEFT JOIN '.GALAXIA_TABLE_PREFIX.'activity_roles gar ON gia.wf_activity_id=gar.wf_activity_id
826                LEFT JOIN '.GALAXIA_TABLE_PREFIX.'user_roles gur ON gur.wf_role_id=gar.wf_role_id
827                INNER JOIN '.GALAXIA_TABLE_PREFIX.'processes gp ON gp.wf_p_id=gi.wf_p_id '.$mid;
828    }
829    $result = $this->query($query,$bindvars);
830    $retinst = Array();
831    $retacts = Array();
832    if (!!$result)
833    {
834      while($res = $result->fetchRow())
835      {
836        // Get instances per activity
837        if (count($retinst)==0)
838        {//the first time we retain instance data
839          $retinst[] = array_slice($res,0,-7);
840        }
841        $retacts[] = array_slice($res,10);
842      }
843    }
844    $retval = Array();
845    $retval["instance"] = $retinst{0};
846    $retval["activities"] = $retacts;
847    return $retval;
848  }
849 
850  /**
851   * Aborts an instance by terminating the instance with status 'aborted', and removes all running activities
852   *
853   * @access public
854   * @param int $activityId 
855   * @param int $instanceId
856   * @return bool 
857   */
858  function gui_abort_instance($activityId,$instanceId)
859  {
860    $user = galaxia_retrieve_running_user();
861   
862    // start a transaction
863    $this->db->StartTrans();
864
865    if (!($this->wf_security->checkUserAction($activityId, $instanceId,'abort')))
866    {
867      $this->error[] = ($this->wf_security->get_error());
868      $this->db->FailTrans();
869    }
870    else
871    {
872      //the security object said everything was fine
873      $instance = new Instance($this->db);
874      $instance->getInstance($instanceId);
875      if (!empty($instance->instanceId))
876      {
877          if (!($instance->abort()))
878          {
879            $this->error[] = ($instance->get_error());
880            $this->db->FailTrans();
881          }
882      }
883      unset($instance);
884    }
885    // perform commit (return true) or Rollback (return false) if Failtrans it will automatically rollback
886    return $this->db->CompleteTrans();
887  }
888 
889  /**
890   * Exception handling for an instance, setting instance status to 'exception', but keeps all running activities.
891   * Instance can be resumed afterwards via gui_resume_instance()
892   *
893   * @access public
894   * @param int $activityId 
895   * @param int $instanceId
896   * @return bool 
897   */
898  function gui_exception_instance($activityId,$instanceId)
899  {
900    $user = galaxia_retrieve_running_user();
901   
902    // start a transaction
903    $this->db->StartTrans();
904
905    if (!($this->wf_security->checkUserAction($activityId, $instanceId,'exception')))
906    {
907      $this->error[] = ($this->wf_security->get_error());
908      $this->db->FailTrans();
909    }
910    else
911    {
912      //the security object said everything was fine
913      $query = "update ".GALAXIA_TABLE_PREFIX."instances
914              set wf_status=?
915              where wf_instance_id=?";
916      $this->query($query, array('exception',$instanceId));
917    }
918    // perform commit (return true) or Rollback (return false) if Failtrans it will automatically rollback
919    return $this->db->CompleteTrans();
920  }
921
922  /**
923   * Resumes an instance by setting instance status from 'exception' back to 'active'
924   *
925   * @access public
926   * @param int $activityId 
927   * @param int $instanceId
928   * @return bool 
929   */ 
930  function gui_resume_instance($activityId,$instanceId)
931  {
932    $user = galaxia_retrieve_running_user();
933   
934    // start a transaction
935    $this->db->StartTrans();
936
937    if (!($this->wf_security->checkUserAction($activityId, $instanceId,'resume')))
938    {
939      $this->error[] = ($this->wf_security->get_error());
940      $this->db->FailTrans();
941    }
942    else
943    {
944      //the security object said everything was fine
945      $query = "update ".GALAXIA_TABLE_PREFIX."instances
946              set wf_status=?
947              where wf_instance_id=?";
948      $this->query($query, array('active',$instanceId));
949    }
950    // perform commit (return true) or Rollback (return false) if Failtrans it will automatically rollback
951    return $this->db->CompleteTrans();
952  }
953
954  /**
955   * Restarts an automated activity (non-interactive) which is still in running mode (maybe it failed)
956   *
957   * @access public
958   * @param int $activityId 
959   * @param int $instanceId
960   * @return bool 
961   */ 
962  function gui_restart_instance($activityId,$instanceId)
963  {
964    $user = galaxia_retrieve_running_user();
965   
966    //start a transaction
967    $this->db->StartTrans();
968   
969    if (!($this->wf_security->checkUserAction($activityId, $instanceId,'restart')))
970    {
971      $this->error[] = ($this->wf_security->get_error());
972      $this->db->FailTrans();
973    }
974    else
975    {
976      //the security object said everything was fine
977      $instance =& new Instance($this->db);
978      $instance->getInstance($instanceId);
979      // we force the execution of the activity
980      $result = $instance->executeAutomaticActivity($activityId, $instanceId);     
981      //TODO handle information returned in the sendAutorouted like in the completed activity template
982      //_debug_array($result);
983      $this->error[] = $instance->get_error();
984      unset($instance);
985    }
986    // perform commit (return true) or Rollback (return false) if Failtrans it will automatically rollback
987    return $this->db->CompleteTrans();
988  }
989
990  /**
991   * This function send a non autorouted activity i.e. take the transition which was not
992   * taken automatically. It can be as well used to walk a transition which failed the first time by the admin
993   *
994   * @access public
995   * @param int $activityId 
996   * @param int $instanceId
997   * @return bool 
998   */   
999  function gui_send_instance($activityId,$instanceId)
1000  {
1001    $user = galaxia_retrieve_running_user();
1002   
1003    //start a transaction
1004    $this->db->StartTrans();
1005   
1006    if (!($this->wf_security->checkUserAction($activityId, $instanceId,'send')))
1007    {
1008      $this->error[] = ($this->wf_security->get_error());
1009      $this->db->FailTrans();
1010    }
1011    else
1012    {
1013      //the security object said everything was fine
1014      $instance =& new Instance($this->db);
1015      $instance->getInstance($instanceId);
1016      // we force the continuation of the flow
1017      $result = $instance->sendAutorouted($activityId,true);
1018      //TODO handle information returned in the sendAutorouted like in the completed activity template
1019      //_debug_array($result);
1020      $this->error[] = $instance->get_error();
1021      unset($instance);
1022    }
1023    // perform commit (return true) or Rollback (return false) if Failtrans it will automatically rollback
1024    return $this->db->CompleteTrans();
1025  }
1026
1027 
1028  /**
1029   * Releases instances
1030   * 
1031   * @access public
1032   * @param int $activityId 
1033   * @param int $instanceId
1034   * @return bool 
1035   */
1036  function gui_release_instance($activityId,$instanceId)
1037  {
1038    $user = galaxia_retrieve_running_user();
1039   
1040    // start a transaction
1041    $this->db->StartTrans();
1042
1043    if (!($this->wf_security->checkUserAction($activityId, $instanceId,'release')))
1044    {
1045      $this->error[] = ($this->wf_security->get_error());
1046      $this->db->FailTrans();
1047    }
1048    else
1049    {
1050      //the security object said everything was fine
1051      $query = "update ".GALAXIA_TABLE_PREFIX."instance_activities
1052                set wf_user = ?
1053                where wf_instance_id=? and wf_activity_id=?";
1054      $this->query($query, array('*',$instanceId,$activityId));
1055    }
1056    // perform commit (return true) or Rollback (return false) if Failtrans it will automatically rollback
1057    return $this->db->CompleteTrans();
1058  }
1059 
1060  /**
1061   * Grabs instance for this activity and user if the security object agreed
1062   * 
1063   * @access public
1064   * @param int $activityId 
1065   * @param int $instanceId
1066   * @return bool 
1067   */ 
1068  function gui_grab_instance($activityId,$instanceId)
1069  {
1070    $user = galaxia_retrieve_running_user();
1071   
1072    // start a transaction
1073    $this->db->StartTrans();
1074    //this check will as well lock the table rows
1075    if (!($this->wf_security->checkUserAction($activityId, $instanceId,'grab')))
1076    {
1077      $this->error[] = ($this->wf_security->get_error());
1078      $this->db->FailTrans();
1079    }
1080    else
1081    {
1082      //the security object said everything was fine
1083      $query = "update ".GALAXIA_TABLE_PREFIX."instance_activities
1084                set wf_user = ?
1085                where wf_instance_id=? and wf_activity_id=?";
1086      $this->query($query, array($user,$instanceId,$activityId));
1087    }
1088    // perform commit (return true) or Rollback (return false) if Failtrans it will automatically rollback
1089    return $this->db->CompleteTrans();
1090  }
1091
1092 
1093 
1094  /**
1095  * Gets avaible actions for a given user on a given activity and a given instance assuming he already have access to it.
1096  * To be able to decide this function needs the user id, instance_id and activity_id.
1097  * Optional arguments can be retrieved by internal queries BUT if you want this function to be fast and if you already
1098  * have theses datas you should give as well these fields (all or none)
1099  *
1100  * @access public 
1101  * @param int $user User id (required)
1102  * @param int $instanceId Instance id (can be 0 if you have no instance - for start or standalone activities, required)
1103  * @param int $activityId Activity id (can be 0 if you have no activity - for aborted or completed instances, required)
1104  * @param bool $readonly Role mode, if true this is a readonly access, if false it is a not-only-read access (required)
1105  * @param int $pId Process id
1106  * @param string $actType Activity type string ('split', 'activity', 'switch', etc.)
1107  * @param string $actInteractive Activity interactivity ('y' or 'n')
1108  * @param string $actAutorouted Activity routage ('y' or 'n')
1109  * @param string $actStatus Activity status ('completed' or 'running')
1110  * @param int $instanceOwner Instance owner user id
1111  * @param string $instanceStatus Instance status ('completed', 'active', 'exception', 'aborted')
1112  * @param mixed $currentUser Current user of the instance (user id or '*')
1113  * @return array In this form:
1114  * array('action name' => 'action description')
1115  * 'actions names' are: 'grab', 'release', 'run', 'send', 'view', 'exception', 'resume' and 'monitor'
1116  * Some config values can change theses rules but basically here they are:
1117  *     * 'grab'        : be the user of this activity. User has access to it and instance status is ok.
1118  *     * 'release'     : let * be the user of this activity. Must be the actual user or the owner of the instance.
1119  *     * 'run'         : run an associated form. This activity is interactive, user has access, instance status is ok.
1120  *     * 'send'        : send this instance, activity was non-autorouted and he has access and status is ok.
1121  *     * 'view'        : view the instance, activity ok, always avaible except for start or standalone act or processes with view activities.
1122  *     * 'viewrun'     : view the instance in a view activity, need to have a role on this view activity
1123  *     * 'abort'       : abort an instance, ok when we are the user
1124  *     * 'exception'   : set the instance status to exception, need to be the user
1125  *     * 'resume'      : back to running when instance status was exception, need to be the user
1126  *     * 'monitor'     : special user rights to administer the instance
1127  * 'actions description' are translated explanations like 'release access to this activity'
1128  * WARNING: this is a snapshot, the engine give you a snaphsots of the rights a user have on an instance-activity
1129  * at a given time, this is not meaning theses rights will still be there when the user launch the action.
1130  * You should absolutely use the GUI Object to execute theses actions (except monitor) and they could be rejected.
1131  * WARNING: we do not check the user access rights. If you launch this function for a list of instances obtained via this
1132  * GUI object theses access rights are allready checked.
1133  */
1134  function getUserActions($user, $instanceId, $activityId, $readonly, $pId=0, $actType='not_set', $actInteractive='not_set', $actAutorouted='not_set', $actStatus='not_set', $instanceOwner='not_set', $instanceStatus='not_set', $currentUser='not_set')
1135  {
1136    $result= array();//returned array
1137
1138    //check if we have all the args and retrieve the ones whe did not have:
1139    if ((!($pId)) ||
1140      ($actType=='not_set') ||
1141      ($actInteractive=='not_set') ||
1142      ($actAutorouted=='not_set') ||
1143      ($actStatus=='not_set') ||
1144      ($instanceOwner=='not_set') ||
1145      ($currentUser=='not_set') ||
1146      ($instanceStatus=='not_set'))
1147    {
1148      // get process_id, type, interactivity, autorouting and act status and others for this instance
1149      // we retrieve info even if ended or in exception or aborted instances
1150      // and if $instanceId is 0 we get all standalone and start activities
1151      //echo '<br> call gui_get_user_instance_status:'.$pId.':'.$actType.':'.$actInteractive.':'.$actAutorouted.':'.$actStatus.':'.$instanceOwner.':'.$currentUser.':'.$instanceStatus;
1152      $array_info = $this->gui_get_user_instance_status($user,$instanceId,0,true,true,true);
1153     
1154      //now set our needed values
1155      $instance = $array_info['instance'];
1156      $pId = $instance['instance_id'];
1157      $instanceStatus = $instance['instance_status'];
1158      $instanceOwner = $instance['owner'];
1159     
1160      if (!((int)$activityId))
1161      {
1162        //we have no activity Id, like for aborted or completed instances, we set default values
1163        $actType = '';
1164        $actInteractive = 'n';
1165        $actAutorouted = 'n';
1166        $actstatus = '';
1167        $currentUser = 0;
1168      }
1169      else
1170      {
1171        $find=false;
1172        foreach ($array_info['activities'] as $activity)
1173        {
1174          //_debug_array($activity);
1175          //echo "<br> ==>".$activity['id']." : ".$activityId;
1176          if ((int)$activity['id']==(int)$activityId)
1177          {
1178            $actType = $activity['type'];
1179            $actInteractive = $activity['is_interactive'];
1180            $actAutorouted = $activity['is_autorouted'];
1181            $actstatus = $activity['status'];
1182            $currentUser = $activity['user'];
1183            $find = true;
1184            break;
1185          }
1186        }
1187        //if the activity_id can't be find we return empty actions
1188        if (!($find))
1189        {
1190          return array();
1191        }
1192      }
1193    }
1194   
1195    //now use the security object to get actions avaible, this object know the rules
1196    $view_activity = $this->gui_get_process_view_activity($pId);
1197    $result =& $this->wf_security->getUserActions($user, $instanceId, $activityId, $readonly, $pId, $actType, $actInteractive, $actAutorouted, $actStatus, $instanceOwner, $instanceStatus, $currentUser, $view_activity);
1198    return $result;
1199  }
1200
1201 
1202}
1203?>
Note: See TracBrowser for help on using the repository browser.