1 <?php
2 /**
3 * @package Joomla.Libraries
4 * @subpackage Component
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * Rule to process URLs without a menu item
14 *
15 * @since 3.4
16 */
17 class JComponentRouterRulesNomenu implements JComponentRouterRulesInterface
18 {
19 /**
20 * Router this rule belongs to
21 *
22 * @var JComponentRouterView
23 * @since 3.4
24 */
25 protected $router;
26
27 /**
28 * Class constructor.
29 *
30 * @param JComponentRouterView $router Router this rule belongs to
31 *
32 * @since 3.4
33 */
34 public function __construct(JComponentRouterView $router)
35 {
36 $this->router = $router;
37 }
38
39 /**
40 * Dummymethod to fullfill the interface requirements
41 *
42 * @param array &$query The query array to process
43 *
44 * @return void
45 *
46 * @since 3.4
47 * @codeCoverageIgnore
48 */
49 public function preprocess(&$query)
50 {
51 }
52
53 /**
54 * Parse a menu-less URL
55 *
56 * @param array &$segments The URL segments to parse
57 * @param array &$vars The vars that result from the segments
58 *
59 * @return void
60 *
61 * @since 3.4
62 */
63 public function parse(&$segments, &$vars)
64 {
65 $active = $this->router->menu->getActive();
66
67 if (!is_object($active))
68 {
69 $views = $this->router->getViews();
70
71 if (isset($views[$segments[0]]))
72 {
73 $vars['view'] = array_shift($segments);
74
75 if (isset($views[$vars['view']]->key) && isset($segments[0]))
76 {
77 $vars[$views[$vars['view']]->key] = preg_replace('/-/', ':', array_shift($segments), 1);
78 }
79 }
80 }
81 }
82
83 /**
84 * Build a menu-less URL
85 *
86 * @param array &$query The vars that should be converted
87 * @param array &$segments The URL segments to create
88 *
89 * @return void
90 *
91 * @since 3.4
92 */
93 public function build(&$query, &$segments)
94 {
95 $menu_found = false;
96
97 if (isset($query['Itemid']))
98 {
99 $item = $this->router->menu->getItem($query['Itemid']);
100
101 if (!isset($query['option']) || ($item && $item->query['option'] === $query['option']))
102 {
103 $menu_found = true;
104 }
105 }
106
107 if (!$menu_found && isset($query['view']))
108 {
109 $views = $this->router->getViews();
110 if (isset($views[$query['view']]))
111 {
112 $view = $views[$query['view']];
113 $segments[] = $query['view'];
114
115 if ($view->key && isset($query[$view->key]))
116 {
117 if (is_callable(array($this->router, 'get' . ucfirst($view->name) . 'Segment')))
118 {
119 $result = call_user_func_array(array($this->router, 'get' . ucfirst($view->name) . 'Segment'), array($query[$view->key], $query));
120 $segments[] = str_replace(':', '-', array_shift($result));
121 }
122 else
123 {
124 $segments[] = str_replace(':', '-', $query[$view->key]);
125 }
126 unset($query[$views[$query['view']]->key]);
127 }
128 unset($query['view']);
129 }
130 }
131 }
132 }
133