source: companies/serpro/workflow/inc/class.run_activity.inc.php @ 903

Revision 903, 34.8 KB checked in by niltonneto, 15 years ago (diff)

Importacao inicial do Expresso do Serpro

Line 
1<?php
2        /**************************************************************************\
3        * eGroupWare                                                               *
4        * http://www.egroupware.org                                                *
5        * --------------------------------------------                             *
6        *  This program is free software; you can redistribute it and/or modify it *
7        *  under the terms of the GNU General Public License as published by the   *
8        *  Free Software Foundation; either version 2 of the License, or (at your  *
9        *  option) any later version.                                              *
10        \**************************************************************************/
11
12        require_once(dirname(__FILE__) . SEP . 'class.WorkflowUtils.inc.php'  ); /* superclass source code       */
13        require_once(dirname(__FILE__) . SEP . 'class.basecontroller.inc.php' ); /* module controller            */
14        require_once(dirname(__FILE__) . SEP . 'class.basemodel.inc.php'      ); /* module logic                 */
15        require_once(dirname(__FILE__) . SEP . 'class.process_factory.inc.php'); /* module process factory class */
16        require_once(dirname(__FILE__) . SEP . 'class.utils.security.php'     ); /* sanitizes input data         */
17
18        /**
19         * @package Workflow
20         * @author Mauricio Luiz Viani - viani@celepar.pr.gov.br
21         * @author Sidnei Augusto Drovetto Jr. - drovetto@gmail.com
22         * @author Carlos Eduardo Nogueira Goncalves - cadu.br@gmail.com
23         * @license GPL
24         * @license http://www.gnu.org/copyleft/gpl.html GPL
25     */
26        class run_activity extends WorkflowUtils
27        {
28                /**
29                 * @var array  $public_functions func
30                 * @access public
31                 */
32                var $public_functions = array(
33                        'go'    => true,
34                        'goAjax'  => true
35                );
36                /**
37                 * @var object $runtime Runtime Object from the workflow engine
38                 * @access public
39                 */
40                var $runtime;
41                // This is the object we'll be running to obtain the rigth activity
42                //var $base_activity;
43                //This is the right activity object
44                /**
45                 * @var object $activity Activity engine object.
46                 * @access public
47                 */
48                var $activity;
49                /**
50                 * @var object $process Process engine object. Used to retrieve at least paths and configuration values
51                 * @access public
52                 */
53                var $process;
54                /**
55                 * @var object $GUI  GUI engine object. Act carefully with it.
56                 * @access public
57                 */
58                var $GUI;
59                /**
60                 * @var array $message a message array
61                 * @access public
62                 */
63                var $message = Array();
64                /**
65                 * @var object $categories categorie object for categories
66                 * @access public
67                 */
68                var $categories;
69                /**
70                 * @var array $conf
71                 * @access public
72                 */
73                var $conf = array();
74                /**
75                 * @var $wf_template local activity template
76                 * @access public
77                 */
78                var $wf_template;
79                /**
80                 * @var object $instance
81                 * @access public
82                 */
83                var $instance;
84                /**
85                 * @var object $activity_type The type of activity
86                 * @access public
87                 */
88                var $activity_type;
89                // then we retain all usefull vars as members, to make them avaible in user's source code
90                // theses are data which can be set before the user code and which are not likely to change because of the user code
91                /**
92                 * @var int $process_id The process id
93                 * @access public
94                 */
95                var $process_id;
96                /**
97                 * @var int $activity_id The activity id
98                 * @access public
99                 */
100                var $activity_id;
101                /**
102                 * @var int $process_name The process name
103                 * @access public
104                 */
105                var $process_name;
106                /**
107                 * @var int $process_version The process_version
108                 * @access public
109                 */
110                var $process_version;
111                /**
112                 * @var int $activity_name The process id
113                 * @access public
114                 */
115                var $activity_name;
116                /**
117                 * @var int $user_name The user name
118                 * @access public
119                 */
120                var $user_name;
121                /**
122                 * @var int $view_activity activity id of the view activity avaible for this process
123                 * @access public
124                 */
125                var $view_activity;
126                // theses 4 vars aren't avaible for the user code, they're set only after this user code was executed
127                /**
128                 * @var int $instance_id Instance id
129                 * @access public
130                 */
131                var $instance_id=0;
132                /**
133                 * @var string $instance_name Instance name
134                 * @access public
135                 */
136                var $instance_name='';
137                /**
138                 * @var int $instance_owner Instance owner id
139                 * @access public
140                 */
141                var $instance_owner=0;
142                /**
143                 * @var string $owner_name Owner name
144                 * @access public
145                 */
146                var $owner_name='';
147
148                /**
149                 * @var bool $print_mode print mode
150                 * @access public
151                 */
152                var $print_mode = false;
153                /**
154                 * @var bool $enable_print_mode print mode
155                 * @access public
156                 */
157                var $enable_print_mode = false;
158
159                /**
160                 * @var array $act_role_names of roles associated with the activity, usefull for lists of users associated with theses roles
161                 * @access public
162                 */
163                var $act_role_names= Array();
164
165                /**
166                 * @var array $agents Array of ui_agent objects
167                 * @access public
168                 */
169                var $agents = Array();
170
171                /* CELEPAR */
172                /**
173                 * @var object $smarty holds a Smarty instance
174                 * @access public
175                 */
176                var $smarty;
177                /**
178                 * @var array $wf holds a global environment vector
179                 * @access public
180                 */
181                var $wf;
182                /**
183                 * @var $download_mode activates download mode
184                 * @access public
185                 */
186                var $download_mode;
187                /**
188                 * @var string $_template_name holds the template's file name
189                 * @access public
190                 */
191                var $_template_name = null;
192                /**
193                 * @var bool Indicates wether the current instance is a child instance or not
194         * @access public
195         */
196                var $isChildInstance = false;
197
198                private $workflowSmarty = null;
199                /* CELEPAR */
200                /**
201                 * Constructor
202                 *
203                 * @access public
204                 */
205                function run_activity()
206                {
207                        parent::WorkflowUtils();
208                        $this->runtime          = CreateObject('workflow.workflow_wfruntime');
209                        $this->runtime->setDebug(_DEBUG);
210                        //$this->base_activity  = CreateObject('workflow.workflow_baseactivity');
211                        //$this->process                = CreateObject('workflow.workflow_process');
212                        $this->GUI              = CreateObject('workflow.workflow_gui');
213                        $this->categories       = CreateObject('phpgwapi.categories');
214
215                        $this->workflowSmarty = CreateObject('workflow.workflow_smarty', false);
216                        // TODO: open a new connection to the database under a different username to allow privilege handling on tables
217                        /* CELEPAR */
218                        unset($this->db);
219                }
220
221                /**
222                  * This function is used to run all activities for specified instances. it could be interactive activities
223                  * or automatic activities. this second case is the reason why we return some values
224                  * @param int $activityId is the activity_id it run
225                  * @param int $iid is the instance id it run for
226                  * @param $auto is true by default
227                  * @return mixed AN ARRAY, or at least true or false. This array can contain :
228                  * a key 'failure' with an error string the engine will retrieve in instance error messages in case of
229                  *     failure (this will mark your execution as Bad),
230                  * a key 'debug' with a debug string the engine will retrieve in instance error messages,
231                  */
232                function go($activity_id=0, $iid=0, $auto=0)
233                {
234                        $result=Array();
235
236                        if ($iid)
237                        {
238                                $_REQUEST['iid'] = $iid;
239                        }
240                        $iid = $_REQUEST['iid'];
241
242                        //$activity_id is set when we are in auto mode. In interactive mode we get if from POST or GET
243                        if (!$activity_id)
244                        {
245                                $activity_id    = (int)get_var('activity_id', array('GET','POST'), 0);
246                        }
247
248                        // load activity and instance
249                        if (!$activity_id)
250                        {
251                                $result['failure'] =  $this->runtime->fail(lang('Cannot run unknown activity'), true, _DEBUG, $auto);
252                                return $result;
253                        }
254
255                        //initalising activity and instance objects inside the WfRuntime object
256                        if (!($this->runtime->loadRuntime($activity_id,$iid)))
257                        {
258                                $result['failure'] = $this->runtime->fail(lang('Cannot run the activity'), true, _DEBUG, $auto);
259                                return $result;
260                        }
261
262                        $activity =& $this->runtime->getActivity($activity_id, true, true);
263                        $this->activity =& $activity;
264                        // the instance is avaible with $instance or $this->instance
265                        // note that for standalone activities this instance can be an empty instance object, but false is a bad value
266                        //$this->instance =& $this->runtime->loadInstance($iid);
267
268                        // HERE IS A BIG POINT: we map the instance to a runtime object
269                        // user code will manipulate a stance, thinking it's an instance, but it is
270                        // in fact a WfRuntime object, mapping all instance functions
271                        $this->instance =& $this->runtime;
272                        $instance =& $this->instance;
273                        $GLOBALS['workflow']['wf_runtime'] =& $this->runtime;
274                        if (!($instance))
275                        {
276                                $result['failure'] = $this->runtime->fail(lang('Cannot run the activity without instance'), true, _DEBUG, $auto);
277                                return $result;
278                        }
279                        $this->instance_id = $instance->getInstanceId();
280
281                        // load process
282                        $this->process =& $this->runtime->getProcess();
283                        if (!($this->process))
284                        {
285                                $result['failure'] = $this->runtime->fail(lang('Cannot run the activity without her process').$instance, true, _DEBUG, $auto);
286                                return $result;
287                        }
288
289                        //set some global variables needed
290                        $GLOBALS['workflow']['__leave_activity']=false;
291                        $GLOBALS['user'] = $GLOBALS['phpgw_info']['user']['account_id'];
292
293                        //load role names, just an information
294                        $this->act_role_names = $activity->getActivityRoleNames();
295
296                        //set some other usefull vars
297                        $this->activity_type    = $activity->getType();
298                        $this->process_id       = $activity->getProcessId();
299                        $this->activity_id      = $activity_id;
300                        $this->process_name     = $this->process->getName();
301                        $this->process_version  = $this->process->getVersion();
302                        $this->activity_name    = $activity->getName();
303                        $this->user_name        = $GLOBALS['phpgw']->accounts->id2name($GLOBALS['user']);
304                        $this->view_activity    = $this->GUI->gui_get_process_view_activity($this->process_id);
305
306                        //we set them in $GLOBALS['workflow'] as well
307                        $GLOBALS['workflow']['wf_activity_type']                        =& $this->activity_type;
308                        $GLOBALS['workflow']['wf_process_id']                           =& $this->process_id;
309                        $GLOBALS['workflow']['wf_activity_id']                          =& $this->activity_id;
310                        $GLOBALS['workflow']['wf_process_name']                         =& $this->process_name;
311                        $GLOBALS['workflow']['wf_normalized_name']                      =  $this->process->getNormalizedName();
312                        $GLOBALS['workflow']['wf_process_version']                      =& $this->process_version;
313                        $GLOBALS['workflow']['wf_activity_name']                        =& $this->activity_name;
314                        $GLOBALS['workflow']['wf_user_name']                            =& $this->user_name;
315                        $GLOBALS['workflow']['wf_user_id']                                      =& $GLOBALS['user'];
316                        $GLOBALS['workflow']['wf_view_activity']                        =& $this->view_activity;
317                        $GLOBALS['workflow']['wf_workflow_path']                        = $GLOBALS['phpgw_info']['server']['webserver_url'].SEP.'workflow';
318                        $GLOBALS['workflow']['wf_resources_path']                       = $GLOBALS['phpgw_info']['server']['webserver_url'] . SEP . 'workflow/redirect.php?pid=' . $this->process_id . '&file=';
319                        $GLOBALS['workflow']['wf_default_resources_path']       = $GLOBALS['workflow']['factory']->getInstance('TemplateServer')->generateLink('processes');
320                        $GLOBALS['workflow']['wf_workflow_resources_path']      = $GLOBALS['workflow']['factory']->getInstance('TemplateServer')->generateLink('');
321                        $GLOBALS['workflow']['wf_activity_url']                         = $GLOBALS['phpgw_info']['server']['webserver_url'].SEP.'index.php?menuaction=workflow.'.get_class($this).'.go&activity_id='.$activity_id;
322                        $GLOBALS['workflow']['wf_user_cnname']                          = $GLOBALS['workflow']['factory']->getInstance('WorkflowLDAP')->getName($GLOBALS['user']);
323                        $GLOBALS['workflow']['wf_back_link']                            = $GLOBALS['phpgw_info']['server']['webserver_url'].SEP.'workflow'.SEP.'index.php?start_tab=1';
324                        $GLOBALS['workflow']['wf_js_path']                                      = $GLOBALS['phpgw_info']['server']['webserver_url'].SEP.'workflow'.SEP.'js'.SEP.'jscode';
325                        $GLOBALS['workflow']['wf_user_activities']                      = $this->GUI->gui_list_user_activities($GLOBALS['user'], '0', '-1', 'ga.wf_name__ASC', '', '', false, true, true, true, '');
326                        if ($iid)
327                                $GLOBALS['workflow']['wf_instance_url'] = $GLOBALS['phpgw_info']['server']['webserver_url'].SEP.'index.php?menuaction=workflow.'.get_class($this).'.go&activity_id='.$activity_id."&iid=".$iid;
328                        else
329                                unset($GLOBALS['workflow']['wf_instance_url']);
330                        $wf =& $GLOBALS['workflow'];
331
332                        /* CELEPAR */
333                        /* path to the local functions developed by Celepar */
334                        $functions = PHPGW_SERVER_ROOT . SEP . 'workflow' . SEP . 'inc' . SEP . 'local' . SEP . 'functions' . SEP . 'local.functions.php';
335
336                        /* activate local functions */
337                        require_once($functions);
338                        /* CELEPAR */
339
340                        //get configuration options with default values if no init was done before
341                        $myconf = array(
342                                'execute_activities_in_debug_mode'      => 0,
343                                'execute_activities_using_secure_connection' => 0
344                        );
345                        //this will give use asked options and som others used by WfRuntime
346                        $this->conf =& $this->runtime->getConfigValues($myconf);
347                        if ($this->conf['execute_activities_using_secure_connection'])
348                        {
349                                if (($GLOBALS['phpgw_info']['server']['use_https'] > 0) && ($_SERVER['HTTPS'] != 'on') && (!isset($GLOBALS['workflow']['job']['processID'])))
350                                {
351                                        header("Location: https://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}");
352                                        exit;
353                                }
354                        }
355
356                        if ($auto && (!$this->isChildInstance) && $activity->isInteractive())
357                        {
358                                $actualUser = $GLOBALS['user'];
359                                $actualUserGroups = $_SESSION['phpgw_info']['workflow']['user_groups'];
360                                foreach ($instance->instance->activities as $actTmp)
361                                {
362                                        if ($actTmp['wf_activity_id'] == $activity_id)
363                                        {
364                                                $newUser = $actTmp['wf_user'];
365                                                break;
366                                        }
367                                }
368
369                                /* pretend to be the user */
370                                $_SESSION['phpgw_info']['workflow']['user_groups'] = galaxia_retrieve_user_groups($newUser);
371                                $_SESSION['phpgw_info']['workflow']['account_id'] = $newUser;
372                                $GLOBALS['phpgw_info']['user']['account_id'] = $newUser;
373                                $GLOBALS['workflow']['wf_user_id'] = $newUser;
374                                $GLOBALS['user'] = $newUser;
375
376                                /* check some permissions */
377                                if (($newUser == '*') || (!$this->runtime->checkUserRun($newUser)) || ($GLOBALS['workflow']['factory']->getInstance('WorkflowLDAP')->getName($newUser) === false))
378                                {
379                                        $_SESSION['phpgw_info']['workflow']['user_groups'] = $actualUserGroups;
380                                        $_SESSION['phpgw_info']['workflow']['account_id'] = $actualUser;
381                                        $GLOBALS['phpgw_info']['user']['account_id'] = $actualUser;
382                                        $GLOBALS['workflow']['wf_user_id'] = $actualUser;
383                                        $GLOBALS['user'] = $actualUser;
384
385                                        return false;
386                                }
387                                $smarty = CreateObject('workflow.process_smarty');
388                        }
389
390                        // run the activity
391                        //interactive section
392                        if (!$auto && $activity->isInteractive())
393                        {
394
395                                $this->print_mode = get_var('print_mode', array('POST','GET'), false);
396                                $this->download_mode = get_var('download_mode', array('POST','GET'), false);
397
398                                /* #celepar# smarty code*/
399
400                                $smarty = CreateObject('workflow.process_smarty');
401
402                                $smarty->template_dir  = GALAXIA_PROCESSES.SEP.$this->process->getNormalizedName().SEP.'code'.SEP.'templates';
403                                $smarty->compile_dir   = GALAXIA_PROCESSES.SEP.$this->process->getNormalizedName().SEP.'smarty'.SEP.'compiled';
404                                $smarty->config_dir    = GALAXIA_PROCESSES.SEP.$this->process->getNormalizedName().SEP.'code'.SEP.'templates';
405                                $smarty->cache_dir     = GALAXIA_PROCESSES.SEP.$this->process->getNormalizedName().SEP.'smarty'.SEP.'cache';
406                                $smarty->plugins_dir[] = PHPGW_SERVER_ROOT.SEP.'workflow'.SEP.'inc'.SEP.'smarty'.SEP.'wf_plugins';
407
408                                $GLOBALS['phpgw']->template =& $smarty;
409                                $this->wf_template =& $smarty;
410                                $_template_name = null;
411
412                                //set resource path to use in templates
413                                $smarty->assign('wf_resources_path',$GLOBALS['workflow']['wf_resources_path']);
414                                $smarty->assign('wf_default_resources_path', $GLOBALS['workflow']['wf_default_resources_path']);
415                                $smarty->assign('wf_workflow_resources_path', $GLOBALS['workflow']['wf_workflow_resources_path']);
416                                $smarty->assign('wf_workflow_path',$GLOBALS['workflow']['wf_workflow_path']);
417                                $smarty->assign('wf_js_path',$GLOBALS['workflow']['wf_js_path']);
418                                $smarty->assign('wf_back_link',$GLOBALS['workflow']['wf_back_link']);
419                                $smarty->assign('wf_activity_url',$GLOBALS['workflow']['wf_activity_url']);
420
421                                /* register the prefilter smarty plugin wf_default_template */
422                                $smarty->load_filter('pre', 'wf_default_template');
423                        }
424
425                        if ($this->conf['execute_activities_in_debug_mode'])
426                        {
427                                ini_set('display_errors',true);
428                                error_reporting(E_ALL & ~E_NOTICE);
429                        }
430
431                        /* Global to control workflow database connection */
432                        $GLOBALS['workflow_db'] = array();
433
434                        /* BEGIN WORKFLOW MVC SETTINGS */
435                        $env = array( );                                           //create settings vector
436                        $env['view']          =& $smarty;                          //view layer instance
437                        $env['template_file'] =& $_template_name;                  //template file to be shown
438                        $env['dao']           =& wf_create_object('wf_db');        //data access object instance
439                        $env['workflow']      =& $GLOBALS['workflow'];             //workflow environment information
440                        $env['instance']      =& $instance;                        //process manager instance
441                        $env['activity']      =& $activity;                        //activity manager instance
442                        $security             =& new SecurityUtils();              //input sanitizer class
443                        $env['request']       =& $security->process($_REQUEST);    //sanitizes input data from client
444                        $env['factory']       =& new ProcessFactory();             //instantiation controller class
445                        $env['natural']           =& wf_create_object('wf_natural');   //data access object instance for mainframe
446                        /* END WORKFLOW MVC SETTINGS */
447
448                        $GLOBALS['workflow_env'] = &$env;
449
450                        //echo "<br><br><br><br><br>Including $source <br>In request: <pre>";print_r($_REQUEST);echo "</pre>";
451                        //[__leave_activity] is setted if needed in the xxx_pre code or by the user in his code
452                        // HERE the user code is 'executed'. Note that we do not use include_once or require_once because
453                        //it could the same code several times with automatic activities looping in the graph and it still
454                        //need to be executed
455                        $_engineProcessCodeDirectory = GALAXIA_PROCESSES . SEP . $this->process->getNormalizedName(). SEP . 'code';
456                        $_engineCompilerDirectory = GALAXIA_LIBRARY . SEP . 'compiler';
457                        $_engineFiles = array();
458
459                        /* generate the list of needed files */
460                        $_engineFiles[] = "{$_engineProcessCodeDirectory}/shared.php";
461                        $_engineFiles[] = "{$_engineCompilerDirectory}/_shared_pre.php";
462                        $_engineFiles[] = "{$_engineCompilerDirectory}/{$activity->getType()}_pre.php";
463                        if ($activity->getAgents() !== false)
464                                $_engineFiles[] = "{$_engineCompilerDirectory}/agents_pre.php";
465                        $_engineFiles[] = "{$_engineProcessCodeDirectory}/activities/{$activity->getNormalizedName()}.php";
466                        $_engineFiles[] = "{$_engineCompilerDirectory}/{$activity->getType()}_pos.php";
467                        if ($activity->getAgents() !== false)
468                                $_engineFiles[] = "{$_engineCompilerDirectory}/agents_pos.php";
469                        $_engineFiles[] = "{$_engineCompilerDirectory}/_shared_pos.php";
470
471                        /* check if the required files exists */
472                        foreach ($_engineFiles as $_engineFile)
473                                if (!file_exists($_engineFile))
474                                        return array('failure' => $this->runtime->fail(lang('the following file could not be found: %1', $_engineFile), true, _DEBUG));
475
476                        /* activate the security policy */
477                        $GLOBALS['workflow']['factory']->getInstance('WorkflowSecurity')->enableSecurityPolicy();
478
479                        /* include the files */
480                        foreach ($_engineFiles as $_engineFile)
481                                require $_engineFile;
482
483                        /* disconnect the workflow database objects that are still connected */
484                        while (count($GLOBALS['workflow_db']) > 0) {
485                                $wfdb_obj = $GLOBALS['workflow_db'][count($GLOBALS['workflow_db'])];
486                                if ($wfdb_obj && $wfdb_obj->Link_ID != 0) {
487                                        $wfdb_obj->disconnect();
488                                }
489                                unset($GLOBALS['workflow_db'][count($GLOBALS['workflow_db'])]);
490                        }
491                        unset($GLOBALS['workflow_db']);
492
493                        unset($GLOBALS['workflow_env']);
494
495                        /* check if the developer wants to user the download mode */
496                        if (isset($GLOBALS['workflow']['downloadMode']) && ($GLOBALS['workflow']['downloadMode'] == true))
497                                $this->download_mode = true;
498
499                        if ($auto && (!$this->isChildInstance) && $activity->isInteractive() && (!empty($actualUser)))
500                        {
501                                $_SESSION['phpgw_info']['workflow']['user_groups'] = $actualUserGroups;
502                                $_SESSION['phpgw_info']['workflow']['account_id'] = $actualUser;
503                                $GLOBALS['phpgw_info']['user']['account_id'] = $actualUser;
504                                $GLOBALS['workflow']['wf_user_id'] = $actualUser;
505                                $GLOBALS['user'] = $actualUser;
506                        }
507
508                        //Now that the instance is ready and that user code has maybe change some things
509                        // we can catch some others usefull vars
510                        $this->instance_id      = $instance->getInstanceId();
511                        $this->instance_name    = $instance->getName();
512                        $this->instance_owner   = $instance->getOwner();
513                        $this->owner_name       = $GLOBALS['phpgw']->accounts->id2name($this->instance_owner);
514                        if ($this->owner_name == '')
515                        {
516                                $this->owner_name = lang('Nobody');
517                        }
518                        $GLOBALS['workflow']['wf_instance_id']  =& $this->instance_id;
519                        $GLOBALS['workflow']['wf_instance_name']=& $this->instance_name;
520                        $GLOBALS['workflow']['wf_instance_owner']=& $this->instance_owner;
521                        $GLOBALS['workflow']['wf_owner_name']=& $this->owner_name;
522
523                        //was template changed?
524                        if ($_template_name)
525                        {
526                                $this->_template_name = $_template_name;
527                        }
528                        else
529                        {
530                                $this->_template_name = $this->activity->getNormalizedName().'.tpl';
531                        }
532
533                        // TODO: process instance comments
534
535                        $instructions = $this->runtime->handle_postUserCode(_DEBUG);
536                        switch($instructions['action'])
537                        {
538                                //interactive activity completed
539                                case 'completed':
540                                        // re-retrieve instance data which could have been modified by an automatic activity
541                                        $this->instance_id                = $instance->getInstanceId();
542                                        $this->instance_name              = $instance->getName();
543                                        $this->activityCompleteMessage    = $instance->getActivityCompleteMessage();
544
545                                        if (!$auto)
546                                        {
547                                                $this->assignCommonVariables();
548                                                // and display completed template
549                                                if ($GLOBALS['phpgw_info']['user']['preferences']['workflow']['show_activity_complete_page'] === '0')
550                                                        header('Location: workflow/index.php');
551                                                else
552                                                        $this->showCompletedPage();
553                                        }
554                                        break;
555                                //interactive activity still in interactive mode
556                                case 'loop':
557                                        if (!$auto)
558                                        {
559                                                $this->assignCommonVariables();
560                                                $this->showForm();
561                                        }
562                                        break;
563                                //nothing more
564                                case 'leaving':
565                                        if (!$auto)
566                                        {
567                                                $this->assignCommonVariables();
568                                                $this->showCancelledPage();
569                                        }
570                                        break;
571                                //non-interactive activities, auto-mode
572                                case 'return':
573                                        $result=Array();
574                                        $this->message[] = $this->GUI->get_error(false, _DEBUG);
575                                        $this->message[] = $this->runtime->get_error(false, _DEBUG);
576                                        //$this->message[] = $this->process->get_error(false, _DEBUG);
577                                        $result =& $instructions['engine_info'];
578                                        $this->message[] = $result['debug'];
579                                        $result['debug'] = implode('<br />',array_filter($this->message));
580                                        return $result;
581                                        break;
582                                default:
583                                        return $this->runtime->fail(lang('unknown instruction from the workflow engine: %1', $instructions['action']), true, _DEBUG);
584                                        break;
585                        }
586                }
587
588                /**
589                 * goajax
590                 *
591                 * @param int $activity_id
592                 * @param int $iid
593                 * @param bool $auto
594                 * @return array
595                 */
596                function goAjax($activity_id=0, $iid=0, $auto=0)
597                {
598                        $result=Array();
599
600                        if ($iid)
601                                $_REQUEST['iid'] = $iid;
602                        $iid = $_REQUEST['iid'];
603
604                        //$activity_id is set when we are in auto mode. In interactive mode we get if from POST or GET
605                        if (!$activity_id)
606                                $activity_id    = (int)get_var('activity_id', array('GET','POST'), 0);
607
608                        // load activity and instance
609                        if (!$activity_id)
610                        {
611                                $result['failure'] =  $this->runtime->fail(lang('Cannot run unknown activity'), true, _DEBUG, $auto);
612                                return $result;
613                        }
614
615                        //initalising activity and instance objects inside the WfRuntime object
616                        if (!($this->runtime->loadRuntime($activity_id,$iid)))
617                        {
618                                $result['failure'] = $this->runtime->fail(lang('Cannot run the activity'), true, _DEBUG, $auto);
619                                return $result;
620                        }
621
622                        $activity =& $this->runtime->getActivity($activity_id, true, true);
623                        $this->activity =& $activity;
624                        // the instance is avaible with $instance or $this->instance
625                        // note that for standalone activities this instance can be an empty instance object, but false is a bad value
626                        // HERE IS A BIG POINT: we map the instance to a runtime object
627                        // user code will manipulate a stance, thinking it's an instance, but it is
628                        // in fact a WfRuntime object, mapping all instance functions
629                        $this->instance =& $this->runtime;
630                        $instance =& $this->instance;
631                        $GLOBALS['workflow']['wf_runtime'] =& $this->runtime;
632                        if (!($instance))
633                        {
634                                $result['failure'] = $this->runtime->fail(lang('Cannot run the activity without instance'), true, _DEBUG, $auto);
635                                return $result;
636                        }
637                        $this->instance_id = $instance->getInstanceId();
638
639                        // load process
640                        $this->process =& $this->runtime->getProcess();
641                        if (!($this->process))
642                        {
643                                $result['failure'] = $this->runtime->fail(lang('Cannot run the activity without her process').$instance, true, _DEBUG, $auto);
644                                return $result;
645                        }
646
647                        //set some global variables needed
648                        $GLOBALS['user'] = $GLOBALS['phpgw_info']['user']['account_id'];
649
650                        //load role names, just an information
651                        $this->act_role_names = $activity->getActivityRoleNames();
652
653                        //set some other usefull vars
654                        $this->activity_type    = $activity->getType();
655                        $this->process_id       = $activity->getProcessId();
656                        $this->activity_id      = $activity_id;
657                        $this->process_name     = $this->process->getName();
658                        $this->process_version  = $this->process->getVersion();
659                        $this->activity_name    = $activity->getName();
660                        $this->user_name        = $GLOBALS['phpgw']->accounts->id2name($GLOBALS['user']);
661                        $this->view_activity    = $this->GUI->gui_get_process_view_activity($this->process_id);
662
663                        //we set them in $GLOBALS['workflow'] as well
664                        $GLOBALS['workflow']['wf_activity_type']                        =& $this->activity_type;
665                        $GLOBALS['workflow']['wf_process_id']                           =& $this->process_id;
666                        $GLOBALS['workflow']['wf_activity_id']                          =& $this->activity_id;
667                        $GLOBALS['workflow']['wf_process_name']                         =& $this->process_name;
668                        $GLOBALS['workflow']['wf_normalized_name']                      =  $this->process->getNormalizedName();
669                        $GLOBALS['workflow']['wf_process_version']                      =& $this->process_version;
670                        $GLOBALS['workflow']['wf_activity_name']                        =& $this->activity_name;
671                        $GLOBALS['workflow']['wf_user_name']                            =& $this->user_name;
672                        $GLOBALS['workflow']['wf_user_id']                                      =& $GLOBALS['user'];
673                        $GLOBALS['workflow']['wf_view_activity']                        =& $this->view_activity;
674                        $GLOBALS['workflow']['wf_workflow_path']                        = $GLOBALS['phpgw_info']['server']['webserver_url'].SEP.'workflow';
675                        $GLOBALS['workflow']['wf_resources_path']                       = $GLOBALS['phpgw_info']['server']['webserver_url'] . SEP . 'workflow/redirect.php?pid=' . $this->process_id . '&file=';
676                        $GLOBALS['workflow']['wf_default_resources_path']       = $GLOBALS['workflow']['factory']->getInstance('TemplateServer')->generateLink('processes');
677                        $GLOBALS['workflow']['wf_workflow_resources_path']      = $GLOBALS['workflow']['factory']->getInstance('TemplateServer')->generateLink('');
678                        $GLOBALS['workflow']['wf_activity_url']                         = $GLOBALS['phpgw_info']['server']['webserver_url'].SEP.'index.php?menuaction=workflow.'.get_class($this).'.go&activity_id='.$activity_id;
679                        $GLOBALS['workflow']['wf_user_cnname']                          = $GLOBALS['workflow']['factory']->getInstance('WorkflowLDAP')->getName($GLOBALS['user']);
680                        $GLOBALS['workflow']['wf_back_link']                            = $GLOBALS['phpgw_info']['server']['webserver_url'].SEP.'workflow'.SEP.'index.php?start_tab=1';
681                        $GLOBALS['workflow']['wf_js_path']                                      = $GLOBALS['phpgw_info']['server']['webserver_url'].SEP.'workflow'.SEP.'js'.SEP.'jscode';
682                        $GLOBALS['workflow']['wf_user_activities']                      = $this->GUI->gui_list_user_activities($GLOBALS['user'], '0', '-1', 'ga.wf_name__ASC', '', '', false, true, true, true, '');
683                        if ($iid)
684                                $GLOBALS['workflow']['wf_instance_url'] = $GLOBALS['phpgw_info']['server']['webserver_url'].SEP.'index.php?menuaction=workflow.'.get_class($this).'.go&activity_id='.$activity_id."&iid=".$iid;
685                        else
686                                unset($GLOBALS['workflow']['wf_instance_url']);
687
688                        /* CELEPAR */
689                        /* activate local functions */
690                        require_once(PHPGW_SERVER_ROOT . SEP . 'workflow' . SEP . 'inc' . SEP . 'local' . SEP . 'functions' . SEP . 'local.functions.php');
691                        /* CELEPAR */
692
693                        //get configuration options with default values if no init was done before
694                        $myconf = array(
695                                'execute_activities_in_debug_mode'      => 0,
696                                'execute_activities_using_secure_connection' => 0
697                        );
698                        //this will give use asked options and som others used by WfRuntime
699                        $this->conf =& $this->runtime->getConfigValues($myconf);
700
701                        // run the activity
702                        if ($this->conf['execute_activities_in_debug_mode'])
703                        {
704                                ini_set('display_errors',true);
705                                error_reporting(E_ALL & ~E_NOTICE);
706                        }
707
708                        /* Global to control workflow database connection */
709                        $GLOBALS['workflow_db'] = array();
710
711                        /* BEGIN WORKFLOW MVC SETTINGS */
712                        $env = array( );                                           //create settings vector
713                        $env['dao']           =& wf_create_object('wf_db');        //data access object instance
714                        $env['workflow']      =& $GLOBALS['workflow'];             //workflow environment information
715                        $env['instance']      =& $instance;                        //process manager instance
716                        $env['activity']      =& $activity;                        //activity manager instance
717                        $security             =& new SecurityUtils();              //input sanitizer class
718                        $env['request']       =& $security->process($_REQUEST); //sanitizes input data from client
719                        $env['factory']       =& new ProcessFactory();                     //instantiation controller class
720                        $env['natural']           =& wf_create_object('wf_natural');   //data access object instance for mainframe
721                        /* END WORKFLOW MVC SETTINGS */
722
723                        require_once(dirname(__FILE__) . SEP . 'nano' . SEP . 'JSON.php');
724                        require_once(dirname(__FILE__) . SEP . 'nano' . SEP . 'NanoUtil.class.php');
725                        require_once(dirname(__FILE__) . SEP . 'nano' . SEP . 'NanoJsonConverter.class.php');
726                        require_once(dirname(__FILE__) . SEP . 'nano' . SEP . 'NanoRequest.class.php');
727                        require_once(dirname(__FILE__) . SEP . 'nano' . SEP . 'NanoController.class.php');
728
729                        /* activate the security policy */
730                        $GLOBALS['workflow']['factory']->getInstance('WorkflowSecurity')->enableSecurityPolicy();
731
732                        $GLOBALS['workflow_env'] = &$env;
733                        $nc = new NanoController();
734                        $nc->setClassPath(GALAXIA_PROCESSES . SEP . $this->process->getNormalizedName(). SEP . 'code');
735                        $nc->iterateOverVirtualRequests();
736                        $nc->outputResultData();
737
738                        /* disconnect the workflow database objects that are still connected */
739                        while (count($GLOBALS['workflow_db']) > 0) {
740                                $wfdb_obj = $GLOBALS['workflow_db'][count($GLOBALS['workflow_db'])];
741                                if ($wfdb_obj && $wfdb_obj->Link_ID != 0) {
742                                        $wfdb_obj->disconnect();
743                                }
744                                unset($GLOBALS['workflow_db'][count($GLOBALS['workflow_db'])]);
745                        }
746                        unset($GLOBALS['workflow_db']);
747
748                        unset($GLOBALS['workflow_env']);
749
750                        if (!is_null($iid))
751                                $instance->instance->sync();
752                }
753
754                /**
755                 * Create a child instance
756                 *
757                 * @param int $activityID
758                 * @param mixed $properties
759                 * @param string $user
760                 * @param bool $parentLock
761                 * @return int The instance ID of the just created instance
762                 * @access public
763                 */
764                function goChildInstance($activityID, $properties, $user, $parentLock)
765                {
766                        $this->isChildInstance = true;
767                        $this->runtime->instance->isChildInstance = true;
768                        $this->runtime->instance->activityID = $activityID;
769                        $this->runtime->instance->parentLock = $parentLock;
770                        $this->runtime->instance->setProperties($properties);
771                        if ($user != '*')
772                                $this->runtime->setNextUser($user);
773
774                        /* run the selected activity */
775                        ob_start();
776                        $this->go($activityID, 0, true);
777                        ob_end_clean();
778
779                        /* return the just created child instance */
780                        return $this->runtime->instance_id;
781                }
782
783                /**
784                 * Show the page avaible when completing an activity
785                 * @return void
786                 * @access public
787                 */
788                function showCompletedPage()
789                {
790                        $this->workflowSmarty->assign('activityEvent', 'completed');
791                        $this->showAfterRunningPage();
792                }
793
794                /**
795                 * Show the page avaible when leaving an activity
796                 * @return void
797                 * @access public
798                 */
799                function showCancelledPage()
800                {
801                        $this->workflowSmarty->assign('activityEvent', 'cancelled');
802                        $this->showAfterRunningPage();
803                }
804
805                /**
806                 * Common code of pages showed after activity pages
807                 * @return void
808                 * @access public
809                 */
810                function showAfterRunningPage()
811                {
812                        /* get the header/footer */
813                        $this->assignHeader();
814
815                        /* generate the activity list */
816                        $processActivities = array_filter($GLOBALS['workflow']['wf_user_activities']['data'], create_function('$a', 'return ($a["wf_p_id"] == ' . $GLOBALS['workflow']['wf_process_id'] . ');'));
817                        $activityList = array(0 => '-- Selecione uma atividade --');
818                        foreach ($processActivities as $processActivity)
819                                $activityList[$processActivity['wf_activity_id']] = $processActivity['wf_name'];
820
821                        /* assign some variables */
822                        $this->workflowSmarty->assign('processName', $this->process_name);
823                        $this->workflowSmarty->assign('processVersion', $this->process_version);
824                        $this->workflowSmarty->assign('activityName', $this->activity_name);
825                        $this->workflowSmarty->assign('activityCompleteMessage', $this->activityCompleteMessage);
826                        $this->workflowSmarty->assign('activityBaseURL', $GLOBALS['phpgw_info']['server']['webserver_url']);
827                        $this->workflowSmarty->assign('activityList', $activityList);
828
829                        /* display the template */
830                        $this->workflowSmarty->display('after_running.tpl');
831                }
832
833                /**
834                 * Assign common information of interactive forms (e.g., error messages)
835                 * @return void
836                 * @access public
837                 */
838                function assignCommonVariables()
839                {
840                        $this->message[] = $this->GUI->get_error(false, _DEBUG);
841                        $this->message[] = $this->runtime->get_error(false, _DEBUG);
842                        $activityErrors = array_filter(array_merge(explode('<br />', $this->message[0]), explode('<br />', $this->message[1])));
843                        $this->workflowSmarty->assign('activityErrors', $activityErrors);
844                }
845
846                /**
847                 * Show the activity page (workflow template and activity template)
848                 * @return void
849                 * @access public
850                 */
851                function showForm()
852                {
853                        /* define the header */
854                        $this->assignHeader();
855
856                        /* define the variables */
857                        $activityOutput = $this->wf_template->fetch($this->_template_name);
858                        $actionURL = isset($GLOBALS['workflow']['wf_instance_url']) ? $GLOBALS['workflow']['wf_instance_url'] : $GLOBALS['workflow']['wf_activity_url'];
859                        $CSSLink = $this->get_css_link('run_activity', $this->print_mode);
860                        $CSSMedia = $this->print_mode ? 'print' : 'all';
861
862                        /* assign the variables to smarty */
863                        $this->workflowSmarty->assign('activityOutput', $activityOutput);
864                        $this->workflowSmarty->assign('actionURL', $actionURL);
865                        $this->workflowSmarty->assign('CSSLink', $CSSLink);
866                        $this->workflowSmarty->assign('CSSMedia', $CSSMedia);
867
868                        $this->workflowSmarty->display('run_activity.tpl');
869                        unset($smarty);
870                }
871
872                /**
873                 * Define if the header, footer and navigation bar will be shown
874                 * @return void
875                 * @access public
876                 */
877                function assignHeader()
878                {
879                        $headerConfig = 0;
880                        if (!$this->download_mode)
881                        {
882                                $headerConfig |= workflow_smarty::SHOW_HEADER | workflow_smarty::SHOW_FOOTER;
883                                if ($this->runtime->activity->child_name != 'View')
884                                        $headerConfig |= workflow_smarty::SHOW_NAVIGATION_BAR;
885                        }
886
887                        $this->workflowSmarty->setHeader($headerConfig);
888                        $this->workflowSmarty->assign('header', $this->workflowSmarty->expressoHeader);
889                        $this->workflowSmarty->assign('footer', $this->workflowSmarty->expressoFooter);
890                }
891        }
892?>
Note: See TracBrowser for help on using the repository browser.