source: sandbox/workflow/flumen/inc/class.run_activity.inc.php @ 1293

Revision 1293, 33.9 KB checked in by gbisotto, 15 years ago (diff)

Ticket #609 - Modificada a classe Factory e ProcessFactory? para automatizaro acesso ao objetos

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