/
class.field.php
1746 lines (1563 loc) · 61.9 KB
/
class.field.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @package toolkit
*/
/**
* The Field class represents a Symphony Field object. Fields are the building
* blocks for Sections. All fields instances are unique and can only be used once
* in a Symphony install. Fields have their own field table which records where
* instances of this field type have been used in other sections and their settings.
* They also spinoff other `tbl_entry_data_{id}` tables that actually store data for
* entries particular to this field.
*/
class Field
{
/**
* The desired result when creating a field in the section editor
* @var integer
*/
const __OK__ = 100;
/**
* If an error occurring when saving a section because of this field,
* this will be returned
* @var integer
*/
const __ERROR__ = 150;
/**
* When saving a section, if a value that is required is missing,
* this will be returned
* @var integer
*/
const __MISSING_FIELDS__ = 200;
/**
* If a value for a setting is invalid, this will be returned
* @var integer
*/
const __INVALID_FIELDS__ = 220;
/**
* If there already is an instance of this field in this section and
* `mustBeUnique()` returns true, this will be returned
* @var integer
* @see mustBeUnique()
*/
const __DUPLICATE__ = 300;
/**
* Fields can returned this is an error occurred when saving the
* field's settings that doesn't fit another `Field` constant
* @var integer
*/
const __ERROR_CUSTOM__ = 400;
/**
* If the field name is not a valid QName, this error will be returned
* @var integer
*/
const __INVALID_QNAME__ = 500;
/**
* Used by the `FieldManager` to return fields that can be toggled
* @var integer
*/
const __TOGGLEABLE_ONLY__ = 600;
/**
* Used by the `FieldManager` to return fields that can't be toggled
* @var integer
*/
const __UNTOGGLEABLE_ONLY__ = 700;
/**
* Used by the `FieldManager` to return fields that can be filtered
* @var integer
*/
const __FILTERABLE_ONLY__ = 800;
/**
* Used by the `FieldManager` to return fields that can't be filtered
* @var integer
*/
const __UNFILTERABLE_ONLY__ = 900;
/**
* Used by the `FieldManager` to just return all fields
* @var integer
*/
const __FIELD_ALL__ = 1000;
/**
* Used to manage the joins when this field used in a datasource
* @var integer
*/
protected $_key = 0;
/**
* An associative array of the settings for this `Field` instance
* @var array
*/
protected $_settings = array();
/**
* Whether this field is required inherently, defaults to false.
* @var boolean
*/
protected $_required = false;
/**
* Whether this field can be viewed on the entries table. Note
* that this is not the same variable as the one set when saving
* a field in the section editor, rather just the if the field has
* the ability to be shown. Defaults to true.
* @var boolean
*/
protected $_showcolumn = true;
/**
* Whether this field has an association that should be shown on
* the Publish Index. This does not mean that it will be, but just
* that this field has the ability too. Defaults to false.
* @var boolean
*/
protected $_showassociation = false;
/**
* Construct a new instance of this field.
*/
public function __construct()
{
$this->_handle = (strtolower(get_class($this)) == 'field' ? 'field' : strtolower(substr(get_class($this), 5)));
}
/**
* Test whether this field can show the table column.
*
* @return boolean
* true if this can, false otherwise.
*/
public function canShowTableColumn()
{
return $this->_showcolumn;
}
/**
* Test whether this field can show the association column in
* the Publish Index.
*
* @since Symphony 2.6.0
* @return boolean
* true if this can, false otherwise.
*/
public function canShowAssociationColumn()
{
return $this->_showassociation;
}
/**
* Test whether this field can be toggled using the With Selected menu
* on the Publish Index.
*
* @return boolean
* true if it can be toggled, false otherwise.
*/
public function canToggle()
{
return false;
}
/**
* Accessor to the toggle states. This default implementation returns
* an empty array.
*
* @return array
* the array of toggle states.
*/
public function getToggleStates()
{
return array();
}
/**
* Toggle the field data. This default implementation always returns
* the input data.
*
* @param array $data
* the data to toggle.
* @param string $newState
* the new value to set
* @param integer $entry_id (optional)
* an optional entry ID for more intelligent processing. defaults to null
* @return array
* the toggled data.
*/
public function toggleFieldData(array $data, $newState, $entry_id = null)
{
return $data;
}
/**
* Test whether this field can be filtered. This default implementation
* prohibits filtering. Filtering allows the XML output results to be limited
* according to an input parameter. Subclasses should override this if
* filtering is supported.
*
* @return boolean
* true if this can be filtered, false otherwise.
*/
public function canFilter()
{
return false;
}
/**
* Test whether this field can be filtered in the publish index. This default
* implementation prohibts filtering. Publish Filtering allows the index view
* to filter results. Subclasses should override this if
* filtering is supported.
*
* @return boolean
* true if this can be publish-filtered, false otherwise.
*/
public function canPublishFilter()
{
return $this->canFilter();
}
/**
* Test whether this field can be prepopulated with data. This default
* implementation does not support pre-population and, thus, returns false.
*
* @return boolean
* true if this can be pre-populated, false otherwise.
*/
public function canPrePopulate()
{
return false;
}
/**
* Test whether this field can be sorted. This default implementation
* returns false.
*
* @return boolean
* true if this field is sortable, false otherwise.
*/
public function isSortable()
{
return false;
}
/**
* Test whether this field must be unique in a section, that is, only one of
* this field's type is allowed per section. This default implementation
* always returns false.
*
* @return boolean
* true if the content of this field must be unique, false otherwise.
*/
public function mustBeUnique()
{
return false;
}
/**
* Test whether this field supports data-source output grouping. This
* default implementation prohibits grouping. Data-source grouping allows
* clients of this field to group the XML output according to this field.
* Subclasses should override this if grouping is supported.
*
* @return boolean
* true if this field does support data-source grouping, false otherwise.
*/
public function allowDatasourceOutputGrouping()
{
return false;
}
/**
* Test whether this field requires grouping. If this function returns true
* SQL statements generated in the `EntryManager` will include the `DISTINCT` keyword
* to only return a single row for an entry regardless of how many 'matches' it
* might have. This default implementation returns false.
*
* @return boolean
* true if this field requires grouping, false otherwise.
*/
public function requiresSQLGrouping()
{
return false;
}
/**
* Test whether this field supports data-source parameter output. This
* default implementation prohibits parameter output. Data-source
* parameter output allows this field to be provided as a parameter
* to other data-sources or XSLT. Subclasses should override this if
* parameter output is supported.
*
* @return boolean
* true if this supports data-source parameter output, false otherwise.
*/
public function allowDatasourceParamOutput()
{
return false;
}
/**
* Accessor to the handle of this field object. The Symphony convention is
* for field subclass names to be prefixed with field. Handle removes this prefix
* so that the class handle can be used as the field type.
*
* @return string
* The field classname minus the field prefix.
*/
public function handle()
{
return $this->_handle;
}
/**
* Accessor to the name of this field object. The name may contain characters
* that normally would be stripped in the handle while also allowing the field
* name to be localized. If a name is not set, it will return the handle of the
* the field
*
* @return string
* The field name
*/
public function name()
{
return ($this->_name ? $this->_name : $this->_handle);
}
/**
* Clean the input value using html entity encode and the database specific
* clean methods.
*
* @param mixed $value
* the value to clean.
* @return string
* the cleaned value.
*/
public function cleanValue($value)
{
return html_entity_decode(Symphony::Database()->cleanValue($value));
}
/**
* Fields have settings that define how that field will act in a section, including
* if it's required, any validators, if it can be shown on the entries table etc. This
* function will set a setting to a value. This function will set a setting to a value
* overwriting any existing value for this setting
*
* @param string $setting
* the setting key.
* @param mixed $value
* the value of the setting.
*/
public function set($setting, $value)
{
$this->_settings[$setting] = $value;
}
/**
* Add or overwrite the settings of this field by providing an associative array
* of the settings. This will do nothing if the input array is empty. If a setting is
* omitted from the input array, it will not be unset by this function
*
* @param array $array
* the associative array of settings for this field
*/
public function setArray(array $array = array())
{
if (empty($array)) {
return;
}
foreach ($array as $setting => $value) {
$this->set($setting, $value);
}
}
/**
* Fill the input data array with default values for known keys provided
* these settings are not already set. The input array is then used to set
* the values of the corresponding settings for this field. This function
* is called when a section is saved.
*
* @param array $settings
* the data array to initialize if necessary.
*/
public function setFromPOST(array $settings = array())
{
$settings['location'] = (isset($settings['location']) ? $settings['location'] : 'main');
$settings['required'] = (isset($settings['required']) && $settings['required'] === 'yes' ? 'yes' : 'no');
$settings['show_column'] = (isset($settings['show_column']) && $settings['show_column'] === 'yes' ? 'yes' : 'no');
$this->setArray($settings);
}
/**
* Accessor to the a setting by name. If no setting is provided all the
* settings of this `Field` instance are returned.
*
* @param string $setting (optional)
* the name of the setting to access the value for. This is optional and
* defaults to null in which case all settings are returned.
* @return null|mixed|array
* the value of the setting if there is one, all settings if the input setting
* was omitted or null if the setting was supplied but there is no value
* for that setting.
*/
public function get($setting = null)
{
if (is_null($setting)) {
return $this->_settings;
}
if (!isset($this->_settings[$setting])) {
return null;
}
return $this->_settings[$setting];
}
/**
* Unset the value of a setting by the key
*
* @param string $setting
* the key of the setting to unset.
*/
public function remove($setting)
{
unset($this->_settings[$setting]);
}
/**
* Just prior to the field being deleted, this function allows
* Fields to cleanup any additional things before it is removed
* from the section. This may be useful to remove data from any
* custom field tables or the configuration.
*
* @since Symphony 2.2.1
* @return boolean
*/
public function tearDown()
{
return true;
}
/**
* Allows a field to set default settings.
*
* @param array $settings
* the array of settings to populate with their defaults.
*/
public function findDefaults(array &$settings)
{
}
/**
* Display the default settings panel, calls the `buildSummaryBlock`
* function after basic field settings are added to the wrapper.
*
* @see buildSummaryBlock()
* @param XMLElement $wrapper
* the input XMLElement to which the display of this will be appended.
* @param mixed $errors
* the input error collection. this defaults to null.
* @throws InvalidArgumentException
*/
public function displaySettingsPanel(XMLElement &$wrapper, $errors = null)
{
// Create header
$location = ($this->get('location') ? $this->get('location') : 'main');
$header = new XMLElement('header', null, array('class' => 'frame-header ' . $location, 'data-name' => $this->name()));
$label = (($this->get('label')) ? $this->get('label') : __('New Field'));
$header->appendChild(new XMLElement('h4', '<strong>' . $label . '</strong> <span class="type">' . $this->name() . '</span>'));
$wrapper->appendChild($header);
// Create content
$wrapper->appendChild(Widget::Input('fields['.$this->get('sortorder').'][type]', $this->handle(), 'hidden'));
if ($this->get('id')) {
$wrapper->appendChild(Widget::Input('fields['.$this->get('sortorder').'][id]', $this->get('id'), 'hidden'));
}
$wrapper->appendChild($this->buildSummaryBlock($errors));
}
/**
* Construct the html block to display a summary of this field, which is the field
* Label and it's location within the section. Any error messages generated are
* appended to the optional input error array. This function calls
* `buildLocationSelect` once it is completed
*
* @see buildLocationSelect()
* @param array $errors (optional)
* an array to append html formatted error messages to. this defaults to null.
* @throws InvalidArgumentException
* @return XMLElement
* the root XML element of the html display of this.
*/
public function buildSummaryBlock($errors = null)
{
$div = new XMLElement('div');
// Publish label
$label = Widget::Label(__('Label'));
$label->appendChild(
Widget::Input('fields['.$this->get('sortorder').'][label]', $this->get('label'))
);
if (isset($errors['label'])) {
$div->appendChild(Widget::Error($label, $errors['label']));
} else {
$div->appendChild($label);
}
// Handle + placement
$group = new XMLElement('div');
$group->setAttribute('class', 'two columns');
$label = Widget::Label(__('Handle'));
$label->setAttribute('class', 'column');
$label->appendChild(Widget::Input('fields['.$this->get('sortorder').'][element_name]', $this->get('element_name')));
if (isset($errors['element_name'])) {
$group->appendChild(Widget::Error($label, $errors['element_name']));
} else {
$group->appendChild($label);
}
// Location
$group->appendChild($this->buildLocationSelect($this->get('location'), 'fields['.$this->get('sortorder').'][location]'));
$div->appendChild($group);
return $div;
}
/**
* Build the location select widget. This widget allows users to select
* whether this field will appear in the main content column or in the sidebar
* when creating a new entry.
*
* @param string|null $selected (optional)
* the currently selected location, if there is one. this defaults to null.
* @param string $name (optional)
* the name of this field. this is optional and defaults to `fields[location]`.
* @param string $label_value (optional)
* any predefined label for this widget. this is an optional argument that defaults
* to null.
* @throws InvalidArgumentException
* @return XMLElement
* An XMLElement representing a `<select>` field containing the options.
*/
public function buildLocationSelect($selected = null, $name = 'fields[location]', $label_value = null)
{
if (!$label_value) {
$label_value = __('Placement');
}
$label = Widget::Label($label_value);
$label->setAttribute('class', 'column');
$options = array(
array('main', $selected == 'main', __('Main content')),
array('sidebar', $selected == 'sidebar', __('Sidebar'))
);
$label->appendChild(Widget::Select($name, $options));
return $label;
}
/**
* Construct the html widget for selecting a text formatter for this field.
*
* @param string $selected (optional)
* the currently selected text formatter name if there is one. this defaults
* to null.
* @param string $name (optional)
* the name of this field in the form. this is optional and defaults to
* "fields[format]".
* @param string $label_value
* the default label for the widget to construct. if null is passed in then
* this defaults to the localization of "Formatting".
* @throws InvalidArgumentException
* @return XMLElement
* An XMLElement representing a `<select>` field containing the options.
*/
public function buildFormatterSelect($selected = null, $name = 'fields[format]', $label_value)
{
$formatters = TextformatterManager::listAll();
if (!$label_value) {
$label_value = __('Formatting');
}
$label = Widget::Label($label_value);
$label->setAttribute('class', 'column');
$options = array();
$options[] = array('none', false, __('None'));
if (!empty($formatters) && is_array($formatters)) {
foreach ($formatters as $handle => $about) {
$options[] = array($handle, ($selected == $handle), $about['name']);
}
}
$label->appendChild(Widget::Select($name, $options));
return $label;
}
/**
* Append a validator selector to a given `XMLElement`. Note that this
* function differs from the other two similarly named build functions in
* that it takes an `XMLElement` to append the Validator to as a parameter,
* and does not return anything.
*
* @param XMLElement $wrapper
* the parent element to append the XMLElement of the Validation select to,
* passed by reference.
* @param string $selected (optional)
* the current validator selection if there is one. defaults to null if there
* isn't.
* @param string $name (optional)
* the form element name of this field. this defaults to "fields[validator]".
* @param string $type (optional)
* the type of input for the validation to apply to. this defaults to 'input'
* but also accepts 'upload'.
* @param array $errors (optional)
* an associative array of errors
* @throws InvalidArgumentException
*/
public function buildValidationSelect(XMLElement &$wrapper, $selected = null, $name = 'fields[validator]', $type = 'input', array $errors = null)
{
include TOOLKIT . '/util.validators.php';
$rules = ($type == 'upload' ? $upload : $validators);
$label = Widget::Label(__('Validation Rule'));
$label->setAttribute('class', 'column');
$label->appendChild(new XMLElement('i', __('Optional')));
$label->appendChild(Widget::Input($name, $selected));
$ul = new XMLElement('ul', null, array('class' => 'tags singular', 'data-interactive' => 'data-interactive'));
foreach ($rules as $name => $rule) {
$ul->appendChild(new XMLElement('li', $name, array('class' => $rule)));
}
if (isset($errors['validator'])) {
$div = new XMLElement('div');
$div->appendChild($label);
$div->appendChild($ul);
$wrapper->appendChild(Widget::Error($div, $errors['validator']));
} else {
$wrapper->appendChild($label);
$wrapper->appendChild($ul);
}
}
/**
* Append the html widget for selecting an association interface and editor
* for this field.
*
* @param XMLElement $wrapper
* the parent XML element to append the association interface selection to,
* if either interfaces or editors are provided to the system.
* @since Symphony 2.5.0
*/
public function appendAssociationInterfaceSelect(XMLElement &$wrapper)
{
$wrapper->setAttribute('data-condition', 'associative');
$interfaces = Symphony::ExtensionManager()->getProvidersOf(iProvider::ASSOCIATION_UI);
$editors = Symphony::ExtensionManager()->getProvidersOf(iProvider::ASSOCIATION_EDITOR);
if (!empty($interfaces) || !empty($editors)) {
$association_context = $this->getAssociationContext();
$group = new XMLElement('div');
if (!empty($interfaces) && !empty($editors)) {
$group->setAttribute('class', 'two columns');
}
// Create interface select
if (!empty($interfaces)) {
$label = Widget::Label(__('Association Interface'), null, 'column');
$label->appendChild(new XMLElement('i', __('Optional')));
$options = array(
array(null, false, __('None'))
);
foreach ($interfaces as $id => $name) {
$options[] = array($id, ($association_context['interface'] === $id), $name);
}
$select = Widget::Select('fields[' . $this->get('sortorder') . '][association_ui]', $options);
$label->appendChild($select);
$group->appendChild($label);
}
// Create editor select
if (!empty($editors)) {
$label = Widget::Label(__('Association Editor'), null, 'column');
$label->appendChild(new XMLElement('i', __('Optional')));
$options = array(
array(null, false, __('None'))
);
foreach ($editors as $id => $name) {
$options[] = array($id, ($association_context['editor'] === $id), $name);
}
$select = Widget::Select('fields[' . $this->get('sortorder') . '][association_editor]', $options);
$label->appendChild($select);
$group->appendChild($label);
}
$wrapper->appendChild($group);
}
}
/**
* Get association data of the current field from the page context.
*
* @since Symphony 2.5.0
* @return array
*/
public function getAssociationContext() {
$context = Symphony::Engine()->Page->getContext();
$associations = $context['associations']['parent'];
$field_association = array();
$count = 0;
if (!empty($associations)) {
$associationsCount = count($associations);
for ($i = 0; $i < $associationsCount; $i++) {
if ($associations[$i]['child_section_field_id'] == $this->get('id')) {
if ($count === 0) {
$field_association = $associations[$i];
$count++;
} else {
$field_association['parent_section_id'] .= '|' . $associations[$i]['parent_section_id'];
$field_association['parent_section_field_id'] .= '|' . $associations[$i]['parent_section_field_id'];
}
}
}
}
return $field_association;
}
/**
* Set association data for the current field.
*
* @since Symphony 2.5.0
* @param XMLElement $wrapper
*/
public function setAssociationContext(XMLElement &$wrapper) {
$association_context = $this->getAssociationContext();
if (!empty($association_context)) {
$wrapper->setAttributeArray(array(
'data-parent-section-id' => $association_context['parent_section_id'],
'data-parent-section-field-id' => $association_context['parent_section_field_id'],
'data-child-section-id' => $association_context['child_section_id'],
'data-child-section-field-id' => $association_context['child_section_field_id'],
'data-interface' => $association_context['interface'],
'data-editor' => $association_context['editor']
));
}
}
/**
* Append and set a labeled html checkbox to the input XML element if this
* field is set as a required field.
*
* @param XMLElement $wrapper
* the parent XML element to append the constructed html checkbox to if
* necessary.
* @throws InvalidArgumentException
*/
public function appendRequiredCheckbox(XMLElement &$wrapper)
{
if (!$this->_required) {
return;
}
$this->createCheckboxSetting($wrapper, 'required', __('Make this a required field'));
}
/**
* Append the show column html widget to the input parent XML element. This
* displays a column in the entries table or not.
*
* @param XMLElement $wrapper
* the parent XML element to append the checkbox to.
* @throws InvalidArgumentException
*/
public function appendShowColumnCheckbox(XMLElement &$wrapper)
{
if (!$this->_showcolumn) {
return;
}
$this->createCheckboxSetting($wrapper, 'show_column', __('Display in entries table'));
}
/**
* Append the show association html widget to the input parent XML element. This
* widget allows fields that provide linking to hide or show the column in the linked
* section, similar to how the Show Column functionality works, but for the linked
* section.
*
* @param XMLElement $wrapper
* the parent XML element to append the checkbox to.
* @param string $help (optional)
* a help message to show below the checkbox.
* @throws InvalidArgumentException
*/
public function appendShowAssociationCheckbox(XMLElement &$wrapper, $help = null)
{
if (!$this->_showassociation) {
return;
}
$label = $this->createCheckboxSetting($wrapper, 'show_association', __('Display associations in entries table'), $help);
$label->setAttribute('data-condition', 'associative');
}
/**
* Given the setting name and the label, this helper method will add
* the required markup for a checkbox to the given `$wrapper`.
*
* @since Symphony 2.5.2
* @param XMLElement $wrapper
* Passed by reference, this will have the resulting markup appended to it
* @param string $setting
* This will be used with $this->get() to get the existing value
* @param string $label_description
* This will be localisable and displayed after the checkbox when
* generated.
* @param string $help (optional)
* A help message to show below the checkbox.
* @return XMLElement
* The Label and Checkbox that was just added to the `$wrapper`.
*/
public function createCheckboxSetting(XMLElement &$wrapper, $setting, $label_description, $help = null)
{
$order = $this->get('sortorder');
$name = "fields[$order][$setting]";
$label = Widget::Checkbox($name, $this->get($setting), $label_description, $wrapper, $help);
$label->addClass('column');
return $label;
}
/**
* Append the default status footer to the field settings panel.
* Displays the required and show column checkboxes.
*
* @param XMLElement $wrapper
* the parent XML element to append the checkbox to.
* @throws InvalidArgumentException
*/
public function appendStatusFooter(XMLElement &$wrapper)
{
$fieldset = new XMLElement('fieldset');
$div = new XMLElement('div', null, array('class' => 'two columns'));
$this->appendRequiredCheckbox($div);
$this->appendShowColumnCheckbox($div);
$fieldset->appendChild($div);
$wrapper->appendChild($fieldset);
}
/**
* Check the field's settings to ensure they are valid on the section
* editor
*
* @param array $errors
* the array to populate with the errors found.
* @param boolean $checkForDuplicates (optional)
* if set to true, duplicate Field name's in the same section will be flagged
* as errors. Defaults to true.
* @return integer
* returns the status of the checking. if errors has been populated with
* any errors `self::__ERROR__`, `self::__OK__` otherwise.
*/
public function checkFields(array &$errors, $checkForDuplicates = true)
{
$parent_section = $this->get('parent_section');
$label = $this->get('label');
$element_name = $this->get('element_name');
if (Lang::isUnicodeCompiled()) {
$valid_name = preg_match('/^[\p{L}]([0-9\p{L}\.\-\_]+)?$/u', $element_name);
} else {
$valid_name = preg_match('/^[A-z]([\w\d-_\.]+)?$/i', $element_name);
}
if ($label === '') {
$errors['label'] = __('This is a required field.');
} elseif (strtolower($label) === 'id') {
$errors['label'] = __('%s is a reserved name used by the system and is not allowed for a field handle. Try using %s instead.', array('<code>ID</code>', '<code>UID</code>'));
}
if ($element_name === '') {
$errors['element_name'] = __('This is a required field.');
} elseif ($element_name === 'id') {
$errors['element_name'] = __('%s is a reserved name used by the system and is not allowed for a field handle. Try using %s instead.', array('<code>id</code>', '<code>uid</code>'));
} elseif (!$valid_name) {
$errors['element_name'] = __('Invalid element name. Must be valid %s.', array('<code>QName</code>'));
} elseif ($checkForDuplicates) {
if (FieldManager::fetchFieldIDFromElementName($element_name, $parent_section) !== $this->get('id')) {
$errors['element_name'] = __('A field with that element name already exists. Please choose another.');
}
}
// Check that if the validator is provided that it's a valid regular expression
if (!is_null($this->get('validator')) && $this->get('validator') !== '') {
if (@preg_match($this->get('validator'), 'teststring') === false) {
$errors['validator'] = __('Validation rule is not a valid regular expression');
}
}
return (!empty($errors) ? self::__ERROR__ : self::__OK__);
}
/**
* Format this field value for display in the publish index tables.
*
* Since Symphony 2.5.0, this function will call `Field::prepareReadableValue`
* in order to get the field's human readable value.
*
* @param array $data
* an associative array of data for this string. At minimum this requires a
* key of 'value'.
* @param XMLElement $link (optional)
* an XML link structure to append the content of this to provided it is not
* null. it defaults to null.
* @param integer $entry_id (optional)
* An option entry ID for more intelligent processing. defaults to null
* @return string
* the formatted string summary of the values of this field instance.
*/
public function prepareTableValue($data, XMLElement $link = null, $entry_id = null)
{
$value = $this->prepareReadableValue($data, $entry_id, true, __('None'));
if ($link) {
$link->setValue($value);
return $link->generate();
}
return $value;
}
/**
* Format this field value for display as readable text value. By default, it
* will call `Field::prepareTextValue` to get the raw text value of this field.
*
* If $truncate is set to true, Symphony will truncate the value to the
* configuration setting `cell_truncation_length`.
*
* @since Symphony 2.5.0
* @param array $data
* an associative array of data for this string. At minimum this requires a
* key of 'value'.
* @param integer $entry_id (optional)
* An option entry ID for more intelligent processing. Defaults to null.
* @param string $defaultValue (optional)
* The value to use when no plain text representation of the field's data
* can be made. Defaults to null.
* @return string
* the readable text summary of the values of this field instance.
*/
public function prepareReadableValue($data, $entry_id = null, $truncate = false, $defaultValue = null)
{
$value = $this->prepareTextValue($data, $entry_id);
if ($truncate) {
$max_length = Symphony::Configuration()->get('cell_truncation_length', 'symphony');
$max_length = ($max_length ? $max_length : 75);
$value = (General::strlen($value) <= $max_length ? $value : General::substr($value, 0, $max_length) . '…');
}