Automad
 All Classes Functions Variables Pages
html.php
1 <?php
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  * AUTOMAD
29  *
30  * Copyright (c) 2014 by Marc Anton Dahmen
31  * http://marcdahmen.de
32  *
33  * Licensed under the MIT license.
34  * http://automad.org/license
35  */
36 
37 
38 namespace Automad\Core;
39 
40 
41 defined('AUTOMAD') or die('Direct access not permitted!');
42 
43 
52 class Html {
53 
54 
77  public static function addImage($glob, $w = false, $h = false, $crop = false, $link = false, $target = false, $class = false, $addCaption = false) {
78 
79  if ($files = glob($glob)) {
80 
81  $file = reset($files);
82 
83  Debug::log('Html: First image matching ' . basename($glob) . ' is ' . $file);
84 
85  $img = new Image($file, $w, $h, $crop);
86 
87  if ($img->file) {
88 
89  if ($target) {
90  $target = ' target="' . $target . '"';
91  }
92 
93  $html = '';
94 
95  if ($link) {
96  $html .= '<a href="' . $link . '"' . $target . '>';
97  }
98 
99  if ($class) {
100  $class = ' class="' . $class . '"';
101  }
102 
103  if ($addCaption) {
104  $caption = ' data-caption="' . self::addVariable('image_caption_' . Parse::sanitize(basename($file))) . '"';
105  } else {
106  $caption = '';
107  }
108 
109  $html .= '<div class="' . AM_HTML_CLASS_IMAGE_WRAPPER . '"><img' . $class . ' src="' . $img->file . '" alt="' . $img->description . '" title="' . $img->description . '" width="' . $img->width . '" height="' . $img->height . '"' . $caption . ' /></div>';
110 
111  if ($link) {
112  $html .= '</a>';
113  }
114 
115  return $html;
116 
117  }
118 
119  }
120 
121  }
122 
123 
133  public static function addLink($Page, $classes = '', $text = '') {
134 
135  if ($Page->isHome()) {
136  $classes .= ' ' . AM_HTML_CLASS_HOME;
137  }
138 
139  if ($Page->isCurrent()) {
140  $classes .= ' ' . AM_HTML_CLASS_CURRENT;
141  }
142 
143  if ($Page->isInCurrentPath() && !$Page->isHome()) {
144  $classes .= ' ' . AM_HTML_CLASS_CURRENT_PATH;
145  }
146 
147  $classes = trim($classes);
148 
149  if ($classes) {
150  $classes = ' class="' . $classes . '"';
151  }
152 
153  if (!$text) {
154  $text = strip_tags($Page->data[AM_KEY_TITLE]);
155  $title = '';
156  } else {
157  $title = ' title="' . strip_tags($Page->data[AM_KEY_TITLE]) . '"';
158  }
159 
160  return '<a' . $classes . $title . ' href="' . $Page->url . '">' . $text . '</a>';
161 
162  }
163 
164 
174  public static function addVariable($name) {
175 
176  return AM_DEL_VAR_OPEN . $name . AM_DEL_VAR_CLOSE;
177 
178  }
179 
180 
189  public static function generateBreadcrumbs($pages, $separator = '') {
190 
191  $i = 1;
192 
193  $html = '<div class="' . AM_HTML_CLASS_BREADCRUMBS . '">';
194 
195  foreach ($pages as $Page) {
196 
197  $html .= '<a href="' . $Page->url . '">' . strip_tags($Page->data[AM_KEY_TITLE]) . '</a>';
198 
199  // Add separator for all but the last page.
200  if ($i++ < count($pages)) {
201  $html .= ' ' . $separator . ' ';
202  }
203 
204  }
205 
206  $html .= '</div>';
207 
208  return $html;
209 
210  }
211 
212 
221  public static function generateFilterMenu($tags, $targetPage = '') {
222 
223  if ($tags) {
224 
225  $query = Parse::queryArray();
226  $current = Parse::queryKey('filter');
227 
228  $html = '<ul class="' . AM_HTML_CLASS_FILTER . '">';
229 
230  // If there is no $tagetPage in the options, the filters will be used to filter a page list
231  // on the current page without leaving the page after selecting a tag.
232  // In that case, a visitor stays on the page while using the filters and therefore needs
233  // the option to "reset" the filters again to an "unfiltered" mode.
234  // The "All" button gets added for that purpose.
235  if (!$targetPage) {
236 
237  // Check if current query is empty.
238  // No query means no filter - in that case the AM_HTML_CLASS_CURRENT gets applied to the "All" button.
239  if (!$current) {
240  $class = ' class="' . AM_HTML_CLASS_CURRENT . '" ';
241  } else {
242  $class = ' ';
243  }
244 
245  // Only change the ['filter'] key
246  $query['filter'] = '';
247 
248  ksort($query);
249 
250  $html .= '<li><a' . $class . 'href="?' . http_build_query($query, '', '&amp;') . '">' . AM_HTML_TEXT_FILTER_ALL . '</a></li>';
251 
252  }
253 
254  foreach ($tags as $tag) {
255 
256  // Check if $tag equals current filter in query
257  if ($current == $tag) {
258  $class = ' class="' . AM_HTML_CLASS_CURRENT . '" ';
259  } else {
260  $class = ' ';
261  }
262 
263  // Only change the ['filter'] key
264  $query['filter'] = $tag;
265 
266  ksort($query);
267 
268  $html .= '<li><a' . $class . 'href="' . $targetPage . '?' . http_build_query($query, '', '&amp;') . '">' . $tag . '</a></li>';
269 
270  }
271 
272  $html .= '</ul>';
273 
274  return $html;
275 
276  }
277 
278  }
279 
280 
297  public static function generateImageSet(
298  $files,
299  $width = false,
300  $height = false,
301  $crop = false,
302  $class = false,
303  $firstWidth = false,
304  $firstHeight = false,
305  $firstClass = false,
306  $enlargedWidth = false,
307  $enlargedHeight = false
308  ) {
309 
310  if (is_array($files)) {
311 
312  $html = '';
313  $first = true;
314 
315  foreach($files as $file) {
316 
317  // Save $class, $width and $hight in temporary variables for standard images, to keep original parameters untouched and avoid overwriting these in case the first image has special values.
318  if ($class) {
319  $classAttribute = ' class="' . $class . '"';
320  } else {
321  $classAttribute = '';
322  }
323 
324  $w = $width;
325  $h = $height;
326 
327  // Temporary variables can (if defined) be overwritten here for the first image.
328  if ($first) {
329 
330  if ($firstClass) {
331  $classAttribute = ' class="' . $firstClass . '"';
332  }
333 
334  if ($firstWidth) {
335  $w = $firstWidth;
336  }
337 
338  if ($firstHeight) {
339  $h = $firstHeight;
340  }
341 
342  $first = false;
343 
344  }
345 
346  // Add image.
347  $enlargedImage = new Image($file, $enlargedWidth, $enlargedHeight);
348  $html .= '<div' . $classAttribute . '>' . self::addImage($file, $w, $h, $crop, $enlargedImage->file, false, AM_HTML_CLASS_LIST_ITEM_IMG, true) . '</div>';
349 
350  }
351 
352  return $html;
353 
354  }
355 
356  }
357 
358 
378  public static function generatePagelist(
379  $pages,
380  $vars,
381  $glob = false,
382  $width = false,
383  $height = false,
384  $crop = false,
385  $class = false,
386  $maxChars = false,
387  $header = false,
388  $style = false,
389  $firstWidth = false,
390  $firstHeight = false,
391  $firstClass = false
392  ) {
393 
394  if ($pages) {
395 
396  if (!$class) {
397  $class = AM_HTML_CLASS_LIST_ITEM;
398  }
399 
400  if (!$maxChars) {
401  $maxChars = AM_HTML_LIST_MAX_CHARS;
402  }
403 
404  $html = '';
405 
406  if ($header) {
407  $html .= '<div class="' . AM_HTML_CLASS_LIST_HEADER . '">' . $header . '</div>';
408  }
409 
410  $html .= '<ul class="' . AM_HTML_CLASS_LIST . '">';
411 
412  // Set $first to true to determine first element in forach loop.
413  $first = true;
414 
415  foreach ($pages as $Page) {
416 
417  // Define $cls, $w and $h per item to avoid overwriting originals and allow special values for the first item.
418  $cls = $class;
419  $w = $width;
420  $h = $height;
421 
422  // First item only.
423  if ($first) {
424 
425  if ($firstClass) {
426  $cls = $firstClass;
427  }
428 
429  if ($firstWidth) {
430  $w = $firstWidth;
431  }
432 
433  if ($firstHeight) {
434  $h = $firstHeight;
435  }
436 
437  $first = false;
438 
439  }
440 
441  // Build string for inline styles to override the site's CSS.
442  $styleAttribute = '';
443 
444  if (is_array($style)) {
445 
446  foreach ($style as $property => $propertyVariable) {
447  if (isset($Page->data[$propertyVariable])) {
448  $styleAttribute .= $property . ': ' . $Page->data[$propertyVariable] . '; ';
449  }
450  }
451 
452  if ($styleAttribute) {
453  $styleAttribute = ' style="' . trim($styleAttribute) . '"';
454  }
455 
456  }
457 
458  $html .= '<li class="' . $cls . '"><a href="' . $Page->url . '"' . $styleAttribute . '>';
459 
460  // Image
461  if ($glob) {
462 
463  // For each page, the file pattern(s) is (are) matched against the page's direcory (if the glob is relative),
464  // to find a corresponding image as thumbnail.
465  // For example $glob = '*.jpg *.png' will always use the first JPG in the page's directoy and if nothing is found, it will try the first PNG.
466  $images = Parse::fileDeclaration($glob, $Page);
467  $image = reset($images);
468  $html .= Html::addImage($image, $w, $h, $crop, false, false, AM_HTML_CLASS_LIST_ITEM_IMG);
469 
470  }
471 
472  $html .= '<div class="' . AM_HTML_CLASS_LIST_ITEM_DATA . '">';
473 
474  // Variables
475  foreach ($vars as $var) {
476 
477  if (isset($Page->data[$var])) {
478 
479  $text = strip_tags($Page->data[$var]);
480 
481  // Shorten $text to maximal characters (full words).
482  if (strlen($text) > $maxChars) {
483  // Cut $text to max chars
484  $text = substr($text, 0, $maxChars);
485  // Find last space and get position
486  $pos = strrpos($text, ' ');
487  // Cut $text again at last space's position (< $maxChars)
488  $text = substr($text, 0, $pos) . ' ...';
489  }
490 
491  // Variable key is used to define the html class.
492  // That makes styling with CSS very customizable.
493  $html .= '<div class="' . $var . '">' . $text . '</div>';
494 
495  }
496 
497  }
498 
499  $html .= '</div></a></li>';
500 
501  }
502 
503  $html .= '</ul>';
504 
505  return $html;
506 
507  }
508 
509  }
510 
511 
524  public static function generateNav($pages, $class = false) {
525 
526  if ($pages) {
527 
528  if (!$class) {
529  $class = AM_HTML_CLASS_NAV;
530  }
531 
532  $html = '<ul class="' . $class . '">';
533 
534  foreach($pages as $Page) {
535  $html .= '<li>' . self::addLink($Page) . '</li>';
536  }
537 
538  $html .= '</ul>';
539 
540  return $html;
541 
542  }
543 
544  }
545 
546 
559  public static function generateSearchField($url, $placeholder = false, $formClass = false, $inputClass = false, $button = false, $buttonClass = false) {
560 
561  if ($placeholder) {
562  $placeholder = ' placeholder="' . $placeholder . '"';
563  }
564 
565  if ($formClass) {
566  $formClass = ' class="' . $formClass . '"';
567  }
568 
569  if ($inputClass) {
570  $inputClass = ' class="' . $inputClass . '"';
571  }
572 
573  if ($buttonClass) {
574  $buttonClass = ' class="' . $buttonClass . '"';
575  }
576 
577  if ($button) {
578  $button = '<button' . $buttonClass . ' type="submit">' . $button . '</button>';
579  }
580 
581  return '<form' . $formClass . ' method="get" action="' . $url . '" onsubmit="return (this.search.value.length > 0)">' .
582  '<input' . $inputClass . ' type="text" name="search"' . $placeholder . ' value="" />' .
583  $button .
584  '</form>';
585 
586  }
587 
588 
596  public static function generateSortMenu($options) {
597 
598  // Get all items from the query string to keep the current filters/search items when building the links.
599  $query = Parse::queryArray();
600 
601  // Determine the current sort settings, by merging the default options with possible items from the query string.
602  $current = array_merge(reset($options), $query);
603 
604  $html = '<ul class="' . AM_HTML_CLASS_SORT . '">';
605 
606  foreach ($options as $text => $opt) {
607 
608  // Test. whether the current "button" matches the current sort settings.
609  if ($current['sortItem'] == $opt['sortItem'] && $current['sortOrder'] == $opt['sortOrder']) {
610  $class = ' class="' . AM_HTML_CLASS_CURRENT . '" ';
611  } else {
612  $class = ' ';
613  }
614 
615  // Merge query with sorting options. The second array just makes sure,
616  // that both items (item & order) are overwritten in the current "button", even if $opt doesn't have both keys.
617  $query = array_merge($query, array('sortItem' => '', 'sortOrder' => false), $opt);
618  ksort($query);
619 
620  $html .= '<li><a' . $class . 'href="?' . http_build_query($query, '', '&amp;') . '">' . $text . '</a></li>';
621 
622  }
623 
624  $html .= '</ul>';
625 
626  return $html;
627 
628  }
629 
630 
640  public static function generateTree($parentUrl, $expandAll, $collection) {
641 
642  $Selection = new Selection($collection);
643  $Selection->filterByParentUrl($parentUrl);
644  $Selection->sortPagesByBasename();
645 
646  $pages = $Selection->getSelection();
647 
648  if ($pages) {
649 
650  // Use first element in $pages to determine the current level.
651  $pagesKeys = array_keys($pages);
652  $level = ' level-' . $pages[array_shift($pagesKeys)]->level;
653 
654  $html = '<ul class="' . AM_HTML_CLASS_TREE . $level . '">';
655 
656  foreach ($pages as $Page) {
657 
658  $html .= '<li>';
659  $html .= self::addLink($Page);
660 
661  if ($expandAll || $Page->isCurrent() || $Page->isInCurrentPath()) {
662  $html .= self::generateTree($Page->url, $expandAll, $collection);
663  }
664 
665  $html .= '</li>';
666 
667  }
668 
669  $html .= '</ul>';
670 
671  return $html;
672 
673  }
674 
675  }
676 
677 
678 }
679 
680 
681 ?>
static generateSearchField($url, $placeholder=false, $formClass=false, $inputClass=false, $button=false, $buttonClass=false)
Definition: html.php:559
static log($element, $description= '')
Definition: debug.php:113
static generateBreadcrumbs($pages, $separator= '')
Definition: html.php:189
static fileDeclaration($str, $Page, $stripBaseDir=false)
Definition: parse.php:116
static generateNav($pages, $class=false)
Definition: html.php:524
static generateImageSet($files, $width=false, $height=false, $crop=false, $class=false, $firstWidth=false, $firstHeight=false, $firstClass=false, $enlargedWidth=false, $enlargedHeight=false)
Definition: html.php:297
static generateTree($parentUrl, $expandAll, $collection)
Definition: html.php:640
static generateSortMenu($options)
Definition: html.php:596
static sanitize($str, $removeDots=false)
Definition: parse.php:413
static queryKey($key)
Definition: parse.php:294
static addImage($glob, $w=false, $h=false, $crop=false, $link=false, $target=false, $class=false, $addCaption=false)
Definition: html.php:77
static generatePagelist($pages, $vars, $glob=false, $width=false, $height=false, $crop=false, $class=false, $maxChars=false, $header=false, $style=false, $firstWidth=false, $firstHeight=false, $firstClass=false)
Definition: html.php:378
static queryArray()
Definition: parse.php:270
static addVariable($name)
Definition: html.php:174
static generateFilterMenu($tags, $targetPage= '')
Definition: html.php:221
static addLink($Page, $classes= '', $text= '')
Definition: html.php:133