-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfly_cat_restriction.php
576 lines (387 loc) · 13.5 KB
/
fly_cat_restriction.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
<?php
$plugin['name'] = 'fly_cat_restriction';
$plugin['version'] = '1.1';
$plugin['author'] = 'Superfly';
$plugin['author_uri'] = 'http://www.flyweb.at';
$plugin['description'] = 'Restrict Article Categories for Sections in Write Panel';
$plugin['order'] = '5';
$plugin['type'] = '4';
if (!defined('PLUGIN_HAS_PREFS')) define('PLUGIN_HAS_PREFS', 0x0001);
if (!defined('PLUGIN_LIFECYCLE_NOTIFY')) define('PLUGIN_LIFECYCLE_NOTIFY', 0x0002);
$plugin['flags'] = '3';
# --- BEGIN PLUGIN CODE ---
/**
*
* Textpattern CMS Plugin <www.textpattern.com>
*
* fly_cat_restriction.php
* Flyweb Categories Restriction Plugin
*
* This Plugin is for the CMS Write Panel. It can deactivate Category
* Names relative to Sections Names.
*
* @author flyweb productions <www.flyweb.at>
* @copyright 2015 flyweb productions
* @license http://opensource.org/licenses/MIT - MIT License (MIT)
* @version 1.1 <https://github.com/brachycera/fw_cat_restriction>
*
*/
if (txpinterface === 'admin') {
add_privs('fly_cat_restriction','1');
add_privs('plugin_prefs.fly_cat_restriction', '1');
register_tab("extensions", "fly_cat_restriction", 'Category Restrictions');
register_callback('fly_cat_restriction', 'plugin_lifecycle.fly_cat_restriction', 'installed');
register_callback('fly_cat_restriction', 'plugin_lifecycle.fly_cat_restriction', 'deleted');
register_callback('fly_cat_restriction', 'plugin_prefs.fly_cat_restriction');
register_callback('fly_cat_restriction', 'article_ui','sort_display');
register_callback('fly_cat_restriction', 'fly_cat_restriction');
}
/**
*
* fly_cat_restriction - Plugin Factory Function
*
* @param string $event - Textpattern Event Callback Parameter
* @param string $step - Textpattern Step Callback Parameter
*
*/
function fly_cat_restriction($event, $step){
$fly_cat_restriction = new fly_cat_restrictionClass();
switch ($step) {
case 'installed':
$fly_cat_restriction->install();
break;
case 'deleted':
$fly_cat_restriction->uninstall();
break;
case 'save':
$fly_cat_restriction->adminpage();
break;
case 'savePref':
$fly_cat_restriction->savePref();
break;
case 'sort_display':
$fly_cat_restriction->javascript();
break;
default:
$fly_cat_restriction->adminpage();
break;
}
return;
}
/**
*
* fly_cat_restrictionClass
*
* This Plugin adds the ability to restrict Categories from specific Sections in the Write Tab
*
*
* @version 1.0
* @author Superfly <www.flyweb.at>
* @project Textpattern Admin Plugin
*
*/
class fly_cat_restrictionClass {
/**
*
* __construct
* Adds Theme Class
*
*/
function __construct(){
global $theme;
$this->theme = $theme;
}
/**
*
* Install Database Table txp_fly_cat_restriction
*
*/
function install() {
if (!getThings("Show tables like '" . PFX . "txp_fly_cat_restriction'")) {
//figure out what MySQL version we are using (from _update.php)
$mysqlversion = mysql_get_server_info();
$tabletype = ( intval($mysqlversion[0]) >= 5 || preg_match('#^4\.(0\.[2-9]|(1[89]))|(1\.[2-9])#', $mysqlversion))
? " ENGINE=MyISAM "
: " TYPE=MyISAM ";
if ( isset($txpcfg['dbcharset']) && (intval($mysqlversion[0]) >= 5 || preg_match('#^4\.[1-9]#', $mysqlversion)))
$tabletype .= " CHARACTER SET = ". $txpcfg['dbcharset'] ." ";
// Create the txp_fly_cat_restrictions table
$fly_cat_restrictionsTable = safe_query("CREATE TABLE `" . PFX . "txp_fly_cat_restriction` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`section` VARCHAR(128) NOT NULL,
`category` VARCHAR(64) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) $tabletype");
}
}
/**
*
* uninstall - Uninstall Database Table txp_fly_cat_restriction
*
*/
function uninstall() {
if ( getThings("Show tables like '" . PFX . "txp_fly_cat_restriction'") ) {
$sql = "DROP TABLE IF EXISTS " . PFX . "txp_fly_cat_restriction;";
$dropTable = safe_query($sql);
}
$sqlPref = "DELETE FROM " . PFX . "txp_prefs WHERE name='fly_restrict_cat_pref';";
$deletePref = safe_query($sqlPref);
}
/**
*
* Saves Categories from HTML Form to Database
*
* @param string $_POST['section']
* @param array $_POST['categories']
* @uses lib/txplib_theme.php - announce()
* @return string - Admin Theme Notification message
*
*/
function save() {
$section = gps('section');
$categories = array_filter( array_map('array_filter', gps('categories')) );
$sqlCat = '';
$sqlDelCat = '';
if ( !empty($categories) ){
foreach ($categories as $category) {
$sqlDelCat .= ( !empty($category['id']) && !empty($category['name']) ? "id != '" . $category['id'] . "' AND " : '');
$sqlCat .= ( empty($category['id']) && !empty($category['name']) ? "('', '" . $section . "', '" . $category['name'] . "'), " : '');
}
if ( !empty($sqlDelCat) )
$sqlDelCat = 'AND (' . trim($sqlDelCat, 'AND ') . ')';
$sqlDel = "DELETE FROM " . PFX . "txp_fly_cat_restriction WHERE section='" . $section . "'" . $sqlDelCat;
$delete = safe_query($sqlDel);
$sqlCat = trim($sqlCat, ', ');
if ( !empty($sqlCat) ){
$sql = "INSERT INTO " . PFX . "txp_fly_cat_restriction VALUES " . $sqlCat;
$insert = safe_query($sql);
}
return $this->theme->announce( 'Saved restricted Categroies' );
}
}
/**
*
* savePref - Save setting to show/hide restricted Categories in Write Panel
*
* @param string $_POST['fly_restrict_cat_pref']
* @uses lib/txplib_theme.php - announce_async()
* @return string - Admin Theme Javascript Notification message
*
*/
function savePref(){
if ( gps('fly_restrict_cat_pref') == 'true' ){
set_pref('fly_restrict_cat_pref', true, 'fly_cat_restriction');
echo $this->theme->announce_async( 'Restricted Categories are hidden' );
} else {
set_pref('fly_restrict_cat_pref', false, 'fly_cat_restriction');
echo $this->theme->announce_async( 'Restricted Categories are shown');
}
}
/**
*
* Loads all restrictet Catgories from the database and converts the result to JSON
*
* @return string $restriction - JSON Encoded Result: Key is Sectionname - Values are Category Name and ID
*
*/
function restrictions_JSON() {
$sql = "SELECT * FROM " . PFX . "txp_fly_cat_restriction ORDER BY section DESC";
$result = getRows($sql);
if ( $result == true ){
$i=1; // NTMS: we cant use the array key. json_encode() would make an JS-array from array key '0', and not the desired JS-object
foreach ($result as $item) {
$filterItem[ $item['section'] ][$i]['name'] = $item['category'];
$filterItem[ $item['section'] ][$i]['id'] = $item['id'];
$i++;
}
$restriction = json_encode( $filterItem );
} else
$restriction = "[{}]";
return $restriction;
}
/**
*
* adminpage - Admin Page to configure and save restrictet categories
*
* @return string HTML Code
*
*/
function adminpage() {
if ( !empty( $_POST['step'] ) == 'save' )
$saveMsg = $this->save();
pagetop( 'Article Category Restrictions' );
echo '<div class="text-column">' . "\n";
echo '<h1>Category Restrictions</h1>' . "\n";
echo '<p>This plugin only works with Article-Categories. Only parent categories can be disabled. Sub-categories get automatically disabled if Parent-Category is disabled.</p>' . "\n";
echo '<div>' . form(
'<label for="save_pref">' .
checkbox('fly_restrict_cat_pref', '" onchange="savePref(this); ', 0, null, 'save_pref') .
'Hide restricted Categories in Dropdown Menu on Write Panel</label>'
) . "\n";
echo '</div><br/>' . "\n";
echo '<style type="text/css">label {display:block; cursor:pointer;}</style>' . "\n";
echo form(
startTable('', 'left', 'txp-list', '5', '30%') .
'<thead>'. tr ( hCell('Choose a section and disable the desired categories', null) ) . '</thead>'.
tr( tda($this->sections_dropdown(), ' class="align-center"') )
. $this->categories_checkboxes() .
tr(
tda(
'<p>' . fInput( 'submit', 'save', 'Save Categories', 'publish' ) . '</p>' .
sInput( 'save' ) .
eInput( 'fly_cat_restriction' ),
' style="text-align:center"'
)
) .
endTable(),
null,
null,
'post',
null,
null,
'theForm'
); // form() END
echo '</div><br clear="all"/>';
echo $this->javascript('checkbox');
echo '<br/><p align="center"><small>This Plugin was made by ' . href('flyweb Productions', 'http://www.flyweb.at') . '.</small></p>' . "\n";
if (!empty($saveMsg))
echo $saveMsg;
}
/**
*
* HTML Dropdown Menu with Section Names
*
* @param string $_POST['section] - If present Section gets preselected in Dropdown Menu
* @return string $dropdown - HTML Dropdown Menu
*
*/
function sections_dropdown() {
$dropdown = '';
$section = gps('section');
$getSections = safe_column('name', PFX . 'txp_section', "name != 'default'");
if ($getSections) {
$dropdown .= '<label for="section">Section ' . "\n";
$dropdown .= selectInput('section', $getSections, $section, false, '', 'section');
$dropdown .= '</label>' . "\n";
}
return $dropdown;
}
/**
*
* HTML Table Rows with Categporie Checkboxes for given Section
*
* @return string $rows
*
*/
function categories_checkboxes() {
$rows = '';
$getCategories = getTree('root', 'article');
if ($getCategories) {
foreach ($getCategories as $catKey => $category) {
if ( $category['parent'] == 'root' ){
$cell =
tda(
'<label for="cat-' . $category['name'] . '">' .
checkbox('categories[' . $catKey . '][name]', $category['name'], 0, null, 'cat-' . $category['name']) .
$category['title'] . '</label>' .
hInput( 'categories[' . $catKey . '][id]', '')
);
} else {
$cell = tda( '      ' . $category['title']);
}
$rows .= tr( $cell );
}
}
return $rows;
}
/**
*
* javascript - Javascript Functions for Write Panel and Admin Page.
*
* @param string $element - Set JS-Function for "dropdown" (Write Panel) or "checkbox" (Admin Page)
* @uses jQuery
* @return string $html
*
*/
function javascript($element='dropdown') {
$JSON = $this->restrictions_JSON();
$class = ( get_pref('fly_restrict_cat_pref') ? 'restrictedHidden' : 'restricted' );
$html = '<style type="text/css"> .restricted {background-color: red; color: white; } .restrictedHidden { display:none; }</style>' . "\n";
$html .= "<script type=\"text/javascript\">
jQuery(function() {
var filter = JSON.parse('$JSON');
$('#section').change( function() {
if( filter.hasOwnProperty(this.value) == true ){
catObj = filter[this.value];
} else {
catObj = [];
}
fw_cat_$element(catObj);
}).change();
$(\"input[id^='cat-']\").change( function() {
$(this).parents('tr').toggleClass('selected');
}
);
function fw_cat_checkbox(catObj) {
$(\"input[id^='cat-']\").prop('checked', false);
$(\"input[id^='cat-']\").parents('tr').removeClass('selected');
$(\"input[id^='cat-']\").parent().next('input').prop('value', '');
Object.keys(catObj).forEach(function (key) {
var val = catObj[key];
$(\"#cat-\" + val.name).prop('checked', true);
$(\"#cat-\" + val.name).parents('tr').addClass('selected');
$(\"#cat-\" + val.name).parent().next('input').prop('value', val.id);
});
};
function fw_cat_dropdown(catObj) {
$(\"select[name='Category1'] option:gt(0), select[name='Category2'] option:gt(0)\").attr('disabled', false);
$(\"select[name='Category1'] option:gt(0), select[name='Category2'] option:gt(0)\").removeClass('$class');
Object.keys(catObj).forEach(function (key) {
var val = catObj[key];
$(\"#categories_group option[value='\" + val.name + \"']\").attr('disabled', 'disabled');
$(\"#categories_group option[value='\" + val.name + \"']\").attr('selected', false);
$(\"#categories_group option[value='\" + val.name + \"']\").nextUntil( $(\" option:not(:contains('\u00a0')) \") ).attr('disabled', 'disabled');
$(\"#categories_group option[value='\" + val.name + \"']\").addClass('$class');
$(\"#categories_group option[value='\" + val.name + \"']\").nextUntil( $(\" option:not(:contains('\u00a0')) \") ).addClass('$class');
});
};
});
function savePref(pref){
sendAsyncEvent(
{
event: textpattern.event,
step: 'savePref',
fly_restrict_cat_pref: pref.checked
}, null, 'script'
);
};
</script>";
echo $html;
}
} // class fly_cat_restrictionsClass() END
# --- END PLUGIN CODE ---
if (0) {
?>
<!--
# --- BEGIN PLUGIN HELP ---
<h1>Category Restrictions</h1>
<p>With this Plugin you can restrict Article-Categories from specific sections in the Write Panels Categories Dropdowns.</p>
<h2>Installation</h2>
<p>After installing the Plugin you can find the Settings Page "Category Restrictions" under the Extensions Tab.</p>
<h2>Changelog</h2>
<p><strong>Version 1.0</strong> Initial release - 03. Feb. 2015</p>
<p><strong>Version 1.1</strong> 06. Feb. 2015
<ul>
<li>Changed plugin name from fw_cat_restrictions to fly_cat_restrictions to follow TXP plugin guidelines</li>
<li>Bugfix for Dropdown if section and category names were equal</li>
<li>Added Feature Request to show/hide restricted Categories on Write Tab. Set via checkbox on Plugin Setting page</li>
<li>Added CSS-Classes .restricted and .restrictedHidden for Option Tag in Dropdown on Write Tab</li>
</ul>
</p>
<br><br><br><br><br><hr><p align="center"><small>This Plugin was made by <a href="http://www.flyweb.at"> flyweb Productions</a>.</small></p>
# --- END PLUGIN HELP ---
-->
<?php
}
?>