source: extensions/AMenuManager/amm_ajax.php @ 20461

Last change on this file since 20461 was 16006, checked in by grum, 12 years ago

feature:2642- compatibility with Piwigo 2.4

  • Property svn:executable set to *
File size: 22.0 KB
Line 
1<?php
2/*
3 * -----------------------------------------------------------------------------
4 * Plugin Name: Advanced Menu Manager
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
21
22  define('PHPWG_ROOT_PATH',dirname(dirname(dirname(__FILE__))).'/');
23  if(!defined('AJAX_CALL')) define('AJAX_CALL', true);
24
25  /*
26   * set ajax module in admin mode if request is used for admin interface
27   */
28  if(!isset($_REQUEST['ajaxfct'])) $_REQUEST['ajaxfct']='';
29  if(preg_match('/^admin\./i', $_REQUEST['ajaxfct'])) define('IN_ADMIN', true);
30
31  // the common.inc.php file loads all the main.inc.php plugins files
32  include_once(PHPWG_ROOT_PATH.'include/common.inc.php' );
33  include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCAjax.class.inc.php');
34  include_once('amm_root.class.inc.php');
35
36  load_language('plugin.lang', AMM_PATH);
37
38
39  class AMM_Ajax extends AMM_root
40  {
41    /**
42     * constructor
43     */
44    public function __construct($prefixeTable, $filelocation)
45    {
46      parent::__construct($prefixeTable, $filelocation);
47      $this->loadConfig();
48      $this->checkRequest();
49      $this->returnAjaxContent();
50    }
51
52    /**
53     * check the $_REQUEST values and set default values
54     *
55     */
56    protected function checkRequest()
57    {
58      global $user;
59
60      if(!isset($_REQUEST['errcode'])) $_REQUEST['errcode']='';
61      GPCAjax::checkToken();
62
63      // check if asked function is valid
64      if(!($_REQUEST[GPC_AJAX]=='admin.links.get' or
65           $_REQUEST[GPC_AJAX]=='admin.links.set' or
66           $_REQUEST[GPC_AJAX]=='admin.links.list' or
67           $_REQUEST[GPC_AJAX]=='admin.links.delete' or
68           $_REQUEST[GPC_AJAX]=='admin.links.order' or
69           $_REQUEST[GPC_AJAX]=='admin.links.setConfig' or
70           $_REQUEST[GPC_AJAX]=='admin.randomPict.setConfig' or
71           $_REQUEST[GPC_AJAX]=='admin.blocks.get' or
72           $_REQUEST[GPC_AJAX]=='admin.blocks.set' or
73           $_REQUEST[GPC_AJAX]=='admin.blocks.delete' or
74           $_REQUEST[GPC_AJAX]=='admin.blocks.list' or
75           $_REQUEST[GPC_AJAX]=='admin.album.setConfig' or
76           $_REQUEST[GPC_AJAX]=='admin.coreBlocks.setConfig'
77           )) $_REQUEST[GPC_AJAX]='';
78
79      if(preg_match('/^admin\./i', $_REQUEST[GPC_AJAX]) and !is_admin()) $_REQUEST[GPC_AJAX]='';
80
81
82      if($_REQUEST[GPC_AJAX]!='')
83      {
84        /*
85         * no check for admin.links.list request
86         */
87
88        /*
89         * check admin.links.get request
90         * check admin.blocks.get request
91         */
92        if($_REQUEST[GPC_AJAX]=='admin.links.get' or
93           $_REQUEST[GPC_AJAX]=='admin.blocks.get request')
94        {
95          if(!isset($_REQUEST['id'])) $_REQUEST['id']='';
96
97          if($_REQUEST['id']=='') $_REQUEST[GPC_AJAX]='';
98        }
99
100        /*
101         * check admin.links.set request
102         */
103        if($_REQUEST[GPC_AJAX]=='admin.links.set')
104        {
105          if(!isset($_REQUEST['id'])) $_REQUEST['id']='';
106          if(!isset($_REQUEST['datas']['label'])) $_REQUEST['datas']['label']='';
107          if(!isset($_REQUEST['datas']['url'])) $_REQUEST['datas']['url']='';
108          if(!isset($_REQUEST['datas']['icon'])) $_REQUEST['datas']['icon']='';
109          if(!isset($_REQUEST['datas']['mode'])) $_REQUEST['datas']['mode']='0';
110          if(!isset($_REQUEST['datas']['visible'])) $_REQUEST['datas']['visible']='n';
111          if(!isset($_REQUEST['datas']['accessUsers']) or $_REQUEST['datas']['accessUsers']=='') $_REQUEST['datas']['accessUsers']=array();
112          if(!isset($_REQUEST['datas']['accessGroups']) or $_REQUEST['datas']['accessGroups']=='') $_REQUEST['datas']['accessGroups']=array();
113
114          if($_REQUEST['datas']['label']=='' or
115             $_REQUEST['datas']['url']=='' or
116             $_REQUEST['datas']['icon']=='' or
117             !($_REQUEST['datas']['mode']=='0' or $_REQUEST['datas']['mode']=='1') or
118             !($_REQUEST['datas']['visible']=='y' or $_REQUEST['datas']['visible']=='n')
119            ) $_REQUEST[GPC_AJAX]='';
120        }
121
122        /*
123         * check admin.links.delete request
124         * check admin.blocks.delete request
125         *
126         */
127        if($_REQUEST[GPC_AJAX]=='admin.links.delete' or
128           $_REQUEST[GPC_AJAX]=='admin.blocks.delete')
129        {
130          if(!isset($_REQUEST['id'])) $_REQUEST['id']='';
131
132          if($_REQUEST['id']=='') $_REQUEST[GPC_AJAX]='';
133        }
134
135        /*
136         * check admin.links.order request
137         */
138        if($_REQUEST[GPC_AJAX]=='admin.links.order')
139        {
140          if(!isset($_REQUEST['datas']['links']) or $_REQUEST['datas']['links']=='') $_REQUEST['datas']['links']=array();
141
142          if(count($_REQUEST['datas']['links'])<=1) $_REQUEST[GPC_AJAX]='';
143        }
144
145
146        /*
147         * check admin.links.setConfig request
148         */
149        if($_REQUEST[GPC_AJAX]=='admin.links.setConfig')
150        {
151          if(!isset($_REQUEST['datas']['showIcons'])) $_REQUEST['datas']['showIcons']='';
152          if(!isset($_REQUEST['datas']['title']) or $_REQUEST['datas']['title']=='') $_REQUEST['datas']['title']=array();
153
154          if($_REQUEST['datas']['showIcons']=='' or
155             count($_REQUEST['datas']['title'])==0
156            ) $_REQUEST[GPC_AJAX]='';
157        }
158
159
160
161        /*
162         * check admin.randomPict.setConfig request
163         */
164        if($_REQUEST[GPC_AJAX]=='admin.randomPict.setConfig')
165        {
166          if(!isset($_REQUEST['datas']['blockHeight'])) $_REQUEST['datas']['blockHeight']='';
167          if(!isset($_REQUEST['datas']['blockTitles']) or $_REQUEST['datas']['blockTitles']=='') $_REQUEST['datas']['blockTitles']=array();
168          if(!isset($_REQUEST['datas']['infosName'])) $_REQUEST['datas']['infosName']='';
169          if(!isset($_REQUEST['datas']['infosComment'])) $_REQUEST['datas']['infosComment']='';
170          if(!isset($_REQUEST['datas']['freqDelay'])) $_REQUEST['datas']['freqDelay']='';
171          if(!isset($_REQUEST['datas']['selectMode'])) $_REQUEST['datas']['selectMode']='';
172          if(!isset($_REQUEST['datas']['selectCat']) or $_REQUEST['datas']['selectCat']=='') $_REQUEST['datas']['selectCat']=array();
173
174          if(!is_numeric($_REQUEST['datas']['blockHeight']) or
175             count($_REQUEST['datas']['blockTitles'])==0 or
176             !($_REQUEST['datas']['infosName']=='n' or
177               $_REQUEST['datas']['infosName']=='o' or
178               $_REQUEST['datas']['infosName']=='u') or
179             !($_REQUEST['datas']['infosComment']=='n' or
180               $_REQUEST['datas']['infosComment']=='o' or
181               $_REQUEST['datas']['infosComment']=='u') or
182             !is_numeric($_REQUEST['datas']['freqDelay']) or
183             !($_REQUEST['datas']['selectMode']=='a' or
184               $_REQUEST['datas']['selectMode']=='f' or
185               $_REQUEST['datas']['selectMode']=='c') or
186             ($_REQUEST['datas']['selectMode']=='c' and
187              count($_REQUEST['datas']['selectCat'])==0)
188            ) $_REQUEST[GPC_AJAX]='';
189        }
190
191
192        /*
193         * check admin.blocks.set request
194         */
195        if($_REQUEST[GPC_AJAX]=='admin.blocks.set')
196        {
197          if(!isset($_REQUEST['id'])) $_REQUEST['id']='';
198          if(!isset($_REQUEST['datas']['nfo'])) $_REQUEST['datas']['nfo']='';
199          if(!isset($_REQUEST['datas']['visible'])) $_REQUEST['datas']['visible']='';
200          if(!isset($_REQUEST['datas']['langs']) or $_REQUEST['datas']['langs']=='') $_REQUEST['datas']['langs']=array();
201
202          if($_REQUEST['datas']['nfo']=='' or
203             !($_REQUEST['datas']['visible']=='y' or $_REQUEST['datas']['visible']=='n') or
204             count($_REQUEST['datas']['langs'])==0
205            ) $_REQUEST[GPC_AJAX]='';
206        }
207
208
209
210
211        /*
212         * check admin.coreBlocks.setConfig request
213         */
214        if($_REQUEST[GPC_AJAX]=='admin.coreBlocks.setConfig')
215        {
216          if(!isset($_REQUEST['datas']['menuItems']) or $_REQUEST['datas']['menuItems']=='') $_REQUEST['datas']['menuItems']=array();
217          if(!isset($_REQUEST['datas']['blocks']) or $_REQUEST['datas']['blocks']=='') $_REQUEST['datas']['blocks']=array();
218
219          if(count($_REQUEST['datas']['menuItems'])!=count($this->defaultMenus)
220            ) $_REQUEST[GPC_AJAX]='';
221        }
222
223
224        /*
225         * check admin.album.setConfig request
226         */
227        if($_REQUEST[GPC_AJAX]=='admin.album.setConfig')
228        {
229          if(!isset($_REQUEST['datas']['selectCat']) or $_REQUEST['datas']['selectCat']=='') $_REQUEST['datas']['selectCat']=array();
230        }
231
232      }
233
234    } //checkRequest
235
236
237    /**
238     * return ajax content
239     */
240    protected function returnAjaxContent()
241    {
242      $result="KO!".l10n('g002_error_invalid_ajax_call');
243      switch($_REQUEST[GPC_AJAX])
244      {
245        case 'admin.links.get':
246          $result=$this->ajax_amm_admin_linksGet($_REQUEST['id']);
247          break;
248        case 'admin.links.set':
249          $result=$this->ajax_amm_admin_linksSet($_REQUEST['id'],$_REQUEST['datas']['label'],$_REQUEST['datas']['url'],$_REQUEST['datas']['mode'],$_REQUEST['datas']['icon'],$_REQUEST['datas']['visible'],$_REQUEST['datas']['accessUsers'],$_REQUEST['datas']['accessGroups']);
250          break;
251        case 'admin.links.list':
252          $result=$this->ajax_amm_admin_linksList();
253          break;
254        case 'admin.links.order':
255          $result=$this->ajax_amm_admin_linksOrder($_REQUEST['datas']['links']);
256          break;
257        case 'admin.links.delete':
258          $result=$this->ajax_amm_admin_linksDelete($_REQUEST['id']);
259          break;
260        case 'admin.links.setConfig':
261          $result=$this->ajax_amm_admin_linksSetConfig($_REQUEST['datas']);
262          break;
263
264        case 'admin.randomPict.setConfig':
265          $result=$this->ajax_amm_admin_randomPictSetConfig($_REQUEST['datas']);
266          break;
267
268        case 'admin.blocks.get':
269          $result=$this->ajax_amm_admin_blocksGet($_REQUEST['id']);
270          break;
271        case 'admin.blocks.set':
272          $result=$this->ajax_amm_admin_blocksSet($_REQUEST['id'],$_REQUEST['datas']['visible'],$_REQUEST['datas']['nfo'],$_REQUEST['datas']['langs']);
273          break;
274        case 'admin.blocks.list':
275          $result=$this->ajax_amm_admin_blocksList();
276          break;
277        case 'admin.blocks.delete':
278          $result=$this->ajax_amm_admin_blocksDelete($_REQUEST['id']);
279          break;
280
281        case 'admin.coreBlocks.setConfig':
282          $result=$this->ajax_amm_admin_coreBlocksSetConfig($_REQUEST['datas']['menuItems'], $_REQUEST['datas']['blocks']);
283          break;
284
285        case 'admin.album.setConfig':
286          $result=$this->ajax_amm_admin_albumSetConfig($_REQUEST['datas']);
287          break;
288      }
289      GPCAjax::returnResult($result);
290    }
291
292
293    /*
294     * -------------------------------------------------------------------------
295     *
296     * ADMIN FUNCTIONS
297     *
298     * -------------------------------------------------------------------------
299     */
300
301
302    /*
303     * -------------------------------------------------------------------------
304     * Links
305     * -------------------------------------------------------------------------
306     */
307
308    /**
309     * return a html formatted list of urls
310     */
311    private function ajax_amm_admin_linksList()
312    {
313      global $template, $user;
314      $local_tpl = new Template(AMM_PATH."admin/", "");
315      $local_tpl->set_filename('body_page',
316                    dirname($this->getFileLocation()).'/admin/amm_linkslinks_detail.tpl');
317
318
319
320      $datas['links']=array();
321
322      $links=$this->getLinks();
323      foreach($links as $link)
324      {
325        $datas['links'][]=array(
326          'id' => $link['id'],
327          'label' => $link['label'],
328          'url' => $link['url'],
329          'mode' => l10n("g002_mode_".$this->urlsModes[$link['mode']]),
330          'icon' => "plugins/".AMM_DIR."/links_pictures/".$link['icon'],
331          'visible' => l10n('g002_yesno_'.$link['visible'])
332        );
333      }
334      $local_tpl->assign('themeconf', $template->get_template_vars('themeconf'));
335      $local_tpl->assign('datas', $datas);
336      $local_tpl->assign('plugin', array('PATH' => AMM_PATH));
337
338      return($local_tpl->parse('body_page', true));
339    }
340
341
342    /**
343     * update links order
344     *
345     * @param Array $links
346     * @return String : OK or KO
347     */
348    private function ajax_amm_admin_linksOrder($links)
349    {
350      return($this->setLinksOrder($links)?'OK':'KO');
351    }
352
353
354    /**
355     * delete a link
356     *
357     * @param Integer $id : link id
358     * @return String : OK or KO
359     */
360    private function ajax_amm_admin_linksDelete($id)
361    {
362      return($this->deleteLink($id)?'OK':'KO');
363    }
364
365    /**
366     * return link content as a json string
367     *
368     * @param Integer $id : link id
369     * @return String : json string
370     */
371    private function ajax_amm_admin_linksGet($id)
372    {
373      $link=$this->getLink($id);
374      $link['accessUsers']=explode(',', $link['accessUsers']);
375      $link['accessGroups']=explode(',', $link['accessGroups']);
376      return(json_encode($link));
377    }
378
379    /**
380     * set link values
381     * if id is empty, create a new link
382     *
383     * @param String $id : link id
384     * @param String $label : link label
385     * @param String $url : link url
386     * @param String $mode : link mode (open a new window or not)
387     * @param String $icon : displayed icon
388     * @param String $visible : link visibility
389     * @return String : $id if OK, otherwise -1
390     */
391    private function ajax_amm_admin_linksSet($id, $label, $url, $mode, $icon, $visible, $accessUsers, $accessGroups)
392    {
393      return($this->setLink($id, $label, $url, $mode, $icon, $visible, implode(',', $accessUsers), implode(',', $accessGroups)));
394    }
395
396    /**
397     * set the links config
398     *
399     * $config is an array with keys :
400     *  String 'showIcons' : values 'y' or 'n'
401     *  Array  'titles'    : each array occurs is an array('id' => '', 'value' => '')
402     *                            id = lang id ('fr_FR', 'en_UK', ...)
403     *
404     * @param Array $config
405     * @return String : OK or KO
406     */
407    private function ajax_amm_admin_linksSetConfig($config)
408    {
409      $this->config['amm_links_show_icons']=$config['showIcons'];
410
411      $this->config['amm_links_title']=array();
412      foreach($config['title'] as $title)
413      {
414        $this->config['amm_links_title'][$title['id']]=base64_encode($title['value']);
415      }
416
417      $this->saveConfig();
418
419      return('OK!'.l10n('g002_config_saved'));
420    }
421
422
423    /*
424     * -------------------------------------------------------------------------
425     * random picture
426     * -------------------------------------------------------------------------
427     */
428
429
430    /**
431     * set the random picture config
432     *
433     * $config is an array with keys :
434     *  Integer 'blockHeight' : if value=0, the browser assume an automatic size
435     *  Array  'blockTitles'  : each array occurs is an array('id' => '', 'value' => '')
436     *                            id = lang id ('fr_FR', 'en_UK', ...)
437     *  String 'infosName'      : allow to display picture's name ;
438     *                              can take 'n' (no), 'o' (over), or 'u' (under)
439     *  String 'infosComment'   : allow to display picture's comment ;
440     *                              can take 'n' (no), 'o' (over), or 'u' (under)
441     *  Integer 'freqDelay'     : allow to choose change frequency (delay is in
442     *                            milliseconds)
443     *                            if value=0, there is no change
444     *  String 'selectMode'     : allows to choose picture to be randomly selected
445     *                              can take 'a' (all), 'f' (webmaster's favorites), 'c' (from categories)
446     *  Array  'selectCat'      : list of selected categories (choosing a cat implies to choose all sub cat)
447     *
448     * @param Array $config
449     * @return String : OK or KO
450     */
451    private function ajax_amm_admin_randomPictSetConfig($config)
452    {
453      $this->config['amm_randompicture_selectMode']=$config['selectMode'];
454      $this->config['amm_randompicture_selectCat']=$config['selectCat'];
455      $this->config['amm_randompicture_showname']=$config['infosName'];
456      $this->config['amm_randompicture_showcomment']=$config['infosComment'];
457      $this->config['amm_randompicture_periodicchange']=$config['freqDelay'];
458      $this->config['amm_randompicture_height']=($config['blockHeight']==99)?0:$config['blockHeight'];
459      $this->config['amm_randompicture_title']=array();
460      foreach($config['blockTitles'] as $title)
461      {
462        $this->config['amm_randompicture_title'][$title['id']]=base64_encode($title['value']);
463      }
464
465      $this->saveConfig();
466      return('OK!'.l10n('g002_config_saved'));
467    }
468
469
470
471    /*
472     * -------------------------------------------------------------------------
473     * personalised blocks
474     * -------------------------------------------------------------------------
475     */
476
477    /**
478     * return a html formatted list of blocks
479     */
480    private function ajax_amm_admin_blocksList()
481    {
482      global $template, $user;
483
484      $local_tpl = new Template(AMM_PATH."admin/", "");
485      $local_tpl->set_filename('body_page',
486                    dirname($this->getFileLocation()).'/admin/amm_personalised_detail.tpl');
487
488
489
490      $datas=array(
491        'blocks'=>array()
492      );
493
494      $blocks=$this->getPersonalisedBlocks(false, '', true);
495      foreach($blocks as $block)
496      {
497        $datas['blocks'][]=array(
498          'id' => $block['id'],
499          'nfo' => $block['nfo'],
500          'title' => $block['title'],
501          'visible' => l10n('g002_yesno_'.$block['visible'])
502        );
503      }
504
505      $local_tpl->assign('themeconf', $template->get_template_vars('themeconf'));
506      $local_tpl->assign('datas', $datas);
507      $local_tpl->assign('plugin', array('PATH' => AMM_PATH));
508
509      return($local_tpl->parse('body_page', true));
510    }
511
512    /**
513     * delete a block
514     *
515     * @param Integer $id : block id
516     * @return String : OK or KO
517     */
518    private function ajax_amm_admin_blocksDelete($id)
519    {
520      return($this->deletePersonalisedBlock($id)?'OK':'KO');
521    }
522
523    /**
524     * return block content as a json string
525     *
526     * @param Integer $id : block id
527     * @return String : json string
528     */
529    private function ajax_amm_admin_blocksGet($id)
530    {
531      return(json_encode($this->getPersonalisedBlock($id)));
532    }
533
534    /**
535     * set block values
536     * if id is empty, create a new block
537     *
538     * @param String $id      : block id
539     * @param String $visible : block visibility ('y' or 'n')
540     * @param String $nfo     : block description
541     * @param Array  $langs   : block langs, each record is an array
542     *                                  array('title' => '', 'content' => '')
543     * @return String : $id if OK, otherwise -1
544     */
545    private function ajax_amm_admin_blocksSet($id, $visible, $nfo, $lang)
546    {
547      return($this->setPersonalisedBlock($id, $visible, $nfo, $lang));
548    }
549
550    /*
551     * -------------------------------------------------------------------------
552     * core blocks
553     * -------------------------------------------------------------------------
554     */
555
556    /**
557     * set the menu config
558     *  - core blocks content
559     *  - blocks order&visibility
560     *
561     * @param Array $subMenus : for core blocks, sub menu items
562     *                          array(
563     *                            'subMenuId' => array(
564     *                                          'visibility' => (String),
565     *                                          'order' => (Integer),
566     *                                          'container' => (String),
567     *                                        )
568     *                          )
569     * @param Array $menus    : menu blocks order&visibility
570     *                          array(
571     *                            'block1' => array(
572     *                                'id'    => (String),
573     *                                'order' => (Integer),
574     *                                'users' => array(),
575     *                                'groups' => array()
576     *                            )
577     *                          )
578     * @return String : OK or KO
579     */
580    private function ajax_amm_admin_coreBlocksSetConfig($subMenu, $menus)
581    {
582      foreach($subMenu as $key=>$val)
583      {
584        if(!isset($subMenu[$key]['visibilityUser']) or $subMenu[$key]['visibilityUser']=='') $subMenu[$key]['visibilityUser']=array();
585        if(!isset($subMenu[$key]['visibilityGroup']) or $subMenu[$key]['visibilityGroup']=='') $subMenu[$key]['visibilityGroup']=array();
586
587        $subMenu[$key]['visibility']=implode(',', $subMenu[$key]['visibilityUser']).'/'.implode(',', $subMenu[$key]['visibilityGroup']);
588        unset($subMenu[$key]['visibilityUser']);
589        unset($subMenu[$key]['visibilityGroup']);
590      }
591      $this->config['amm_blocks_items']=$subMenu;
592      $this->saveConfig();
593
594
595      foreach($menus as $key=>$val)
596      {
597        if(!isset($menus[$key]['users']) or $menus[$key]['users']=='') $menus[$key]['users']=array();
598        if(!isset($menus[$key]['groups']) or $menus[$key]['groups']=='') $menus[$key]['groups']=array();
599      }
600      $this->setRegisteredBlocks($menus);
601
602      return('OK!'.l10n('g002_config_saved'));
603    }
604
605
606
607    /*
608     * -------------------------------------------------------------------------
609     * album to menu
610     * -------------------------------------------------------------------------
611     */
612
613
614    /**
615     * set the album to menu config
616     *
617     * $config is an array with keys :
618     *  Array  'selectCat'      : list of selected categories
619     *
620     * @param Array $config
621     * @return String : OK or KO
622     */
623    private function ajax_amm_admin_albumSetConfig($config)
624    {
625      $this->config['amm_albums_to_menu']=$config['selectCat'];
626
627      $this->saveConfig();
628      return('OK!'.l10n('g002_config_saved'));
629    }
630
631
632  } //class
633
634
635  $returned=new AMM_Ajax($prefixeTable, __FILE__);
636?>
637
Note: See TracBrowser for help on using the repository browser.