source: extensions/AMenuManager/amm_pip.class.inc.php @ 16445

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

feature:2642- compatibility with Piwigo 2.4

  • Property svn:executable set to *
File size: 13.8 KB
RevLine 
[3681]1<?php
2/* -----------------------------------------------------------------------------
3  Plugin     : Advanced Menu Manager
4  Author     : Grum
[3690]5    email    : grum@piwigo.org
[16445]6    website  : http://www.grum.fr
[3681]7
8    << May the Little SpaceFrog be with you ! >>
9  ------------------------------------------------------------------------------
10  See main.inc.php for release information
11
12  PIP classe => manage integration in public interface
13
14  --------------------------------------------------------------------------- */
15if (!defined('PHPWG_ROOT_PATH')) { die('Hacking attempt!'); }
16
17include_once(PHPWG_PLUGINS_PATH.'AMenuManager/amm_root.class.inc.php');
18
19class AMM_PIP extends AMM_root
20{
[4382]21  protected $displayRandomImageBlock=true;
[8962]22  protected $registeredBlocks;
23  protected $randomPictProp=null;
24  protected $users;
25  protected $groups;
26  protected $currentBuiltMenu=-1;
[3681]27
28  function AMM_PIP($prefixeTable, $filelocation)
29  {
30    parent::__construct($prefixeTable, $filelocation);
31
[8962]32    $this->users=new GPCUsers();
33    $this->groups=new GPCGroups();
34
[5545]35    $this->loadConfig();
36    $this->initEvents();
[3681]37  }
38
39
[8962]40  /**
41   * initialize events call for the plugin
42   */
[5545]43  public function initEvents()
[3681]44  {
[5545]45    parent::initEvents();
[8962]46
[16006]47    add_event_handler('blockmanager_register_blocks', array(&$this, 'registerBlocks') );
[8962]48    add_event_handler('blockmanager_prepare_display', array(&$this, 'blockmanagerSortBlocks') );
[12665]49    add_event_handler('blockmanager_apply', array(&$this, 'blockmanagerApply'), 45 );
[8962]50    add_event_handler('loc_end_page_header', array(&$this, 'applyJS'));
51    add_event_handler('get_categories_menu_sql_where', array(&$this, 'buildMenuFromCat'), 75);
[3681]52  }
53
[16006]54  public function loadCSS()
55  {
56    parent::loadCSS();
57    GPCCore::addHeaderCSS('amm_main', 'plugins/'.$this->getDirectory().'/'.$this->getPluginNameFiles()."2.css");
58  }
[8962]59
[16006]60
[8962]61  public function blockmanagerApply($menu_ref_arr)
[3681]62  {
[8962]63    $menu=&$menu_ref_arr[0];
[3681]64
[8962]65    $this->addBlockRandomPicture($menu);
66    $this->addBlockLinks($menu);
67    $this->addBlockPersonnal($menu);
68    $this->addBlockAlbum($menu);
69    $this->manageBlocksContent($menu);
70    $this->manageBlocks($menu);
[3681]71  }
72
[8962]73
74  /**
75   * Add a new random picture block
76   */
77  private function addBlockRandomPicture(&$menu)
[3681]78  {
[8962]79    global $user;
[3681]80
[8962]81    if((
82        ($block=$menu->get_block('mbAMM_randompict'))!=null) and
83        ($user['nb_total_images']>0) and
[11036]84        isset($this->config['amm_randompicture_title'][$user['language']]) and
85        $this->displayRandomImageBlock
[8962]86      )
87    {
[10247]88      GPCCore::addHeaderJS('jquery', 'themes/default/js/jquery.min.js');
[15366]89      GPCCore::addHeaderJS('amm.randomPictPublic', 'plugins/AMenuManager/js/amm_randomPictPublic.js', array('jquery'));
[3681]90
[8962]91      $block->set_title(base64_decode($this->config['amm_randompicture_title'][$user['language']]));
92      $block->template=dirname(__FILE__).'/menu_templates/menubar_randompic.tpl';
93
94      $this->randomPictProp = array(
95        'delay' => $this->config['amm_randompicture_periodicchange'],
96        'blockHeight' => $this->config['amm_randompicture_height'],
97        'showname' => $this->config['amm_randompicture_showname'],
98        'showcomment' => $this->config['amm_randompicture_showcomment'],
99        'pictures' => $this->getRandomPictures($this->config['amm_randompicture_preload'])
[3690]100      );
[8962]101
102      if(count($this->randomPictProp['pictures'])==0) $this->displayRandomImageBlock=false;
[3681]103    }
[4382]104    else
105    {
106      $this->displayRandomImageBlock=false;
107    }
[8962]108  }
[3681]109
[8962]110
111  /**
112   * Add a new block (links)
113   */
114  private function addBlockLinks(&$menu)
115  {
116    global $user;
117
118    $nbLink=0;
119
120    if(($block=$menu->get_block('mbAMM_links'))!=null &&
121       isset($this->config['amm_links_title'][$user['language']])
122      )
[3681]123    {
[8962]124      $urls=$this->getLinks(true);
125
126      if(count($urls)>0)
[3681]127      {
[8962]128        $userGroups=$this->getUserGroups($user['id']);;
129
130        foreach($urls as $key => $val)
131        {
132          $this->users->setAlloweds(explode(",", $val['accessUsers']), false);
133          $this->groups->setAlloweds(explode(",", $val['accessGroups']), false);
134
135          if(!$this->users->isAllowed($user['status']))
136          {
137            unset($urls[$key]);
138          }
139          else
140          {
141            $ok=true;
142            foreach($userGroups as $group)
143            {
144              if(!$this->groups->isAllowed($group)) $ok=false;
145            }
146            if(!$ok) unset($urls[$key]);
147          }
148        }
149
[5545]150        if($this->config['amm_links_show_icons']=='y')
[3681]151        {
[8962]152          foreach($urls as $key => $url)
[3681]153          {
[8962]154            $urls[$key]['icon']=get_root_url().'plugins/'.AMM_DIR."/links_pictures/".$url['icon'];
[3681]155          }
156        }
157
[8962]158        $block->set_title(base64_decode($this->config['amm_links_title'][$user['language']]));
159        $block->template=dirname(__FILE__).'/menu_templates/menubar_links.tpl';
[3681]160
161        $block->data = array(
162          'LINKS' => $urls,
[5545]163          'icons' => $this->config['amm_links_show_icons']
[3681]164        );
165      }
166    }
[8962]167  }
[3681]168
169
[8962]170  /**
171   * Add personnal blocks
172   */
173  private function addBlockPersonnal(&$menu)
174  {
175    $sections=$this->getPersonalisedBlocks(true);
176
[3681]177    if(count($sections))
178    {
[8962]179      $idDone=array();
[3681]180      foreach($sections as $key => $val)
181      {
[8962]182        if(!isset($idDone[$val['id']]))
[3681]183        {
[8962]184          if(($block=$menu->get_block('mbAMM_personalised'.$val['id']))!= null)
[3681]185          {
[8962]186            $block->set_title($val['title']);
[3681]187            $block->template = dirname(__FILE__).'/menu_templates/menubar_personalised.tpl';
188            $block->data = stripslashes($val['content']);
189          }
[8962]190          $idDone[$val['id']]="";
[3681]191        }
192      }
193    }
[8962]194  }
[3681]195
[4382]196
[8962]197
198
199  /**
200   * Add album to menu
201   */
202  private function addBlockAlbum(&$menu)
203  {
204    if(count($this->config['amm_albums_to_menu'])>0)
205    {
206      $sql="SELECT id, name, permalink, global_rank
207            FROM ".CATEGORIES_TABLE."
208            WHERE id IN(".implode(',', $this->config['amm_albums_to_menu']).");";
209
210      $result=pwg_query($sql);
211      if($result)
212      {
213        while($row=pwg_db_fetch_assoc($result))
214        {
215          $this->currentBuiltMenu=$row['id'];
216
217          $row['name']=trigger_event('render_category_name', $row['name'], 'amm_album_to_menu');
218
219          if(($block=$menu->get_block('mbAMM_album'.$row['id']))!= null)
220          {
221            $block->set_title($row['name']);
222            $block->template = dirname(__FILE__).'/menu_templates/menubar_album.tpl';
223            $block->data = array(
224              'album' => get_categories_menu(),
225              'name' => $row['name'],
226              'link' => make_index_url(array('category' => $row)),
227              'nbPictures' => ''
228            );
[12665]229/*
230            $nbImages=0;
231            foreach($block->data['album'] as $val)
232            {
233              $nbImages+=$val['nb_images'];
234            }
235            $block->data['nbPictures']="*** $nbImages";
236*/
[8962]237          }
238        }
239      }
240      $this->currentBuiltMenu=-1;
241    }
242  }
243
244  /**
245   * manage items from special & menu blocks
246   *  - reordering items
247   *  - grouping items
248   *  - managing rights to access
249   */
250  private function manageBlocksContent(&$menu)
251  {
252    global $user;
253
[4382]254    $blocks=Array();
255
[4395]256    if($menu->is_hidden('mbMenu'))
257    {
[8962]258      // if block is hidden, make a fake to manage AMM submenu features
[4395]259      // the fake block isn't displayed
260      $blocks['menu']=new DisplayBlock('amm_mbMenu');
261      $blocks['menu']->data=Array();
262    }
263    else
264    {
265      $blocks['menu']=$menu->get_block('mbMenu');
266    }
267
268    if($menu->is_hidden('mbSpecials'))
269    {
[8962]270      // if block is hidden, make a fake to manage AMM submenu features
[4395]271      // the fake block isn't displayed
272      $blocks['special']=new DisplayBlock('amm_mbSpecial');
273      $blocks['special']->data=Array();
274    }
275    else
276    {
277      $blocks['special']=$menu->get_block('mbSpecials');
278    }
279
[4382]280    $menuItems=array_merge($blocks['menu']->data, $blocks['special']->data);
[8962]281    $this->sortCoreBlocksItems();
[4382]282
283    $blocks['menu']->data=Array();
284    $blocks['special']->data=Array();
[8962]285    $userGroups=$this->getUserGroups($user['id']);
[4382]286
[8962]287    foreach($this->config['amm_blocks_items'] as $key => $val)
[3681]288    {
[4382]289      if(isset($menuItems[$key]))
[3681]290      {
[4389]291        $access=explode("/",$val['visibility']);
[8962]292        $this->users->setAlloweds(str_replace(",", "/", $access[0]), false);
293        $this->groups->setAlloweds(str_replace(",", "/", $access[1]), false);
[4389]294
[8962]295        /*
296         * test if user status is allowed to access the menu item
[4389]297         * if access is managed by group, the user have to be associated with an allowed group to access the menu item
[8962]298         */
299        if($this->users->isAllowed($user['status']))
[4389]300        {
[8962]301          $ok=true;
302          foreach($userGroups as $group)
303          {
304            if(!$this->groups->isAllowed($group)) $ok=false;
305          }
306          if($ok) $blocks[$val['container']]->data[$key]=$menuItems[$key];
[4389]307        }
[3681]308      }
309    }
[4382]310    if(count($blocks['menu']->data)==0) $menu->hide_block('mbMenu');
311    if(count($blocks['special']->data)==0) $menu->hide_block('mbSpecials');
312  }
[3681]313
[4389]314
[8962]315  /**
316   * return groups for a user
317   *
318   * @param String $userId
319   * @return Array
320   */
321  private function getUserGroups($userId)
[4389]322  {
[8962]323    global $user;
324
[4389]325    $returned=array();
[8962]326
327    $sql="SELECT group_id FROM ".USER_GROUP_TABLE." WHERE user_id='".$user['id']."';";
[4389]328    $result=pwg_query($sql);
329    if($result)
330    {
[5427]331      while($row=pwg_db_fetch_assoc($result))
[4389]332      {
[8962]333        $returned[]=$row['group_id'];
[4389]334      }
335    }
336    return($returned);
337  }
338
[8962]339
340  /**
341   * reordering blocks and manage access right
342   *
343   */
344  private function manageBlocks($menu)
[3681]345  {
[8962]346    $this->registeredBlocks=$this->getRegisteredBlocks(true);
[3681]347
[8962]348    foreach($menu->get_registered_blocks() as $key => $block)
[3681]349    {
[8962]350      if(!isset($this->registeredBlocks[$block->get_id()]))
[3681]351      {
[8962]352        $menu->hide_block($block->get_id());
[3681]353      }
354    }
[8962]355
[3681]356  }
357
358
[8962]359  /**
360   * sort menu blocks according to AMM rules (overriding piwigo's sort rules)
361   */
362  public function blockmanagerSortBlocks($blocks)
[3681]363  {
[8962]364    $this->registeredBlocks=$this->getRegisteredBlocks(true);
[3683]365
[11036]366    if(!isset($this->registeredBlocks['mbAMM_randompict'])) $this->displayRandomImageBlock=false;
367
[8962]368    foreach($blocks[0]->get_registered_blocks() as $key => $block)
369    {
370      if(isset($this->registeredBlocks[$block->get_id()]))
[3683]371      {
[8962]372        $blocks[0]->set_block_position($block->get_id(), $this->registeredBlocks[$block->get_id()]['order']);
[3683]373      }
[8962]374    }
375  }
[3681]376
377
378
[8962]379
380
381
382
383  /**
384   * return a list of thumbnails
385   * each array items is an array
386   *  'imageId'   => (integer)
387   *  'imageFile' => (String)
388   *  'comment'   => (String)
389   *  'path'      => (String)
390   *  'catId'     => (String)
391   *  'name'      => (String)
392   *  'permalink' => (String)
393   *  'imageName' => (String)
394   *
395   * @param Integer $number : number of returned images
396   * @return Array
397   */
398  private function getRandomPictures($num=25)
399  {
400    global $user;
401
402    $returned=array();
403
404    $sql=array();
405
[15366]406    $sql['select']="SELECT i.id as image_id, i.file as image_file, i.comment, i.path, c.id as catid, c.name, c.permalink, RAND() as rndvalue, i.name as imgname ";
[8962]407    $sql['from']="FROM ".CATEGORIES_TABLE." c, ".IMAGES_TABLE." i, ".IMAGE_CATEGORY_TABLE." ic ";
408    $sql['where']="WHERE c.id = ic.category_id
409            AND ic.image_id = i.id
410            AND i.level <= ".$user['level']." ";
411
412    if($user['forbidden_categories']!="")
413    {
414      $sql['where'].=" AND c.id NOT IN (".$user['forbidden_categories'].") ";
415    }
416
417    switch($this->config['amm_randompicture_selectMode'])
418    {
419      case 'f':
420        $sql['from'].=", ".USER_INFOS_TABLE." ui
421          LEFT JOIN ".FAVORITES_TABLE." f ON ui.user_id=f.user_id ";
422        $sql['where'].=" AND ui.status='webmaster'
423                         AND f.image_id = i.id ";
424        break;
425      case 'c':
426        $sql['where'].="AND (";
427        foreach($this->config['amm_randompicture_selectCat'] as $key => $val)
428        {
429          $sql['where'].=($key==0?'':' OR ')." FIND_IN_SET($val, c.uppercats) ";
430        }
431        $sql['where'].=") ";
432        break;
433    }
434
435    $sql=$sql['select'].$sql['from'].$sql['where']." ORDER BY rndvalue LIMIT 0,$num";
436
437
438    $result = pwg_query($sql);
439    if($result)
440    {
441      while($row=pwg_db_fetch_assoc($result))
[3681]442      {
[8962]443        $row['section']='category';
444        $row['category']=array(
445          'id' => $row['catid'],
446          'name' => $row['name'],
447          'permalink' => $row['permalink']
[3681]448        );
449
[8962]450        $row['link']=make_picture_url($row);
[15366]451        $row['thumb']=DerivativeImage::thumb_url(array('id'=>$row['image_id'], 'path'=>$row['path']));
[8962]452
453        $returned[]=$row;
[3681]454      }
[8962]455    }
[3681]456
[8962]457    return($returned);
[3681]458  }
459
[4363]460
[8962]461
462
[4363]463  public function applyJS()
464  {
[4499]465    global $user, $template, $page;
[4363]466
[5421]467    if(!array_key_exists('body_id', $page))
468    {
469      /*
470       * it seems the error message reported on mantis:1476 is displayed because
471       * the 'body_id' doesn't exist in the $page
472       *
473       * not abble to reproduce the error, but initializing the key to an empty
474       * value if it doesn't exist may be a sufficient solution
475       */
476      $page['body_id']="";
477    }
478
[8962]479
[4499]480    if($this->displayRandomImageBlock && $page['body_id'] == 'theCategoryPage')
[4363]481    {
482      $local_tpl = new Template(AMM_PATH."admin/", "");
[5545]483      $local_tpl->set_filename('body_page', dirname($this->getFileLocation()).'/menu_templates/menubar_randompic.js.tpl');
[4363]484
[8962]485      $local_tpl->assign('data', $this->randomPictProp);
[4363]486
[8962]487      $template->append('head_elements', $local_tpl->parse('body_page', true));
488    }
489  }
[4363]490
[8962]491
492
493  public function buildMenuFromCat($where)
494  {
495    global $user;
496
497    if($this->currentBuiltMenu>-1)
498    {
499      if($user['expand'])
500      {
501        $where=preg_replace('/id_uppercat\s+is\s+NULL/i', 'id_uppercat is NOT NULL', $where);
502      }
503      else
504      {
505        $where=preg_replace('/id_uppercat\s+is\s+NULL/i', 'id_uppercat is NULL OR id_uppercat IN ('.$this->currentBuiltMenu.')', $where);
506      }
507
508      $where.=" AND FIND_IN_SET(".$this->currentBuiltMenu.", uppercats) AND cat_id!=".$this->currentBuiltMenu." ";
[4363]509    }
510
[8962]511    return($where);
[4363]512  }
513
[3681]514} // AMM_PIP class
515
516
517?>
Note: See TracBrowser for help on using the repository browser.