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

Last change on this file since 28263 was 28241, checked in by plg, 11 years ago

strong speed performance improvement on Random Picture for big galleries (10k+ photos), use 5-minute buffer as cache

  • Property svn:executable set to *
File size: 14.7 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    if (preg_match('/(Googlebot|bingbot|Baiduspider|yandex|AhrefsBot|msnbot|NCollector)/', $_SERVER["HTTP_USER_AGENT"]))
405    {
406      return($returned);
407    }
408
409    $sql=array();
410
411    // because ORDER BY RAND() can be very slow on a big database, let's
412    // make a first query with no join and by security take 5 times
413    // $num. We keep the result in session for 5 minutes.
414    if (!isset($_SESSION['amm_random_pics'])
415        or !isset($_SESSION['amm_random_pics_generated_on'])
416        or $_SESSION['amm_random_pics_generated_on'] < time() - 5*60) // 5 minutes ago
417    {
418      $query = '
419SELECT id
420  FROM '.IMAGES_TABLE.'
421  WHERE level <= '.$user['level'].'
422  ORDER BY RAND() LIMIT '.($num*5).'
423;';
424      $_SESSION['amm_random_pics'] = query2array($query, null, 'id');
425      $_SESSION['amm_random_pics_generated_on'] = time();
426    }
427   
428    $sql['select'] = '
429SELECT
430    i.id as image_id,
431    i.file as image_file,
432    i.comment,
433    i.path,
434    c.id as catid,
435    c.name,
436    c.permalink,
437    i.name as imgname
438';
439   
440    $sql['from'] = '
441  FROM '.CATEGORIES_TABLE.' c
442    JOIN '.IMAGE_CATEGORY_TABLE.' ic ON ic.category_id = c.id
443    JOIN '.IMAGES_TABLE.' i ON i.id = ic.image_id
444';
445   
446    $sql['where'] = '
447  WHERE i.id IN ('.implode(',', $_SESSION['amm_random_pics']).')
448    AND i.level <= '.$user['level'].'
449';
450
451    if($user['forbidden_categories']!="")
452    {
453      $sql['where'].=" AND c.id NOT IN (".$user['forbidden_categories'].") ";
454    }
455
456    switch($this->config['amm_randompicture_selectMode'])
457    {
458      case 'f':
459        $sql['from'].=", ".USER_INFOS_TABLE." ui
460          LEFT JOIN ".FAVORITES_TABLE." f ON ui.user_id=f.user_id ";
461        $sql['where'].=" AND ui.status='webmaster'
462                         AND f.image_id = i.id ";
463        break;
464      case 'c':
465        $sql['where'].="AND (";
466        foreach($this->config['amm_randompicture_selectCat'] as $key => $val)
467        {
468          $sql['where'].=($key==0?'':' OR ')." FIND_IN_SET($val, c.uppercats) ";
469        }
470        $sql['where'].=") ";
471        break;
472    }
473
474    $sql = $sql['select'].$sql['from'].$sql['where']." ORDER BY RAND() LIMIT $num;";
475
476    $result = pwg_query($sql);
477    if($result)
478    {
479      while($row=pwg_db_fetch_assoc($result))
480      {
481        $row['section']='category';
482        $row['category']=array(
483          'id' => $row['catid'],
484          'name' => $row['name'],
485          'permalink' => $row['permalink']
486        );
487
488        $row['link']=make_picture_url($row);
489        $row['thumb']=DerivativeImage::thumb_url(array('id'=>$row['image_id'], 'path'=>$row['path']));
490
491        $returned[]=$row;
492      }
493    }
494
495    return($returned);
496  }
497
498
499
500
501  public function applyJS()
502  {
503    global $user, $template, $page;
504
505    if(!array_key_exists('body_id', $page))
506    {
507      /*
508       * it seems the error message reported on mantis:1476 is displayed because
509       * the 'body_id' doesn't exist in the $page
510       *
511       * not abble to reproduce the error, but initializing the key to an empty
512       * value if it doesn't exist may be a sufficient solution
513       */
514      $page['body_id']="";
515    }
516
517
518    if($this->displayRandomImageBlock && $page['body_id'] == 'theCategoryPage')
519    {
520      $local_tpl = new Template(AMM_PATH."admin/", "");
521      $local_tpl->set_filename('body_page', dirname($this->getFileLocation()).'/menu_templates/menubar_randompic.js.tpl');
522
523      $local_tpl->assign('data', $this->randomPictProp);
524
525      $template->append('head_elements', $local_tpl->parse('body_page', true));
526    }
527  }
528
529
530
531  public function buildMenuFromCat($where)
532  {
533    global $user;
534
535    if($this->currentBuiltMenu>-1)
536    {
537      if($user['expand'])
538      {
539        $where=preg_replace('/id_uppercat\s+is\s+NULL/i', 'id_uppercat is NOT NULL', $where);
540      }
541      else
542      {
543        $where=preg_replace('/id_uppercat\s+is\s+NULL/i', 'id_uppercat is NULL OR id_uppercat IN ('.$this->currentBuiltMenu.')', $where);
544      }
545
546      $where.=" AND FIND_IN_SET(".$this->currentBuiltMenu.", uppercats) AND cat_id!=".$this->currentBuiltMenu." ";
547    }
548
549    return($where);
550  }
551
552} // AMM_PIP class
553
554
555?>
Note: See TracBrowser for help on using the repository browser.