-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.php
More file actions
3148 lines (2519 loc) · 83.8 KB
/
index.php
File metadata and controls
3148 lines (2519 loc) · 83.8 KB
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
/*
* NAME
*
* index.php
*
* CONCEPT
*
* Top-level index for polymorphic pattern manipulation project.
*
* FUNCTIONS
*
* SelectTemplate present a form for selecting a template
* SelectFeature present a form for selecting a feature
* PatternForm present a form for adding or editing a pattern
* FeatureForm present a form for adding or editing a feature
* AbsorbFeatureEdit accept pattern_feature edits
* AddPattern add a pattern
* TemplateForm present a form for adding or editing a template
* SelectPattern select a pattern for editing
* ViewPattern manage selecting pattern for display
* ManageFeatures manage features associated with a template
* ManagePatterns manage pattern language members
* EatMe members come and go - right here
* AddFeature add the selected feature to the selected template
* RemoveFeatures remove 1 or more features from a template
* AbsorbPatternUpdate absorb pattern update
* AbsorbNewPattern absorb a new pattern
* TemplateMenu popup menu of pattern templates
* PLForm present a form for adding/editing a pattern language
* SelectPL select a pattern_language for editing
* SelectPV select a pattern_view for editing
* PVForm present a form for adding/editing a pattern_view
* AbsorbPV absorb new or edited pattern_view
* ValidateView warn about issues in a a pattern_view
* Status all the things on a single screen
*
* NOTES
*
* This toy application is for exploring the design of "polymorphic"
* patterns: patterns that lack a rigid definition. It's based upon
* the concept of "pattern features," which have a name and data type
* and are associated with patterns via the pattern_template and
* pt_feature tables.
*
* A pattern_feature:
*
* CREATE TABLE `pattern_feature` (
* id int unsigned NOT NULL AUTO_INCREMENT,
* name varchar(255) NOT NULL,
* type enum('string', 'text', 'image', 'integer'),
* notes varchar(255),
* PRIMARY KEY (id),
* UNIQUE KEY `name` (`name`,`type`)
* );
*
* A named pattern_template record defines a set of features:
*
* CREATE TABLE pattern_template (
* id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
* name VARCHAR(255) NOT NULL,
* notes VARCHAR(255),
* );
*
* Each feature type has an associated table that stores values. For example,
* a "title" feature, which has type "varchar(255)", which the user sees
* as "string."
*
* CREATE TABLE pf_string (
* id int unsigned NOT NULL AUTO_INCREMENT,
* pid int(10) unsigned NOT NULL,
* pfid int(10) unsigned NOT NULL,
* language char(2) NOT NULL DEFAULT 'en',
* value tinytext NOT NULL,
* PRIMARY KEY (id,language),
* KEY (pfid),
* CONSTRAINT FOREIGN KEY (pid) REFERENCES pattern (id),
* CONSTRAINT FOREIGN KEY (pfid) REFERENCES pattern_feature (id)
* ) ;
*
* Here is the definition of a pattern, each of which is associated
* with a pattern_template:
*
* CREATE TABLE pattern (
* id int unsigned NOT NULL AUTO_INCREMENT,
* ptid int unsigned NOT NULL,
* PRIMARY KEY (id),
* KEY ptid (ptid),
* CONSTRAINT FOREIGN KEY (ptid) REFERENCES pattern_template (id)
* )
*
* And here is the pt_feature table that associated features with
* pattern_templates:
*
* CREATE TABLE `pt_feature` (
* ptid int unsigned NOT NULL,
* fid int(10) unsigned NOT NULL,
* KEY ptid (ptid),
* KEY fid (fid),
* CONSTRAINNT UNIQUE(ptid, fid),
* CONSTRAINT FOREIGN KEY (ptid) REFERENCES pattern_template (id),
* CONSTRAINT FOREIGN KEY (fid) REFERENCES pattern_feature (id)
* );
*
* For example, we have a pattern_template named "Liberating Voices"
* with id 1. To associate a pattern_feature with the name "title" -
* id 2 - with each pattern using that template we have a row in
* pt_feature with fid = 2 and ptid = 1. For the pattern with the
* title "The Good Life" - pattern.id = 3 - there is a row in the
* pf_string table with pfid = 2 and pid = 3.
*
*/
if(false) {
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
}
require 'pps.php';
DataStoreConnect();
$SuppressMain = false;
const ANOTHER = 'Accept and enter another';
const ADDPATTERNS = 'Accept and add patterns';
const ADDFEATURES = 'Accept and add features';
/* SelectTemplate()
*
* Present a form for selecting a template.
*
* The $context argument, an array, tells us how to label the page
* (label), what hidden field to include (context = action), what
* submit buttons to include (submit[], with 'label' and, optionally,
* 'id' fields), how to label the 0th menu item (zlabel, optional),
* and what leading instructions to include (notes, optional).
*/
function SelectTemplate($context) {
$templates = GetTemplates();
$seltemplate = "<select name=\"template_id\" id=\"template_id\">
<option value=\"0\"></option>
";
print "<h2>{$context['label']}</h2>\n";
foreach($templates as $template) {
# If we are selecting a template for a pattern add, only offer templates
# with associated features.
if($context['context'] == 'pattern' && $context['action'] == 'add')
$disabled = ($template['fcount']) ? '' : ' disabled="disabled"';
# If we are selecting a template for a pattern edit, only offer templates
# with associated patterns.
elseif($context['context'] == 'pattern' && $context['action'] == 'edit')
$disabled = ($template['pcount']) ? '' : ' disabled="disabled"';
else
$disabled = '';
$seltemplate .= " <option value=\"{$template['id']}\"$disabled>
{$template['name']}
</option>
";
}
$seltemplate .= "</select>\n";
$submit = '';
foreach($context['submit'] as $sub) {
if(array_key_exists('id', $sub))
$id = " id=\"{$sub['id']}\"";
else
$id = '';
$submit .= "<input type=\"submit\" name=\"submit\" value=\"{$sub['label']}\"$id>\n";
}
$submit .= "<input type=\"submit\" name=\"submit\" value=\"Cancel\">\n";
if(isset($context['notes']))
print $context['notes'];
print "<form action=\"{$_SERVER['SCRIPT_NAME']}\" method=\"POST\" class=\"featureform\" id=\"selecttemplate\">
<input type=\"hidden\" name=\"{$context['context']}\" value=\"{$context['action']}\">
<div class=\"fname\">Select a template:</div>
<div>$seltemplate</div>
<div class=\"fsub\">
$submit
</div>
</form>
<script>
/* stidf()
*
* Handle 'input' events on the select-template popup. We don't want the
* submit buttons to be active unless a template has been selected.
*/
function stidf() {
submitState = (template_id.value != '0')
submitState2 = (template_id.value > 1)
if(metadata)
metadata.disabled = ! submitState
if(features)
features.disabled = ! submitState2
if(tsel)
tsel.disabled = ! submitState
} /* end stidf() */
selecttemplate = document.querySelector('#selecttemplate')
accept = document.querySelector('#accept')
accepta = document.querySelector('#accepta')
if(template_id = document.querySelector('#template_id'))
template_id.addEventListener('input', stidf)
metadata = document.querySelector('#metadata')
features = document.querySelector('#features')
tsel = document.querySelector('#tsel')
if(metadata || features || tsel)
stidf()
</script>
";
} /* end SelectTemplate() */
/* SelectFeature()
*
* Select a pattern_feature.
*/
function SelectFeature() {
$features = GetFeatures();
$menu = '<select name="feature_id" id="feature_id">
<option value="0"></option>
';
foreach($features as $feature) {
$menu .= " <option value=\"{$feature['id']}\" title=\"{$feature['notes']}\">{$feature['name']} - {$feature['type']}</option>\n";
}
$menu .= '</select>
';
print '<h2>Edit a Feature</h2>
';
Alert("Select the feature you wish to edit.");
print "<form method=\"POST\" action=\"{$_SERVER['SCRIPT_NAME']}\" class=\"featureform\" id=\"selectfeature\">
<input type=\"hidden\" name=\"feature\" value=\"edit\">
<div>$menu<div>
<div>
<input type=\"submit\" name=\"submit\" value=\"Select\" id=\"accept\">
<input type=\"submit\" name=\"submit\" value=\"Cancel\">
<div>
</form>
";
print "<script>
function f(e) {
enable = feature_id.value != '0'
accept.disabled = !enable
} /* end feature_id() */
var feature_id = document.querySelector('#feature_id')
var accept = document.querySelector('#accept')
feature_id.addEventListener('change', f)
f()
</script>
";
} /* end SelectFeature() */
/* PatternForm()
*
* Present a form for adding or editing a pattern. If editing, we have
* the id of the pattern; if adding, the id of the template.
*/
function PatternForm($action, $id) {
print "<style>
.ff3 {
display: grid;
grid-template-columns: repeat(3, auto);
border: 1px solid black;
width: max-content;
background-color: #ffd;
margin-left: 1em;
}
.ff3 div {
padding: .2em;
}
</style>
";
if($action == 'edit') {
// editing an existing pattern
$pattern = GetPattern($id);
$features = $pattern['features'];
$action = 'absorb_edit';
$ptitle = isset($features['title']) ? $features['title']['value'] : '(no title)';
$context = "<input type=\"hidden\" name=\"id\" value=\"$id\">\n";
$nvalue = $pattern['notes'];
$note = "<h2>Editing <em>$ptitle</em></h2>
";
$submit = ' <input type="submit" name="submit" value="Accept" id="accept">
<input type="submit" name="submit" value="Delete">
';
$pas = GetPatternAuthors(['pattern_id' => $id]);
$pas = $pas[$id];
if(count($pas))
foreach($pas as $pa)
$pattern_authors[$pa['author_id']] = $pa;
else
$pattern_authors = [];
} else {
// adding a new pattern
$title = 'Adding Pattern';
$template = GetTemplate($id);
$features = $template['features'];
$action = 'absorb_add';
$context = "<input type=\"hidden\" name=\"template_id\" value=\"$id\">\n";
$nvalue = '';
$note = '<h2>Add a Pattern</h2>
';
Alert("Adding a pattern with template <code>{$template['name']}</code>\n");
$submit = ' <input type="submit" name="submit" value="Accept" id="accept">
<input type="submit" id="accepta" name="submit" value="' . ANOTHER . '">
';
$pattern_authors = [];
}
print "$note\n";
Alert("Required features are displayed <span class=\"required\">like this</span>. Check the checkbox at the right to remove a value.\n");
print "<form enctype=\"multipart/form-data\" action=\"{$_SERVER['SCRIPT_NAME']}\" class=\"ff3\" method=\"POST\" id=\"patternform\">
<div class=\"fh\">Feature name</div>
<div class=\"fh\">Value</div>
<div class=\"fh\"></div>
<input type=\"hidden\" name=\"pattern\" value=\"$action\">
$context
";
/* Loop on features for this template, offering input elements for each.
* We name the input fields with a "f-" prefix on the feature name to
* minimize the chance of a clash with another parameter.
*
* Features of most types have but a name and a value, but image types
* have both an input type=file as well as a value for the alternate text.
*/
$imgs = ''; # image preview elements
foreach($features as $feature) {
$ilink = '';
if($feature['type'] == 'integer') {
# use 5-char <input type="text">
if(isset($feature['value'])) {
$value = " value=\"{$feature['value']}\"";
} else {
$value = '';
}
$input = "<input name=\"f-{$feature['id']}\" type=\"text\" size=\"5\"$value>\n";
} elseif($feature['type'] == 'string') {
# use <input type="text"> for string type
$remove = '';
$value = '';
$iclass = $feature['required'] ? ' class="rinput"' : '';
if(isset($feature['value'])) {
$value = " value=\"{$feature['value']}\"";
if(!$feature['required'])
$remove = "<input type=\"checkbox\" name=\"d-{$feature['id']}\" title=\"remove\">";
}
$input = "<input name=\"f-{$feature['id']}\" type=\"text\" size=\"80\"$value$iclass>";
} elseif($feature['type'] == 'image') {
# use <input type="file"> for images, <input type="text"> for alttext
if(isset($feature['alttext']))
$value = " value=\"{$feature['alttext']}\"";
else
$value = '';
$input = "<input name=\"{$feature['id']}\" type=\"file\">";
$input2 = "<input type=\"text\" name=\"f-{$feature['id']}\" size=\"50\"$value>";
// link to view existing image
if(isset($feature['hash'])) {
/* There is an image to preview:
* compute the path
* display a link to unhide/hide the preview element
* add a <div class="imagebox"> containing the image preview and
* filename, hidden
* add a checkbox to delete the feature
*/
$ipath = ImagePath($feature['hash']);
$ilink = "<span class=\"ilink\" id=\"l-{$feature['hash']}\" title=\"{$feature['filename']}\"> preview </span>";
$remove = "<input type=\"checkbox\" name=\"d-{$feature['id']}\" title=\"remove\">";
$imgs .= "<div class=\"imagebox\" id=\"i-{$feature['hash']}\"><img src=\"$ipath\">
<div id=\"imagemeta\">
<div class=\"h\">File name:</div>
<div><code>{$feature['filename']}</code></div>
</div>
</div>
";
}
} elseif($feature['type'] == 'text') {
# use a <textarea> for text type.
if(isset($feature['value'])) {
$value = $feature['value'];
$remove = "<input type=\"checkbox\" name=\"d-{$feature['id']}\" title=\"remove\">";
} else {
$value = '';
$remove = '';
}
$input = "<textarea name=\"f-{$feature['id']}\" rows=\"3\" cols=\"80\">$value</textarea>\n";
} else {
Error("Unrecognized feature type <code>{$feature['type']}</code>");
}
$lclass = ($feature['required']) ? 'fname required' : 'fname';
print " <div class=\"$lclass\">{$feature['name']}:</div>
<div>{$input}{$ilink}</div>
<div>$remove</div>
";
if($feature['type'] == 'image')
print " <div class=\"fname required\" style=\"font-size: 80%\">Alternate text:</div>
<div>$input2</div>
<div></div>
";
} // end loop on features
// authorship
$authors = GetAuthors();
$paoptions = [];
foreach($authors as $author) {
$selected = (count($pattern_authors) && isset($pattern_authors[$author['id']]))
? ' selected' : '';
$aname = $author['name'];
if(!is_null($author['affiliation']))
$aname .= " ({$author['affiliation']})";
$paoptions[] = " <option value=\"{$author['id']}\"$selected>$aname</option>";
}
$aselect = "<select name=\"authors\" multiple>\n" .
implode("\n", $paoptions) . "\n</select>\n";
print "<div class=\"fname\">Notes:</div>
<div><input type=\"text\" name=\"notes\" size=\"60\" value=\"$nvalue\"></div>
<div></div>
<div class=\"fname\">Authors:</div>
<div>$aselect</div><div></div>
<div class=\"fsub3\">
$submit
<input type=\"submit\" name=\"submit\" value=\"Cancel\">
</div>
</form>
$imgs
<script>
/* pf()
*
* Input fields that are required have the 'rinput' class. We disable
* the submit buttons unless there is a value provided for all of those.
*
* Alternative text is required for image file uploads. We disable the
* submit buttons unless alternative text is provided for each file upload
* field for which a file is provided.
*/
function pf(e) {
disable = false
// look at required fields
rinputs = document.querySelectorAll('.rinput')
for(rinput of rinputs)
if(rinput.value.length == 0)
disable = true
if(!disable) {
// look at file uploads
ffs = document.querySelectorAll(\"input[type='file']\")
for(ff of ffs) {
if(ff.value.length) {
if(at = document.querySelector('input[name=\"f-' + ff.name + '\"'))
if(! at.value.length)
disable = true
}
}
}
if(accept = document.querySelector('#accept'))
accept.disabled = disable
} // end pf()
finputs = document.querySelectorAll(\"input[type='file']\") // file fields
rinputs = document.querySelectorAll('.rinput') // required fields
if(rinputs.length) {
for(rinput of rinputs)
rinput.addEventListener('input', pf)
pf()
}
if(finputs.length) {
// put a click event listener on each file input
for(finput of finputs) {
finput.addEventListener('input', pf)
// the input field for alt text for this field is named f-<feature-id>
if(atinput = document.querySelector('input[name=\"f-' + finput.name + '\"]'))
atinput.addEventListener('input', pf)
}
}
</script>
";
} /* end PatternForm() */
/* FeatureForm()
*
* Display a form for entering or editing a feature.
*/
function FeatureForm($id = null) {
$disabled = ''; // used to disable the type menu
if(isset($id)) {
# Editing an existing feature.
$feature = GetFeature($id);
$checked = $feature['required'] ? ' checked="checked"' : '';
if(!isset($feature))
Error('System error: no feature with id <code>$id</code> exists.');
$fname = " value=\"{$feature['name']}\"";
$fnotes = $feature['notes'];
$fid = "<input type=\"hidden\" name=\"feature_id\" value=\"{$feature['id']}\">\n";
print "<h2>Edit Feature</h2>\n";
$another = '<input id=\"faformsubmit3\" type="submit" name="submit" value="Delete">
';
if($stats = FeatureStats($id)) {
$valueCount = 0;
$instr = "<p class=\"alert\">This feature is used by the following templates (number of patterns with feature values):\n";
foreach($stats as $stat) {
$instr .= "<br><span style=\"margin-left: 2em\"><i>{$stat['name']}</i> ({$stat['count']} patterns</span>)\n";
$valueCount += $stat['count'];
}
if($valueCount)
$disabled = ' disabled="disabled"';
} else {
$instr = "<p class=\"alert\">This feature isn't used in any templates yet.</p>\n";
}
} else {
# Defining a new feature.
$fname = $fnotes = $fid = '';
print "<h2>Add a Feature</h2>
<p class=\"instr\">Select from these types:</p>
<ul>
<li><code>string</code>: a character string of 255 characters or fewer</li>
<li><code>text</code>: up to sixteen million characters</li>
<li><code>integer</code>: a integer value</li>
<li><code>image</code>: a graphic image, up to sixteen million bytes</li>
</ul>
";
$another = '<input type="submit" id="accepta" name="submit" value="' . ANOTHER . "\">\n";
$instr = "<p class=\"alert\">Enter a name, data type, and optional notes
for this new feature. Check the <code>Required?</code> box if every pattern
using a template with this feature is required to have a value for it.</p>
";
}
$typemenu = "<select name=\"type\" id=\"faformtype\"$disabled>
<option value=\"0\">Select a type</option>
";
foreach(TYPE as $alias => $type) {
$selected = (isset($feature) && $feature['type'] == $alias)
? ' selected="selected"' : '';
$typemenu .= " <option value=\"$alias\"$selected>$alias</option>\n";
$checked = '';
}
$typemenu .= "</select>\n";
print "$instr
<form action=\"{$_SERVER['SCRIPT_NAME']}\" class=\"featureform\" method=\"POST\" id=\"featureform\">
<input type=\"hidden\" name=\"feature\" value=\"specify\">
$fid
<div class=\"fname\">Feature name:</div>
<div><input type=\"text\" name=\"name\" id=\"faformname\"$fname></div>
<div class=\"fname\">Type:</div>
<div>$typemenu</div>
<div class=\"fname\">Required?</div>
<div><input type=\"checkbox\" name=\"required\"$checked></div>
<div class=\"fname\">Notes:</div>
<div><textarea name=\"notes\" rows=\"3\" cols=\"80\">$fnotes</textarea></div>
<div class=\"fsub\">
<input type=\"submit\" name=\"submit\" id=\"accept\" value=\"Accept\" id=\"accept\">
$another
<input type=\"submit\" name=\"submit\" value=\"Cancel\">
</div>
</form>
<script>
/* faform()
*
* Control enablement of submit buttons on feature add/edit form.
*/
function faform(event) {
enable = (faformtype.value != '0') && (faformname.value.length > 0)
accept.disabled = ! enable
if(accepta)
accepta.disabled = ! enable
} /* end faform() */
var faformtype = document.querySelector('#faformtype')
var faformname = document.querySelector('#faformname')
var accept = document.querySelector('#accept')
var accepta = document.querySelector('#accepta')
faformname.addEventListener('input', faform)
faformtype.addEventListener('change', faform)
faform()
</script>
";
} /* end FeatureForm() */
/* AbsorbFeatureEdit()
*
* Absorb the edit of a pattern_feature.
*/
function AbsorbFeatureEdit($id, $value) {
$feature = GetFeature($id);
if(!isset($feature))
Error('No such feature');
$update = [];
foreach($value as $k => $v) {
if($feature[$k] != $v)
$update[$k] = $v;
}
if(count($update)) {
$update['id'] = $id;
UpdateFeature($update);
Alert('Feature update accepted.');
} else
Alert('No changes to feature.');
} /* end AbsorbFeatureEdit(() */
/* AddPattern()
*
* Add a pattern. To do it, the user first selects a template so we
* know what features to include.
*/
function AddPattern($template_id = null) {
if(isset($template_id))
PatternForm('add', $template_id);
else
SelectTemplate([
'label' => 'Select Template',
'context' => 'pattern',
'action' => 'add',
'submit' => [
[
'id' => 'tsel',
'label' => 'Select'
]
],
'notes' => "<p class=\"alert\">Select the template that this pattern will use.
Templates with no associated features are disabled.</p>\n"
]);
} /* end AddPattern() */
/* TemplateForm()
*
* Present a form for adding, editing, or deleting a template.
*/
function TemplateForm($id = null) {
$addfeatures = '<input type="submit" name="submit" value="' . ADDFEATURES .
'" id="accepta">';
if(isset($id)) {
# Editing a template.
if(!$id)
Error("You haven't selected a template.");
$count = CountPatterns($id);
print "<h2>Edit Template</h2>
<p class=\"alert\">This template is used by <code>$count</code> patterns.</p>
";
$template = GetTemplate($id);
if(! isset($template))
Error('Template not found.');
$idf = "<input type=\"hidden\" name=\"template_id\" value=\"$id\">\n";
$name_value = " value=\"{$template['name']}\"";
$notes_value = $template['notes'];
$delete = '<input type="submit" name="submit" value="Delete">
';
if($id == 1)
$addfeatures = $delete = '';
} else {
# Adding a template.
print "<h2>Add a Template</h2>\n";
$idf = '';
$name_value = $notes_value = '';
$delete = '';
}
print "<form action=\"{$_SERVER['SCRIPT_NAME']}\" method=\"POST\" class=\"featureform\" id=\"tmeta\">
<input type=\"hidden\" name=\"template\" value=\"absorb_template\">
$idf
<div class=\"fname\">Template name:</div>
<div><input type=\"text\" id=\"name\" name=\"name\"$name_value\"></div>
<div class=\"fname\">Notes:</div>
<div>
<textarea name=\"notes\" rows=\"3\" cols=\"80\">$notes_value</textarea>
</div>
<div class=\"fsub\">
$addfeatures
<input type=\"submit\" name=\"submit\" value=\"Accept\" id=\"accept\">
$delete
<input type=\"submit\" name=\"submit\" value=\"Cancel\">
</div>
</form>
<script>
function et(e) {
tname = document.querySelector('#name')
submitstate = (tname.value.length > 0) ? false : true
accept.disabled = submitstate
if(accepta)
accepta.disabled = submitstate
} // end et()
accept = document.querySelector('#accept') // submit button 1
accepta = document.querySelector('#accepta') // submit button 2
tname = document.querySelector('#name')
tname.addEventListener('input', et)
et()
</script>
";
} /* end TemplateForm() */
/* SelectPattern()
*
* Select a pattern for editing. First, specify a pattern_template.
*/
function SelectPattern($template_id = null) {
if(isset($template_id)) {
# template has been selected, offer a menu of patterns using it
$patterns = GetPatterns(['ptid' => $template_id]);
$template = GetTemplate($template_id);
if(!count($patterns))
Error('No patterns found for this tempate');
$selpattern = '<select name="pattern_id" id="pattern_id">
<option value="0"></option>
';
foreach($patterns as $pattern) {
$title = (isset($pattern['title']))
? $pattern['title'] : '(no title)';
$selpattern .= " <option value=\"{$pattern['id']}\">$title - (id {$pattern['id']})</option>\n";
}
$selpattern .= '</select>
';
print "<h2>Select a Pattern</h2>\n";
Alert("Select a pattern using template <code>{$template['name']}</code>.\n");
print "<form action=\"{$_SERVER['SCRIPT_NAME']}\" method=\"POST\" class=\"featureform\" id=\"selpat\">
<input type=\"hidden\" name=\"pattern\" value=\"edit\">
<div class=\"fname\">Select a pattern:</div>
<div>$selpattern</div>
<div class=\"fsub\">
<input type=\"submit\" id=\"accept\" name=\"submit\" value=\"Edit\">
<input type=\"submit\" name=\"submit\" value=\"Cancel\">
</div>
</form>
<script>
function spidf() {
submitState = (pattern_id.value != '0')
if(accept)
accept.disabled = ! submitState
} /* end spidf()
accept = document.querySelector('#accept') // submit button 1
pattern_id = document.querySelector('#pattern_id'))
pattern_id.addEventListener('input', spidf)
spidf()
</script>
";
} else {
/* Select a pattern_template. */
SelectTemplate([
'label' => 'Edit Pattern: Select a Pattern Template',
'notes' => '<p class="alert">Every pattern is associated with a pattern
template. Filter the pattern selection menu to those of a particular template, or
select from all patterns. Only templates with associated patterns can be
selected.</p>
',
'context' => 'pattern',
'action' => 'edit',
'submit' => [
[
'label' => 'Select',
'id' => 'tsel'
]
]
]);
}
} /* end SelectPattern() */
/* ViewPattern()
*
* View a pattern. There are these prerequite steps:
*
* 1. Select a pattern language.
* 2. Select pattern views that share templates with patterns in the language.
* 3. Click on a per-pattern link to generate the page.
*/
function ViewPattern($context) {
if(array_key_exists('plid', $context)) {
# Language has been selected, offer a set of links and
# per-template popup menus to choose a view. 'change' event
# handlers on the popup menus controls whether the links are
# active, which requires a view be selected. In the case of a
# template with no views, the anchors are never activated. In
# the case of a template with a single view, the view id is in
# a hidden field.
$alerts = '';
$pl = GetPL($plid = $context['plid']);
$plmembers = GetPLMembers($plid);
$templates = GetTemplates();
# Which templates participate?
$ptids = [];
foreach($plmembers as $plmember)
$ptids[$plmember['ptid']] = 0;
# Which views are available for each template? Build popup menus
# in $pvsels for those with multiple, a hidden field for those with
# just one.
$pvsels = [];
$multi = 0;
foreach($ptids as $ptid => $vcount) {
$pvs[$ptid] = GetPVs(['ptid' => $ptid]);
if(isset($pvs[$ptid])) {
if(($ptids[$ptid] = count($pvs[$ptid])) > 1) {
# there are multiple views for this template; offer a popup
$multi++;
$template = $templates[$ptid];
$pvsels[$ptid] = "<div class=\"fname\">
Select a view for template <code>{$template['name']}</code>:
</div>
<div>
<select name=\"pvid-$ptid\" id=\"pvid-$ptid\" class=\"selv\">
<option value=\"0\"></option>
";
foreach($pvs[$ptid] as $pv)
$pvsels[$ptid] .= " <option value=\"{$pv['id']}\">{$pv['name']}</option>\n";
$pvsels[$ptid] .= ' </select>
</div>
';
} elseif(count($pvs[$ptid])) {
# there is one view for this template; use hidden element
$pv = array_shift($pvs[$ptid]);
$pvid = $pv['id'];
$pvsels[$ptid] = "<input type=\"hidden\" name=\"pvid-$ptid\" id=\"pvid-$ptid\" value=\"$pvid\" class=\"selv\" data-text=\"{$pv['name']}\">\n";
} else {
/* there are no pattern views for this template, complain */
$template = $templates[$ptid];
if(strlen($alerts))
$alerts .= "<br>\n";
$alerts .= "There are no pattern views defined for template <em>{$template['name']} </em>";
}
}
} /* end loop on participating templates */
# Build $titles as a sorted-by-title array of arrays with 'title',
# 'ptid', and 'id' fields, one per pattern
$titles = [];
foreach($plmembers as $plmember) {
$pattern = GetPattern($plmember['pid']);
$title = $pattern['features']['title']['value'];
$titles[] = [
'id' => $pattern['id'],
'ptid' => $pattern['ptid'],
'title' => $title
];
}
usort($titles, 'bytitle');
print "<h2>View <em>{$pl['name']}</em> Patterns</h2>\n";
if($multi > 1) {
Alert('Select a pattern view for each template. Click on a linked pattern title to view that pattern with that view.');
$display = '';
} elseif($multi) {
Alert('Select a pattern view for this template. Click on a linked pattern title to view that pattern with that view.');
$display = '';
} else {
Alert('Click on a linked pattern title to view that pattern.');
$display = ' style="display: none"';
}
print "<div class=\"featureform\" id=\"pview\"$display>
";
foreach($pvsels as $pvsel)
print $pvsel;
print "</div>\n";
if(strlen($alerts))
Alert($alerts);
print "<ul id=\"ice\">\n";
foreach($titles as $title)
print " <li><a data-pid=\"{$title['id']}\" data-ptid=\"{$title['ptid']}\" data-tname=\"{$templates[$title['ptid']]['name']}\">{$title['title']}</a></li>\n";
print "</ul>
<p><a href=\"./\">Continue</a>.</p>
<style>
.linklike {
color: #339;
text-decoration: underline;
}
.linklike:hover {
cursor: pointer;