1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear |
---|
7 | # Licensed under the GPL version 2.0 license. |
---|
8 | # See LICENSE file or |
---|
9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
10 | # |
---|
11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
12 | if (!defined('DC_RC_PATH')) { return; } |
---|
13 | |
---|
14 | |
---|
15 | /** |
---|
16 | @ingroup DC_CORE |
---|
17 | @nosubgrouping |
---|
18 | @brief URL Handler for admin urls |
---|
19 | |
---|
20 | */ |
---|
21 | class dcAdminURL |
---|
22 | { |
---|
23 | /** @var dcCore dcCore instance */ |
---|
24 | protected $core; |
---|
25 | protected $urls; |
---|
26 | |
---|
27 | /** |
---|
28 | Inits dcAdminURL object |
---|
29 | |
---|
30 | @param core <b>dcCore</b> Dotclear core reference |
---|
31 | */ |
---|
32 | public function __construct($core) |
---|
33 | { |
---|
34 | $this->core = $core; |
---|
35 | $this->urls = new ArrayObject(); |
---|
36 | } |
---|
37 | |
---|
38 | /** |
---|
39 | * Registers a new url |
---|
40 | * @param string $name the url name |
---|
41 | * @param string $url url value |
---|
42 | * @param array $params query string params (optional) |
---|
43 | */ |
---|
44 | public function register ($name,$url,$params = array()) { |
---|
45 | $this->urls[$name] = array('url' => $url, 'qs' => $params); |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * Registers a new url as a copy of an existing one |
---|
50 | * @param string $name url name |
---|
51 | * @param streing $orig url to copy information from |
---|
52 | * @param array $params extra parameters to add |
---|
53 | * @param string $newurl new url if different from the original |
---|
54 | */ |
---|
55 | public function registercopy ($name,$orig,$params = array(),$newurl='') { |
---|
56 | if (!isset($this->urls[$orig])) { |
---|
57 | throw new exception ('Unknown URL handler for '.$orig); |
---|
58 | } |
---|
59 | $url = $this->urls[$orig]; |
---|
60 | $url['qs'] = array_merge($url['qs'],$params); |
---|
61 | if ($newurl != '') { |
---|
62 | $url['url'] = $newurl; |
---|
63 | } |
---|
64 | $this->urls[$name] = $url; |
---|
65 | |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * retrieves a URL given its name, and optional parameters |
---|
70 | * |
---|
71 | * @param string $name URL Name |
---|
72 | * @param array $params query string parameters, given as an associative array |
---|
73 | * @param string $separator separator to use between QS parameters |
---|
74 | * @return string the forged url |
---|
75 | */ |
---|
76 | public function get ($name,$params=array(),$separator='&') { |
---|
77 | if (!isset($this->urls[$name])) { |
---|
78 | throw new exception ('Unknown URL handler for '.$name); |
---|
79 | } |
---|
80 | $url = $this->urls[$name]; |
---|
81 | $p = array_merge($url['qs'],$params); |
---|
82 | $u = $url['url']; |
---|
83 | if (!empty($p)) { |
---|
84 | $u .= '?'.http_build_query($p,'',$separator); |
---|
85 | } |
---|
86 | return $u; |
---|
87 | } |
---|
88 | |
---|
89 | } |
---|
90 | |
---|