1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Call.php - The Jaxon Call |
5
|
|
|
* |
6
|
|
|
* This class is used to create js ajax requests to callable classes and functions. |
7
|
|
|
* |
8
|
|
|
* @package jaxon-core |
|
|
|
|
9
|
|
|
* @author Jared White |
|
|
|
|
10
|
|
|
* @author J. Max Wilson |
|
|
|
|
11
|
|
|
* @author Joseph Woolley |
|
|
|
|
12
|
|
|
* @author Steffen Konerow |
|
|
|
|
13
|
|
|
* @author Thierry Feuzeu <[email protected]> |
14
|
|
|
* @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson |
|
|
|
|
15
|
|
|
* @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White & J. Max Wilson |
|
|
|
|
16
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
17
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
18
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
19
|
|
|
*/ |
|
|
|
|
20
|
|
|
|
21
|
|
|
namespace Jaxon\Request\Call; |
22
|
|
|
|
23
|
|
|
use Jaxon\App\Dialog\Library\DialogLibraryManager; |
24
|
|
|
|
25
|
|
|
use function array_map; |
26
|
|
|
use function array_shift; |
27
|
|
|
use function func_get_args; |
28
|
|
|
|
29
|
|
|
class Call extends JsCall |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var DialogLibraryManager |
33
|
|
|
*/ |
34
|
|
|
protected $xDialogLibraryManager; |
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The arguments of the else() calls |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $aMessage = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* A condition to check before making the call |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $aCondition = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The arguments of the confirm() call |
52
|
|
|
* |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $aConfirm = []; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The constructor. |
59
|
|
|
* |
60
|
|
|
* @param string $sName The javascript function or method name |
|
|
|
|
61
|
|
|
* @param DialogLibraryManager $xDialogLibraryManager |
|
|
|
|
62
|
|
|
*/ |
63
|
|
|
public function __construct(string $sName, DialogLibraryManager $xDialogLibraryManager) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
parent::__construct($sName); |
66
|
|
|
$this->xDialogLibraryManager = $xDialogLibraryManager; |
67
|
|
|
} |
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Make a phrase to be displayed in js code |
71
|
|
|
* |
72
|
|
|
* @param string $sPhrase |
|
|
|
|
73
|
|
|
* @param array $aArgs |
|
|
|
|
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
private function makePhrase(string $sPhrase, array $aArgs = []): array |
78
|
|
|
{ |
79
|
|
|
array_shift($aArgs); |
80
|
|
|
return [ |
81
|
|
|
'str' => $sPhrase, |
82
|
|
|
'args' => array_map(fn($xArg) => Parameter::make($xArg), $aArgs), |
83
|
|
|
]; |
84
|
|
|
} |
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set the message if the condition to the call is not met |
88
|
|
|
* |
89
|
|
|
* The first parameter is the message to show. The second allows inserting data from |
90
|
|
|
* the webpage in the message using positional placeholders. |
91
|
|
|
* |
92
|
|
|
* @param string $sType |
|
|
|
|
93
|
|
|
* @param string $sMessage |
|
|
|
|
94
|
|
|
* @param array $aArgs |
|
|
|
|
95
|
|
|
* |
96
|
|
|
* @return Call |
97
|
|
|
*/ |
98
|
|
|
private function setMessage(string $sType, string $sMessage, array $aArgs): Call |
99
|
|
|
{ |
100
|
|
|
$this->aMessage = [ |
101
|
|
|
'lib' => $this->xDialogLibraryManager->getMessageLibrary()->getName(), |
102
|
|
|
'type' => $sType, |
103
|
|
|
'phrase' => $this->makePhrase($sMessage, $aArgs), |
104
|
|
|
]; |
105
|
|
|
return $this; |
106
|
|
|
} |
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Show a message if the condition to the call is not met |
110
|
|
|
* |
111
|
|
|
* @param string $sMessage The message to show |
|
|
|
|
112
|
|
|
* |
113
|
|
|
* @return Call |
114
|
|
|
*/ |
115
|
|
|
public function elseShow(string $sMessage): Call |
116
|
|
|
{ |
117
|
|
|
return $this->setMessage('warning', $sMessage, func_get_args()); |
118
|
|
|
} |
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Show an information message if the condition to the call is not met |
122
|
|
|
* |
123
|
|
|
* @param string $sMessage The message to show |
|
|
|
|
124
|
|
|
* |
125
|
|
|
* @return Call |
126
|
|
|
*/ |
127
|
|
|
public function elseInfo(string $sMessage): Call |
128
|
|
|
{ |
129
|
|
|
return $this->setMessage('info', $sMessage, func_get_args()); |
130
|
|
|
} |
|
|
|
|
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Show a success message if the condition to the call is not met |
134
|
|
|
* |
135
|
|
|
* @param string $sMessage The message to show |
|
|
|
|
136
|
|
|
* |
137
|
|
|
* @return Call |
138
|
|
|
*/ |
139
|
|
|
public function elseSuccess(string $sMessage): Call |
140
|
|
|
{ |
141
|
|
|
return $this->setMessage('success', $sMessage, func_get_args()); |
142
|
|
|
} |
|
|
|
|
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Show a warning message if the condition to the call is not met |
146
|
|
|
* |
147
|
|
|
* @param string $sMessage The message to show |
|
|
|
|
148
|
|
|
* |
149
|
|
|
* @return Call |
150
|
|
|
*/ |
151
|
|
|
public function elseWarning(string $sMessage): Call |
152
|
|
|
{ |
153
|
|
|
return $this->setMessage('warning', $sMessage, func_get_args()); |
154
|
|
|
} |
|
|
|
|
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Show an error message if the condition to the call is not met |
158
|
|
|
* |
159
|
|
|
* @param string $sMessage The message to show |
|
|
|
|
160
|
|
|
* |
161
|
|
|
* @return Call |
162
|
|
|
*/ |
163
|
|
|
public function elseError(string $sMessage): Call |
164
|
|
|
{ |
165
|
|
|
return $this->setMessage('error', $sMessage, func_get_args()); |
166
|
|
|
} |
|
|
|
|
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Add a condition to the request |
170
|
|
|
* |
171
|
|
|
* The request is sent only if the condition is true. |
172
|
|
|
* |
173
|
|
|
* @param mixed $xCondition The condition to check |
|
|
|
|
174
|
|
|
* |
175
|
|
|
* @return Call |
176
|
|
|
*/ |
177
|
|
|
public function when($xCondition): Call |
178
|
|
|
{ |
179
|
|
|
$this->aCondition = [true, Parameter::make($xCondition)]; |
180
|
|
|
return $this; |
181
|
|
|
} |
|
|
|
|
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Add a condition to the request |
185
|
|
|
* |
186
|
|
|
* The request is sent only if the condition is false. |
187
|
|
|
* |
188
|
|
|
* @param mixed $xCondition The condition to check |
|
|
|
|
189
|
|
|
* |
190
|
|
|
* @return Call |
191
|
|
|
*/ |
192
|
|
|
public function unless($xCondition): Call |
193
|
|
|
{ |
194
|
|
|
$this->aCondition = [false, Parameter::make($xCondition)]; |
195
|
|
|
return $this; |
196
|
|
|
} |
|
|
|
|
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Check if a value is equal to another before sending the request |
200
|
|
|
* |
201
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
202
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
203
|
|
|
* |
204
|
|
|
* @return Call |
205
|
|
|
*/ |
206
|
|
|
public function ifeq($xValue1, $xValue2): Call |
207
|
|
|
{ |
208
|
|
|
$this->aCondition = ['==', Parameter::make($xValue1), Parameter::make($xValue2)]; |
209
|
|
|
return $this; |
210
|
|
|
} |
|
|
|
|
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Check if a value is equal to another before sending the request |
214
|
|
|
* |
215
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
216
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
217
|
|
|
* |
218
|
|
|
* @return Call |
219
|
|
|
*/ |
220
|
|
|
public function ifteq($xValue1, $xValue2): Call |
221
|
|
|
{ |
222
|
|
|
$this->aCondition = ['===', Parameter::make($xValue1), Parameter::make($xValue2)]; |
223
|
|
|
return $this; |
224
|
|
|
} |
|
|
|
|
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Check if a value is not equal to another before sending the request |
228
|
|
|
* |
229
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
230
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
231
|
|
|
* |
232
|
|
|
* @return Call |
233
|
|
|
*/ |
234
|
|
|
public function ifne($xValue1, $xValue2): Call |
235
|
|
|
{ |
236
|
|
|
$this->aCondition = ['!=', Parameter::make($xValue1), Parameter::make($xValue2)]; |
237
|
|
|
return $this; |
238
|
|
|
} |
|
|
|
|
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Check if a value is not equal to another before sending the request |
242
|
|
|
* |
243
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
244
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
245
|
|
|
* |
246
|
|
|
* @return Call |
247
|
|
|
*/ |
248
|
|
|
public function ifnte($xValue1, $xValue2): Call |
249
|
|
|
{ |
250
|
|
|
$this->aCondition = ['!==', Parameter::make($xValue1), Parameter::make($xValue2)]; |
251
|
|
|
return $this; |
252
|
|
|
} |
|
|
|
|
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Check if a value is greater than another before sending the request |
256
|
|
|
* |
257
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
258
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
259
|
|
|
* |
260
|
|
|
* @return Call |
261
|
|
|
*/ |
262
|
|
|
public function ifgt($xValue1, $xValue2): Call |
263
|
|
|
{ |
264
|
|
|
$this->aCondition = ['>', Parameter::make($xValue1), Parameter::make($xValue2)]; |
265
|
|
|
return $this; |
266
|
|
|
} |
|
|
|
|
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Check if a value is greater or equal to another before sending the request |
270
|
|
|
* |
271
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
272
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
273
|
|
|
* |
274
|
|
|
* @return Call |
275
|
|
|
*/ |
276
|
|
|
public function ifge($xValue1, $xValue2): Call |
277
|
|
|
{ |
278
|
|
|
$this->aCondition = ['>=', Parameter::make($xValue1), Parameter::make($xValue2)]; |
279
|
|
|
return $this; |
280
|
|
|
} |
|
|
|
|
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Check if a value is lower than another before sending the request |
284
|
|
|
* |
285
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
286
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
287
|
|
|
* |
288
|
|
|
* @return Call |
289
|
|
|
*/ |
290
|
|
|
public function iflt($xValue1, $xValue2): Call |
291
|
|
|
{ |
292
|
|
|
$this->aCondition = ['<', Parameter::make($xValue1), Parameter::make($xValue2)]; |
293
|
|
|
return $this; |
294
|
|
|
} |
|
|
|
|
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Check if a value is lower or equal to another before sending the request |
298
|
|
|
* |
299
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
300
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
301
|
|
|
* |
302
|
|
|
* @return Call |
303
|
|
|
*/ |
304
|
|
|
public function ifle($xValue1, $xValue2): Call |
305
|
|
|
{ |
306
|
|
|
$this->aCondition = ['<=', Parameter::make($xValue1), Parameter::make($xValue2)]; |
307
|
|
|
return $this; |
308
|
|
|
} |
|
|
|
|
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Add a confirmation question to the request |
312
|
|
|
* |
313
|
|
|
* @param string $sQuestion The question to ask |
|
|
|
|
314
|
|
|
* |
315
|
|
|
* @return Call |
316
|
|
|
*/ |
317
|
|
|
public function confirm(string $sQuestion): Call |
318
|
|
|
{ |
319
|
|
|
$this->aConfirm = [ |
320
|
|
|
'lib' => $this->xDialogLibraryManager->getQuestionLibrary()->getName(), |
321
|
|
|
'phrase' => $this->makePhrase($sQuestion, func_get_args()), |
322
|
|
|
]; |
323
|
|
|
return $this; |
324
|
|
|
} |
|
|
|
|
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Convert this call to array |
328
|
|
|
* |
329
|
|
|
* @return array |
330
|
|
|
*/ |
331
|
|
|
public function toArray(): array |
332
|
|
|
{ |
333
|
|
|
$aCall = parent::toArray(); |
334
|
|
|
if(($this->aConfirm)) |
|
|
|
|
335
|
|
|
{ |
336
|
|
|
$aCall['confirm'] = $this->aConfirm; |
337
|
|
|
} |
338
|
|
|
if(($this->aCondition)) |
|
|
|
|
339
|
|
|
{ |
340
|
|
|
$aCall['condition'] = $this->aCondition; |
341
|
|
|
} |
342
|
|
|
if(($this->aMessage)) |
|
|
|
|
343
|
|
|
{ |
344
|
|
|
$aCall['else'] = $this->aMessage; |
345
|
|
|
} |
346
|
|
|
return $aCall; |
347
|
|
|
} |
|
|
|
|
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Check if the request has a parameter of type Parameter::PAGE_NUMBER |
351
|
|
|
* |
352
|
|
|
* @return ParameterInterface|null |
353
|
|
|
*/ |
354
|
|
|
private function findPageNumber(): ?ParameterInterface |
355
|
|
|
{ |
356
|
|
|
foreach($this->aParameters as $xParameter) |
357
|
|
|
{ |
358
|
|
|
if($xParameter->getType() === Parameter::PAGE_NUMBER) |
359
|
|
|
{ |
360
|
|
|
return $xParameter; |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
return null; |
364
|
|
|
} |
|
|
|
|
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Check if the request has a parameter of type Parameter::PAGE_NUMBER |
368
|
|
|
* |
369
|
|
|
* @return bool |
370
|
|
|
*/ |
371
|
|
|
public function hasPageNumber(): bool |
372
|
|
|
{ |
373
|
|
|
return $this->findPageNumber() !== null; |
374
|
|
|
} |
|
|
|
|
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Set a value to the Parameter::PAGE_NUMBER parameter |
378
|
|
|
* |
379
|
|
|
* @param integer $nPageNumber The current page number |
|
|
|
|
380
|
|
|
* |
381
|
|
|
* @return Call |
382
|
|
|
*/ |
383
|
|
|
public function setPageNumber(int $nPageNumber): Call |
384
|
|
|
{ |
385
|
|
|
/** @var Parameter */ |
|
|
|
|
386
|
|
|
$xParameter = $this->findPageNumber(); |
387
|
|
|
if($xParameter !== null) |
388
|
|
|
{ |
389
|
|
|
$xParameter->setValue($nPageNumber); |
|
|
|
|
390
|
|
|
} |
391
|
|
|
return $this; |
392
|
|
|
} |
|
|
|
|
393
|
|
|
} |
394
|
|
|
|