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

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

feature:2642- compatibility with Piwigo 2.4

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