-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.php
2194 lines (1838 loc) · 75.5 KB
/
index.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
// TODO http://localhost/mod2/?url=https://lux.collections.yale.edu/data/place/cfda276e-fddd-4d58-aab5-25894ba991dd highlights a few new linked.art automatic formatting
// issues that would need to be captured.
// Added option to link subgraphs as nodes - needs diagram to be set as "flowchart" and not "graph"
// Added tests for hover text - it just uses a default example text just now
// Added the option of fixing the properties tags to the lines or letting them float - add "fix" after //Flowchart LR fix
$versions = array(
"jquery" => "3.7.0",
"bootstrap" => "5.3.1",
"mermaid" => "10.3.0", // 9.2.2 available but it breaks the zoom option, so would need to check.
"tether" => "2.0.0",
"pako" => "2.1.0",
"base64" => "3.7.5"
);
if (isset($_GET["debug"])) {}
if (isset($_GET["simple"])) {$simple = true;}
else {$simple = false;}
if (isset($_SERVER["SCRIPT_URI"]))
{$thisPage = $_SERVER["SCRIPT_URI"];}
else
{$thisPage = "./";}
$pako = false;
$diagram = "flowchart";
$fixlinks = false;
$orientation = "LR";
$default = file_get_contents("default.csv");
$config = getRemoteJsonDetails ("config.json", false, true);
$uniqueClasses = getRemoteJsonDetails ("unique_classes.json", false, true);
$examples = getRemoteJsonDetails ("examples.json", false, true);
$usedClasses = array();
$subGraphs = array();
$subGraphCount = 0;
$allClasses = formatClassDef ($config["format"]);
$bn_number = "0";
$usedFormats = array();
$jsonData = array();
$doc_example_links = array(
"LRNF" => array("TBNF", "LRF", "LR"),
"TBNF" => array("LRNF", "TBF", "TB"),
"LRF" => array("TBF", "LRNF", "LR fix"),
"TBF" => array("LRF", "TBNF", "TB fix"),
);
// Expects pako compressed data and pulls image directly from https://mermaid.ink
if (isset($_GET["image"]))
{getModelImage($_GET["image"]);
exit;}
// Default process of using the tool - receiving data from POST form.
else if (isset($_POST["triplesTxt"]) and $_POST["triplesTxt"])
{$triplesTxt = checkTriples ($_POST["triplesTxt"]);}
// Pulls in prepared data from local examples of defined files
// TODO - local data needs to be updated to pako compression
else if (isset($_GET["example"]) and isset($examples[$_GET["example"]]))
{
$ex = $examples[$_GET["example"]];
if (isset($ex["data"]))
{$triplesTxt = gzuncompress(base64_decode(urldecode($ex["data"])));}
else
{$triplesTxt = checkTriples (file_get_contents($ex["uri"]));}
if ($_GET["example"] == "object2")
{$triplesTxt = replaceFCDefIfExists ($triplesTxt, "//Flowchart LR fix");}
else if ($_GET["example"] == "documentation")
{$triplesTxt = docExampleTriples ($doc_example_links["LRNF"], $triplesTxt);}
}
// Used to ad additional format options to the default "instructions diagram
else if (isset($_GET["example"]) and isset($doc_example_links[$_GET["example"]]))
{$triplesTxt = docExampleTriples ($doc_example_links[$_GET["example"]]);}
// TODO works with an external data source
else if (isset($_GET["url"]))
{$fc = getRemoteURL ($_GET["url"]);
$triplesTxt = checkTriples ($fc);}
// TODO Need to update to allow data to be sent as pako compressed - three options
// 1: Duplicate pako JavaScript compression (used by MLE) in PHP
// 2: Call local Javascript function via Node JS to preform the compression
// 3: Move all data formatting to AJAX processes and make use of the default pako compression as needed.
// CURRENT PLAN is to follow option 3
else if (isset($_GET["data"]) and preg_match("/^[p][a][k][o][:](.+)$/", $_GET["data"], $m))
{
$triplesTxt = "Please wait tooltip Processing supplied data";
$pako = $m[1];//$_GET["data"];
}
else if (isset($_POST["triples"]))
{
$triples = getCleanTriples($_POST["triples"]);
$cleanTriplesTxt = implode("\n", $triples);
$raw = getRaw($triples);
$mermaid = Mermaid_formatData ($raw["test"]);
header('Content-Type: application/json');
header("Access-Control-Allow-Origin: *");
echo json_encode(array("triples" => $cleanTriplesTxt, "mermaid" => $mermaid));
exit;
}
// TODO simple PHP based compression option (URLs much longer) want to still have the option as a fall back
else if (isset($_GET["data"]))
{$triplesTxt = gzuncompress(base64_decode($_GET["data"]));}
// Default instructions diagram
else
{$triplesTxt = docExampleTriples ($doc_example_links["LRNF"]);}
// TODO Thumbnail display in diagram nodes might be possible with - but needs to be re-examined as it was not sorted
//
// O4 -- "crm:P48_has_preferred_identifier" -->O6[                        <img src='https://research.ng-london.org.uk/iiif/pics/tmp/raphael_pyr/N-1171/08_Images_of_Frames/raphael%20capitals%20right%20and%20left-PYR.tif/full/,125/0/default.jpg'/>                        ]
// The text " "s are required to get the box to be bigger - it results in a slide like display.
// in 9.1.4 it seems to work without the " "s
// O4 -- "crm:P48_has_preferred_identifier" -->O6[<img src='https://research.ng-london.org.uk/iiif/pics/tmp/raphael_pyr/N-1171/08_Images_of_Frames/raphael%20capitals%20right%20and%20left-PYR.tif/full/,125/0/default.jpg'/>];
// TODO move process into JavaScript with pako compression
$data = urlencode(base64_encode(gzcompress($triplesTxt)));
$bookmark = $thisPage.'?data='.$data;
// TODO move to JavaScript and AJAX processes.
$triples = getCleanTriples($triplesTxt);
$cleanTriplesTxt = implode("\n", $triples);
$raw = getRaw($triples);
$mermaid = Mermaid_formatData ($raw["test"]);
if ($simple)
{$html = buildPageSimple ($cleanTriplesTxt, $mermaid);}
else
{$html = buildPage ($cleanTriplesTxt, $mermaid);}
echo $html;
exit;
////////////////////////////////////////////////////////////////////////
function replaceFCDefIfExists ($string, $newline=false)
{
$lines = explode("\n", $string, 2); // Split into an array with at most 2 elements
if (isset($lines[0]) && preg_match('/^\/\/flowchart.+$/', strtolower($lines[0]))) {
$replacement = "New first line";
$newString = $newline . "\n" . (isset($lines[1]) ? $lines[1] : '');
return ($newString);
}
else
{return ($newline . "\n" . $string);}
}
function docExampleTriples ($ex, $use=false)
{
global $default;
if(!$use) {$use = $default;}
$layout_comments = array(
"TB" => array ("Flowchart TB", "In addition to the default left-right (LR) orientation diagrams can also be arranged from the top-bottom (TB)"),
"LR" => array ("Flowchart LR", "In addition to the optional top-bottom (TB) orientation diagrams can also be arranged with the default Left-Right (LR) orientation"),
"F" => array ("Fixed Properties", "This format extends and straightens the lines linking the various concepts together to ensure there is a flat section of the line for the link property to be specifically fixed to. This can result in a larger overall diagram, but can be required when there are higher numbers of property links being displayed together"),
"NF" => array ("Relaxed Properties", "This format curves the lines linking the various concepts together to minimise the size of the generated diagram.")
);
$triplesTxt = "//Flowchart $ex[2] \n".checkTriples ($use);
$do = str_split($ex[0], 2);
$triplesTxt .= "\nDynamic Modeller\tcan be formatted with\t".$layout_comments[$do[0]][0]."|https://research.ng-london.org.uk/modelling/?example=$ex[0]";
$triplesTxt .= "\n".$layout_comments[$do[0]][0]."\thas comment\t".json_encode($layout_comments[$do[0]][1]);
$do = str_split($ex[1], 2);
$triplesTxt .= "\nDynamic Modeller\tcan be formatted with\t".$layout_comments[$do[1]][0]."|https://research.ng-london.org.uk/modelling/?example=$ex[1]";
$triplesTxt .= "\n".$layout_comments[$do[1]][0]."\thas comment\t".json_encode($layout_comments[$do[1]][1]);
return ($triplesTxt);
}
function buildExamplesDD ()
{
global $examples;
ob_start();
echo <<<END
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="dropdownMenuExamples" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Examples
</a>
<div class="dropdown-menu dropdown-menu-end" aria-labelledby="dropdownMenuExamples">
END;
$html = ob_get_contents();
ob_end_clean(); // Don't send output to client
foreach ($examples as $k => $a)
{$html .= "<a class=\"dropdown-item\" href=\"./?example=$k\">$a[title]</a>\n";}
$html .= "</div></li>";
return ($html);
}
function buildLinksDD ()
{
global $bookmark;
$date = date('Y-m-d_H-i-s');
ob_start(); //style="margin-right: 8px; float:right; margin-bottom: 16px;"
//
echo <<<END
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="dropdownMenuLinks" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Links
</a>
<div class="dropdown-menu dropdown-menu-end" aria-labelledby="dropdownMenuLinks">
<a class="dropdown-item" id="downloadLink" title="Mermaid Get PNG" href="" download="model_$date.png">Download Image</a>
<!-- <a class="dropdown-item" title="Bookmark Link" href="$bookmark" target="_blank">Bookmark Link</a> -->
<a class="dropdown-item" id="bookmark" title="Bookmark Link" href="" target="_blank">Bookmark Link</a>
<a class="dropdown-item" id="mermaidLink" title="Edit further in the Mermaid Live Editor" href="" target="_blank">Mermaid Editor</a>
<a class="dropdown-item" id="mermaidCode" title="Copy Mermaid Code to Clipboard" onclick="copyMermaid()" href="">Mermaid Code</a>
END;
$html = ob_get_contents();
ob_end_clean(); // Don't send output to client
$html .= "</div></li>";
return ($html);
}
function debugJsonConversaion ($json, $php, $triples)
{
global $examples;
$php = print_r($php, true);
ob_start();
echo <<<END
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid" style="padding:0px;">
<div class="container-fluid" style="padding:0px;">
<div class="row" style="padding:0px;margin:0px;">
<div class="col-sm-4" style="padding:0px;height:98vh;background-color:white;">
<pre style="height:100%;overflow:scroll;">$json</pre></div>
<div class="col-sm-4" style="padding:0px;height:98vh;background-color:#efefef;">
<pre style="height:100%;overflow:scroll;">$php</pre></div>
<div class="col-sm-4" style="padding:0px;height:98vh;background-color:white;">
<pre style="height:100%;overflow:scroll;">$triples</pre></div>
</div>
<br>
</div>
</div>
</body>
</html>
END;
$html = ob_get_contents();
ob_end_clean(); // Don't send output to client
echo $html;
exit;
}
function buildPage ($triplesTxt, $mermaid)
{
global $thisPage, $versions, $pako;
$exms = buildExamplesDD ();
$links = buildLinksDD ();
$modal = buildModal ();
$code = array(
"code" => $mermaid,
"mermaid" => array(
"theme" => "default",
//"securityLevel" => "loose", This option forces an alert in the live editor
"logLevel" => "warn",
"flowchart" => array(
"curve" => "basis",
"htmlLabels" => true)
));
$json_code = json_encode($code);
$bw = "26px";
$vs[0] = $versions["bootstrap"];
$vs[1] = $versions["jquery"];
$vs[2] = $versions["bootstrap"];
$vs[3] = $versions["mermaid"];
$vs[4] = $versions["tether"];
$vs[5] = $versions["pako"];
$vs[6] = $versions["base64"];
$jslib = "https://unpkg.com";
$jslib = "https://cdn.jsdelivr.net/npm";
ob_start();
echo <<<END
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta charset="utf-8">
<title>Dynamic Simple Modelling</title>
<link href="$jslib/bootstrap@$vs[0]/dist/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<link href="https://cdn.jsdelivr.net/npm/@fortawesome/[email protected]/css/all.min.css" rel="stylesheet" type="text/css">
<link href="css/local.css" rel="stylesheet" type="text/css">
<style>
/* Added to get the hover texts or tooltips to appear and be formatted.
based on values in https://unpkg.com/browse/[email protected]/dist/mermaid.css */
div.mermaidTooltip {
position: absolute;
text-align: center;
max-width: 300px;
padding: 5px;
font-family: 'trebuchet ms', verdana, arial;
font-size: 1rem;
background: #ffffde;
border: 1px solid #aaaa33;
border-radius: 5px;
pointer-events: none;
z-index: 10000;
}
</style>
</head>
<body>
<div id="page" class="container-fluid">
<div class="d-flex flex-column mb-3 vh-100">
<!-- LEVEL 1 -->
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a title="GitHub Dynamic Modelling" href="https://github.com/jpadfield/dynamic-modelling" target="_blank" class="imbutton" style="float:right;" >
<img alt="GitHub Logo" aria-label="GitHub Logo" src="graphics/GitHub-Mark-64px.png" style="margin-left:10px;" width="32" /></a>
<h1 class="navbar-brand" style="font-size:1.5rem;margin:0px 16px 0px 16px;">Simple Dynamic Modelling</h1>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span></button>
<div class="collapse navbar-collapse float-end" id="navbarSupportedContent">
<span class="navbar-text w-100">
<ul class="navbar-nav ml-auto float-end">
$exms
$links
<li class="nav-item">
<a href="#myModal" data-bs-toggle="modal" data-bs-target="#helpModalCenter" class="nav-link me-4">Info</a></li>
</ul>
</span>
</div>
</nav> <!-- CLOSE LEVEL 1 -->
<!-- LEVEL 2 -->
<div class="" style="" role="region" >
<form id="triplesFrom" action="$thisPage" method="post">
<div id="textholder" class="textareadiv form-group flex-grow-1 d-flex flex-column">
<textarea class="form-control flex-grow-1 rounded-0 detectTab" id="triplesTxt" name="triplesTxt" style="overflow-y:scroll;" aria-label="Textarea for triples" rows="10">$triplesTxt</textarea>
<div class="tbtns" style="">
<button title="Refresh Model" class="btn btn-default textbtn" id="refreshM" type="submit" aria-label="Refresh Model"><img aria-label="Refresh Model" alt="Refresh Model" src="graphics/view-refresh.png" width="$bw" /></button>
<button title="Clear Text" class="btn btn-default textbtn" id="clear" type="button" aria-label="Clear Textarea"><img aria-label="Clear Text" alt="Clear Text" src="graphics/clear-text.png" width="$bw" /></button>
<button title="Help" class="btn btn-default textbtn" id="help" type="button" data-bs-toggle="modal" data-bs-target="#helpModalCenter" aria-label="Open Help Modal"><img alt="Help" aria-label="Help" src="graphics/help.png" width="$bw" /></button>
<button title="Toggle Text Fullscreen" class="btn btn-default textbtn" id="tfs" type="button" aria-label="Toggle Textarea Full-screen" onclick="togglefullscreen('tfs', 'textholder')"><img alt="Toggle Fullscreen" aria-label="Toggle Fullscreen" src="graphics/view-fullscreen.png" width="$bw" /></button>
</div>
</div>
</form>
</div><!-- CLOSE LEVEL 2 -->
<!-- LEVEL 3 -->
<div role="main" aria-label="Holder for the actual flow diagram model" id="holder" class="flex-grow-1 moddiv">
<div class="tbtns" style="">
<div class="form-check form-switch">
<input title="Toggle Pan & Zoom function" class="form-check-input" type="checkbox" role="switch" id="zoom-toggle" style="margin-right:0.5em; margin-bottom:2px; margin-top:8px; width:3em; height:1.5em;" onclick="modelZoom()">
<button title="Toggle Model Fullscreen" class="btn btn-default nav-button textbtn" id="fs" aria-label="Toggle Model Full-screen" style="top:0px;left:0px;" onclick="togglefullscreen('fs', 'holder')"><img alt="Toggle Fullscreen" aria-label="Toggle Fullscreen" src="graphics/view-fullscreen.png" width="$bw" /></button>
</div>
</div>
<!-- <div style="overflow: hidden; height: 100%;" tabindex=0> -->
<div id="modelDiv" style="height:100%" class="mermaid">$mermaid</div>
<div id="modelDivTxt" style="display:none">$mermaid</div>
<!-- </div> -->
</div><!-- CLOSE LEVEL 3 -->
</div><!-- CLOSE FLEX DIV -->
$modal
</div><!-- CLOSE PAGE -->
<script src="$jslib/jquery@$vs[1]/dist/jquery.min.js"></script>
<script src="$jslib/tether@$vs[4]/dist/js/tether.min.js"></script>
<script src="$jslib/bootstrap@$vs[2]/dist/js/bootstrap.bundle.min.js"></script>
<!-- <script src="$jslib/mermaid@$vs[3]/dist/mermaid.min.js"></script> -->
<script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';
let config = {
maxTextSize: 900000,
startOnLoad:true,
securityLevel: "loose",
logLevel: 4,
flowchart: { curve: 'basis', useMaxWidth: false, htmlLabels: true },
mermaid: {
callback:function(id) {modelZoom ()}
}}
mermaid.initialize(config);
</script>
<script src="$jslib/pako@$vs[5]/dist/pako.min.js"></script>
<script src="$jslib/js-base64@$vs[6]/base64.min.js"></script>
<script src="./js/svg-pan-zoom.js" crossorigin="anonymous"></script>
<script src="./js/local.js"></script>
<script>
let code = JSON.stringify($json_code);
let pcode = '$pako';
</script>
</body>
</html>
END;
$html = ob_get_contents();
ob_end_clean(); // Don't send output to client
return($html);
}
function buildPageSimple ($triplesTxt, $mermaid)
{
global $thisPage, $versions, $pako;
$exms = buildExamplesDD ();
$links = buildLinksDD ();
$modal = buildModal ();
$code = array(
"code" => $mermaid,
"mermaid" => array(
"theme" => "default",
//"securityLevel" => "loose", This option forces an alert in the live editor
"logLevel" => "warn",
"flowchart" => array(
"curve" => "basis",
"htmlLabels" => true)
));
$json_code = json_encode($code);
$bw = "26px";
$vs[0] = $versions["bootstrap"];
$vs[1] = $versions["jquery"];
$vs[2] = $versions["bootstrap"];
$vs[3] = $versions["mermaid"];
$vs[4] = $versions["tether"];
$vs[5] = $versions["pako"];
$vs[6] = $versions["base64"];
$jslib = "https://unpkg.com";
$jslib = "https://cdn.jsdelivr.net/npm";
ob_start();
echo <<<END
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta charset="utf-8">
<title>Dynamic Simple Modelling</title>
<link href="$jslib/bootstrap@$vs[0]/dist/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<link href="https://cdn.jsdelivr.net/npm/@fortawesome/[email protected]/css/all.min.css" rel="stylesheet" type="text/css">
<link href="css/local.css" rel="stylesheet" type="text/css">
<style>
/* Added to get the hover texts or tooltips to appear and be formatted.
based on values in https://unpkg.com/browse/[email protected]/dist/mermaid.css */
div.mermaidTooltip {
position: absolute;
text-align: center;
max-width: 300px;
padding: 5px;
font-family: 'trebuchet ms', verdana, arial;
font-size: 1rem;
background: #ffffde;
border: 1px solid #aaaa33;
border-radius: 5px;
pointer-events: none;
z-index: 10000;
}
</style>
</head>
<body class="vh-100" style="overflow:hidden" >
<div class="tbtns" style="">
<div class="form-check form-switch">
<input title="Toggle Pan & Zoom function" class="form-check-input" type="checkbox" role="switch" id="zoom-toggle" style="margin-right:0.5em; margin-bottom:2px; margin-top:8px; width:3em; height:1.5em;" onclick="modelZoom()">
</div>
</div>
<div id="modelDiv" style="height:100%" class="mermaid">$mermaid</div>
<div id="modelDivTxt" style="display:none">$mermaid</div>
<script src="$jslib/jquery@$vs[1]/dist/jquery.min.js"></script>
<script src="$jslib/tether@$vs[4]/dist/js/tether.min.js"></script>
<script src="$jslib/bootstrap@$vs[2]/dist/js/bootstrap.bundle.min.js"></script>
<!-- <script src="$jslib/mermaid@$vs[3]/dist/mermaid.min.js"></script> -->
<script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';
let config = {
maxTextSize: 900000,
startOnLoad:true,
securityLevel: "loose",
logLevel: 4,
flowchart: { curve: 'basis', useMaxWidth: false, htmlLabels: true },
mermaid: {
callback:function(id) {modelZoom ()}
}}
mermaid.initialize(config);
</script>
<script src="$jslib/pako@$vs[5]/dist/pako.min.js"></script>
<script src="$jslib/js-base64@$vs[6]/base64.min.js"></script>
<script src="./js/svg-pan-zoom.js" crossorigin="anonymous"></script>
<script src="./js/local.js"></script>
<script>
let code = JSON.stringify($json_code);
let pcode = '$pako';
</script>
</body>
</html>
END;
$html = ob_get_contents();
ob_end_clean(); // Don't send output to client
return($html);
}
function buildModal ()
{
// Based on https://bbbootstrap.com/snippets/modal-multiple-tabs-89860645
$tabs = array(
"Information" => 'This is an interactive live modelling system which can automatically convert simple <b>tab</b> separated triples or JSON-LD (experimental) into graphical models and flow diagrams using the <a href="https://mermaid-js.github.io/">Mermaid Javascript library</a>. It has been designed to be very simple to use. The tab separated triples can be typed directly into the web-page, but users can also work and prepare data in three (or four columns if applying formatting) of a online spreadsheet and then just copy the relevant columns and paste them directly into the data entry text box.<br/><br/>In general the tool makes use of a simple set of predefined formats for the flow diagrams, taken from the Mermaid library, but a <a href="?example=example_formats">series of additional predefined formats</a> have also be provided and can be defined as a fourth "triple".<br/><br/>The <a href="./">default landing page</a> presents an example set or data, and the generated model demonstrates the functionality provided. As a new user it is recommended that you try editing this data to see how the diagrams are built. Additional examples are also available via the <b>Examples</b> menu option in the upper right.<br/><br/> The system has also be defined to allow models to be shared via automatically generate, and often quite long, URLs. This can be accessed via the <b>Links</b> menu option, as the <b>Bookmark Link</b>. A static image version of any given model can be saved by following the <b>Download Image</b> option and using the tools provide by the <a href="https://mermaid.ink/">Mermaid Ink</a> system. It is also possible to further edit a model using the full options of the Mermaid library using the <a href="https://mermaid-js.github.io/mermaid-live-editor">Mermaid Live Editor</a>, via the <b>Mermaid Editor</b> link.
<br/><br/>
<h5>Acknowledgements:</h5>
This tool was originally developed within the National Gallery, but its continue development and public presentation has also been supported by:
<br/><br/>
<h6></a>The H2020 <a href="https://www.iperionhs.eu/" rel="nofollow">IPERION-HS</a> project</h6>
<p dir="auto"><a href="https://www.iperionhs.eu/" rel="nofollow"><img height="42px" src="./graphics/IPERION-HS%20Logo.png" alt="IPERION-HS" style="max-width: 100%;"></a>
<a href="https://www.iperionhs.eu/" rel="nofollow"><img height="32px" src="./graphics/iperionhs-eu-tag2.png" alt="IPERION-HS" style="max-width: 100%;"></a></p>
<br/>
<h6>The H2020 <a href="https://sshopencloud.eu/" rel="nofollow">SSHOC</a> project</h6>
<p><a href="https://sshopencloud.eu/" rel="nofollow"><img height="48px" src="./graphics/sshoc-logo.png" alt="SSHOC" style="max-width: 100%;"></a>
<a href="https://sshopencloud.eu/" rel="nofollow"><img height="32px" src="./graphics/sshoc-eu-tag2.png" alt="SSHOC" style="max-width: 100%;"></a></p>
<br/>
<h6>The AHRC Funded <a href="https://linked.art/" rel="nofollow">Linked.Art</a> project</h6>
<p><a href="https://ahrc.ukri.org/" rel="nofollow"><img height="48px" src="./graphics/UKRI_AHR_Council-Logo_Horiz-RGB.png" alt="Linked.Art" style="max-width: 100%;"></a></p>',
//"Blank Nodes" => 'Details to be added',
//"Formatting" => 'Details to be added',
//"Aliases" => 'Details to be added'
);
$tabHeaders = false;
$tabContents = false;
$no = 1;
$active = "active";
foreach ($tabs as $k => $ht)
{
$dno = sprintf('%02d', $no);
$tabHeaders .= "
<li class=\"nav-item\">
<a href=\"#tab$dno\" class=\"nav-link $active\" data-bs-toggle=\"tab\">$k</a>
</li>";
$tabContents .= "
<div class=\"tab-pane fade show $active\" id=\"tab$dno\">
<h5 class=\"text-center mb-4 mt-0 pt-4\">$k</h5>
<div class=\"m-4\">$ht</div>
</div>";
$active = "";
$no++;
}
ob_start();
echo <<<END
<!-- Modal-->
<div id="helpModalCenter" tabindex="-1" role="dialog" aria-label="Help Modal" aria-hidden="true" class="modal fade text-left">
<div role="document" class="modal-dialog modal-lg modal-dialog-centered">
<div class="modal-content">
<ul class="nav nav-tabs" id="myTab">
$tabHeaders
</ul>
<div class="tab-content">
$tabContents
</div>
<div class="line"></div>
<div class="modal-footer d-flex flex-column justify-content-center border-0">
<p class="text-muted">More questions or issues? - <a href="https://github.com/jpadfield/dynamic-modelling/issues">Try Github</a>.</p>
</div>
</div>
</div>
</div>
END;
$html = ob_get_contents();
ob_end_clean(); // Don't send output to client
return ($html);
}
function buildModalDefault()
{
// Based on https://bbbootstrap.com/snippets/modal-multiple-tabs-89860645
$tabs = array(
"My Apps" => ' <h5 class="text-center mb-4 mt-0 pt-4">My Apps</h5>
<h6 class="px-3">Most Used Apps</h6>
<ol class="pb-4">
<li>Watsapp</li>
<li>Instagram</li>
<li>Chrome</li>
<li>Linkedin</li>
</ol>
</div>
<div class="px-3">
<h6 class="pt-3 pb-3 mb-4 border-bottom"><span class="fa fa-android"></span> Suggested Apps</h6>
<h6 class="text-primary pb-2"><a href="#">Opera Browser</a> <span class="text-secondary">- One of the best browsers</span></h6>
<h6 class="text-primary pb-2"><a href="#">Camscanner</a> <span class="text-secondary">- Easily scan your documents</span></h6>
<h6 class="text-primary pb-4"><a href="#">Coursera</a> <span class="text-secondary">- Learn online, lecturers from top universities</span></h6>',
"Knowledge Center" => '<h5 class="text-center mb-4 mt-0 pt-4">Knowledge Center</h5>
<form>
<div class="form-group pb-5 px-3"> <select name="account" class="form-control">
<option selected disabled>Select Product</option>
<option>Product 1</option>
<option>Product 2</option>
<option>Product 3</option>
<option>Product 4</option>
</select> </div>
</form>
</div>
<div class="px-3">
<h6 class="pt-3 pb-3 mb-4 border-bottom"><span class="fa fa-star"></span> Popular Topics</h6>
<h6 class="text-primary pb-2"><a href="#">Getting started with Blazemeter</a></h6>
<h6 class="text-primary pb-2"><a href="#">Creating tests</a></h6>
<h6 class="text-primary pb-4"><a href="#">Running tests</a></h6>',
"Communities" => ' <h5 class="text-center mb-4 mt-0 pt-4">Communities</h5>
<form>
<div class="form-group pb-5 px-3 row justify-content-center"> <button type="button" class="btn btn-primary">New Community +</button> </div>
</form>
</div>
<div class="px-3">
<div class="border border-1 box">
<h5>Community 1</h5>
<p class="text-muted mb-1">Members : <strong>27</strong></p>
</div>
<div class="border border-1 box">
<h5>Community 2</h5>
<p class="text-muted mb-1">Members : <strong>16</strong></p>
</div>',
"Education" => ' <h5 class="text-center mb-4 mt-0 pt-4">Education</h5>
<form>
<div class="form-group pb-2 px-3"> <input type="text" placeholder="Enter College Name" class="form-control"> </div>
<div class="form-group row pb-2 px-3">
<div class="col-6"> <input type="text" placeholder="Percentage" class="form-control"> </div>
<div class="col-6"> <input type="text" placeholder="Grade" class="form-control"> </div>
</div>
<div class="form-group px-3 pb-2"> <label class="form-control-label">
<h6>What are you good at ?</h6>
</label>
<div class="custom-control custom-checkbox"> <input class="custom-control-input" id="option1" type="checkbox" value=""> <label class="custom-control-label" for="option1">Web Development</label> </div>
<div class="custom-control custom-checkbox"> <input class="custom-control-input" id="option2" type="checkbox" value=""> <label class="custom-control-label" for="option2">Data Structures & Algorithms</label> </div>
<div class="custom-control custom-checkbox"> <input class="custom-control-input" id="option3" type="checkbox" value=""> <label class="custom-control-label" for="option3">Android Development</label> </div>
<div class="custom-control custom-checkbox"> <input class="custom-control-input" id="option4" type="checkbox" value=""> <label class="custom-control-label" for="option4">Blockchain</label> </div>
<div class="custom-control custom-checkbox"> <input class="custom-control-input" id="option5" type="checkbox" value=""> <label class="custom-control-label" for="option5">Machine Learning Algorithms</label> </div>
</div>
<div class="form-group pb-5 row justify-content-center"> <button type="button" class="btn btn-primary px-3">Submit</button> </div>
</form>
</div>
<div class="px-3">
<h6 class="pt-3 pb-3 mb-4 border-bottom"><span class="fa fa-rocket"></span> Trending Technologies</h6>
<h6 class="text-primary pb-2"><a href="#">Augmented Reality and Virtual Reality</a></h6>
<h6 class="text-primary pb-2"><a href="#">Angular and React</a></h6>
<h6 class="text-primary pb-2"><a href="#">Big Data and Hadoop</a></h6>
<h6 class="text-primary pb-4"><a href="#">Internet of Things (IoT)</a></h6>',
);
$tHeaders = false;
$tFields = false;
$at = " active";
$sh = "show";
$tc = "font-weight-bold";
$no = 1;
foreach ($tabs as $k => $ht)
{
$dno = sprintf('%02d', $no);
$tHeaders .= "
<div class=\"tabs$at\" id=\"tab$dno\">".
"<h6 class=\"$tc\">$k</h6></div>";
$tFields .= "
<fieldset id=\"tab${dno}1\" class=\"$sh\"><div class=\"bg-light\">
$ht
</div></fieldset>";
$at = "";
$sh = "";
$tc = "text-muted";
$no++;
}
ob_start();
echo <<<END
<!-- Modal-->
<div id="helpModalCenter" tabindex="-1" role="dialog" aria-labelledby="helpModalCenterTitle" aria-hidden="true" class="modal fade text-left">
<div role="document" class="modal-dialog modal-lg modal-dialog-centered">
<div class="modal-content">
<!-- Tab headers, numbered from tab01 -> tab0n, etc -->
<div class="modal-header row d-flex justify-content-between mx-1 mx-sm-3 mb-0 pb-0 border-0">
$tHeaders
</div>
<div class="line"></div>
<!-- Tab Contents, numbered from tab011 -> tab0n1, etc -->
<div class="modal-body p-0">
$tFields
</div>
<div class="line"></div>
<div class="modal-footer d-flex flex-column justify-content-center border-0">
<p class="text-muted">Can't find what you're looking for?</p> <button type="button" class="btn btn-primary">Contact Support Team</button>
</div>
</div>
</div>
</div>
END;
$html = ob_get_contents();
ob_end_clean(); // Don't send output to client
return ($html);
}
function getCleanTriples($triplesTxt)
{
$lastLine = 0;
$cleanData = array();
$data = explode("\n", $triplesTxt);
foreach ($data as $k => $line)
{
if (preg_match("/^.+\t.+\t.+$/", $line, $m))
{$trip = explode ("\t", $line);}
else if (preg_match("/^.+[,].+[,].+$/", $line, $m))
{$trip = explode (",", $line);}
else
{$trip = array($line);}
$trip = array_map('trim', $trip);
// Starting things with @ can upset mermaid
foreach ($trip as $tk => $tv)
{
if (preg_match("/^[\@](.+$)/", $tv, $m))
{$tv = $m[1];}
$trip[$tk] = parseEntities($tv);
}
//only consider the first 4 values - removes spaces coming from spreadsheets
$trip = array_slice($trip, 0, 4);
// Considered as a data line
if ($trip[0])
{$lastLine = $k;}
// Allow gaps of up to two lines between blocks of triples and remove others.
if ($k <= $lastLine + 2)
{$cleanData[] = implode("\t", $trip);}
}
return ($cleanData);
}
function getRaw($data)
{
global $orientation, $config, $fixlinks, $diagram, $subGraphs, $things,$subGraphCount;
$au = $config["unique"]["regex"];
$output = array();
$no = 0;
$bn = 0;
$tn = 0;
$ono = 0;
$bnew = false;
$bba = array();
$bbano = 1;
$tag = "test";
$output[$tag]["model"] = $tag;
$output[$tag]["comment"] = ucfirst ($tag)." Model";
$output[$tag]["count"] = 0;
$output[$tag]["objects"] = array();
//pair rdf:type and crm:p2_has_type "objects"
$typeObjects = array();
foreach ($data as $k => $line)
{
if (preg_match("/^.+\t.+\t.+$/", $line, $m))
{$trip = explode ("\t", $line);}
else if (preg_match("/^.+[,].+[,].+$/", $line, $m))
{$trip = explode (",", $line);}
else
{$trip = array($line);}
$trip = array_map('trim', $trip);
// Starting things with @ can upset mermaid
foreach ($trip as $tk => $tv)
{if (preg_match("/^[\@](.+$)/", $tv, $m))
{$trip[$tk] = $m[1];}}
$trip["bn"] = false; //used to flag new blank nodes and possibly other formatting controls
$trip["type"] = false; //used to flag new blank nodes and possibly other formatting controls
// Increment triple number
$tn++;
if(preg_match("/^[\/][\/][ ]Model[:][\s]*([a-zA-Z0-9 ]+)[\s]*[\/][\/](.+)$/", $line, $m))
{$output[$tag]["comment"] = $m[2];}
else if((preg_match("/^[\/][\/][ ]*[gG]raph[ ]*([LT][BR])(.*)$/", $line, $m)) or
(preg_match("/^[\/][\/][ ]*[gG]raph[ ]*([LT][BR])(.*)$/", $trip[0], $m)))
{$orientation = $m[1];
$diagram = "graph";
if (strtolower(trim($m[2])) == "fix") {$fixlinks = true;}
$trip = array($line);}
else if((preg_match("/^[\/][\/][ ]*[fF]lowchart[ ]*([LT][BR])(.*)$/", $line, $m)) or
(preg_match("/^[\/][\/][ ]*[fF]lowchart[ ]*([LT][BR])(.*)$/", $trip[0], $m)))
{$orientation = $m[1];
$diagram = "flowchart";
if (strtolower(trim($m[2])) == "fix") {$fixlinks = true;}
$trip = array($line);}
else if(preg_match("/^[\/][\/][ ]*[sS][uU][bB][gG][Rr][Aa][Pp][Hh][ ]*(.*)$/", $line, $m))
{
$subGraphCount++;
$sgdts = array();
$sg = trim ($m[1]);
if (preg_match("/^[-]([A-Z][A-Z])(.+)$/", $sg, $sm))
{$sg = trim ($sm[2]);
$sgDir = $sm[1];}
else
{$sgDir = false;}
if (preg_match("/^[\"][\/][\/](.+)[\"]$/", $sg, $sm))
{$sgdts["id"] = str_replace(' ', "", $sm[1]);
$sgdts["lab"] = "[\" \"]";}
else if (preg_match("/^[\/][\/](.+)$/", $sg, $sm))
{$sgdts["id"] = str_replace(' ', "", $sm[1]);
$sgdts["lab"] = "[\" \"]";}
else if (!$sg)
{$sgdts["id"] = "sgID-".$subGraphCount;
$sgdts["lab"] = "[\" \"]";
$sg = "//".$sgdts["id"];}
else
{$sgdts["id"] = str_replace(' ', "", $sg);
$sgdts["lab"] = "[\"$sg\"]";}
$subGraphs[$sg] = $sgdts;
$things[$sg] = $sgdts["id"];
$trip = array("subgraph", $sg, $sgDir);
}
else if(preg_match("/^[\/][\/][ ]*[eE][nN][dD][ ]*(.*)$/", $line, $m))
{$trip = array("end", $m[1], "");}
// ignore lines that are commented out
else if(preg_match("/^[\/#][\/#].*$/", $line, $m))
{$trip = array($line);}
// Ignore notes, empty lines or commented lines
if (isset($trip[2]))
{
if (in_array(strtolower($trip[0]), array("_blank node", "_bn")))
{$bnd = true;
$trip["bn"] = true;}
else
{$bnd = false;}
$typeCheck = preg_replace('/[. ]/', "_", strtolower($trip[1]));
//echo "<!-- $typeCheck -->\n";
// Defining a thing as have type "Type" is a special case so the "Type" is left as a literal by default
if (in_array ($typeCheck, array(
"crm:p2_has_type", "has_type", "type", "rdf:type", "classified_as")) and strtolower($trip[2]) != "type")
{$pt = true;
$trip["type"] = true;}
else
{$pt = false;}
// Ensure subsequent Blank Nodes are seen as new.
if ( $bnd and $pt and !$bnew) {
$bn++;
$bnew=true;}
// Flag as not a new blank node after listing other predicates or typing something else
else if ((!$bnd and $pt) or !$pt) {
$bnew=false;}
// Number each blank node to make it unique
if ($bnd)
{$trip[0] = $trip[0]."-N".$bn;}
// Catching reference to a previous blank node
else if (preg_match("/^(_[bB][a-z]*[ ]*[Nn][a-z]*)[-]([0-9]+)$/", $trip[0], $m))
{$trip[0] = "$m[1]-N".($bn-$m[2]);}
// Current process is assuming that the subject and the object can not both be a new Blank Nodes
if (in_array(strtolower($trip[2]), array("_blank node", "_bn")))
{$trip[2] = $trip[2]."-N".$bn;
$bnew=false;}
else if (preg_match("/^(_[bB][a-z]*[ ]*[Nn][a-z]*)[-]([0-9]+)$/", $trip[2], $m))
{$trip[2] = $m[1]."-N".($bn-$m[2]);}
// Number the predicates so they are all unique
// NOT REQUIRED
//$trip[1] = $trip[1]."-N".$tn;
// Ensure that all refs to listed unique classes are unique so the diagram
// does not Overlap too much - only parsing "subjects"
foreach ($au as $rxk => $rxv)
{
if(preg_match("/^$rxv$/", $trip[2], $m))
{
$check = strtolower($trip[0]."-".$trip[2]);
if (isset($typeObjects[$check]))
{$trip[2] = $typeObjects[$check];}//$trip[2]."-". $tn;}
else
{$typeObjects[$check] = $trip[2]."-". $tn;
$trip[2] = $trip[2]."-". $tn;}
break 1;
}
}
// list unique "objects"
if (!in_array($trip[0], $output[$tag]["objects"]))
{$output[$tag]["objects"][] = $trip[0];}
$output[$tag]["triples"][] = $trip;
$output[$tag]["count"]++;
}
else //Empty lines will force a new Blank node to be considered
{$bnew=false;}
if ($trip[0] == "// Stop") // For debugging
{break;}
}
return ($output);
}
function formatClassDef ($formats)
{
$allClasses = array();
$classDef = false;