source: extensions/GrumPluginClasses/gpc_ajax.php @ 8804

Last change on this file since 8804 was 7451, checked in by grum, 14 years ago

fix bug:1960
implement feature:1971

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