Webylon 3.1 API Docs
  • Package
  • Class
  • Tree
  • Deprecated
  • Download
Version: current
  • 3.2
  • 3.1

Packages

  • auth
  • Booking
  • cart
    • shipping
    • steppedcheckout
  • Catalog
  • cms
    • assets
    • batchaction
    • batchactions
    • bulkloading
    • comments
    • content
    • core
    • export
    • newsletter
    • publishers
    • reports
    • security
    • tasks
  • Dashboard
  • DataObjectManager
  • event
  • faq
  • forms
    • actions
    • core
    • fields-basic
    • fields-dataless
    • fields-datetime
    • fields-files
    • fields-formatted
    • fields-formattedinput
    • fields-relational
    • fields-structural
    • transformations
    • validators
  • googlesitemaps
  • guestbook
  • installer
  • newsletter
  • None
  • photo
    • gallery
  • PHP
  • polls
  • recaptcha
  • sapphire
    • api
    • bulkloading
    • control
    • core
    • cron
    • dev
    • email
    • fields-formattedinput
    • filesystem
    • formatters
    • forms
    • i18n
    • integration
    • misc
    • model
    • parsers
    • search
    • security
    • tasks
    • testing
    • tools
    • validation
    • view
    • widgets
  • seo
    • open
      • graph
  • sfDateTimePlugin
  • spamprotection
  • stealth
    • captha
  • subsites
  • userform
    • pagetypes
  • userforms
  • webylon
  • widgets

