source: sandbox/workflow/trunk/inc/class.basemodel.inc.php @ 3060

Revision 3060, 10.9 KB checked in by viani, 14 years ago (diff)

Ticket #950 - Merged 2838:3056 /trunk/workflow em /sandbox/workflow/trunk

  • Property svn:executable set to *
Line 
1<?php
2/**
3 * Implements workflow module processes providing support for MVC architecture
4 * @author Carlos Eduardo Nogueira Gonçalves
5 * @author Sidnei Augusto Drovetto Jr. - drovetto@gmail.com
6 * @version 1.3
7 * @package Workflow
8 * @license http://www.gnu.org/copyleft/gpl.html GPL
9 */
10class BaseModel
11{
12        /* begin constants declaration */
13        /**
14         * @var string $CANCELAR Stopping activities shortcut
15         * @access public
16         */
17        var $CANCELAR = '__leave_activity';
18
19        /* end constants declaration */
20
21        /* begin attributes declaration */
22        /**
23         * @var array $workflow Workflow module information
24     * @access public
25     */
26        var $workflow;
27
28        /**
29     * @var object $activity Activity class instance
30     * @access public
31     */
32    var $activity;
33
34        /**
35     * @var object $factory Factory class reference
36     * @access public
37     */
38    var $factory;
39
40        /**
41     * @var object $instance Instance class reference
42     * @access public
43     */
44    var $instance;
45
46        /**
47         * @var object $DAO DAO (Data Access Object) instance
48     * @access public
49     */
50    var $DAO;
51
52        /**
53         * @var object $natural Natural instance
54     * @access public
55     */
56    var $natural;
57
58        /**
59     * @var array $viewData Data sent to view layer by Controller.
60     * Template vars must have the same names of this array's keys
61     * @access public
62     */
63    var $viewData = array();
64
65        /**
66         * @var array $request Data received from the user
67     * @access public
68     */
69    var $request;
70
71        /**
72     * @var string $commandText Holds SQL commands
73     * @access public
74     */
75    var $commandText;
76
77        /**
78     * @var object Holds query resultsets
79     * @access public
80     */
81    var $resultSet;
82
83        /**
84     * @var array $resultRow Holds resultset records
85     * @access public
86     */
87    var $resultRow;
88        /* end attributes declaration */
89       
90        /**
91     * Constructor
92     *
93     * @param array $env MVC settings
94     * @param boolean $autoAssign Automatically fills layers attributes with request data
95     * @return object
96     * @access public
97     */
98        function BaseModel(&$env, $autoAssign = false)
99    {
100        $this->DAO      =& $env['dao']     ;
101        $this->workflow =& $env['workflow'];
102        $this->instance =& $env['instance'];
103        $this->activity =& $env['activity'];
104        $this->request  =& $env['request'] ;
105        $this->factory  =& $env['factory'] ;
106        $this->natural  =& $env['natural'] ;
107
108        if ( $autoAssign ) {
109                $this->getRequest();
110        }
111         }
112        /* begin methods declaration */
113
114   /**
115    * Returns layer's attributes along with their values
116    *
117    * @return array result vector
118    * @access public
119    */
120        function getAttributes()
121    {
122                /* gets associative vector with name and declaration values of attributes */
123                $attributes = get_class_vars(get_class($this));
124                /* result vector */
125                $result = array();
126                /* iterates over attributes list */
127                foreach ($attributes as $attribute => $value) {
128                        /* parses only process-level attributes, whose names start with a _ signal */
129                        if (ereg('^_{1}', $attribute)) {
130                                $result[$attribute] = $this->{$attribute};
131                        }
132                }
133                /* return result vector */
134                return $result;
135        }
136
137   /**
138    * Returns the user supplied attributes that are also an attribute of the model class
139        * @param mixed $fields Use an array of attributes names to use only those in the array. Use null (default value) to select all the attributes. Also, it's possible to use a string to specify only one attribute.
140    * @return mixed The filtered array or false in case of errors.
141    * @access public
142    */
143        function filterUserSuppliedAttributes($fields)
144        {
145                $attributes = $this->getAttributes();
146                if (is_null($fields))
147                {
148                        $fields = $attributes;
149                }
150                else
151                {
152                        if (is_string($fields))
153                                $fields = array($fields);
154
155                        if (!is_array($fields))
156                                return false;
157
158                        $fields = array_flip(array_intersect(array_keys($attributes), $fields));
159                }
160
161                return $fields;
162        }
163
164   /**
165    * Creates module API class instance
166    *
167    * @param string $obj Class name
168    * @return mixed
169    * @deprecated 2.2.00.000
170    * @access public
171    */
172        function &getInstance($obj)
173        {
174                wf_warn_deprecated_method('Factory', 'getInstance');
175                return(wf_create_object(strtolower($obj)));
176        }
177
178   /**
179    * Maps request vars to layer attributes
180        * @param mixed $fields Use an array of attributes names to use only those in the array. Use null (default value) to select all the attributes. Also, it's possible to use a string to specify only one attribute.
181        * @return bool true in case of success of false otherwise.
182    * @access public
183    */
184        function getRequest($fields = null, $from = null)
185        {
186                if (($fields = $this->filterUserSuppliedAttributes($fields)) === false)
187                        return false;
188
189                /* define the origin of the variables */
190                $request = is_null($from) ? $this->request : $from;
191                foreach ($request as $key => $value)
192                        if (array_key_exists($key, $fields))
193                                @$this->{$key} = $value;
194
195                return true;
196        }
197
198   /**
199    * Stores value to be shown on view layer
200    *
201    * @param string $var Template var name
202    * @param string $value Template var value
203    * @return void
204    * @access public
205    */
206        function addViewVar($var, $value)
207    {
208                @$this->viewData[$var] = $value;
209        }
210
211   /**
212    * Retrieves view layer vars
213    *
214    * @param string $var Template var name
215    * @return mixed
216    * @access public
217    */
218        function getViewVar($var)
219    {
220                if ( array_key_exists($var , $this->viewData) ) {
221                        return ( $this->viewData[$var] );
222                }
223        }
224
225   /**
226    * Catches module properties
227    *
228    * @param string $property Property name
229    * @return mixed property value
230    * @access public
231    */
232        function getWfProperty($property)
233    {
234                return( $this->workflow[$property] );
235        }
236
237   /**
238    * Sets module properties
239    *
240    * @param string $property property name
241    * @param string $value value
242    * @return void
243    * @access public
244    */
245        function setWfProperty($property , $value)
246    {
247                if ( array_key_exists($property , $this->workflow) ) {
248                        $this->workflow[$property] = $value;
249                }
250        }
251
252        /**
253         * Assign an identifier to the instance
254         *
255         * @param string $param Indentifier to instance
256         * @return void
257         * @access public
258         */
259        function setNameInstance($param)
260        {
261                $this->instance->setName($param);
262        }
263
264    /**
265     * Serializes an array
266     *
267     * @param array $input Array var to be serialized
268     * @access public
269     * @return object ArrayObject
270     * @since PHP 5
271     * @throws Exception
272     */
273    function toObject($input = array())
274    {
275        if (is_array($input)) {
276            return new ArrayObject($input);
277        }
278        return false;
279    }
280
281    /**
282     * Unserializes an array
283     *
284     * @param object $input ArrayObject to be unserialized
285     * @since PHP 5
286     * @return array
287     * @access public
288     */
289    function fromObject(ArrayObject $input)
290    {
291        return iterator_to_array($input->getIterator());
292    }
293
294    /**
295     * Give useful runtime information about classes and objects
296     *
297     * @param mixed $target Target class name or class instance
298     * @return object Object with runtime information as properties
299     * @access public
300     * @since PHP 5
301     */
302    function inspect($target = null)
303    {
304        if (!is_null($target)) {
305            switch(gettype($target)) {
306                case 'string':
307                    return new ReflectionClass($target);
308                    break;
309                case 'object':
310                    return new ReflectionObject($target);
311                    break;
312                default:
313                    return false;
314                    break;
315            }
316        }
317        return false;
318    }
319
320   /**
321     * Builds SQL commands
322     *
323     * @param string $commandText SQL command
324     * @return void
325     * @access public
326     */
327        function commandBuilder($commandText)
328    {
329                $this->commandText .= $commandText;
330        }
331
332   /**
333     * Finalizes an activity life cycle
334     *
335     * @return void
336     * @access public
337     */
338        function commitInstance()
339    {
340                $this->instance->complete();
341        }
342
343   /**
344     * Updates module data with layer attributes
345         * @param mixed $fields Use an array of attributes names to use only those in the array. Use null (default value) to select all the attributes. Also, it's possible to use a string to specify only one attribute.
346     * @return bool true in case of success of false otherwise.
347     * @access public
348     */
349    function updateInstance($fields = null)
350    {
351        $attributes = $this->getAttributes();
352                if (($fields = $this->filterUserSuppliedAttributes($fields)) === false)
353                        return false;
354
355                $fields = array_keys($fields);
356        foreach ($fields as $fieldName)
357                        $this->instance->set($fieldName, $attributes[$fieldName]);
358                return true;
359    }
360
361   /**
362     * Updates layer attributes with module data
363         * @param mixed $fields Use an array of attributes names to use only those in the array. Use null (default value) to select all the attributes. Also, it's possible to use a string to specify only one attribute.
364     * @return bool true in case of success of false otherwise.
365     * @access public
366     */
367    function updateAttributes($fields = null)
368    {
369                if (($fields = $this->filterUserSuppliedAttributes($fields)) === false)
370                        return false;
371
372                $fields = array_keys($fields);
373                foreach ($fields as $fieldName)
374                        if ($this->instance->exists($fieldName))
375                                $this->{$fieldName} = $this->instance->get($fieldName); /* now fills the current object with the values retrieved from the instance */
376
377                return true;
378    }
379
380   /**
381     * Sends a file for download. This method halts the execution of php by using the 'exit' "function".
382     *
383         * @param string $filename The filename
384         * @param string $data The file's data
385         * @param string $mime The mime type of the file. If not used, a mime type that forces the download will be used.
386     * @return bool false in case of error. In case of success, the script is interrupted to avoid incorrect behavior of php
387     * @access public
388     */
389        function sendFile($filename, $data, $mime = null)
390        {
391                /* check if everything is ok */
392                if (empty($filename))
393                        return false;
394
395                /* check if the user supplied a mime type */
396                if (is_null($mime))
397                        $mime = 'application/force-download';
398
399                /* use the download mode */
400                $this->setWfProperty('downloadMode', true);
401
402                /* send the file */
403                header('Pragma: public');
404                header('Cache-Control: cache, must-revalidate');
405                header('Content-Type: ' . $mime);
406                header('Content-Disposition: attachment; filename="' . $filename . '"');
407                header('Pragma: no-cache');
408                header('Expires: 0');
409                header('Content-length: ' . strlen($data));
410                echo $data;
411
412                /* ends PHP execution to avoid incorret behavior */
413                exit();
414        }
415
416   /**
417     * Provides constructor access to its subclasses for processes startup settings
418     *
419     * @param array   $env MVC settings
420     * @param boolean $autoAssign Automatically fills layers attributes with request data
421     * @return object
422     * @access public
423     */
424        function super(&$env, $autoAssign = false)
425    {
426                BaseModel::BaseModel(&$env, $autoAssign);
427        }
428
429        /* end methods declaration */
430}
431?>
Note: See TracBrowser for help on using the repository browser.