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\Factory; |
17
|
|
|
|
18
|
|
|
use Jaxon\Request\Request as JaxonRequest; |
19
|
|
|
use Jaxon\Request\Support\CallableObject; |
20
|
|
|
|
21
|
|
|
// Extends Parameter for compatibility with older versions (see function rq()) |
22
|
|
|
class Request extends Parameter |
23
|
|
|
{ |
24
|
|
|
use \Jaxon\Utils\Traits\Config; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The prefix to prepend on each call |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $sPrefix; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Set the name of the class to call |
35
|
|
|
* |
36
|
|
|
* @param string|null $sClass The callable class |
37
|
|
|
* |
38
|
|
|
* @return Factory |
39
|
|
|
*/ |
40
|
|
|
public function setClassName($sClass) |
41
|
|
|
{ |
42
|
|
|
$sClass = trim($sClass, '.\\ '); |
43
|
|
|
if(($sClass)) |
44
|
|
|
{ |
45
|
|
|
$xCallable = jaxon()->di()->get($sClass); |
46
|
|
|
$this->sPrefix = $this->getOption('core.prefix.class') . $xCallable->getJsName() . '.'; |
47
|
|
|
} |
48
|
|
|
else |
49
|
|
|
{ |
50
|
|
|
$this->sPrefix = $this->getOption('core.prefix.function'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set the callable object to call |
58
|
|
|
* |
59
|
|
|
* @param CallableObject $xCallable The callable object |
60
|
|
|
* |
61
|
|
|
* @return Factory |
62
|
|
|
*/ |
63
|
|
|
public function setCallable(CallableObject $xCallable) |
64
|
|
|
{ |
65
|
|
|
$this->sPrefix = $this->getOption('core.prefix.class') . $xCallable->getJsName() . '.'; |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Return the javascript call to a Jaxon function or object method |
72
|
|
|
* |
73
|
|
|
* @param string $sFunction The function or method (without class) name |
74
|
|
|
* @param ... $xParams The parameters of the function or method |
75
|
|
|
* |
76
|
|
|
* @return \Jaxon\Request\Request |
77
|
|
|
*/ |
78
|
|
|
public function call($sFunction) |
79
|
|
|
{ |
80
|
|
|
$aArguments = func_get_args(); |
81
|
|
|
$sFunction = (string)$sFunction; |
82
|
|
|
// Remove the function name from the arguments array. |
83
|
|
|
array_shift($aArguments); |
84
|
|
|
|
85
|
|
|
// Makes legacy code works |
86
|
|
|
if(strpos($sFunction, '.') !== false) |
87
|
|
|
{ |
88
|
|
|
// If there is a dot in the name, then it is a call to a class |
89
|
|
|
$this->sPrefix = $this->getOption('core.prefix.class'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// Make the request |
93
|
|
|
$xRequest = new JaxonRequest($this->sPrefix . $sFunction); |
94
|
|
|
$xRequest->useSingleQuote(); |
95
|
|
|
$xRequest->addParameters($aArguments); |
96
|
|
|
return $xRequest; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return the javascript call to a generic function |
101
|
|
|
* |
102
|
|
|
* @param string $sFunction The function or method (with class) name |
103
|
|
|
* @param ... $xParams The parameters of the function or method |
104
|
|
|
* |
105
|
|
|
* @return \Jaxon\Request\Request |
106
|
|
|
*/ |
107
|
|
|
public function func($sFunction) |
108
|
|
|
{ |
109
|
|
|
$aArguments = func_get_args(); |
110
|
|
|
$sFunction = (string)$sFunction; |
111
|
|
|
// Remove the function name from the arguments array. |
112
|
|
|
array_shift($aArguments); |
113
|
|
|
// Make the request |
114
|
|
|
$xRequest = new JaxonRequest($sFunction); |
115
|
|
|
$xRequest->useSingleQuote(); |
116
|
|
|
$xRequest->addParameters($aArguments); |
117
|
|
|
return $xRequest; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Make the pagination links for a registered Jaxon class method |
122
|
|
|
* |
123
|
|
|
* @param integer $nItemsTotal The total number of items |
124
|
|
|
* @param integer $nItemsPerPage The number of items per page page |
125
|
|
|
* @param integer $nCurrentPage The current page |
126
|
|
|
* @param string $sMethod The name of function or a method prepended with its class name |
127
|
|
|
* @param ... $xParams The parameters of the function or method |
128
|
|
|
* |
129
|
|
|
* @return string the pagination links |
130
|
|
|
*/ |
131
|
|
|
public function paginate($nItemsTotal, $nItemsPerPage, $nCurrentPage, $sMethod) |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
// Get the args list starting from the $sMethod |
134
|
|
|
$aArgs = array_slice(func_get_args(), 3); |
135
|
|
|
// Make the request |
136
|
|
|
$request = call_user_func_array('self::call', $aArgs); |
137
|
|
|
$paginator = jaxon()->paginator($nItemsTotal, $nItemsPerPage, $nCurrentPage, $request); |
138
|
|
|
return $paginator->toHtml(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.