Classes

  • CheckboxField
  • CheckboxField_Disabled
  • CheckboxField_Readonly
  • CheckboxSetField
  • DropdownField
  • GroupedDropdownField
  • ListboxField
  • LookupField
  • NullableField
  • OptionsetField
  • ReadonlyField
  • RoomServiceDropdownField
  • SimpleHTMLEditorField
  • SimpleTinyMCEField
  • SimpleWysiwygField
  • StateDropdownField
  • StateProvinceDropdownField
  • TextareaField
  • TextField
  1 <?php
  2 /**
  3  * Displays a set of checkboxes as a logical group.
  4  *
  5  * ASSUMPTION -> IF you pass your source as an array, you pass values as an array too.
  6  *              Likewise objects are handled the same.
  7  * 
  8  * @todo Document the different source data that can be used
  9  * with this form field - e.g ComponentSet, DataObjectSet,
 10  * array. Is it also appropriate to accept so many different
 11  * types of data when just using an array would be appropriate?
 12  * 
 13  * @todo Make use of FormField->createTag() to generate the
 14  * HTML tag(s) for this field.
 15  * 
 16  * @package forms
 17  * @subpackage fields-basic
 18  */
 19 class CheckboxSetField extends OptionsetField {
 20     
 21     protected $disabled = false;
 22     
 23     /**
 24      * @var Array
 25      */
 26     protected $defaultItems = array();
 27     
 28     /**
 29      * @todo Explain different source data that can be used with this field,
 30      * e.g. SQLMap, DataObjectSet or an array.
 31      * 
 32      * @todo Should use CheckboxField FieldHolder rather than constructing own markup.
 33      */
 34     function Field() {
 35         Requirements::css(SAPPHIRE_DIR . '/css/CheckboxSetField.css');
 36 
 37         $source = $this->source;
 38         $values = $this->value;
 39 
 40         // Get values from the join, if available
 41         if(is_object($this->form)) {
 42             $record = $this->form->getRecord();
 43             if(!$values && $record && $record->hasMethod($this->name)) {
 44                 $funcName = $this->name;
 45                 $join = $record->$funcName();
 46                 if($join) {
 47                     foreach($join as $joinItem) {
 48                         $values[] = $joinItem->ID;
 49                     }
 50                 }
 51             }
 52         }
 53         
 54         // Source is not an array
 55         if(!is_array($source) && !is_a($source, 'SQLMap')) {
 56             if(is_array($values)) {
 57                 $items = $values;
 58             } else {
 59                 // Source and values are DataObject sets.
 60                 if($values && is_a($values, 'DataObjectSet')) {
 61                     foreach($values as $object) {
 62                         if(is_a($object, 'DataObject')) {
 63                             $items[] = $object->ID;
 64                         }
 65                    }
 66                 } elseif($values && is_string($values)) {
 67                     $items = explode(',', $values);
 68                     $items = str_replace('{comma}', ',', $items);
 69                 }
 70             }
 71         } else {
 72             // Sometimes we pass a singluar default value thats ! an array && !DataObjectSet
 73             if(is_a($values, 'DataObjectSet') || is_array($values)) {
 74                 $items = $values;
 75             } else {
 76                 $items = explode(',', $values);
 77                 $items = str_replace('{comma}', ',', $items);
 78             }
 79         }
 80             
 81         if(is_array($source)) {
 82             unset($source['']);
 83         }
 84         
 85         $odd = 0;
 86         $options = '';
 87         
 88         if ($source == null) {
 89             $source = array();
 90             $options = "<li>No options available</li>";
 91         }
 92 
 93         if($source) foreach($source as $index => $item) {
 94             if(is_a($item, 'DataObject')) {
 95                 $key = $item->ID;
 96                 $value = $item->Title;
 97             } else {
 98                 $key = $index;
 99                 $value = $item;
100             }
101             
102             $odd = ($odd + 1) % 2;
103             $key_lat = Convert::rus2lat($key);
104                         $extraClass = $odd ? 'odd' : 'even';
105             $extraClass .= ' val' . str_replace(' ', '', $key_lat);
106             $itemID = $this->id() . '_' . ereg_replace('[^a-zA-Z0-9]+', '', $key_lat);
107             $checked = '';
108             
109             if(isset($items)) {
110                 $checked = (in_array($key, $items) || in_array($key, $this->defaultItems)) ? ' checked="checked"' : '';
111             }
112 
113             $disabled = ($this->disabled || in_array($key, $this->disabledItems)) ? $disabled = ' disabled="disabled"' : '';
114             $options .= "<li class=\"$extraClass\"><input id=\"$itemID\" name=\"$this->name[$key_lat]\" type=\"checkbox\" value=\"$key\"$checked $disabled class=\"checkbox\" /> <label for=\"$itemID\">$value</label></li>\n"; 
115         }
116         
117         return "<ul id=\"{$this->id()}\" class=\"optionset checkboxsetfield{$this->extraClass()}\">\n$options</ul>\n"; 
118     }
119     
120     function setDisabled($val) {
121         $this->disabled = $val;
122     }
123     
124     /**
125      * Default selections, regardless of the {@link setValue()} settings.
126      * Note: Items marked as disabled through {@link setDisabledItems()} can still be
127      * selected by default through this method.
128      * 
129      * @param Array $items Collection of array keys, as defined in the $source array
130      */
131     function setDefaultItems($items) {
132         $this->defaultItems = $items;
133     }
134     
135     /**
136      * @return Array
137      */
138     function getDefaultItems() {
139         return $this->defaultItems;
140     }
141     
142     /**
143      * Load a value into this CheckboxSetField
144      */
145     function setValue($value, $obj = null) {
146         // If we're not passed a value directly, we can look for it in a relation method on the object passed as a second arg
147         if(!$value && $obj && $obj instanceof DataObject && $obj->hasMethod($this->name)) {
148             $funcName = $this->name;
149             $selected = $obj->$funcName();
150             if ($selected && is_object($selected) ) {
151                 $value = $selected->toDropdownMap('ID', 'ID');
152             }
153         }
154 
155         parent::setValue($value, $obj);
156     }
157     
158     /**
159      * Save the current value of this CheckboxSetField into a DataObject.
160      * If the field it is saving to is a has_many or many_many relationship,
161      * it is saved by setByIDList(), otherwise it creates a comma separated
162      * list for a standard DB text/varchar field.
163      *
164      * @param DataObject $record The record to save into
165      */
166     function saveInto(DataObject $record) {
167         $fieldname = $this->name ;
168         if($fieldname && $record && ($record->has_many($fieldname) || $record->many_many($fieldname))) {
169             $idList = array();
170             if($this->value) foreach($this->value as $id => $bool) {
171                if($bool) {
172                     $idList[] = $id;
173                 }
174             }
175             $record->$fieldname()->setByIDList($idList);
176         } elseif($fieldname && $record) {
177             if($this->value) {
178                 $this->value = str_replace(',', '{comma}', $this->value);
179                 $record->$fieldname = implode(",", $this->value);
180             } else {
181                 $record->$fieldname = '';
182             }
183         }
184     }
185     
186     /**
187      * Return the CheckboxSetField value as an array 
188      * selected item keys.
189      * 
190      * @return string
191      */
192     function dataValue() {
193         if($this->value && is_array($this->value)) {
194             $filtered = array();
195             foreach($this->value as $item) {
196                 if($item) {
197                     $filtered[] = str_replace(",", "{comma}", $item);
198                 }
199             }
200             
201             return implode(',', $filtered);
202         }
203         
204         return '';
205     }
206     
207     function performDisabledTransformation() {
208         $clone = clone $this;
209         $clone->setDisabled(true);
210         
211         return $clone;
212     }
213     
214     /**
215      * Transforms the source data for this CheckboxSetField
216      * into a comma separated list of values.
217      * 
218      * @return ReadonlyField
219      */
220     function performReadonlyTransformation() {
221         $values = '';
222         $data = array();
223         
224         $items = $this->value;
225         if($this->source) {
226             foreach($this->source as $source) {
227                 if(is_object($source)) {
228                     $sourceTitles[$source->ID] = $source->Title;
229                 }
230             }
231         }
232         
233         if($items) {
234             // Items is a DO Set
235             if(is_a($items, 'DataObjectSet')) {
236                 foreach($items as $item) {
237                     $data[] = $item->Title;
238                 }
239                 if($data) $values = implode(', ', $data);
240                 
241             // Items is an array or single piece of string (including comma seperated string)
242             } else {
243                 if(!is_array($items)) {
244                     $items = preg_split('/ *, */', trim($items));
245                 }
246                 
247                 foreach($items as $item) {
248                     if(is_array($item)) {
249                         $data[] = $item['Title'];
250                     } elseif(is_array($this->source) && !empty($this->source[$item])) {
251                         $data[] = $this->source[$item];
252                     } elseif(is_a($this->source, 'DataObjectSet')) {
253                         $data[] = $sourceTitles[$item];
254                     } else {
255                         $data[] = $item;
256                     }
257                 }
258                 
259                 $values = implode(', ', $data);
260             }
261         }
262         
263         $title = ($this->title) ? $this->title : '';
264         
265         $field = new ReadonlyField($this->name, $title, $values);
266         $field->setForm($this->form);
267         
268         return $field;
269     }
270     
271     function ExtraOptions() {
272         return FormField::ExtraOptions();
273     }
274     
275 }
276 ?>
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -
Webylon 3.1 API Docs API documentation generated by ApiGen 2.8.0