Skip to content

Commit f033290

Browse files
committed
Updated Docs
1 parent 6569184 commit f033290

File tree

2 files changed

+118
-18
lines changed

2 files changed

+118
-18
lines changed

README.md

Lines changed: 111 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# CI3-Navigation-Library
22
Simple library for CodeIgniter v3 which outputs nav links in <ul> with support for active pages and multiple menus.
33

4-
Author: **Daniel Waghorn**
4+
The library makes use of an underlying database for storing menus and the items within these menus. This provides a solid and familiar foundation on which front end management tools can be built on to allow users to manage the menus.
5+
6+
Author: **Daniel Waghorn**
57
[daniel-waghorn.com](https://www.daniel-waghorn.com)
68

9+
## Licencing
10+
This project is released in the public domain under the [MIT Licence](http://opensource.org/licenses/MIT).
11+
712
## Structure
813
The folders in this repo are as follows:
914

@@ -18,9 +23,110 @@ The folders in this repo are as follows:
1823
## Usage
1924
### Setup
2025
1. Setup the database schema by importing the SQL file for navigation to create tables.
21-
2. Copy the contents of `libraries` and `models` into their respective folders in your `application` directory.
22-
3. Import the library whenever you need to output navigation; typically your header partial view using:
26+
2. Copy the contents of `config`, `libraries` and `models` into their respective folders in your `application` directory.
27+
3. Import the library whenever you need to output navigation and invoke generation by following the steps below at [Using the Library](#using-the-library)
28+
29+
30+
### Database Structure
31+
The tables used by the database should be in your primary CodeIgniter database and consist of `ci-nav-menus`, `ci-nav-items` and `ci-nav-inmenu`.
32+
33+
The `ci-nav-menus` table links `MenuID`s to a human readable `MenuName`. For example if I needed a user menu and an admin menu in my application this table would consist of two rows e.g:
34+
35+
| MenuID | MenuName |
36+
| ------ | ------------------- |
37+
| 1 | users |
38+
| 2 | admin |
39+
40+
41+
A table named `ci-nav-items` holds individual navigation items or links and their related data. This promotes reuse of items across multiple menus.
42+
43+
* `ItemID` is the primary key which is used to link items into menus.
44+
* `ItemName` is merely a name which makes the item easily recognisable in the database tables.
45+
* `ItemHumanName` is the name which is rendered in the output for the item. This can include HTML if you want to include e.g. an icon font for each item.
46+
* `ItemLink` is the URL that links to the item's destination. This should be relative if linking internally; for instance if your login url is `http://mysite.com/login` the value in this field would simply be `login`. If linking externally then include a fully qualified url such as `http://externalsite.com/page`.
47+
* `ParentItem` links nav items to another nav item which would be its parent. If a nav item has other nav items referring to its `ItemID` in their respective `ParentItem` field then a submenu will be rendered underneath this item.
48+
** N.B. This field should be [NULL] by default.
49+
50+
Finally a table named `ci-nav-inmenu` links nav items to menus. This table contains three columns:
51+
* `MenuID` which references the `MenuID` for a given menu in `ci-nav-menus`.
52+
* `ItemID` which references the `ItemID` for the item to link into the menu as in `ci-nav-items`
53+
* `LinkWeight` which assigns a weight to a particular relation. Larger `LinkWeight`s sink to the bottom/end of menu, lower `LinkWeight`s float to the top/start of menu.
54+
55+
For example to place a nav item with `ItemID` = 2 into the menu with `MenuID` = 1 the `ci-nav-inmenu` table should contain the following row:
56+
57+
| MenuID | ItemID | LinkWeight |
58+
| ------ | ------ | ---------- |
59+
| 1 | 2 | 50 |
60+
61+
I assigned `LinkWeight` 50 since on a scale of 1 to 100 this would be neutral, and leaves plenty of options to arrange links either side.
62+
63+
### Navigation Configurations
64+
This library supports interchangeable config files which control the markup used when rendering the navigation. These files reside in the `config` folder, where the default config file which is loaded is `navigation.php`.
65+
66+
This file contains PHPdoc explaining each of the settings and what they control.
67+
68+
To specify an alternative configuration to load simply supply an associative array as a second parameter when loading the library as below.
69+
70+
```php
71+
$this->load->library('navigation',array('config' => 'navigation_foundation'));
72+
```
73+
74+
The associative array should contain `'config' => 'config_file_name'`. ** Make sure not to include the .php extension when specifying `config_file_name`.
75+
76+
### Using the Library
77+
Using the library is simple; in each controller it's easiest to load it in the constructor for that controller like so:
78+
79+
```php
80+
$this->load->library('navigation');
81+
```
82+
83+
You can also alternatively autoload it or implement it in your `MY_Controller` superclass constructor.
84+
85+
Whenever you need to output navigation it's normally easiest to have CodeIgniter return the markup rather than render it. This way you can pass the markup to your views via `$data` and output it exactly where you need it.
86+
87+
An example of this would be something like:
88+
89+
```php
90+
$data['navigation'] = $this->navigation->generateNav_fromName('user');
91+
```
92+
93+
Where this would store the markup for the 'user' menu into `$data['navigation']`.
94+
95+
This data is then output in the view by placing `<?php echo $navigation ?>` wherever you want the markup to be placed.
96+
97+
### Function Description
98+
99+
The file at `libraries/Navigation.php` contains a few functions which you will need to invoke in order to create navigation.
100+
101+
This file contains PHPdoc explaining each along with parameters and return values.
102+
103+
#### Generate Navigation from Menu Name
104+
This function takes the name as specified in the `ci-nav-menus` table for `MenuName` and returns the menu with the linked items.
105+
Usage:
23106
```php
24-
$this->load->library('navigation');
107+
$data['navigation'] = $this->navigation->generateNav_fromName('public');
25108
```
26-
You can also autoload it if you need navigation on every page.
109+
110+
This would generate the markup for the menu with name 'public'.
111+
112+
#### Generate Navigation from Menu ID
113+
This function takes the id as specified in the `ci-nav-menus` table for `MenuID` and returns the menu with the linked items.
114+
Usage:
115+
```php
116+
$data['navigation'] = $this->navigation->generateNav_fromID(2);
117+
```
118+
119+
This would generate the markup for the menu with ID 2.
120+
121+
#### Generate Role Based Nav
122+
This function allows you to generate a menu based on a user's group association if your project is using [Ben Edmund's Ion Auth](http://benedmunds.com/ion_auth/ "Ion Auth Homepage") library.
123+
Define user groups in Ion Auth, then edit `generateRoleBasedNav` to return the menu specific to the context.
124+
125+
The default method contains a few examples for integration as well as support for public, user and admin menus out of the box.
126+
127+
Usage:
128+
```php
129+
$data['navigation'] = $this->navigation->generateRoleBasedNav();
130+
```
131+
132+

libraries/Navigation.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -265,25 +265,19 @@ public function generateNav_fromID($menu_id) {
265265
public function generateRoleBasedNav() {
266266
/**
267267
* Outputs navigation selectively based on user authentication
268+
* groups.
269+
* N.B. Required Ion Auth library.
270+
* http://benedmunds.com/ion_auth/
271+
*
268272
* @return HTML markup for navigation
269273
*/
270274

271275
if (!$this->CI->ion_auth->logged_in()){
272276
return $this->generateNav_fromName('public');
273277
} else {
274-
// Customer Group
275-
if ($this->CI->ion_auth->in_group('customer')){
276-
return $this->generateNav_fromName('customer');
277-
}
278-
279-
// Business Group
280-
if ($this->CI->ion_auth->in_group('business')){
281-
return $this->generateNav_fromName('business');
282-
}
283-
284-
// Introducer Group
285-
if ($this->CI->ion_auth->in_group('introducer')){
286-
return $this->generateNav_fromName('introducer');
278+
// User Group
279+
if ($this->CI->ion_auth->in_group('user')){
280+
return $this->generateNav_fromName('user');
287281
}
288282

289283
// Admins

0 commit comments

Comments
 (0)