@@ -16,11 +16,15 @@ abstract class Rb_Router
1616 protected $ _component = '' ;
1717 protected $ _menus = null ;
1818
19- public function __construct ()
20- {
21- // setup extension naming convention
22- $ this ->_component = Rb_Extension::getInstance ($ this ->_component );
23- }
19+
20+ /**
21+ *
22+ * Invoke to getroute parameter according to key
23+ * @param String $key , key have route path
24+ */
25+ abstract protected function _routes ($ key );
26+
27+
2428
2529 function getName ()
2630 {
@@ -34,17 +38,23 @@ function getName()
3438 }
3539 return $ name ;
3640 }
37-
38- public static function getInstance ($ name )
41+
42+ /**
43+ *
44+ * Invoke to get router instance
45+ * @param unknown_type $name
46+ * @param unknown_type $prefix
47+ */
48+ public static function getInstance ( $ prefix ='Rb_ ' )
3949 {
40- return Rb_Factory::getInstance ($ name , '' , $ this -> _component -> getPrefixClass () );
50+ return Rb_Factory::getInstance (' router ' , '' , $ prefix );
4151 }
4252
4353 // Load component menu records
4454 public function _getMenus ()
4555 {
4656 if ($ this ->_menus ===null ){
47- $ this ->_menus = JSite:: getMenu ()->getItems ('component_id ' ,JComponentHelper::getComponent ($ this ->_option )->id );
57+ $ this ->_menus = Rb_Factory:: getApplication ()-> getMenu (' site ' )->getItems ('component_id ' ,JComponentHelper::getComponent ($ this ->_component )->id );
4858 }
4959
5060 return $ this ->_menus ;
@@ -139,14 +149,30 @@ public function getSelectedMenu(&$query, $menus)
139149 }
140150
141151
152+ /**
153+ *
154+ * Invoke to get Slugify query parameter (Encode-logic)
155+ * @param Array $query Array of query Parameter
156+ * @param $var, Parameter need to slugify
157+ *
158+ * @return slug (slugify var)
159+ */
142160 protected function _slugify ($ query , $ var )
143161 {
144162 return $ query [$ var ];
145163 }
146164
147- protected function _deSlugify ($ var , $ value , $ parts )
165+ /**
166+ *
167+ * Invoke to de-slugify query parameter
168+ * @param $var
169+ * @param $value
170+ * @param $segments
171+ */
172+ protected function _deSlugify ($ var , &$ segments , $ parts )
148173 {
149- return $ value ;
174+ $ value = array_shift ($ segments );
175+ return $ value ;
150176 }
151177
152178 public function build ( &$ query )
@@ -156,15 +182,18 @@ public function build( &$query )
156182 $ temp_added_vars = array ();
157183 // if itemId is the first key, then these are menu links, only then consider it.
158184 // else the itemID might be of current page, not for the link
159- if (isset ($ query ['Itemid ' ]) && (array_shift (array_keys ($ query )) === 'Itemid ' ) ){
160- // if item-id exists, then pick the var from menu and put into query, if not exist already
161- $ item = JSite::getMenu ()->getItem ($ query ['Itemid ' ]);
162- foreach ($ item ->query as $ var =>$ value ){
163- if (!isset ($ query [$ var ])){
164- $ query [$ var ]= $ value ;
165- $ temp_added_vars []=$ var ;
166- }
167- }
185+ if (isset ($ query ['Itemid ' ]) ) {
186+ $ keys = array_keys ($ query );
187+ if ( (array_shift ($ keys ) === 'Itemid ' ) ){
188+ // if item-id exists, then pick the var from menu and put into query, if not exist already
189+ $ item = Rb_Factory::getApplication ()->getMenu ()->getItem ($ query ['Itemid ' ]);
190+ foreach ($ item ->query as $ var =>$ value ){
191+ if (!isset ($ query [$ var ])){
192+ $ query [$ var ]= $ value ;
193+ $ temp_added_vars []=$ var ;
194+ }
195+ }
196+ }
168197 }
169198
170199 //find the selected menu
@@ -176,7 +205,7 @@ public function build( &$query )
176205 }
177206
178207 //can we process the route further
179- $ key = @ $ query[ ' view ' ] . ' / ' . @ $ query [ ' task ' ] ;
208+ $ key = $ this -> getBuildKey ( $ query, $ segments , $ selMenu ) ;
180209 $ route =$ this ->_routes ($ key );
181210
182211 $ route = array_merge (array ('view ' , 'task ' ), $ route );
@@ -224,9 +253,16 @@ public function parse( &$segments )
224253 {
225254 // initialize
226255 $ parts = array ();
256+ $ view ='' ;
257+ $ task = '' ;
227258
259+ // fix segments (see JRouter::_decodeSegments)
260+ foreach (array_keys ($ segments ) as $ key ) {
261+ $ segments [$ key ] = str_replace (': ' , '- ' , $ segments [$ key ]);
262+ }
263+
228264 // find if any menu selected
229- $ item = JFactory ::getApplication ()->getMenu ()->getActive ();
265+ $ item = Rb_Factory ::getApplication ()->getMenu ()->getActive ();
230266
231267 // find view
232268 if (isset ($ item ->query ['view ' ])){
@@ -242,18 +278,36 @@ public function parse( &$segments )
242278 $ task = array_shift ($ segments );
243279 }
244280
245- $ key = $ view. ' / ' . $ task ;
281+ $ key = $ this -> getParseKey ( $ view, $ task, $ segments ) ;
246282 $ route =$ this ->_routes ($ key );
247283
248284 //remove not-required variables, which can be calculated from URL itself
285+ $ parts ['view ' ] = $ view ;
286+ $ parts ['task ' ] = $ task ;
287+
249288
250- $ parts = array ('view ' =>$ view , 'task ' => $ task );
251289 foreach ($ route as $ var ){
252- $ value = array_shift ($ segments );
253- $ value = $ this ->_deSlugify ($ var , $ value , $ parts );
290+ $ value = $ this ->_deSlugify ($ var ,$ segments , $ parts );
254291 $ parts [$ var ]=$ value ;
255292 }
256293
257294 return $ parts ;
258295 }
296+
297+ public function getBuildKey (&$ query , &$ segments , &$ selMenu )
298+ {
299+ return @$ query ['view ' ] .'/ ' . @$ query ['task ' ];
300+ }
301+
302+ /**
303+ *
304+ * Generate key. Using this key we will get query parameter
305+ * @param $view
306+ * @param $task
307+ */
308+ public function getParseKey ($ view , $ task , $ segments )
309+ {
310+ $ key = $ view .'/ ' .$ task ;
311+ return $ key ;
312+ }
259313}
0 commit comments