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
+ }
0 commit comments