source: branches/1.2/workflow/inc/engine/src/API/Process.php @ 1349

Revision 1349, 6.8 KB checked in by niltonneto, 15 years ago (diff)

Ticket #561 - Inclusão do módulo Workflow faltante nessa versão.

  • Property svn:executable set to *
Line 
1<?php
2require_once (GALAXIA_LIBRARY.SEP.'src'.SEP.'common'.SEP.'Base.php');
3/**
4 * Represents the process that is being executed when an activity
5 * is executed. You can access this class methods using $process from any activity.
6 * No need to instantiate a new object
7 *
8 * @package Galaxia
9 * @license http://www.gnu.org/copyleft/gpl.html GPL
10 */
11class Process extends Base {
12        /**
13        * @var string $name
14        * @access protected
15        */
16        var $name;
17        /**
18        * @var string $description
19        * @access protected
20        */
21        var $description;
22        /**
23        * @var int $version
24        * @access protected
25        */
26        var $version;
27        /**
28        * @var string $normalizedName
29        * @access protected
30        */
31        var $normalizedName;
32        /**
33        * @var string $pId Process id
34        * @access protected
35        */
36        var $pId = 0;
37        /**
38        * @var array $config
39        * @access protected
40        */
41        var $config = array();
42
43        /**
44         * Constructor
45         *
46         * @param object $db
47         * @return object
48         * @access public
49         */
50        function Process(&$db)
51        {
52                $this->child_name = 'Process';
53                parent::Base($db);
54        }
55
56        /**
57         * Loads a process from the database
58         *
59         * @param int $pId
60         * @return bool
61         * @access public
62         */
63        function getProcess($pId)
64        {
65                $query = "select * from `".GALAXIA_TABLE_PREFIX."processes` where `wf_p_id`=?";
66                $result = $this->query($query,array($pId));
67                if(!$result->numRows()) return false;
68                $res = $result->fetchRow();
69                $this->name = $res['wf_name'];
70                $this->description = $res['wf_description'];
71                $this->normalizedName = $res['wf_normalized_name'];
72                $this->version = $res['wf_version'];
73                $this->pId = $res['wf_p_id'];
74                //config is load only on the first getConfigValues call
75        }
76 
77  /**
78   * Gets the process Id
79   *
80   * @return int
81   * @access public
82   */
83        function getProcessId()
84        {
85                return $this->pId;
86        }
87 
88  /**
89   * Gets the normalized name of the process
90   *
91   * @return string
92   * @access public
93   */
94        function getNormalizedName()
95        {
96                return $this->normalizedName;
97        }
98 
99        /**
100         * Gets the process name
101         *
102         * @return string
103         * @access public
104         */
105        function getName()
106        {
107                return $this->name;
108        }
109 
110        /**
111         * Gets the process version
112         *
113         * @return int
114         * @access public
115         */
116        function getVersion()
117        {
118                return $this->version;
119        }
120
121  /**
122   * Gets information about an activity in this process by name
123   *
124   * @param string $actname
125   * @return array
126   * @access public
127   */
128        function getActivityByName($actname)
129        {
130                // Get the activity data
131                $query = "select * from `".GALAXIA_TABLE_PREFIX."activities` where `wf_p_id`=? and `wf_name`=?";
132                $pId = $this->pId;
133                $result = $this->query($query,array($pId,$actname));
134                if(!$result->numRows()) return false;
135                $res = $result->fetchRow();
136                return $res;
137        }
138
139        /**
140         * Store config values for this process
141         *
142         * @param array $parameters pairs of (config_variables_names => (type => value))
143     * type can be int or text, anything else is considered text
144     * int value of -1 is considered as 'default global configuration option'. So nothing
145     * will be stored for this process (and existing values are erased)
146         * @return bool
147         * @access public
148         */
149        function setConfigValues(&$parameters)
150        {
151                if (!is_array($parameters))
152                {
153                  return false;
154                }
155                $array_delete=array();
156                $array_set=array();
157                $pId = (string)$this->pId;
158                foreach ($parameters as $config_var => $config_value)
159                {
160                  //all config values will be deleted
161                  $array_delete[] = array($pId, $config_var);
162                 
163                  //foreach but normally there's only one loop
164                  foreach($config_value as $value_type => $value_zone)
165                  {
166                    $ok = true;
167                    if ($value_type=='int')
168                    {
169                      //special value, refer to global config values for this process conf variable
170                      //we don't want any value stored for this process conf variable
171                      //so we break the foreach before setting the $array_set
172                      if ($value_zone == -1)
173                      {
174                        $ok=false;
175                        break;
176                      }
177                      //else it's classic
178                      $value_int = $value_zone;
179                      $value= '';
180                    }
181                    else
182                    {
183                      $value = $value_zone;
184                      $value_int = null;
185                    }
186                  }
187                  //we are going to set this config value if $ok says so
188                  if ($ok) $array_set[] = array($config_var, $value, $value_int, $pId);
189                }
190                //delete previous config values if they are in a bulk statement
191                if (count($array_delete)>0)
192                {
193                  $result= $this->query("DELETE from ".GALAXIA_TABLE_PREFIX."process_config where wf_p_id=? and wf_config_name=?",$array_delete, -1,-1,true,'',true);
194                }
195                //insert in a bulk statement
196                if (count($array_set)>0)
197                {
198                    $result= $this->query("INSERT into ".GALAXIA_TABLE_PREFIX."process_config
199                      (wf_config_name,wf_config_value,wf_config_value_int,wf_p_id) values (?,?,?,?)"
200                      ,$array_set, -1,-1,true,'', true);
201                }
202        }
203 
204        /**
205         * Gets all the process configuration values. The configuration data is then cached for this process object life
206         *
207         * @param array $parameters pairs of (config_variables_names => default_values)
208     * For a variable name which has no previous value and no global value (default process value)
209     * it will return default_value and this default value will be the NEW STORED value
210     * If no default value is given we assume it's a false
211         * @return array
212         * @access public
213         */
214        function getConfigValues(&$parameters)
215        {
216                if (!is_array($parameters))
217                {
218                  return false;
219                }
220                if (count($this->config) == 0)
221                { // first time we come
222                  // Get all the config data for this process
223                  $query = "select * from ".GALAXIA_TABLE_PREFIX."process_config where wf_p_id=?";
224                  $pId = $this->pId;
225                  $result = $this->query($query,array($pId));
226               
227                  if($result->numRows()>0)
228                  {
229                    //we add process datas for some config_name, we store it in $this->config
230                    while ($res=$result->fetchRow())
231                    {
232                      //int values are not stored in the same field
233                      $int_value= $res['wf_config_value_int'];
234                      if (isset($int_value))
235                      {
236                        $this->config[$res['wf_config_name']] = $int_value;
237                      }
238                      else
239                      {
240                        $this->config[$res['wf_config_name']] = $res['wf_config_value'];
241                      }
242                    }
243                  }
244                }// the second time we jump here
245               
246                //parse config_name asked
247                $local_array = array();
248                $global_default_array = array();
249                foreach ($parameters as $config_var => $default_value)
250                {
251                  if (isset($this->config[$config_var]))
252                  {// we already know this config value
253                    //echo "<br>ok we had one for ".$config_var;
254                    $local_array[$config_var] = $this->config[$config_var];
255                  }
256                  else
257                  {
258                    // we have no value for it here, we'll ask it in the global conf
259                    //echo "<br>we had nothing for ".$config_var;
260                    $global_default_array[$config_var] = $default_value;
261                  }
262                }
263               
264                // if we have some not set value that we need to check in global conf
265                if (count($global_default_array) > 0)
266                {
267                  $global_array =& galaxia_get_config_values($global_default_array);
268                }
269                $result = (array)$local_array + (array)$global_array;
270                return $result;
271        }
272}
273
274?>
Note: See TracBrowser for help on using the repository browser.