-
Notifications
You must be signed in to change notification settings - Fork 5
/
smd_tags.php
5424 lines (4650 loc) · 226 KB
/
smd_tags.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
// This is a PLUGIN TEMPLATE for Textpattern CMS.
// Copy this file to a new name like abc_myplugin.php. Edit the code, then
// run this file at the command line to produce a plugin for distribution:
// $ php abc_myplugin.php > abc_myplugin-0.1.txt
// Plugin name is optional. If unset, it will be extracted from the current
// file name. Plugin names should start with a three letter prefix which is
// unique and reserved for each plugin author ("abc" is just an example).
// Uncomment and edit this line to override:
$plugin['name'] = 'smd_tags';
// Allow raw HTML help, as opposed to Textile.
// 0 = Plugin help is in Textile format, no raw HTML allowed (default).
// 1 = Plugin help is in raw HTML. Not recommended.
# $plugin['allow_html_help'] = 1;
$plugin['version'] = '0.8.1';
$plugin['author'] = 'Stef Dawson';
$plugin['author_uri'] = 'https://stefdawson.com/';
$plugin['description'] = 'Unlimited tag taxonomy for articles, images, files and links';
// Plugin load order:
// The default value of 5 would fit most plugins, while for instance comment
// spam evaluators or URL redirectors would probably want to run earlier
// (1...4) to prepare the environment for everything else that follows.
// Values 6...9 should be considered for plugins which would work late.
// This order is user-overrideable.
$plugin['order'] = '4';
// Plugin 'type' defines where the plugin is loaded
// 0 = public : only on the public side of the website (default)
// 1 = public+admin : on both the public and admin side
// 2 = library : only when include_plugin() or require_plugin() is called
// 3 = admin : only on the admin side (no AJAX)
// 4 = admin+ajax : only on the admin side (AJAX supported)
// 5 = public+admin+ajax : on both the public and admin side (AJAX supported)
$plugin['type'] = '5';
// Plugin "flags" signal the presence of optional capabilities to the core plugin loader.
// Use an appropriately OR-ed combination of these flags.
// The four high-order bits 0xf000 are available for this plugin's private use
if (!defined('PLUGIN_HAS_PREFS')) define('PLUGIN_HAS_PREFS', 0x0001); // This plugin wants to receive "plugin_prefs.{$plugin['name']}" events
if (!defined('PLUGIN_LIFECYCLE_NOTIFY')) define('PLUGIN_LIFECYCLE_NOTIFY', 0x0002); // This plugin wants to receive "plugin_lifecycle.{$plugin['name']}" events
$plugin['flags'] = '3';
// Plugin 'textpack' is optional. It provides i18n strings to be used in conjunction with gTxt().
// Syntax:
// ## arbitrary comment
// #@event
// #@language ISO-LANGUAGE-CODE
// abc_string_name => Localized String
$plugin['textpack'] = <<<EOT
#@owner
smd_tags
#@language en, en-gb, en-us
#@admin-side
smd_tags => Tags (smd)
#@smd_tag
smd_tag_ac_std => Standard
smd_tag_ac_str => Strict
smd_tag_all_lbl => All
smd_tag_already_exists => already exists
smd_tag_assign_parent_lbl => Assign parent
smd_tag_bi_both => Both
smd_tag_bi_down => Down
smd_tag_bi_up => Up
smd_tag_by_column => Column
smd_tag_by_row => Row
smd_tag_category_linked_lbl => Linked to category "{category}":
smd_tag_category_unlinked_lbl => Unlinked:
smd_tag_children_and => and its children
smd_tag_children_del => Delete children
smd_tag_children_pro => Promote children
smd_tag_clear => clr
smd_tag_clink_lbl => Linked cat
smd_tag_count_lbl => Matched tags:
smd_tag_created => {type} tag "{name}" created
smd_tag_created_rep_lbl => Created tags:
smd_tag_create_lbl => Create/Edit tag
smd_tag_deleted => {type} tag "{name}" deleted
smd_tag_deleted_lbl => Deleted tags:
smd_tag_err_in_use_lbl => Tags in use:
smd_tag_err_orphaned_lbl => Orphaned tags:
smd_tag_exists => {type} tag "{name}" already exists
smd_tag_extras => More...
smd_tag_import_results_pt1 => Articles:
smd_tag_import_results_pt2 => Tags linked:
smd_tag_in_use => {type} tag "{name}" is in use
smd_tag_link_to_cat_lbl => Link to category
smd_tag_manage_lbl => Manage tags
smd_tag_missing_rep_lbl => Missing tags:
smd_tag_multi_delete => Tags deleted: {number} (see report for details)
smd_tag_not_available => Tables are not installed
smd_tag_not_created => {type} tag "{name}" NOT created
smd_tag_not_created_rep_lbl => Tags NOT created:
smd_tag_not_parent_linked_lbl => Not linked:
smd_tag_not_updated => {type} tag "{name}" NOT updated
smd_tag_no_desc => No description available
smd_tag_no_name => {type} tag NOT created: needs a name
smd_tag_no_parent => No parent tags.
smd_tag_no_sections => No sections
smd_tag_no_tags => No {type} tags defined
smd_tag_parent_lbl => Parent
smd_tag_parent_linked_lbl => Assigned these tag(s) to "{parent}":
smd_tag_pool_lbl => Tag pool
smd_tag_prefs_installed => Preferences installed
smd_tag_prefs_not_installed => Preferences not installed.
smd_tag_prefs_p => Interface settings
smd_tag_prefs_pane => Prefs
smd_tag_prefs_removed => Preferences removed
smd_tag_prefs_some => Not all preferences available.
smd_tag_prefs_some_explain => This is either a new installation or a different version<br />of the plugin to one you had before.
smd_tag_prefs_some_opts1 => Choose "Remove prefs" to remove them all or "Install prefs" to add<br />any new ones, leaving all existing prefs untouched.
smd_tag_prefs_some_opts2 => Click "Install tables" to add or update the tables<br />leaving all existing data untouched.
smd_tag_prefs_some_tbl => Not all table info available.
smd_tag_prefs_t => Tag management
smd_tag_prefs_title => Tag Preferences
smd_tag_prefs_u => URL management
smd_tag_pref_install_lbl => Install prefs
smd_tag_pref_remove_lbl => Remove prefs
smd_tag_pref_show_lbl => Preferences
smd_tag_p_enable => Enable tags in
smd_tag_p_input => Enter tags using
smd_tag_p_lbi => Bi-directional tag trees
smd_tag_p_linkcat => Link tags to categories
smd_tag_p_listpar => Permit parent tag selection
smd_tag_p_master => Master parent tag
smd_tag_p_qtag => Quick tag (<a href="http://plugins.jquery.com/project/autocompletex">requires plugin</a>)
smd_tag_p_qtpath => JS plugin dir
smd_tag_p_qtstyl => JS style dir
smd_tag_p_size => Select/textarea rows
smd_tag_recent_report => Display recent report
smd_tag_report_lbl => Report: smd_tags
smd_tag_rss_uc_only => (ignored for tru_tags)
smd_tag_rss_uc_tru_tags_only => (ignored for Textpattern cats)
smd_tag_sel_list => Select list
smd_tag_sync_cfs_delim_lbl => Custom field delimiter
smd_tag_sync_cfs_lbl => Custom field
smd_tag_sync_delete_orig_lbl => Delete original
smd_tag_sync_force_cat_lbl => Force category link if tag already exists
smd_tag_sync_force_parent_lbl => Force parent if tag already exists
smd_tag_sync_import_opts => Import options
smd_tag_sync_lbl => Import tags
smd_tag_sync_link_cat_lbl => Link tags to category
smd_tag_sync_parent_cat_lbl => Link to category
smd_tag_sync_parent_lbl => Start from parent category
smd_tag_sync_parent_tag_lbl => Assign to parent tag
smd_tag_sync_plugin_opts => Source options
smd_tag_sync_plugs_not_installed => tru_tags or rss_unlimited_categories must be installed and activated to import
smd_tag_sync_section_lbl => Articles from section
smd_tag_sync_txpcats_lbl => Textpattern category
smd_tag_sync_type1 => tru_tags
smd_tag_sync_type2 => rss_unlimited_categories
smd_tag_sync_type3 => Textpattern categories
smd_tag_sync_type4 => Custom field
smd_tag_sync_type_lbl => Import from
smd_tag_tags_pane => Manage tags
smd_tag_tag_search => Tag search
smd_tag_tbl_installed => Tag tables installed.
smd_tag_tbl_install_lbl => Install tables
smd_tag_tbl_not_installed => Tag tables NOT installed.
smd_tag_tbl_not_removed => Tag tables NOT removed.
smd_tag_tbl_rebuild_lbl => Rebuild tags
smd_tag_tbl_rebuilt => Tag tables rebuilt
smd_tag_tbl_removed => Tag tables removed.
smd_tag_tbl_remove_lbl => Remove tables
smd_tag_textbox => Text area
smd_tag_textboxplus => Text area+
smd_tag_textlist => Text list
smd_tag_title => Tags
smd_tag_toggle => tog
smd_tag_t_astart => Initial pane
smd_tag_t_auto => Auto name
smd_tag_t_cols => Tag layout
smd_tag_t_colsord => Order tags by
smd_tag_t_count => Show tag usage counts
smd_tag_t_deltree => When deleting a parent
smd_tag_t_delused => Allow deletion of used tags
smd_tag_t_desc_textile => Textile description
smd_tag_t_desc_tooltip => Show description as tooltip
smd_tag_t_enrep => Automatically display reports
smd_tag_t_hilite => Clicked RGB colour
smd_tag_t_hover => Mouse-over RGB colour
smd_tag_t_indent => Sub-tag level indicator
smd_tag_t_mdelim => Multi-tag delimiter (1 char)
smd_tag_t_size_desc => Description width, height
smd_tag_unable_to_create => cannot create
smd_tag_updated => {type} tag "{name}" updated
smd_tag_u_combi => Use tag combinations
smd_tag_u_combi_and => AND combinator char
smd_tag_u_combi_or => OR combinator char
smd_tag_u_pnam => URL name parameter
smd_tag_u_ptyp => URL type parameter
smd_tag_u_sec => Trigger(s) for tag lists
smd_tag_with_filtered => With filtered:
EOT;
if (!defined('txpinterface'))
@include_once('zem_tpl.php');
# --- BEGIN PLUGIN CODE ---
//<?php
/**
* smd_tags: A Textpattern CMS plugin for unlimited, structured taxonomy across content types.
*
* Features:
* Article, image, file and link tags
* Rapid tag creation / editing / multi-editing
* Optionally import tags from rss_uc, tru_tags, category1/2 or custom fields
* Create tag lists, trees and clouds
* Find related content based on tag
* Tag combinators from the URL
*
* @author Stef Dawson
* @link https://stefdawson.com/
*
* @todo
* Alter tag pool layout to allow collapsing groups of parent->children
* Use article_row_info(), link_format_info()
* Investigate storing tag data as jQuery .data() instead of in <span>s (from Txp 4.6+ it can be done in HTML data-* attributes)
* Pagination of tag lists / related content
* Feed handler prefs to inject tags into RSS+Atom (like tru_tags: code at bottom of this plugin)
* Add option to display tag lists AND TextArea(+) on list pages
* See if rapid tagging can be made more intuitive wrt Create/Save to avoid clobbering already created tags (from pieman http://forum.textpattern.com/viewtopic.php?pid=240099#p240099)
* When refreshing the Manage tab, FF remembers the Title and tries to add it on page load, throwing a msgarea warning about the tag already exists (or that name is empty)
* Possibility of section tags? (from jakob)
* Per-section / per-category tag counts
* Query tags without section/article context (Dwayne: http://forum.textpattern.com/viewtopic.php?pid=246750#p246750)
* On list pages:
* AJAX fetch of the tag set for TextArea and TextArea+ modes to improve page load time (plus we then only grab the ones in the current category hierarchy)
* Add autocomplete options to smd_tags prefs so autofill etc can be configured (from aswihart)
* Check what happens when deleting content (e.g. images): it sometimes triggers " '' is not an Integer " assertion (from curiouz http://forum.textpattern.com/viewtopic.php?pid=236023#p236023)
* Perhaps offer sort option for List mode to a) retain hierarchy, b) strict alphabetical, c) group them somehow to show how sets of tags are related (from pieman)
* @tofix
* Sanitize tags from custom fields / cats during import
* Blob of output from AJAX (cf. "If you spot such JavaScript responses your plugin breaks AJAX at the write tab, for instance by echo()ing something as text/html when send_script_response() with text/javascript would be the proper reply with the expected mime type.")
* Query count -- cache tag sets and verify URL tag lists are as optimum as possible (see smd_bio)
* Live search sometimes gives wrong results when group tags contain no (or very few) children
* Verify limit/offset behaviour (giz: http://forum.textpattern.com/viewtopic.php?pid=244020#p244020)
* smd_tags_context() : Invalid argument supplied for foreach() (frickinmuck: http://forum.textpattern.com/viewtopic.php?pid=249250#p249250)
* Deal with parent attribute in smd_tag_name better (stop it showing 'root'?)
* Detect scenario when article saves fail and don't update tags under these circumstances (e.g. when concurrent users are editing the same document)
* When adding a new tag, it should immediately become available as a parent.
* When adding a tag with an apostrophe, the tag title has a backslash added in the box upon save. But it's not there in subsequent views, only after saving/updating.
*/
// ------------------------
// ADMIN SIDE CODE
if (!defined('SMD_TAG')) {
define("SMD_TAG", 'smd_tags');
}
if (!defined('SMD_TAGC')) {
define("SMD_TAGC", 'smd_tags_cat');
}
if (!defined('SMD_TAGU')) {
define("SMD_TAGU", 'smd_tags_used');
}
if (class_exists('\Textpattern\Tag\Registry')) {
Txp::get('\Textpattern\Tag\Registry')
->register('smd_tag_list')
->register('smd_tag_name')
->register('smd_tag_count')
->register('smd_tag_info')
->register('smd_if_tag')
->register('smd_if_tag_list')
->register('smd_related_tags');
}
if (txpinterface === 'admin') {
add_privs('smd_tags','1,2');
add_privs('smd_tags_users','1,2,3,4,5,6');
add_privs('plugin_prefs.smd_tags','1,2');
// Extensions tab
register_tab('extensions', 'smd_tags', gTxt('smd_tags'));
register_callback('smd_tags', 'smd_tags');
register_callback('smd_tags_users', 'smd_tags_users');
// Note the callbacks are ordered with 'save' first
register_callback('smd_tags_multi_edit', 'list', 'list_multi_edit');
register_callback('smd_tags_savelist', 'article', 'create');
register_callback('smd_tags_savelist', 'article', 'edit');
register_callback('smd_tags_loadlist', 'article');
register_callback('smd_tags_multi_edit', 'image', 'image_delete');
register_callback('smd_tags_savelist', 'image', 'image_save');
register_callback('smd_tags_loadlist', 'image');
register_callback('smd_tags_multi_edit', 'file', 'file_delete');
register_callback('smd_tags_savelist', 'file', 'file_save');
register_callback('smd_tags_loadlist', 'file');
register_callback('smd_tags_multi_edit', 'link', 'link_multi_edit');
register_callback('smd_tags_savelist', 'link', 'link_post');
register_callback('smd_tags_savelist', 'link', 'link_save');
register_callback('smd_tags_loadlist', 'link');
register_callback('smd_tags_setup', 'plugin_prefs.smd_tags');
register_callback('smd_tags_welcome', 'plugin_lifecycle.smd_tags');
register_callback('smd_tags_inject_css', 'admin_side', 'head_end');
} elseif (txpinterface === 'public') {
// register_callback('smd_tags_url_handler', 'pretext');
smd_tags_url_handler();
}
/**
* Runs on plugin installation.
*
* @param string $evt Textpattern event (panel)
* @param string $stp Textpattern step (action)
*/
function smd_tags_welcome($evt, $stp)
{
$msg = '';
switch ($stp) {
case 'installed':
smd_tags_table_install();
$msg = 'Happy tagging!';
break;
case 'deleted':
smd_tags_table_remove();
break;
}
return $msg;
}
/**
* Step dispatcher.
*
* @param string $evt Textpattern event (panel)
* @param string $stp Textpattern step (action)
*/
function smd_tags($evt, $stp)
{
$available_steps = array(
'smd_tag_catlist' => true,
'smd_tag_create' => true,
'smd_tag_parentlist' => true,
'smd_tag_save' => true,
'smd_tags_delete' => true,
'smd_tags_import' => true,
'smd_tags_import_one' => true,
'smd_tags_manage' => false,
'smd_tags_multi_set_parent' => true,
'smd_tags_multi_catlink' => true,
'smd_tags_prefs_show' => false,
'smd_tags_prefs_install' => false,
'smd_tags_prefs_remove' => false,
'smd_tags_prefs_update' => true,
'smd_tags_sync' => false,
'smd_tags_table_install' => false,
'smd_tags_table_install_ui' => false,
'smd_tags_table_remove' => false,
'smd_tags_table_remove_ui' => false,
'smd_tags_table_rebuild' => false,
);
if (!$stp or !bouncer($stp, $available_steps)) {
$pref = smd_tags_pref_get('smd_tag_t_astart');
$stp = 'smd_tags_prefs_show';
if ($pref) {
switch ($pref['smd_tag_t_astart']['val']) {
case 1:
$stp = 'smd_tags_manage';
break;
case 0:
default:
$stp = 'smd_tags_prefs_show';
}
}
}
$stp();
}
/**
* Ajax step dispatcher.
*
* @param string $evt Textpattern event (panel)
* @param string $stp Textpattern step (action)
*/
function smd_tags_users($evt, $stp)
{
$available_steps = array(
'smd_tag_catlist' => true,
'smd_tag_parentlist' => true,
'smd_tag_get_desc' => true,
);
if (!$stp or !bouncer($stp, $available_steps)) {
// Do nothing
} else {
$stp();
}
}
// ------------------------
// Create dropdown and populate it with tags of the relevant type, highlighting any $sel tags
function smd_multiTreeSelectInput($select_name = '', $tree = '', $sel = '')
{
$pref = smd_tags_pref_get(array('smd_tag_p_size', 'smd_tag_p_listpar'), 1);
$rows = $pref['smd_tag_p_size']['val'];
$incl = $pref['smd_tag_p_listpar']['val'];
$out[] = '<select id="'.$select_name.'" name="'.$select_name.'[]" class="list" size="'. $rows .'"' .(($rows==1) ? '' : ' multiple="multiple"'). '>';
foreach ($tree as $leaf) {
if ($leaf['name'] == 'root') continue;
$selected='';
foreach ($sel as $gid => $item) {
if ($leaf['id'] == $item['tag_id']) {
$selected = ' selected="selected"';
continue;
}
}
$leaf['name'] = htmlspecialchars($leaf['name']);
$indent = ($leaf['level'] > 0) ? str_repeat(sp.sp,$leaf['level']) : '';
$selectable = (!$incl && $leaf['children'] > 0) ? ' disabled="disabled"' : '';
$out[] = t.'<option value="'.$leaf['id'].'"'.$selected.$selectable.'>'.$indent.htmlspecialchars($leaf['title']).'</option>';
}
$out[] = '</select>';
return join('',$out);
}
// ------------------------
// Called whenever one of the 4 main tabs are used on the admin side. Inserts the input method if required
function smd_tags_loadlist($evt, $stp)
{
global $app_mode, $step;
if (smd_tags_table_exist(1)) {
$ctrls = smd_tags_pref_get(array('smd_tag_p_enable', 'smd_tag_p_input', 'smd_tag_p_size', 'smd_tag_p_qtag', 'smd_tag_p_qtpath', 'smd_tag_p_qtstyl', 'smd_tag_p_linkcat', 'smd_tag_t_desc_tooltip'), 1);
$onoff = smd_tags_pref_explode($ctrls['smd_tag_p_enable']['val']);
$iptyp = $ctrls['smd_tag_p_input']['val'];
$selsz = $ctrls['smd_tag_p_size']['val'];
$quick = $ctrls['smd_tag_p_qtag']['val'];
$jsdir = $ctrls['smd_tag_p_qtpath']['val'];
$csdir = $ctrls['smd_tag_p_qtstyl']['val'];
$clink = $ctrls['smd_tag_p_linkcat']['val'];
$toolt = $ctrls['smd_tag_t_desc_tooltip']['val'];
$jstail = ($jsdir) ? '/' : '';
$cstail = ($csdir) ? '/' : '';
$addIt = false;
$itemID = smd_getID();
$edtBtn = has_privs('smd_tags') ? ' ['.eLink('smd_tags', 'smd_tags_manage', 'smd_tag_type', $evt, gTxt('edit')).'] ' : '';
$tagtop = gTxt('smd_tag_title').$edtBtn;
$selist = '<span id="smd_tags" name="smd_tags_bylist" class="smd_tagip"></span>';
$selhid = '<span id="smd_tags" name="smd_tags_bylist" class="smd_hidden"></span>';
$tooltip = ($toolt) ? '<span id="smd_tag_tt" class="smd_tooltip smd_hidden"></span>' : '';
$widget = '';
switch ($iptyp) {
case 0:
$ipmeth = $selist;
break;
case 1:
$ipmeth = '<span name="smd_tags_bylink" id="smd_tags_bylink" class="smd_tagip"></span>'.$selhid;
break;
case 2:
case 3:
$ipmeth = '<textarea name="smd_tags_bytext" rows="'.$selsz.'" class="smd_tagip"></textarea>'.$selhid;
break;
}
switch ($evt) {
case "article":
$addIt = ($onoff[gTxt('tab_list')] == 1) ? true : false;
if ($addIt) {
extract(gpsa(array('view','from_view')));
$view = ($view) ? $view : "text";
$grabcats = 'jQuery("#category-1 option:selected, #category-2 option:selected")';
$jsElem = 'jQuery(".category-2").parent()';
$trigger = 'jQuery("#category-1, #category-2")';
if ($view != 'text') {
$widget = '';
} else {
$widget = escape_js('<div>'.$tagtop.'</div>' . $ipmeth);
}
}
break;
case "image":
if (in_array($step, array('image_edit', 'image_replace', 'thumbnail_insert'))) {
$addIt = ($onoff[gTxt('tab_image')] == 1) ? true : false;
if ($addIt) {
$grabcats = 'jQuery("#image_category option:selected")';
$jsElem = 'jQuery(".edit-image-category")';
$trigger = 'jQuery("#image_category")';
$widget = escape_js(inputLabel('smd_tags', $ipmeth, $tagtop));
}
}
break;
case "file":
if (in_array($step, array('file_edit', 'file_replace'))) {
$addIt = ($onoff[gTxt('tab_file')] == 1) ? true : false;
if ($addIt) {
$grabcats = 'jQuery("#file_category option:selected")';
$jsElem = 'jQuery(".edit-file-category")';
$trigger = 'jQuery("#file_category")';
$widget = escape_js(inputLabel('smd_tags', $ipmeth, $tagtop));
}
}
break;
case "link":
if ($step == "link_edit") {
$addIt = ($onoff[gTxt('tab_link')] == 1) ? true : false;
if ($addIt) {
$grabcats = 'jQuery("#link_category option:selected")';
$jsElem = 'jQuery(".edit-link-category")';
$trigger = 'jQuery("#link_category")';
$widget = escape_js(inputLabel('smd_tags', $ipmeth, $tagtop));
}
}
break;
}
// Don't re-serve the css+javascript if this is an AJAX save
if ($app_mode == 'async') {
$addIt = false;
}
// Add the dropdown and js toggle/clear options if required
if ($addIt) {
$qs = array(
"event" => "smd_tags_users",
);
$qsVars = "index.php".join_qs($qs);
if ($quick > 0) {
echo <<<EOJS
<script type="text/javascript" src="{$jsdir}{$jstail}jquery.autocomplete.pack.js"></script>
<link rel="stylesheet" type="text/css" href="{$csdir}{$cstail}jquery.autocomplete.css"></script>
EOJS;
}
echo script_js(<<<EOJS
jQuery(function() {
{$jsElem}.after('{$widget}{$tooltip}');
function smd_tagList(typ) {
var grabcats = [];
{$grabcats}.each(function() {
var meVal = jQuery(this).val();
if (meVal != '') {
grabcats.push(meVal);
}
});
grabcats = grabcats.join(",");
var smd_link_mode = (('{$clink}'=='0') ? '2' : ((grabcats=='') ? 1 : ''));
jQuery.post('{$qsVars}', { step: "smd_tag_parentlist", name: 'smd_tag_parent', type: typ, cat: grabcats, link_mode: smd_link_mode, itemid: "{$itemID}", _txp_token: textpattern._txp_token },
function(data) {
jQuery("#smd_tags").html(data);
jQuery("#smd_tags_bylink").empty();
var smd_tagpool = [];
var smd_tagsel = [];
var smd_taglinks = [];
jQuery('#smd_tags select option').each(function() {
var curr = jQuery(this);
var txt = jQuery.trim(curr.text());
smd_tagpool.push(txt);
if (jQuery("#smd_tags_bylink").text() != "") {
jQuery("#smd_tags_bylink").append(" · ");
}
if (curr.prop("selected") == true) {
smd_tagsel.push(txt);
jQuery("#smd_tags_bylink").append('<span data-id="'+curr.val()+'" class="smd_sel">'+txt+'</span>');
} else {
jQuery("#smd_tags_bylink").append('<span data-id="'+curr.val()+'">'+txt+'</span>');
}
});
jQuery("#smd_tags_bylink span").click(function() {
jQuery(this).toggleClass("smd_sel");
var ltxt = jQuery(this).text();
jQuery("#smd_tags select option").each(function() {
var curr = jQuery(this);
if (jQuery.trim(curr.text()) == ltxt) {
this.selected = !this.selected;
}
});
});
jQuery("textarea[name='smd_tags_bytext']").val(smd_tagsel.join(", "));
if ({$quick} > 0) {
jQuery("textarea[name='smd_tags_bytext']").autocomplete(smd_tagpool, {
multiple: true,
mustMatch: (($quick==2)?true:false),
autoFill: true
});
}
jQuery("textarea[name='smd_tags_bytext']").keyup(function() {
smd_tagsResyncSelect({$quick});
});
jQuery('#smd_clr').click(function() {
jQuery('#smd_tags select option').prop("selected", "");
});
jQuery('#smd_tog').click(function() {
jQuery('#smd_tags select option').each(function() {
this.selected = !this.selected;
});
});
// Tooltips
if ('{$toolt}' == '1') {
jQuery('#smd_tags select, #smd_tags_bylink').hover( function(ev) {
// Do nothing
}, function() {
// Hide tooltip when exiting the entire tag area
jQuery('.smd_tooltip').hide('fast');
});
jQuery('#smd_tags select option, #smd_tags_bylink span').hover( function(ev) {
var me = jQuery(this);
var meTT = jQuery('#smd_tag_tt');
var thisTag = me.val();
if (!thisTag) {
thisTag = me.data('id').toString();
}
if (jQuery('#smd_tag_tt').data(thisTag)) {
smd_tags_show_desc(ev, meTT.data(thisTag));
} else {
jQuery.post('{$qsVars}',
{
step: "smd_tag_get_desc",
tag_ref: thisTag,
_txp_token: textpattern._txp_token
},
function(data) {
meTT.data(thisTag, data);
smd_tags_show_desc(ev, meTT.data(thisTag));
});
}
});
}
}
);
}
function smd_tags_show_desc(ev, content) {
jQuery('#smd_tag_tt').hide().empty().append(content).show();
jQuery('.smd_tooltip').css('left', ev.pageX - 60).css('top', ev.pageY + 15).css('display', 'block');
}
smd_tagList('{$evt}');
if ('{$clink}'=='1') {
{$trigger}.change(function() {
smd_tagList('{$evt}');
});
}
function smd_tagsResyncSelect(lvl) {
var tbTags = jQuery("textarea[name='smd_tags_bytext']").val().split(/,\s+?/);
jQuery("#smd_tags select option").each(function() {
var curr = jQuery(this);
if(jQuery.inArray(jQuery.trim(curr.text()), tbTags) == -1) {
curr.prop("selected", "");
} else {
curr.prop("selected", true);
}
});
}
});
EOJS
);
}
}
}
// ------------------------
// Update the tags used table if some tags have been added/saved
function smd_tags_savelist($evt, $stp)
{
if (smd_tags_table_exist(1)) {
$ctrls = smd_tags_pref_get(array('smd_tag_p_qtag', 'smd_tag_p_input', 'smd_tag_p_linkcat', 'smd_tag_p_master'), 1);
$quick = $ctrls['smd_tag_p_qtag']['val'];
$iptyp = $ctrls['smd_tag_p_input']['val'];
$clink = $ctrls['smd_tag_p_linkcat']['val'];
$god = $ctrls['smd_tag_p_master']['val'];
$god_clause = ($god == '') ? '' : " AND parent = '" . doSlash($god) . "'";
$itemID = smd_getID();
if ($itemID) {
// Can't use the 'edit' step because otherwise save triggers on article page load;
// hence, the 'save' button state is used instead. Also, on article creation, the global
// step is set to 'edit' by the time the plugin is hit, so we hack the step if the 'publish'
// button was hit
$stp = (ps('publish')) ? 'publish' : $stp;
$saveSteps = array('create', 'publish', 'image_save', 'file_save', 'link_save', 'link_post');
if (ps('save') || in_array($stp, $saveSteps)) {
safe_delete(SMD_TAGU, 'item_id="'.$itemID.'" AND type="'.$evt.'"');
$tags = ps('smd_tag_parent') ? ps('smd_tag_parent') : array();
$allowedTags = array();
if ($god_clause) {
$allowedTags = safe_column('id', SMD_TAG, "type='$evt'" . $god_clause);
}
// Validate the incoming tags to those in the currently selected category|ies
if ($clink) {
$usedCats = ($evt=="article") ? array(gps('Category1'), gps('Category2')) : array(gps('category'));
$catids = safe_column('id', 'txp_category', "name IN (" .join(',', quote_list($usedCats)). ") AND type='$evt'");
$validTags = ($catids) ? safe_column('tag_id', SMD_TAGC, "cat_id IN (" .join(',', quote_list($catids)). ")") : array();
foreach ($validTags as $validTag) {
$rows = smd_tag_tree(safe_field('name',SMD_TAG,"id='".doSlash($validTag)."'"), $evt, '1=1', SMD_TAG);
foreach ($rows as $row) {
$allowedTags[] = $row['id'];
}
}
$tags = array_intersect(array_unique($allowedTags), $tags);
}
$vals = array();
foreach ($tags as $val){
safe_insert(SMD_TAGU, 'tag_id = "'.$val.'", item_id="'.$itemID.'", type="'.$evt.'"');
$vals[] = $val;
}
$tagsText = preg_split("/,\s+?/", ps('smd_tags_bytext'));
// If we're in non-STRICT mode and there are any typed tags that are not in the select list, check they exist and make links to them
if ($quick < 2) {
$added = ($vals) ? safe_column("title", SMD_TAG, "id IN ('".join("','",$vals)."') AND type='$evt'") : array();
$tagsText = array_diff($tagsText, $added);
// Only loop over tags that don't appear to be already defined
foreach ($tagsText as $tag) {
if ($tag == "") continue;
$exists = safe_field('id', SMD_TAG, "name = '$tag' AND type = '$evt'"); // 1st check
// Textarea+ input system can add (linked) tags with impunity
if ($iptyp == 3 && !$exists) {
$tagBits = explode('-->', $tag);
if (count($tagBits) == 2) {
$tagparent = strtolower(sanitizeForUrl($tagBits[0]));
$tagparent = safe_field('name', SMD_TAG, "name = '$tagparent' AND type = '$evt'"); // Check parent exists for this type
$tagparent = ($tagparent === false) ? 'root' : $tagparent;
$tagchild = $tagBits[1];
} else {
$tagparent = 'root';
$tagchild = $tagBits[0];
}
$sanitag = strtolower(sanitizeForUrl($tagchild));
$already = safe_field('id', SMD_TAG, "name = '$sanitag' AND type = '$evt'"); // Check again because parent-->child will fail 1st check
if ($already === false) {
$exists = safe_insert(SMD_TAG, "name='".$sanitag."', title='$tagchild', parent='$tagparent', type='$evt'");
}
$catfield = ($evt=="article") ? gps('Category1') : gps('category');
if ($clink && $catfield && $exists) {
// Assign new tag to current category (cat1 for articles)
$catid = safe_insert(SMD_TAGC,"tag_id='$exists', cat_id=(SELECT id FROM ".PFX."txp_category WHERE name='$catfield' AND type='$evt')");
}
rebuild_tree_full($evt, SMD_TAG);
}
if ($exists) {
safe_insert(SMD_TAGU, 'tag_id = "'.$exists.'", item_id="'.$itemID.'", type="'.$evt.'"');
}
}
}
}
}
}
}
// ------------------------
// Handle changes to the tags table during multi-item edits
function smd_tags_multi_edit($evt, $stp)
{
// In Txp 4.0.6 the only tabs to allow multi-edits are the Article (list) and Link tabs. Cheat for now
if (smd_tags_table_exist(1)) {
switch ($evt) {
case "list":
case "link":
$method = ps('edit_method');
$selected = ps('selected');
break;
default:
$method = "delete";
$selected = array(ps('id'));
break;
}
// 'list' is a special case that equates to articles
$type = ($evt == "list") ? "article" : $evt;
if ($selected) {
switch ($method) {
case "delete":
$ids = array();
foreach ($selected as $id) {
$id = assert_int($id);
if (safe_delete(SMD_TAGU, 'item_id = '.$id.' AND type="'.$type.'"')) {
$ids[] = $id;
}
}
return join(', ', $ids);
break;
}
}
return '';
}
}
// ------------------------
// Grab the current article/image/file/link ID
function smd_getID()
{
if (!empty($GLOBALS['ID'])) { // newly-saved item
$itemID = intval($GLOBALS['ID']);
} else {
$itemID = (gps('ID')) ? gps('ID') : gps('id');
}
return $itemID;
}
/**
* @param string $evt Textpattern event (panel)
* @param string $stp Textpattern step (action)
*/
function smd_tags_inject_css($evt, $stp)
{
global $event;
$eventmap = array(
'article' => 'list',
'file' => 'file',
'image' => 'image',
'link' => 'link',
'list' => 'list',
);
$smd_tag_preflist = smd_tags_get_prefs('name');
$prefset = smd_tags_pref_get($smd_tag_preflist);
if ($prefset) {
$onoff = smd_tags_pref_explode($prefset['smd_tag_p_enable']['val']);
$mapped = isset($eventmap[$event]) ? $eventmap[$event] : '';
$addIt = ($mapped && $onoff[gTxt('tab_'.$mapped)] == 1) ? true : false;
$stylereps = array(
'{hov}' => $prefset['smd_tag_t_hover']['val'],
'{cur}' => $prefset['smd_tag_t_hilite']['val'],
);
$plugin_styles = array();
if ($event === 'smd_tags') {
$plugin_styles[] = smd_tags_get_style_rules('all');
} elseif ($addIt) {
$plugin_styles[] = smd_tags_get_style_rules('common');
}
if ($plugin_styles) {
$content = strtr(implode(n, $plugin_styles), $stylereps);
if (class_exists('\Textpattern\UI\Style')) {
echo Txp::get('\Textpattern\UI\Style')->setContent($content);
} else {
echo '<style>' . $content . '</style>';
}
}
}
return;
}
/**
* CSS definitions: hopefully kind to themers.
*/
function smd_tags_get_style_rules($type = 'all')
{
$smd_tags_styles['tag'] = array(
'.smd_current { padding:3px; font-weight:bold; background:#{cur}; }',
'.smd_tags_showpars, .smd_tags_linkcat { max-width:12em; }',
'.smd_tag_list_adm { display: flex; flex-wrap: wrap; justify-content: space-between; }',
'.smd_tag_list_adm.byrow { flex-direction: column; }',
'.smd_tag_list_adm.bycol ul { border:1px solid #ccc; padding:1em; }',
'.smd_tag_list_adm.byrow li { display:inline-block; }',
'.smd_tag_list_adm.bycol li { list-style-type:none; }',
'#smd_tag_filt { float:right; margin:0; padding:1em 2em; box-shadow: 4px 4px 5px #999; }',
'#smd_tag_multisel { margin:10px 0 0; }',
'.smd_tags_new label {display:block; margin:2px 1px; }',
'.smd_tags_new input[type="submit"] { margin:1.5em 0 0; }',
'.smd_tags_input_group { float:left; margin:0 1em; }',
'.smd_tags_pool { clear:both; padding:1em 0;}',
'#smd_tag_report_pane { display:none; position:absolute; left:200px; max-width:500px; border:3px ridge #999; opacity:.92; filter:alpha(opacity:92); padding:15px 20px; background-color:#e2dfce; color:#80551e; }',
'#smd_tag_report_pane .publish { float:right; }',
'.smd_tags_btn_ok { position: absolute; top: .27777777777778em; right: .72222222222222em; font-size: 18px; font-weight: 700; text-decoration: none; }',
'.txp-actions { float:right; }',
);
$smd_tags_styles['common'] = array(
'.smd_hidden { display:none; }',
'.smd_hover { cursor:pointer; background:#{hov}; }',
'.smd_fakebtn, #smd_tags_bylink span { cursor:pointer; }',
'.smd_tagip { margin:.4em 0 .8em; max-width:400px; }',
'.smd_sel { font-weight:bold; }',
'.smd_tooltip { position:absolute; background:#eee; max-width:300px; min-width:150px; border:1px solid black; padding:1em; box-shadow:5px 5px 4px #999; z-index:100; }',
);
$out = array();
if ($type === 'all') {
$out = array_merge($smd_tags_styles['tag'], $smd_tags_styles['common']);
} elseif (array_key_exists($type, $smd_tags_styles)) {
$out = $smd_tags_styles[$type];
}
return implode(n, $out);
}
/**
* Fetch all prefs.
*
* Internally, it tracks two lists:
* a) Current prefs
* b) Legacy prefs that needs removing
*/
function smd_tags_get_prefs($type)
{
$current = array(
'smd_tag_prefs_p' => array(
'smd_tag_p_enable' => '1111', // one digit for article/image/file/link. 1=enabled; 0=disabled
'smd_tag_p_input' => '0', // 0=select, 1=links, 2=textarea, 3=textarea+
'smd_tag_p_lbi' => '0',
'smd_tag_p_linkcat' => '0',
'smd_tag_p_listpar' => '1', // permit parent in tag tree on content pages
'smd_tag_p_master' => '', // special 'global' category (per type: same name for each)
'smd_tag_p_qtag' => '0', // 0=no autocomplete, 1=std a/c, 2=strict a/c
'smd_tag_p_qtpath' => '', // Dir to js file(s) relative to textpattern dir
'smd_tag_p_qtstyl' => '', // Dir to css file(s)
'smd_tag_p_size' => '6',
),
'smd_tag_prefs_t' => array(
'smd_tag_t_astart' => '0',
'smd_tag_t_auto' => '1',
'smd_tag_t_cols' => '1',
'smd_tag_t_colsord' => '0',
'smd_tag_t_count' => '1',
'smd_tag_t_deltree' => '0',
'smd_tag_t_delused' => '0',
'smd_tag_t_desc_textile' => '1',
'smd_tag_t_desc_tooltip' => '1',
'smd_tag_t_enrep' => '1',
'smd_tag_t_hilite' => '91919a',
'smd_tag_t_hover' => 'aaa',
'smd_tag_t_indent' => ' ',
'smd_tag_t_mdelim' => ',',
'smd_tag_t_size_desc' => '240, 80',
),
'smd_tag_prefs_u' => array(
'smd_tag_u_combi' => '1',
'smd_tag_u_combi_and' => '+',
'smd_tag_u_combi_or' => '|',
'smd_tag_u_pnam' => 'smd_tag',
'smd_tag_u_ptyp' => 'smd_tagtype',
'smd_tag_u_sec' => 'smd_tags',
),
);
$old = array(
'smd_tag_p_first',
);
switch ($type) {