Skip to content

Commit 7ba2ec2

Browse files
committed
Updated schema to include link weights
Also improved security by binding params to statements.
1 parent fb57c96 commit 7ba2ec2

File tree

5 files changed

+124
-40
lines changed

5 files changed

+124
-40
lines changed

config/navigation.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
* Contains the closing tag e.g. </ul>
1212
* @var item_open:
1313
* Contains the open tag for a nav item e.g. <li>
14-
* @var item_open_active:
15-
* Contains the open tag for an active item e.g.
16-
* <li class="active">
14+
* @var item_open_active_class:
15+
* Contains class to be added to an open tag which is active link.
16+
* E.G. 'active'
17+
* @var item_open_dropdown_class:
18+
* Contains class to be added to a dropdown parent link open tag.
19+
* E.G. 'dropdown'
1720
* @var item_close:
1821
* Contains the close tag for an item e.g. </li>
1922
* @var anchor:
@@ -35,12 +38,16 @@
3538

3639
$config['item_open'] = '<li>';
3740

38-
$config['item_open_active'] = '<li class="active">';
41+
$config['item_open_active_class'] = 'active';
42+
43+
$config['item_open_dropdown_class'] = 'dropdown';
3944

4045
$config['item_close'] = '</li>';
4146

4247
$config['anchor'] = '<a href="{$url}" {$extra}>{$text}</a>';
4348

49+
$config['anchor_dropdown'] = '<a href="{$url}" {$extra}>{$text}</a>';
50+
4451
$config['dropdown_open'] = '<ul class="dropdown">';
4552

4653
$config['dropdown_close'] = '</ul>';

config/navigation_foundation.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
* Contains the closing tag e.g. </ul>
1212
* @var item_open:
1313
* Contains the open tag for a nav item e.g. <li>
14-
* @var item_open_active:
15-
* Contains the open tag for an active item e.g.
16-
* <li class="active">
14+
* @var item_open_active_class:
15+
* Contains class to be added to an open tag which is active link.
16+
* E.G. 'active'
17+
* @var item_open_dropdown_class:
18+
* Contains class to be added to a dropdown parent link open tag.
19+
* E.G. 'dropdown'
1720
* @var item_close:
1821
* Contains the close tag for an item e.g. </li>
1922
* @var anchor:
@@ -35,12 +38,16 @@
3538

3639
$config['item_open'] = '<li>';
3740

38-
$config['item_open_active'] = '<li class="active">';
41+
$config['item_open_active_class'] = 'active';
42+
43+
$config['item_open_dropdown_class'] = '';
3944

4045
$config['item_close'] = '</li>';
4146

4247
$config['anchor'] = '<a href="{$url}" {$extra}>{$text}</a>';
4348

49+
$config['anchor'] = '<a href="{$url}" {$extra}>{$text}</a>';
50+
4451
$config['dropdown_open'] = '<ul class="has-dropdown">';
4552

4653
$config['dropdown_close'] = '</ul>';

libraries/Navigation.php

