source: trunk/workflow/inc/engine/src/GUI/GUI.php @ 7655

Revision 7655, 50.8 KB checked in by douglasz, 11 years ago (diff)

Ticket #3236 - Melhorias de performance no codigo do Expresso.

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