1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Factory.php - Jaxon Request Factory |
5
|
|
|
* |
6
|
|
|
* Create Jaxon client side requests, which will generate the client script necessary |
7
|
|
|
* to invoke a jaxon request from the browser to registered objects. |
8
|
|
|
* |
9
|
|
|
* @package jaxon-core |
10
|
|
|
* @author Thierry Feuzeu <[email protected]> |
11
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
13
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Jaxon\Request; |
17
|
|
|
|
18
|
|
|
use Jaxon\Jaxon; |
19
|
|
|
use Jaxon\Request\Factory\Request; |
20
|
|
|
use Jaxon\Request\Factory\Parameter; |
21
|
|
|
use Jaxon\Request\Support\CallableObject; |
22
|
|
|
use Jaxon\Request\Support\CallableRepository; |
23
|
|
|
|
24
|
|
|
// Extends Parameter for compatibility with older versions (see function rq()) |
25
|
|
|
class Factory |
26
|
|
|
{ |
27
|
|
|
use \Jaxon\Utils\Traits\Config; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The prefix to prepend on each call |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $sPrefix = ''; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The callable repository |
38
|
|
|
* |
39
|
|
|
* @var CallableRepository |
40
|
|
|
*/ |
41
|
|
|
protected $xRepository = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The class constructor |
45
|
|
|
* |
46
|
|
|
* @param CallableRepository $xRepository |
47
|
|
|
*/ |
48
|
|
|
public function __construct(CallableRepository $xRepository) |
49
|
|
|
{ |
50
|
|
|
$this->xRepository = $xRepository; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Set the name of the class to call |
55
|
|
|
* |
56
|
|
|
* @param string|null $sClass The callable class |
57
|
|
|
* |
58
|
|
|
* @return Factory |
59
|
|
|
*/ |
60
|
|
|
public function setClassName($sClass) |
61
|
|
|
{ |
62
|
|
|
$this->sPrefix = $this->getOption('core.prefix.function'); |
63
|
|
|
|
64
|
|
|
$sClass = trim($sClass, '.\\ '); |
65
|
|
|
if(!$sClass) |
66
|
|
|
{ |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if(!($xCallable = $this->xRepository->getCallableObject($sClass))) |
71
|
|
|
{ |
72
|
|
|
// Todo: decide which of these values to return |
73
|
|
|
// return null; |
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->sPrefix = $this->getOption('core.prefix.class') . $xCallable->getJsName() . '.'; |
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Set the callable object to call |
83
|
|
|
* |
84
|
|
|
* @param CallableObject $xCallable The callable object |
85
|
|
|
* |
86
|
|
|
* @return Factory |
87
|
|
|
*/ |
88
|
|
|
public function setCallable(CallableObject $xCallable) |
89
|
|
|
{ |
90
|
|
|
$this->sPrefix = $this->getOption('core.prefix.class') . $xCallable->getJsName() . '.'; |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Return the javascript call to a Jaxon function or object method |
97
|
|
|
* |
98
|
|
|
* @param string $sFunction The function or method (without class) name |
99
|
|
|
* @param ... $xParams The parameters of the function or method |
100
|
|
|
* |
101
|
|
|
* @return Request |
102
|
|
|
*/ |
103
|
|
|
public function call($sFunction) |
104
|
|
|
{ |
105
|
|
|
$aArguments = func_get_args(); |
106
|
|
|
$sFunction = (string)$sFunction; |
107
|
|
|
// Remove the function name from the arguments array. |
108
|
|
|
array_shift($aArguments); |
109
|
|
|
|
110
|
|
|
// Makes legacy code works |
111
|
|
|
if(strpos($sFunction, '.') !== false) |
112
|
|
|
{ |
113
|
|
|
// If there is a dot in the name, then it is a call to a class |
114
|
|
|
$this->sPrefix = $this->getOption('core.prefix.class'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// Make the request |
118
|
|
|
$xRequest = new Request($this->sPrefix . $sFunction); |
119
|
|
|
$xRequest->useSingleQuote(); |
120
|
|
|
$xRequest->addParameters($aArguments); |
121
|
|
|
return $xRequest; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Return the javascript call to a generic function |
126
|
|
|
* |
127
|
|
|
* @param string $sFunction The function or method (with class) name |
128
|
|
|
* @param ... $xParams The parameters of the function or method |
129
|
|
|
* |
130
|
|
|
* @return Request |
131
|
|
|
*/ |
132
|
|
|
public function func($sFunction) |
133
|
|
|
{ |
134
|
|
|
$aArguments = func_get_args(); |
135
|
|
|
$sFunction = (string)$sFunction; |
136
|
|
|
// Remove the function name from the arguments array. |
137
|
|
|
array_shift($aArguments); |
138
|
|
|
// Make the request |
139
|
|
|
$xRequest = new Request($sFunction); |
140
|
|
|
$xRequest->useSingleQuote(); |
141
|
|
|
$xRequest->addParameters($aArguments); |
142
|
|
|
return $xRequest; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Make the pagination links for a registered Jaxon class method |
147
|
|
|
* |
148
|
|
|
* @param integer $nItemsTotal The total number of items |
149
|
|
|
* @param integer $nItemsPerPage The number of items per page page |
150
|
|
|
* @param integer $nCurrentPage The current page |
151
|
|
|
* |
152
|
|
|
* @return string the pagination links |
153
|
|
|
*/ |
154
|
|
|
public function paginate($nItemsTotal, $nItemsPerPage, $nCurrentPage) |
155
|
|
|
{ |
156
|
|
|
// Get the args list starting from the $sMethod |
157
|
|
|
$aArgs = array_slice(func_get_args(), 3); |
158
|
|
|
// Make the request |
159
|
|
|
$request = call_user_func_array([$this, 'call'], $aArgs); |
160
|
|
|
$paginator = jaxon()->paginator($nItemsTotal, $nItemsPerPage, $nCurrentPage, $request); |
161
|
|
|
return $paginator->toHtml(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Make a parameter of type Jaxon::FORM_VALUES |
166
|
|
|
* |
167
|
|
|
* @param string $sFormId The id of the HTML form |
168
|
|
|
* |
169
|
|
|
* @return Parameter |
170
|
|
|
*/ |
171
|
|
|
public function form($sFormId) |
172
|
|
|
{ |
173
|
|
|
return new Parameter(Jaxon::FORM_VALUES, $sFormId); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Make a parameter of type Jaxon::INPUT_VALUE |
178
|
|
|
* |
179
|
|
|
* @param string $sInputId the id of the HTML input element |
180
|
|
|
* |
181
|
|
|
* @return Parameter |
182
|
|
|
*/ |
183
|
|
|
public function input($sInputId) |
184
|
|
|
{ |
185
|
|
|
return new Parameter(Jaxon::INPUT_VALUE, $sInputId); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Make a parameter of type Jaxon::CHECKED_VALUE |
190
|
|
|
* |
191
|
|
|
* @param string $sInputId the name of the HTML form element |
192
|
|
|
* |
193
|
|
|
* @return Parameter |
194
|
|
|
*/ |
195
|
|
|
public function checked($sInputId) |
196
|
|
|
{ |
197
|
|
|
return new Parameter(Jaxon::CHECKED_VALUE, $sInputId); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Make a parameter of type Jaxon::CHECKED_VALUE |
202
|
|
|
* |
203
|
|
|
* @param string $sInputId the name of the HTML form element |
204
|
|
|
* |
205
|
|
|
* @return Parameter |
206
|
|
|
*/ |
207
|
|
|
public function select($sInputId) |
208
|
|
|
{ |
209
|
|
|
return self::input($sInputId); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Make a parameter of type Jaxon::ELEMENT_INNERHTML |
214
|
|
|
* |
215
|
|
|
* @param string $sElementId the id of the HTML element |
216
|
|
|
* |
217
|
|
|
* @return Parameter |
218
|
|
|
*/ |
219
|
|
|
public function html($sElementId) |
220
|
|
|
{ |
221
|
|
|
return new Parameter(Jaxon::ELEMENT_INNERHTML, $sElementId); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Make a parameter of type Jaxon::QUOTED_VALUE |
226
|
|
|
* |
227
|
|
|
* @param string $sValue the value of the parameter |
228
|
|
|
* |
229
|
|
|
* @return Parameter |
230
|
|
|
*/ |
231
|
|
|
public function string($sValue) |
232
|
|
|
{ |
233
|
|
|
return new Parameter(Jaxon::QUOTED_VALUE, $sValue); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Make a parameter of type Jaxon::NUMERIC_VALUE |
238
|
|
|
* |
239
|
|
|
* @param numeric $nValue the value of the parameter |
240
|
|
|
* |
241
|
|
|
* @return Parameter |
242
|
|
|
*/ |
243
|
|
|
public function numeric($nValue) |
244
|
|
|
{ |
245
|
|
|
return new Parameter(Jaxon::NUMERIC_VALUE, intval($nValue)); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Make a parameter of type Jaxon::NUMERIC_VALUE |
250
|
|
|
* |
251
|
|
|
* @param numeric $nValue the value of the parameter |
252
|
|
|
* |
253
|
|
|
* @return Parameter |
254
|
|
|
*/ |
255
|
|
|
public function int($nValue) |
256
|
|
|
{ |
257
|
|
|
return self::numeric($nValue); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Make a parameter of type Jaxon::JS_VALUE |
262
|
|
|
* |
263
|
|
|
* @param string $sValue the javascript code of the parameter |
264
|
|
|
* |
265
|
|
|
* @return Parameter |
266
|
|
|
*/ |
267
|
|
|
public function javascript($sValue) |
268
|
|
|
{ |
269
|
|
|
return new Parameter(Jaxon::JS_VALUE, $sValue); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Make a parameter of type Jaxon::JS_VALUE |
274
|
|
|
* |
275
|
|
|
* @param string $sValue the javascript code of the parameter |
276
|
|
|
* |
277
|
|
|
* @return Parameter |
278
|
|
|
*/ |
279
|
|
|
public function js($sValue) |
280
|
|
|
{ |
281
|
|
|
return self::javascript($sValue); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Make a parameter of type Jaxon::PAGE_NUMBER |
286
|
|
|
* |
287
|
|
|
* @return Parameter |
288
|
|
|
*/ |
289
|
|
|
public function page() |
290
|
|
|
{ |
291
|
|
|
// By default, the value of a parameter of type Jaxon::PAGE_NUMBER is 0. |
292
|
|
|
return new Parameter(Jaxon::PAGE_NUMBER, 0); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|