· 8 years ago · Jun 01, 2018, 06:24 AM
1<?php
2
3 if(!defined('__IN_SYMPHONY__')) die('<h2>Symphony Error</h2><p>You cannot directly access this file</p>');
4
5 Class fieldSelectBox_Link extends Field{
6
7 function __construct(&$parent){
8 parent::__construct($parent);
9 $this->_name = 'Select Box Link';
10 $this->_required = true;
11
12 // Set default
13 $this->set('show_column', 'no');
14 $this->set('required', 'yes');
15 }
16
17 function canToggle(){
18 return ($this->get('allow_multiple_selection') == 'yes' ? false : true);
19 }
20
21 function canFilter(){
22 return true;
23 }
24
25 function canImport() {
26 return true;
27 }
28
29 public function isSortable() {
30 return true;
31 }
32
33 public function prepareImportValue($value) {
34 return $this->fetchAssociatedEntrySearchValue(array(
35 'handle' => Lang::createHandle($value)
36 ));
37 }
38
39 function allowDatasourceOutputGrouping(){
40 return true;
41 }
42
43 function allowDatasourceParamOutput(){
44 return true;
45 }
46
47 public function getParameterPoolValue($data){
48 return $data['relation_id'];
49 }
50
51 function groupRecords($records){
52
53 if(!is_array($records) || empty($records)) return;
54
55 $groups = array($this->get('element_name') => array());
56
57 foreach($records as $r){
58 $data = $r->getData($this->get('id'));
59
60 $value = $data['relation_id'];
61
62 $primary_field = $this->__findPrimaryFieldValueFromRelationID($data['relation_id']);
63
64 if(!isset($groups[$this->get('element_name')][$value])){
65 $groups[$this->get('element_name')][$value] = array('attr' => array('link-id' => $data['relation_id'], 'link-handle' => Lang::createHandle($primary_field['value'])),
66 'records' => array(), 'groups' => array());
67 }
68
69 $groups[$this->get('element_name')][$value]['records'][] = $r;
70
71 }
72
73 return $groups;
74
75 }
76
77 function prepareTableValue($data, XMLElement $link=NULL){
78 $result = array();
79
80 if(!is_array($data) || (is_array($data) && !isset($data['relation_id']))) return parent::prepareTableValue(NULL);
81
82 if(!is_array($data['relation_id'])){
83 $data['relation_id'] = array($data['relation_id']);
84 }
85
86 foreach($data['relation_id'] as $relation_id){
87 if((int)$relation_id <= 0) continue;
88
89 $primary_field = $this->__findPrimaryFieldValueFromRelationID($relation_id);
90
91 if(!is_array($primary_field) || empty($primary_field)){
92 continue;
93 }
94
95 $result[$relation_id] = $primary_field;
96
97 }
98
99 if(!is_null($link)){
100 $label = NULL;
101 foreach($result as $item){
102 $label .= ' ' . $item['value'];
103 }
104 $link->setValue(General::sanitize(trim($label)));
105 return $link->generate();
106 }
107
108 $output = NULL;
109
110 foreach($result as $relation_id => $item){
111 $link = Widget::Anchor($item['value'], sprintf('%s/symphony/publish/%s/edit/%d/', URL, $item['section_handle'], $relation_id));
112
113 $output .= $link->generate() . ' ';
114 }
115
116 return trim($output);
117 }
118
119 private function __findPrimaryFieldValueFromRelationID($id){
120
121 $primary_field = $this->Database->fetchRow(0,
122
123 "SELECT `f`.*, `s`.handle AS `section_handle`
124 FROM `tbl_fields` AS `f`
125 INNER JOIN `tbl_sections` AS `s` ON `s`.id = `f`.parent_section
126 WHERE `f`.id = '".$this->get('related_field_id')."'
127 ORDER BY `f`.sortorder ASC
128 LIMIT 1"
129
130 );
131
132 if(!$primary_field) return NULL;
133
134 $field = $this->_Parent->create($primary_field['type']);
135
136 $data = $this->Database->fetchRow(0,
137 "SELECT *
138 FROM `tbl_entries_data_".$this->get('related_field_id')."`
139 WHERE `entry_id` = '$id' ORDER BY `id` DESC LIMIT 1"
140 );
141
142 if(empty($data)) return NULL;
143
144 $primary_field['value'] = $field->prepareTableValue($data);
145
146 return $primary_field;
147
148 }
149
150 function processRawFieldData($data, &$status, $simulate=false, $entry_id=NULL){
151
152 $status = self::__OK__;
153 if(!is_array($data)) return array('relation_id' => $data);
154
155 if(empty($data)) return NULL;
156
157 $result = array();
158
159 foreach($data as $a => $value) {
160 $result['relation_id'][] = $data[$a];
161 }
162
163 return $result;
164
165 }
166
167 function fetchAssociatedEntrySearchValue($data){
168 if(!is_array($data)) return $data;
169
170 $searchvalue = $this->_engine->Database->fetchRow(0,
171 sprintf("
172 SELECT `entry_id` FROM `tbl_entries_data_%d`
173 WHERE `handle` = '%s'
174 LIMIT 1", $this->get('related_field_id'), addslashes($data['handle']))
175 );
176
177 return $searchvalue['entry_id'];
178 }
179
180 function fetchAssociatedEntryCount($value){
181 return $this->_engine->Database->fetchVar('count', 0, "SELECT count(*) AS `count` FROM `tbl_entries_data_".$this->get('id')."` WHERE `relation_id` = '$value'");
182 }
183
184 function fetchAssociatedEntryIDs($value){
185 return $this->_engine->Database->fetchCol('entry_id', "SELECT `entry_id` FROM `tbl_entries_data_".$this->get('id')."` WHERE `relation_id` = '$value'");
186 }
187
188 function appendFormattedElement(&$wrapper, $data, $encode=false){
189
190 if(!is_array($data) || empty($data)) return;
191
192 $list = new XMLElement($this->get('element_name'));
193 if(!is_array($data['relation_id'])) $data['relation_id'] = array($data['relation_id']);
194
195 foreach ($data['relation_id'] as $relation_id) {
196 $primary_field = $this->__findPrimaryFieldValueFromRelationID($relation_id);
197 $section = $this->_engine->Database->fetchRow(0, "SELECT `id`, `handle` FROM `tbl_sections` WHERE `id` = '".$primary_field['parent_section']."' LIMIT 1");
198
199 $item_handle = Lang::createHandle($primary_field['value']);
200
201 $value = General::sanitize($primary_field['value']);
202
203 if ($encode) {
204 $value = General::sanitize($value);
205 }
206
207 $list->appendChild(new XMLElement('item', $value, array(
208 'handle' => $item_handle,
209 'id' => $relation_id
210 )));
211 }
212
213 $wrapper->appendChild($list);
214
215 }
216
217 function findOptions(){
218
219 $values = array();
220
221 $sql = "SELECT DISTINCT `entry_id` FROM `tbl_entries_data_".$this->get('related_field_id')."`
222 ORDER BY `entry_id` DESC";
223
224 if($results = $this->Database->fetch($sql)){
225 foreach($results as $r){
226 $value = $this->__findPrimaryFieldValueFromRelationID($r['entry_id']);
227 $values[$r['entry_id']] = $value['value'];
228 }
229 }
230
231 return $values;
232
233 }
234
235 function displayPublishPanel(&$wrapper, $data=NULL, $flagWithError=NULL, $fieldnamePrefix=NULL, $fieldnamePostfix=NULL){
236
237 $states = $this->findOptions();
238
239 $options = array();
240
241 if($this->get('required') != 'yes') $options[] = array(NULL, false, NULL);
242
243 if(is_array($data['relation_id'])) {
244 $entry_id = array();
245
246 foreach($data['relation_id'] as $value) {
247 $entry_id[] = $value;
248 }
249
250 foreach($states as $id => $v){
251 if (in_array($id, $entry_id)) {
252 $options[] = array($id, TRUE, $v);
253 }
254
255 else{
256 $options[] = array($id, FALSE, $v);
257 }
258 }
259 }
260 else{
261 $entry_id = $data['relation_id'];
262 foreach($states as $id => $v){
263 $options[] = array($id, $id == $entry_id, $v);
264 }
265 }
266
267 $fieldname = 'fields'.$fieldnamePrefix.'['.$this->get('element_name').']'.$fieldnamePostfix;
268 if($this->get('allow_multiple_selection') == 'yes') $fieldname .= '[]';
269
270 $label = Widget::Label($this->get('label'));
271 $label->appendChild(Widget::Select($fieldname, $options, ($this->get('allow_multiple_selection') == 'yes' ? array('multiple' => 'multiple') : NULL)));
272
273 if($flagWithError != NULL) $wrapper->appendChild(Widget::wrapFormElementWithError($label, $flagWithError));
274 else $wrapper->appendChild($label);
275 }
276
277 function commit(){
278
279 if(!parent::commit()) return false;
280
281 $id = $this->get('id');
282
283 if($id === false) return false;
284
285 $fields = array();
286
287 $fields['field_id'] = $id;
288 if($this->get('related_field_id') != '') $fields['related_field_id'] = $this->get('related_field_id');
289 $fields['allow_multiple_selection'] = ($this->get('allow_multiple_selection') ? $this->get('allow_multiple_selection') : 'no');
290
291 $this->Database->query("DELETE FROM `tbl_fields_".$this->handle()."` WHERE `field_id` = '$id'");
292
293 if(!$this->Database->insert($fields, 'tbl_fields_' . $this->handle())) return false;
294
295 $sections = $this->get('related_field_id');
296
297 $this->removeSectionAssociation($id);
298
299 $section_id = $this->Database->fetchVar('parent_section', 0, "SELECT `parent_section` FROM `tbl_fields` WHERE `id` = '".$fields['related_field_id']."' LIMIT 1");
300
301 $this->createSectionAssociation(NULL, $id, $this->get('related_field_id'));
302
303 return true;
304
305 }
306
307 function buildSortingSQL(&$joins, &$where, &$sort, $order='ASC'){
308 $joins .= "INNER JOIN `tbl_entries_data_".$this->get('id')."` AS `ed` ON (`e`.`id` = `ed`.`entry_id`) ";
309 $sort = 'ORDER BY ' . (strtolower($order) == 'random' ? 'RAND()' : "`ed`.`relation_id` $order");
310 }
311
312 function buildDSRetrivalSQL($data, &$joins, &$where, $andOperation=false){
313
314 $field_id = $this->get('id');
315
316 if($andOperation):
317
318 foreach($data as $key => $bit){
319 $joins .= " LEFT JOIN `tbl_entries_data_$field_id` AS `t$field_id$key` ON (`e`.`id` = `t$field_id$key`.entry_id) ";
320 $where .= " AND `t$field_id$key`.relation_id = '$bit' ";
321 }
322
323 else:
324
325 $joins .= " LEFT JOIN `tbl_entries_data_$field_id` AS `t$field_id` ON (`e`.`id` = `t$field_id`.entry_id) ";
326 $where .= " AND `t$field_id`.relation_id IN ('".@implode("', '", $data)."') ";
327
328 endif;
329
330 return true;
331
332 }
333
334 function findDefaults(&$fields){
335 if(!isset($fields['allow_multiple_selection'])) $fields['allow_multiple_selection'] = 'no';
336 }
337
338 function displaySettingsPanel(&$wrapper, $errors=NULL){
339
340 parent::displaySettingsPanel($wrapper, $errors);
341
342 $div = new XMLElement('div', NULL, array('class' => 'group'));
343
344 $label = Widget::Label('Options');
345
346 $sectionManager = new SectionManager($this->_engine);
347 $sections = $sectionManager->fetch(NULL, 'ASC', 'name');
348 $field_groups = array();
349
350 if(is_array($sections) && !empty($sections)){
351 foreach($sections as $section) $field_groups[$section->get('id')] = array('fields' => $section->fetchFields(), 'section' => $section);
352 }
353
354 $options = array();
355
356 foreach($field_groups as $group){
357
358 if(!is_array($group['fields'])) continue;
359
360 $fields = array();
361 foreach($group['fields'] as $f){
362 if($f->get('id') != $this->get('id') && $f->canPrePopulate()) $fields[] = array($f->get('id'), ($this->get('related_field_id') == $f->get('id')), $f->get('label'));
363 }
364
365 if(is_array($fields) && !empty($fields)) $options[] = array('label' => $group['section']->get('name'), 'options' => $fields);
366 }
367
368 $label->appendChild(Widget::Select('fields['.$this->get('sortorder').'][related_field_id]', $options));
369 $div->appendChild($label);
370
371 if(isset($errors['related_field_id'])) $wrapper->appendChild(Widget::wrapFormElementWithError($div, $errors['related_field_id']));
372 else $wrapper->appendChild($div);
373
374 ## Allow selection of multiple items
375 $label = Widget::Label();
376 $input = Widget::Input('fields['.$this->get('sortorder').'][allow_multiple_selection]', 'yes', 'checkbox');
377 if($this->get('allow_multiple_selection') == 'yes') $input->setAttribute('checked', 'checked');
378 $label->setValue($input->generate() . ' Allow selection of multiple options');
379 $wrapper->appendChild($label);
380
381 $this->appendShowColumnCheckbox($wrapper);
382 $this->appendRequiredCheckbox($wrapper);
383
384 }
385
386
387 function createTable(){
388
389 return $this->_engine->Database->query(
390
391 "CREATE TABLE IF NOT EXISTS `tbl_entries_data_" . $this->get('id') . "` (
392 `id` int(11) unsigned NOT NULL auto_increment,
393 `entry_id` int(11) unsigned NOT NULL,
394 `relation_id` int(11) unsigned NOT NULL,
395 PRIMARY KEY (`id`),
396 KEY `entry_id` (`entry_id`),
397 KEY `relation_id` (`relation_id`)
398 ) TYPE=MyISAM;"
399
400 );
401 }
402
403 public function getExampleFormMarkup(){
404 return Widget::Input('fields['.$this->get('element_name').']', '...', 'hidden');
405 }
406
407 }