|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Response.php - The Jaxon Response |
|
5
|
|
|
* |
|
6
|
|
|
* This class collects commands to be sent back to the browser in response to a jaxon request. |
|
7
|
|
|
* Commands are encoded and packaged in json format. |
|
8
|
|
|
* |
|
9
|
|
|
* @package jaxon-core |
|
10
|
|
|
* @author Jared White |
|
11
|
|
|
* @author J. Max Wilson |
|
12
|
|
|
* @author Joseph Woolley |
|
13
|
|
|
* @author Steffen Konerow |
|
14
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
15
|
|
|
* @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson |
|
16
|
|
|
* @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White & J. Max Wilson |
|
17
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
|
18
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
19
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace Jaxon\Response; |
|
23
|
|
|
|
|
24
|
|
|
use Jaxon\Exception\AppException; |
|
25
|
|
|
use Jaxon\Script\JqCall; |
|
26
|
|
|
use Jaxon\Script\JsExpr; |
|
27
|
|
|
use Jaxon\Script\JsCall; |
|
28
|
|
|
use Jaxon\Plugin\Response\DataBag\DataBagContext; |
|
29
|
|
|
use Jaxon\Plugin\Response\DataBag\DataBagPlugin; |
|
30
|
|
|
use Jaxon\Plugin\Response\Pagination\Paginator; |
|
31
|
|
|
use Jaxon\Plugin\Response\Pagination\PaginatorPlugin; |
|
32
|
|
|
use Jaxon\Plugin\Response\Psr\PsrPlugin; |
|
33
|
|
|
use Jaxon\Plugin\Response\Script\ScriptPlugin; |
|
34
|
|
|
use Psr\Http\Message\ResponseInterface as PsrResponseInterface; |
|
35
|
|
|
use Closure; |
|
36
|
|
|
|
|
37
|
|
|
use function array_shift; |
|
38
|
|
|
use function func_get_args; |
|
39
|
|
|
use function json_encode; |
|
40
|
|
|
|
|
41
|
|
|
abstract class AjaxResponse extends AbstractResponse |
|
42
|
|
|
{ |
|
43
|
|
|
/** |
|
44
|
|
|
* @inheritDoc |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getContentType(): string |
|
47
|
|
|
{ |
|
48
|
|
|
return 'application/json'; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritDoc |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getOutput(): string |
|
55
|
|
|
{ |
|
56
|
|
|
return json_encode(['jxn' => ['commands' => $this->xManager->getCommands()]]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Add a command to call the specified javascript function with the given (optional) parameters |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $sFunc The name of the function to call |
|
63
|
|
|
* |
|
64
|
|
|
* @return self |
|
65
|
|
|
*/ |
|
66
|
|
|
public function call(string $sFunc): self |
|
67
|
|
|
{ |
|
68
|
|
|
$aArgs = func_get_args(); |
|
69
|
|
|
array_shift($aArgs); |
|
70
|
|
|
$this->xManager->addCommand('script.exec.call', |
|
71
|
|
|
['func' => $this->str($sFunc), 'args' => $aArgs]); |
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Add a command to execute the specified json expression |
|
77
|
|
|
* |
|
78
|
|
|
* @param JsExpr $xJsExpr The json expression to execute |
|
79
|
|
|
* |
|
80
|
|
|
* @return self |
|
81
|
|
|
*/ |
|
82
|
|
|
public function exec(JsExpr $xJsExpr): self |
|
83
|
|
|
{ |
|
84
|
|
|
$this->xManager->addCommand('script.exec.expr', ['expr' => $xJsExpr]); |
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Response command that prompts user with [ok] [cancel] style message box |
|
90
|
|
|
* |
|
91
|
|
|
* The provided closure will be called with a response object as unique parameter. |
|
92
|
|
|
* If the user clicks cancel, the response commands defined in the closure will be skipped. |
|
93
|
|
|
* |
|
94
|
|
|
* @param Closure $fConfirm A closure that defines the commands that can be skipped |
|
95
|
|
|
* @param string $sQuestion The question to ask to the user |
|
96
|
|
|
* @param array $aArgs The arguments for the placeholders in the question |
|
97
|
|
|
* |
|
98
|
|
|
* @throws AppException |
|
99
|
|
|
* |
|
100
|
|
|
* @return self |
|
101
|
|
|
*/ |
|
102
|
|
|
public function confirm(Closure $fConfirm, string $sQuestion, array $aArgs = []): self |
|
103
|
|
|
{ |
|
104
|
|
|
$this->xManager->addConfirmCommand('script.confirm', |
|
105
|
|
|
fn() => $fConfirm($this), $sQuestion, $aArgs); |
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Add a command to display an alert message to the user |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $sMessage The message to be displayed |
|
113
|
|
|
* @param array $aArgs The arguments for the placeholders in the message |
|
114
|
|
|
* |
|
115
|
|
|
* @return self |
|
116
|
|
|
*/ |
|
117
|
|
|
public function alert(string $sMessage, array $aArgs = []): self |
|
118
|
|
|
{ |
|
119
|
|
|
$this->xManager->addAlertCommand('dialog.alert.show', $sMessage, $aArgs); |
|
120
|
|
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Add a command to display a debug message to the user |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $sMessage The message to be displayed |
|
127
|
|
|
* |
|
128
|
|
|
* @return self |
|
129
|
|
|
*/ |
|
130
|
|
|
public function debug(string $sMessage): self |
|
131
|
|
|
{ |
|
132
|
|
|
$this->xManager->addCommand('script.debug', ['message' => $this->str($sMessage)]); |
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Add a command to ask the browser to navigate to the specified URL |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $sURL The relative or fully qualified URL |
|
140
|
|
|
* @param integer $nDelay Number of seconds to delay before the redirect occurs |
|
141
|
|
|
* |
|
142
|
|
|
* @return self |
|
143
|
|
|
*/ |
|
144
|
|
|
public function redirect(string $sURL, int $nDelay = 0): self |
|
145
|
|
|
{ |
|
146
|
|
|
$this->xManager->addCommand('script.redirect', [ |
|
147
|
|
|
'delay' => $nDelay, |
|
148
|
|
|
'url' => $this->xPluginManager->getParameterReader()->parseUrl($sURL), |
|
149
|
|
|
]); |
|
150
|
|
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Add a command to make Jaxon to pause execution of the response commands, |
|
155
|
|
|
* returning control to the browser so it can perform other commands asynchronously. |
|
156
|
|
|
* |
|
157
|
|
|
* After the specified delay, Jaxon will continue execution of the response commands. |
|
158
|
|
|
* |
|
159
|
|
|
* @param integer $tenths The number of 1/10ths of a second to sleep |
|
160
|
|
|
* |
|
161
|
|
|
* @return self |
|
162
|
|
|
*/ |
|
163
|
|
|
public function sleep(int $tenths): self |
|
164
|
|
|
{ |
|
165
|
|
|
$this->xManager->addCommand('script.sleep', ['duration' => $tenths]); |
|
166
|
|
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Create a JQuery selector expression, and link it to the current response. |
|
171
|
|
|
* |
|
172
|
|
|
* @param string $sPath The jQuery selector path |
|
173
|
|
|
* @param mixed $xContext A context associated to the selector |
|
174
|
|
|
* |
|
175
|
|
|
* @return JqCall |
|
176
|
|
|
*/ |
|
177
|
|
|
public function jq(string $sPath = '', $xContext = null): JqCall |
|
178
|
|
|
{ |
|
179
|
|
|
/** @var ScriptPlugin */ |
|
180
|
|
|
$xPlugin = $this->plugin('script'); |
|
181
|
|
|
return $xPlugin->jq($sPath, $xContext); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Create a js expression, and link it to the current response. |
|
186
|
|
|
* |
|
187
|
|
|
* @param string $sObject |
|
188
|
|
|
* |
|
189
|
|
|
* @return JsCall |
|
190
|
|
|
*/ |
|
191
|
|
|
public function js(string $sObject = ''): JsCall |
|
192
|
|
|
{ |
|
193
|
|
|
/** @var ScriptPlugin */ |
|
194
|
|
|
$xPlugin = $this->plugin('script'); |
|
195
|
|
|
return $xPlugin->js($sObject); |
|
|
|
|
|
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Shortcut to get the factory for calls to a global js object or function. |
|
200
|
|
|
* |
|
201
|
|
|
* @return JsCall |
|
202
|
|
|
*/ |
|
203
|
|
|
public function jw(): JsCall |
|
204
|
|
|
{ |
|
205
|
|
|
return $this->js('w'); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Get the databag with a given name |
|
210
|
|
|
* |
|
211
|
|
|
* @param string $sName |
|
212
|
|
|
* |
|
213
|
|
|
* @return DataBagContext |
|
214
|
|
|
*/ |
|
215
|
|
|
public function bag(string $sName): DataBagContext |
|
216
|
|
|
{ |
|
217
|
|
|
/** @var DataBagPlugin */ |
|
218
|
|
|
$xPlugin = $this->plugin('bags'); |
|
219
|
|
|
return $xPlugin->bag($sName); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Render an HTML pagination control. |
|
224
|
|
|
* |
|
225
|
|
|
* @param int $nPageNumber The current page number |
|
226
|
|
|
* @param int $nItemsPerPage The number of items per page |
|
227
|
|
|
* @param int $nTotalItems The total number of items |
|
228
|
|
|
* |
|
229
|
|
|
* @return Paginator |
|
230
|
|
|
*/ |
|
231
|
|
|
public function paginator(int $nPageNumber, int $nItemsPerPage, int $nTotalItems): Paginator |
|
232
|
|
|
{ |
|
233
|
|
|
/** @var PaginatorPlugin */ |
|
234
|
|
|
$xPlugin = $this->plugin('pg'); |
|
235
|
|
|
return $xPlugin->paginator($nPageNumber, $nItemsPerPage, $nTotalItems); |
|
|
|
|
|
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Convert this response to a PSR7 response object |
|
240
|
|
|
* |
|
241
|
|
|
* @return PsrResponseInterface |
|
242
|
|
|
*/ |
|
243
|
|
|
public function toPsr(): PsrResponseInterface |
|
244
|
|
|
{ |
|
245
|
|
|
/** @var PsrPlugin */ |
|
246
|
|
|
$xPlugin = $this->plugin('psr'); |
|
247
|
|
|
return $xPlugin->ajax(); |
|
|
|
|
|
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|