source: sandbox/workflow/trunk/inc/class.run_activity.inc.php @ 2372

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

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

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