| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The smallest unit of an item, the field it self. |
| 5 |
*/ |
| 6 |
abstract class VP_Control_FieldMulti extends VP_Control_Field |
| 7 |
{ |
| 8 |
|
| 9 |
protected $_items = array(); |
| 10 |
|
| 11 |
protected $_items_binding; |
| 12 |
|
| 13 |
protected $_raw_default; |
| 14 |
|
| 15 |
/** |
| 16 |
* Basic self setup of the object |
| 17 |
* @param SimpleXMLElement $simpleXML SimpleXML object representation of the field |
| 18 |
* @return VP_Control_FieldMulti Field object |
| 19 |
*/ |
| 20 |
protected function _basic_make($arr) |
| 21 |
{ |
| 22 |
parent::_basic_make($arr); |
| 23 |
|
| 24 |
if (!empty($arr['items'])) |
| 25 |
{ |
| 26 |
if(isset($arr['items']['data']) and is_array($arr['items']['data'])) |
| 27 |
{ |
| 28 |
foreach ($arr['items']['data'] as $data) |
| 29 |
{ |
| 30 |
if($data['source'] == 'function') |
| 31 |
{ |
| 32 |
$function = $data['value']; |
| 33 |
$params = explode(',', !empty($data['params']) ? $data['params'] : ''); |
| 34 |
|
| 35 |
$items = call_user_func_array($function, $params); |
| 36 |
$arr['items'] = array_merge($arr['items'], $items); |
| 37 |
} |
| 38 |
else if($data['source'] == 'binding') |
| 39 |
{ |
| 40 |
$function = $data['value']; |
| 41 |
$field = $data['field']; |
| 42 |
$this->set_items_binding($function . '|' . $field); |
| 43 |
} |
| 44 |
} |
| 45 |
unset($arr['items']['data']); |
| 46 |
} |
| 47 |
if(is_array($arr['items'])) foreach ($arr['items'] as $item) |
| 48 |
{ |
| 49 |
$the_item = new VP_Control_Field_Item_Generic(); |
| 50 |
$the_item->value($item['value']) |
| 51 |
->label($item['label']); |
| 52 |
if( isset( $item['img'] ) ) |
| 53 |
$the_item->img($item['img']); |
| 54 |
$this->add_item($the_item); |
| 55 |
} |
| 56 |
} |
| 57 |
if (isset($arr['default'])) |
| 58 |
{ |
| 59 |
$arr['default'] = (array) $arr['default']; |
| 60 |
if(!VP_Util_Reflection::is_multiselectable($this)) |
| 61 |
{ |
| 62 |
$arr['default'] = (array) reset($arr['default']); |
| 63 |
} |
| 64 |
$this->_raw_default = $arr['default']; |
| 65 |
$this->_process_default(); |
| 66 |
} |
| 67 |
return $this; |
| 68 |
} |
| 69 |
|
| 70 |
public function _process_default() |
| 71 |
{ |
| 72 |
$defaults = array(); |
| 73 |
$items = $this->get_items(); |
| 74 |
|
| 75 |
foreach ($this->_raw_default as $def) |
| 76 |
{ |
| 77 |
switch ($def) |
| 78 |
{ |
| 79 |
case '{{all}}': |
| 80 |
if(VP_Util_Reflection::is_multiselectable($this)) |
| 81 |
$defaults = array_merge($defaults, array_keys($items)); |
| 82 |
break; |
| 83 |
case '{{first}}': |
| 84 |
$first = VP_Util_Array::first($items); |
| 85 |
if(!is_null($first)) |
| 86 |
$defaults[] = $first->value; |
| 87 |
break; |
| 88 |
case '{{last}}': |
| 89 |
$last = end($items); |
| 90 |
if(!is_null($last)) |
| 91 |
$defaults[] = $last->value; |
| 92 |
break; |
| 93 |
default: |
| 94 |
$defaults[] = $def; |
| 95 |
break; |
| 96 |
} |
| 97 |
} |
| 98 |
$defaults = array_unique($defaults); |
| 99 |
if(!empty($defaults)) |
| 100 |
$this->set_default($defaults); |
| 101 |
} |
| 102 |
|
| 103 |
protected function _setup_data() |
| 104 |
{ |
| 105 |
parent::_setup_data(); |
| 106 |
$this->add_single_data('head_info', 'items_binding', $this->get_items_binding()); |
| 107 |
$this->add_data('items', $this->get_items()); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Setter for $_default |
| 112 |
* |
| 113 |
* @param mixed $_default default value of the field |
| 114 |
*/ |
| 115 |
public function set_default($_default) { |
| 116 |
if(is_array($_default) and !VP_Util_Reflection::is_multiselectable($this)) |
| 117 |
$_default = VP_Util_Array::first($_default); |
| 118 |
$this->_default = $_default; |
| 119 |
return $this; |
| 120 |
} |
| 121 |
|
| 122 |
public function add_items($items) |
| 123 |
{ |
| 124 |
$this->_items = array_merge($this->_items, $items); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Add single item |
| 129 |
* @param VP_Control_Field_Item_ $opt Single item item |
| 130 |
*/ |
| 131 |
public function add_item($opt) |
| 132 |
{ |
| 133 |
$this->_items[$opt->value] = $opt; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Getter for $_items |
| 138 |
* |
| 139 |
* @return Array array of items {value, label} |
| 140 |
*/ |
| 141 |
public function get_items() { |
| 142 |
return $this->_items; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Setter for $_items |
| 147 |
* |
| 148 |
* @param Array $_items array of items |
| 149 |
*/ |
| 150 |
public function set_items($_items) { |
| 151 |
$this->_items = $_items; |
| 152 |
return $this; |
| 153 |
} |
| 154 |
|
| 155 |
public function add_items_from_array($_items) { |
| 156 |
if(is_array($_items)) |
| 157 |
{ |
| 158 |
foreach ($_items as $item) |
| 159 |
{ |
| 160 |
$the_item = new VP_Control_Field_Item_Generic(); |
| 161 |
$the_item->value($item['value']) |
| 162 |
->label($item['label']); |
| 163 |
$this->add_item($the_item); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Get $_items_binding |
| 170 |
* |
| 171 |
* @return String bind rule string |
| 172 |
*/ |
| 173 |
public function get_items_binding() { |
| 174 |
return $this->_items_binding; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Set $_items_binding |
| 179 |
* |
| 180 |
* @param String $_items_binding bind rule string |
| 181 |
*/ |
| 182 |
public function set_items_binding($_items_binding) { |
| 183 |
$this->_items_binding = $_items_binding; |
| 184 |
return $this; |
| 185 |
} |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* EOF |
| 191 |
*/ |