source: extensions/lmt/lmt_ajax.php @ 15382

Last change on this file since 15382 was 15341, checked in by grum, 12 years ago

feature:2641 - Compatibility with Piwigo 2.4

  • Property svn:executable set to *
File size: 11.0 KB
Line 
1<?php
2/*
3 * -----------------------------------------------------------------------------
4 * Plugin Name: LMT
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 */
20
21define('PHPWG_ROOT_PATH',dirname(dirname(dirname(__FILE__))).'/');
22
23/*
24 * set ajax module in admin mode if request is used for admin interface
25 */
26if(!isset($_REQUEST['ajaxfct'])) $_REQUEST['ajaxfct']='';
27if(preg_match('/^admin\./i', $_REQUEST['ajaxfct']))
28{
29  define('IN_ADMIN', true);
30}
31
32// the common.inc.php file loads all the main.inc.php plugins files
33include_once(PHPWG_ROOT_PATH.'include/common.inc.php' );
34include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCAjax.class.inc.php');
35include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCPagesNavigation.class.inc.php');
36include_once('lmt_root.class.inc.php');
37
38load_language('plugin.lang', LMT_PATH);
39
40
41class LMT_Ajax extends LMT_root
42{
43  /**
44   * constructor
45   */
46  public function __construct($prefixeTable, $filelocation)
47  {
48    parent::__construct($prefixeTable, $filelocation);
49    $this->loadConfig();
50    $this->checkRequest();
51    $this->returnAjaxContent();
52  }
53
54  /**
55   * check the $_REQUEST values and set default values
56   *
57   */
58  protected function checkRequest()
59  {
60    global $user;
61
62    if(!isset($_REQUEST['ajaxfct'])) $_REQUEST['ajaxfct']='';
63    if(!isset($_REQUEST['errcode'])) $_REQUEST['errcode']='';
64
65    // check if asked function is valid
66    if(!($_REQUEST['ajaxfct']=='admin.img.list' or
67         $_REQUEST['ajaxfct']=='admin.manage.list'
68         )) $_REQUEST['ajaxfct']='';
69
70    if(preg_match('/^admin\./i', $_REQUEST['ajaxfct']) and !is_admin()) $_REQUEST['ajaxfct']='';
71
72
73    if($_REQUEST['ajaxfct']!='admin.img.list')
74    {
75      if(!isset($_REQUEST['numPage']) or
76         $_REQUEST['numPage'] <=0) $_REQUEST['ajaxfct']='';
77
78
79      if(!isset($_REQUEST['filter']) ||
80         !($_REQUEST['filter']=="BY" ||
81           $_REQUEST['filter']=="BY-SA" ||
82           $_REQUEST['filter']=="BY-ND" ||
83           $_REQUEST['filter']=="BY-NC-ND" ||
84           $_REQUEST['filter']=="BY-NC-SA" ||
85           $_REQUEST['filter']=="BY-NC" ||
86           $_REQUEST['filter']=="CRIGHT" ||
87           $_REQUEST['filter']=="CLEFT" ||
88           $_REQUEST['filter']=="CC0" ||
89           $_REQUEST['filter']=="PD"
90        ))
91      {
92        $_REQUEST['filter']='';
93      }
94    }
95
96    if($_REQUEST['ajaxfct']!='admin.manage.list')
97    {
98      if(!isset($_REQUEST['numPage']) or
99         $_REQUEST['numPage'] <=0) $_REQUEST['ajaxfct']='';
100    }
101
102  } //checkRequest
103
104
105  /**
106   * return ajax content
107   */
108  protected function returnAjaxContent()
109  {
110    $result="<p class='errors'>An error has occured</p>";
111    switch($_REQUEST['ajaxfct'])
112    {
113      case 'admin.img.list':
114        $result=$this->ajax_lmt_admin_imgList($_REQUEST['numPage'], $_REQUEST['filter']);
115        break;
116      case 'admin.manage.list':
117        $result=$this->ajax_lmt_admin_manageList($_REQUEST['numPage']);
118        break;
119    }
120    GPCAjax::returnResult($result);
121  }
122
123
124  /*------------------------------------------------------------------------*
125   *
126   * ADMIN FUNCTIONS
127   *
128   *----------------------------------------------------------------------- */
129
130
131  /**
132   * this function returns the list of pictures with a license
133   *
134   * @param Integer $pagenum : page number to display
135   * @param String $filter : filter to apply
136   * @return String : html string, ready to be displayed
137   */
138  protected function ajax_lmt_admin_imgList($pagenum, $filter)
139  {
140    global $conf, $template;
141
142    $template->set_filename('lmt_page',
143                  dirname($this->getFileLocation()).'/admin/plugin_admin_listitems.tpl');
144
145
146    $img_ids=array();
147    $img_liste = array();
148    $sql="SELECT SQL_CALC_FOUND_ROWS lmti.*, img.file, img.path,
149                  GROUP_CONCAT(cat.id) AS catid,
150                  GROUP_CONCAT(CASE WHEN cat.name IS NULL THEN '' ELSE cat.name END SEPARATOR '@sep@') AS catname,
151                  GROUP_CONCAT(CASE WHEN cat.permalink IS NULL THEN '' ELSE cat.permalink END SEPARATOR '@sep@') AS catpermalink,
152                  GROUP_CONCAT(CASE WHEN cat.dir IS NULL THEN 'V' ELSE 'P' END) AS cattype,
153                  lmtla.text1, lmtla.text2
154          FROM ".$this->tables["images"]." lmti
155                  LEFT OUTER JOIN ".$this->tables["licence_author"]." lmtla ON lmtla.id = lmti.author_id,
156               ".IMAGES_TABLE." img
157                  LEFT OUTER JOIN ".IMAGE_CATEGORY_TABLE." imgcat ON img.id = imgcat.image_id
158                  LEFT OUTER JOIN ".CATEGORIES_TABLE." cat ON imgcat.category_id = cat.id
159          WHERE lmti.image_id = img.id ";
160    if($_REQUEST['filter']!="")
161    {
162      $sql.=" AND lmti.licence_type='".$_REQUEST['filter']."'";
163    }
164
165    $sql.=" GROUP BY lmti.image_id ORDER BY cat.id, img.id ";
166
167    if($this->config['lmt_list_maxitems']>0)
168    {
169      $refdbt = ($pagenum-1)*$this->config['lmt_list_maxitems'];
170      $sql.=" LIMIT ".$refdbt.", ".$this->config['lmt_list_maxitems'];
171    }
172
173    $result=pwg_query($sql);
174    if($result)
175    {
176      while($row = pwg_db_fetch_assoc($result))
177      {
178        $filenfo = pathinfo($row['path']);
179        preg_match("/(.*)\./i", $filenfo["basename"], $tmp);
180        $filenfo['filename'] = $tmp[1];
181
182        $tmp=array(
183              'id'=>explode(',',$row['catid']),
184              'name'=>explode('@sep@',$row['catname']),
185              'type'=>explode(',',$row['cattype']),
186              'plinks'=>explode('@sep@',$row['catpermalink']),
187              'link'=>array()
188            );
189        $tmpcat=$this->makeImageDatas($tmp, $row['image_id']);
190
191        $img_ids[]=$row['image_id'];
192        $img_liste[]=array(
193          'id' => $row['image_id'],
194          'licence' => ($row['licence_type']=="CRIGHT")?l10n("lmt_lbl_cc_s-".strtolower($row['licence_type'])):"",
195          'licencei' => 'plugins/'.basename(LMT_PATH)."/img/".strtolower($row['licence_type'])."_80x15.png",
196          'aut_text1' => $row['text1'],
197          'aut_text2' => $row['text2'],
198          'file' => $row['file'],
199          'cat' => $tmpcat,
200          'thumb' => str_replace(PHPWG_ROOT_PATH, './', DerivativeImage::thumb_url(array('id'=>$row['image_id'], 'path'=>$row['path'])))
201        );
202      }
203    }
204
205    $sql="select FOUND_ROWS()";
206    $result=pwg_query($sql);
207    $nb=pwg_db_fetch_row($result);
208
209    $GPCPagesNavigation = new GPCPagesNavigation();
210    $GPCPagesNavigation->setNbItems($nb[0]);
211    $GPCPagesNavigation->setNbItemsPerPage($this->config['lmt_list_maxitems']);
212    $GPCPagesNavigation->setCurrentPage($pagenum);
213    $GPCPagesNavigation->setOptions(array(
214      "text_prev" => l10n("lmt_nav_prev"),
215      "text_next" => l10n("lmt_nav_next"),
216      "text_first" => l10n("lmt_nav_first"),
217      "text_last" => l10n("lmt_nav_last")
218    ));
219    $navbar=$GPCPagesNavigation->makeNavigation("loadpage");
220    if($navbar!="")
221    {
222      $navbar=", ".$navbar;
223    }
224
225    $template->assign('imgliste', $img_liste);
226    return($nb[0]."&nbsp;".l10n("lmt_lst_nb_photos").$navbar."#".$template->parse('lmt_page', true));
227  }
228
229  protected function ajax_lmt_admin_manageList($pagenum)
230  {
231    global $conf, $user, $template;
232
233    $template->set_filename('lmt_page',
234                  dirname($this->getFileLocation()).'/admin/plugin_admin_manageitems.tpl');
235
236    if(!isset($_REQUEST['select']))
237    {
238      $_REQUEST['select']="caddie";
239    }
240
241    $img_liste = array();
242    $sql="SELECT SQL_CALC_FOUND_ROWS img.id as image_id, img.file, img.path,
243                  GROUP_CONCAT(cat.id) AS catid,
244                  GROUP_CONCAT(CASE WHEN cat.name IS NULL THEN '' ELSE cat.name END SEPARATOR '@sep@') AS catname,
245                  GROUP_CONCAT(CASE WHEN cat.permalink IS NULL THEN '' ELSE cat.permalink END SEPARATOR '@sep@') AS catpermalink,
246                  GROUP_CONCAT(CASE WHEN cat.dir IS NULL THEN 'V' ELSE 'P' END) AS cattype,
247                 lmti.licence_type, lmtla.text1, lmtla.text2
248          FROM ".CADDIE_TABLE." caddie,
249              (".IMAGES_TABLE." img
250                  LEFT OUTER JOIN ".IMAGE_CATEGORY_TABLE." imgcat ON img.id = imgcat.image_id
251                  LEFT OUTER JOIN ".CATEGORIES_TABLE." cat ON imgcat.category_id = cat.id)
252                LEFT OUTER JOIN ".$this->tables["images"]." AS lmti ON img.id = lmti.image_id
253                LEFT OUTER JOIN ".$this->tables["licence_author"]." lmtla  ON lmtla.id = lmti.author_id
254          WHERE img.id = caddie.element_id
255            AND caddie.user_id = '".$user['id']."'
256          GROUP BY img.id
257          ORDER BY cat.id, img.id ";
258
259    if($this->config['lmt_list_maxitems']>0)
260    {
261      $refdbt = ($pagenum-1)*$this->config['lmt_list_maxitems'];
262      $sql.=" LIMIT ".$refdbt.", ".$this->config['lmt_list_maxitems'];
263    }
264
265    $result=pwg_query($sql);
266
267    if($result)
268    {
269      while($row = pwg_db_fetch_assoc($result))
270      {
271        $filenfo = pathinfo($row['path']);
272        preg_match("/(.*)\./i", $filenfo["basename"], $tmp);
273        $filenfo['filename'] = $tmp[1];
274
275        $tmp=array(
276              'id'=>explode(',',$row['catid']),
277              'name'=>explode('@sep@',$row['catname']),
278              'type'=>explode(',',$row['cattype']),
279              'plinks'=>explode('@sep@',$row['catpermalink']),
280              'link'=>array()
281            );
282        $tmpcat=$this->makeImageDatas($tmp, $row['image_id']);
283
284        $img_liste[]=array(
285          'id' => $row['image_id'],
286          'licence' => ($row['licence_type']=="")?"DEFAULT":$row['licence_type'],
287          'licencei' => ($row['licence_type']=="")?"":'plugins/'.basename(LMT_PATH)."/img/".strtolower($row['licence_type'])."_80x15.png",
288          'aut_text1' => $row['text1'],
289          'aut_text2' => $row['text2'],
290          'file' => $row['file'],
291          'cat' => $tmpcat,
292          'thumb' => str_replace(PHPWG_ROOT_PATH, './', DerivativeImage::thumb_url(array('id'=>$row['image_id'], 'path'=>$row['path'])))
293        );
294      }
295    }
296
297    $sql="select FOUND_ROWS()";
298    $result=pwg_query($sql);
299    $nb=pwg_db_fetch_row($result);
300
301    $GPCPagesNavigation = new GPCPagesNavigation();
302    $GPCPagesNavigation->setNbItems($nb[0]);
303    $GPCPagesNavigation->setNbItemsPerPage($this->config['lmt_list_maxitems']);
304    $GPCPagesNavigation->setCurrentPage($pagenum);
305    $GPCPagesNavigation->setOptions(array(
306      "text_prev" => l10n("lmt_nav_prev"),
307      "text_next" => l10n("lmt_nav_next"),
308      "text_first" => l10n("lmt_nav_first"),
309      "text_last" => l10n("lmt_nav_last")
310    ));
311    $navbar=$GPCPagesNavigation->makeNavigation("loadpage");
312    if($navbar!="")
313    {
314      $navbar=", ".$navbar;
315    }
316
317    $template->assign('imgliste', $img_liste);
318    return($nb[0]."&nbsp;".l10n("lmt_lst_nb_photos").$navbar."#".$template->parse('lmt_page', true));
319  }
320
321
322
323} //class
324
325
326$returned=new LMT_Ajax($prefixeTable, __FILE__);
327?>
Note: See TracBrowser for help on using the repository browser.