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_shift; |
26
|
|
|
use function func_get_args; |
27
|
|
|
|
28
|
|
|
class Call extends JsCall |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var DialogLibraryManager |
32
|
|
|
*/ |
33
|
|
|
protected $xLibraryManager; |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The arguments of the else() calls |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $aMessage = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* A condition to check before making the call |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $aCondition = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The arguments of the confirm() call |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $aConfirm = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* The constructor. |
58
|
|
|
* |
59
|
|
|
* @param string $sName The javascript function or method name |
|
|
|
|
60
|
|
|
* @param DialogLibraryManager $xLibraryManager |
|
|
|
|
61
|
|
|
*/ |
62
|
|
|
public function __construct(string $sName, DialogLibraryManager $xLibraryManager) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
parent::__construct($sName); |
65
|
|
|
$this->xLibraryManager = $xLibraryManager; |
66
|
|
|
} |
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param array $aArgs |
|
|
|
|
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
private function getArgs(array $aArgs): array |
74
|
|
|
{ |
75
|
|
|
array_shift($aArgs); |
76
|
|
|
return $aArgs; |
77
|
|
|
} |
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Show a message if the condition to the call is not met |
81
|
|
|
* |
82
|
|
|
* @param string $sMessage The message to show |
|
|
|
|
83
|
|
|
* |
84
|
|
|
* @return Call |
85
|
|
|
*/ |
86
|
|
|
public function elseShow(string $sMessage): Call |
87
|
|
|
{ |
88
|
|
|
$this->aMessage = $this->xLibraryManager->warning($sMessage, $this->getArgs(func_get_args())); |
89
|
|
|
return $this; |
90
|
|
|
} |
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Show an information message if the condition to the call is not met |
94
|
|
|
* |
95
|
|
|
* @param string $sMessage The message to show |
|
|
|
|
96
|
|
|
* |
97
|
|
|
* @return Call |
98
|
|
|
*/ |
99
|
|
|
public function elseInfo(string $sMessage): Call |
100
|
|
|
{ |
101
|
|
|
$this->aMessage = $this->xLibraryManager->info($sMessage, $this->getArgs(func_get_args())); |
102
|
|
|
return $this; |
103
|
|
|
} |
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Show a success message if the condition to the call is not met |
107
|
|
|
* |
108
|
|
|
* @param string $sMessage The message to show |
|
|
|
|
109
|
|
|
* |
110
|
|
|
* @return Call |
111
|
|
|
*/ |
112
|
|
|
public function elseSuccess(string $sMessage): Call |
113
|
|
|
{ |
114
|
|
|
$this->aMessage = $this->xLibraryManager->success($sMessage, $this->getArgs(func_get_args())); |
115
|
|
|
return $this; |
116
|
|
|
} |
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Show a warning message if the condition to the call is not met |
120
|
|
|
* |
121
|
|
|
* @param string $sMessage The message to show |
|
|
|
|
122
|
|
|
* |
123
|
|
|
* @return Call |
124
|
|
|
*/ |
125
|
|
|
public function elseWarning(string $sMessage): Call |
126
|
|
|
{ |
127
|
|
|
$this->aMessage = $this->xLibraryManager->warning($sMessage, $this->getArgs(func_get_args())); |
128
|
|
|
return $this; |
129
|
|
|
} |
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Show an error message if the condition to the call is not met |
133
|
|
|
* |
134
|
|
|
* @param string $sMessage The message to show |
|
|
|
|
135
|
|
|
* |
136
|
|
|
* @return Call |
137
|
|
|
*/ |
138
|
|
|
public function elseError(string $sMessage): Call |
139
|
|
|
{ |
140
|
|
|
$this->aMessage = $this->xLibraryManager->error($sMessage, $this->getArgs(func_get_args())); |
141
|
|
|
return $this; |
142
|
|
|
} |
|
|
|
|
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Add a confirmation question to the request |
146
|
|
|
* |
147
|
|
|
* @param string $sQuestion The question to ask |
|
|
|
|
148
|
|
|
* |
149
|
|
|
* @return Call |
150
|
|
|
*/ |
151
|
|
|
public function confirm(string $sQuestion): Call |
152
|
|
|
{ |
153
|
|
|
$this->aConfirm = $this->xLibraryManager->confirm($sQuestion, $this->getArgs(func_get_args())); |
154
|
|
|
return $this; |
155
|
|
|
} |
|
|
|
|
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Add a condition to the request |
159
|
|
|
* |
160
|
|
|
* The request is sent only if the condition is true. |
161
|
|
|
* |
162
|
|
|
* @param mixed $xCondition The condition to check |
|
|
|
|
163
|
|
|
* |
164
|
|
|
* @return Call |
165
|
|
|
*/ |
166
|
|
|
public function when($xCondition): Call |
167
|
|
|
{ |
168
|
|
|
$this->aCondition = [true, Parameter::make($xCondition)]; |
169
|
|
|
return $this; |
170
|
|
|
} |
|
|
|
|
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Add a condition to the request |
174
|
|
|
* |
175
|
|
|
* The request is sent only if the condition is false. |
176
|
|
|
* |
177
|
|
|
* @param mixed $xCondition The condition to check |
|
|
|
|
178
|
|
|
* |
179
|
|
|
* @return Call |
180
|
|
|
*/ |
181
|
|
|
public function unless($xCondition): Call |
182
|
|
|
{ |
183
|
|
|
$this->aCondition = [false, Parameter::make($xCondition)]; |
184
|
|
|
return $this; |
185
|
|
|
} |
|
|
|
|
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Check if a value is equal to another before sending the request |
189
|
|
|
* |
190
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
191
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
192
|
|
|
* |
193
|
|
|
* @return Call |
194
|
|
|
*/ |
195
|
|
|
public function ifeq($xValue1, $xValue2): Call |
196
|
|
|
{ |
197
|
|
|
$this->aCondition = ['==', Parameter::make($xValue1), Parameter::make($xValue2)]; |
198
|
|
|
return $this; |
199
|
|
|
} |
|
|
|
|
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Check if a value is equal to another before sending the request |
203
|
|
|
* |
204
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
205
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
206
|
|
|
* |
207
|
|
|
* @return Call |
208
|
|
|
*/ |
209
|
|
|
public function ifteq($xValue1, $xValue2): Call |
210
|
|
|
{ |
211
|
|
|
$this->aCondition = ['===', Parameter::make($xValue1), Parameter::make($xValue2)]; |
212
|
|
|
return $this; |
213
|
|
|
} |
|
|
|
|
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Check if a value is not equal to another before sending the request |
217
|
|
|
* |
218
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
219
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
220
|
|
|
* |
221
|
|
|
* @return Call |
222
|
|
|
*/ |
223
|
|
|
public function ifne($xValue1, $xValue2): Call |
224
|
|
|
{ |
225
|
|
|
$this->aCondition = ['!=', Parameter::make($xValue1), Parameter::make($xValue2)]; |
226
|
|
|
return $this; |
227
|
|
|
} |
|
|
|
|
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Check if a value is not equal to another before sending the request |
231
|
|
|
* |
232
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
233
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
234
|
|
|
* |
235
|
|
|
* @return Call |
236
|
|
|
*/ |
237
|
|
|
public function ifnte($xValue1, $xValue2): Call |
238
|
|
|
{ |
239
|
|
|
$this->aCondition = ['!==', Parameter::make($xValue1), Parameter::make($xValue2)]; |
240
|
|
|
return $this; |
241
|
|
|
} |
|
|
|
|
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Check if a value is greater than another before sending the request |
245
|
|
|
* |
246
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
247
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
248
|
|
|
* |
249
|
|
|
* @return Call |
250
|
|
|
*/ |
251
|
|
|
public function ifgt($xValue1, $xValue2): Call |
252
|
|
|
{ |
253
|
|
|
$this->aCondition = ['>', Parameter::make($xValue1), Parameter::make($xValue2)]; |
254
|
|
|
return $this; |
255
|
|
|
} |
|
|
|
|
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Check if a value is greater or equal to another before sending the request |
259
|
|
|
* |
260
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
261
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
262
|
|
|
* |
263
|
|
|
* @return Call |
264
|
|
|
*/ |
265
|
|
|
public function ifge($xValue1, $xValue2): Call |
266
|
|
|
{ |
267
|
|
|
$this->aCondition = ['>=', Parameter::make($xValue1), Parameter::make($xValue2)]; |
268
|
|
|
return $this; |
269
|
|
|
} |
|
|
|
|
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Check if a value is lower than another before sending the request |
273
|
|
|
* |
274
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
275
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
276
|
|
|
* |
277
|
|
|
* @return Call |
278
|
|
|
*/ |
279
|
|
|
public function iflt($xValue1, $xValue2): Call |
280
|
|
|
{ |
281
|
|
|
$this->aCondition = ['<', Parameter::make($xValue1), Parameter::make($xValue2)]; |
282
|
|
|
return $this; |
283
|
|
|
} |
|
|
|
|
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Check if a value is lower or equal to another before sending the request |
287
|
|
|
* |
288
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
289
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
290
|
|
|
* |
291
|
|
|
* @return Call |
292
|
|
|
*/ |
293
|
|
|
public function ifle($xValue1, $xValue2): Call |
294
|
|
|
{ |
295
|
|
|
$this->aCondition = ['<=', Parameter::make($xValue1), Parameter::make($xValue2)]; |
296
|
|
|
return $this; |
297
|
|
|
} |
|
|
|
|
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Convert this call to array |
301
|
|
|
* |
302
|
|
|
* @return array |
303
|
|
|
*/ |
304
|
|
|
public function toArray(): array |
305
|
|
|
{ |
306
|
|
|
$aCall = parent::toArray(); |
307
|
|
|
if(($this->aConfirm)) |
|
|
|
|
308
|
|
|
{ |
309
|
|
|
$aCall['confirm'] = [ |
310
|
|
|
...$this->aConfirm, |
311
|
|
|
'lib' => $this->xLibraryManager->getQuestionLibrary()->getName(), |
312
|
|
|
]; |
313
|
|
|
} |
314
|
|
|
if(($this->aCondition)) |
|
|
|
|
315
|
|
|
{ |
316
|
|
|
$aCall['condition'] = $this->aCondition; |
317
|
|
|
} |
318
|
|
|
if(($this->aMessage)) |
|
|
|
|
319
|
|
|
{ |
320
|
|
|
$aCall['else'] = [ |
321
|
|
|
...$this->aMessage, |
322
|
|
|
'lib' => $this->xLibraryManager->getMessageLibrary()->getName(), |
323
|
|
|
]; |
324
|
|
|
} |
325
|
|
|
return $aCall; |
326
|
|
|
} |
|
|
|
|
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Check if the request has a parameter of type Parameter::PAGE_NUMBER |
330
|
|
|
* |
331
|
|
|
* @return ParameterInterface|null |
332
|
|
|
*/ |
333
|
|
|
private function findPageNumber(): ?ParameterInterface |
334
|
|
|
{ |
335
|
|
|
foreach($this->aParameters as $xParameter) |
336
|
|
|
{ |
337
|
|
|
if($xParameter->getType() === Parameter::PAGE_NUMBER) |
338
|
|
|
{ |
339
|
|
|
return $xParameter; |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
return null; |
343
|
|
|
} |
|
|
|
|
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Check if the request has a parameter of type Parameter::PAGE_NUMBER |
347
|
|
|
* |
348
|
|
|
* @return bool |
349
|
|
|
*/ |
350
|
|
|
public function hasPageNumber(): bool |
351
|
|
|
{ |
352
|
|
|
return $this->findPageNumber() !== null; |
353
|
|
|
} |
|
|
|
|
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Set a value to the Parameter::PAGE_NUMBER parameter |
357
|
|
|
* |
358
|
|
|
* @param integer $nPageNumber The current page number |
|
|
|
|
359
|
|
|
* |
360
|
|
|
* @return Call |
361
|
|
|
*/ |
362
|
|
|
public function setPageNumber(int $nPageNumber): Call |
363
|
|
|
{ |
364
|
|
|
/** @var Parameter */ |
|
|
|
|
365
|
|
|
$xParameter = $this->findPageNumber(); |
366
|
|
|
if($xParameter !== null) |
367
|
|
|
{ |
368
|
|
|
$xParameter->setValue($nPageNumber); |
|
|
|
|
369
|
|
|
} |
370
|
|
|
return $this; |
371
|
|
|
} |
|
|
|
|
372
|
|
|
} |
373
|
|
|
|