1 | <?php |
||
14 | class Moo_EditableField extends DataObject |
||
15 | { |
||
16 | private static $db = [ |
||
17 | 'Name' => 'Varchar', |
||
18 | 'Title' => 'Varchar(255)', |
||
19 | 'Required' => 'Boolean', |
||
20 | 'CustomErrorMessage' => 'Varchar(255)', |
||
21 | 'CustomSettings' => 'Text', |
||
22 | ]; |
||
23 | private static $singular_name = 'Editable Field'; |
||
24 | private static $plural_name = 'Editable Fields'; |
||
25 | |||
26 | protected $customSettingsFields = []; |
||
27 | /** |
||
28 | * Instance of FormField. |
||
29 | * |
||
30 | * @var FormField |
||
31 | */ |
||
32 | protected $field; |
||
33 | |||
34 | /** |
||
35 | * To prevent having tables for each fields minor settings we store it as |
||
36 | * a serialized array in the database. |
||
37 | * |
||
38 | * @return array Return all the Settings |
||
39 | */ |
||
40 | 8 | public function getSettings() |
|
41 | 1 | { |
|
42 | 8 | return (!empty($this->CustomSettings)) ? unserialize($this->CustomSettings) : []; |
|
43 | } |
||
44 | |||
45 | /** |
||
46 | * Set the custom settings for this field as we store the minor details in |
||
47 | * a serialized array in the database. |
||
48 | * |
||
49 | * @param array $settings the custom settings |
||
50 | */ |
||
51 | 8 | public function setSettings($settings = []) |
|
55 | |||
56 | /** |
||
57 | * Set a given field setting. Appends the option to the settings or overrides |
||
58 | * the existing value. |
||
59 | * |
||
60 | * @param string $key |
||
61 | * @param string $value |
||
62 | */ |
||
63 | 1 | public function setSetting($key, $value) |
|
64 | { |
||
65 | 1 | $settings = $this->getSettings(); |
|
66 | 1 | $settings[$key] = $value; |
|
67 | |||
68 | 1 | $this->setSettings($settings); |
|
69 | 1 | } |
|
70 | |||
71 | /** |
||
72 | * Return just one custom setting or empty string if it does |
||
73 | * not exist. |
||
74 | * |
||
75 | * @param string $setting |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | 3 | public function getSetting($setting) |
|
80 | { |
||
81 | 3 | $settings = $this->getSettings(); |
|
82 | 3 | if (isset($settings) && count($settings) > 0) { |
|
83 | 2 | if (isset($settings[$setting])) { |
|
84 | 2 | return $settings[$setting]; |
|
85 | } |
||
86 | } |
||
87 | |||
88 | 3 | return ''; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Get the path to the icon for this field type, relative to the site root. |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | 1 | public function getIcon() |
|
97 | { |
||
98 | 1 | return 'editablefield/images/formfields/' . strtolower(substr($this->class, 4)) . '.png'; |
|
99 | } |
||
100 | |||
101 | 1 | public function getIconTag() |
|
102 | { |
||
103 | 1 | return '<img src="' . $this->getIcon() . '"/>'; |
|
104 | } |
||
105 | |||
106 | 1 | public function getCMSFields() |
|
107 | { |
||
108 | 1 | $fields = parent::getCMSFields(); |
|
109 | |||
110 | // Remove field to be recreated in separate tabs |
||
111 | 1 | $fields->removeByName([ |
|
112 | 1 | 'Required', 'CustomErrorMessage', 'CustomSettings', 'Options', |
|
113 | 1 | ]); |
|
114 | |||
115 | //Implement custom field Configuration on this field. Includes such things as |
||
116 | // * settings and options of a given editable form field. |
||
117 | |||
118 | 1 | $fields->addFieldsToTab('Root.FieldConfiguration', [ |
|
119 | 1 | new TextField( |
|
120 | 1 | $this->getSettingName('RightTitle'), _t('Moo_EditableField.RIGHTTITLE', 'Right Title'), |
|
121 | 1 | $this->getSetting('RightTitle') |
|
122 | 1 | ), |
|
123 | 1 | ]); |
|
124 | |||
125 | $validateFields = [ |
||
126 | 1 | new CheckboxField('Required', _t('Moo_EditableField.REQUIRED', 'Is this field Required?'), |
|
127 | 1 | $this->Required), new TextField('CustomErrorMessage', |
|
128 | 1 | _t('Moo_EditableField.CUSTOMERROR', 'Custom Error Message'), |
|
129 | 1 | $this->CustomErrorMessage), |
|
130 | 1 | ]; |
|
131 | 1 | $fields->addFieldsToTab('Root.Validation', $validateFields); |
|
132 | |||
133 | 1 | if (method_exists($this, 'getFieldConfiguration')) { |
|
134 | 1 | $v = $this->getFieldConfiguration(); |
|
|
|||
135 | 1 | $fields->addFieldsToTab('Root.FieldConfiguration', $v); |
|
136 | 1 | } |
|
137 | |||
138 | 1 | if (method_exists($this, 'getFieldValidationOptions')) { |
|
139 | $v = $this->getFieldValidationOptions(); |
||
140 | if (is_array($v)) { |
||
141 | $fields->addFieldsToTab('Root.Validation', $v); |
||
142 | } |
||
143 | } |
||
144 | |||
145 | 1 | return $fields; |
|
146 | } |
||
147 | |||
148 | 1 | public function getCMSValidator() |
|
149 | { |
||
150 | 1 | return new RequiredFields( |
|
151 | 1 | 'Title', 'Name' |
|
152 | 1 | ); |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * Returns the Title for rendering in the front-end (with XML values escaped). |
||
157 | * |
||
158 | * @return string |
||
159 | */ |
||
160 | 8 | public function getTitle() |
|
164 | |||
165 | /** |
||
166 | * Generate a name for the Setting field. |
||
167 | * |
||
168 | * @param string $field |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | 1 | public function getSettingName($field) |
|
176 | |||
177 | /** |
||
178 | * How to save the data submitted in this field into the database object |
||
179 | * which this field represents. |
||
180 | * |
||
181 | * Any class's which call this should also call |
||
182 | * {@link parent::populateFromPostData()} to ensure that this method is |
||
183 | * called |
||
184 | * |
||
185 | * @param array $data |
||
186 | * |
||
187 | * @throws ValidationException |
||
188 | */ |
||
189 | 8 | public function onBeforeWrite() |
|
212 | |||
213 | /** |
||
214 | * Return a FormField. |
||
215 | * |
||
216 | * @return FormField |
||
217 | */ |
||
218 | 1 | public function getFormField() |
|
226 | |||
227 | /** |
||
228 | * Initiate a form field. |
||
229 | * |
||
230 | * @return FormField |
||
231 | */ |
||
232 | protected function initFormField() |
||
236 | |||
237 | /** |
||
238 | * Return the error message for this field. Either uses the custom |
||
239 | * one (if provided) or the default SilverStripe message. |
||
240 | * |
||
241 | * @return Varchar |
||
242 | */ |
||
243 | public function getErrorMessage() |
||
253 | |||
254 | 1 | public function onBeforeDuplicate(Moo_EditableField $field) |
|
258 | } |
||
259 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.