source: extensions/MenuRandomPhoto/main.inc.php @ 31403

Last change on this file since 31403 was 30080, checked in by JanisV, 10 years ago

Fixed cache

  • Property svn:executable set to *
File size: 6.3 KB
Line 
1<?php
2/*
3Plugin Name: Menu Random Photo
4Version: auto
5Description: Adds a random picture block into menu
6Plugin URI: auto
7Author: JanisV
8*/
9
10global $conf;
11
12if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
13
14define('MRP_ID',    basename(dirname(__FILE__)));
15define('MRP_PATH' , PHPWG_PLUGINS_PATH . MRP_ID . '/');
16define('MRP_ADMIN', get_root_url() . 'admin.php?page=plugin-' . MRP_ID);
17
18add_event_handler('init', 'MRP_init');
19add_event_handler('blockmanager_register_blocks', 'MRP_register_menubar_blocks', EVENT_HANDLER_PRIORITY_NEUTRAL-1);
20
21if (defined('IN_ADMIN'))
22{
23  add_event_handler('get_admin_plugin_menu_links', 'MRP_admin_menu');
24}
25else
26{
27  add_event_handler('blockmanager_apply', 'MRP_blockmanager_apply');
28  add_event_handler('loc_end_page_header', 'MRP_end_page_header');
29}
30
31function MRP_init()
32{
33  global $conf, $user;
34
35  load_language('plugin.lang', MRP_PATH);
36
37  $conf['MRP'] = unserialize($conf['MRP']);
38}
39
40function MRP_register_menubar_blocks($menu_ref_arr)
41{
42  $menu = & $menu_ref_arr[0];
43  if ($menu->get_id() != 'menubar')
44    return;
45  $menu->register_block( new RegisteredBlock('mbRandomPhoto', 'Menu Random Photo', 'MRP'));
46}
47
48function MRP_blockmanager_apply($menu_ref_arr)
49{
50  global $user, $conf;
51
52  $menu = & $menu_ref_arr[0];
53 
54  if((
55      ($block=$menu->get_block('mbRandomPhoto'))!=null) and
56      ($user['nb_total_images']>0)
57    )
58  {
59    $block->set_title($conf['MRP']['title']);
60    $block->template=realpath(MRP_PATH.'template/menu_random_photo.tpl');
61  }
62}
63
64  function MRP_end_page_header()
65  {
66    global $user, $template, $page, $conf;
67
68    if(!array_key_exists('body_id', $page))
69    {
70      /*
71       * it seems the error message reported on mantis:1476 is displayed because
72       * the 'body_id' doesn't exist in the $page
73       *
74       * not abble to reproduce the error, but initializing the key to an empty
75       * value if it doesn't exist may be a sufficient solution
76       */
77      $page['body_id']="";
78    }
79
80    if($page['body_id'] == 'theCategoryPage')
81    {
82      $randomPictProp = array(
83        'delay' => $conf['MRP']['delay'],
84        'blockHeight' => $conf['MRP']['height'],
85//        'showname' => $this->config['amm_randompicture_showname'],
86//        'showcomment' => $this->config['amm_randompicture_showcomment'],
87        'pictures' => getRandomPictures($conf['MRP']['randompicture_preload']),
88      );
89
90      if (count($randomPictProp['pictures']) > 0)
91      {
92        $local_tpl = new Template(MRP_PATH.'/template', "");
93        $local_tpl->set_filename('body_page', realpath(MRP_PATH.'/template/menu_random_photo.js.tpl'));
94
95        $local_tpl->assign('data', $randomPictProp);
96
97        $template->append('head_elements', $local_tpl->parse('body_page', true));
98      }
99    }
100  }
101
102  /**
103   * return a list of thumbnails
104   * each array items is an array
105   *  'imageId'   => (integer)
106   *  'imageFile' => (String)
107   *  'comment'   => (String)
108   *  'path'      => (String)
109   *  'catId'     => (String)
110   *  'name'      => (String)
111   *  'permalink' => (String)
112   *  'imageName' => (String)
113   *
114   * @param Integer $number : number of returned images
115   * @return Array
116   */
117  function getRandomPictures($num=25)
118  {
119    global $user, $conf;
120
121    $returned=array();
122
123    if (preg_match('/(Googlebot|bingbot|Baiduspider|yandex|AhrefsBot|msnbot|NCollector)/', $_SERVER["HTTP_USER_AGENT"]))
124    {
125      return($returned);
126    }
127
128    $sql=array();
129
130    // because ORDER BY RAND() can be very slow on a big database, let's
131    // make a first query with no join and by security take 5 times
132    // $num. We keep the result in session for 5 minutes.
133    if (!isset($_SESSION['mrp_random_pics'])
134        or !isset($_SESSION['mrp_random_pics_generated_on'])
135        or $_SESSION['mrp_random_pics_generated_on'] < time() - 5*60) // 5 minutes ago
136    {
137      $query = '
138SELECT id
139  FROM '.IMAGES_TABLE.'
140  WHERE level <= '.$user['level'].'
141  ORDER BY RAND() LIMIT '.($num*5).'
142;';
143      $_SESSION['mrp_random_pics'] = query2array($query, null, 'id');
144      $_SESSION['mrp_random_pics_generated_on'] = time();
145    }
146   
147    $sql['select'] = '
148SELECT
149    i.id as image_id,
150    i.file as image_file,
151    i.comment,
152    i.path,
153    c.id as catid,
154    c.name,
155    c.permalink,
156    i.name as imgname
157';
158   
159    $sql['from'] = '
160  FROM '.CATEGORIES_TABLE.' c
161    JOIN '.IMAGE_CATEGORY_TABLE.' ic ON ic.category_id = c.id
162    JOIN '.IMAGES_TABLE.' i ON i.id = ic.image_id
163';
164   
165    $sql['where'] = '
166  WHERE i.id IN ('.implode(',', $_SESSION['mrp_random_pics']).')
167    AND i.level <= '.$user['level'].'
168';
169
170    if($user['forbidden_categories']!="")
171    {
172      $sql['where'].=" AND c.id NOT IN (".$user['forbidden_categories'].") ";
173    }
174/*
175    switch($this->config['amm_randompicture_selectMode'])
176    {
177      case 'f':
178        $sql['from'].=", ".USER_INFOS_TABLE." ui
179          LEFT JOIN ".FAVORITES_TABLE." f ON ui.user_id=f.user_id ";
180        $sql['where'].=" AND ui.status='webmaster'
181                         AND f.image_id = i.id ";
182        break;
183      case 'c':
184        $sql['where'].="AND (";
185        foreach($this->config['amm_randompicture_selectCat'] as $key => $val)
186        {
187          $sql['where'].=($key==0?'':' OR ')." FIND_IN_SET($val, c.uppercats) ";
188        }
189        $sql['where'].=") ";
190        break;
191    }
192*/
193    $sql = $sql['select'].$sql['from'].$sql['where']." ORDER BY RAND() LIMIT $num;";
194
195    $result = pwg_query($sql);
196    if($result)
197    {
198      if ($conf['MRP']['square'])
199      {
200        $height = $conf['MRP']['height'];
201        if($height == 0)
202          $derivative_params = ImageStdParams::get_by_type(IMG_SQUARE);
203        else
204          $derivative_params = ImageStdParams::get_custom($height, $height, 1, $height, $height);
205      }
206      else
207      {
208        $derivative_params = IMG_THUMB;
209      }
210
211      while($row=pwg_db_fetch_assoc($result))
212      {
213        $row['section']='categories';
214        $row['category']=array(
215          'id' => $row['catid'],
216          'name' => $row['name'],
217          'permalink' => $row['permalink']
218        );
219
220        $row['link']=make_picture_url($row);
221        $infos = array('id'=>$row['image_id'], 'path'=>$row['path']);
222        $row['thumb']=DerivativeImage::url($derivative_params, $infos);
223
224        $returned[]=$row;
225      }
226    }
227
228    return($returned);
229  }
230
231function MRP_admin_menu($menu)
232{
233  $menu[] = array(
234    'NAME' => 'Menu Random Photo',
235    'URL'  => MRP_ADMIN,
236  );
237
238  return $menu;
239}
240
241?>
Note: See TracBrowser for help on using the repository browser.