Skip to content

Commit fb57c96

Browse files
committed
Navigation Rendering
Nav rendering correctly, small issue with current page.
1 parent 3f5e055 commit fb57c96

File tree

2 files changed

+236
-11
lines changed

2 files changed

+236
-11
lines changed

libraries/Navigation.php

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
<?php defined('BASEPATH') OR exit('No direct script access allowed');
2+
/**
3+
* Navigation library for CI3-Navigation-Library
4+
* User: danielwaghorn
5+
* Date: 11/06/15
6+
* Time: 11:06
7+
*/
8+
9+
class Navigation {
10+
/**
11+
* Class for rendering navigation.
12+
*
13+
* @var $_navigation_open:
14+
* Contains the open tag e.g. <ul class="nav">
15+
* @var $_navigation_close:
16+
* Contains the closing tag e.g. </ul>
17+
* @var $_item_open:
18+
* Contains the open tag for a nav item e.g. <li>
19+
* @var $_item_open_active:
20+
* Contains the open tag for an active item e.g.
21+
* <li class="active">
22+
* @var $_item_close:
23+
* Contains the close tag for an item e.g. </li>
24+
* @var $_anchor:
25+
* Contains the template for an anchor in an item
26+
* e.g. <a href="{$url}" {$extra}>{$text}</a>
27+
* where $url is the link to the item,
28+
* $extra is any additional attributes e.g. class="main"
29+
* and $text is the text to be held in the anchor.
30+
* @var $_dropdown_open:
31+
* Contains the open tag for a dropdown e.g. <ul class="dropdown">
32+
* @var $_dropdown_close:
33+
* Contains the close tag for a dropdown e.g. </ul>
34+
*/
35+
36+
/* Markup variables */
37+
private $_navigation_open;
38+
private $_navigation_close;
39+
private $_item_open;
40+
private $_item_open_active;
41+
private $_item_close;
42+
private $_anchor;
43+
private $_dropdown_open;
44+
private $_dropdown_close;
45+
46+
private $_current_url;
47+
private $_base_url;
48+
49+
private $_output;
50+
51+
protected $CI;
52+
53+
public function __construct($params = array('config' => 'navigation'))
54+
{
55+
/**
56+
* Constructor for Navigation
57+
* @param $params : array containing arguments
58+
* In this instance argument can specify custom config to load,
59+
* otherwise the default config is used.
60+
* Usage: $params = array( 'config' => 'myconfig');
61+
* In $this->load->library('navigation',$params);
62+
* Where 'myconfig.php' exists in your application/config folder.
63+
*/
64+
65+
// Gets CI ref to use in this lib.
66+
$this->CI =& get_instance();
67+
$this->CI->load->helper('url');
68+
69+
// Load configuration
70+
$this->CI->config->load($params['config'],TRUE);
71+
72+
// Store configuration
73+
$this->_navigation_open = $this->CI->config->item('navigation_open',$params['config']);
74+
$this->_navigation_close = $this->CI->config->item('navigation_close',$params['config']);
75+
$this->_item_open = $this->CI->config->item('item_open',$params['config']);
76+
$this->_item_open_active = $this->CI->config->item('item_open_active',$params['config']);
77+
$this->_item_close = $this->CI->config->item('item_close',$params['config']);
78+
$this->_anchor = $this->CI->config->item('anchor',$params['config']);
79+
$this->_dropdown_open = $this->CI->config->item('dropdown_open',$params['config']);
80+
$this->_dropdown_close = $this->CI->config->item('dropdown_close',$params['config']);
81+
82+
// Get current URL
83+
$this->_current_url = rtrim(current_url(),'/');
84+
85+
// Get site URL
86+
$this->_base_url = base_url();
87+
88+
// Load model
89+
$this->CI->load->model('nav_model','nav');
90+
}
91+
92+
function isCurrentPage($url) {
93+
/**
94+
* Checks whether current page is = $url
95+
* @param url : relative url to site url e.g. 'login/forgot-password'
96+
* @return : boolean indicating result
97+
*/
98+
99+
// Remove site url
100+
$page_url = str_replace($this->_current_url,$this->_base_url,'');
101+
return strcmp($url,$page_url);
102+
}
103+
104+
function bindAnchor($url, $text, $extra = '') {
105+
/**
106+
* Takes parameters for an anchor and binds them to template.
107+
* @param url : url to put in href
108+
* @param text : text to put between anchor
109+
* @param OPTIONAL extra : extra attributes and data
110+
*/
111+
112+
$vars = array(
113+
'{$url}' => $url,
114+
'{$text}' => $text,
115+
'{$extra}' => $extra
116+
);
117+
118+
return strtr($this->_anchor, $vars);
119+
}
120+
121+
function outputItem($item) {
122+
/**
123+
* Outputs the markup for a single item.
124+
* @param item : query row for a single nav item.
125+
* @returns output : string composed of HTML to be rendered
126+
*/
127+
128+
$output = '';
129+
130+
if ($this->isCurrentPage($item->ItemLink)) {
131+
$output .= $this->_item_open_active;
132+
} else {
133+
$output .= $this->_item_open;
134+
}
135+
136+
// Output link
137+
$output .= $this->bindAnchor($item->ItemLink, $item->ItemHumanName);
138+
139+
// Check for sub items.
140+
$subItems = $this->CI->nav->getSubItems($item->ItemID);
141+
142+
if (count($subItems->result_array()) > 0) {
143+
$this->renderDropdown($subItems);
144+
}
145+
146+
$output .= $this->_item_close;
147+
148+
return $output;
149+
}
150+
151+
function renderDropdown($subItems) {
152+
/**
153+
* Takes subitems and returns markup.
154+
* @param subItems : Query result containing nav items
155+
* @returns output : string composed of HTML to be rendered
156+
*/
157+
158+
$output = $this->_dropdown_open;
159+
160+
foreach ($subItems->result_array() as $item) {
161+
162+
// Check if current page and open item
163+
if ($this->isCurrentPage($item->ItemLink)) {
164+
$output .= $this->_item_open_active;
165+
} else {
166+
$output .= $this->_item_open;
167+
}
168+
169+
// Output link
170+
$output .= $this->bindAnchor($item->ItemLink, $item->ItemHumanName);
171+
172+
// Close item
173+
$output .= $this->_item_close;
174+
}
175+
176+
$output .= $this->_dropdown_close;
177+
178+
return $output;
179+
}
180+
181+
public function generateNav_fromName($menu_name)
182+
{
183+
/**
184+
* Resolves a menu name to ID then returns the menu output.
185+
* @param menu_name : string identifier of the menu as in CI-Nav-Menus
186+
* @returns output : string composed of HTML to be rendered.
187+
*/
188+
$menu_id = $this->CI->nav->getMenuID($menu_name);
189+
return $this->generateNav_fromID($menu_id);
190+
}
191+
192+
public function generateNav_fromID($menu_id) {
193+
/**
194+
* Generates output for menu from a menu ID as specified in
195+
* CI-Nav-Menus.
196+
* @param menu_id : int ID of the menu to be generate
197+
* @returns output : string composed of HTML to be rendered.
198+
*/
199+
200+
// Open Container
201+
$this->_output = $this->_navigation_open;
202+
203+
$top_level = $this->CI->nav->getTopLevelNav_byID($menu_id);
204+
205+
// if ($top_level->num_rows() > 0)
206+
// {
207+
foreach ($top_level->result() as $item)
208+
{
209+
// Output each nav item
210+
$this->_output .= $this->outputItem($item);
211+
}
212+
// }
213+
214+
$this->_output .= $this->_navigation_close;
215+
216+
return $this->_output;
217+
}
218+
219+
220+
221+
222+
}

