Pluf Framework

Sign in or create your account | Project List | Help

Pluf Framework Commit Details

Date:2009-05-13 10:09:21 (9 months 27 days ago)
Author:Loïc d'Anterroches
Commit:af00cbc50a919d68ceb6ef390a10e1517caf529e
Message:Fixed issue 49, orm generation from model does not fill automatically the selection list for a mant-to-many relation.

Files: src/Pluf/DB/Field.php (1 diff)
src/Pluf/DB/Field/Manytomany.php (1 diff)
src/Pluf/Form/Field.php (2 diffs)
src/Pluf/Form/Field/Integer.php (1 diff)
src/Pluf/Form/Model.php (1 diff)
src/Pluf/Model.php (1 diff)

Change Details

src/Pluf/DB/Field.php
9797            }
9898        }
9999        foreach (array_keys($def) as $key) {
100            if (!in_array($key, array('widget', 'label', 'required',
100            if (!in_array($key, array('widget', 'label', 'required', 'multiple',
101101                                      'initial', 'choices', 'widget_attrs'))) {
102102                unset($def[$key]);
103103            }
src/Pluf/DB/Field/Manytomany.php
2525{
2626    public $type = 'manytomany';
2727
28    function formField($def, $form_field='Pluf_Form_Field_Varchar')
28    function formField($def, $form_field='Pluf_Form_Field_Integer')
2929    {
3030        $method = 'get_'.$def['name'].'_list';
31        $items = $def['model_instance']->$method();
32        $choices = array();
33        foreach ($items as $item) {
34            $choices[(string) $item] = $item->id;
31        $def['multiple'] = true;
32        $def['initial'] = array();
33        foreach ($def['model_instance']->$method() as $item) {
34            $def['initial'][(string) $item] = $item->id;
35        }
36        $def['choices'] = array();
37        foreach (Pluf::factory($def['model'])->getList() as $item) {
38            $def['choices'][(string) $item] = $item->id;
3539        }
36        $def['choices'] = $choices;
3740        if (!isset($def['widget'])) {
3841            $def['widget'] = 'Pluf_Form_Widget_SelectMultipleInput';
3942        }
src/Pluf/Form/Field.php
5151     */
5252    public $hidden_widget = 'Pluf_Form_Widget_HiddenInput';
5353    public $value = ''; /**< Current value of the field. */
54    /**
55     * Returning multiple values (select multiple etc.)
56     */
57    public $multiple = false;
5458    protected $empty_values = array('', null, array());
5559
5660    /**
...... 
111115     */
112116    function clean($value)
113117    {
114        if ($this->required and in_array($value, $this->empty_values)) {
118        if (!$this->multiple and $this->required
119            and in_array($value, $this->empty_values)) {
120            throw new Pluf_Form_Invalid(__('This field is required.'));
121        }
122        if ($this->multiple and $this->required and empty($value)) {
115123            throw new Pluf_Form_Invalid(__('This field is required.'));
116124        }
117125        return $value;
118126    }
119127
120128    /**
129     * Set the default empty value for a field.
130     *
131     * @param mixed Value
132     * @return mixed Value
133     */
134    function setDefaultEmpty($value)
135    {
136        if (in_array($value, $this->empty_values) and !$this->multiple) {
137            $value = '';
138        }
139        if (in_array($value, $this->empty_values) and $this->multiple) {
140            $value = array();
141        }
142        return $value;
143    }
144
145    /**
146     * Multi-clean a value.
147     *
148     * If you are getting multiple values, you need to go through all
149     * of them and validate them against the requirements. This will
150     * do that for you. Basically, it is cloning the field, marking it
151     * as not multiple and validate each value. It will throw an
152     * exception in case of failure.
153     *
154     * If you are implementing your own field which could be filled by
155     * a "multiple" widget, you need to perform a check on
156     * $this->multiple.
157     *
158     * @see Pluf_Form_Field_Integer::clean
159     *
160     * @param array Values
161     * @return array Values
162     */
163    public function multiClean($value)
164    {
165        $field = clone($this);
166        $field->multiple = false;
167        reset($value);
168        while (list($i, $val) = each($value)) {
169            $value[$i] = $field->clean($val);
170        }
171        reset($value);
172        return $value;
173    }
174
175    /**
121176     * Returns the HTML attributes to add to the field.
122177     *
123178     * @param object Widget
src/Pluf/Form/Field/Integer.php
3030    public function clean($value)
3131    {
3232        parent::clean($value);
33        if (in_array($value, $this->empty_values)) {
34            $value = '';
35        }
36        if (is_array($value)) {
37            reset($value);
38            while (list($i, $val) = each($value)) {
39                if (!preg_match('/[0-9]+/', $val)) {
40                    throw new Pluf_Form_Invalid(__('The value must be an integer.'));
41                }
42                $this->checkMinMax($val);
43                $value[$i] = (int) $val;
44            }
45            reset($value);
46            return $value;
33        $value = $this->setDefaultEmpty($value);
34        if ($this->multiple) {
35            return $this->multiClean($value);
4736        } else {
48            if (!preg_match('/[0-9]+/', $value)) {
37            if (!preg_match('/^[\+\-]?[0-9]+$/', $value)) {
4938                throw new Pluf_Form_Invalid(__('The value must be an integer.'));
5039            }
5140            $this->checkMinMax($value);
src/Pluf/Form/Model.php
4747        }
4848        foreach ($cols as $name=>$def) {
4949            $db_field = new $def['type']('', $name);
50            $defaults = array('blank' => true, 'verbose' => $name, 'help_text' => '', 'editable' => true);
51            $def = array_merge($defaults, $def);
50            $def = array_merge(array('blank' => true,
51                                     'verbose' => $name,
52                                     'help_text' => '',
53                                     'editable' => true),
54                               $def);
5255            if ($def['editable']) {
5356                // The 'model_instance' and 'name' are used by the
5457                // ManyToMany field.
src/Pluf/Model.php
196196    /**
197197     * Get the raw data of the object.
198198     *
199     * For the many to many relations, the value is an array of ids.
200     *
199201     * @return array Associative array of the data.
200202     */
201203    function getData()
202204    {
205        foreach ($this->_a['cols'] as $col=>$val) {
206            $field = new $val['type']();
207            if ($field->type == 'manytomany') {
208                $this->_data[$col] = array();
209                $method = 'get_'.strtolower($col).'_list';
210                foreach ($this->$method() as $item) {
211                    $this->_data[$col][] = $item->id;
212                }
213            }
214        }
203215        return $this->_data;
204216    }
205217

Archive Download the corresponding diff file

Branches:
master