source: sandbox/workflow/branches/993/inc/class.WorkflowUtils.inc.php @ 2544

Revision 2544, 12.8 KB checked in by pedroerp, 14 years ago (diff)

Ticket #993 - Inserindo parâmetros 'app name' e 'app version' na Settings.

  • Property svn:executable set to *
Line 
1<?php
2        require_once 'common.inc.php';
3
4        //set here the global DEBUG level which is actually 0 (nothing) or 1 (all)
5        if (!defined('_DEBUG')) define('_DEBUG', 0);
6        /**
7         * @package Workflow
8         * @license http://www.gnu.org/copyleft/gpl.html GPL
9         */
10        class WorkflowUtils
11        {
12                /**
13                 * @var array $public_functions Array of public functions
14                 * @access public
15                 */
16                var $public_functions = array(
17                        'export'        => true,
18                );
19                /**
20                 * @var object $t the template
21                 * @access public
22                 */
23                var $t;         
24                /**
25                 * @var int wf_p_id 
26                 * @access public
27                 */
28                var $wf_p_id;
29                /**
30                 * @var array $message message array
31                 * @access public
32                 */
33                var $message = array();
34
35                //TODO: when migration to bo_workflow_forms will be closed erase theses vars--------------
36                //nextmatchs (max number of rows per page) and associated vars
37               
38                /**
39                 * @var int $nextmatchs
40                 * @access public
41                 */
42                var $nextmatchs;
43               
44                /**
45                 * @var int $start actual starting row number
46                 * @access public
47                 */
48                var $start;
49                /**
50                 * @var int $total_records total number of rows
51                 * @access public
52                 */
53                var $total_records;
54                /**
55                 * @var array $message message array column used for order
56                 * @access public
57                 */
58                var $order;
59                /**
60                 * @var string $sort ASC or DESC
61                 * @access public
62                 */
63                var $sort;
64                /**
65                 * @var array $sort_mode combination of order and sort   
66                 * @access public
67                 */
68                var $sort_mode;
69                /**
70                 * @var array $search_str 
71                 * @access public
72                 */
73                var $search_str;
74                //------------------------------------------------------------------------------------------
75                /**
76                 * @var array $stats
77                 * @access public
78                 */
79                var $stats;
80                /**
81                 * @var array $wheres
82                 * @access public
83                 */
84                var $wheres = array();
85
86                /**
87                 * Constructor of workflow class
88                 * 
89                 * @access public
90                 * @return void
91                 */
92                function WorkflowUtils()
93                {
94                        // check version
95                        $version = Settings::get('workflow', 'app', 'version');
96                        if (alessthanb($version, '1.2.01.006'))
97                        {
98                                $GLOBALS['phpgw']->common->phpgw_header();
99                                echo parse_navbar();
100                                die("Please upgrade this application to be able to use it");
101                        }
102
103                        $this->t                =& $GLOBALS['phpgw']->template;
104                        $this->wf_p_id          = (int)get_var('p_id', 'any', 0);
105                        $this->start            = (int)get_var('start', 'any', 0);
106                        $this->search_str       = get_var('find', 'any', '');
107                        $this->nextmatchs       = Factory::getInstance('nextmatchs');
108                }
109
110                /**
111                 * Fill the process bar
112                 *
113                 * @param array $proc_info
114                 * @access public
115                 * @return string
116                 */
117                function fill_proc_bar($proc_info)
118                {
119                        //echo "proc_info: <pre>";print_r($proc_info);echo "</pre>";
120                        $this->t->set_file('proc_bar_tpl', 'proc_bar.tpl');
121                        $templateServer = &Factory::getInstance('TemplateServer');
122
123                        if ($proc_info['wf_is_valid'] == 'y')
124                        {
125                                $dot_color = 'green';
126                                $alt_validity = lang('valid');
127                        }
128                        else
129                        {
130                                $dot_color = 'red';
131                                $alt_validity = lang('invalid');
132                        }
133
134                        // if process is active show stop button. Else show start button, but only if it is valid. If it's not valid, don't show any activation or stop button.
135                        if ($proc_info['wf_is_active'] == 'y')
136                        {
137                                $start_stop = '<td><a href="'. $GLOBALS['phpgw']->link('/index.php', 'menuaction=workflow.ui_adminactivities.form&p_id='. $proc_info['wf_p_id'] .'&deactivate_proc='. $proc_info['wf_p_id']) .'"><img border ="0" src="'. $templateServer->generateImageLink('stop.gif') .'" alt="'. lang('stop') .'" title="'. lang('stop') .'" />'.lang('stop').'</a></td>';
138                        }
139                        elseif ($proc_info['wf_is_valid'] == 'y')
140                        {
141                                $start_stop = '<td><a href="'. $GLOBALS['phpgw']->link('/index.php', 'menuaction=workflow.ui_adminactivities.form&p_id='. $proc_info['wf_p_id'] .'&activate_proc='. $proc_info['wf_p_id']) .'"><img border ="0" src="'. $templateServer->generateImageLink('refresh2.gif') .'" alt="'. lang('activate') .'" title="'. lang('activate') .'" />'.lang('activate').'</a></td>';
142                        }
143                        else
144                        {
145                                $start_stop = '';
146                        }
147                        $this->t->set_var(array(
148                                'proc_name'                             => $proc_info['wf_name'],
149                                'version'                               => $proc_info['wf_version'],
150                                'img_validity'                  => $templateServer->generateImageLink($dot_color.'_dot.gif'),
151                                'alt_validity'                  => $alt_validity,
152                                'start_stop'                    => $start_stop,
153                                'link_admin_activities' => $GLOBALS['phpgw']->link('/index.php', 'menuaction=workflow.ui_adminactivities.form&p_id='. $proc_info['wf_p_id']),
154                                'img_activity'                  => $templateServer->generateImageLink('Activity.gif'),
155                                'link_admin_jobs'       => $GLOBALS['phpgw']->link('/index.php', 'menuaction=workflow.ui_adminjobs.form&p_id='. $proc_info['wf_p_id']),
156                                'img_job'                       => $templateServer->generateImageLink('clock.png'),
157                                'link_admin_processes'          => $GLOBALS['phpgw']->link('/index.php', 'menuaction=workflow.ui_adminprocesses.form&p_id='. $proc_info['wf_p_id']),
158                                'img_change'                    => $templateServer->generateImageLink('change.gif'),
159                                'link_admin_shared_source'      => $GLOBALS['phpgw']->link('/index.php', 'menuaction=workflow.ui_adminsource.form&p_id='. $proc_info['wf_p_id']),
160                                'img_code'                      => $templateServer->generateImageLink('code.png'),
161                                'link_admin_export'             => $GLOBALS['phpgw']->link('/index.php', 'menuaction=workflow.WorkflowUtils.export&p_id='. $proc_info['wf_p_id']),
162                                'link_admin_roles'              => $GLOBALS['phpgw']->link('/index.php', 'menuaction=workflow.ui_adminroles.form&p_id='. $proc_info['wf_p_id']),
163                                'img_roles'                     => $templateServer->generateImageLink('roles.png'),
164                                'link_graph'                    => $GLOBALS['phpgw']->link('/index.php', 'menuaction=workflow.ui_adminactivities.show_graph&p_id=' . $proc_info['wf_p_id']),
165                                'img_process'                   => $templateServer->generateImageLink('Process.gif'),
166                                'link_save_process'             => $GLOBALS['phpgw']->link('/index.php', 'menuaction=workflow.ui_adminprocesses.save_process&id='. $proc_info['wf_p_id']),
167                                'img_save'                      => $templateServer->generateImageLink('save.png')
168                        ));
169
170                        $this->translate_template('proc_bar_tpl');
171                        return $this->t->parse('proc_bar', 'proc_bar_tpl');
172                }
173                /**
174                 * Select activity representation icon
175                 *
176                 * @var string $type type of activity
177                 * @var bool   $interactive interactive?
178                 * @access public
179                 * @return void
180                 */
181                function act_icon($type, $interactive)
182                {
183                        switch($type)
184                        {
185                                case 'activity':
186                                        $ic = "mini_".(($interactive == 'y')? 'blue_':'')."rectangle.gif";
187                                        break;
188                                case 'switch':
189                                        $ic = "mini_".(($interactive == 'y')? 'blue_':'')."diamond.gif";
190                                        break;
191                                case 'start':
192                                        $ic="mini_".(($interactive == 'y')? 'blue_':'')."circle.gif";
193                                        break;
194                                case 'end':
195                                        $ic="mini_".(($interactive == 'y')? 'blue_':'')."dbl_circle.gif";
196                                        break;
197                                case 'split':
198                                        $ic="mini_".(($interactive == 'y')? 'blue_':'')."triangle.gif";
199                                        break;
200                                case 'join':
201                                        $ic="mini_".(($interactive == 'y')? 'blue_':'')."inv_triangle.gif";
202                                        break;
203                                case 'standalone':
204                                        $ic="mini_".(($interactive == 'y')? 'blue_':'')."hexagon.gif";
205                                        break;
206                                case 'view':
207                                        $ic="mini_blue_eyes.gif";
208                                        break;
209                                default:
210                                        $ic="no-activity.gif";
211                        }
212                        return '<img src="'. Factory::getInstance('TemplateServer')->generateImageLink($ic) .'" alt="'. lang($type) .'" title="'. lang($type) .'" />';
213                }
214
215                /**
216                 * Translate template file
217                 * @param string $template_name template name
218                 * @return void
219                 * @access public
220                 */
221                function translate_template($template_name)
222                {
223                        $undef = $this->t->get_undefined($template_name);
224                        if ($undef != False)
225                        {
226                                foreach ($undef as $value)
227                                {
228                                        $valarray = explode('_', $value);
229                                        $type = array_shift($valarray);
230                                        $newval = implode(' ', $valarray);
231                                        if ($type == 'lang')
232                                        {
233                                                $this->t->set_var($value, lang($newval));
234                                        }
235                                }
236                        }
237                }
238                /**
239                 * Show errors
240                 * @param object $activity_manager
241                 * @param string $error_str destination string to place errors
242                 * @return bool
243                 * @access public
244                 */
245                function show_errors(&$activity_manager, &$error_str)
246                {
247                        $valid = $activity_manager->validate_process_activities($this->wf_p_id);
248                        $errors = $activity_manager->get_error(true);
249                        $warnings = $activity_manager->get_warning(true);
250                        $tmp = array();
251
252                        /* remove empty errors from the error list */
253                        foreach ($errors as $index => $error)
254                                if (trim($error) == '')
255                                        unset($errors[$index]);
256
257                        if ((count($warnings) > 0) || (count($errors) > 0))
258                        {
259                                $error_str = '';
260                                $output = 'y';
261                                if (count($errors) > 0)
262                                {
263                                        $error_str = '<b>' . lang('The following items must be corrected to be able to activate this process').':</b><br/><small><ul>';
264                                        foreach ($errors as $error)
265                                        {
266                                                $error_str .= '<li>'. $error . '<br/>';
267                                        }
268                                        $error_str .= '</ul></small>';
269                                        $output = 'n';
270                                }
271
272                                if (count($warnings) > 0)
273                                {
274                                        if ($error_str != '')
275                                                $error_str .= "<br />";
276
277                                        $error_str .= '<b>' . lang('warnings in this process').':</b><br/><small><ul>';
278                                        foreach ($warnings as $warning)
279                                                if (trim($warning) != '')
280                                                        $error_str .= '<li>'. $warning . '<br/>';
281
282                                        $error_str .= '</ul></small>';
283                                }
284                                return $output;
285                        }
286                        else
287                        {
288                                $error_str = '';
289                                return 'y';
290                        }
291                }
292               
293                /**
294                 * Get source code
295                 * @param string $proc_name process name
296                 * @param string $act_name activity name
297                 * @param string $type actyvity type
298                 * @access public
299                 * @return string source code dat
300                 */
301                function get_source($proc_name, $act_name, $type)
302                {
303                        switch($type)
304                        {
305                                case 'code':
306                                        $path =  'activities' . SEP . $act_name . '.php';
307                                        break;
308                                case 'template':
309                                        $path = 'templates' . SEP . $act_name . '.tpl';
310                                        break;
311                                default:
312                                        $path = 'shared.php';
313                                        break;
314                        }
315                        $complete_path = GALAXIA_PROCESSES . SEP . $proc_name . SEP . 'code' . SEP . $path;
316                                                                                                if (!$file_size = filesize($complete_path)) return '';
317                        $fp = fopen($complete_path, 'r');
318                        $data = fread($fp, $file_size);
319                        fclose($fp);
320                        return $data;
321                }
322                /**
323                 * Save the source of process
324                 *
325                 * @param string $proc_name process name
326                 * @param string $act_name activity name
327                 * @param string $type type of activity
328                 * @param string $source source code of activity
329                 * @return void
330                 * @access public
331                 */
332                function save_source($proc_name, $act_name, $type, $source)
333                {
334                        // in case code was filtered
335                        if (!$source) $source = @$GLOBALS['egw_unset_vars']['_POST[source]'];
336
337                        switch($type)
338                        {
339                                case 'code':
340                                        $path =  'activities' . SEP . $act_name . '.php';
341                                        break;
342                                case 'template':
343                                        $path = 'templates' . SEP . $act_name . '.tpl';
344                                        break;
345                                default:
346                                        $path = 'shared.php';
347                                        break;
348                        }
349                        $complete_path = GALAXIA_PROCESSES . SEP . $proc_name . SEP . 'code' . SEP . $path;
350                        // In case you want to be warned when source code is changed:
351                        // mail('yourmail@domain.com', 'source changed', "PATH: $complete_path \n\n SOURCE: $source");
352                        $fp = fopen($complete_path, 'w');
353                        fwrite($fp, $source);
354                        fclose($fp);
355                }
356
357                /**
358                 * Export process to a xml file to be downloaded
359                 * @access public
360                 * @return void
361                 */
362                function export()
363                {
364                        $this->process_manager  = Factory::getInstance('workflow_processmanager');
365
366                        // retrieve process info
367                        $proc_info = $this->process_manager->get_process($this->wf_p_id);
368                        $filename = $proc_info['wf_normalized_name'].'.xml';
369                        $out = $this->process_manager->serialize_process($this->wf_p_id);
370                        $mimetype = 'application/xml';
371                        // MSIE5 and Opera show allways the document if they recognise. But we want to oblige them do download it, so we use the mimetype x-download:
372                        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 5') || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera 7'))
373                                $mimetype = 'application/x-download';
374                        // Show appropiate header for a file to be downloaded:
375                        header("content-disposition: attachment; filename=$filename");
376                        header("content-type: $mimetype");
377                        header('content-length: ' . strlen($out));
378                        echo $out;
379                }
380               
381                /**
382                * Get the href link for the css file, searching for themes specifics stylesheet if any
383                * @param string $css_name is the name of the css file, without the .css extension
384                * @param bool $print_mode is false by default, if true '_print.css' is appended to the name if this css print file exists
385                * @return string a string containing the link to a css file that you can use in a href, you'll have at least a link
386                * to a non-existent css in template/default/css/
387                * @access public
388                */
389                function get_css_link($css_name, $print_mode = false)
390                {
391                        $file = "css/$css_name" . (($print_mode !== false) ? '_print' : '') . '.css';
392                        return Factory::getInstance('TemplateServer')->getWebFile($file);
393                }
394
395                /**
396                 * Return a given duration in human readable form, usefull for workitems duration
397                 * @param int $to given duration
398                 * @return string given duration in human readable form
399                 * @access public
400                 */
401                function time_diff($to) {
402                        $days = (int)($to/(24*3600));
403                        $to = $to - ($days*(24*3600));
404                        $hours = (int)($to/3600);
405                        $to = $to - ($hours*3600);
406                        $min = date("i", $to);
407                        $to = $to - ($min*60);                 
408                        $sec = date("s", $to);
409
410                        return lang('%1 days, %2:%3:%4',$days,$hours,$min,$sec);
411                }
412
413        }
414?>
Note: See TracBrowser for help on using the repository browser.