[795] | 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 | */ |
---|
| 10 | class 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 |
---|
| 170 | * @access public |
---|
| 171 | */ |
---|
| 172 | function &getInstance($obj) |
---|
| 173 | { |
---|
| 174 | return(wf_create_object(strtolower($obj))); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | /** |
---|
| 178 | * Maps request vars to layer attributes |
---|
| 179 | * @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. |
---|
| 180 | * @return bool true in case of success of false otherwise. |
---|
| 181 | * @access public |
---|
| 182 | */ |
---|
| 183 | function getRequest($fields = null, $from = null) |
---|
| 184 | { |
---|
| 185 | if (($fields = $this->filterUserSuppliedAttributes($fields)) === false) |
---|
| 186 | return false; |
---|
| 187 | |
---|
| 188 | /* define the origin of the variables */ |
---|
| 189 | $request = is_null($from) ? $this->request : $from; |
---|
| 190 | foreach ($request as $key => $value) |
---|
| 191 | if (array_key_exists($key, $fields)) |
---|
| 192 | @$this->{$key} = $value; |
---|
| 193 | |
---|
| 194 | return true; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | /** |
---|
| 198 | * Stores value to be shown on view layer |
---|
| 199 | * |
---|
| 200 | * @param string $var Template var name |
---|
| 201 | * @param string $value Template var value |
---|
| 202 | * @return void |
---|
| 203 | * @access public |
---|
| 204 | */ |
---|
| 205 | function addViewVar($var, $value) |
---|
| 206 | { |
---|
| 207 | @$this->viewData[$var] = $value; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | /** |
---|
| 211 | * Retrieves view layer vars |
---|
| 212 | * |
---|
| 213 | * @param string $var Template var name |
---|
| 214 | * @return mixed |
---|
| 215 | * @access public |
---|
| 216 | */ |
---|
| 217 | function getViewVar($var) |
---|
| 218 | { |
---|
| 219 | if ( array_key_exists($var , $this->viewData) ) { |
---|
| 220 | return ( $this->viewData[$var] ); |
---|
| 221 | } |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | /** |
---|
| 225 | * Catches module properties |
---|
| 226 | * |
---|
| 227 | * @param string $property Property name |
---|
| 228 | * @return mixed property value |
---|
| 229 | * @access public |
---|
| 230 | */ |
---|
| 231 | function getWfProperty($property) |
---|
| 232 | { |
---|
| 233 | return( $this->workflow[$property] ); |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | /** |
---|
| 237 | * Sets module properties |
---|
| 238 | * |
---|
| 239 | * @param string $property property name |
---|
| 240 | * @param string $value value |
---|
| 241 | * @return void |
---|
| 242 | * @access public |
---|
| 243 | */ |
---|
| 244 | function setWfProperty($property , $value) |
---|
| 245 | { |
---|
| 246 | if ( array_key_exists($property , $this->workflow) ) { |
---|
| 247 | $this->workflow[$property] = $value; |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | /** |
---|
| 252 | * Assign an identifier to the instance |
---|
| 253 | * |
---|
| 254 | * @param string $param Indentifier to instance |
---|
| 255 | * @return void |
---|
| 256 | * @access public |
---|
| 257 | */ |
---|
| 258 | function setNameInstance($param) |
---|
| 259 | { |
---|
| 260 | $this->instance->setName($param); |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | /** |
---|
| 264 | * Serializes an array |
---|
| 265 | * |
---|
| 266 | * @param array $input Array var to be serialized |
---|
| 267 | * @access public |
---|
| 268 | * @return object ArrayObject |
---|
| 269 | * @since PHP 5 |
---|
| 270 | * @throws Exception |
---|
| 271 | */ |
---|
| 272 | function toObject($input = array()) |
---|
| 273 | { |
---|
| 274 | if (is_array($input)) { |
---|
| 275 | return new ArrayObject($input); |
---|
| 276 | } |
---|
| 277 | return false; |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | /** |
---|
| 281 | * Unserializes an array |
---|
| 282 | * |
---|
| 283 | * @param object $input ArrayObject to be unserialized |
---|
| 284 | * @since PHP 5 |
---|
| 285 | * @return array |
---|
| 286 | * @access public |
---|
| 287 | */ |
---|
| 288 | function fromObject(ArrayObject $input) |
---|
| 289 | { |
---|
| 290 | return iterator_to_array($input->getIterator()); |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | /** |
---|
| 294 | * Give useful runtime information about classes and objects |
---|
| 295 | * |
---|
| 296 | * @param mixed $target Target class name or class instance |
---|
| 297 | * @return object Object with runtime information as properties |
---|
| 298 | * @access public |
---|
| 299 | * @since PHP 5 |
---|
| 300 | */ |
---|
| 301 | function inspect($target = null) |
---|
| 302 | { |
---|
| 303 | if (!is_null($target)) { |
---|
| 304 | switch(gettype($target)) { |
---|
| 305 | case 'string': |
---|
| 306 | return new ReflectionClass($target); |
---|
| 307 | break; |
---|
| 308 | case 'object': |
---|
| 309 | return new ReflectionObject($target); |
---|
| 310 | break; |
---|
| 311 | default: |
---|
| 312 | return false; |
---|
| 313 | break; |
---|
| 314 | } |
---|
| 315 | } |
---|
| 316 | return false; |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | /** |
---|
| 320 | * Builds SQL commands |
---|
| 321 | * |
---|
| 322 | * @param string $commandText SQL command |
---|
| 323 | * @return void |
---|
| 324 | * @access public |
---|
| 325 | */ |
---|
| 326 | function commandBuilder($commandText) |
---|
| 327 | { |
---|
| 328 | $this->commandText .= $commandText; |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | /** |
---|
| 332 | * Finalizes an activity life cycle |
---|
| 333 | * |
---|
| 334 | * @return void |
---|
| 335 | * @access public |
---|
| 336 | */ |
---|
| 337 | function commitInstance() |
---|
| 338 | { |
---|
| 339 | $this->instance->complete(); |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | /** |
---|
| 343 | * Updates module data with layer attributes |
---|
| 344 | * @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. |
---|
| 345 | * @return bool true in case of success of false otherwise. |
---|
| 346 | * @access public |
---|
| 347 | */ |
---|
| 348 | function updateInstance($fields = null) |
---|
| 349 | { |
---|
| 350 | $attributes = $this->getAttributes(); |
---|
| 351 | if (($fields = $this->filterUserSuppliedAttributes($fields)) === false) |
---|
| 352 | return false; |
---|
| 353 | |
---|
| 354 | $fields = array_keys($fields); |
---|
| 355 | foreach ($fields as $fieldName) |
---|
| 356 | $this->instance->set($fieldName, $attributes[$fieldName]); |
---|
| 357 | return true; |
---|
| 358 | } |
---|
| 359 | |
---|
| 360 | /** |
---|
| 361 | * Updates layer attributes with module data |
---|
| 362 | * @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. |
---|
| 363 | * @return bool true in case of success of false otherwise. |
---|
| 364 | * @access public |
---|
| 365 | */ |
---|
| 366 | function updateAttributes($fields = null) |
---|
| 367 | { |
---|
| 368 | if (($fields = $this->filterUserSuppliedAttributes($fields)) === false) |
---|
| 369 | return false; |
---|
| 370 | |
---|
| 371 | $fields = array_keys($fields); |
---|
| 372 | foreach ($fields as $fieldName) |
---|
| 373 | if ($this->instance->exists($fieldName)) |
---|
| 374 | $this->{$fieldName} = $this->instance->get($fieldName); /* now fills the current object with the values retrieved from the instance */ |
---|
| 375 | |
---|
| 376 | return true; |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | /** |
---|
| 380 | * Sends a file for download. This method halts the execution of php by using the 'exit' "function". |
---|
| 381 | * |
---|
| 382 | * @param string $filename The filename |
---|
| 383 | * @param string $data The file's data |
---|
| 384 | * @param string $mime The mime type of the file. If not used, a mime type that forces the download will be used. |
---|
| 385 | * @return bool false in case of error. In case of success, the script is interrupted to avoid incorrect behavior of php |
---|
| 386 | * @access public |
---|
| 387 | */ |
---|
| 388 | function sendFile($filename, $data, $mime = null) |
---|
| 389 | { |
---|
| 390 | /* check if everything is ok */ |
---|
| 391 | if (empty($filename)) |
---|
| 392 | return false; |
---|
| 393 | |
---|
| 394 | /* check if the user supplied a mime type */ |
---|
| 395 | if (is_null($mime)) |
---|
| 396 | $mime = 'application/force-download'; |
---|
| 397 | |
---|
| 398 | /* use the download mode */ |
---|
| 399 | $this->setWfProperty('downloadMode', true); |
---|
| 400 | |
---|
| 401 | /* send the file */ |
---|
| 402 | header('Pragma: public'); |
---|
| 403 | header('Cache-Control: cache, must-revalidate'); |
---|
| 404 | header('Content-Type: ' . $mime); |
---|
| 405 | header('Content-Disposition: attachment; filename="' . $filename . '"'); |
---|
| 406 | header('Pragma: no-cache'); |
---|
| 407 | header('Expires: 0'); |
---|
| 408 | header('Content-length: ' . strlen($data)); |
---|
| 409 | echo $data; |
---|
| 410 | |
---|
| 411 | /* ends PHP execution to avoid incorret behavior */ |
---|
| 412 | exit(); |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | /** |
---|
| 416 | * Provides constructor access to its subclasses for processes startup settings |
---|
| 417 | * |
---|
| 418 | * @param array $env MVC settings |
---|
| 419 | * @param boolean $autoAssign Automatically fills layers attributes with request data |
---|
| 420 | * @return object |
---|
| 421 | * @access public |
---|
| 422 | */ |
---|
| 423 | function super(&$env, $autoAssign = false) |
---|
| 424 | { |
---|
| 425 | BaseModel::BaseModel(&$env, $autoAssign); |
---|
| 426 | } |
---|
| 427 | |
---|
| 428 | /* end methods declaration */ |
---|
| 429 | } |
---|
| 430 | ?> |
---|