models/nav_model.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ public function getMenuID($menu_name) {
2323
* @param $menu_name : string name associated to menu defined in CI-Nav-Menus
2424
* @returns int menu ID otherwise -1;
2525
*/
26+
if (isset($menu_name) && ctype_alnum ($menu_name)) {
27+
$query = $this->db->query('SELECT MenuID FROM `CI-Nav-Menus` WHERE MenuName = "' . $menu_name . '"');
28+
$row = $query->row();
29+
return $row->MenuID;
30+
}
2631

27-
$query = $this->db->query('SELECT `MenuName` FROM `CI-Nav-Menus` WHERE `MenuID` = ' . $menu_name);
28-
$row = $query->row();
29-
return $row->MenuName;
32+
return NULL;
3033
}
3134

3235
public function getTopLevelNav_byName($menu_name) {
@@ -38,7 +41,7 @@ public function getTopLevelNav_byName($menu_name) {
3841
*/
3942

4043
// Check $nav_name is not null
41-
if (isset($menu_name) && (strcmp(preg_replace("/[^a-zA-Z0-9]+/", "", $menu_name),$menu_name))) {
44+
if (isset($menu_name) && ctype_alnum($menu_name)) {
4245
// Get ID
4346
$menu_ID = $this->getMenuID($menu_name);
4447
// Return Menu
@@ -55,13 +58,13 @@ public function getTopLevelNav_byID($menu_ID) {
5558
* @returns : Query with result
5659
*/
5760

58-
if (isset($menu_ID) && $menu_ID != -1 && is_int($menu_ID)) {
61+
if (isset($menu_ID) && $menu_ID != -1 && ctype_digit($menu_ID)) {
5962

60-
$query = $this->db->query('SELECT `ItemName`, `ItemHumanName`, `ItemLink`
61-
FROM `CI-Nav-Items` AS I
62-
INNER JOIN `CI-Nav-InMenu` AS C
63+
$query = $this->db->query('SELECT `ItemName`, `ItemHumanName`, `ItemLink`, I.`ItemID`
64+
FROM `CI-Nav-Items` I
65+
INNER JOIN `CI-Nav-InMenu` C
6366
ON C.`ItemID` = I.`ItemID`
64-
WHERE C.`MenuID` = ' . $menu_ID);
67+
WHERE C.`MenuID` = ' . $menu_ID );
6568

6669
return $query;
6770

@@ -78,11 +81,11 @@ public function getSubItems($item_ID) {
7881
* @returns : Query with result of MenuItems
7982
*/
8083

81-
if (isset($item_ID) && is_int($item_ID)) {
84+
if (isset($item_ID) && ctype_digit($item_ID)) {
8285

8386
$query = $this->db->query('SELECT `ItemName`, `ItemHumanName`, `ItemLink`
8487
FROM `CI-Nav-Items`
85-
WHERE `ParentItem` = ' . $item_ID);
88+
WHERE `ParentItem` = ' . $item_ID );
8689
return $query;
8790

8891
}

0 commit comments

Comments
 (0)