source: extensions/GrumPluginClasses/gpc_ajax.php @ 7175

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

Packing js files + add categorySelector functionnalities

File size: 8.1 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']=='admin.categorySelector.getList' or
77           $_REQUEST['ajaxfct']=='public.categorySelector.getList'
78          )
79        ) $_REQUEST['ajaxfct']='';
80
81      if(preg_match('/^admin\./i', $_REQUEST['ajaxfct']) and !is_admin()) $_REQUEST['ajaxfct']='';
82
83      if($_REQUEST['ajaxfct']!='')
84      {
85        /*
86         * check admin.rbuilder.fillCaddie values
87         */
88        if($_REQUEST['ajaxfct']=="admin.rbuilder.fillCaddie")
89        {
90          if(!isset($_REQUEST['fillMode'])) $_REQUEST['fillMode']="add";
91
92          if(!($_REQUEST['fillMode']=="add" or
93               $_REQUEST['fillMode']=="replace")) $_REQUEST['fillMode']="add";
94
95          if(!isset($_REQUEST['requestNumber'])) $_REQUEST['ajaxfct']="";
96        }
97
98        /*
99         * check admin.categorySelector.getList values
100         */
101        if($_REQUEST['ajaxfct']=="admin.categorySelector.getList" or
102           $_REQUEST['ajaxfct']=="public.categorySelector.getList")
103        {
104          if(!isset($_REQUEST['filter'])) $_REQUEST['filter']="accessible";
105
106          if(!($_REQUEST['filter']=="public" or
107               $_REQUEST['filter']=="accessible" or
108               $_REQUEST['filter']=="all")
109            ) $_REQUEST['filter']="accessible";
110
111          if(!isset($_REQUEST['galleryRoot'])) $_REQUEST['galleryRoot']="y";
112
113          if(!($_REQUEST['galleryRoot']=="y" or
114               $_REQUEST['galleryRoot']=="n")
115            ) $_REQUEST['galleryRoot']="y";
116
117          if(!isset($_REQUEST['tree'])) $_REQUEST['tree']="n";
118
119          if(!($_REQUEST['tree']=="y" or
120               $_REQUEST['tree']=="n")
121            ) $_REQUEST['tree']="n";
122        }
123
124
125      }
126    } //checkRequest()
127
128
129    /**
130     * return ajax content
131     */
132    protected function returnAjaxContent()
133    {
134      $result="<p class='errors'>An error has occured</p>";
135      switch($_REQUEST['ajaxfct'])
136      {
137        case 'admin.rbuilder.fillCaddie':
138          $result=$this->ajax_gpc_admin_rbuilderFillCaddie($_REQUEST['fillMode'], $_REQUEST['requestNumber']);
139          break;
140        case 'admin.categorySelector.getList':
141          $result=$this->ajax_gpc_admin_CategorySelectorGetList($_REQUEST['filter'], $_REQUEST['galleryRoot'], $_REQUEST['tree']);
142          break;
143        case 'public.categorySelector.getList':
144          $result=$this->ajax_gpc_public_CategorySelectorGetList($_REQUEST['filter'], $_REQUEST['galleryRoot'], $_REQUEST['tree']);
145          break;
146      }
147      GPCAjax::returnResult($result);
148    }
149
150
151
152    /*------------------------------------------------------------------------*
153     *
154     * ADMIN FUNCTIONS
155     *
156     *----------------------------------------------------------------------- */
157
158    /**
159     * fill the caddie with the result of the requestNumber
160     *
161     * @param String $fillMode : 'addCaddie' or 'replaceCaddie'
162     * @param Integer $requestNumber : number of the request in cache
163     * @return String :
164     */
165    private function ajax_gpc_admin_rbuilderFillCaddie($mode, $requestNumber)
166    {
167      global $user;
168
169      switch($mode)
170      {
171        case "replace":
172          $sql="DELETE FROM ".CADDIE_TABLE." WHERE user_id = '".$user['id']."';";
173          pwg_query($sql);
174        case "add":
175          $sql="INSERT IGNORE INTO ".CADDIE_TABLE."
176                  SELECT '".$user['id']."', image_id
177                  FROM ".$this->tables['result_cache']."
178                  WHERE id = '$requestNumber';";
179          pwg_query($sql);
180          break;
181      }
182    }
183
184
185
186    /**
187     * return the list of all categories
188     *
189     * @param String $filter : 'public' or 'accessible' or 'all'
190     * @param String $galleryRoot : 'y' if the gallery root is in the list
191     * @param String $tree : 'y' to obtain a recursive array, 'n' to obtain a flat array
192     * @return String : json string
193     */
194    private function ajax_gpc_admin_CategorySelectorGetList($filter, $galleryRoot, $tree)
195    {
196      global $user;
197
198      include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCCategorySelector.class.inc.php');
199
200      $categorySelector=new GPCCategorySelector(
201        array(
202          'filter' => $filter,
203          'galleryRoot' => ($galleryRoot=='y')?true:false,
204          'tree' => ($tree=='y')?true:false,
205          'userMode' => 'admin'
206        )
207      );
208
209      $returned=array(
210        'userId' => $user['id'],
211        'nbCategories' => 0,
212        'categories' => $categorySelector->getCategoryList(),
213        'status' => array(
214          0=>l10n('Private'),
215          1=>l10n('Public')
216        )
217      );
218      $returned['nbCategories']=count($returned['categories']);
219
220      return(json_encode($returned));
221    } //ajax_gpc_admin_CategorySelectorGetList
222
223
224    /**
225     * return the list of all categories
226     *
227     * @param String $filter : 'public' or 'accessible' or 'all'
228     * @param String $galleryRoot : 'y' if the gallery root is in the list
229     * @param String $tree : 'y' to obtain a recursive array, 'n' to obtain a flat array
230     * @param String $userMode : 'public' or 'admin'
231     * @return String : json string
232     */
233    private function ajax_gpc_public_CategorySelectorGetList($filter, $galleryRoot, $tree)
234    {
235      global $user;
236
237      include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCCategorySelector.class.inc.php');
238
239      $categorySelector=new GPCCategorySelector(
240        array(
241          'filter' => $filter,
242          'galleryRoot' => ($galleryRoot=='y')?true:false,
243          'tree' => ($tree=='y')?true:false,
244          'userMode' => 'public'
245        )
246      );
247
248      $returned=array(
249        'userId' => $user['id'],
250        'nbCategories' => 0,
251        'categories' => $categorySelector->getCategoryList(),
252        'status' => array(
253          0=>l10n('Private'),
254          1=>l10n('Public')
255        )
256      );
257      $returned['nbCategories']=count($returned['categories']);
258
259      return(json_encode($returned));
260    } //ajax_gpc_public_CategorySelectorGetList
261
262
263
264  } //class
265
266
267  $returned=new GPC_Ajax($prefixeTable, __FILE__);
268?>
Note: See TracBrowser for help on using the repository browser.