This repository has been archived by the owner on Dec 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathglz_custom_fields_code.php
executable file
·457 lines (393 loc) · 19.7 KB
/
glz_custom_fields_code.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
<?php
// Including helper files. If we can't have classes, we will use includes
require_once('lib/gTxt.php');
require_once('lib/db.php');
require_once('lib/helpers.php');
require_once('lib/callbacks.php');
global $event;
// globals, expensive operations mostly
before_glz_custom_fields();
if (@txpinterface == "admin") {
// INSTALL ROUTINES
// checks if all tables exist and everything is setup properly
add_privs('glz_custom_fields_install', "1");
register_callback("glz_custom_fields_install", "plugin_lifecycle.glz_custom_fields", "installed");
// we'll be doing this only on the pages that we care about, not everywhere
if ( in_array($event, array("article", "prefs", "glz_custom_fields", "plugin_prefs.glz_custom_fields")) ) {
// we need some stylesheets & JS
add_privs('glz_custom_fields_css_js', "1,2,3,4,5,6");
register_callback('glz_custom_fields_css_js', "admin_side", 'head_end');
// we need to make sure that all custom field values will be converted to strings first - think checkboxes & multi-selects etc.
if ( (gps("step") == "edit") || (gps("step") == "create") ) {
add_privs('glz_custom_fields_before_save', "1,2,3,4,5,6");
register_callback('glz_custom_fields_before_save', "article", '', 1);
}
}
// Custom Fields tab under Extensions
add_privs('glz_custom_fields', "1,2");
register_tab("extensions", 'glz_custom_fields', "Custom Fields");
register_callback('glz_custom_fields', "glz_custom_fields");
// plugin preferences
add_privs('plugin_prefs.glz_custom_fields', "1,2");
register_callback('glz_custom_fields_preferences', 'plugin_prefs.glz_custom_fields');
// YES, finally the default custom fields are replaced by the new, pimped ones : )
add_privs('glz_custom_fields_replace', "1,2,3,4,5,6");
register_callback('glz_custom_fields_replace', 'article_ui', 'custom_fields');
// YES, now we have textarea custom fields as well ; )
register_callback('glz_custom_fields_replace', 'article_ui', 'body');
}
// -------------------------------------------------------------
// everything is happening in this function... generates the content for Extensions > Custom Fields
function glz_custom_fields() {
global $event, $all_custom_sets, $glz_notice, $prefs;
// we have $_POST, let's see if there is any CRUD
if ( $_POST ) {
$incoming = stripPost();
// DEBUG
// die(dmp($incoming));
extract($incoming);
// create an empty $value if it's not set in the $_POST
if ( !isset($value) )
$value = '';
// we are deleting a new custom field
if ( gps('delete') ) {
glz_custom_fields_MySQL("delete", $custom_set, PFX."txp_prefs");
glz_custom_fields_MySQL("delete", $custom_set, PFX."txp_lang");
glz_custom_fields_MySQL("delete", $custom_set, PFX."custom_fields");
glz_custom_fields_MySQL("delete", glz_custom_number($custom_set), PFX."textpattern");
$glz_notice[] = glz_custom_fields_gTxt("deleted", array('{custom_set_name}' => $custom_set_name));
}
// we are resetting one of the mighty 10
if ( gps('reset') ) {
glz_custom_fields_MySQL("reset", $custom_set, PFX."txp_prefs");
glz_custom_fields_MySQL("delete", $custom_set, PFX."custom_fields");
glz_custom_fields_MySQL("reset", glz_custom_number($custom_set), PFX."textpattern", array(
'custom_set_type' => $custom_set_type,
'custom_field' => glz_custom_number($custom_set)
));
$glz_notice[] = glz_custom_fields_gTxt("reset", array('{custom_set_name}' => $custom_set_name));
}
// we are adding a new custom field
if ( gps("custom_field_number") ) {
$custom_set_name = gps("custom_set_name");
// if no name was specified, abort
if ( !$custom_set_name )
$glz_notice[] = glz_custom_fields_gTxt("no_name");
else {
$custom_set_name = glz_clean_string($custom_set_name);
$name_exists = glz_check_custom_set_name($all_custom_sets, $custom_set_name);
// if name doesn't exist
if ( $name_exists == FALSE ) {
glz_custom_fields_MySQL("new", $custom_set_name, PFX."txp_prefs", array(
'custom_field_number' => $custom_field_number,
'custom_set_type' => $custom_set_type,
'custom_set_position' => $custom_set_position
));
glz_custom_fields_MySQL("new", $custom_set_name, PFX."txp_lang", array(
'custom_field_number' => $custom_field_number,
'lang' => $GLOBALS['prefs']['language']
));
glz_custom_fields_MySQL("new", $custom_set_name, PFX."textpattern", array(
'custom_field_number' => $custom_field_number,
'custom_set_type' => $custom_set_type
));
// there are custom fields for which we do not need to touch custom_fields table
if ( !in_array($custom_set_type, array("textarea", "text_input")) ) {
glz_custom_fields_MySQL("new", $custom_set_name, PFX."custom_fields", array(
'custom_field_number' => $custom_field_number,
'value' => $value
));
}
$glz_notice[] = glz_custom_fields_gTxt("created", array('{custom_set_name}' => $custom_set_name));
}
// name exists, abort
else
$glz_notice[] = glz_custom_fields_gTxt("exists", array('{custom_set_name}' => $custom_set_name));
}
}
// we are editing an existing custom field
if ( gps('save') ) {
if ( !empty($custom_set_name) ) {
$custom_set_name = glz_clean_string($custom_set_name);
$name_exists = glz_check_custom_set_name($all_custom_sets, $custom_set_name, $custom_set);
// if name doesn't exist we'll need to create a new custom_set
if ( $name_exists == FALSE ) {
glz_custom_fields_MySQL("update", $custom_set, PFX."txp_prefs", array(
'custom_set_name' => $custom_set_name,
'custom_set_type' => $custom_set_type,
'custom_set_position' => $custom_set_position
));
// custom sets need to be changed based on their type
glz_custom_fields_MySQL("update", $custom_set, PFX."textpattern", array(
'custom_set_type' => $custom_set_type,
'custom_field' => glz_custom_number($custom_set)
));
// for textareas we do not need to touch custom_fields table
if ( $custom_set_type != "textarea" ) {
glz_custom_fields_MySQL("delete", $custom_set, PFX."custom_fields");
glz_custom_fields_MySQL("new", $custom_set_name, PFX."custom_fields", array(
'custom_set' => $custom_set,
'value' => $value
));
}
$glz_notice[] = glz_custom_fields_gTxt("updated", array('{custom_set_name}' => $custom_set_name));
}
// name exists, abort
else
$glz_notice[] = glz_custom_fields_gTxt("exists", array('{custom_set_name}' => $custom_set_name));
}
else
$glz_notice[] = glz_custom_fields_gTxt('no_name');
}
// need to re-fetch data since things modified
$all_custom_sets = glz_custom_fields_MySQL("all");
}
pagetop("Custom Fields");
// the table with all custom fields follows
echo
n.'<div class="listtables">'.n.
' <table class="txp-list glz_custom_fields">'.n.
' <thead>'.n.
' <tr>'.n.
' <th>Position</th>'.n.
' <th>Name</th>'.n.
' <th>Type</th>'.n.
' <th> </th>'.n.
' </tr>'.n.
' </thead>'.n.
' <tbody>'.n;
// looping through all our custom fields to build the table
$i = 0;
foreach ( $all_custom_sets as $custom => $custom_set ) {
// first 10 fields cannot be deleted, just reset
if ( $i < 10 ) {
// can't reset a custom field that is not set
$reset_delete = ( $custom_set['name'] ) ?
glz_form_buttons("reset", "Reset", $custom, htmlspecialchars($custom_set['name']), $custom_set['type'], '', 'return confirm(\'By proceeding you will RESET ALL data in `textpattern` and `custom_fields` tables for `'.$custom.'`. Are you sure?\');') :
NULL;
}
else {
$reset_delete = glz_form_buttons("delete", "Delete", $custom, htmlspecialchars($custom_set['name']), $custom_set['type'], '', 'return confirm(\'By proceeding you will DELETE ALL data in `textpattern` and `custom_fields` tables for `'.$custom.'`. Are you sure?\');');
}
$edit = glz_form_buttons("edit", "Edit", $custom, htmlspecialchars($custom_set['name']), $custom_set['type'], $custom_set['position']);
echo
' <tr>'.n.
' <td class="custom_set_position">'.$custom_set['position'].'</td>'.n.
' <td class="custom_set_name">'.$custom_set['name'].'</td>'.n.
' <td class="type">'.(($custom_set['name']) ? glz_custom_fields_gTxt($custom_set['type']) : '').'</td>'.n.
' <td class="events">'.$reset_delete.sp.$edit.'</td>'.n.
' </tr>'.n;
$i++;
}
echo
' </tbody>'.n.
' </table>'.n;
'</div>'.n;
// the form where custom fields are being added/edited
$legend = gps('edit') ?
'Edit '.gps('custom_set') :
'Add new custom field';
$custom_field = gps('edit') ?
'<input name="custom_set" value="'.gps('custom_set').'" type="hidden" />' :
'<input name="custom_field_number" value="'.glz_custom_next($all_custom_sets).'" type="hidden" />';
$custom_set = gps('edit') ?
gps('custom_set') :
NULL;
$custom_name = gps('edit') ?
gps('custom_set_name') :
NULL;
$custom_set_position = gps('edit') ?
gps('custom_set_position') :
NULL;
$arr_custom_set_types = glz_custom_set_types();
$custom_set_types = NULL;
foreach ( $arr_custom_set_types as $custom_type_group => $custom_types ) {
$custom_set_types .= '<optgroup label="'.ucfirst($custom_type_group).'">'.n;
foreach ($custom_types as $custom_type) {
$selected = ( gps('edit') && gps('custom_set_type') == $custom_type ) ?
' selected="selected"' :
NULL;
$custom_set_types .= '<option value="'.$custom_type.'"'.$selected.'>'.glz_custom_fields_gTxt($custom_type).'</option>'.n;
}
$custom_set_types .= '</optgroup>'.n;
}
// fetching the values for this custom field
if ( gps('edit') ) {
if ( $custom_set_type == "text_input" )
$arr_values = glz_custom_fields_MySQL('all_values', glz_custom_number($custom_set), '', array('custom_set_name' => $custom_set_name, 'status' => 4));
else
$arr_values = glz_custom_fields_MySQL("values", $custom_set, '', array('custom_set_name' => $custom_set_name));
$values = ( $arr_values ) ?
implode("\r\n", $arr_values) :
'';
}
else
$values = '';
$action = gps('edit') ?
'<input name="save" value="Save" type="submit" class="submit" />' :
'<input name="add_new" value="Add new" type="submit" class="submit" />';
// this needs to be different for a script
$value = ( isset($custom_set_type) && $custom_set_type == "custom-script" ) ?
'<input type="text" name="value" id="value" value="'.$values.'" class="left"/><span class="right"><em>Relative path from your website\'s public folder</em></span>' :
'<textarea name="value" id="value" class="left">'.$values.'</textarea><span class="right"><em>Each value on a separate line</em> <br /><em>One {default} value allowed</em></span>';
// ok, all is set, let's build the form
echo
'<form method="post" action="index.php" id="add_edit_custom_field">'.n.
'<input name="event" value="glz_custom_fields" type="hidden" />'.n.
$custom_field.n.
'<fieldset>'.n.
' <legend>'.$legend.'</legend>'.n.
' <p class="clearfix">
<label for="custom_set_name" class="left">Name:</label>
<input type="text" name="custom_set_name" value="'.htmlspecialchars($custom_name).'" id="custom_set_name" class="left" />
<span class="right"><em>Only word characters allowed</em></span>
</p>'.n.
' <p class="clearfix">
<label for="custom_set_type" class="left">Type:</label>
<select name="custom_set_type" id="custom_set_type" class="left">
'. $custom_set_types.'
</select>
</p>'.n.
' <p class="clearfix">
<label for="custom_set_position" class="left">Position:</label>
<input type="text" name="custom_set_position" value="'.htmlspecialchars($custom_set_position).'" id="custom_set_position" class="left" />
<span class="right"><em>Automatically assigned if blank</em></span>
</p>'.n.
' <p class="clearfix">
<label for="value" class="left">Value:</label>
'. $value.'
</p>'.n.
' '.$action.n.
'</fieldset>'.n.
'</form>'.n;
}
// -------------------------------------------------------------
// glz_custom_fields preferences
function glz_custom_fields_preferences() {
global $event, $glz_notice;
if ( $_POST && gps('save') ) {
glz_custom_fields_MySQL("update_plugin_preferences", $_POST['glz_custom_fields_prefs']);
$glz_notice[] = glz_custom_fields_gTxt("preferences_updated");
// need to re-fetch from db because this has changed since $prefs has been populated
}
$current_preferences = glz_custom_fields_MySQL('plugin_preferences');
pagetop("glz_custom_fields Preferences");
// custom_fields
$arr_values_ordering = array(
'ascending' => "Ascending",
'descending' => "Descending",
'custom' => "As entered"
);
$values_ordering = '<select name="glz_custom_fields_prefs[values_ordering]" id="glz_custom_fields_prefs_values_ordering">';
foreach ( $arr_values_ordering as $value => $title ) {
$selected = ($current_preferences['values_ordering'] == $value) ? ' selected="selected"' : '';
$values_ordering .= "<option value=\"$value\"$selected>$title</option>";
}
$values_ordering .= "</select>";
$multiselect_size = '<input type="text" name="glz_custom_fields_prefs[multiselect_size]" id="glz_custom_fields_prefs_multiselect_size" value="'.$current_preferences['multiselect_size'].'" />';
$custom_scripts_path_error = ( @fopen($current_preferences['custom_scripts_path'], "r") ) ?
'' :
'<br /><em class="red">Folder does not exist, please create it.</em>';
// jquery.datePicker
$datepicker_url_error = ( @fopen($current_preferences['datepicker_url']."/datePicker.js", "r") ) ?
'' :
'<br /><em class="red">Folder does not exist, please create it.</em>';
$arr_date_format = array("dd/mm/yyyy", "mm/dd/yyyy", "yyyy-mm-dd", "dd mm yy");
$date_format = '<select name="glz_custom_fields_prefs[datepicker_format]" id="glz_custom_fields_prefs_datepicker_format">';
foreach ( $arr_date_format as $format ) {
$selected = ($current_preferences['datepicker_format'] == $format) ? ' selected="selected"' : '';
$date_format .= "<option value=\"$format\"$selected>$format</option>";
}
$date_format .= "</select>";
$arr_days = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
$first_day = '<select name="glz_custom_fields_prefs[datepicker_first_day]" id="glz_custom_fields_prefs_datepicker_first_day">';
foreach ( $arr_days as $key => $day ) {
$selected = ($current_preferences['datepicker_first_day'] == $key) ? ' selected="selected"' : '';
$first_day .= "<option value=\"$key\"$selected>$day</option>";
}
$first_day .= "</select>";
$start_date = '<input type="text" name="glz_custom_fields_prefs[datepicker_start_date]" id="glz_custom_fields_prefs_datepicker_start_date" value="'.$current_preferences['datepicker_start_date'].'" />';
// jquery.timePicker
$timepicker_url_error = ( @fopen($current_preferences['timepicker_url']."/timePicker.js", "r") ) ?
'' :
'<br /><em class="red">Folder does not exist, please create it.</em>';
$arr_time_format = array('true' => "24 hours", 'false' => "12 hours");
$show_24 = '<select name="glz_custom_fields_prefs[timepicker_show_24]" id="glz_custom_fields_prefs_timepicker_show_24">';
foreach ( $arr_time_format as $value => $title ) {
$selected = ($current_preferences['timepicker_show_24'] == $value) ? ' selected="selected"' : '';
$show_24 .= "<option value=\"$value\"$selected>$title</option>";
}
$show_24 .= "</select>";
$out = <<<EOF
<form action="index.php" method="post">
<table id="list" class="glz_custom_fields_prefs" cellpadding="0" cellspacing="0" align="center">
<tbody>
<tr class="heading">
<td colspan="2"><h2 class="pref-heading">Custom Fields</h2></td>
</tr>
<tr>
<th scope="row"><label for="glz_custom_fields_prefs_values_ordering">Order for custom field values</th>
<td>{$values_ordering}</td>
</tr>
<tr>
<th scope="row"><label for="glz_custom_fields_prefs_multiselect_size">Multi-select field size</th>
<td>{$multiselect_size}</td>
</tr>
<tr>
<th scope="row"><label for="glz_custom_fields_prefs_custom_scripts_path">Custom scripts path</th>
<td><input type="text" name="glz_custom_fields_prefs[custom_scripts_path]" id="glz_custom_fields_prefs_custom_scripts_path" value="{$current_preferences['custom_scripts_path']}" />{$custom_scripts_path_error}</td>
</tr>
<tr class="heading">
<td colspan="2"><h2 class="pref-heading left">Date Picker</h2> <a href="http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/index.html" title="A flexible unobtrusive calendar component for jQuery" class="right">jQuery datePicker</a></td>
</tr>
<tr>
<th scope="row"><label for="glz_custom_fields_prefs_datepicker_url">Date Picker plugin URL</th>
<td><input type="text" name="glz_custom_fields_prefs[datepicker_url]" id="glz_custom_fields_prefs_datepicker_url" value="{$current_preferences['datepicker_url']}" />{$datepicker_url_error}</td>
</tr>
<tr>
<th scope="row"><label for="glz_custom_fields_prefs_datepicker_format">Date format</th>
<td>{$date_format}</td>
</tr>
<tr>
<th scope="row"><label for="glz_custom_fields_prefs_datepicker_first_day">First day of week</th>
<td>{$first_day}</td>
</tr>
<tr>
<th scope="row"><label for="glz_custom_fields_prefs_datepicker_start_date">Start date</th>
<td>{$start_date}<br /><em class="grey">MUST be the same as "Date format"</em></td>
</tr>
<tr class="heading">
<td colspan="2"><h2 class="pref-heading left">Time Picker</h2> <a href="http://labs.perifer.se/timedatepicker/" title="jQuery time picker" class="right">jQuery timePicker</a></td>
</tr>
<tr>
<th scope="row"><label for="glz_custom_fields_prefs_timepicker_url">Time Picker plugin URL</th>
<td><input type="text" name="glz_custom_fields_prefs[timepicker_url]" id="glz_custom_fields_prefs_timepicker_url" value="{$current_preferences['timepicker_url']}" />{$timepicker_url_error}</td>
</tr>
<tr>
<th scope="row"><label for="glz_custom_fields_prefs_timepicker_start_time">Start time</th>
<td><input type="text" name="glz_custom_fields_prefs[timepicker_start_time]" id="glz_custom_fields_prefs_timepicker_start_time" value="{$current_preferences['timepicker_start_time']}" /></td>
</tr>
<tr>
<th scope="row"><label for="glz_custom_fields_prefs_timepicker_end_time">End time</th>
<td><input type="text" name="glz_custom_fields_prefs[timepicker_end_time]" id="glz_custom_fields_prefs_timepicker_end_time" value="{$current_preferences['timepicker_end_time']}" /></td>
</tr>
<tr>
<th scope="row"><label for="glz_custom_fields_prefs_timepicker_step">Step</th>
<td><input type="text" name="glz_custom_fields_prefs[timepicker_step]" id="glz_custom_fields_prefs_timepicker_step" value="{$current_preferences['timepicker_step']}" /></td>
</tr>
<tr>
<th scope="row"><label for="glz_custom_fields_prefs_timepicker_step">Time format</th>
<td>{$show_24}</td>
</tr>
<tr>
<td colspan="2" class="noline">
<input class="publish" type="submit" name="save" value="Save" />
<input type="hidden" name="event" value="plugin_prefs.glz_custom_fields" />
</td>
</tr>
</tbody>
</table>
EOF;
echo $out;
}
?>