source: extensions/GrumPluginClasses/gpc_ajax.php @ 7370

Last change on this file since 7370 was 7370, checked in by grum, 13 years ago

externalise and pack some js ; rename criteriaBuilder.js files ; improve templates & css theming ; fix bug and add functionnalities for request builder ; update key languages

File size: 13.2 KB
Line 
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 *  - public.categorySelector.getList
23 *  - public.rbuilder.searchExecute
24 *  - public.rbuilder.searchGetPage
25 *  - public.tagSelector.get
26 *  - admin.tagSelector.get
27 *
28 *
29 * -----------------------------------------------------------------------------
30 */
31
32  define('PHPWG_ROOT_PATH',dirname(dirname(dirname(__FILE__))).'/');
33
34  /*
35   * set ajax module in admin mode if request is used for admin interface
36   */
37  if(!isset($_REQUEST['ajaxfct'])) $_REQUEST['ajaxfct']='';
38  if(preg_match('/^admin\./i', $_REQUEST['ajaxfct']))
39  {
40    define('IN_ADMIN', true);
41  }
42
43  // the common.inc.php file loads all the main.inc.php plugins files
44  include_once(PHPWG_ROOT_PATH.'include/common.inc.php' );
45  include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/CommonPlugin.class.inc.php');
46  include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCAjax.class.inc.php');
47
48  global $page;
49  $page['root_path']='./';
50
51  load_language('plugin.lang', GPC_PATH);
52
53  class GPC_Ajax extends CommonPlugin
54  {
55    /**
56     * constructor
57     */
58    public function __construct($prefixeTable, $filelocation)
59    {
60      $this->setPluginName("Grum Plugin Classes");
61      $this->setPluginNameFiles("gpc");
62      parent::__construct($prefixeTable, $filelocation);
63
64      $tableList=array('result_cache');
65      $this->setTablesList($tableList);
66
67      $this->loadConfig();
68      $this->checkRequest();
69      $this->returnAjaxContent();
70    }
71
72    /**
73     * check the $_REQUEST values and set default values
74     *
75     */
76    protected function checkRequest()
77    {
78      global $user;
79
80      if(!isset($_REQUEST['ajaxfct'])) $_REQUEST['ajaxfct']='';
81
82      // check if asked function is valid
83      if(!(
84           $_REQUEST['ajaxfct']=='admin.rbuilder.fillCaddie' or
85           $_REQUEST['ajaxfct']=='public.rbuilder.searchExecute' or
86           $_REQUEST['ajaxfct']=='public.rbuilder.searchGetPage' or
87           $_REQUEST['ajaxfct']=='admin.categorySelector.getList' or
88           $_REQUEST['ajaxfct']=='public.categorySelector.getList' or
89           $_REQUEST['ajaxfct']=='public.tagSelector.get' or
90           $_REQUEST['ajaxfct']=='admin.tagSelector.get'
91          )
92        ) $_REQUEST['ajaxfct']='';
93
94      if(preg_match('/^admin\./i', $_REQUEST['ajaxfct']) and !is_admin()) $_REQUEST['ajaxfct']='';
95
96      if($_REQUEST['ajaxfct']!='')
97      {
98        /*
99         * check admin.rbuilder.fillCaddie values
100         */
101        if($_REQUEST['ajaxfct']=="admin.rbuilder.fillCaddie")
102        {
103          if(!isset($_REQUEST['fillMode'])) $_REQUEST['fillMode']="add";
104
105          if(!($_REQUEST['fillMode']=="add" or
106               $_REQUEST['fillMode']=="replace")) $_REQUEST['fillMode']="add";
107
108          if(!isset($_REQUEST['requestNumber'])) $_REQUEST['ajaxfct']="";
109        }
110
111        /*
112         * check public.rbuilder.searchExecute values
113         */
114        if($_REQUEST['ajaxfct']=="public.rbuilder.searchExecute")
115        {
116          if(!isset($_REQUEST['requestName'])) $_REQUEST['ajaxfct']="";
117        }
118
119        /*
120         * check public.rbuilder.searchGetPage values
121         */
122        if($_REQUEST['ajaxfct']=="public.rbuilder.searchGetPage")
123        {
124           if(!isset($_REQUEST['requestNumber'])) $_REQUEST['ajaxfct']="";
125
126          if(!isset($_REQUEST['page'])) $_REQUEST['page']=0;
127
128          if($_REQUEST['page']<0) $_REQUEST['page']=0;
129
130          if(!isset($_REQUEST['numPerPage'])) $_REQUEST['numPerPage']=25;
131
132          if($_REQUEST['numPerPage']>100) $_REQUEST['numPerPage']=100;
133        }
134
135
136        /*
137         * check admin.tagSelector.get values
138         */
139        if($_REQUEST['ajaxfct']=="admin.tagSelector.get" or
140           $_REQUEST['ajaxfct']=="public.tagSelector.get")
141        {
142          if(!isset($_REQUEST['letters'])) $_REQUEST['ajaxfct']="";
143          if(!isset($_REQUEST['filter'])) $_REQUEST['filter']="affected";
144
145          if(!($_REQUEST['filter']=="affected" or
146               $_REQUEST['filter']=="all")
147            ) $_REQUEST['filter']="affected";
148
149          if(!isset($_REQUEST['maxTags'])) $_REQUEST['maxTags']=1000;
150          if($_REQUEST['maxTags']<0) $_REQUEST['maxTags']=0;
151
152          if(!isset($_REQUEST['ignoreCase'])) $_REQUEST['ignoreCase']=true;
153
154        }
155
156
157        /*
158         * check public.categorySelector.getList values
159         */
160        if($_REQUEST['ajaxfct']=="admin.categorySelector.getList" or
161           $_REQUEST['ajaxfct']=="public.categorySelector.getList")
162        {
163          if(!isset($_REQUEST['filter'])) $_REQUEST['filter']="accessible";
164
165          if(!($_REQUEST['filter']=="public" or
166               $_REQUEST['filter']=="accessible" or
167               $_REQUEST['filter']=="all")
168            ) $_REQUEST['filter']="accessible";
169
170          if(!isset($_REQUEST['galleryRoot'])) $_REQUEST['galleryRoot']="y";
171
172          if(!($_REQUEST['galleryRoot']=="y" or
173               $_REQUEST['galleryRoot']=="n")
174            ) $_REQUEST['galleryRoot']="y";
175
176          if(!isset($_REQUEST['tree'])) $_REQUEST['tree']="n";
177
178          if(!($_REQUEST['tree']=="y" or
179               $_REQUEST['tree']=="n")
180            ) $_REQUEST['tree']="n";
181        }
182
183      }
184    } //checkRequest()
185
186
187    /**
188     * return ajax content
189     */
190    protected function returnAjaxContent()
191    {
192      $result="<p class='errors'>An error has occured</p>";
193      switch($_REQUEST['ajaxfct'])
194      {
195        case 'admin.rbuilder.fillCaddie':
196          $result=$this->ajax_gpc_admin_rbuilderFillCaddie($_REQUEST['fillMode'], $_REQUEST['requestNumber']);
197          break;
198        case 'public.rbuilder.searchExecute':
199          $result=$this->ajax_gpc_public_rbuilderSearchExecute();
200          break;
201        case 'public.rbuilder.searchGetPage':
202          $result=$this->ajax_gpc_public_rbuilderSearchGetPage();
203          break;
204        case 'admin.categorySelector.getList':
205          $result=$this->ajax_gpc_admin_CategorySelectorGetList($_REQUEST['filter'], $_REQUEST['galleryRoot'], $_REQUEST['tree']);
206          break;
207        case 'public.categorySelector.getList':
208          $result=$this->ajax_gpc_public_CategorySelectorGetList($_REQUEST['filter'], $_REQUEST['galleryRoot'], $_REQUEST['tree']);
209          break;
210        case 'admin.tagSelector.get':
211          $result=$this->ajax_gpc_both_TagSelectorGet('admin', $_REQUEST['letters'], $_REQUEST['filter'], $_REQUEST['maxTags'], $_REQUEST['ignoreCase']);
212          break;
213        case 'public.tagSelector.get':
214          $result=$this->ajax_gpc_both_TagSelectorGet('public', $_REQUEST['letters'], $_REQUEST['filter'], $_REQUEST['maxTags'], $_REQUEST['ignoreCase']);
215          break;
216      }
217      GPCAjax::returnResult($result);
218    }
219
220
221
222    /*------------------------------------------------------------------------*
223     *
224     * ADMIN FUNCTIONS
225     *
226     *----------------------------------------------------------------------- */
227
228    /**
229     * fill the caddie with the result of the requestNumber
230     *
231     * @param String $fillMode : 'addCaddie' or 'replaceCaddie'
232     * @param Integer $requestNumber : number of the request in cache
233     * @return String :
234     */
235    private function ajax_gpc_admin_rbuilderFillCaddie($mode, $requestNumber)
236    {
237      global $user;
238
239      switch($mode)
240      {
241        case "replace":
242          $sql="DELETE FROM ".CADDIE_TABLE." WHERE user_id = '".$user['id']."';";
243          pwg_query($sql);
244        case "add":
245          $sql="INSERT IGNORE INTO ".CADDIE_TABLE."
246                  SELECT '".$user['id']."', image_id
247                  FROM ".$this->tables['result_cache']."
248                  WHERE id = '$requestNumber';";
249          pwg_query($sql);
250          break;
251      }
252    }
253
254
255
256    /**
257     * return the list of all categories
258     *
259     * @param String $filter : 'public' or 'accessible' or 'all'
260     * @param String $galleryRoot : 'y' if the gallery root is in the list
261     * @param String $tree : 'y' to obtain a recursive array, 'n' to obtain a flat array
262     * @return String : json string
263     */
264    private function ajax_gpc_admin_CategorySelectorGetList($filter, $galleryRoot, $tree)
265    {
266      global $user;
267
268      include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCCategorySelector.class.inc.php');
269
270      $categorySelector=new GPCCategorySelector(
271        array(
272          'filter' => $filter,
273          'galleryRoot' => ($galleryRoot=='y')?true:false,
274          'tree' => ($tree=='y')?true:false,
275          'userMode' => 'admin'
276        )
277      );
278
279      $returned=array(
280        'userId' => $user['id'],
281        'nbCategories' => 0,
282        'categories' => $categorySelector->getCategoryList(),
283        'status' => array(
284          0=>l10n('Private'),
285          1=>l10n('Public')
286        )
287      );
288      $returned['nbCategories']=count($returned['categories']);
289
290      return(json_encode($returned));
291    } //ajax_gpc_admin_CategorySelectorGetList
292
293
294    /**
295     * return the list of all categories
296     *
297     * @param String $filter : 'public' or 'accessible' or 'all'
298     * @param String $galleryRoot : 'y' if the gallery root is in the list
299     * @param String $tree : 'y' to obtain a recursive array, 'n' to obtain a flat array
300     * @param String $userMode : 'public' or 'admin'
301     * @return String : json string
302     */
303    private function ajax_gpc_public_CategorySelectorGetList($filter, $galleryRoot, $tree)
304    {
305      global $user;
306
307      include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCCategorySelector.class.inc.php');
308
309      $categorySelector=new GPCCategorySelector(
310        array(
311          'filter' => $filter,
312          'galleryRoot' => ($galleryRoot=='y')?true:false,
313          'tree' => ($tree=='y')?true:false,
314          'userMode' => 'public'
315        )
316      );
317
318      $returned=array(
319        'userId' => $user['id'],
320        'nbCategories' => 0,
321        'categories' => $categorySelector->getCategoryList(),
322        'status' => array(
323          0=>l10n('Private'),
324          1=>l10n('Public')
325        )
326      );
327      $returned['nbCategories']=count($returned['categories']);
328
329      return(json_encode($returned));
330    } //ajax_gpc_public_CategorySelectorGetList
331
332
333    /**
334     *
335     * @return String :
336     */
337    private function ajax_gpc_public_rbuilderSearchExecute()
338    {
339      global $prefixeTable;
340      include_once(GPC_PATH."classes/GPCRequestBuilder.class.inc.php");
341      GPCRequestBuilder::init($prefixeTable, 'gpc');
342      return(GPCRequestBuilder::executeRequest($_REQUEST['ajaxfct']));
343    }
344
345    /**
346     *
347     * @return String :
348     */
349    private function ajax_gpc_public_rbuilderSearchGetPage()
350    {
351      global $prefixeTable;
352      include_once(GPC_PATH."classes/GPCRequestBuilder.class.inc.php");
353      GPCRequestBuilder::init($prefixeTable, 'gpc');
354      return(GPCRequestBuilder::executeRequest($_REQUEST['ajaxfct']));
355    }
356
357
358
359    /**
360     * return the list of all known tags
361     *
362     * @param Striong $mode : 'admin' or 'public'
363     * @param String $letters : filtering on letters
364     * @param String $filter : 'accessible' or 'all'
365     * @param Integer $maxTags : maximum of items returned ; 0 = no limits
366     * @return String : json string
367     */
368    private function ajax_gpc_both_TagSelectorGet($mode, $letters, $filter, $maxTags, $ignoreCase)
369    {
370      global $user;
371
372      $returned=array(
373        'totalNbTags' => 0,
374        'tags' => array(),
375      );
376
377      $binary='';
378      $where=' LOWER(ptt.name) ';
379      if(!$ignoreCase)
380      {
381        $binary=" BINARY ";
382        $where=" ptt.name ";
383      }
384
385
386      $sql="SELECT DISTINCT SQL_CALC_FOUND_ROWS ptt.id, ptt.name
387            FROM ((".TAGS_TABLE." ptt ";
388
389      if($filter=='affected')
390      {
391        $sql.=" JOIN ".IMAGE_TAG_TABLE." pitt
392                ON pitt.tag_id = ptt.id ) ";
393      }
394      else
395      {
396        $sql.=" ) ";
397      }
398
399
400      if($mode=='public' and $filter=='affected')
401      {
402        $sql.=" JOIN ".IMAGE_CATEGORY_TABLE." pict
403                ON pict.image_id = pitt.image_id )
404                JOIN ".USER_CACHE_CATEGORIES_TABLE." pucc
405                ON (pucc.cat_id = pict.category_id AND pucc.user_id='".$user['id']."' )";
406      }
407      else
408      {
409        $sql.=" ) ";
410      }
411
412      $sql.=" WHERE $where LIKE $binary '%$letters%'
413            ORDER BY ptt.name ";
414
415      if($maxTags>0) $sql.=" LIMIT 0, ".$maxTags;
416
417      $result=pwg_query($sql);
418      if($result)
419      {
420        while($row=pwg_db_fetch_assoc($result))
421        {
422          $returned['tags'][]=$row;
423        }
424
425        $sql="SELECT FOUND_ROWS();";
426        $result=pwg_query($sql);
427        if($result)
428        {
429          while($row=pwg_db_fetch_row($result))
430          {
431            $returned['totalNbTags']=$row[0];
432          }
433        }
434      }
435
436      return(json_encode($returned));
437    } //ajax_gpc_both_TagSelectorGet
438
439
440  } //class
441
442
443  $returned=new GPC_Ajax($prefixeTable, __FILE__);
444
445
446
447
448
449?>
Note: See TracBrowser for help on using the repository browser.