1 | 2 | simandl | <?php |
2 | | | |
3 | | | namespace Phem\Environment; |
4 | | | |
5 | | | /** |
6 | | | * Description of LinkBuilder |
7 | | | * |
8 | | | * @author Vejvis |
9 | | | */ |
10 | | | class LinkBuilder |
11 | | | { |
12 | | | |
13 | | | public function link($params) |
14 | | | { |
15 | | | //EnvironmentManager::getLogger()->append("LinkBuilder -> link()", $params); |
16 | | | if (is_array($params)) |
17 | | | { |
18 | | | return $this->linkFromArray($params); |
19 | | | } |
20 | | | else |
21 | | | { |
22 | | | return $this->linkFomString($params); |
23 | | | } |
24 | | | } |
25 | | | |
26 | | | public function linkToResource($pathFromResources) |
27 | | | { |
28 | | | if (defined("RESOURCES_SUBDOMAIN")) |
29 | | | { |
30 | | | return "http://".RESOURCES_SUBDOMAIN."/".$pathFromResources; |
31 | | | } |
32 | | | else |
33 | | | { |
34 | | | return SITE_SUBDIR . DS . RESOURCES_SYMLINK . DS . $pathFromResources; |
35 | | | } |
36 | | | } |
37 | | | |
38 | | | public function externalLink($params) |
39 | | | { |
40 | | | return "http://" . $_SERVER["SERVER_NAME"] . $this->link($params); |
41 | | | } |
42 | | | |
43 | | | public function frontendLink($params) |
44 | | | { |
45 | | | if(is_array($params)) |
46 | | | { |
47 | | | return "http://".SROVNAVAC_ADMIN_DEFAULT_SITE . ".povinne-ruceni.tv" .$this->linkRewriteFromArray($params); |
48 | | | } |
49 | | | else |
50 | | | { |
51 | | | return "http://".SROVNAVAC_ADMIN_DEFAULT_SITE . ".povinne-ruceni.tv" .$this->linkRewriteFromString($params); |
52 | | | } |
53 | | | } |
54 | | | |
55 | | | private function linkRewriteFromArray($params) |
56 | | | { |
57 | | | $p = ""; |
58 | | | |
59 | | | $controller = (array_key_exists("controller", $params) ? $params["controller"] : null); |
60 | | | unset($params["controller"]); |
61 | | | |
62 | | | $task = (array_key_exists("task", $params) ? $params["task"] : null); |
63 | | | unset($params["task"]); |
64 | | | |
65 | | | $product = (array_key_exists("product", $params) ? $params["product"] : null); |
66 | | | unset($params["product"]); |
67 | | | |
68 | | | $uuid = (array_key_exists("uuid", $params) ? $params["uuid"] : null); |
69 | | | unset($params["uuid"]); |
70 | | | |
71 | | | foreach ($params as $key => $value) |
72 | | | { |
73 | | | $p.=$key . "=" . $value . "&"; |
74 | | | } |
75 | | | |
76 | | | $link = "/srovnani"; |
77 | | | if ($controller != null) |
78 | | | $link.="/$controller"; |
79 | | | |
80 | | | if ($task != null) |
81 | | | $link.="/$task"; |
82 | | | |
83 | | | if ($product != null) |
84 | | | $link.="/$product"; |
85 | | | else if ($uuid != null) |
86 | | | $link.="/$uuid"; |
87 | | | |
88 | | | if ($p != "") |
89 | | | $link.= "?" . substr($p, 0, strlen($p) - 1); |
90 | | | |
91 | | | return $link; |
92 | | | } |
93 | | | |
94 | | | private function linkNormalFromArray($params) |
95 | | | { |
96 | | | $p = ""; |
97 | | | |
98 | | | foreach ($params as $key => $value) |
99 | | | { |
100 | | | $p.=$key . "=" . $value . "&"; |
101 | | | } |
102 | | | |
103 | | | $link = "index.php"; |
104 | | | |
105 | | | if ($p != "") |
106 | | | $link.= "?" . substr($p, 0, strlen($p) - 1); |
107 | | | |
108 | | | return $link; |
109 | | | } |
110 | | | |
111 | | | private function linkFromArray($params) |
112 | | | { |
113 | | | |
114 | | | if (REWRITE_LINKS) |
115 | | | { |
116 | | | |
117 | | | return $this->linkRewriteFromArray($params); |
118 | | | } |
119 | | | else |
120 | | | { |
121 | | | return $this->linkNormalFromArray($params); |
122 | | | } |
123 | | | } |
124 | | | |
125 | | | private function linkRewriteFromString($link) |
126 | | | { |
127 | | | //pozice dvojteÄky |
128 | | | $p1 = strpos($link, ":"); |
129 | | | $p2 = strpos($link, "="); |
130 | | | |
131 | | | if (!$p1) |
132 | | | { |
133 | | | $odd = "="; |
134 | | | } |
135 | | | else if (!$p2) |
136 | | | { |
137 | | | $odd = ":"; |
138 | | | } |
139 | | | else if ($p2 < $p1) |
140 | | | { |
141 | | | $odd = "="; |
142 | | | } |
143 | | | else |
144 | | | { |
145 | | | $odd = ":"; |
146 | | | } |
147 | | | |
148 | | | |
149 | | | if ($link == "") |
150 | | | return $this->linkRewriteFromArray(array()); |
151 | | | $link = explode(",", $link); |
152 | | | $params = array(); |
153 | | | foreach ($link as $l) |
154 | | | { |
155 | | | $l = explode($odd, $l); |
156 | | | $params[$l[0]] = $l[1]; |
157 | | | } |
158 | | | return $this->linkRewriteFromArray($params); |
159 | | | } |
160 | | | |
161 | | | private function linkFomString($link) |
162 | | | { |
163 | | | //pozice dvojteÄky |
164 | | | $p1 = strpos($link, ":"); |
165 | | | $p2 = strpos($link, "="); |
166 | | | |
167 | | | if (!$p1) |
168 | | | { |
169 | | | $odd = "="; |
170 | | | } |
171 | | | else if (!$p2) |
172 | | | { |
173 | | | $odd = ":"; |
174 | | | } |
175 | | | else if ($p2 < $p1) |
176 | | | { |
177 | | | $odd = "="; |
178 | | | } |
179 | | | else |
180 | | | { |
181 | | | $odd = ":"; |
182 | | | } |
183 | | | |
184 | | | |
185 | | | if ($link == "") |
186 | | | return $this->linkFromArray(array()); |
187 | | | $link = explode(",", $link); |
188 | | | $params = array(); |
189 | | | foreach ($link as $l) |
190 | | | { |
191 | | | $l = explode($odd, $l); |
192 | | | $params[$l[0]] = $l[1]; |
193 | | | } |
194 | | | return $this->linkFromArray($params); |
195 | | | } |
196 | | | |
197 | | | public function setUrlParameter($url, $key, $value) |
198 | | | { |
199 | | | $parseUrl = parse_url($url); |
200 | | | parse_str($parseUrl['query'], $query); |
201 | | | $query[$key] = $value; |
202 | | | return $parseUrl['path'].'?'.http_build_query($query); |
203 | | | } |
204 | | | |
205 | | | public function navigate($controller,$task) |
206 | | | { |
207 | | | return $this->link(array("controller"=>$controller,"task"=>$task)); |
208 | | | } |
209 | | | |
210 | | | } |