source: extensions/GrumPluginClasses/gpc_ajax.php @ 7312

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

rbuilder is now accessible from public pages

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