-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft.quill.php
405 lines (344 loc) · 12.6 KB
/
ft.quill.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
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Quill Fieldtype
*/
class Quill_ft extends EE_Fieldtype
{
public $info = array();
public $has_array_data = true;
private $default_settings = array(
'theme' => 'snow',
'placeholder' => '',
'field_wide' => true
);
private $default_column = array(
'type' => 'MEDIUMTEXT'
);
/**
* Constructor
*/
public function __construct()
{
ee()->load->helper('form');
$addon = ee('Addon')->get('quill');
$this->info = array(
'name' => $addon->getName(),
'version' => $addon->getVersion()
);
}
/**
* Accepts Content Type
*
* {@inheritdoc}
* @see EE_Fieldtype::accepts_content_type()
*/
public function accepts_content_type($name)
{
// return ($name == 'channel' || $name == 'grid' || $name == 'fluid_field');
return ($name == 'channel' || $name == 'grid');
}
public function display_field($data)
{
$value = array();
if(! empty($data)) {
$value['text'] = $data;
}
if( (! is_null($this->content_id) && ! isset($this->settings['grid_field_id'])) || (isset($this->settings['grid_row_id']) && isset($this->settings['grid_row_id']))) {
$select = isset($this->settings['grid_field_id']) ? "col_ops_{$this->settings['col_id']}" : "field_ops_{$this->field_id}";
$where = isset($this->settings['grid_field_id']) ? ['row_id' => $this->settings['grid_row_id']] : ['entry_id' => $this->content_id];
$table = isset($this->settings['grid_field_id']) ? "channel_grid_field_{$this->settings['grid_field_id']}" : "channel_data_field_{$this->field_id}";
$query = ee('db');
$query->select($select);
$query->where($where);
$query->limit(1);
$result = $query->get($table);
if($result->num_rows() > 0) {
$ops = $result->row()->{$select};
if(! is_null($ops)) {
$value['ops'] = $ops;
}
}
}
$value = (! empty($value)) ? json_encode($value) : null;
if(REQ == 'CP') {
$this->_cp_resources();
}
return form_input(array(
'name' => $this->field_name,
'value' => $value,
'class' => 'quill-input'
));
}
/**
* Save
*
* Preps the data for saving
*
* @param $data string
* Current field data, blank for new entries
* @return string Data to save to the database
*
* {@inheritdoc}
* @see EE_Fieldtype::save()
*/
public function save($data)
{
if(empty($data)) {
return null;
}
$data = json_decode($data);
if(empty($data)) {
return null;
}
$cache_name = "quill_field_{$this->field_id}";
if (isset($this->settings['grid_field_id'])) {
$cache_name .= "_grid_" . $this->settings['grid_field_id'];
$cache_name .= "_col_" . $this->settings['col_id'];
$cache_name .= "_row_" . $this->settings['grid_row_name'];
}
ee()->session->set_cache(__CLASS__, $cache_name, $data);
return (isset($data->text) && $data->text !== "\n") ? $data->text : null;
}
/**
* Post Save
*
* Handles any custom logic after an entry is saved.
* Called after an entry is added or updated. Available data is identical to save. This is a good method to implement if you need the content ID of the fieldtype’s newly-saved parent content type.
*
* @param $data string
* Current field data, blank for new entries
* @return void
*
* {@inheritdoc}
* @see EE_Fieldtype::post_save()
*/
public function post_save($data)
{
// Prevent saving if save() was never called, happens in Channel Form
// if the field is missing from the form
$cache_name = "quill_field_{$this->field_id}";
if (isset($this->settings['grid_field_id'])) {
$cache_name .= "_grid_" . $this->settings['grid_field_id'];
$cache_name .= "_col_" . $this->settings['col_id'];
$cache_name .= "_row_" . $this->settings['grid_row_name'];
}
if (($data = ee()->session->cache(__CLASS__, $cache_name, null)) === null) {
return;
}
$col_name = (isset($this->settings['grid_field_id'])) ? "col_ops_{$this->settings['col_id']}" : "field_ops_{$this->field_id}";
$where = (isset($this->settings['grid_field_id'])) ? ['row_id' => $this->settings['grid_row_id']] : ['entry_id' => $this->content_id];
$table = (isset($this->settings['grid_field_id'])) ? "channel_grid_field_{$this->settings['grid_field_id']}" : "channel_data_field_{$this->field_id}";
$ops = (isset($data->ops)) ? $data->ops : null;
$ops = (gettype($ops) == 'array') ? json_encode($ops) : $ops;
$values = array(
$col_name => $ops
);
$result = ee()->db->get_where($table, $where, 1);
if ($result->num_rows() < 1) {
ee()->db->insert($table, array_merge($where, $values));
return;
}
$query = ee('db');
$query->set($values);
$query->where($where);
$query->update($table);
}
/**
* Validate
*
* Validates the field input
*
* @param $data array|string Current field data, blank for new entries
* @return boolean|string TRUE if the field validates, an error message otherwise
*
* {@inheritdoc}
* @see EE_Fieldtype::validate()
*/
public function validate($data)
{
return true;
}
public function replace_tag($data, $params = array(), $tagdata = false)
{
$text = $data;
$ops = null;
$settings = array_merge($this->default_settings, $this->settings);
if( (! is_null($this->content_id) && ! isset($this->settings['grid_field_id'])) || (isset($this->settings['grid_row_id']) && isset($this->settings['grid_row_id']))) {
$select = isset($this->settings['grid_field_id']) ? "col_ops_{$this->settings['col_id']}" : "field_ops_{$this->field_id}";
$where = isset($this->settings['grid_field_id']) ? ['row_id' => $this->settings['grid_row_id']] : ['entry_id' => $this->content_id];
$table = isset($this->settings['grid_field_id']) ? "channel_grid_field_{$this->settings['grid_field_id']}" : "channel_data_field_{$this->field_id}";
$query = ee('db');
$query->select($select);
$query->where($where);
$query->limit(1);
$result = $query->get($table);
if($result->num_rows() > 0) {
$ops = $result->row()->{$select};
}
}
$params = array_merge([
'data' => 'text', // 'text' or 'ops',
'encode_ops' => 'none', // 'none' or 'base64'
], $params);
if ($params['encode_ops'] == 'base64') {
$ops = base64_encode($ops);
}
if ($tagdata !== false) {
$vars = [
'text' => $text,
'ops' => $ops,
'ops:base64' => base64_encode($ops),
'theme' => $settings['theme'],
'placeholder' => $settings['placeholder'],
];
return ee()->TMPL->parse_variables($tagdata, [$vars]);
}
$output = $text;
if ($params['data'] == 'ops') {
$output = $ops;
}
// $output = ee('Format')->make('Text', $output)->attributeSafe();
return $output;
}
/**
* Installation
*
* By returning an array from within install we can provide a default set of global settings.
*
* {@inheritdoc}
* @see EE_Fieldtype::install()
*/
public function install()
{
return $this->default_settings;
}
/**
* Settings Modify Colum
*
* Allows the specification of an array of fields to be added, modified or dropped when fields are created, edited or deleted.
*
* {@inheritdoc}
* @see EE_Fieldtype::settings_modify_column()
*/
public function settings_modify_column($data)
{
$id = $data['field_id'];
return array(
"field_id_{$id}" => $this->default_column,
"field_ops_{$id}" => $this->default_column
);
}
/**
* Grid Settings Modify Colum
*
* {@inheritdoc}
* @see EE_Fieldtype::grid_settings_modify_column()
*/
public function grid_settings_modify_column($data)
{
$id = $data['col_id'];
return array(
"col_id_{$id}" => $this->default_column,
"col_ops_{$id}" => $this->default_column
);
}
function validate_settings($data)
{
$validator = ee('Validation')->make(array(
'theme' => 'required|enum[snow,bubble]',
'placeholder' => 'maxLength[50]|noHtml|xss'
));
return $validator->validate($data);
}
/**
* Save (Individual) Settings
*
* {@inheritdoc}
* @see EE_Fieldtype::save_settings()
*/
public function save_settings($data)
{
return array(
'theme' => isset($data['theme']) ? $data['theme'] : $this->default_settings['theme'],
'placeholder' => isset($data['placeholder']) ? $data['placeholder'] : $this->default_settings['placeholder'],
'field_wide' => true
);
}
/**
* Display (Individual) Settings
*
* {@inheritdoc}
* @see EE_Fieldtype::display_settings()
*/
public function display_settings($data)
{
$values = array_merge($this->default_settings, $data);
$settings = array(
array(
'title' => 'theme',
'desc' => 'theme_desc',
'fields' => array(
'theme' => array(
'type' => 'dropdown',
'value' => $values['theme'],
'required' => true,
'choices' => array(
'snow' => 'Snow',
'bubble' => 'Bubble'
)
)
)
),
array(
'title' => 'placeholder',
'desc' => 'placeholder_desc',
'fields' => array(
'placeholder' => array(
'type' => 'text',
'value' => $values['placeholder'],
'placeholder' => 'Compose an epic...'
)
)
)
);
if ($this->content_type() == 'grid') {
return array(
'field_options' => $settings
);
}
return array(
'field_options_quill' => array(
'label' => 'field_options',
'group' => 'quill',
'settings' => $settings
)
);
}
private function _cp_resources()
{
if (! ee()->session->cache(__CLASS__, 'cp_resources_js', false)) {
ee()->cp->load_package_js('quill.min');
ee()->cp->load_package_js('main');
ee()->session->set_cache(__CLASS__, 'cp_resources_js', true);
}
if (isset($this->settings['grid_field_id']) && ! ee()->session->cache(__CLASS__, 'cp_resources_js_grid', false)) {
ee()->cp->load_package_js('grid');
ee()->session->set_cache(__CLASS__, 'cp_resources_js_grid', true);
}
if (! ee()->session->cache(__CLASS__, 'cp_resources_css_main', false)) {
ee()->cp->load_package_css('main');
ee()->session->set_cache(__CLASS__, 'cp_resources_css_main', true);
}
if (isset($this->settings['theme']) && $this->settings['theme'] == 'bubble' && !ee()->session->cache(__CLASS__, 'cp_resources_css_bubble', false)) {
ee()->cp->load_package_css('quill.bubble');
ee()->session->set_cache(__CLASS__, 'cp_resources_css_bubble', true);
}
if (isset($this->settings['theme']) && $this->settings['theme'] == 'snow' && !ee()->session->cache(__CLASS__, 'cp_resources_css_snow', false)) {
ee()->cp->load_package_css('quill.snow');
ee()->session->set_cache(__CLASS__, 'cp_resources_css_snow', true);
}
}
}
// END Quill_ft class
/* End of file ft.quill.php */
/* Location: ./system/user/addons/quill/ft.quill.php */