|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Call.php - The Jaxon Call |
|
5
|
|
|
* |
|
6
|
|
|
* This class is used to create client side 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\Response\Plugin\JQuery\DomSelector; |
|
24
|
|
|
use Jaxon\Ui\Dialogs\DialogFacade; |
|
25
|
|
|
use Jaxon\Ui\Pagination\Paginator; |
|
26
|
|
|
|
|
27
|
|
|
use function array_map; |
|
28
|
|
|
use function array_shift; |
|
29
|
|
|
use function func_get_args; |
|
30
|
|
|
use function implode; |
|
31
|
|
|
|
|
32
|
|
|
class Call extends JsCall |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var DialogFacade |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $xDialogFacade; |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var Paginator |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $xPaginator; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* A condition to check before sending this request |
|
46
|
|
|
* |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $sCondition = null; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* The arguments of the confirm() call |
|
53
|
|
|
* |
|
54
|
|
|
* @var array |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $aConfirmArgs = []; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* The arguments of the elseShow() call |
|
60
|
|
|
* |
|
61
|
|
|
* @var array |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $aMessageArgs = []; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var array |
|
67
|
|
|
*/ |
|
68
|
|
|
private $aVariables; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var string |
|
72
|
|
|
*/ |
|
73
|
|
|
private $sVars; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @var int |
|
|
|
|
|
|
77
|
|
|
*/ |
|
78
|
|
|
private $nVarId; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* The constructor. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $sName The javascript function or method name |
|
|
|
|
|
|
84
|
|
|
* @param DialogFacade $xDialogFacade |
|
|
|
|
|
|
85
|
|
|
* @param Paginator $xPaginator |
|
|
|
|
|
|
86
|
|
|
*/ |
|
87
|
|
|
public function __construct(string $sName, DialogFacade $xDialogFacade, Paginator $xPaginator) |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
parent::__construct($sName); |
|
90
|
|
|
$this->xDialogFacade = $xDialogFacade; |
|
91
|
|
|
$this->xPaginator = $xPaginator; |
|
|
|
|
|
|
92
|
|
|
} |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Add a condition to the request |
|
96
|
|
|
* |
|
97
|
|
|
* The request is sent only if the condition is true. |
|
98
|
|
|
* |
|
99
|
|
|
* @param mixed $xCondition The condition to check |
|
|
|
|
|
|
100
|
|
|
* |
|
101
|
|
|
* @return Call |
|
102
|
|
|
*/ |
|
103
|
|
|
public function when($xCondition): Call |
|
104
|
|
|
{ |
|
105
|
|
|
$this->sCondition = Parameter::make($xCondition)->getScript(); |
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Add a condition to the request |
|
111
|
|
|
* |
|
112
|
|
|
* The request is sent only if the condition is false. |
|
113
|
|
|
* |
|
114
|
|
|
* @param mixed $xCondition The condition to check |
|
|
|
|
|
|
115
|
|
|
* |
|
116
|
|
|
* @return Call |
|
117
|
|
|
*/ |
|
118
|
|
|
public function unless($xCondition): Call |
|
119
|
|
|
{ |
|
120
|
|
|
$this->sCondition = '!(' . Parameter::make($xCondition)->getScript() . ')'; |
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Check if a value is equal to another before sending the request |
|
126
|
|
|
* |
|
127
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
|
|
128
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
|
|
129
|
|
|
* |
|
130
|
|
|
* @return Call |
|
131
|
|
|
*/ |
|
132
|
|
|
public function ifeq($xValue1, $xValue2): Call |
|
133
|
|
|
{ |
|
134
|
|
|
$this->sCondition = Parameter::make($xValue1) . '==' . Parameter::make($xValue2); |
|
|
|
|
|
|
135
|
|
|
return $this; |
|
136
|
|
|
} |
|
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Check if a value is not equal to another before sending the request |
|
140
|
|
|
* |
|
141
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
|
|
142
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
|
|
143
|
|
|
* |
|
144
|
|
|
* @return Call |
|
145
|
|
|
*/ |
|
146
|
|
|
public function ifne($xValue1, $xValue2): Call |
|
147
|
|
|
{ |
|
148
|
|
|
$this->sCondition = Parameter::make($xValue1) . '!=' . Parameter::make($xValue2); |
|
|
|
|
|
|
149
|
|
|
return $this; |
|
150
|
|
|
} |
|
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Check if a value is greater than another before sending the request |
|
154
|
|
|
* |
|
155
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
|
|
156
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
|
|
157
|
|
|
* |
|
158
|
|
|
* @return Call |
|
159
|
|
|
*/ |
|
160
|
|
|
public function ifgt($xValue1, $xValue2): Call |
|
161
|
|
|
{ |
|
162
|
|
|
$this->sCondition = Parameter::make($xValue1) . '>' . Parameter::make($xValue2); |
|
|
|
|
|
|
163
|
|
|
return $this; |
|
164
|
|
|
} |
|
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Check if a value is greater or equal to another before sending the request |
|
168
|
|
|
* |
|
169
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
|
|
170
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
|
|
171
|
|
|
* |
|
172
|
|
|
* @return Call |
|
173
|
|
|
*/ |
|
174
|
|
|
public function ifge($xValue1, $xValue2): Call |
|
175
|
|
|
{ |
|
176
|
|
|
$this->sCondition = Parameter::make($xValue1) . '>=' . Parameter::make($xValue2); |
|
|
|
|
|
|
177
|
|
|
return $this; |
|
178
|
|
|
} |
|
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Check if a value is lower than another before sending the request |
|
182
|
|
|
* |
|
183
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
|
|
184
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
|
|
185
|
|
|
* |
|
186
|
|
|
* @return Call |
|
187
|
|
|
*/ |
|
188
|
|
|
public function iflt($xValue1, $xValue2): Call |
|
189
|
|
|
{ |
|
190
|
|
|
$this->sCondition = Parameter::make($xValue1) . '<' . Parameter::make($xValue2); |
|
|
|
|
|
|
191
|
|
|
return $this; |
|
192
|
|
|
} |
|
|
|
|
|
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Check if a value is lower or equal to another before sending the request |
|
196
|
|
|
* |
|
197
|
|
|
* @param mixed $xValue1 The first value to compare |
|
|
|
|
|
|
198
|
|
|
* @param mixed $xValue2 The second value to compare |
|
|
|
|
|
|
199
|
|
|
* |
|
200
|
|
|
* @return Call |
|
201
|
|
|
*/ |
|
202
|
|
|
public function ifle($xValue1, $xValue2): Call |
|
203
|
|
|
{ |
|
204
|
|
|
$this->sCondition = Parameter::make($xValue1) . '<=' . Parameter::make($xValue2); |
|
|
|
|
|
|
205
|
|
|
return $this; |
|
206
|
|
|
} |
|
|
|
|
|
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Add a confirmation question to the request |
|
210
|
|
|
* |
|
211
|
|
|
* @param string $sQuestion The question to ask |
|
|
|
|
|
|
212
|
|
|
* |
|
213
|
|
|
* @return Call |
|
214
|
|
|
*/ |
|
215
|
|
|
public function confirm(string $sQuestion): Call |
|
216
|
|
|
{ |
|
217
|
|
|
$this->sCondition = '__confirm__'; |
|
|
|
|
|
|
218
|
|
|
$this->aConfirmArgs = array_map(function($xParameter) { |
|
219
|
|
|
return Parameter::make($xParameter); |
|
220
|
|
|
}, func_get_args()); |
|
221
|
|
|
return $this; |
|
222
|
|
|
} |
|
|
|
|
|
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Set the message to show if the condition to send the request is not met |
|
226
|
|
|
* |
|
227
|
|
|
* The first parameter is the message to show. The followings allow inserting data from |
|
228
|
|
|
* the webpage in the message using positional placeholders. |
|
229
|
|
|
* |
|
230
|
|
|
* @param string $sMessage The message to show if the request is not sent |
|
|
|
|
|
|
231
|
|
|
* |
|
232
|
|
|
* @return Call |
|
233
|
|
|
*/ |
|
234
|
|
|
public function elseShow(string $sMessage): Call |
|
235
|
|
|
{ |
|
236
|
|
|
$this->aMessageArgs = array_map(function($xParameter) { |
|
237
|
|
|
return Parameter::make($xParameter); |
|
238
|
|
|
}, func_get_args()); |
|
239
|
|
|
return $this; |
|
240
|
|
|
} |
|
|
|
|
|
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Make unique js vars for parameters of type DomSelector |
|
244
|
|
|
* |
|
245
|
|
|
* @param ParameterInterface $xParameter |
|
|
|
|
|
|
246
|
|
|
* |
|
247
|
|
|
* @return ParameterInterface |
|
248
|
|
|
*/ |
|
249
|
|
|
private function _makeUniqueJsVar(ParameterInterface $xParameter): ParameterInterface |
|
250
|
|
|
{ |
|
251
|
|
|
if($xParameter instanceof DomSelector) |
|
252
|
|
|
{ |
|
253
|
|
|
$sParameterStr = $xParameter->getScript(); |
|
254
|
|
|
if(!isset($this->aVariables[$sParameterStr])) |
|
255
|
|
|
{ |
|
256
|
|
|
// The value is not yet defined. A new variable is created. |
|
257
|
|
|
$sVarName = 'jxnVar' . $this->nVarId; |
|
|
|
|
|
|
258
|
|
|
$this->aVariables[$sParameterStr] = $sVarName; |
|
259
|
|
|
$this->sVars .= "$sVarName=$xParameter;"; |
|
|
|
|
|
|
260
|
|
|
$this->nVarId++; |
|
261
|
|
|
} |
|
262
|
|
|
else |
|
263
|
|
|
{ |
|
264
|
|
|
// The value is already defined. The corresponding variable is assigned. |
|
265
|
|
|
$sVarName = $this->aVariables[$sParameterStr]; |
|
266
|
|
|
} |
|
267
|
|
|
$xParameter = new Parameter(Parameter::JS_VALUE, $sVarName); |
|
268
|
|
|
} |
|
269
|
|
|
return $xParameter; |
|
270
|
|
|
} |
|
|
|
|
|
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* Make a phrase to be displayed in js code |
|
274
|
|
|
* |
|
275
|
|
|
* @param array $aArgs |
|
|
|
|
|
|
276
|
|
|
* |
|
277
|
|
|
* @return string |
|
278
|
|
|
*/ |
|
279
|
|
|
private function makePhrase(array $aArgs): string |
|
280
|
|
|
{ |
|
281
|
|
|
if(empty($aArgs)) |
|
282
|
|
|
{ |
|
283
|
|
|
return ''; |
|
284
|
|
|
} |
|
285
|
|
|
// The first array entry is the message. |
|
286
|
|
|
$sPhrase = array_shift($aArgs); |
|
287
|
|
|
if(empty($aArgs)) |
|
288
|
|
|
{ |
|
289
|
|
|
return $sPhrase; |
|
290
|
|
|
} |
|
291
|
|
|
$nParamId = 1; |
|
292
|
|
|
foreach($aArgs as &$xParameter) |
|
293
|
|
|
{ |
|
294
|
|
|
$xParameter = $this->_makeUniqueJsVar($xParameter); |
|
295
|
|
|
$xParameter = "'$nParamId':" . $xParameter->getScript(); |
|
296
|
|
|
$nParamId++; |
|
297
|
|
|
} |
|
298
|
|
|
$sPhrase .= '.supplant({' . implode(',', $aArgs) . '})'; |
|
299
|
|
|
return $sPhrase; |
|
300
|
|
|
} |
|
|
|
|
|
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* Make a phrase to be displayed in js code |
|
304
|
|
|
* |
|
305
|
|
|
* @param array $aArgs |
|
|
|
|
|
|
306
|
|
|
* |
|
307
|
|
|
* @return string |
|
308
|
|
|
*/ |
|
309
|
|
|
private function makeMessage(array $aArgs): string |
|
310
|
|
|
{ |
|
311
|
|
|
if(!($sPhrase = $this->makePhrase($aArgs))) |
|
|
|
|
|
|
312
|
|
|
{ |
|
313
|
|
|
return ''; |
|
314
|
|
|
} |
|
315
|
|
|
return $this->xDialogFacade->getMessageLibrary(true)->warning($sPhrase); |
|
316
|
|
|
} |
|
|
|
|
|
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* Returns a string representation of the script output (javascript) from this request object |
|
320
|
|
|
* |
|
321
|
|
|
* @return string |
|
322
|
|
|
*/ |
|
323
|
|
|
public function getScript(): string |
|
324
|
|
|
{ |
|
325
|
|
|
/* |
|
326
|
|
|
* JQuery variables sometimes depend on the context where they are used, eg. when their value depends on $(this). |
|
327
|
|
|
* When a confirmation question is added, the Jaxon calls are made in a different context, |
|
328
|
|
|
* making those variables invalid. |
|
329
|
|
|
* To avoid issues related to these context changes, the JQuery selectors values are first saved into |
|
330
|
|
|
* local variables, which are then used in Jaxon function calls. |
|
331
|
|
|
*/ |
|
|
|
|
|
|
332
|
|
|
$this->sVars = ''; // Javascript code defining all the variables values. |
|
333
|
|
|
$this->nVarId = 1; // Position of the variables, starting from 1. |
|
334
|
|
|
// This array will avoid declaring multiple variables with the same value. |
|
335
|
|
|
// The array key is the variable value, while the array value is the variable name. |
|
336
|
|
|
$this->aVariables = []; // Array of local variables. |
|
337
|
|
|
foreach($this->aParameters as &$xParameter) |
|
338
|
|
|
{ |
|
339
|
|
|
$xParameter = $this->_makeUniqueJsVar($xParameter); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
$sMessageScript = $this->makeMessage($this->aMessageArgs); |
|
343
|
|
|
$sScript = parent::getScript(); |
|
|
|
|
|
|
344
|
|
|
if($this->sCondition === '__confirm__') |
|
345
|
|
|
{ |
|
346
|
|
|
$sConfirmPhrase = $this->makePhrase($this->aConfirmArgs); |
|
347
|
|
|
$sScript = $this->xDialogFacade->getQuestionLibrary() |
|
|
|
|
|
|
348
|
|
|
->confirm($sConfirmPhrase, $sScript, $sMessageScript); |
|
|
|
|
|
|
349
|
|
|
} |
|
350
|
|
|
elseif($this->sCondition !== null) |
|
351
|
|
|
{ |
|
352
|
|
|
$sScript = empty($sMessageScript) ? 'if(' . $this->sCondition . '){' . $sScript . ';}' : |
|
|
|
|
|
|
353
|
|
|
'if(' . $this->sCondition . '){' . $sScript . ';}else{' . $sMessageScript . ';}'; |
|
354
|
|
|
} |
|
355
|
|
|
return $this->sVars . $sScript; |
|
356
|
|
|
} |
|
|
|
|
|
|
357
|
|
|
|
|
358
|
|
|
/** |
|
359
|
|
|
* Check if the request has a parameter of type Parameter::PAGE_NUMBER |
|
360
|
|
|
* |
|
361
|
|
|
* @return bool |
|
362
|
|
|
*/ |
|
363
|
|
|
public function hasPageNumber(): bool |
|
364
|
|
|
{ |
|
365
|
|
|
foreach($this->aParameters as $xParameter) |
|
366
|
|
|
{ |
|
367
|
|
|
if($xParameter->getType() === Parameter::PAGE_NUMBER) |
|
368
|
|
|
{ |
|
369
|
|
|
return true; |
|
370
|
|
|
} |
|
371
|
|
|
} |
|
372
|
|
|
return false; |
|
373
|
|
|
} |
|
|
|
|
|
|
374
|
|
|
|
|
375
|
|
|
/** |
|
376
|
|
|
* Set a value to the Parameter::PAGE_NUMBER parameter |
|
377
|
|
|
* |
|
378
|
|
|
* @param integer $nPageNumber The current page number |
|
|
|
|
|
|
379
|
|
|
* |
|
380
|
|
|
* @return Call |
|
381
|
|
|
*/ |
|
382
|
|
|
public function setPageNumber(int $nPageNumber): Call |
|
383
|
|
|
{ |
|
384
|
|
|
// Set the value of the Parameter::PAGE_NUMBER parameter |
|
385
|
|
|
foreach($this->aParameters as $xParameter) |
|
386
|
|
|
{ |
|
387
|
|
|
if($xParameter->getType() === Parameter::PAGE_NUMBER) |
|
388
|
|
|
{ |
|
389
|
|
|
$xParameter->setValue($nPageNumber); |
|
390
|
|
|
break; |
|
391
|
|
|
} |
|
392
|
|
|
} |
|
393
|
|
|
return $this; |
|
394
|
|
|
} |
|
|
|
|
|
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* Make the pagination links for this request |
|
398
|
|
|
* |
|
399
|
|
|
* @param integer $nCurrentPage The current page |
|
|
|
|
|
|
400
|
|
|
* @param integer $nItemsPerPage The number of items per page |
|
|
|
|
|
|
401
|
|
|
* @param integer $nItemsTotal The total number of items |
|
|
|
|
|
|
402
|
|
|
* |
|
403
|
|
|
* @return Paginator |
|
404
|
|
|
*/ |
|
405
|
|
|
public function paginate(int $nCurrentPage, int $nItemsPerPage, int $nItemsTotal): Paginator |
|
406
|
|
|
{ |
|
407
|
|
|
return $this->xPaginator->setup($this, $nCurrentPage, $nItemsPerPage, $nItemsTotal); |
|
408
|
|
|
} |
|
|
|
|
|
|
409
|
|
|
|
|
410
|
|
|
/** |
|
411
|
|
|
* Make the pagination links for this request |
|
412
|
|
|
* |
|
413
|
|
|
* @param integer $nCurrentPage The current page |
|
|
|
|
|
|
414
|
|
|
* @param integer $nItemsPerPage The number of items per page |
|
|
|
|
|
|
415
|
|
|
* @param integer $nItemsTotal The total number of items |
|
|
|
|
|
|
416
|
|
|
* |
|
417
|
|
|
* @return Paginator |
|
418
|
|
|
*/ |
|
419
|
|
|
public function pg(int $nCurrentPage, int $nItemsPerPage, int $nItemsTotal): Paginator |
|
420
|
|
|
{ |
|
421
|
|
|
return $this->paginate($nCurrentPage, $nItemsPerPage, $nItemsTotal); |
|
422
|
|
|
} |
|
|
|
|
|
|
423
|
|
|
|
|
424
|
|
|
/** |
|
425
|
|
|
* Make the pagination links for this request |
|
426
|
|
|
* |
|
427
|
|
|
* @param integer $nCurrentPage The current page |
|
|
|
|
|
|
428
|
|
|
* @param integer $nItemsPerPage The number of items per page |
|
|
|
|
|
|
429
|
|
|
* @param integer $nItemsTotal The total number of items |
|
|
|
|
|
|
430
|
|
|
* |
|
431
|
|
|
* @return array |
|
432
|
|
|
*/ |
|
433
|
|
|
public function pages(int $nCurrentPage, int $nItemsPerPage, int $nItemsTotal): array |
|
434
|
|
|
{ |
|
435
|
|
|
return $this->xPaginator->setup($this, $nCurrentPage, $nItemsPerPage, $nItemsTotal)->getPages(); |
|
436
|
|
|
} |
|
|
|
|
|
|
437
|
|
|
} |
|
438
|
|
|
|