-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathphpdialog.php
939 lines (855 loc) · 25.5 KB
/
phpdialog.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
<?php
/**
*
* _
* /' `\ /' /'
* /' ._)--/'-- --/'--
* (____ /' ____ ,__________ ____ /' ____
* ) /' /' ) /' ) ) /' )-- /' /' )
* /'/' /' /' /' /' /' /' /' /' /(___,/'
*(_____,/' (__(___,/(__/' /' /(__/(___,/' (__ O(________ O
* /'
* /'
* /'
*
* ---------------------------------------------------------------------------
* Stamp t.e.
* The Beautiful Template Engine
* @author Gabor de Mooij
* @version 2.3.4
* @copyright 2019
* @license New BSD License
* ---------------------------------------------------------------------------
*/
namespace StampTemplateEngine {
class StampTE
{
/**
* Clear white space gaps left by
* paste markers or not?
*/
protected static $clearws = TRUE;
/**
* HTML5 Document template cache.
* @var string
*/
protected static $html5Tpl = NULL;
/**
* Holds the template
* @var string
*/
protected $template;
/**
* Collection of initial matches from template
* @var array
*/
protected $matches;
/**
* Processed array of HTML parts found in template,
* keyed by IDs.
* @var array
*/
protected $catalogue;
/**
* Identifier of current template snippet.
* @var string
*/
protected $id;
/**
* A Stamp contains a sketchbook with snippets to generate new
* stamps from. This way StampTE allows lazy initialization of
* new Stamps as soon as they are fetched using the get() command.
*
* @var array
*/
protected $sketchBook = array();
/**
* List of slots in the current Stamp.
* Mainly for introspection by smart template processors.
*
* @var array
*/
protected $slots = array();
/**
* Selector ID. The currently selected Glue Point.
* A Glue Point can be selected using a magic getter.
* The ID for this magically selected Glue Point is stored in this
* variable.
*
* @var string
*/
protected $select = NULL;
/**
* Cache array. Cache keeps the planet from burning up.
*
* @var array
*/
protected $cache = array();
/**
* Holds the translator function to be used for
* translations.
*
* @var closure
*/
protected $translator = NULL;
/**
* Holds the factory function to be used whenever
* a Stamp template is returned.
*
* @var closure
*/
protected $factory = NULL;
/**
* Sets the white space clearing mechanism.
* TRUE means: clear gaps caused by replaced paste markers.
* FALSE means: leave gaps (faster).
* Default is TRUE.
*
* @param boolean $trueOrFalse preferred method
*
* @return void
*/
public static function setClearWS( $clearWhiteSpace )
{
self::$clearws = (boolean) $clearWhiteSpace;
}
/**
* Returns a StampTE template configured with a proper
* HTML 5 document using UTF-8 correct encoding (secure with
* default XSS escaping features).
*
* This default template contains two cut markers: link and script,
* two glue points: head and body and one slot: title.
*
* Usage example:
*
* <code>
* $tpl = StampTE::createHtml5Utf8Document();
* $tpl->setTitle('Welcome to StampTE'); //set the title.
* $tpl->head->add( $linkTag ); //Add stylesheets and scripts!
* $tpl->body->add( $myDocument ); //Add your body content!
* </code>
*
* @return StampTE template
*/
public static function createHtml5Utf8Document()
{
return self::fromFile( __dir__ . '/html5document.html' );
}
/**
* Returns a Stamp instance using the contents of the specified
* file.
*
* @param string $fname path to file to read
*
* @return StampTE
*/
public static function fromFile( $fname )
{
return new self( file_get_contents( $fname ) );
}
/**
* Constructor. Pass nothing if you plan to use cache.
* Creates a new Stamp object from a string.
* Upon constructing a Stamp object the string will be parsed.
* All cut points, i.e. <!-- cut:X -->Y<!-- /cut:X --> will
* be collected and stored in the internal 'sketchbook'.
* They can now be used as HTML snippets like this:
*
* <code>
* $stpl = new StampTE( $tpl );
* $stpl->getX(); //where X is cut point
* </code>
*
* @param string $tpl HTML Template
* @param string $id identification string for this template
*/
public function __construct( $tpl='', $id='root' )
{
if ( is_null( $tpl ) ) $tpl = '';
if ( is_object( $tpl ) && !method_exists( $tpl, '__toString' ) ) $tpl = '['.get_class( $tpl ).' instance]';
$tpl = (string) $tpl;
$this->id = strval( $id );
$this->template = $tpl;
$this->matches = array();
$pattern = '/\s*<!\-\-\s(cut|slot):(\S+)\s\-\->(.*)?<!\-\-\s\/(cut|slot):\2\s\-\->/sU';
$me = $this;
$slots = &$this->slots;
$this->template = preg_replace_callback( $pattern, function( $matches ) use ( $me, &$slots ) {
list( , $type, $id, $snippet ) = $matches;
if ( $type === 'cut' ) {
$me->addToSketchBook( $id, $snippet );
return '<!-- paste:self'.$id.' -->';
} else {
$slots[$id] = TRUE;
return "#$id#";
}
}, $this->template );
$this->template = preg_replace( '/#([^\?\s#]+)(\?)?#/sU', '#&$1$2#', $this->template );
}
/**
* Internal method that needs to be public because PHP is too stupid to understand
* $this in closures.
*
* @param string $id
* @param string $snippet
*/
public function addToSketchBook( $id, $snippet )
{
$this->catalogue[$id] = count( $this->sketchBook );
$this->sketchBook[] = $snippet;
}
/**
* Creates an instance of StampTE template using a file.
*
* @param string $filename file containing HTML input
*
* @return static
*/
public static function load( $filename )
{
if ( !file_exists( $filename ) ) throw new StampTEException( '[S001] Could not find file: ' . $filename );
$template = file_get_contents( $filename );
return new static( $template );
}
/**
* Checks whether a snippet with ID $id is in the catalogue.
*
* @param string $id identifier you are looking for
*
* @return boolean $yesNo whether the snippet with this ID is available or not.
*/
public function inCatalogue( $id )
{
return ( boolean ) ( isset( $this->catalogue[$id] ) );
}
/**
* Returns a new instance of StampTE configured with the template
* that corresponds to the specified ID.
*
* @param string $id identifier
*
* @return StampTE $snippet
*/
public function get( $id )
{
if ( strpos( $id, '.') !== FALSE) {
$parts = ( explode( '.', $id ) );
$id = reset( $parts );
array_shift( $parts );
$rest = implode( '.', $parts );
}
if ( $this->inCatalogue( $id ) ) {
$snippet = $this->sketchBook[$this->catalogue[$id]];
if ( $this->factory ) {
$new = call_user_func_array( $this->factory, array( $snippet, $id ) );
} else {
$new = new static( $snippet, $id );
}
//Pass the translator and the factory.
$new->translator = $this->translator;
$new->factory = $this->factory;
if ( isset( $parts ) ) {
return $new->get( $rest );
} else {
return $new;
}
} else {
throw new StampTEException( '[S101] Cannot find Stamp Snippet with ID '.preg_replace( '/\W/', '', $id ) );
}
}
/**
* Collects snippets from the template.
* $list needs to be a | pipe separated list of snippet IDs. The snippets
* will be returned as an array so you can obtain them using the list()
* statement.
*
* @param string $list List of IDs you want to fetch from template
*
* @return array $snippets Snippets obtained from template
*/
public function collect( $list )
{
if ( isset( $this->cache[$list] ) ) return $this->cache[$list];
$listItems = explode( '|', $list );
$collection = array();
foreach( $listItems as $item ) {
$collection[] = $this->get( $item );
}
return $collection;
}
/**
* Returns a snippet/template as a string. Besides converting the instance
* to a string this function removes all HTML comments and unnecessary space.
* If you don't want this use a different toString method like ->getString()
*
* @return string $string string representation of HTML snippet/template
*/
public function __toString()
{
$template = $this->template;
$template = preg_replace( "/\s*<!--\s*(paste):[\S]*\s*-->/m", "", $template );
if ( strpos($template, '#&' ) !== FALSE ) {
$template = preg_replace( "/data\-stampte=\"#\&\w+\?#\"/m", "", $template );
$template = preg_replace( "/#\&\w+\?#/m", "", $template );
}
if ( self::$clearws ) $template = preg_replace( '/\s*<!--\sclr\s-->/m', '', $template );
return $template;
}
/**
* Returns the template as a string.
*
* @return string $raw raw template
*/
public function getString() {
return $this->template;
}
/**
* Glues a snippet to a glue point in the current snippet/template.
* The glue() method also accepts raw strings.
*
* The glue method will append the Stamp object or string specified in
* $snippet to the template at the glue point marked by the glue point marker,
* i.e. <!-- patse:X --> where X is the name of the glue point.
* A glue point can have conditions, in this case you MUST provide a Stamp
* object rather than a raw string because the ID of the object needs to be checked.
* Conditional glue points have the format: <!-- paste:X(Y,Z) --> where Y,Z are the
* allowed IDS (from the cut markers).
*
* Note that conditional glue points are rather slow. Consider writing a small shell
* script to remove the conditions before deploying your templates to a production
* environment (assuming they're not needed there).
*
* If you pass a raw string for a conditional glue point you'll get a S003 exception.
* If your Stamp object is rejected by the glue point you'll get a S102 exception.
*
* @throws StampTEException
*
* @param string $what ID of the Glue Point you want to append the contents of the snippet to.
* @param StampTE|string $snippet a StampTE snippet/template to be glued at this point
*
* @return StampTE $snippet self, chainable
*/
public function glue( $what, $snippet )
{
$matches = array();
$pattern = "<!-- paste:{$what}(";
//No conditions! fast track method is possible!
if ( strpos( $this->template, $pattern ) === FALSE ) {
$pattern = "<!-- paste:{$what} -->";
$clear = (self::$clearws) ? '<!-- clr -->' : '';
$replacement = $clear.$snippet.$pattern;
$this->template = str_replace( $pattern, $replacement, $this->template );
return $this;
}
$pattern = '/\s*<!\-\-\spaste:'.$what.'(\(([^\)]+)\))?\s\-\->/u';
$this->template = preg_replace_callback( $pattern, function( $matches ) use ( $snippet, $what ) {
$copyOrig = $matches[0];
if ( isset($matches[2]) ) {
if ( !is_object( $snippet ) ) throw new StampTEException( '[S003] Snippet is not an object or string. Conditional glue point requires object.' );
$allowedSnippets = $matches[2];
$allowedMap = array_flip( explode( ',', $allowedSnippets ) );
if ( !isset( $allowedMap[$snippet->getID()] ) ) throw new StampTEException( '[S102] Snippet '.$snippet->getID().' not allowed in slot '.$what );
}
return $snippet.$copyOrig;
}, $this->template );
return $this;
}
/**
* Glues all elements in the specified array.
* This is a quick way to glue multiple elements as well.
*
* @param array $map list of key=>value pairs to glue
*
* @return StampTE $snippet self, chainable
*/
public function glueAll( $map )
{
foreach( $map as $slot => $value ) {
if ( is_array( $value ) ) {
foreach( $value as $item ) {
$this->glue( $slot, $item );
}
} else {
$this->glue( $slot, $value );
}
}
return $this;
}
/**
* Injects a piece of data into the slot marker in the snippet/template.
*
* @param string $where ID of the slot where to inject the data
* @param string $data the data to inject in the slot
* @param boolean $raw if TRUE output will not be escaped
*
* @return StampTE $snippet self, chainable
*/
public function inject( $slot, $data, $raw = FALSE )
{
if ( !$raw ) $data = $this->filter( $data );
$where = "#&$slot#";
$whereOpt = "#&$slot?#";
$this->template = str_replace( $where, $data, $this->template );
$this->template = str_replace( $whereOpt, $data, $this->template );
return $this;
}
/**
* Injects a piece of data into an attribute slot marker in the snippet/template.
*
* @param string $slot name of the slot where the data should be injected
* @param string $data the data to be injected in the slot
* @param boolean $raw if TRUE output will not be escaped
*
* @return StampTE
*/
public function injectAttr( $slot, $data, $raw = FALSE )
{
if ( !$raw ) $data = $this->filter( $data );
$where = "data-stampte=\"#&$slot#\"";
$whereOpt = "data-stampte=\"#&$slot?#\"";
$this->template = str_replace( $where, $data, $this->template );
$this->template = str_replace( $whereOpt, $data, $this->template );
return $this;
}
/**
* Alias for inject($where,$data,TRUE)
*
* @param string $where ID of the slot where to inject the data
* @param string $data the data to inject in the slot
*
* @return StampTE $snippet self, chainable
*/
public function injectRaw( $where, $data )
{
return $this->inject( $where, $data, TRUE );
}
/**
* Same as inject() but injects an entire array of slot->data pairs.
*
* @param array $array Array of slot->data pairs
* @param boolean $raw if TRUE output will not be escaped
*
* @return StampTE self, chainable
*/
public function injectAll( $array, $raw = FALSE )
{
foreach( $array as $key => $value ) {
$this->inject( $key, $value, $raw );
}
return $this;
}
/**
* Returns the identifier of the current snippet/template.
*
* @return string $id ID of this snippet/template
*/
public function getID()
{
return $this->id;
}
/**
* Copies the current snippet/template.
*
* @return StampTE $copy Copy of the current template/snippet
*/
public function copy()
{
return clone( $this );
}
/**
* Collects a list, just like collect() but stores result in cache array.
*
* @param string $list Pipe separated list of IDs.
*
* @return self
*/
public function writeToCache( $list )
{
$this->cache[$list] = $this->collect( $list );
return $this;
}
/**
* Returns the cache object for storage to disk.
*
* @return string $cache serialized cache object.
*/
public function getCache()
{
return serialize( $this->cache );
}
/**
* Loads cache data.
*
* @param string $rawCacheData the serialized cached string as retrieved from getCache().
*
* @return self
*/
public function loadIntoCache( $rawCacheData )
{
$this->cache = unserialize( $rawCacheData );
if ( !is_array( $this->cache ) ) throw new StampTEException( '[S004] Failed to unserialize cache object.' );
return $this;
}
/**
* Filters data.
*
* @param string $string
*
* @return string $string
*/
protected function filter( $data )
{
$data = iconv("UTF-8","UTF-8//IGNORE", $data);
$filtered = htmlspecialchars( $data, ENT_QUOTES, 'UTF-8' );
$filtered = str_replace( '`', '`', $filtered ); //Prevent MSIE backtick XSS hack
return $filtered;
}
/**
* Selects a Glue Point to attach a Stamp to.
* Note that although this seems like a getter this method
* actually returns the same StampTE. It's both evil and beautiful at the same time.
*
* @param string $gluePoint
*
* @return StampTE
*/
public function &__get( $gluePoint )
{
$this->select = $gluePoint;
return $this;
}
/**
* Call interceptor.
* Intercepts:
* - getX(), routes to get('X')
* - setX(Y), routes to inject('X',Y)
*/
public function __call($method, $arguments)
{
if ( strpos( $method, 'get' ) === 0 ) {
return $this->get( lcfirst( substr( $method, 3) ) );
} elseif ( strpos( $method, 'set' ) === 0 ) {
$this->inject( lcfirst( substr( $method, 3 ) ), $arguments[0] );
return $this;
} elseif ( strpos( $method, 'say' ) ===0 ) {
$this->inject( lcfirst( substr( $method, 3) ), call_user_func_array( $this->translator, $arguments ) );
return $this;
}
}
/**
* Glues the specified Stamp object to the currently selected
* Glue Point.
*
* @param StampTE $stamp
*/
public function add( StampTE $stamp )
{
if ( $this->select === NULL ) {
$this->select = 'self'.$stamp->getID();
}
$this->glue( $this->select, $stamp );
$this->select = NULL; //reset
return $this;
}
/**
* Sets the translator function to be used for translations.
* The translator function will be called automatically as soon as you invoke the magic
* method:
*
* sayX(Y) where X is the slot you want to inject the contents of Y into.
*
* Note that optional arguments are allowed and passed to the translator.
*
* @param closure $closure
*/
public function setTranslator( $closure )
{
if ( !is_callable( $closure ) ) throw new StampTEException( '[S005] Invalid Translator. Translator must be callable.' );
$this->translator = $closure;
}
/**
* Sets the object factory. If get(X) gets called StampTE will call the
* factory with template and ID for X to give you the opportunity to
* wrap the template object in your own wrapper class.
*
* @param closure $factory
*/
public function setFactory( $factory )
{
if ( !is_callable( $factory ) ) throw new StampTEException( '[S006] Invalid Factory. Factory must be callable.' );
$this->factory = $factory;
}
/**
* Attr is a shortcut to quickly set an attribute.
*
* @param string $slot slot
* @param boolean $onOff whether to fill in the slot or not
*
* @return StampTE
*/
public function attr( $slot, $onOff = TRUE )
{
return ( $onOff ) ? $this->injectAttr( $slot, $slot ) : $this;
}
}
//Stamp Exception
class StampTEException extends \Exception {}
}
namespace PHPDialog {
use StampTemplateEngine\StampTE;
/**
* Dialog class.
*
* PHPDialog is a PHP library to display dialog-like HTML documents
* with just one function call:
*
* Usage:
* Dialog::show( 'Hello', 'World' );
*
*/
class Dialog {
private static $template = <<<HTML
<!DOCTYPE html>
<html lang="#language#">
<head>
<meta charset="utf-8">
<title>#title#</title>
<meta name="viewport" content="initial-scale=1.0,width=device-width" >
<style>
body {
min-height:1000px;
background-color:#backgroundColor#;
font:#font#;
background-size:cover;
background-repeat: no-repeat;
background-position:center;
background: linear-gradient(0deg, #backgroundColor# 0%, #textColor# 100%);
}
body,a { color:#textColor#; }
form {
width:calc(100% - 20px);
max-width:800px;
margin:auto;
margin-top:200px;
padding: 1px 10px 10px 10px;
border-radius:5px;
background-color:#foregroundColor#;
animation-name: animatetop;
animation-duration: 0.5s;
box-shadow: 0 0 20px #000;
}
h1 { text-align:center; font:#headerFont#; }
a,input[type=submit] { height:40px; line-height:40px; text-align:center;}
hr { background-color: #backgroundColor#; border:none; height:1px; margin-top:40px; }
.field { margin-bottom:10px; display:block; width:calc(100% - 10px); padding:5px; border: 1px #backgroundColor# solid; border-radius:3px; }
.field::placeholder { opacity:0.4; font-style:italic; }
input[type=submit] {
background-color:#backgroundColor#;
color:#textColor#;
display:block;
border:none;
border-radius:5px;
float:right;
margin-left:10px;
padding: 0 20px 0 20px;
cursor:pointer;
border: 1px transparent solid;
}
input[type=submit]:hover {
background-color:#foregroundColor#;
color:#textColor#;
border: 1px #backgroundColor# solid;
}
@media(max-width:600px) {
a,input[type=submit] { width: 100%; display:block; float:none; clear:both; margin-bottom:10px; margin-left:0; }
form { margin-top: 0; }
}
@keyframes animatetop { from { opacity: 0; transform:scale(0.1); } to {opacity: 1; transform:scale(1); } }
input:focus { outline: 1px solid #textColor#; }
#css?#
</style>
</head>
<body class="custom">
<form method="#method#" action="#action#">
<h1>#title#</h1>
<p>#message#</p>
<!-- cut:field -->
<input title="#title?#" class="field" #autofocus?# type="#type#" name="#name#" value="#value?#" placeholder="#placeholder?#">
<!-- /cut:field -->
<hr>
<!-- cut:button -->
<input title="#label?#" type="submit" name="#name#" value="#label#">
<!-- /cut:button -->
<!-- cut:link -->
<a title="#label?#" href="#href#">#label#</a>
<!-- /cut:link -->
</form>
</body>
</html>
HTML
;
private static $language = 'en';
private static $backgroundColor = '#ccc';
private static $foregroundColor = '#fff';
private static $textColor = '#444';
private static $font = '18px sans-serif';
private static $headerFont = '40px Arial';
private static $css = '';
/**
* Sets the template to be used by the Dialog class
* to generate dialog-like HTML documents.
* If you use the all-in-one file, by default the
* 'onboard' template will be used.
*
* @param string $templateParam template (full HTML StampTE template)
*
* @return void
*/
public static function setTemplate( $templateParam ) {
self::$template = $templateParam;
}
/**
* Sets the language to be used by the Dialog class
* to generate dialog-like HTML documents.
* The default is: 'en'.
*
* @param string $languageParam language code
*
* @return void
*/
public static function setLanguage( $languageParam ) {
self::$language = $languageParam;
}
/**
* Sets the colors to be used by the Dialog class
* to generate dialog-like HTML documents.
* The defaults are: #ccc #fff #444.
*
* Note:
* RGB(A) notations are also allowed.
*
* @param string $backgroundColor color code
* @param string $foregroundColor color code
* @param string $textColor color code
*
* @return void
*/
public static function colors( $backgroundColor, $foregroundColor = '#fff', $textColor = '#000' ) {
self::$backgroundColor = $backgroundColor;
self::$foregroundColor = $foregroundColor;
self::$textColor = $textColor;
}
/**
* Sets the theme to be used by the Dialog class
* to generate dialog-like HTML documents.
* The theme consists of the text font and the
* header/title font.
*
* Defaults:
* '18px sans-serif' for text
* '40px Arial' for header
*
* @param string $font font code
* @param string $headerFont font code
*
* @return void
*/
public static function theme( $font, $headerFont = null ) {
if ( !is_null( $font ) ) {
self::$font = $font;
}
if ( !is_null( $headerFont ) ) {
self::$headerFont = $headerFont;
}
}
/**
* Sets additional CSS rules to be incorporated in the
* resulting HTML document.
*
* @param string $css custom css code
*
* @return void
*/
public static function css( $css ) {
self::$css = $css;
}
/**
* Renders a dialog-like document.
*
* Alert:
* Dialog::render( 'Notice', 'Mind the gap!' );
*
* Confirm:
* Dialog::render( 'Confirm', 'Do you agree', ['no'=>'/'], ['yes'=>'/proceed'] );
*
* Prompt:
* Dialog::render( 'Question', 'What is your name?', [], ['register'=>'/register'], ['name'=>'myname', 'type'=>'text'] );
*
* 'Promptfirm':
* Dialog::render( 'Question', 'What is your name?', ['wont tell'=>'/'], ['register'=>'/register'], ['name'=>'myname', 'type'=>'text'] );
*
* Custom:
* Dialog::render( some other combination );
*
* Note:
* Although multiple submit buttons are allowed, only the action of
* the last button will be used.
*
* Note:
* For every field element, at least the properties name and type
* need to be specified.
*
* @param string $title title of the dialog box
* @param string $message message to display in the box
* @param array $links a series of links to display (format is: href => label)
* @param array $buttons a series of submit buttons (format is: name => action)
* @param array $fields additional input fields (format is: [ property => value ])
*
* @return void
*/
public static function render( $title, $message, $links, $buttons=[], $fields=[] ) {
$dialog = new StampTE( self::$template );
$dialog->setTitle( $title );
$dialog->setMessage( $message );
$dialog->setLanguage( self::$language );
$dialog->setBackgroundColor( self::$backgroundColor );
$dialog->setForegroundColor( self::$foregroundColor );
$dialog->setTextColor( self::$textColor );
$dialog->setFont(self::$font);
$dialog->setHeaderFont(self::$headerFont);
$dialog->setCss(self::$css);
foreach( $buttons as $name => $action ) {
$dialog->setMethod('POST');
$dialog->setAction($action);
$button = $dialog->getButton();
$button->setLabel($name);
$button->setName($name);
$dialog->add( $button );
}
foreach( $links as $label => $href ) {
$link = $dialog->getLink();
$link->setHref( $href );
$link->setLabel( $label );
$dialog->add( $link );
}
foreach( $fields as $definition ) {
$field = $dialog->getField();
foreach( $definition as $key => $value ) {
$field->inject( $key, $value );
}
$dialog->add( $field );
}
return $dialog;
}
/**
* Renders and displays a dialog box.
* For more details see Dialog::render().
*
* @see Dialog::render()
*/
public static function show( $title, $message, $links, $buttons=[], $fields=[] ) {
echo strval( self::render( $title, $message, $links, $buttons, $fields ) );
}
}
}