diff options
Diffstat (limited to 'Templates.class.php')
-rw-r--r-- | Templates.class.php | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/Templates.class.php b/Templates.class.php new file mode 100644 index 0000000..9cfec4f --- /dev/null +++ b/Templates.class.php @@ -0,0 +1,120 @@ +<?php + +include_once "db.php"; + +function apply() { + $args = func_get_args(); + $f = $args[0]; + $args = array_slice($args, 1); + return function() use ($f, $args) { + return call_user_func_array($f, $args); + }; +} + +// Queue List + +function template_queue_list_text($url_generator, array $queues) { + $count = count($queues); +?> +Number of queues: <?=$count?>. + +<?php + foreach($queues as $q) { + echo($q->toPlainText($url_generator) . "\n"); + } +} + +function template_queue_list_html(UrlGenerator $url_generator, array $queues) { + html_default(apply("render_queues", $url_generator, $queues)); +} + +function render_queues($url_generator, $queues) { + $count = count($queues); +?> +<p> +Number of queues: <?=$count?>. +</p> + +<?php + foreach($queues as $q) { + echo($q->toHtml($url_generator, FALSE) . "\n"); + } +} + + +// Queue + +function template_queue_html(UrlGenerator $url_generator, Queue $queue) { + html_default(apply("render_queue_html", $url_generator, $queue)); +} + +function render_queue_html(UrlGenerator $url_generator, Queue $queue) { + echo url_to_a($url_generator->queues(), "All queues"); + echo $queue->toHtml($url_generator, TRUE); +} + +function template_queue_text(UrlGenerator $url_generator, Queue $queue) { + echo($queue->toPlainText($url_generator)); +} + +// Item List + +function template_item_list_html(UrlGenerator $url_generator, $queue, array $items) { + html_default(apply("render_item_list_html", $url_generator, $queue, $items)); +} + +function render_item_list_html(UrlGenerator $url_generator, $queue, array $items) { + $queue_url = url_to_a($url_generator->queue($queue)); + $html = + "<h1>Items in $queue</h1>" . + "<ul>" . + "<li>$queue_url</li>" . + "<li>Queue has " . count($items) . " items.</li>" . + "</ul>"; + echo $html; + + foreach($items as $item) { + echo($item->toHtml($url_generator)); + } +} + +function template_item_list_text(UrlGenerator $url_generator, $queue, array $items) { + echo("This queue has " . count($items) . " items.\n"); + echo("\n"); + + foreach($items as $item) { + echo($item->toPlainText($url_generator)); + echo("\n"); + } +} + +// Common + +function invoke($f) { + if(is_callable($f)) { + $f(); + } + else { + echo($f); + } +} + +function html_default($body, $head = NULL) { +?> +<html> +<head> +<?php +if($head != NULL) { + $head(); +} +?> +</head> +<body> +<?php +invoke($body); +?> +</body> +</html> +<?php +} +?> |