source: trunk/workflow/inc/class.bo_adminsource.inc.php @ 7655

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

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

  • Property svn:executable set to *
Line 
1<?php
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
12require_once('class.bo_ajaxinterface.inc.php');
13
14/**
15 * Invalid file name identifier
16 * @name INVALID_FILE_NAME
17 */
18define( INVALID_FILE_NAME   , 0 );
19/**
20 * Invalid process id identifier
21 * @name INVALID_PROCESS_ID
22 */
23define( INVALID_PROCESS_ID  , 1 );
24/**
25 * File already exists identifier
26 * @name FILE_ALREADY_EXISTS
27 */
28define( FILE_ALREADY_EXISTS , 2 );
29/**
30 * File created indentifier
31 * @name FILE_CREATED
32 */
33define( FILE_CREATED            , 3 );
34/**
35 * File not created identifier
36 * @name FILE_NOT_CREATED
37 */
38define( FILE_NOT_CREATED        , 4 );
39/**
40 * @package Workflow
41 * @license http://www.gnu.org/copyleft/gpl.html GPL
42 * @author Rodrigo Daniel C de Lira - rodrigo.lira@gmail.com
43 * @author Sidnei Augusto Drovetto Junior - drovetto@gmail.com
44 */
45class bo_adminsource extends bo_ajaxinterface
46{
47
48        /**
49        * @var array $public_functions Array of public functions
50        * @access public
51        */
52        var $public_functions = array('export_file' => true
53                                      );
54       
55        /**
56        * Construtor
57        *
58        * @access public
59        */     
60        function bo_adminsource() {
61                parent::bo_ajaxinterface();             
62                }
63       
64        /**
65        * Assign Unit To File Size
66        * @param integer $value value
67        * @return string file size
68        * @access public
69        */
70        function _assignUnitToFileSize($value)
71        {
72                $fileSizeUnit = array();
73                $fileSizeUnit[] = 'bytes';
74                $fileSizeUnit[] = 'Kb';
75                $fileSizeUnit[] = 'Mb';
76                $fileSizeUnit[] = 'Gb';
77
78                $unitSelect = 0;
79                while ($value > 1024.0)
80                {
81                        $value /= 1024.0;
82                        ++$unitSelect;
83                }
84
85                $output = round($value, 1);
86                $output .= " " . $fileSizeUnit[$unitSelect];
87
88                return $output;
89        }
90
91        /**
92        * Get process toolbar data
93        * @param  array $p process process data
94        * @return array
95        * @access public
96        */
97        function get_toolbar_data($p)
98        {
99
100                $process_manager = &Factory::newInstance('ProcessManager');
101                $proc_info       = $process_manager->get_process($p['proc_id']);
102
103                $web_server_url  = $_SESSION['phpgw_info']['workflow']['server']['webserver_url'];
104                $img_default_dir = Factory::getInstance('TemplateServer')->generateImageLink('');
105
106                if ($proc_info['wf_is_valid'] == 'y')
107                {
108                        $dot_color = 'green';
109                        $alt_validity = tra('valid');
110                }
111                else
112                {
113                        $dot_color = 'red';
114                        $alt_validity = tra('invalid');
115                }
116
117                // 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.
118                if ($proc_info['wf_is_active'] == 'y')
119                {
120                        $start_stop_link = $web_server_url.'/index.php?menuaction=workflow.ui_adminactivities.form&p_id='. $proc_info['wf_p_id'] .'&deactivate_proc='. $proc_info['wf_p_id'];
121                        $start_stop_img  = $img_default_dir.'stop.gif';
122                        $start_stop_desc = 'Parar';
123                }
124                elseif ($proc_info['wf_is_valid'] == 'y')
125                {
126                        $start_stop_link = $web_server_url.'/index.php?menuaction=workflow.ui_adminactivities.form&p_id='. $proc_info['wf_p_id'] .'&activate_proc='. $proc_info['wf_p_id'];
127                        $start_stop_img  = $img_default_dir.'refresh2.gif';
128                        $start_stop_desc = 'Ativar';
129                }
130                else
131                {
132                        $start_stop_link = '';
133                        $start_stop_img  = '';
134                }
135
136                /* load other processes link */
137                $proc_ids = $GLOBALS['ajax']->acl->get_granted_processes($_SESSION['phpgw_info']['workflow']['account_id']);
138                if (count($proc_ids))
139                        $where = ' wf_p_id in ('. implode(',',$proc_ids) .') ';
140                else
141                        $where = ' wf_p_id = -1 ';
142
143                $processesInfo = &$process_manager->list_processes(0, -1, 'wf_name__asc', '', $where);
144                $otherProcesses = array();
145                foreach ($processesInfo['data'] as $pi)
146                        $otherProcesses[] = array("name" => $pi['wf_name'] . " (v" . $pi['wf_version'] . ")", "link" => $web_server_url . "/index.php?menuaction=workflow.ui_adminsource.form&p_id=" . $pi['wf_p_id'], "pid" => $pi['wf_p_id']);
147
148                $toolbar_data = array (
149                        'proc_name'                                     => $proc_info['wf_name'],
150                        'version'                                       => $proc_info['wf_version'],
151                        'img_validity'                          => $img_default_dir.$dot_color.'_dot.gif',
152                        'alt_validity'                          => $alt_validity,
153                        'start_stop_link'                       => $start_stop_link,
154                        'start_stop_img'                        => $start_stop_img,
155                        'start_stop_desc'                       => $start_stop_desc,
156                        'link_admin_activities'         => $web_server_url.'/index.php?menuaction=workflow.ui_adminactivities.form&p_id='. $proc_info['wf_p_id'],
157                        'img_activity'                          => $img_default_dir.'Activity.gif',
158                        'link_admin_jobs'               => $web_server_url.'/index.php?menuaction=workflow.ui_adminjobs.form&p_id='. $proc_info['wf_p_id'],
159                        'img_job'                               => $img_default_dir.'clock.png',
160                        'link_admin_processes'          => $web_server_url.'/index.php?menuaction=workflow.ui_adminprocesses.form&p_id='. $proc_info['wf_p_id'],
161                        'img_change'                            => $img_default_dir.'change.gif',
162                        'link_admin_shared_source'      => $web_server_url.'/index.php?menuaction=workflow.ui_adminsource.form&p_id='. $proc_info['wf_p_id'],
163                        'img_code'                                      => $img_default_dir.'code.gif',
164                        'link_admin_export'                     => $web_server_url.'/index.php?menuaction=workflow.WorkflowUtils.export&p_id='. $proc_info['wf_p_id'],
165                        'link_admin_roles'                      => $web_server_url.'/index.php?menuaction=workflow.ui_adminroles.form&p_id='. $proc_info['wf_p_id'],
166                        'img_roles'                                     => $img_default_dir.'roles.png',
167                        'link_graph'                            => $web_server_url.'/index.php?menuaction=workflow.ui_adminactivities.show_graph&p_id=' . $proc_info['wf_p_id'],
168                        'img_process'                           => $img_default_dir.'Process.gif',
169                        'link_save_process'                     => $web_server_url.'/index.php?menuaction=workflow.ui_adminprocesses.save_process&id='. $proc_info['wf_p_id'],
170                        'img_save'                                      => $img_default_dir.'save.png',
171                        'proc_id'                                       => $p['proc_id'],
172                        'other_processes'                       => $otherProcesses
173                );
174
175                return $toolbar_data;
176                }
177
178
179        /**
180        * Get process model files
181        * @param  array $p process process data
182        * @return array
183        * @access public
184        */
185        function get_model_files($p)
186        {
187                switch($p['type'])
188                {
189                        case 'include'  : $path = PHPGW_SERVER_ROOT . SEP . 'workflow' . SEP . 'js' . SEP . 'adminsource' . SEP . 'inc';
190                                                          break;
191                        case 'template' : $path = PHPGW_SERVER_ROOT . SEP . 'workflow' . SEP . 'js' . SEP . 'adminsource' . SEP . 'templates';
192                                                          break;
193                        case 'js'       : $path = PHPGW_SERVER_ROOT . SEP . 'workflow' . SEP . 'js' . SEP . 'local';
194                                                          break;
195                }
196
197                $col_file_name  = array();
198                $files          = array();
199
200                if ($handle = opendir($path))
201                {
202                        while (false !== ($file_name = readdir($handle)))
203                        {
204                                if (!is_dir($path.SEP.$file_name))
205                                {
206                                        $files[] = array('file_name'     => $file_name);
207                                        $col_file_name[]  = $file_name;
208                                }
209                        }
210                }
211
212                array_multisort($col_file_name,SORT_ASC,$files);
213
214                return $files;
215        }
216
217        /**
218        * Get process php files
219        * @param array $p process data
220        * @return array
221        * @access public
222        */
223        function get_php_files($p)
224        {
225                $process_manager    = &Factory::newInstance('ProcessManager');
226                $proc_info          = $process_manager->get_process($p['proc_id']);
227                $activity_manager   = &Factory::newInstance('ActivityManager');
228                $process_activities = $activity_manager->list_activities($p['proc_id'], 0, -1, 'wf_name__asc', '','',false);
229                $path = GALAXIA_PROCESSES . SEP . $proc_info['wf_normalized_name'] . SEP . 'code' . SEP .'activities' . SEP;
230
231                $files = array();
232
233                $col_file_name  = array();
234                $col_tamanho    = array();
235                $col_modificado = array();
236
237                foreach ($process_activities['data'] as $process_activity)
238                {
239
240                        $file_name   = $process_activity['wf_normalized_name'].'.php';
241                        $activity_id = $process_activity['wf_activity_id'];
242                        $tamanho     = filesize($path.$file_name);
243                        $modificado  = date('d/m/Y H:i:s', filemtime($path.$file_name) );
244
245                        $files[] = array('file_name'     => $file_name,
246                                                         'activity_id'   => $activity_id,
247                                                         'tamanho'           => $this->_assignUnitToFileSize($tamanho),
248                                                         'modificado'    => $modificado,
249                                                         'tipo_atividade'=> $process_activity['wf_type'],
250                                                         'interativa'    => $process_activity['wf_is_interactive'],
251                                                         'proc_name'     => $proc_info['wf_normalized_name'],
252                                                         'proc_id'               => $proc_info['wf_p_id'],
253                                                         'tipo_codigo'   => 'atividade'
254                        );
255
256                        $col_file_name[]  = $file_name;
257                        $col_tamanho[]    = $tamanho;
258                        $col_modificado[] = $modificado;
259                }
260
261                if (isset($p['sort']))
262                {
263                        $order_by = ($p['order_by'] == 1) ? SORT_ASC : SORT_DESC;
264
265                        switch ($p['sort'])
266                        {
267                                case 'file_name' :  array_multisort($col_file_name,$order_by,$files);
268                                                                        break;
269                                case 'tamanho'   :  array_multisort($col_tamanho,SORT_NUMERIC,$order_by,$files);
270                                                                        break;
271                                case 'modificado':  array_multisort($col_modificado,$order_by,$files);
272                                                                        break;
273
274                        }
275                }
276
277                return $files;
278        }
279
280        /**
281        * Delete process file
282        * @param array $p process data
283        * @return array
284        * @access public
285        */
286        function delete_file($p)
287        {
288                if ((strpos($p['file_name'],'/') !== false) || (strpos($p['file_name'],'/') !== false))
289                        return 'Não foi possível executar a operação solicitada';
290                $process_manager = &Factory::newInstance('ProcessManager');
291                $proc_info = $process_manager->get_process($p['proc_id']);
292                $file_name = $p['file_name'];
293                $proc_name = $proc_info['wf_normalized_name'];
294                $type      = $p['type'];
295                if (strpos($file_name,'/')) return 'Nome de arquivo inválido.';
296                if (!strlen($proc_name)) return 'ID de Processo inválido.';
297
298    switch($type)
299    {
300        case 'atividade':
301            $path =  'activities' . SEP . $file_name;
302            break;
303            case 'template':
304            $path =  'templates' . SEP . $file_name;
305            break;
306        case 'include':
307            $path = $file_name;
308            break;
309                case 'resource':
310                                $path = GALAXIA_PROCESSES . '/' . $proc_info['wf_normalized_name'] . '/resources/' . $file_name;
311            break;
312
313    }
314
315                if ($type == 'resource')
316                {
317                        $complete_path = $path;
318                }
319                else
320                {
321                        $complete_path = GALAXIA_PROCESSES . SEP . $proc_name . SEP . 'code' . SEP . $path;
322                }
323
324                if (file_exists($complete_path))
325                {
326                        if (unlink($complete_path))
327                        {
328                                return 'Arquivo '.$file_name.' excluido com sucesso.';
329                        }
330                        else
331                        {
332                                return 'Não foi possivel excluir o arquivo '.$file_name.'.';
333                        }
334    }
335                else
336                {
337                        return 'O arquivo '.$file_name.' não existe.';
338                }
339        }
340        /**
341        * Create process new file
342        * @param array $p process
343        * @return array
344        * @access public
345        */
346        function create_file($p)
347        {
348                $process_manager = &Factory::newInstance('ProcessManager');
349                $proc_info = $process_manager->get_process($p['proc_id']);
350                $file_name = $p['file_name'];
351                $proc_name = $proc_info['wf_normalized_name'];
352                $type      = $p['type'];
353
354                if ((strpos($file_name,'/') !== false) || (strpos($file_name,'/') !== false))
355                {
356                        return INVALID_FILE_NAME;
357                }
358
359                if (!strlen($proc_name))
360                {
361                        return INVALID_PROCESS_ID;
362                }
363
364                switch($type)
365                {
366                        case 'atividade':
367                                $path =  'activities' . SEP . $file_name;
368                                $complete_path = GALAXIA_PROCESSES . SEP . $proc_name . SEP . 'code' . SEP . $path;
369                                break;
370                        case 'template':
371                                $path =  'templates' . SEP . $file_name;
372                                $complete_path = GALAXIA_PROCESSES . SEP . $proc_name . SEP . 'code' . SEP . $path;
373                                break;
374                        case 'include':
375                                $path = $file_name;
376                                $complete_path = GALAXIA_PROCESSES . SEP . $proc_name . SEP . 'code' . SEP . $path;
377                                break;
378                        case 'resource':
379                                $complete_path = GALAXIA_PROCESSES . '/' . $proc_name . '/resources/' . $file_name;
380                                break;
381                }
382
383                if (file_exists($complete_path))
384                {
385                        if (!$p['rewrite'])
386                        {
387                                return FILE_ALREADY_EXISTS;
388                        } else {
389                                unlink($complete_path);
390                        }
391                }
392
393                if ($fp = fopen($complete_path, 'w'))
394                {
395                        $basepath = PHPGW_SERVER_ROOT.SEP.'workflow'.SEP.'js'.SEP.'adminsource';
396                        switch ($type)
397                        {
398                        case 'template':  $basepath = $basepath.SEP.'templates';
399                                break;
400                        case 'include' :  $basepath = $basepath.SEP.'inc';
401                                break;
402                        }
403
404                        if ($type == 'template' || $type == 'include')
405                        {
406                                if (file_exists($basepath.SEP.$p['modelo']))
407                                {
408                                        fwrite($fp,file_get_contents($basepath.SEP.$p['modelo']));
409                                }
410                        }
411
412                        fclose($fp);
413                        return FILE_CREATED;
414                }
415                else
416                {
417                        return FILE_NOT_CREATED;
418                }
419        }
420
421        /**
422        * Get process include files
423        * @param  array $p process
424        * @return array
425        * @access public
426        */
427        function get_include_files($p)
428        {
429                $process_manager    = &Factory::newInstance('ProcessManager');
430                $proc_info          = $process_manager->get_process($p['proc_id']);
431                $path = GALAXIA_PROCESSES . SEP . $proc_info['wf_normalized_name'] . SEP . 'code';
432
433
434                $col_file_name  = array();
435                $col_tamanho    = array();
436                $col_modificado = array();
437                $files          = array();
438
439                if ($handle = opendir($path))
440                {
441                        while (false !== ($file_name = readdir($handle)))
442                        {
443                                if (!is_dir($path.SEP.$file_name))
444                                {
445                                        $tamanho     = filesize($path.SEP.$file_name);
446                                        $modificado  = date('d/m/Y H:i:s', filemtime($path.SEP.$file_name) );
447
448                                        $files[] = array('file_name'     => $file_name,
449                                                         'tamanho'           => $this->_assignUnitToFileSize($tamanho),
450                                                         'modificado'    => $modificado,
451                                                         'proc_name'     => $proc_info['wf_normalized_name'],
452                                                         'proc_id'               => $proc_info['wf_p_id'],
453                                                         'tipo_codigo'   => 'include'
454                                        );
455
456                                        $col_file_name[]  = $file_name;
457                                        $col_tamanho[]    = $tamanho;
458                                        $col_modificado[] = $modificado;
459                                }
460                        }
461                }
462
463
464                if (isset($p['sort']))
465                {
466                        $order_by = ($p['order_by'] == 1) ? SORT_ASC : SORT_DESC;
467
468                        switch ($p['sort'])
469                        {
470                                case 'file_name' :  array_multisort($col_file_name,$order_by,$files);
471                                                                        break;
472                                case 'tamanho'   :  array_multisort($col_tamanho,SORT_NUMERIC,$order_by,$files);
473                                                                        break;
474                                case 'modificado':  array_multisort($col_modificado,$order_by,$files);
475                                                                        break;
476
477                        }
478                }
479
480                return $files;
481        }
482
483        /**
484        * Get process template files
485        * @param array $p process data
486        * @return array
487        * @access public
488        */
489        function get_template_files($p)
490        {
491                $process_manager    = &Factory::newInstance('ProcessManager');
492                $proc_info          = $process_manager->get_process($p['proc_id']);
493                $path = GALAXIA_PROCESSES . SEP . $proc_info['wf_normalized_name'] . SEP . 'code' . SEP .'templates';
494
495                $col_file_name  = array();
496                $col_tamanho    = array();
497                $col_modificado = array();
498
499                if ($handle = opendir($path))
500                {
501                        while (false !== ($file_name = readdir($handle)))
502                        {
503                                if (!is_dir($path.SEP.$file_name))
504                                {
505                                        $tamanho     = filesize($path.SEP.$file_name);
506                                        $modificado  = date('d/m/Y H:i:s', filemtime($path.SEP.$file_name) );
507
508                                        $files[] = array('file_name'     => $file_name,
509                                                         'tamanho'           => $this->_assignUnitToFileSize($tamanho),
510                                                         'modificado'    => $modificado,
511                                                         'proc_name'     => $proc_info['wf_normalized_name'],
512                                                         'proc_id'               => $proc_info['wf_p_id'],
513                                                         'tipo_codigo'   => 'template'
514                                        );
515
516                                        $col_file_name[]  = $file_name;
517                                        $col_tamanho[]    = $tamanho;
518                                        $col_modificado[] = $modificado;
519
520                                }
521                        }
522                }
523
524                if (isset($p['sort']))
525    {
526        $order_by = ($p['order_by'] == 1) ? SORT_ASC : SORT_DESC;
527
528        switch ($p['sort'])
529        {
530            case 'file_name' :  array_multisort($col_file_name,$order_by,$files);
531                                break;
532            case 'tamanho'   :  array_multisort($col_tamanho,SORT_NUMERIC,$order_by,$files);
533                                break;
534            case 'modificado':  array_multisort($col_modificado,$order_by,$files);
535                                break;
536
537        }
538    }
539
540                return $files;
541        }
542
543        /**
544        * Upload process resource
545        *
546        * @param array $p process
547        * @return array
548        * @access public
549        */
550        function upload_resource($p)
551        {
552                $process_manager = &Factory::newInstance('ProcessManager');
553                $proc_info = $process_manager->get_process($p['proc_id']);
554                $file_name = basename($_FILES['resource_file']['name']);
555
556                $base_path = GALAXIA_PROCESSES . '/' . $proc_info['wf_normalized_name'] . '/resources';
557                $path = $base_path . '/' . $file_name;
558
559
560                if (!strlen($file_name))
561                        return 'É necessário selecionar um arquivo.';
562
563                if (!is_dir($base_path))
564                        return 'A pasta de resources ainda não foi criada no servidor.';
565
566                if (file_exists($path))
567                        return 'O arquivo '.$file_name.' já existe no servidor.';
568
569                if (move_uploaded_file($_FILES['resource_file']['tmp_name'],$path))
570                        return 'Upload realizado com sucesso.';
571                else
572                        return 'Não foi possível realizar upload do arquivo '.$file_name.'.';
573        }
574
575        /**
576        * Export process file
577        *
578        * @access public
579        */
580        function export_file()
581        {
582                if (strpos($_REQUEST['file_name'],'/') !== false)
583                        return 'Não foi possível executar a operação solicitada';
584                $process_manager    = &Factory::newInstance('ProcessManager');
585                $proc_info          = $process_manager->get_process($_REQUEST['proc_id']);
586
587                $proc_name = $proc_info['wf_normalized_name'];
588                $file_name = $_REQUEST['file_name'];
589                $type      = $_REQUEST['type'];
590
591                switch($type)
592                {
593                        case 'atividade':
594                                $path =  'activities' . SEP . $file_name;
595                                break;
596                        case 'template':
597                                $path =  'templates' . SEP . $file_name;
598                                break;
599                        case 'resource':
600                                $path = 'resources' . SEP . $file_name;
601                                break;
602                        case 'include':
603                                $path = $file_name;
604                                break;
605                        default:
606                                exit;
607                }
608
609                if ($type == 'resource')
610                        $completePath = GALAXIA_PROCESSES . SEP . $proc_name . SEP . $path;
611                else
612                        $completePath = GALAXIA_PROCESSES . SEP . $proc_name . SEP . 'code' . SEP . $path;
613
614                Factory::getInstance('ResourcesRedirector')->show($completePath, 'application/force-download');
615                exit;
616        }
617
618        /**
619        * Get process resources files
620        *
621        * @param array $p process
622        * @return array
623        * @access public
624        */
625        function get_resource_files($p)
626        {
627                $process_manager    = &Factory::newInstance('ProcessManager');
628                $proc_info          = $process_manager->get_process($p['proc_id']);
629
630                $path       = GALAXIA_PROCESSES . '/' . $proc_info['wf_normalized_name'] . '/resources';
631
632                if (!is_dir($path))
633                        mkdir($path, 0770);
634
635                $col_file_name  = array();
636                $col_tamanho    = array();
637                $col_modificado = array();
638                $col_tipo               = array();
639
640                if ($handle = opendir($path))
641                {
642                        while (false !== ($file_name = readdir($handle)))
643                        {
644                                if (!is_dir($path.SEP.$file_name))
645                                {
646                                        $tipo        = mime_content_type($path.SEP.$file_name);
647                                        $tamanho     = filesize($path.SEP.$file_name);
648                                        $modificado  = date('d/m/Y H:i:s', filemtime($path.SEP.$file_name) );
649
650                                        $files[] = array(
651                                                         'file_name'     => $file_name,
652                                                         'proc_name'     => $proc_info['wf_normalized_name'],
653                                                         'proc_id'               => $proc_info['wf_p_id'],
654                                                         'tamanho'           => $this->_assignUnitToFileSize($tamanho),
655                                                         'tipo'              => $tipo,
656                                                         'modificado'    => $modificado,
657                                                         'tipo_arquivo'  => 'resource'
658                                        );
659
660                                        $col_file_name[]  = $file_name;
661                                        $col_tamanho[]    = $tamanho;
662                                        $col_modificado[] = $modificado;
663                                        $col_tipo[]       = $tipo;
664
665                                }
666                        }
667                }
668
669                if (isset($p['sort']))
670                {
671                        $order_by = ($p['order_by'] == 1) ? SORT_ASC : SORT_DESC;
672
673                        switch ($p['sort'])
674                        {
675                                case 'file_name' :  array_multisort($col_file_name,$order_by,$files);
676                                                                        break;
677                                case 'tamanho'   :  array_multisort($col_tamanho,SORT_NUMERIC,$order_by,$files);
678                                                                        break;
679                                case 'modificado':  array_multisort($col_modificado,$order_by,$files);
680                                                                        break;
681                                case 'tipo'      :  array_multisort($col_tipo,$order_by,$files);
682                                                                        break;
683
684                        }
685                }
686
687                return $files;
688        }
689
690}
691?>
Note: See TracBrowser for help on using the repository browser.