source: extensions/AMenuManager/amm_root.class.inc.php @ 8962

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

release 3.0.0
fix bug:1716, bug:2128, bug:1296, bug:2052, bug:1910, bug:1723, bug:1709, bug:1680, bug:1477, bug:2129
rewrite plugin core, implement ajax interface, optimize JS

  • Property svn:executable set to *
File size: 18.5 KB
Line 
1<?php
2/* -----------------------------------------------------------------------------
3  Plugin     : Advanced Menu Manager
4  Author     : Grum
5    email    : grum@grum.fr
6    website  : http://photos.grum.fr
7    PWG user : http://forum.piwigo.org/profile.php?id=3706
8
9    << May the Little SpaceFrog be with you ! >>
10  ------------------------------------------------------------------------------
11  See main.inc.php for release information
12
13  AMM_root : root class for plugin
14
15  --------------------------------------------------------------------------- */
16
17if (!defined('PHPWG_ROOT_PATH')) { die('Hacking attempt!'); }
18
19include_once(PHPWG_ROOT_PATH.'include/block.class.php');
20include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/CommonPlugin.class.inc.php');
21include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCUsersGroups.class.inc.php');
22include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCCss.class.inc.php');
23
24
25class AMM_root extends CommonPlugin
26{
27  protected $css;   //the css object
28  protected $defaultMenus = array(
29    /* about visibility & accessibility system :
30     *  - by default, everything is visible (users & groups)
31     *  - items not visibles are listed (in release < 3.3.4, this rule wa applied to groups only)
32     *
33     * on the user interface, checked items are visibles => not checked items are stored
34     */
35    'favorites' => array('container' => 'special', 'visibility' => '/', 'order' => 0, 'translation' => 'My favorites'),
36    'most_visited' => array('container' => 'special', 'visibility' => '/', 'order' => 1, 'translation' => 'Most visited'),
37    'best_rated' => array('container' => 'special', 'visibility' => '/', 'order' => 2, 'translation' => 'Best rated'),
38    'random' => array('container' => 'special', 'visibility' => '/', 'order' => 3, 'translation' => 'Random pictures'),
39    'recent_pics' => array('container' => 'special', 'visibility' => '/', 'order' => 4, 'translation' => 'Recent pictures'),
40    'recent_cats' => array('container' => 'special', 'visibility' => '/', 'order' => 5, 'translation' => 'Recent categories'),
41    'calendar' => array('container' => 'special', 'visibility' => '/', 'order' => 6, 'translation' => 'Calendar'),
42    'qsearch' => array('container' => 'menu', 'visibility' => '/', 'order' => 0, 'translation' => 'Quick search'),
43    'tags' => array('container' => 'menu', 'visibility' => '/', 'order' => 1, 'translation' => 'Tags'),
44    'search' => array('container' => 'menu', 'visibility' => '/', 'order' => 2, 'translation' => 'Search'),
45    'comments' => array('container' => 'menu', 'visibility' => '/', 'order' => 3, 'translation' => 'Comments'),
46    'about' => array('container' => 'menu', 'visibility' => '/', 'order' => 4, 'translation' => 'About'),
47    'rss' => array('container' => 'menu', 'visibility' => '/', 'order' => 5, 'translation' => 'Notification')
48  );
49  protected $urlsModes=array(0 => 'new_window', 1 => 'current_window');
50
51
52
53  public function __construct($prefixeTable, $filelocation)
54  {
55    $this->setPluginName("Advanced Menu Manager");
56    $this->setPluginNameFiles("amm");
57    parent::__construct($prefixeTable, $filelocation);
58
59    $list=array('urls', 'personalised', 'personalised_langs', 'blocks');
60    $this->setTablesList($list);
61  }
62
63  public function __destruct()
64  {
65    unset($this->css);
66    unset($this->defaultMenus);
67    parent::__destruct();
68  }
69
70  /*
71   * ---------------------------------------------------------------------------
72   * common AIP & PIP functions
73   * ---------------------------------------------------------------------------
74   */
75
76  /**
77   * this function initialize config var with default values
78   */
79  public function initConfig()
80  {
81    $this->config=array(
82      'amm_links_show_icons' => 'y',
83      'amm_links_title' => array(),
84      'amm_randompicture_preload' => 25,     //number preloaded random pictures
85      'amm_randompicture_showname' => 'n',     //n:no, o:over, u:under
86      'amm_randompicture_showcomment' => 'n',   //n:no, o:over, u:under
87      'amm_randompicture_periodicchange' => 0,   //0: no periodic change ; periodic change in milliseconds
88      'amm_randompicture_height' => 0,           //0: automatic, otherwise it's the fixed height in pixels
89      'amm_randompicture_title' => array(),
90      'amm_randompicture_selectMode' => 'a',     // a:all, f:webmaster's favorites, c:categories
91      'amm_randompicture_selectCat' => array(),  // categories id list
92      'amm_blocks_items' => $this->defaultMenus,
93      'amm_albums_to_menu' => array(),
94      'amm_old_blk_menubar' => ''                // keep a copy of piwigo's menubar config
95    );
96
97    $languages=get_languages();
98    foreach($languages as $key => $val)
99    {
100      if($key=='fr_FR')
101      {
102        $this->config['amm_links_title'][$key]=base64_encode('Liens');
103        $this->config['amm_randompicture_title'][$key]=base64_encode('Une image au hasard');
104      }
105      else
106      {
107        $this->config['amm_links_title'][$key]=base64_encode('Links');
108        $this->config['amm_randompicture_title'][$key]=base64_encode('A random picture');
109      }
110    }
111  }
112
113  public function loadConfig()
114  {
115    parent::loadConfig();
116  }
117
118  public function initEvents()
119  {
120    add_event_handler('blockmanager_register_blocks', array(&$this, 'registerBlocks') );
121  }
122
123  public function registerBlocks( $menu_ref_arr )
124  {
125    $menu = & $menu_ref_arr[0];
126    if ($menu->get_id() != 'menubar') return;
127
128    $menu->register_block( new RegisteredBlock( 'mbAMM_randompict', 'Random pictures', 'AMM'));
129    $menu->register_block( new RegisteredBlock( 'mbAMM_links', 'Links', 'AMM'));
130
131    $blocks=$this->getPersonalisedBlocks();
132    if(count($blocks))
133    {
134      $idDone=array();
135      foreach($blocks as $key => $val)
136      {
137        if(!isset($idDone[$val['id']]))
138        {
139          $menu->register_block( new RegisteredBlock( 'mbAMM_personalised'.$val['id'], $val['title'], 'AMM'));
140          $idDone[$val['id']]="";
141        }
142      }
143    }
144
145    $this->loadConfig();
146
147    if(count($this->config['amm_albums_to_menu'])>0)
148    {
149      $sql="SELECT id, name
150            FROM ".CATEGORIES_TABLE."
151            WHERE id IN(".implode(',', $this->config['amm_albums_to_menu']).");";
152
153      $result=pwg_query($sql);
154      if($result)
155      {
156        while($row=pwg_db_fetch_assoc($result))
157        {
158          $row['name']=trigger_event('render_category_name', $row['name'], 'amm_album_to_menu');
159
160          $menu->register_block( new RegisteredBlock( 'mbAMM_album'.$row['id'], $row['name'].' ('.l10n('g002_album2menu').') ', 'AMM'));
161        }
162      }
163    }
164  }
165
166
167  /*
168   *  ---------------------------------------------------------------------------
169   *
170   * Links functions
171   *
172   * ---------------------------------------------------------------------------
173   */
174
175  /**
176   * return an array of links (each url is an array)
177   *
178   * @param Bool onlyVisible : if true, only visible links are returned
179   * @return Array
180   */
181  protected function getLinks($onlyVisible=false)
182  {
183    $returned=array();
184    $sql="SELECT id, label, url, mode, icon, position, visible, accessUsers, accessGroups
185          FROM ".$this->tables['urls'];
186    if($onlyVisible)
187    {
188      $sql.=" WHERE visible = 'y' ";
189    }
190    $sql.=" ORDER BY position ASC, id ASC";
191    $result=pwg_query($sql);
192    if($result)
193    {
194      while($row=pwg_db_fetch_assoc($result))
195      {
196        $returned[]=$row;
197      }
198    }
199    return($returned);
200  }
201
202  /**
203   * return values for a given link
204   *
205   * @param String $id : link id
206   * @return Array
207   */
208  protected function getLink($id)
209  {
210    $returned=array();
211    $sql="SELECT id, label, url, mode, icon, position, visible, accessUsers, accessGroups
212          FROM ".$this->tables['urls']."
213          WHERE id = '$id';";
214    $result=pwg_query($sql);
215    if($result)
216    {
217      while($row=pwg_db_fetch_assoc($result))
218      {
219        $returned=$row;
220      }
221    }
222    return($returned);
223  }
224
225  /**
226   * set values for a link
227   * if link id is empty : create a new link
228   * if not, update link
229   *
230   * @param String $id : link id
231   * @return Integer : -1 if fails
232   *                   otherwise link id
233   */
234  protected function setLink($id, $label, $url, $mode, $icon, $visible, $accessUsers, $accessGroups)
235  {
236    if($id=='')
237    {
238      $sql="INSERT INTO ".$this->tables['urls']." VALUES
239            ('',
240             '".pwg_db_real_escape_string($label)."',
241             '".pwg_db_real_escape_string($url)."',
242             '$mode',
243             '$icon',
244             0,
245             '$visible',
246             '$accessUsers',
247             '$accessGroups'
248            );";
249    }
250    else
251    {
252      $sql="UPDATE ".$this->tables['urls']."
253            SET label='".pwg_db_real_escape_string($label)."',
254                url='".pwg_db_real_escape_string($url)."',
255                mode='$mode',
256                icon='$icon',
257                visible='$visible',
258                accessUsers='".pwg_db_real_escape_string($accessUsers)."',
259                accessGroups='".pwg_db_real_escape_string($accessGroups)."'
260            WHERE id='$id';";
261    }
262    $result=pwg_query($sql);
263    if($result)
264    {
265      if($id=='')
266      {
267        return(pwg_db_insert_id());
268      }
269      else
270      {
271        return($id);
272      }
273    }
274    return(-1);
275  }
276
277  /**
278   * delete a given link
279   *
280   * @param String $id : link id
281   * @return Bool : true if deleted, otherwise false
282   */
283  protected function deleteLink($id)
284  {
285    $sql="DELETE FROM ".$this->tables['urls']."
286          WHERE id = '$id';";
287    $result=pwg_query($sql);
288    if($result) return(true);
289    return(false);
290  }
291
292
293
294  /**
295   * return number of links
296   *
297   * @param Bool onlyVisible : if true, only visible links are counted
298   * @return Array
299   */
300  protected function getLinksCount($onlyVisible=false)
301  {
302    $returned=0;
303    $sql="SELECT count(id) FROM ".$this->tables['urls'];
304    if($onlyVisible)
305    {
306      $sql.=" WHERE visible = 'y' ";
307    }
308    $result=pwg_query($sql);
309    if($result)
310    {
311      $tmp=pwg_db_fetch_row($result);
312      $returned=$tmp[0];
313    }
314    return($returned);
315  }
316
317  /**
318   * set order from given links
319   *
320   * @param Array $links : array
321   *                        each item is an array ('id' => '', 'order' =>'')
322   * @return Bool :
323   */
324  protected function setLinksOrder($links)
325  {
326    $returned=true;
327
328    foreach($links as $link)
329    {
330      $sql="UPDATE ".$this->tables['urls']."
331            SET position='".$link['order']."'
332            WHERE id='".$link['id']."';";
333      $result=pwg_query($sql);
334      if(!$result) $returned=false;
335    }
336    return($returned);
337  }
338
339
340  /*
341   *  ---------------------------------------------------------------------------
342   *
343   * Personalised Blocks functions
344   *
345   * ---------------------------------------------------------------------------
346   */
347
348
349  /**
350   * return an array of personalised blocks (each block is an array)
351   *
352   * @param Bool onlyVisible : if true, only visibles blocks are returned
353   * @return Array
354   */
355  protected function getPersonalisedBlocks($onlyVisible=false, $lang='', $emptyContent=false)
356  {
357    global $user;
358
359    if($lang=="") $lang=$user['language'];
360
361    $returned=array();
362    $sql="SELECT pt.id, pt.visible, pt.nfo, ptl.lang, ptl.title, ptl.content
363          FROM ".$this->tables['personalised']." pt
364            LEFT JOIN ".$this->tables['personalised_langs']." ptl
365            ON pt.id=ptl.id
366          WHERE (ptl.lang = '*' OR ptl.lang = '".$lang."') ";
367
368    if($onlyVisible) $sql.=" AND pt.visible = 'y' ";
369    if($emptyContent==false) $sql.=" AND ptl.content != '' ";
370
371    $sql.=" ORDER BY pt.id, ptl.lang ASC ";
372
373    $result=pwg_query($sql);
374    if($result)
375    {
376      while($row=pwg_db_fetch_assoc($result))
377      {
378        $returned[]=$row;
379      }
380    }
381    return($returned);
382  }
383
384  /**
385   * return values for a given personalised block
386   *
387   * @param String $id : link id
388   * @return Array
389   */
390  protected function getPersonalisedBlock($id)
391  {
392    $returned=array(
393      'visible' => false,
394      'nfo' => '',
395      'langs' => array()
396    );
397
398    $sql="SELECT visible, nfo
399          FROM ".$this->tables['personalised']."
400          WHERE id='$id';";
401
402    $result=pwg_query($sql);
403    if($result)
404    {
405      while($row=pwg_db_fetch_assoc($result))
406      {
407        $returned['visible']=$row['visible'];
408        $returned['nfo']=$row['nfo'];
409      }
410
411      $sql="SELECT lang, title, content
412            FROM ".$this->tables['personalised_langs']."
413            WHERE id='$id'
414            ORDER BY lang ASC;";
415
416      $result=pwg_query($sql);
417      if($result)
418      {
419        while($row=pwg_db_fetch_assoc($result))
420        {
421          $returned['langs'][$row['lang']]=$row;
422        }
423      }
424    }
425    return($returned);
426  }
427
428  /**
429   * set values for a personalised block
430   * if block id is empty : create a new block
431   * if not, update block
432   *
433   * @param String $id : block id
434   * @return Integer : -1 if fails
435   *                   otherwise block id
436   */
437  protected function setPersonalisedBlock($id, $visible, $nfo, $langs)
438  {
439    $ok=false;
440
441    if($id=='')
442    {
443      $sql="INSERT INTO ".$this->tables['personalised']." VALUES
444            ('',
445             '$visible',
446             '".pwg_db_real_escape_string($nfo)."'
447            );";
448      $result=pwg_query($sql);
449      if($result) $ok=true;
450      $id=pwg_db_insert_id();
451    }
452    else
453    {
454      $sql="UPDATE ".$this->tables['personalised']."
455            SET visible='$visible',
456                nfo='".pwg_db_real_escape_string($nfo)."'
457            WHERE id='$id';";
458      $result=pwg_query($sql);
459      if($result)
460      {
461        $sql="DELETE FROM ".$this->tables['personalised_langs']."
462              WHERE id='$id';";
463        $result=pwg_query($sql);
464
465        if($result) $ok=true;
466      }
467    }
468
469    if($ok)
470    {
471      $values=array();
472      foreach($langs as $key => $lang)
473      {
474        $values[]="('$id',
475                    '".$lang['lang']."',
476                    '".pwg_db_real_escape_string($lang['title'])."',
477                    '".pwg_db_real_escape_string($lang['content'])."')";
478      }
479      $sql="INSERT INTO ".$this->tables['personalised_langs']." VALUES ".implode(',', $values);
480      $result=pwg_query($sql);
481
482      if($result) $ok=true;
483    }
484
485    if($ok) return($id);
486    return(-1);
487  }
488
489  /**
490   * delete a given personalised block
491   *
492   * @param String $id : block id
493   * @return Bool : true if deleted, otherwise false
494   */
495  protected function deletePersonalisedBlock($id)
496  {
497    $sql="DELETE FROM ".$this->tables['personalised']."
498          WHERE id = '$id';";
499    $result=pwg_query($sql);
500    if($result)
501    {
502      $sql="DELETE FROM ".$this->tables['personalised_langs']."
503            WHERE id = '$id';";
504      $result=pwg_query($sql);
505
506      if($result) return(true);
507    }
508
509    return(false);
510  }
511
512
513
514  /**
515   * return an array of all registered blocks
516   * each array item is an array :
517   *  String  'id'      => ''
518   *  Integer 'order'   => 0
519   *  Array   'users'   => array()
520   *  Array   'groups'  => array()
521   *  String  'name'    => ''
522   *  String  'owner'   => ''
523   *
524   * @param Bool $userFiltered : true if returned blocks are filtered to current
525   *                             user
526   * @return Array
527   */
528  protected function getRegisteredBlocks($userFiltered=false)
529  {
530    global $conf, $user;
531
532    $returned=array();
533    $order=0;
534    $users=new GPCUsers();
535    $groups=new GPCGroups();
536
537    $menu = new BlockManager('menubar');
538    $menu->load_registered_blocks();
539    $registeredBlocks = $menu->get_registered_blocks();
540
541    $sql="SELECT id, `order`, users, groups
542          FROM ".$this->tables['blocks']."
543          ORDER BY `order`;";
544    $result=pwg_query($sql);
545    if($result)
546    {
547      while($row=pwg_db_fetch_assoc($result))
548      {
549        $row['users']=explode(',', $row['users']);
550        $row['groups']=explode(',', $row['groups']);
551
552        if(isset($registeredBlocks[$row['id']]))
553        {
554          $ok=true;
555          if($userFiltered)
556          {
557            $users->setAlloweds($row['users'], false);
558            if($users->isAllowed($user['status']))
559            {
560              $groups->setAlloweds($row['groups'], false);
561              foreach($row['groups'] as $val)
562              {
563                if(!$groups->isAllowed($val)) $ok=false;
564              }
565            }
566            else
567            {
568              $ok=false;
569            }
570          }
571
572          if($ok)
573          {
574            $returned[$row['id']]=array(
575              'id'    => $row['id'],
576              'order' => $row['order'],
577              'users' => $row['users'],
578              'groups'=> $row['groups'],
579              'name'  => $registeredBlocks[$row['id']]->get_name(),
580              'owner' => $registeredBlocks[$row['id']]->get_owner()
581            );
582            $order=$row['order'];
583          }
584          unset($registeredBlocks[$row['id']]);
585        }
586      }
587    }
588
589    /*
590     * add new blocks, unknown from plugin
591     * by default, users & groups are visibles
592     */
593    foreach($registeredBlocks as $key=>$val)
594    {
595      $order+=10;
596
597      $returned[$key]=array(
598        'id'    => $key,
599        'order' => $order,
600        'users' => array(),
601        'groups'=> array(),
602        'name'  => $val->get_name(),
603        'owner' => $val->get_owner()
604      );
605    }
606
607    return($returned);
608  }
609
610
611  /**
612   * set order for registered blocks
613   *
614   * note : Piwigo's order is maintened
615   *
616   * @param Array $block : array of block ; each items is an array
617   *                        String  'id'    => ''
618   *                        Integer 'order' => ''
619   *                        Array   'users'   => array()
620   *                        Array   'groups'  => array()
621   * @return Bool : true, false is something is wrong
622   */
623  protected function setRegisteredBlocks($blocks)
624  {
625    $returned=true;
626
627    $sql="DELETE FROM ".$this->tables['blocks'];
628    pwg_query($sql);
629
630    foreach($blocks as $block)
631    {
632      $sql="INSERT INTO ".$this->tables['blocks']." VALUES (
633            '".pwg_db_real_escape_string($block['id'])."',
634            '".$block['order']."',
635            '".pwg_db_real_escape_string(implode(',', $block['users']))."',
636            '".pwg_db_real_escape_string(implode(',', $block['groups']))."'
637            );";
638      $result=pwg_query($sql);
639      if(!$result) $returned=false;
640    }
641
642    return($returned);
643  }
644
645
646
647
648
649
650
651  protected function sortCoreBlocksItemsCompare($a, $b)
652  {
653    if($a['container']==$b['container'])
654    {
655      if($a['order']==$b['order']) return(0);
656      return(($a['order']<$b['order'])?-1:1);
657    }
658    else return(($a['container']<$b['container'])?-1:1);
659  }
660
661  protected function sortCoreBlocksItems()
662  {
663    uasort($this->config['amm_blocks_items'], array($this, "sortCoreBlocksItemsCompare"));
664  }
665
666} // amm_root  class
667
668
669
670?>
Note: See TracBrowser for help on using the repository browser.