Automad
 All Classes Functions Variables Pages
navbar.php
1 <?php
2 /*
3  * NAVBAR
4  * Extension for Automad
5  *
6  * Copyright (c) 2014 by Marc Anton Dahmen
7  * http://marcdahmen.de
8  *
9  * Licensed under the MIT license.
10  * http://automad.org/license
11  */
12 
13 
14 namespace Extensions;
15 
16 
26 class Navbar {
27 
28 
54  public function Navbar($options, $Automad) {
55 
56  $defaults = array(
57  'fluid' => true,
58  'fixedToTop' => false,
59  'brand' => $Automad->getValue(AM_KEY_SITENAME),
60  'logo' => false,
61  'logoWidth' => 100,
62  'logoHeight' => 100,
63  'search' => 'Search',
64  'searchPosition' => 'left',
65  'levels' => 2
66  );
67 
68  // Merge defaults with options
69  $options = array_merge($defaults, $options);
70 
71  if ($options['fixedToTop']) {
72  $fixed = ' navbar-fixed-top';
73  } else {
74  $fixed = '';
75  }
76 
77  if ($options['fluid']) {
78  $container = 'container-fluid';
79  } else {
80  $container = 'container';
81  }
82 
83  if ($options['logo']) {
84  $brand = \Automad\Core\Html::addImage(AM_BASE_DIR . $options['logo'], $options['logoWidth'], $options['logoHeight']);
85  } else {
86  $brand = $options['brand'];
87  }
88 
89  // Main nav wrapper
90  $html = '<nav class="navbar navbar-default' . $fixed . '">';
91 
92  // To determine all pages for each row, first the "breadcrumbs" get filtered.
93  $Page = $Automad->Context->get();
94  $Selection = new \Automad\Core\Selection($Automad->getCollection());
95  $Selection->filterBreadcrumbs($Page->url);
96  $breadcrumbs = $Selection->getSelection();
97 
98  // Generate rows.
99  foreach ($breadcrumbs as $breadcrumb) {
100 
101  // Limit number of levels to be < $options['levels'].
102  // $options['levels'] == 2 > 2 rows (levels 0 & 1).
103  if ($breadcrumb->level < $options['levels']) {
104 
105  $Selection = new \Automad\Core\Selection($Automad->getCollection());
106  $Selection->filterByParentUrl($breadcrumb->url);
107  $Selection->sortPagesByBasename();
108  $pages = $Selection->getSelection();
109 
110  if ($pages) {
111 
112  if ($breadcrumb->level === 0) {
113 
114  // First level navigation
115  $html .= '<div class="level-' . ($breadcrumb->level + 1) . '">';
116 
117  // Wrapping container
118  $html .= '<div class="' . $container . '">';
119 
120  // Header (brand and collapse button)
121  $html .= '<div class="navbar-header">' .
122  '<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>' .
123  '<a class="navbar-brand" href="/">' . $brand . '</a>' .
124  '</div>';
125 
126  // Collapsable
127  $html .= '<div class="collapse navbar-collapse">';
128 
129  // Page's List
130  $html .= '<ul class="nav navbar-nav">';
131 
132  foreach ($pages as $page) {
133 
134  $html .= '<li';
135 
136  if ($page->isCurrent()) {
137  $html .= ' class="active"';
138  }
139 
140  $html .= '>' . \Automad\Core\Html::addLink($page) . '</li>';
141 
142  }
143 
144  $html .= '</ul>';
145 
146  // Search box
147  if ($options['search']) {
148 
150  AM_PAGE_RESULTS_URL,
151  $options['search'],
152  'navbar-form navbar-' . $options['searchPosition'],
153  'form-control'
154  );
155 
156  }
157 
158  // Close collapse
159  $html .= '</div>';
160 
161  // Close container
162  $html .= '</div>';
163 
164  // Close level
165  $html .= '</div>';
166 
167  } else {
168 
169  // All other levels (>1)
170  $html .= '<div class="level-' . ($breadcrumb->level + 1) . '">'.
171  '<div class="' . $container . '">' .
172  '<div class="collapse navbar-collapse">' .
173  '<ul class="nav navbar-nav">';
174 
175  foreach ($pages as $page) {
176 
177  $html .= '<li';
178 
179  if ($page->isCurrent()) {
180  $html .= ' class="active"';
181  }
182 
183  $html .= '>' . \Automad\Core\Html::addLink($page) . '</li>';
184 
185  }
186 
187  $html .= '</ul>' .
188  '</div>' .
189  '</div>' .
190  '</div>';
191 
192  }
193 
194  }
195 
196  }
197 
198  }
199 
200  // Close nav wrapper
201  $html .= '</nav>';
202 
203  return $html;
204 
205  }
206 
207 
208 }
static generateSearchField($url, $placeholder=false, $formClass=false, $inputClass=false, $button=false, $buttonClass=false)
Definition: html.php:559
static addImage($glob, $w=false, $h=false, $crop=false, $link=false, $target=false, $class=false, $addCaption=false)
Definition: html.php:77
Navbar($options, $Automad)
Definition: navbar.php:54
static addLink($Page, $classes= '', $text= '')
Definition: html.php:133