1 | <?php |
---|
2 | /* |
---|
3 | * ----------------------------------------------------------------------------- |
---|
4 | * Plugin Name: Grum Plugins Classes.3 |
---|
5 | * ----------------------------------------------------------------------------- |
---|
6 | * Author : Grum |
---|
7 | * email : grum@piwigo.org |
---|
8 | * website : http://photos.grum.fr |
---|
9 | * PWG user : http://forum.piwigo.org/profile.php?id=3706 |
---|
10 | * |
---|
11 | * << May the Little SpaceFrog be with you ! >> |
---|
12 | * |
---|
13 | * ----------------------------------------------------------------------------- |
---|
14 | * |
---|
15 | * See main.inc.php for release information |
---|
16 | * |
---|
17 | * manage all the ajax requests |
---|
18 | * |
---|
19 | * known functions : |
---|
20 | * - admin.rbuilder.fillCaddie |
---|
21 | * - admin.categorySelector.getList |
---|
22 | * |
---|
23 | * ----------------------------------------------------------------------------- |
---|
24 | */ |
---|
25 | |
---|
26 | define('PHPWG_ROOT_PATH',dirname(dirname(dirname(__FILE__))).'/'); |
---|
27 | |
---|
28 | /* |
---|
29 | * set ajax module in admin mode if request is used for admin interface |
---|
30 | */ |
---|
31 | if(!isset($_REQUEST['ajaxfct'])) $_REQUEST['ajaxfct']=''; |
---|
32 | if(preg_match('/^admin\./i', $_REQUEST['ajaxfct'])) |
---|
33 | { |
---|
34 | define('IN_ADMIN', true); |
---|
35 | } |
---|
36 | |
---|
37 | // the common.inc.php file loads all the main.inc.php plugins files |
---|
38 | include_once(PHPWG_ROOT_PATH.'include/common.inc.php' ); |
---|
39 | include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/CommonPlugin.class.inc.php'); |
---|
40 | include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCAjax.class.inc.php'); |
---|
41 | |
---|
42 | global $page; |
---|
43 | $page['root_path']='./'; |
---|
44 | |
---|
45 | load_language('plugin.lang', GPC_PATH); |
---|
46 | |
---|
47 | class GPC_Ajax extends CommonPlugin |
---|
48 | { |
---|
49 | /** |
---|
50 | * constructor |
---|
51 | */ |
---|
52 | public function __construct($prefixeTable, $filelocation) |
---|
53 | { |
---|
54 | $this->setPluginName("Grum Plugin Classes"); |
---|
55 | $this->setPluginNameFiles("gpc"); |
---|
56 | parent::__construct($prefixeTable, $filelocation); |
---|
57 | |
---|
58 | $tableList=array('result_cache'); |
---|
59 | $this->setTablesList($tableList); |
---|
60 | |
---|
61 | $this->loadConfig(); |
---|
62 | $this->checkRequest(); |
---|
63 | $this->returnAjaxContent(); |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | * check the $_REQUEST values and set default values |
---|
68 | * |
---|
69 | */ |
---|
70 | protected function checkRequest() |
---|
71 | { |
---|
72 | global $user; |
---|
73 | |
---|
74 | if(!isset($_REQUEST['ajaxfct'])) $_REQUEST['ajaxfct']=''; |
---|
75 | |
---|
76 | // check if asked function is valid |
---|
77 | if(!( |
---|
78 | $_REQUEST['ajaxfct']=='admin.rbuilder.fillCaddie' or |
---|
79 | $_REQUEST['ajaxfct']=='public.rbuilder.searchExecute' or |
---|
80 | $_REQUEST['ajaxfct']=='public.rbuilder.searchGetPage' or |
---|
81 | $_REQUEST['ajaxfct']=='admin.categorySelector.getList' or |
---|
82 | $_REQUEST['ajaxfct']=='public.categorySelector.getList' |
---|
83 | ) |
---|
84 | ) $_REQUEST['ajaxfct']=''; |
---|
85 | |
---|
86 | if(preg_match('/^admin\./i', $_REQUEST['ajaxfct']) and !is_admin()) $_REQUEST['ajaxfct']=''; |
---|
87 | |
---|
88 | if($_REQUEST['ajaxfct']!='') |
---|
89 | { |
---|
90 | /* |
---|
91 | * check admin.rbuilder.fillCaddie values |
---|
92 | */ |
---|
93 | if($_REQUEST['ajaxfct']=="admin.rbuilder.fillCaddie") |
---|
94 | { |
---|
95 | if(!isset($_REQUEST['fillMode'])) $_REQUEST['fillMode']="add"; |
---|
96 | |
---|
97 | if(!($_REQUEST['fillMode']=="add" or |
---|
98 | $_REQUEST['fillMode']=="replace")) $_REQUEST['fillMode']="add"; |
---|
99 | |
---|
100 | if(!isset($_REQUEST['requestNumber'])) $_REQUEST['ajaxfct']=""; |
---|
101 | } |
---|
102 | |
---|
103 | /* |
---|
104 | * check public.rbuilder.searchExecute values |
---|
105 | */ |
---|
106 | if($_REQUEST['ajaxfct']=="public.rbuilder.searchExecute") |
---|
107 | { |
---|
108 | if(!isset($_REQUEST['requestName'])) $_REQUEST['ajaxfct']=""; |
---|
109 | } |
---|
110 | |
---|
111 | /* |
---|
112 | * check public.rbuilder.searchGetPage values |
---|
113 | */ |
---|
114 | if($_REQUEST['ajaxfct']=="public.rbuilder.searchGetPage") |
---|
115 | { |
---|
116 | if(!isset($_REQUEST['requestNumber'])) $_REQUEST['ajaxfct']=""; |
---|
117 | |
---|
118 | if(!isset($_REQUEST['page'])) $_REQUEST['page']=0; |
---|
119 | |
---|
120 | if($_REQUEST['page']<0) $_REQUEST['page']=0; |
---|
121 | |
---|
122 | if(!isset($_REQUEST['numPerPage'])) $_REQUEST['numPerPage']=25; |
---|
123 | |
---|
124 | if($_REQUEST['numPerPage']>100) $_REQUEST['numPerPage']=100; |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | /* |
---|
129 | * check admin.categorySelector.getList values |
---|
130 | */ |
---|
131 | if($_REQUEST['ajaxfct']=="admin.categorySelector.getList" or |
---|
132 | $_REQUEST['ajaxfct']=="public.categorySelector.getList") |
---|
133 | { |
---|
134 | if(!isset($_REQUEST['filter'])) $_REQUEST['filter']="accessible"; |
---|
135 | |
---|
136 | if(!($_REQUEST['filter']=="public" or |
---|
137 | $_REQUEST['filter']=="accessible" or |
---|
138 | $_REQUEST['filter']=="all") |
---|
139 | ) $_REQUEST['filter']="accessible"; |
---|
140 | |
---|
141 | if(!isset($_REQUEST['galleryRoot'])) $_REQUEST['galleryRoot']="y"; |
---|
142 | |
---|
143 | if(!($_REQUEST['galleryRoot']=="y" or |
---|
144 | $_REQUEST['galleryRoot']=="n") |
---|
145 | ) $_REQUEST['galleryRoot']="y"; |
---|
146 | |
---|
147 | if(!isset($_REQUEST['tree'])) $_REQUEST['tree']="n"; |
---|
148 | |
---|
149 | if(!($_REQUEST['tree']=="y" or |
---|
150 | $_REQUEST['tree']=="n") |
---|
151 | ) $_REQUEST['tree']="n"; |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | } |
---|
156 | } //checkRequest() |
---|
157 | |
---|
158 | |
---|
159 | /** |
---|
160 | * return ajax content |
---|
161 | */ |
---|
162 | protected function returnAjaxContent() |
---|
163 | { |
---|
164 | $result="<p class='errors'>An error has occured</p>"; |
---|
165 | switch($_REQUEST['ajaxfct']) |
---|
166 | { |
---|
167 | case 'admin.rbuilder.fillCaddie': |
---|
168 | $result=$this->ajax_gpc_admin_rbuilderFillCaddie($_REQUEST['fillMode'], $_REQUEST['requestNumber']); |
---|
169 | break; |
---|
170 | case 'public.rbuilder.searchExecute': |
---|
171 | $result=$this->ajax_gpc_public_rbuilderSearchExecute(); |
---|
172 | break; |
---|
173 | case 'public.rbuilder.searchGetPage': |
---|
174 | $result=$this->ajax_gpc_public_rbuilderSearchGetPage(); |
---|
175 | break; |
---|
176 | case 'admin.categorySelector.getList': |
---|
177 | $result=$this->ajax_gpc_admin_CategorySelectorGetList($_REQUEST['filter'], $_REQUEST['galleryRoot'], $_REQUEST['tree']); |
---|
178 | break; |
---|
179 | case 'public.categorySelector.getList': |
---|
180 | $result=$this->ajax_gpc_public_CategorySelectorGetList($_REQUEST['filter'], $_REQUEST['galleryRoot'], $_REQUEST['tree']); |
---|
181 | break; |
---|
182 | } |
---|
183 | GPCAjax::returnResult($result); |
---|
184 | } |
---|
185 | |
---|
186 | |
---|
187 | |
---|
188 | /*------------------------------------------------------------------------* |
---|
189 | * |
---|
190 | * ADMIN FUNCTIONS |
---|
191 | * |
---|
192 | *----------------------------------------------------------------------- */ |
---|
193 | |
---|
194 | /** |
---|
195 | * fill the caddie with the result of the requestNumber |
---|
196 | * |
---|
197 | * @param String $fillMode : 'addCaddie' or 'replaceCaddie' |
---|
198 | * @param Integer $requestNumber : number of the request in cache |
---|
199 | * @return String : |
---|
200 | */ |
---|
201 | private function ajax_gpc_admin_rbuilderFillCaddie($mode, $requestNumber) |
---|
202 | { |
---|
203 | global $user; |
---|
204 | |
---|
205 | switch($mode) |
---|
206 | { |
---|
207 | case "replace": |
---|
208 | $sql="DELETE FROM ".CADDIE_TABLE." WHERE user_id = '".$user['id']."';"; |
---|
209 | pwg_query($sql); |
---|
210 | case "add": |
---|
211 | $sql="INSERT IGNORE INTO ".CADDIE_TABLE." |
---|
212 | SELECT '".$user['id']."', image_id |
---|
213 | FROM ".$this->tables['result_cache']." |
---|
214 | WHERE id = '$requestNumber';"; |
---|
215 | pwg_query($sql); |
---|
216 | break; |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | |
---|
221 | |
---|
222 | /** |
---|
223 | * return the list of all categories |
---|
224 | * |
---|
225 | * @param String $filter : 'public' or 'accessible' or 'all' |
---|
226 | * @param String $galleryRoot : 'y' if the gallery root is in the list |
---|
227 | * @param String $tree : 'y' to obtain a recursive array, 'n' to obtain a flat array |
---|
228 | * @return String : json string |
---|
229 | */ |
---|
230 | private function ajax_gpc_admin_CategorySelectorGetList($filter, $galleryRoot, $tree) |
---|
231 | { |
---|
232 | global $user; |
---|
233 | |
---|
234 | include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCCategorySelector.class.inc.php'); |
---|
235 | |
---|
236 | $categorySelector=new GPCCategorySelector( |
---|
237 | array( |
---|
238 | 'filter' => $filter, |
---|
239 | 'galleryRoot' => ($galleryRoot=='y')?true:false, |
---|
240 | 'tree' => ($tree=='y')?true:false, |
---|
241 | 'userMode' => 'admin' |
---|
242 | ) |
---|
243 | ); |
---|
244 | |
---|
245 | $returned=array( |
---|
246 | 'userId' => $user['id'], |
---|
247 | 'nbCategories' => 0, |
---|
248 | 'categories' => $categorySelector->getCategoryList(), |
---|
249 | 'status' => array( |
---|
250 | 0=>l10n('Private'), |
---|
251 | 1=>l10n('Public') |
---|
252 | ) |
---|
253 | ); |
---|
254 | $returned['nbCategories']=count($returned['categories']); |
---|
255 | |
---|
256 | return(json_encode($returned)); |
---|
257 | } //ajax_gpc_admin_CategorySelectorGetList |
---|
258 | |
---|
259 | |
---|
260 | /** |
---|
261 | * return the list of all categories |
---|
262 | * |
---|
263 | * @param String $filter : 'public' or 'accessible' or 'all' |
---|
264 | * @param String $galleryRoot : 'y' if the gallery root is in the list |
---|
265 | * @param String $tree : 'y' to obtain a recursive array, 'n' to obtain a flat array |
---|
266 | * @param String $userMode : 'public' or 'admin' |
---|
267 | * @return String : json string |
---|
268 | */ |
---|
269 | private function ajax_gpc_public_CategorySelectorGetList($filter, $galleryRoot, $tree) |
---|
270 | { |
---|
271 | global $user; |
---|
272 | |
---|
273 | include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCCategorySelector.class.inc.php'); |
---|
274 | |
---|
275 | $categorySelector=new GPCCategorySelector( |
---|
276 | array( |
---|
277 | 'filter' => $filter, |
---|
278 | 'galleryRoot' => ($galleryRoot=='y')?true:false, |
---|
279 | 'tree' => ($tree=='y')?true:false, |
---|
280 | 'userMode' => 'public' |
---|
281 | ) |
---|
282 | ); |
---|
283 | |
---|
284 | $returned=array( |
---|
285 | 'userId' => $user['id'], |
---|
286 | 'nbCategories' => 0, |
---|
287 | 'categories' => $categorySelector->getCategoryList(), |
---|
288 | 'status' => array( |
---|
289 | 0=>l10n('Private'), |
---|
290 | 1=>l10n('Public') |
---|
291 | ) |
---|
292 | ); |
---|
293 | $returned['nbCategories']=count($returned['categories']); |
---|
294 | |
---|
295 | return(json_encode($returned)); |
---|
296 | } //ajax_gpc_public_CategorySelectorGetList |
---|
297 | |
---|
298 | |
---|
299 | /** |
---|
300 | * |
---|
301 | * @return String : |
---|
302 | */ |
---|
303 | private function ajax_gpc_public_rbuilderSearchExecute() |
---|
304 | { |
---|
305 | global $prefixeTable; |
---|
306 | include_once(GPC_PATH."classes/GPCRequestBuilder.class.inc.php"); |
---|
307 | GPCRequestBuilder::init($prefixeTable, 'gpc'); |
---|
308 | return(GPCRequestBuilder::executeRequest($_REQUEST['ajaxfct'])); |
---|
309 | } |
---|
310 | |
---|
311 | /** |
---|
312 | * |
---|
313 | * @return String : |
---|
314 | */ |
---|
315 | private function ajax_gpc_public_rbuilderSearchGetPage() |
---|
316 | { |
---|
317 | global $prefixeTable; |
---|
318 | include_once(GPC_PATH."classes/GPCRequestBuilder.class.inc.php"); |
---|
319 | GPCRequestBuilder::init($prefixeTable, 'gpc'); |
---|
320 | return(GPCRequestBuilder::executeRequest($_REQUEST['ajaxfct'])); |
---|
321 | } |
---|
322 | |
---|
323 | |
---|
324 | } //class |
---|
325 | |
---|
326 | |
---|
327 | $returned=new GPC_Ajax($prefixeTable, __FILE__); |
---|
328 | ?> |
---|