Router.php
3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
namespace Nextend\Framework\Router;
use Nextend\Framework\Form\Form;
use Nextend\Framework\Router\Base\PlatformRouter;
use Nextend\Framework\Router\WordPress\WordPressRouter;
use Nextend\Framework\Url\UrlHelper;
class Router {
/**
* @var Base\PlatformRouter
*/
private $platformRouter;
/**
* @var string
*/
protected $baseUrl;
/**
* @var bool|string
*/
protected $baseUrlAjax;
/**
* @var bool|string
*/
protected $networkUrl = false;
/**
* Router constructor.
*
* @param string $baseUrl
* @param string|bool $baseUrlAjax
* @param string|bool $networkUrl
*/
public function __construct($baseUrl, $baseUrlAjax = false, $networkUrl = false) {
$this->platformRouter = new WordPressRouter($this);
$this->baseUrl = $baseUrl;
if ($baseUrlAjax === false) {
$baseUrlAjax = UrlHelper::add_query_arg(array('nextendajax' => 1), $this->baseUrl);
}
$this->baseUrlAjax = $baseUrlAjax;
$this->networkUrl = $networkUrl;
}
/**
* @return string
*/
public function getBaseUrl() {
return $this->baseUrl;
}
/**
* @param string $baseUrl
*/
public function setBaseUrl($baseUrl) {
$this->baseUrl = $baseUrl;
}
/**
* @return bool|string
*/
public function getNetworkUrl() {
return $this->networkUrl;
}
/**
* @param array|string $url
* @param bool $isPost
* @param bool $isAjax
*
* @return string
*/
public function createUrl($url, $isPost = false, $isAjax = false) {
//create url from array
// [0] = controller/method
// [1] = query parameters
if (is_array($url)) {
$href = $this->route($url[0], (isset($url[1]) ? $url[1] : array()), $isPost, $isAjax);
} elseif (filter_var($url, FILTER_VALIDATE_URL)) {
//completed url, no mods, just fun
$href = $url;
} elseif (strpos($url, "/") !== false) {
//create url from string
//format: controller/method
$href = $this->route($url, array(), $isPost, $isAjax);
} else {
//fake link, replace to hashtag
$href = "#";
}
return $href;
}
/**
* @param array|string $url
*
* @return string
*/
public function createAjaxUrl($url) {
return $this->createUrl($url, false, true);
}
/**
* @param $url
* @param array $queryArgs
* @param bool $isPost
* @param bool $isAjax
*
* @return string
*/
private function route($url, $queryArgs = array(), $isPost = false, $isAjax = false) {
if ($isAjax) {
$baseUrl = $this->baseUrlAjax;
} else {
$baseUrl = $this->baseUrl;
}
$parsedAction = explode("/", $url);
$queryArgs = array(
'nextendcontroller' => strtolower(trim($parsedAction[0])),
'nextendaction' => strtolower(trim($parsedAction[1]))
) + $queryArgs;
if ($isPost || $isAjax) {
$queryArgs += Form::tokenizeUrl();
}
return UrlHelper::add_query_arg($queryArgs, $baseUrl);
}
public function setMultiSite() {
$this->platformRouter->setMultiSite();
}
public function unSetMultiSite() {
$this->platformRouter->unSetMultiSite();
}
}