Automad
 All Classes Functions Variables Pages
toolbox.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 Toolbox {
53 
54 
59  private $Automad;
60 
61 
66  private $collection;
67 
68 
73  public function __construct($Automad) {
74 
75  $this->Automad = $Automad;
76  $this->collection = $this->Automad->getCollection();
77 
78  }
79 
80 
87  public function bootstrapCSS() {
88 
89  return '<link type="text/css" rel="stylesheet" href="/automad/lib/bootstrap/css/bootstrap.min.css" />';
90 
91  }
92 
93 
100  public function bootstrapJS() {
101 
102  return '<script type="text/javascript" src="/automad/lib/bootstrap/js/bootstrap.min.js"></script>';
103 
104  }
105 
106 
113  public function filelist($options = array()) {
114 
115  $this->Automad->getFilelist()->config($options);
116 
117  }
118 
119 
126  public function filterParentByTags() {
127 
128  return Html::generateFilterMenu($this->Automad->Context->get()->tags, $this->Automad->Context->get()->parentUrl);
129 
130  }
131 
132 
140  public function img($options = array()) {
141 
142  // Default options
143  $defaults = array(
144  'file' => '',
145  'width' => false,
146  'height' => false,
147  'crop' => false,
148  'link' => false,
149  'target' => false,
150  'class' => false
151  );
152 
153  // Merge options with defaults
154  $options = array_merge($defaults, $options);
155 
156  if ($options['file']) {
157 
158  $glob = Resolve::filePath($this->Automad->Context->get()->path, $options['file']);
159  return Html::addImage(
160  $glob,
161  $options['width'],
162  $options['height'],
163  $options['crop'],
164  $options['link'],
165  $options['target'],
166  $options['class']
167  );
168 
169  }
170 
171  }
172 
173 
195  public function imgSet($options = array()) {
196 
197  // Default options
198  $defaults = array(
199  'files' => '*.jpg',
200  'width' => false,
201  'height' => false,
202  'crop' => false,
203  'order' => false,
204  'class' => false,
205  'firstWidth' => false,
206  'firstHeight' => false,
207  'firstClass' => false,
208  'enlargedWidth' => false,
209  'enlargedHeight' => false
210  );
211 
212  // Merge options with defaults
213  $options = array_merge($defaults, $options);
214 
215  $files = Parse::fileDeclaration($options['files'], $this->Automad->Context->get());
216 
217  // Sort images.
218  if ($options['order'] == 'asc') {
219  sort($files, SORT_NATURAL);
220  }
221 
222  if ($options['order'] == 'desc') {
223  rsort($files, SORT_NATURAL);
224  }
225 
226  return Html::generateImageSet(
227  $files,
228  $options['width'],
229  $options['height'],
230  $options['crop'],
231  $options['class'],
232  $options['firstWidth'],
233  $options['firstHeight'],
234  $options['firstClass'],
235  $options['enlargedWidth'],
236  $options['enlargedHeight']
237  );
238 
239  }
240 
241 
248  public function jquery() {
249 
250  return '<script type="text/javascript" src="/automad/lib/jquery/jquery-2.0.3.min.js"></script>';
251 
252  }
253 
254 
262  public function level() {
263 
264  return $this->Automad->Context->get()->level;
265 
266  }
267 
268 
276  public function linkPrev($options = array()) {
277 
278  $Selection = new Selection($this->collection);
279  $Selection->filterPrevAndNextToUrl($this->Automad->Context->get()->url);
280 
281  $pages = $Selection->getSelection();
282 
283  if (isset($options['text'])) {
284  $text = $options['text'];
285  } else {
286  $text = false;
287  }
288 
289  // Check if there is a previous page and return HTML
290  if (isset($pages['prev'])) {
291  return Html::addLink($pages['prev'], AM_HTML_CLASS_PREV, $text);
292  }
293 
294  }
295 
296 
304  public function linkNext($options = array()) {
305 
306  $Selection = new Selection($this->collection);
307  $Selection->filterPrevAndNextToUrl($this->Automad->Context->get()->url);
308 
309  $pages = $Selection->getSelection();
310 
311  if (isset($options['text'])) {
312  $text = $options['text'];
313  } else {
314  $text = false;
315  }
316 
317  // Check if there is a next page and return HTML
318  if (isset($pages['next'])) {
319  return Html::addLink($pages['next'], AM_HTML_CLASS_NEXT, $text);
320  }
321 
322  }
323 
324 
332  public function listConfig($options = array()) {
333 
334  $this->pagelist($options);
335 
336  }
337 
338 
346  public function listCount() {
347 
348  $Pagelist = $this->Automad->getPagelist();
349  return count($Pagelist->getPages());
350 
351  }
352 
353 
362  public function listPages($options = array()) {
363 
364  return $this->pagelistMarkup($options);
365 
366  }
367 
368 
376  public function listFilters() {
377 
378  return $this->pagelistFilters();
379 
380  }
381 
382 
391  public function listSort($options = array()) {
392 
393  return $this->pagelistSort($options);
394 
395  }
396 
397 
406  public function metaTitle($options = array()) {
407 
408  $defaults = array(
409  'title' => $this->Automad->getValue(AM_KEY_SITENAME) . ' / ' . $this->Automad->getValue(AM_KEY_TITLE)
410  );
411 
412  // Remove false items.
413  $options = array_filter($options);
414 
415  // Merge filtered options with defaults.
416  $options = array_merge($defaults, $options);
417 
418  return '<title>' . strip_tags($options['title']) . '</title>';
419 
420  }
421 
422 
430  public function navBelow($options = array()) {
431 
432  $defaults = array(
433  'parent' => $this->Automad->Context->get()->url,
434  'homepage' => false,
435  'class' => false
436  );
437 
438  $options = array_merge($defaults, $options);
439 
440  $Selection = new Selection($this->collection);
441  $Selection->filterByParentUrl($options['parent']);
442  $Selection->sortPagesByBasename();
443 
444  $pages = $Selection->getSelection();
445 
446  // Add Homepage to first-level navigation if parent is the homepage and the option 'homepage' is true.
447  if ($options['parent'] == '/' && $options['homepage']) {
448  $pages = array('/' => $this->collection['/']) + $pages;
449  }
450 
451  return Html::generateNav($pages, $options['class']);
452 
453  }
454 
455 
463  public function navBreadcrumbs($options = array()) {
464 
465  if ($this->Automad->Context->get()->level > 0) {
466 
467  $options = array_merge(array('separator' => AM_HTML_STR_BREADCRUMB_SEPARATOR), $options);
468 
469  $Selection = new Selection($this->collection);
470  $Selection->filterBreadcrumbs($this->Automad->Context->get()->url);
471 
472  return Html::generateBreadcrumbs($Selection->getSelection(), $options['separator']);
473 
474  }
475 
476  }
477 
478 
486  public function navChildren($options = array()) {
487 
488  // Always set 'parent' to the current page's parent URL by merging that parameter with the other specified options.
489  return $this->navBelow(array_merge($options, array('parent' => $this->Automad->Context->get()->url)));
490 
491  }
492 
493 
501  public function navPerLevel($options = array()) {
502 
503  $options = array_merge(array('levels' => false), $options);
504  $maxLevel = intval($options['levels']);
505 
506  $Selection = new Selection($this->collection);
507  $Selection->filterBreadcrumbs($this->Automad->Context->get()->url);
508  $pages = $Selection->getSelection();
509 
510  $html = '';
511 
512  foreach ($pages as $page) {
513 
514  if (!$maxLevel || $maxLevel > $page->level) {
515 
516  // Pass current 'parent' along with all other original options.
517  $html .= $this->navBelow(array_merge($options, array('parent' => $page->url)));
518  }
519 
520  }
521 
522  return $html;
523 
524  }
525 
526 
534  public function navSiblings($options = array()) {
535 
536  // Set parent to current parentUrl and overwrite passed options
537  return $this->navBelow(array_merge($options, array('parent' => $this->Automad->Context->get()->parentUrl)));
538 
539  }
540 
541 
549  public function navTop($options = array()) {
550 
551  // Set parent to '/' and overwrite passed options
552  return $this->navBelow(array_merge($options, array('parent' => '/')));
553 
554  }
555 
556 
564  public function navTree($options = array()) {
565 
566  $defaults = array(
567  'all' => true,
568  'parent' => '',
569  'rootLevel' => false
570  );
571 
572  $options = array_merge($defaults, $options);
573 
574  // If 'rootLevel' is not false (!==, can be 0),
575  // the tree always starts below the given level within the breadcrumb trail to the current page.
576  // So, $parent gets dynamically determined in contrast to defining 'parent' within the options.
577  // When 'rootLevel' is defined, the 'parent' option will be ignored.
578  if ($options['rootLevel'] !== false) {
579 
580  $Selection = new Selection($this->collection);
581  $Selection->filterBreadcrumbs($this->Automad->Context->get()->url);
582 
583  foreach ($Selection->getSelection() as $breadcrumb) {
584  if ($breadcrumb->level == $options['rootLevel']) {
585  $parent = $breadcrumb->url;
586  }
587  }
588 
589  } else {
590  // If the 'rootLevel' option is set to false, the 'parent' option will be used.
591  $parent = $options['parent'];
592  }
593 
594  // The tree only gets generated, if $parent is defined, because in case the 'rootLevel' option is
595  // defined and greater than the actual level of the current page, $parent won't be defined.
596  if (isset($parent)) {
597  return Html::generateTree($parent, $options['all'], $this->collection);
598  }
599 
600  }
601 
602 
623  public function pagelist($options = array()) {
624 
625  $this->Automad->getPagelist()->config($options);
626 
627  }
628 
629 
636  public function pagelistFilters() {
637 
638  return Html::generateFilterMenu($this->Automad->getPagelist()->getTags());
639 
640  }
641 
642 
666  public function pagelistMarkup($options = array()) {
667 
668  $defaults = array(
669  'variables' => AM_KEY_TITLE,
670  'glob' => false,
671  'width' => false,
672  'height' => false,
673  'crop' => false,
674  'class' => false,
675  'maxChars' => false,
676  'header' => false,
677  'style' => false,
678  'firstWidth' => false,
679  'firstHeight' => false,
680  'firstClass' => false,
681  'offset' => 0,
682  'limit' => NULL
683  );
684 
685  $options = array_merge($defaults, $options);
686 
687  // Explode list of variables.
688  $options['variables'] = explode(AM_PARSE_STR_SEPARATOR, $options['variables']);
689  $options['variables'] = array_map('trim', $options['variables']);
690 
691  $Pagelist = $this->Automad->getPagelist();
692 
693  return Html::generatePagelist(
694  $Pagelist->getPages($options['offset'], $options['limit']),
695  $options['variables'],
696  $options['glob'],
697  $options['width'],
698  $options['height'],
699  $options['crop'],
700  $options['class'],
701  $options['maxChars'],
702  $options['header'],
703  $options['style'],
704  $options['firstWidth'],
705  $options['firstHeight'],
706  $options['firstClass']
707  );
708 
709  }
710 
711 
728  public function pagelistSort($options = array()) {
729 
730  if (is_array($options) && is_array(reset($options))) {
731 
732  // Sanitize $options
733  foreach ($options as $key => $opt) {
734 
735  // Remove all unneeded array items.
736  $opt = array_intersect_key($opt, array_flip(array('sortItem', 'sortOrder')));
737 
738  // Make sure both required item (sortItem and sortOrder) are existing within $options[$key].
739  $options[$key] = array_merge(array('sortItem' => '', 'sortOrder' => false), $opt);
740 
741  // Set sortOrder to the default order, if its value is invalid.
742  if (!in_array($options[$key]['sortOrder'], array('asc', 'desc'))) {
743  $options[$key]['sortOrder'] = AM_LIST_DEFAULT_SORT_ORDER;
744  }
745 
746  }
747 
748  // Set list defaults.
749  $this->listConfig(reset($options));
750 
751  return Html::generateSortMenu($options);
752 
753  }
754 
755  }
756 
757 
765  public function search($options = array()) {
766 
767  $defaults = array(
768  'placeholder' => 'Search',
769  'formClass' => AM_HTML_CLASS_SEARCH,
770  'inputClass' => AM_HTML_CLASS_SEARCH_INPUT,
771  'button' => false,
772  'buttonClass' => AM_HTML_CLASS_SEARCH_BUTTON
773  );
774 
775  $options = array_merge($defaults, $options);
776 
778  AM_PAGE_RESULTS_URL,
779  $options['placeholder'],
780  $options['formClass'],
781  $options['inputClass'],
782  $options['button'],
783  $options['buttonClass']
784  );
785 
786  }
787 
788 
795  public function template() {
796 
797  return $this->Automad->Context->get()->template;
798 
799  }
800 
801 
808  public function themeURL() {
809 
810  return AM_DIR_THEMES . '/' . $this->Automad->Context->get()->theme;
811 
812  }
813 
814 
821  public function year() {
822 
823  return date('Y');
824 
825  }
826 
827 
828 }
829 
830 
831 ?>
static generateSearchField($url, $placeholder=false, $formClass=false, $inputClass=false, $button=false, $buttonClass=false)
Definition: html.php:559
listSort($options=array())
Definition: toolbox.php:391
linkPrev($options=array())
Definition: toolbox.php:276
navSiblings($options=array())
Definition: toolbox.php:534
navChildren($options=array())
Definition: toolbox.php:486
filelist($options=array())
Definition: toolbox.php:113
static generateBreadcrumbs($pages, $separator= '')
Definition: html.php:189
static fileDeclaration($str, $Page, $stripBaseDir=false)
Definition: parse.php:116
pagelistSort($options=array())
Definition: toolbox.php:728
static generateNav($pages, $class=false)
Definition: html.php:524
imgSet($options=array())
Definition: toolbox.php:195
search($options=array())
Definition: toolbox.php:765
listPages($options=array())
Definition: toolbox.php:362
img($options=array())
Definition: toolbox.php:140
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
navBreadcrumbs($options=array())
Definition: toolbox.php:463
static generateTree($parentUrl, $expandAll, $collection)
Definition: html.php:640
static filePath($pagePath, $filePath)
Definition: resolve.php:72
pagelist($options=array())
Definition: toolbox.php:623
navBelow($options=array())
Definition: toolbox.php:430
navPerLevel($options=array())
Definition: toolbox.php:501
navTree($options=array())
Definition: toolbox.php:564
static generateSortMenu($options)
Definition: html.php:596
listConfig($options=array())
Definition: toolbox.php:332
static addImage($glob, $w=false, $h=false, $crop=false, $link=false, $target=false, $class=false, $addCaption=false)
Definition: html.php:77
metaTitle($options=array())
Definition: toolbox.php:406
navTop($options=array())
Definition: toolbox.php:549
__construct($Automad)
Definition: toolbox.php:73
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
linkNext($options=array())
Definition: toolbox.php:304
pagelistMarkup($options=array())
Definition: toolbox.php:666
static generateFilterMenu($tags, $targetPage= '')
Definition: html.php:221
static addLink($Page, $classes= '', $text= '')
Definition: html.php:133