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

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

release 3.0.0
fix bug on install/update process + fix bug on image random

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