root/trunk/patForms/Datasource/Propel.php

Revision 346, 4.7 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  * Datasource for patForms integration with Propel
4  *
5  * Please note:
6  *
7  *        To use build-in patForms/Propel integration you have to build your
8  *        Propel classes with the following Propel build options enabled:
9  *
10  *         propel.addGenericAccessors = true
11  *         propel.addGenericMutators = true
12  *
13  *
14  * This class is used to autmatically populate form elements like select boxes
15  * with data from tables that are related to the Propel object's table via a
16  * foreign key.
17  *
18  * Please note: this class is currently php5 only since its solely used
19  * as a base class for patForms_Definitions_Propel in order to integrate
20  * patForms with Propel5 as a "rapid form solution".
21  *
22  * $Id$
23  *
24  * @author        Sven Fuchs <svenfuchs@artweb-design.de>
25  * @package        patForms
26  * @subpackage    Datasource
27  * @license        LGPL
28  * @copyright    PHP Application Tools <http://www.php-tools.net>
29  * @see         patForms_Definition
30  */
31
32 /**
33  * Datasource for patForms integration with Propel
34  *
35  * This class is used to autmatically populate form elements like select boxes
36  * with data from tables that are related to the Propel object's table via a
37  * foreign key.
38  *
39  * Please note: this class is currently php5 only since its solely used
40  * as a base class for patForms_Definitions_Propel in order to integrate
41  * patForms with Propel5 as a "rapid form solution".
42  *
43  * @author        Sven Fuchs <svenfuchs@artweb-design.de>
44  * @package        patForms
45  * @subpackage    Datasource
46  * @license        LGPL
47  * @copyright    PHP Application Tools <http://www.php-tools.net>
48  * @see         patForms_Definition
49  */
50 class patForms_Datasource_Propel
51 {
52    /**
53     * The Propel peers classname
54     *
55     * @var      string
56     * @access    private
57     */
58     private $peername;
59
60    /**
61     * The Propel objects classname
62     *
63     * @var      string
64     * @access    private
65     */
66     private $classname;
67
68    /**
69     * Config for the form elements label creation from Propel data
70     *
71     * @var      array
72     * @access    private
73     * @see        patForms_Datasource_Propel::getValues()
74     */
75     private $label;
76
77    /**
78     * Config for the form elements value creation from Propel data
79     *
80     * @var      array
81     * @access    private
82     * @see        patForms_Datasource_Propel::getValues()
83     */
84     private $value;
85
86     public function init($conf)
87     {
88         $this->peername = $conf['peername'];
89         $this->label = $conf['label'];
90         $this->value = $conf['value'];
91
92         $classname = call_user_func(array($this->peername, 'getOMClass'));
93         $this->classname = array_pop($tmp = explode('.', $classname));
94     }
95
96     /**
97      * Returns values for population of the patForms form element (e.g. a select box)
98      *
99      * This method will lookup, format and return data from the Propels object
100      * peer as defined by the members $label and $conf.
101      *
102      * E.g. an array like this:
103      *
104      *     array(
105      *         'initial' => 'Please select one ...'
106      *         'members' => array('Id'),
107      *         'mask' => '%s'
108      *     );
109      *
110      * would build values or labels populated from the Id field of the Propels
111      * object peer and simply display these values. Addionally it would add the
112      * string 'Please select one ...' as the first label to the select box.
113      *
114      * An array like this:
115      *
116      *     array(
117      *         'initial' => 'Please select one ...'
118      *         'members' => array('LastName', 'FirstName'),
119      *         'mask' => '%s (%s)'
120      *     );
121      *
122      * would select the fields Lastname and Firstname and display them with the
123      * format of "LastName (FirstName)".
124      *
125      * @access public
126      * @return array The values for the patForms element
127      */
128     public function getValues()
129     {
130         /* foreach (array($this->label, $this->value) as $arr) {
131             foreach ($arr['members'] as $member) {
132                 $colname = BasePeer::translateFieldName($this->classname, $member,
133                     BasePeer::TYPE_PHPNAME, BasePeer::TYPE_COLNAME);
134                 if (is_array($member)) {
135                     foreach ($member as $member) {
136                         $c->addSelectColumn($colname);
137                     }
138                 } else {
139                     $c->addSelectColumn($colname);
140                 }
141             }
142         } */
143
144         if (isset($this->label['initial']) OR isset($this->value['initial'])) {
145             $label = isset($this->label['initial']) ? $this->label['initial'] : '';
146             $value = isset($this->value['initial']) ? $this->value['initial'] : '';
147             $result[] = array(
148                 'value' => $value,
149                 'label' => $label
150             );
151         }
152
153         $c = new Criteria();
154         $results = call_user_func(array($this->peername, 'doSelect'), $c);
155
156         foreach($results as $object) {
157             foreach (array('label', 'value') as $key) {
158                 $arr = $this->$key;
159                 $params = array($arr['mask']);
160                 foreach ($arr['members'] as $member) {
161                     if (is_array($member)) {
162                         foreach ($member as $member) {
163                             $params[] = $object->getByName($member);
164                         }
165                     } else {
166                         $params[] = $object->getByName($member);
167                     }
168                 }
169                 $$key = call_user_func_array('sprintf', $params);
170                 $tmp[$key] = $$key;
171             }
172             $result[] = $tmp;
173         }
174
175         return $result;
176     }
177 }
178 ?>
Note: See TracBrowser for help on using the browser.