1 <?php
2
3 /**
4 * Generic interface for classes which provide urls.
5 *
6 * @author staabm
7 *
8 * @package redaxo\core
9 */
10 interface rex_url_provider_interface
11 {
12 /**
13 * Returns a Url which contains the given parameters.
14 *
15 * @param array $params A scalar array containing key value pairs for the parameter and its value
16 * @param bool $escape Flag whether the argument separator "&" should be escaped (&)
17 *
18 * @return string The generated Url
19 */
20 public function getUrl(array $params = [], $escape = true);
21 }
22
23 /**
24 * Generic interface for classes which provide a complete rex-context.
25 * A rex-context consists of a set of parameters which may get passed using urls (via parameter) or forms (via hidden input fields).
26 *
27 * @author staabm
28 *
29 * @package redaxo\core
30 */
31 interface rex_context_provider_interface extends rex_url_provider_interface
32 {
33 /**
34 * Returns a html string containg hidden input fields for the given parameters.
35 *
36 * @param array $params A array containing key value pairs for the parameter and its value
37 *
38 * @return string The generated html source containing the hidden input fields
39 */
40 public function getHiddenInputFields(array $params = []);
41 }
42
43 /**
44 * A generic implementiation of rex_context_provider.
45 *
46 * @author staabm
47 *
48 * @package redaxo\core
49 */
50 class rex_context implements rex_context_provider_interface
51 {
52 private $globalParams;
53
54 /**
55 * Constructs a rex_context with the given global parameters.
56 *
57 * @param array $globalParams A array containing only scalar values for key/value
58 */
59 public function __construct(array $globalParams = [])
60 {
61 $this->globalParams = $globalParams;
62 }
63
64 /**
65 * {@inheritdoc}
66 */
67 public function getUrl(array $params = [], $escape = true)
68 {
69 // combine global params with local
70 $_params = array_merge($this->globalParams, $params);
71
72 return rex::isBackend() ? rex_url::backendController($_params, $escape) : rex_url::frontendController($_params, $escape);
73 }
74
75 /**
76 * Set a global parameter.
77 *
78 * @param string $name
79 * @param mixed $value
80 */
81 public function setParam($name, $value)
82 {
83 $this->globalParams[$name] = $value;
84 }
85
86 /**
87 * Returns the value associated with the given parameter $name.
88 * When no value is set, $default will be returned.
89 *
90 * @param string $name
91 * @param string $default
92 *
93 * @return mixed
94 */
95 public function getParam($name, $default = null)
96 {
97 return isset($this->globalParams[$name]) ? $this->globalParams[$name] : $default;
98 }
99
100 /**
101 * @see rex_context_provider::getHiddenInputFields()
102 */
103 public function getHiddenInputFields(array $params = [])
104 {
105 // combine global params with local
106 $_params = array_merge($this->globalParams, $params);
107
108 return self::array2inputStr($_params);
109 }
110
111 /**
112 * Returns a rex_context instance containing all GET and POST parameters.
113 *
114 * @return self
115 */
116 public static function restore()
117 {
118 // $_REQUEST contains some server specific globals, therefore we merge GET and POST manually
119 return new self($_GET + $_POST);
120 }
121
122 /**
123 * Returns a rex_context instance containing all GET parameters.
124 *
125 * @return self
126 */
127 public static function fromGet()
128 {
129 return new self($_GET);
130 }
131
132 /**
133 * Returns a rex_context instance containing all POST parameters.
134 *
135 * @return self
136 */
137 public static function fromPost()
138 {
139 return new self($_POST);
140 }
141
142 /**
143 * Helper method to generate a html string with hidden input fields from an array key-value pairs.
144 *
145 * @param array $array The array which contains the key-value pairs for convertion
146 *
147 * @return string
148 */
149 private static function array2inputStr(array $array)
150 {
151 $inputString = '';
152 foreach ($array as $name => $value) {
153 if (is_array($value)) {
154 foreach ($value as $valName => $valVal) {
155 $inputString .= '<input type="hidden" name="' . rex_escape($name) . '[' . rex_escape($valName) . ']" value="' . rex_escape($valVal) . '" />';
156 }
157 } else {
158 $inputString .= '<input type="hidden" name="' . rex_escape($name) . '" value="' . rex_escape($value) . '" />';
159 }
160 }
161
162 return $inputString;
163 }
164 }
165