root/trunk/patForms/Creator/Definition.php

Revision 346, 3.2 kB (checked in by sfuchs, 3 years ago)

Bug fixes and some clean-up in patForms/Propel integration stuff.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * Creator for patForms instance creation from a patForms_Definition object
4  *
5  * This class creates a patForms instance and populates it with form elements,
6  * rules and other properties as defined by the provided patForms_Definition
7  * object.
8  *
9  * Please note: this class is currently php5 only since its solely used
10  * as a base class for patForms_Definitions_Propel in order to integrate
11  * patForms with Propel5 as a "rapid form solution".
12  *
13  * $Id$
14  *
15  * @author        Sven Fuchs <svenfuchs@artweb-design.de>
16  * @package        patForms
17  * @subpackage    Creator
18  * @license        LGPL
19  * @copyright    PHP Application Tools <http://www.php-tools.net>
20  * @see         patForms_Definition
21  */
22
23 /**
24  * Creator for patForms instance creation from a patForms_Definition object
25  *
26  * This class creates a patForms instance and populates it with form elements,
27  * rules and other properties as defined by the provided patForms_Definition
28  * object.
29  *
30  * Please note: this class is currently php5 only since its solely used
31  * as a base class for patForms_Definitions_Propel in order to integrate
32  * patForms with Propel5 as a "rapid form solution".
33  *
34  * @author        Sven Fuchs <svenfuchs@artweb-design.de>
35  * @package        patForms
36  * @subpackage    Creator
37  * @license        LGPL
38  * @copyright    PHP Application Tools <http://www.php-tools.net>
39  * @see         patForms_Definition
40  */
41
42 class patForms_Creator_Definition
43 {
44     /**
45      * Factory method to create a new patForms instance
46      *
47      * This method will check for the existence of a patForms_Definition xml
48      * file and populate itself from the file if it exists. If it does not
49      * exist the class will lookup necessary data in the Propel peer class
50      * and write it to the xml file.
51      *
52      * When a Propel object is provided as a parameter, the form values will
53      * be populated with the objects member values.
54      *
55      * @static
56      * @access public
57      * @param  object  $definition  A patForms_Definition instance
58      * @param  object  $object      A Propel object instance
59      * @return object The patForms instance
60      */
61     static function create($definition, $object = null)
62     {
63         $form = patForms::createForm(null, array(
64             'name' => $definition->getName()
65         ));
66
67         foreach ($definition->getElements() as $el) {
68
69             if (!empty($el['attributes']['datasource'])) {
70                 $datasource = $el['attributes']['datasource'];
71                 unset($el['attributes']['datasource']);
72             } else {
73                 $datasource = null;
74             }
75
76             $element = $form->createElement($el['name'], $el['type'], $el['attributes']);
77
78             if (!is_null($datasource)) {
79
80                 $type = $datasource['type'];
81                 $ds = patForms::createDatasource($type);
82                 $ds->init($datasource);
83                 $element->attributes['datasource'] = $ds;
84                 // $element->setDatasource($ds); // screws up with php 5.1
85             }
86             if (isset($el['rules'])) {
87                 foreach($el['rules'] as $rule) {
88                     $type = $rule['type'];
89                     $value = $rule['value'];
90                     $ruleObj = &patForms::createRule($type);
91                     $ruleObj->setValue($value);
92                     $element->addRule($ruleObj); // even more strange: this works perfectly
93                 }
94             }
95
96             $form->elements[sizeof($form->elements)] = $element;
97             // $form->addElement($element); // screws up with php 5.1
98
99         }
100         if (!is_null($object)) {
101             $form->setValues($object->toArray());
102         }
103         $form->setAutoValidate($definition->getAutoValidate());
104
105         return    $form;
106     }
107
108 }
109 ?>
Note: See TracBrowser for help on using the browser.