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
Line 
1<?php
2/* -----------------------------------------------------------------------------
3  Plugin     : Advanced Menu Manager
4  Author     : Grum
5    email    : grum@piwigo.org
6    website  : http://www.grum.fr
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{
21  protected $displayRandomImageBlock=true;
22  protected $registeredBlocks;
23  protected $randomPictProp=null;
24  protected $users;
25  protected $groups;
26  protected $currentBuiltMenu=-1;
27
28  function AMM_PIP($prefixeTable, $filelocation)
29  {
30    parent::__construct($prefixeTable, $filelocation);
31
32    $this->users=new GPCUsers();
33    $this->groups=new GPCGroups();
34
35    $this->loadConfig();
36    $this->initEvents();
37  }
38
39
40  /**
41   * initialize events call for the plugin
42   */
43  public function initEvents()
44  {
45    parent::initEvents();
46
47    add_event_handler('blockmanager_register_blocks', array(&$this, 'registerBlocks') );
48    add_event_handler('blockmanager_prepare_display', array(&$this, 'blockmanagerSortBlocks') );
49    add_event_handler('blockmanager_apply', array(&$this, 'blockmanagerApply'), 45 );
50    add_event_handler('loc_end_page_header', array(&$this, 'applyJS'));
51    add_event_handler('get_categories_menu_sql_where', array(&$this, 'buildMenuFromCat'), 75);
52  }
53
54  public function loadCSS()
55  {
56    parent::loadCSS();
57    GPCCore::addHeaderCSS('amm_main', 'plugins/'.$this->getDirectory().'/'.$this->getPluginNameFiles()."2.css");
58  }
59
60
61  public function blockmanagerApply($menu_ref_arr)
62  {
63    $menu=&$menu_ref_arr[0];
64
65    $this->addBlockRandomPicture($menu);
66    $this->addBlockLinks($menu);
67    $this->addBlockPersonnal($menu);
68    $this->addBlockAlbum($menu);
69    $this->manageBlocksContent($menu);
70    $this->manageBlocks($menu);
71  }
72
73
74  /**
75   * Add a new random picture block
76   */
77  private function addBlockRandomPicture(&$menu)
78  {
79    global $user;
80
81    if((
82        ($block=$menu->get_block('mbAMM_randompict'))!=null) and
83        ($user['nb_total_images']>0) and
84        isset($this->config['amm_randompicture_title'][$user['language']]) and
85        $this->displayRandomImageBlock
86      )
87    {
88      GPCCore::addHeaderJS('jquery', 'themes/default/js/jquery.min.js');
89      GPCCore::addHeaderJS('amm.randomPictPublic', 'plugins/AMenuManager/js/amm_randomPictPublic.js', array('jquery'));
90
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'])
100      );
101
102      if(count($this->randomPictProp['pictures'])==0) $this->displayRandomImageBlock=false;
103    }
104    else
105    {
106      $this->displayRandomImageBlock=false;
107    }
108  }
109
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      )
123    {
124      $urls=$this->getLinks(true);
125
126      if(count($urls)>0)
127      {
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
150        if($this->config['amm_links_show_icons']=='y')
151        {
152          foreach($urls as $key => $url)
153          {
154            $urls[$key]['icon']=get_root_url().'plugins/'.AMM_DIR."/links_pictures/".$url['icon'];
155          }
156        }
157
158        $block->set_title(base64_decode($this->config['amm_links_title'][$user['language']]));
159        $block->template=dirname(__FILE__).'/menu_templates/menubar_links.tpl';
160
161        $block->data = array(
162          'LINKS' => $urls,
163          'icons' => $this->config['amm_links_show_icons']
164        );
165      }
166    }
167  }
168
169
170  /**
171   * Add personnal blocks
172   */
173  private function addBlockPersonnal(&$menu)
174  {
175    $sections=$this->getPersonalisedBlocks(true);
176
177    if(count($sections))
178    {
179      $idDone=array();
180      foreach($sections as $key => $val)
181      {
182        if(!isset($idDone[$val['id']]))
183        {
184          if(($block=$menu->get_block('mbAMM_personalised'.$val['id']))!= null)
185          {
186            $block->set_title($val['title']);
187            $block->template = dirname(__FILE__).'/menu_templates/menubar_personalised.tpl';
188            $block->data = stripslashes($val['content']);
189          }
190          $idDone[$val['id']]="";
191        }
192      }
193    }
194  }
195
196
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            );
229/*
230            $nbImages=0;
231            foreach($block->data['album'] as $val)
232            {
233              $nbImages+=$val['nb_images'];
234            }
235            $block->data['nbPictures']="*** $nbImages";
236*/
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
254    $blocks=Array();
255
256    if($menu->is_hidden('mbMenu'))
257    {
258      // if block is hidden, make a fake to manage AMM submenu features
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    {
270      // if block is hidden, make a fake to manage AMM submenu features
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
280    $menuItems=array_merge($blocks['menu']->data, $blocks['special']->data);
281    $this->sortCoreBlocksItems();
282
283    $blocks['menu']->data=Array();
284    $blocks['special']->data=Array();
285    $userGroups=$this->getUserGroups($user['id']);
286
287    foreach($this->config['amm_blocks_items'] as $key => $val)
288    {
289      if(isset($menuItems[$key]))
290      {
291        $access=explode("/",$val['visibility']);
292        $this->users->setAlloweds(str_replace(",", "/", $access[0]), false);
293        $this->groups->setAlloweds(str_replace(",", "/", $access[1]), false);
294
295        /*
296         * test if user status is allowed to access the menu item
297         * if access is managed by group, the user have to be associated with an allowed group to access the menu item
298         */
299        if($this->users->isAllowed($user['status']))
300        {
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];
307        }
308      }
309    }
310    if(count($blocks['menu']->data)==0) $menu->hide_block('mbMenu');
311    if(count($blocks['special']->data)==0) $menu->hide_block('mbSpecials');
312  }
313
314
315  /**
316   * return groups for a user
317   *
318   * @param String $userId
319   * @return Array
320   */
321  private function getUserGroups($userId)
322  {
323    global $user;
324
325    $returned=array();
326
327    $sql="SELECT group_id FROM ".USER_GROUP_TABLE." WHERE user_id='".$user['id']."';";
328    $result=pwg_query($sql);
329    if($result)
330    {
331      while($row=pwg_db_fetch_assoc($result))
332      {
333        $returned[]=$row['group_id'];
334      }
335    }
336    return($returned);
337  }
338
339
340  /**
341   * reordering blocks and manage access right
342   *
343   */
344  private function manageBlocks($menu)
345  {
346    $this->registeredBlocks=$this->getRegisteredBlocks(true);
347
348    foreach($menu->get_registered_blocks() as $key => $block)
349    {
350      if(!isset($this->registeredBlocks[$block->get_id()]))
351      {
352        $menu->hide_block($block->get_id());
353      }
354    }
355
356  }
357
358
359  /**
360   * sort menu blocks according to AMM rules (overriding piwigo's sort rules)
361   */
362  public function blockmanagerSortBlocks($blocks)
363  {
364    $this->registeredBlocks=$this->getRegisteredBlocks(true);
365
366    if(!isset($this->registeredBlocks['mbAMM_randompict'])) $this->displayRandomImageBlock=false;
367
368    foreach($blocks[0]->get_registered_blocks() as $key => $block)
369    {
370      if(isset($this->registeredBlocks[$block->get_id()]))
371      {
372        $blocks[0]->set_block_position($block->get_id(), $this->registeredBlocks[$block->get_id()]['order']);
373      }
374    }
375  }
376
377
378
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
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 ";
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))
442      {
443        $row['section']='category';
444        $row['category']=array(
445          'id' => $row['catid'],
446          'name' => $row['name'],
447          'permalink' => $row['permalink']
448        );
449
450        $row['link']=make_picture_url($row);
451        $row['thumb']=DerivativeImage::thumb_url(array('id'=>$row['image_id'], 'path'=>$row['path']));
452
453        $returned[]=$row;
454      }
455    }
456
457    return($returned);
458  }
459
460
461
462
463  public function applyJS()
464  {
465    global $user, $template, $page;
466
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
479
480    if($this->displayRandomImageBlock && $page['body_id'] == 'theCategoryPage')
481    {
482      $local_tpl = new Template(AMM_PATH."admin/", "");
483      $local_tpl->set_filename('body_page', dirname($this->getFileLocation()).'/menu_templates/menubar_randompic.js.tpl');
484
485      $local_tpl->assign('data', $this->randomPictProp);
486
487      $template->append('head_elements', $local_tpl->parse('body_page', true));
488    }
489  }
490
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." ";
509    }
510
511    return($where);
512  }
513
514} // AMM_PIP class
515
516
517?>
Note: See TracBrowser for help on using the repository browser.