Lines changed: 86 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ class Navigation {
1616
* Contains the closing tag e.g. </ul>
1717
* @var $_item_open:
1818
* 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">
19+
* @var $_item_open_active_class:
20+
* Contains the class for an active item e.g. "active"
21+
* @var $_item_open_dropdown_class:
22+
* Contains the class for an item which has subitems e.g. "dropdown"
2223
* @var $_item_close:
2324
* Contains the close tag for an item e.g. </li>
2425
* @var $_anchor:
@@ -27,6 +28,8 @@ class Navigation {
2728
* where $url is the link to the item,
2829
* $extra is any additional attributes e.g. class="main"
2930
* and $text is the text to be held in the anchor.
31+
* @var $_anchor_dropdown:
32+
* Different template for dropdown parent links.
3033
* @var $_dropdown_open:
3134
* Contains the open tag for a dropdown e.g. <ul class="dropdown">
3235
* @var $_dropdown_close:
@@ -37,9 +40,11 @@ class Navigation {
3740
private $_navigation_open;
3841
private $_navigation_close;
3942
private $_item_open;
40-
private $_item_open_active;
43+
private $_item_open_active_class;
44+
private $_item_open_dropdown_class;
4145
private $_item_close;
4246
private $_anchor;
47+
private $_anchor_dropdown;
4348
private $_dropdown_open;
4449
private $_dropdown_close;
4550

@@ -73,9 +78,11 @@ public function __construct($params = array('config' => 'navigation'))
7378
$this->_navigation_open = $this->CI->config->item('navigation_open',$params['config']);
7479
$this->_navigation_close = $this->CI->config->item('navigation_close',$params['config']);
7580
$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']);
81+
$this->_item_open_active_class = $this->CI->config->item('item_open_active_class',$params['config']);
82+
$this->_item_open_dropdown_class = $this->CI->config->item('item_open_dropdown_class',$params['config']);
7783
$this->_item_close = $this->CI->config->item('item_close',$params['config']);
7884
$this->_anchor = $this->CI->config->item('anchor',$params['config']);
85+
$this->_anchor_dropdown = $this->CI->config->item('anchor_dropdown',$params['config']);
7986
$this->_dropdown_open = $this->CI->config->item('dropdown_open',$params['config']);
8087
$this->_dropdown_close = $this->CI->config->item('dropdown_close',$params['config']);
8188

@@ -97,25 +104,34 @@ function isCurrentPage($url) {
97104
*/
98105

99106
// Remove site url
100-
$page_url = str_replace($this->_current_url,$this->_base_url,'');
101-
return strcmp($url,$page_url);
107+
$page_url = str_replace(rtrim($this->_base_url,"/"),"",$this->_current_url);
108+
if(empty($page_url)){
109+
$page_url = "/";
110+
}
111+
return strcmp("/" . $url,$page_url) == 0;
102112
}
103113

104-
function bindAnchor($url, $text, $extra = '') {
114+
function bindAnchor($url, $text, $extra = '',$isDropdown = false) {
105115
/**
106116
* Takes parameters for an anchor and binds them to template.
107117
* @param url : url to put in href
108118
* @param text : text to put between anchor
109119
* @param OPTIONAL extra : extra attributes and data
120+
* @param OPTIONAL isDropdown : boolean indicating if dropdown or not,
121+
* changes url template.
110122
*/
111123

112124
$vars = array(
113-
'{$url}' => $url,
125+
'{$url}' => $this->_base_url . $url,
114126
'{$text}' => $text,
115127
'{$extra}' => $extra
116128
);
117129

118-
return strtr($this->_anchor, $vars);
130+
if ($isDropdown) {
131+
return strtr($this->_anchor_dropdown,$vars);
132+
} else {
133+
return strtr($this->_anchor, $vars);
134+
}
119135
}
120136

121137
function outputItem($item) {
@@ -127,20 +143,38 @@ function outputItem($item) {
127143

128144
$output = '';
129145

130-
if ($this->isCurrentPage($item->ItemLink)) {
131-
$output .= $this->_item_open_active;
132-
} else {
133-
$output .= $this->_item_open;
134-
}
146+
$classes = '';
135147

136-
// Output link
137-
$output .= $this->bindAnchor($item->ItemLink, $item->ItemHumanName);
148+
$output .= $this->_item_open;
138149

139150
// Check for sub items.
140151
$subItems = $this->CI->nav->getSubItems($item->ItemID);
141152

142-
if (count($subItems->result_array()) > 0) {
143-
$this->renderDropdown($subItems);
153+
if ($this->isCurrentPage($item->ItemLink)) {
154+
$classes .= $this->_item_open_active_class . ' ';
155+
}
156+
157+
if (!is_null($subItems) && count($subItems->result()) > 0){
158+
// See if we have dropdown
159+
$classes .= $this->_item_open_dropdown_class . ' ';
160+
}
161+
162+
if(!strcmp($classes,'') == 0) {
163+
// If classes to add them append to open tag
164+
$output = str_replace('>',' class="' . $classes . '">',$output);
165+
}
166+
167+
// Output link
168+
if (!is_null($subItems) && count($subItems->result()) > 0) {
169+
$output .= $this->bindAnchor($item->ItemLink, $item->ItemHumanName, '', $this->_anchor_dropdown);
170+
} else {
171+
$output .= $this->bindAnchor($item->ItemLink, $item->ItemHumanName);
172+
}
173+
174+
if (!is_null($subItems)){
175+
if (count($subItems->result()) > 0) {
176+
$output .= $this->renderDropdown($subItems);
177+
}
144178
}
145179

146180
$output .= $this->_item_close;
@@ -157,7 +191,7 @@ function renderDropdown($subItems) {
157191

158192
$output = $this->_dropdown_open;
159193

160-
foreach ($subItems->result_array() as $item) {
194+
foreach ($subItems->result() as $item) {
161195

162196
// Check if current page and open item
163197
if ($this->isCurrentPage($item->ItemLink)) {
@@ -202,21 +236,50 @@ public function generateNav_fromID($menu_id) {
202236

203237
$top_level = $this->CI->nav->getTopLevelNav_byID($menu_id);
204238

205-
// if ($top_level->num_rows() > 0)
206-
// {
239+
if (count($top_level->result()) > 0)
240+
{
207241
foreach ($top_level->result() as $item)
208242
{
209243
// Output each nav item
210244
$this->_output .= $this->outputItem($item);
211245
}
212-
// }
246+
}
213247

214248
$this->_output .= $this->_navigation_close;
215249

216250
return $this->_output;
217251
}
218252

253+
public function generateRoleBasedNav() {
254+
/**
255+
* Outputs navigation selectively based on user authentication
256+
* @returns HTML markup for navigation
257+
*/
258+
259+
if (!$this->CI->ion_auth->logged_in()){
260+
return $this->generateNav_fromName('public');
261+
} else {
262+
// Customer Group
263+
if ($this->CI->ion_auth->in_group('customer')){
264+
return $this->generateNav_fromName('customer');
265+
}
266+
267+
// Business Group
268+
if ($this->CI->ion_auth->in_group('business')){
269+
return $this->generateNav_fromName('business');
270+
}
271+
272+
// Introducer Group
273+
if ($this->CI->ion_auth->in_group('introducer')){
274+
return $this->generateNav_fromName('introducer');
275+
}
219276

277+
// Admins
278+
if ($this->CI->ion_auth->is_admin()){
279+
return $this->generateNav_fromName('admin');
280+
}
281+
}
282+
}
220283

221284

222285
}

models/nav_model.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function getMenuID($menu_name) {
2424
* @returns int menu ID otherwise -1;
2525
*/
2626
if (isset($menu_name) && ctype_alnum ($menu_name)) {
27-
$query = $this->db->query('SELECT MenuID FROM `CI-Nav-Menus` WHERE MenuName = "' . $menu_name . '"');
27+
$query = $this->db->query('SELECT `MenuID` FROM `CI-Nav-Menus` WHERE `MenuName` = ?',array($menu_name));
2828
$row = $query->row();
2929
return $row->MenuID;
3030
}
@@ -60,11 +60,11 @@ public function getTopLevelNav_byID($menu_ID) {
6060

6161
if (isset($menu_ID) && $menu_ID != -1 && ctype_digit($menu_ID)) {
6262

63-
$query = $this->db->query('SELECT `ItemName`, `ItemHumanName`, `ItemLink`, I.`ItemID`
63+
$query = $this->db->query('SELECT `ItemName`, `ItemHumanName`, `ItemLink`, C.`ItemID`
6464
FROM `CI-Nav-Items` I
6565
INNER JOIN `CI-Nav-InMenu` C
6666
ON C.`ItemID` = I.`ItemID`
67-
WHERE C.`MenuID` = ' . $menu_ID );
67+
WHERE C.`MenuID` = ? ORDER BY `LinkWeight` ASC',array($menu_ID));
6868

6969
return $query;
7070

@@ -85,7 +85,7 @@ public function getSubItems($item_ID) {
8585

8686
$query = $this->db->query('SELECT `ItemName`, `ItemHumanName`, `ItemLink`
8787
FROM `CI-Nav-Items`
88-
WHERE `ParentItem` = ' . $item_ID );
88+
WHERE `ParentItem` = ? ORDER BY `ItemName` ASC',array($item_ID));
8989
return $query;
9090

9191
}

sql/MySQL_Nav_Tables.sql

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
/*
2-
* SQL File to create tables for CodeIgniter Navigation Library
3-
* Author: Daniel Waghorn
4-
* Format: MySQL;
2+
MySQL File to create Schema for CI3-Navigation-Library
3+
Author: Daniel Waghorn
4+
www.daniel-waghorn.com
5+
6+
Target Server Type : MySQL
7+
Target Server Version : 50625
8+
File Encoding : utf-8
9+
10+
Date: 06/16/2015 11:48:52 AM
511
*/
612

713
SET NAMES utf8;
@@ -14,6 +20,7 @@ DROP TABLE IF EXISTS `CI-Nav-InMenu`;
1420
CREATE TABLE `CI-Nav-InMenu` (
1521
`MenuID` int(11) DEFAULT NULL,
1622
`ItemID` int(11) DEFAULT NULL,
23+
`LinkWeight` int(11) DEFAULT '0',
1724
KEY `MenuID` (`MenuID`),
1825
KEY `ItemID` (`ItemID`),
1926
CONSTRAINT `ItemIDConst` FOREIGN KEY (`ItemID`) REFERENCES `CI-Nav-Items` (`ItemID`) ON DELETE CASCADE ON UPDATE CASCADE,
@@ -33,7 +40,7 @@ CREATE TABLE `CI-Nav-Items` (
3340
PRIMARY KEY (`ItemID`),
3441
KEY `fkParentMenu` (`ParentItem`),
3542
CONSTRAINT `ParentRef` FOREIGN KEY (`ParentItem`) REFERENCES `CI-Nav-Items` (`ItemID`) ON DELETE CASCADE ON UPDATE CASCADE
36-
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
43+
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8;
3744

3845
-- ----------------------------
3946
-- Table structure for `CI-Nav-Menus`
@@ -43,6 +50,6 @@ CREATE TABLE `CI-Nav-Menus` (
4350
`MenuID` int(11) NOT NULL AUTO_INCREMENT,
4451
`MenuName` varchar(255) NOT NULL,
4552
PRIMARY KEY (`MenuID`)
46-
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
53+
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
4754

4855
SET FOREIGN_KEY_CHECKS = 1;

0 commit comments

Comments
 (0)