PluginProbe
WP Ultimate Post Grid / 2.8.0
WP Ultimate Post Grid v2.8.0
4.1.1 trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.1 2.2 2.3 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.2 3.0.0 3.3.0 3.4.0 3.5.0 3.6.0 3.7.0 All 32 releases
wp-ultimate-post-grid / vendor / vafpress / classes / control / fieldmulti.php

fieldmulti.php in WP Ultimate Post Grid 2.8.0, at vendor/vafpress/classes/control/fieldmulti.php

193 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 if( is_array( $items ) ) {
37 $arr['items'] = array_merge($arr['items'], $items);
38 }
39 }
40 else if($data['source'] == 'binding')
41 {
42 $function = $data['value'];
43 $field = $data['field'];
44 $this->set_items_binding($function . '|' . $field);
45 }
46 }
47 unset($arr['items']['data']);
48 }
49 if(is_array($arr['items'])) foreach ($arr['items'] as $item)
50 {
51 $the_item = new VP_Control_Field_Item_Generic();
52 $the_item->value($item['value'])
53 ->label($item['label']);
54 if( isset( $item['img'] ) )
55 $the_item->img($item['img']);
56 $this->add_item($the_item);
57 }
58 }
59 if (isset($arr['default']))
60 {
61 $arr['default'] = (array) $arr['default'];
62 if(!VP_Util_Reflection::is_multiselectable($this))
63 {
64 $arr['default'] = (array) reset($arr['default']);
65 }
66 $this->_raw_default = $arr['default'];
67 $this->_process_default();
68 }
69 return $this;
70 }
71
72 public function _process_default()
73 {
74 $defaults = array();
75 $items = $this->get_items();
76
77 foreach ($this->_raw_default as $def)
78 {
79 switch ($def)
80 {
81 case '{{all}}':
82 if(VP_Util_Reflection::is_multiselectable($this))
83 $defaults = array_merge($defaults, array_keys($items));
84 break;
85 case '{{first}}':
86 $first = VP_Util_Array::first($items);
87 if(!is_null($first))
88 $defaults[] = $first->value;
89 break;
90 case '{{last}}':
91 $last = end($items);
92 if(!is_null($last))
93 $defaults[] = $last->value;
94 break;
95 default:
96 $defaults[] = $def;
97 break;
98 }
99 }
100 $defaults = array_unique($defaults);
101 if(!empty($defaults))
102 $this->set_default($defaults);
103 }
104
105 protected function _setup_data()
106 {
107 parent::_setup_data();
108 $this->add_single_data('head_info', 'items_binding', $this->get_items_binding());
109 $this->add_data('items', $this->get_items());
110 }
111
112 /**
113 * Setter for $_default
114 *
115 * @param mixed $_default default value of the field
116 */
117 public function set_default($_default) {
118 if(is_array($_default) and !VP_Util_Reflection::is_multiselectable($this))
119 $_default = VP_Util_Array::first($_default);
120 $this->_default = $_default;
121 return $this;
122 }
123
124 public function add_items($items)
125 {
126 $this->_items = array_merge($this->_items, $items);
127 }
128
129 /**
130 * Add single item
131 * @param VP_Control_Field_Item_ $opt Single item item
132 */
133 public function add_item($opt)
134 {
135 $this->_items[$opt->value] = $opt;
136 }
137
138 /**
139 * Getter for $_items
140 *
141 * @return Array array of items {value, label}
142 */
143 public function get_items() {
144 return $this->_items;
145 }
146
147 /**
148 * Setter for $_items
149 *
150 * @param Array $_items array of items
151 */
152 public function set_items($_items) {
153 $this->_items = $_items;
154 return $this;
155 }
156
157 public function add_items_from_array($_items) {
158 if(is_array($_items))
159 {
160 foreach ($_items as $item)
161 {
162 $the_item = new VP_Control_Field_Item_Generic();
163 $the_item->value($item['value'])
164 ->label($item['label']);
165 $this->add_item($the_item);
166 }
167 }
168 }
169
170 /**
171 * Get $_items_binding
172 *
173 * @return String bind rule string
174 */
175 public function get_items_binding() {
176 return $this->_items_binding;
177 }
178
179 /**
180 * Set $_items_binding
181 *
182 * @param String $_items_binding bind rule string
183 */
184 public function set_items_binding($_items_binding) {
185 $this->_items_binding = $_items_binding;
186 return $this;
187 }
188
189 }
190
191 /**
192 * EOF
193 */