1<?php
2
3/**
4 * Observium
5 *
6 *   This file is part of Observium.
7 *
8 * @package    observium
9 * @subpackage web
10 * @copyright  (C) 2006-2013 Adam Armstrong, (C) 2013-2019 Observium Limited
11 *
12 */
13
14/**
15 * Generate Bootstrap-format Navbar
16 *
17 *   A little messy, but it works and lets us move to having no navbar markup on pages :)
18 *   Examples:
19 *   print_navbar(array('brand' => "Apps", 'class' => "navbar-narrow", 'options' => array('mysql' => array('text' => "MySQL", 'url' => generate_url($vars, 'app' => "mysql")))))
20 *
21 * @param array $vars
22 * @return none
23 *
24 */
25function print_tabbar($tabbar)
26{
27  $output = '<ul class="nav nav-tabs">';
28
29  foreach ($tabbar['options'] as $option => $array)
30  {
31    if ($array['right'] == TRUE) { $array['class'] .= ' pull-right'; }
32    $output .= '<li class="' . $array['class'] . '">';
33    $output .= '<a href="'.$array['url'].'">';
34    if (isset($array['icon']))
35    {
36      $output .= '<i class="'.$array['icon'].'"></i> ';
37    }
38
39    $output .= $array['text'].'</a></li>';
40  }
41  $output .= '</ul>';
42
43  echo $output;
44}
45
46/**
47 * Generate Bootstrap-format navigation bar
48 *
49 *   A little messy, but it works and lets us move to having no navbar markup on pages :)
50 *   Examples:
51 *   print_navbar(array('brand' => "Apps", 'class' => "navbar-narrow", 'options' => array('mysql' => array('text' => "MySQL", 'url' => generate_url($vars, 'app' => "mysql")))))
52 *
53 * @param array $vars
54 * @return none
55 *
56 */
57function print_navbar($navbar)
58{
59  global $config;
60
61  if (OBSERVIUM_EDITION == 'community' && isset($navbar['community']) && $navbar['community'] === FALSE)
62  {
63    // Skip nonexistant features on community edition
64    return;
65  }
66
67  $id = strgen();
68  // Detect allowed screen ratio for current browser, cached!
69  $ua_info = detect_browser();
70
71  ?>
72
73  <div class="navbar <?php echo $navbar['class']; ?>" style="<?php echo $navbar['style']; ?>">
74    <div class="navbar-inner">
75      <div class="container">
76        <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target="#nav-<?php echo $id; ?>">
77          <span class="oicon-bar"></span>
78        </button>
79
80  <?php
81
82  if (isset($navbar['brand'])) { echo ' <a class="brand '.(isset($navbar['brand-class']) ? $navbar['brand-class'] : '' ).'">'.$navbar['brand'].'</a>'; }
83
84  echo('<div class="nav-collapse" id="nav-'.$id.'">');
85
86  //rewrite navbar (for class pull-right)
87  $newbar = array();
88  foreach (array('options', 'options_right') as $array_name)
89  {
90    if (isset($navbar[$array_name]))
91    {
92      foreach ($navbar[$array_name] as $option => $array)
93      {
94        if (isset($array['userlevel']) && isset($_SESSION['userlevel']) && $_SESSION['userlevel'] < $array['userlevel'])
95        {
96          // skip not permitted menu items
97          continue;
98        }
99        if (OBSERVIUM_EDITION == 'community' && isset($array['community']) && $array['community'] === FALSE)
100        {
101          // Skip not exist features on community
102          continue;
103        }
104
105        if (strstr($array['class'], 'pull-right') || $array_name == 'options_right' || $array['right'] == TRUE)
106        {
107          $array['class'] = str_replace('pull-right', '', $array['class']);
108          $newbar['options_right'][$option] = $array;
109        } else {
110          $newbar['options'][$option] = $array;
111        }
112      }
113    }
114  }
115
116  foreach (array('options', 'options_right') as $array_name)
117  {
118    if ($array_name == 'options_right')
119    {
120      if (!$newbar[$array_name]) { break; }
121      echo('<ul class="nav pull-right">');
122    } else {
123      echo('<ul class="nav">');
124    }
125    foreach ($newbar[$array_name] as $option => $array)
126    {
127
128      // if($array['divider']) { echo '<li class="divider"></li>'; break;}
129
130      if (!is_array($array['suboptions']))
131      {
132        echo('<li class="'.$array['class'].'">');
133
134        $link_opts = '';
135        if (isset($array['link_opts'])) { $link_opts .= ' ' . $array['link_opts']; }
136        if (isset($array['alt']))       { $link_opts .= ' data-rel="tooltip" data-tooltip="'.$array['alt'].'"'; }
137        if (isset($array['id']))        { $link_opts .= ' id="'.$array['id'].'"'; }
138
139        if (empty($array['url']) || $array['url'] == '#') { $array['url'] = 'javascript:void(0)'; }
140        echo('<a href="'.$array['url'].'" '.$link_opts.'>');
141
142        if (isset($array['icon']))
143        {
144          echo('<i class="'.$array['icon'].'"></i>&nbsp;');
145          $array['text'] = '<span>'.$array['text'].'</span>'; // Added span for allow hide by class 'icon'
146        }
147
148        if (isset($array['image']))
149        {
150          if (isset($array['image_2x']) && $ua_info['screen_ratio'] > 1)
151          {
152            // Add hidpi image set
153            $srcset = ' srcset="' . $array['image_2x'] . ' 2x"';
154          } else {
155            $srcset = '';
156          }
157          echo('<img src="' . $array['image'] . '"' . $srcset . ' alt="" /> ');
158        }
159
160        echo($array['text'].'</a>');
161        echo('</li>');
162      } else {
163        echo('  <li class="dropdown '.$array['class'].'">');
164
165        $link_opts = '';
166        if (isset($array['link_opts'])) { $link_opts .= ' ' . $array['link_opts']; }
167        if (isset($array['alt']))       { $link_opts .= ' data-rel="tooltip" data-tooltip="'.$array['alt'].'"'; }
168        if (isset($array['id']))        { $link_opts .= ' id="'.$array['id'].'"'; }
169
170        if (empty($array['url']) || $array['url'] == '#') { $array['url'] = 'javascript:void(0)'; }
171        echo('    <a class="dropdown-toggle" data-hover="dropdown" data-toggle="dropdown" href="'.$array['url'].'" '.$link_opts.'>');
172        if (isset($array['icon'])) { echo('<i class="'.$array['icon'].'"></i> '); }
173        echo($array['text'].'
174            <strong class="caret"></strong>
175          </a>
176        <ul class="dropdown-menu">');
177
178        foreach ($array['suboptions'] as $suboption => $subentry)
179        {
180
181          if (count($subentry['entries']))
182          {
183            navbar_submenu($subentry, $level+1);
184          } else {
185            navbar_entry($subentry, $level+2);
186          }
187
188
189            /*
190
191          if(isset($subarray['divider']) && $subarray['divider'])
192          {
193            echo '<li class="divider"></li>';
194          } else {
195            echo('<li class="'.$subarray['class'].'">');
196            $link_opts = '';
197            if (isset($subarray['link_opts'])) { $link_opts .= ' ' . $subarray['link_opts']; }
198            if (isset($subarray['alt']))       { $link_opts .= ' data-rel="tooltip" data-tooltip="'.$subarray['alt'].'"'; }
199            if (isset($subarray['id']))        { $link_opts .= ' id="'.$subarray['id'].'"'; }
200
201            if (empty($subarray['url']) || $subarray['url'] == '#') { $subarray['url'] = 'javascript:void(0)'; }
202            echo('<a href="'.$subarray['url'].'" '.$link_opts.'>');
203            if (isset($subarray['icon']))
204            {
205              echo('<i class="'.$subarray['icon'].'"></i>&nbsp;');
206              $subarray['text'] = '<span>'.$subarray['text'].'</span>'; // Added span for allow hide by class 'icon'
207            }
208            echo($subarray['text'].'</a>');
209
210            echo('</li>');
211          }
212
213            */
214
215        }
216        echo('    </ul>
217      </li>');
218      }
219    }
220    echo('</ul>');
221  }
222
223  ?>
224        </div>
225      </div>
226    </div>
227  </div>
228
229 <?php
230
231}
232
233
234// DOCME needs phpdoc block
235function navbar_location_menu($array)
236{
237  global $config;
238
239  ksort($array['entries']);
240
241  echo('<ul role="menu" class="dropdown-menu">');
242
243  if (count($array['entries']) > "5")
244  {
245    foreach ($array['entries'] as $entry => $entry_data)
246    {
247      $image = get_icon('location');
248      if ($entry_data['level'] == "location_country")
249      {
250        $entry = country_from_code($entry);
251        $image = get_icon_country($entry);
252      }
253      elseif ($entry_data['level'] == "location")
254      {
255        $name = ($entry === '' ? OBS_VAR_UNSET : escape_html($entry));
256        echo('            <li>' . generate_menu_link(generate_location_url($entry), $image . '&nbsp;' . $name, $entry_data['count']) . '</li>');
257        continue;
258      }
259
260      if ($entry_data['level'] == "location_country")
261      {
262        $url = $entry;
263        // Attach country code to sublevel
264        $entry_data['country'] = $entry;
265      } else {
266        $url = $entry;
267        // Attach country code to sublevel
268        $entry_data['country'] = $array['country'];
269      }
270      if ($url === '') { $url = array(''); }
271      $link_array = array('page' => 'devices',
272                          $entry_data['level'] => var_encode($url));
273      if (isset($array['country'])) { $link_array['location_country'] = var_encode($array['country']); }
274
275      echo('<li class="dropdown-submenu">' . generate_menu_link(generate_url($link_array), $image . '&nbsp;' . $entry, $entry_data['count']));
276
277      navbar_location_menu($entry_data);
278      echo('</li>');
279    }
280  } else {
281    $new_entry_array = array();
282
283    foreach ($array['entries'] as $new_entry => $new_entry_data)
284    {
285      $image = get_icon('location');
286      if ($new_entry_data['level'] == "location_country")
287      {
288        $new_entry = country_from_code($new_entry);
289        $image = get_icon_country($new_entry);
290      }
291      elseif ($new_entry_data['level'] == "location")
292      {
293        $name = ($new_entry === '' ? OBS_VAR_UNSET : escape_html($new_entry));
294        echo('            <li>' . generate_menu_link(generate_location_url($new_entry), $image . '&nbsp;' . $name, $new_entry_data['count']) . '</li>');
295        continue;
296      }
297
298      echo('<li class="nav-header">'.$image.$new_entry.'</li>');
299      foreach ($new_entry_data['entries'] as $sub_entry => $sub_entry_data)
300      {
301        if (is_array($sub_entry_data['entries']))
302        {
303          $link_array = array('page' => 'devices',
304                              $sub_entry_data['level'] => var_encode($sub_entry));
305          if (isset($array['country'])) { $link_array['location_country'] = var_encode($array['country']); }
306
307          echo('<li class="dropdown-submenu">' . generate_menu_link(generate_url($link_array), $image.'&nbsp;' . $sub_entry, $sub_entry_data['count']));
308          navbar_location_menu($sub_entry_data);
309        } else {
310          $name = ($sub_entry === '' ? OBS_VAR_UNSET : escape_html($sub_entry));
311          echo('            <li>' . generate_menu_link(generate_location_url($sub_entry), $image.'&nbsp;' . $name, $sub_entry_data['count']) . '</li>');
312        }
313      }
314    }
315  }
316  echo('</ul>');
317}
318
319// DOCME needs phpdoc block
320function navbar_submenu($entry, $level = 1)
321{
322
323  if(isset($entry['text'])) { $entry['title'] = $entry['text']; }
324
325  echo(str_pad('',($level-1)*2) . '                <li class="dropdown-submenu">' . generate_menu_link($entry['url'], '<i class="' . $entry['icon'] . '"></i>&nbsp;' . $entry['title'], $entry['count'], 'label', NULL, $entry['alert_count']) . PHP_EOL);
326  echo(str_pad('',($level-1)*2) . '                  <ul role="menu" class="dropdown-menu">' . PHP_EOL);
327
328  foreach ($entry['entries'] as $subentry)
329  {
330    if (count($subentry['entries']))
331    {
332      navbar_submenu($subentry, $level+1);
333    } else {
334      navbar_entry($subentry, $level+2);
335    }
336  }
337
338  echo(str_pad('',($level-1)*2) . '                  </ul>' . PHP_EOL);
339  echo(str_pad('',($level-1)*2) . '                <li>' . PHP_EOL);
340}
341
342// DOCME needs phpdoc block
343// FIXME. Move to print navbar
344function navbar_entry($entry, $level = 1)
345{
346  global $cache;
347
348  if ($entry['divider'])
349  {
350    echo(str_pad('',($level-1)*2) . '                <li class="divider"></li>' . PHP_EOL);
351  }
352  else if (isset($entry['userlevel']) && isset($_SESSION['userlevel']) && $_SESSION['userlevel'] < $entry['userlevel'])
353  {
354     // skip not permitted menu items
355     //continue;
356  }
357  else if (OBSERVIUM_EDITION == 'community' && isset($entry['community']) && $entry['community'] === FALSE)
358  {
359     // Skip not exist features on community
360     //continue;
361  }
362  else if ($entry['locations']) // Workaround until the menu builder returns an array instead of echo()
363  {
364    echo(str_pad('',($level-1)*2) . '                <li class="dropdown-submenu">' . PHP_EOL);
365    echo(str_pad('',($level-1)*2) . '                  ' . generate_menu_link(generate_url(array('page'=>'locations')), '<i class="'.$GLOBALS['config']['icon']['location'].'"></i> Locations') . PHP_EOL);
366    navbar_location_menu($cache['locations']);
367    echo(str_pad('',($level-1)*2) . '                </li>' . PHP_EOL);
368  } else {
369    //$entry_text = '<i class="menu-icon ' . $entry['icon'] . '"></i> ';
370    $entry_text = '';
371
372    if (isset($entry['image']))
373    {
374      // Detect allowed screen ratio for current browser, cached!
375      $ua_info = detect_browser();
376      if (isset($entry['image_2x']) && $ua_info['screen_ratio'] > 1)
377      {
378        // Add hidpi image set
379        $srcset = ' srcset="' . $entry['image_2x'] . ' 2x"';
380      } else {
381        $srcset = '';
382      }
383      $entry_text .= '<img src="' . $entry['image'] . '"' . $srcset . ' alt="" /> ';
384    }
385
386    if (isset($entry['title']))
387    {
388      $entry_text .= $entry['title'];
389    } elseif (isset($entry['text'])) {
390      $entry_text .= $entry['text'];
391    }
392
393    $entry['text'] = $entry_text;
394
395
396
397    echo(str_pad('',($level-1)*2) . '                <li>' . generate_menu_link_new($entry) . '</li>' . PHP_EOL);
398  }
399}
400
401
402// EOF
403