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 | --------------------------------------------------------------------------- */ |
---|
15 | if (!defined('PHPWG_ROOT_PATH')) { die('Hacking attempt!'); } |
---|
16 | |
---|
17 | include_once(PHPWG_PLUGINS_PATH.'AMenuManager/amm_root.class.inc.php'); |
---|
18 | |
---|
19 | class 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_change('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, $persistent_cache, $conf; |
---|
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 | $cache_key = $persistent_cache->make_key( |
---|
410 | array( |
---|
411 | 'amm_random_pics', |
---|
412 | $conf['order_by'], |
---|
413 | $user['id'], |
---|
414 | $user['cache_update_time'], |
---|
415 | $this->config['amm_randompicture_selectMode'] |
---|
416 | ) |
---|
417 | ); |
---|
418 | |
---|
419 | if ($persistent_cache->get($cache_key, $returned)) |
---|
420 | { |
---|
421 | shuffle($returned); |
---|
422 | return $returned; |
---|
423 | } |
---|
424 | |
---|
425 | $query = ' |
---|
426 | SELECT id |
---|
427 | FROM '.IMAGES_TABLE.' |
---|
428 | WHERE level <= '.$user['level'].' |
---|
429 | ORDER BY RAND() LIMIT '.($num*5).' |
---|
430 | ;'; |
---|
431 | $image_ids = query2array($query, null, 'id'); |
---|
432 | |
---|
433 | $sql = array(); |
---|
434 | |
---|
435 | $sql['select'] = ' |
---|
436 | SELECT |
---|
437 | i.id as image_id, |
---|
438 | i.file as image_file, |
---|
439 | i.comment, |
---|
440 | i.path, |
---|
441 | c.id as catid, |
---|
442 | c.name, |
---|
443 | c.permalink, |
---|
444 | i.name as imgname |
---|
445 | '; |
---|
446 | |
---|
447 | $sql['from'] = ' |
---|
448 | FROM '.CATEGORIES_TABLE.' c |
---|
449 | JOIN '.IMAGE_CATEGORY_TABLE.' ic ON ic.category_id = c.id |
---|
450 | JOIN '.IMAGES_TABLE.' i ON i.id = ic.image_id |
---|
451 | '; |
---|
452 | |
---|
453 | $sql['where'] = ' |
---|
454 | WHERE i.id IN ('.implode(',', $image_ids).') |
---|
455 | AND i.level <= '.$user['level'].' |
---|
456 | '; |
---|
457 | |
---|
458 | if($user['forbidden_categories']!="") |
---|
459 | { |
---|
460 | $sql['where'].=" AND c.id NOT IN (".$user['forbidden_categories'].") "; |
---|
461 | } |
---|
462 | |
---|
463 | switch($this->config['amm_randompicture_selectMode']) |
---|
464 | { |
---|
465 | case 'f': |
---|
466 | $sql['from'].=", ".USER_INFOS_TABLE." ui |
---|
467 | LEFT JOIN ".FAVORITES_TABLE." f ON ui.user_id=f.user_id "; |
---|
468 | $sql['where'].=" AND ui.status='webmaster' |
---|
469 | AND f.image_id = i.id "; |
---|
470 | break; |
---|
471 | case 'c': |
---|
472 | $sql['where'].="AND ("; |
---|
473 | foreach($this->config['amm_randompicture_selectCat'] as $key => $val) |
---|
474 | { |
---|
475 | $sql['where'].=($key==0?'':' OR ')." FIND_IN_SET($val, c.uppercats) "; |
---|
476 | } |
---|
477 | $sql['where'].=") "; |
---|
478 | break; |
---|
479 | } |
---|
480 | |
---|
481 | $sql = $sql['select'].$sql['from'].$sql['where']." ORDER BY RAND() LIMIT $num;"; |
---|
482 | |
---|
483 | $result = pwg_query($sql); |
---|
484 | if($result) |
---|
485 | { |
---|
486 | while($row=pwg_db_fetch_assoc($result)) |
---|
487 | { |
---|
488 | $row['section']='category'; |
---|
489 | $row['category']=array( |
---|
490 | 'id' => $row['catid'], |
---|
491 | 'name' => $row['name'], |
---|
492 | 'permalink' => $row['permalink'] |
---|
493 | ); |
---|
494 | |
---|
495 | $row['link']=make_picture_url($row); |
---|
496 | $row['thumb']=DerivativeImage::thumb_url(array('id'=>$row['image_id'], 'path'=>$row['path'])); |
---|
497 | |
---|
498 | $returned[]=$row; |
---|
499 | } |
---|
500 | } |
---|
501 | |
---|
502 | if (count($returned) > 0) |
---|
503 | {// cache the results only if not empty - otherwise it is useless |
---|
504 | $persistent_cache->set($cache_key, $returned, 300); |
---|
505 | } |
---|
506 | |
---|
507 | return($returned); |
---|
508 | } |
---|
509 | |
---|
510 | |
---|
511 | |
---|
512 | |
---|
513 | public function applyJS() |
---|
514 | { |
---|
515 | global $user, $template, $page; |
---|
516 | |
---|
517 | if(!array_key_exists('body_id', $page)) |
---|
518 | { |
---|
519 | /* |
---|
520 | * it seems the error message reported on mantis:1476 is displayed because |
---|
521 | * the 'body_id' doesn't exist in the $page |
---|
522 | * |
---|
523 | * not abble to reproduce the error, but initializing the key to an empty |
---|
524 | * value if it doesn't exist may be a sufficient solution |
---|
525 | */ |
---|
526 | $page['body_id']=""; |
---|
527 | } |
---|
528 | |
---|
529 | |
---|
530 | if($this->displayRandomImageBlock && $page['body_id'] == 'theCategoryPage') |
---|
531 | { |
---|
532 | $local_tpl = new Template(AMM_PATH."admin/", ""); |
---|
533 | $local_tpl->set_filename('body_page', dirname($this->getFileLocation()).'/menu_templates/menubar_randompic.js.tpl'); |
---|
534 | |
---|
535 | $local_tpl->assign('data', $this->randomPictProp); |
---|
536 | |
---|
537 | $template->append('head_elements', $local_tpl->parse('body_page', true)); |
---|
538 | } |
---|
539 | } |
---|
540 | |
---|
541 | |
---|
542 | |
---|
543 | public function buildMenuFromCat($where) |
---|
544 | { |
---|
545 | global $user; |
---|
546 | |
---|
547 | if($this->currentBuiltMenu>-1) |
---|
548 | { |
---|
549 | if($user['expand']) |
---|
550 | { |
---|
551 | $where=preg_replace('/id_uppercat\s+is\s+NULL/i', 'id_uppercat is NOT NULL', $where); |
---|
552 | } |
---|
553 | else |
---|
554 | { |
---|
555 | $where=preg_replace('/id_uppercat\s+is\s+NULL/i', 'id_uppercat is NULL OR id_uppercat IN ('.$this->currentBuiltMenu.')', $where); |
---|
556 | } |
---|
557 | |
---|
558 | $where.=" AND FIND_IN_SET(".$this->currentBuiltMenu.", uppercats) AND cat_id!=".$this->currentBuiltMenu." "; |
---|
559 | } |
---|
560 | |
---|
561 | return($where); |
---|
562 | } |
---|
563 | |
---|
564 | } // AMM_PIP class |
---|
565 | |
---|
566 | |
---|
567 | ?> |
---|