| 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 | { |
|---|
| 46 | $this->urls[$name] = array('url' => $url, 'qs' => $params); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | * Registers a new url as a copy of an existing one |
|---|
| 51 | * @param string $name url name |
|---|
| 52 | * @param streing $orig url to copy information from |
|---|
| 53 | * @param array $params extra parameters to add |
|---|
| 54 | * @param string $newurl new url if different from the original |
|---|
| 55 | */ |
|---|
| 56 | public function registercopy ($name,$orig,$params = array(),$newurl='') |
|---|
| 57 | { |
|---|
| 58 | if (!isset($this->urls[$orig])) { |
|---|
| 59 | throw new exception ('Unknown URL handler for '.$orig); |
|---|
| 60 | } |
|---|
| 61 | $url = $this->urls[$orig]; |
|---|
| 62 | $url['qs'] = array_merge($url['qs'],$params); |
|---|
| 63 | if ($newurl != '') { |
|---|
| 64 | $url['url'] = $newurl; |
|---|
| 65 | } |
|---|
| 66 | $this->urls[$name] = $url; |
|---|
| 67 | |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | /** |
|---|
| 71 | * retrieves a URL given its name, and optional parameters |
|---|
| 72 | * |
|---|
| 73 | * @param string $name URL Name |
|---|
| 74 | * @param array $params query string parameters, given as an associative array |
|---|
| 75 | * @param string $separator separator to use between QS parameters |
|---|
| 76 | * @return string the forged url |
|---|
| 77 | */ |
|---|
| 78 | public function get ($name,$params=array(),$separator='&') |
|---|
| 79 | { |
|---|
| 80 | if (!isset($this->urls[$name])) { |
|---|
| 81 | throw new exception ('Unknown URL handler for '.$name); |
|---|
| 82 | } |
|---|
| 83 | $url = $this->urls[$name]; |
|---|
| 84 | $p = array_merge($url['qs'],$params); |
|---|
| 85 | $u = $url['url']; |
|---|
| 86 | if (!empty($p)) { |
|---|
| 87 | $u .= '?'.http_build_query($p,'',$separator); |
|---|
| 88 | } |
|---|
| 89 | return $u; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | /** |
|---|
| 93 | * Returns $urls property content. |
|---|
| 94 | * |
|---|
| 95 | * @return ArrayObject |
|---|
| 96 | */ |
|---|
| 97 | public function dumpUrls() { |
|---|
| 98 | return $this->urls; |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | |
|---|