Changeset 8962 for extensions/AMenuManager/amm_pip.class.inc.php
- Timestamp:
- Jan 28, 2011, 5:10:45 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/AMenuManager/amm_pip.class.inc.php
r5545 r8962 22 22 { 23 23 protected $displayRandomImageBlock=true; 24 protected $registeredBlocks; 25 protected $randomPictProp=null; 26 protected $users; 27 protected $groups; 28 protected $currentBuiltMenu=-1; 24 29 25 30 function AMM_PIP($prefixeTable, $filelocation) … … 28 33 $this->css = new GPCCss(dirname($this->getFileLocation()).'/'.$this->getPluginNameFiles()."2.css"); 29 34 35 $this->users=new GPCUsers(); 36 $this->groups=new GPCGroups(); 37 30 38 $this->loadConfig(); 31 39 $this->initEvents(); … … 33 41 34 42 35 /* --------------------------------------------------------------------------- 36 Public classe functions 37 --------------------------------------------------------------------------- */ 38 39 40 /* 41 initialize events call for the plugin 42 */ 43 /** 44 * initialize events call for the plugin 45 */ 43 46 public function initEvents() 44 47 { 45 //TODELETE: add_event_handler('loc_begin_menubar', array(&$this, 'modify_menu') );46 48 parent::initEvents(); 47 add_event_handler('loading_lang', array(&$this, 'load_lang')); 48 add_event_handler('blockmanager_apply', array(&$this, 'blockmanager_apply') ); 49 50 add_event_handler('blockmanager_prepare_display', array(&$this, 'blockmanagerSortBlocks') ); 51 add_event_handler('blockmanager_apply', array(&$this, 'blockmanagerApply') ); 49 52 add_event_handler('loc_end_page_header', array(&$this->css, 'applyCSS')); 50 add_event_handler('loc_end_page_tail', array(&$this, 'applyJS')); 51 } 52 53 /* 54 load language file 55 */ 56 public function load_lang() 57 { 58 global $lang; 59 60 //load_language('plugin.lang', AMM_PATH); 61 62 // ajax is managed here ; this permit to use user&language properties inside 63 // ajax content 64 $this->return_ajax_content(); 65 } 66 67 public function blockmanager_apply( $menu_ref_arr ) 68 { 69 global $user, $page; 70 $menu = & $menu_ref_arr[0]; 71 72 73 /* 74 Add a new random picture section 75 */ 76 if ( ( ($block = $menu->get_block( 'mbAMM_randompict' ) ) != null ) && ($user['nb_total_images'] > 0) ) 77 { 78 $block->set_title( base64_decode($this->config['amm_randompicture_title'][$user['language']]) ); 79 $block->data = array( 80 "delay" => $this->config['amm_randompicture_periodicchange'], 81 "blockHeight" => $this->config['amm_randompicture_height'], 82 "firstPicture" => $this->ajax_amm_get_random_picture() 53 add_event_handler('loc_end_page_header', array(&$this, 'applyJS')); 54 add_event_handler('get_categories_menu_sql_where', array(&$this, 'buildMenuFromCat'), 75); 55 } 56 57 58 public function blockmanagerApply($menu_ref_arr) 59 { 60 $menu=&$menu_ref_arr[0]; 61 62 $this->addBlockRandomPicture($menu); 63 $this->addBlockLinks($menu); 64 $this->addBlockPersonnal($menu); 65 $this->addBlockAlbum($menu); 66 $this->manageBlocksContent($menu); 67 $this->manageBlocks($menu); 68 } 69 70 71 /** 72 * Add a new random picture block 73 */ 74 private function addBlockRandomPicture(&$menu) 75 { 76 global $user; 77 78 if(( 79 ($block=$menu->get_block('mbAMM_randompict'))!=null) and 80 ($user['nb_total_images']>0) and 81 isset($this->config['amm_randompicture_title'][$user['language']]) 82 ) 83 { 84 GPCCore::addHeaderJS('jquery', 'themes/default/js/jquery.packed.js'); 85 GPCCore::addHeaderJS('amm.randomPictPublic', 'plugins/AMenuManager/js/amm_randomPictPublic'.GPCCore::getMinified().'.js'); 86 87 $block->set_title(base64_decode($this->config['amm_randompicture_title'][$user['language']])); 88 $block->template=dirname(__FILE__).'/menu_templates/menubar_randompic.tpl'; 89 90 $this->randomPictProp = array( 91 'delay' => $this->config['amm_randompicture_periodicchange'], 92 'blockHeight' => $this->config['amm_randompicture_height'], 93 'showname' => $this->config['amm_randompicture_showname'], 94 'showcomment' => $this->config['amm_randompicture_showcomment'], 95 'pictures' => $this->getRandomPictures($this->config['amm_randompicture_preload']) 83 96 ); 84 $block->template = dirname(__FILE__).'/menu_templates/menubar_randompic.tpl'; 97 98 if(count($this->randomPictProp['pictures'])==0) $this->displayRandomImageBlock=false; 85 99 } 86 100 else … … 88 102 $this->displayRandomImageBlock=false; 89 103 } 90 91 /* 92 Add a new section (links) 93 */ 94 if ( ($block = $menu->get_block( 'mbAMM_links' ) ) != null ) 95 { 96 $urls=$this->get_urls(true); 97 if ( count($urls)>0 ) 98 { 104 } 105 106 107 /** 108 * Add a new block (links) 109 */ 110 private function addBlockLinks(&$menu) 111 { 112 global $user; 113 114 $nbLink=0; 115 116 if(($block=$menu->get_block('mbAMM_links'))!=null && 117 isset($this->config['amm_links_title'][$user['language']]) 118 ) 119 { 120 $urls=$this->getLinks(true); 121 122 if(count($urls)>0) 123 { 124 $userGroups=$this->getUserGroups($user['id']);; 125 126 foreach($urls as $key => $val) 127 { 128 $this->users->setAlloweds(explode(",", $val['accessUsers']), false); 129 $this->groups->setAlloweds(explode(",", $val['accessGroups']), false); 130 131 if(!$this->users->isAllowed($user['status'])) 132 { 133 unset($urls[$key]); 134 } 135 else 136 { 137 $ok=true; 138 foreach($userGroups as $group) 139 { 140 if(!$this->groups->isAllowed($group)) $ok=false; 141 } 142 if(!$ok) unset($urls[$key]); 143 } 144 } 145 99 146 if($this->config['amm_links_show_icons']=='y') 100 147 { 101 for ($i=0;$i<count($urls);$i++)148 foreach($urls as $key => $url) 102 149 { 103 $urls[$ i]['icon']=get_root_url().'plugins/'.AMM_DIR."/links_pictures/".$urls[$i]['icon'];150 $urls[$key]['icon']=get_root_url().'plugins/'.AMM_DIR."/links_pictures/".$url['icon']; 104 151 } 105 152 } 106 153 107 $block->set_title( base64_decode($this->config['amm_links_title'][$user['language']]));108 $block->template =dirname(__FILE__).'/menu_templates/menubar_links.tpl';154 $block->set_title(base64_decode($this->config['amm_links_title'][$user['language']])); 155 $block->template=dirname(__FILE__).'/menu_templates/menubar_links.tpl'; 109 156 110 157 $block->data = array( … … 114 161 } 115 162 } 116 117 /* 118 Add personnal blocks random picture section 119 */ 120 $sections=$this->get_sections(true); 163 } 164 165 166 /** 167 * Add personnal blocks 168 */ 169 private function addBlockPersonnal(&$menu) 170 { 171 $sections=$this->getPersonalisedBlocks(true); 121 172 122 173 if(count($sections)) 123 174 { 124 $id _done=array();175 $idDone=array(); 125 176 foreach($sections as $key => $val) 126 177 { 127 if(!isset($id _done[$val['id']]))178 if(!isset($idDone[$val['id']])) 128 179 { 129 if ( ($block = $menu->get_block( 'mbAMM_personalised'.$val['id'] ) ) != null)180 if(($block=$menu->get_block('mbAMM_personalised'.$val['id']))!= null) 130 181 { 131 $block->set_title( $val['title']);182 $block->set_title($val['title']); 132 183 $block->template = dirname(__FILE__).'/menu_templates/menubar_personalised.tpl'; 133 184 $block->data = stripslashes($val['content']); 134 185 } 135 $id _done[$val['id']]="";186 $idDone[$val['id']]=""; 136 187 } 137 188 } 138 189 } 139 140 141 /* manage items from special & menu sections 142 * reordering items 143 * grouping items 144 * managing rights to access 145 */ 190 } 191 192 193 194 195 /** 196 * Add album to menu 197 */ 198 private function addBlockAlbum(&$menu) 199 { 200 if(count($this->config['amm_albums_to_menu'])>0) 201 { 202 $sql="SELECT id, name, permalink, global_rank 203 FROM ".CATEGORIES_TABLE." 204 WHERE id IN(".implode(',', $this->config['amm_albums_to_menu']).");"; 205 206 $result=pwg_query($sql); 207 if($result) 208 { 209 while($row=pwg_db_fetch_assoc($result)) 210 { 211 $this->currentBuiltMenu=$row['id']; 212 213 $row['name']=trigger_event('render_category_name', $row['name'], 'amm_album_to_menu'); 214 215 if(($block=$menu->get_block('mbAMM_album'.$row['id']))!= null) 216 { 217 $block->set_title($row['name']); 218 $block->template = dirname(__FILE__).'/menu_templates/menubar_album.tpl'; 219 $block->data = array( 220 'album' => get_categories_menu(), 221 'name' => $row['name'], 222 'link' => make_index_url(array('category' => $row)), 223 'nbPictures' => '' 224 ); 225 } 226 } 227 } 228 $this->currentBuiltMenu=-1; 229 } 230 } 231 232 /** 233 * manage items from special & menu blocks 234 * - reordering items 235 * - grouping items 236 * - managing rights to access 237 */ 238 private function manageBlocksContent(&$menu) 239 { 240 global $user; 241 146 242 $blocks=Array(); 147 243 148 244 if($menu->is_hidden('mbMenu')) 149 245 { 150 // if block is hidden, make a fake to manage AMM features246 // if block is hidden, make a fake to manage AMM submenu features 151 247 // the fake block isn't displayed 152 248 $blocks['menu']=new DisplayBlock('amm_mbMenu'); … … 160 256 if($menu->is_hidden('mbSpecials')) 161 257 { 162 // if block is hidden, make a fake to manage AMM features258 // if block is hidden, make a fake to manage AMM submenu features 163 259 // the fake block isn't displayed 164 260 $blocks['special']=new DisplayBlock('amm_mbSpecial'); … … 171 267 172 268 $menuItems=array_merge($blocks['menu']->data, $blocks['special']->data); 173 $this->sort SectionsItems();269 $this->sortCoreBlocksItems(); 174 270 175 271 $blocks['menu']->data=Array(); 176 272 $blocks['special']->data=Array(); 177 178 $users=new GPCUsers(""); 179 $groups=new GPCGroups(""); 180 $user_groups=$this->get_user_groups($user['id']); 181 182 foreach($this->config['amm_sections_items'] as $key => $val) 273 $userGroups=$this->getUserGroups($user['id']); 274 275 foreach($this->config['amm_blocks_items'] as $key => $val) 183 276 { 184 277 if(isset($menuItems[$key])) 185 278 { 186 279 $access=explode("/",$val['visibility']); 187 $users->setAlloweds(str_replace(",", "/", $access[0])); 188 $groups->setAlloweds(str_replace(",", "/", $access[1])); 189 190 /* test if user status is allowed to access the menu item 280 $this->users->setAlloweds(str_replace(",", "/", $access[0]), false); 281 $this->groups->setAlloweds(str_replace(",", "/", $access[1]), false); 282 283 /* 284 * test if user status is allowed to access the menu item 191 285 * if access is managed by group, the user have to be associated with an allowed group to access the menu item 192 */ 193 if($users->isAllowed($user['status']) && ( 194 ($access[1]=='') || 195 (($access[1]!='') && $groups->areAllowed($user_groups))) 196 ) 286 */ 287 if($this->users->isAllowed($user['status'])) 197 288 { 198 $blocks[$val['container']]->data[$key]=$menuItems[$key]; 289 $ok=true; 290 foreach($userGroups as $group) 291 { 292 if(!$this->groups->isAllowed($group)) $ok=false; 293 } 294 if($ok) $blocks[$val['container']]->data[$key]=$menuItems[$key]; 199 295 } 200 296 } … … 205 301 206 302 207 protected function get_user_groups($user_id) 208 { 303 /** 304 * return groups for a user 305 * 306 * @param String $userId 307 * @return Array 308 */ 309 private function getUserGroups($userId) 310 { 311 global $user; 312 209 313 $returned=array(); 210 $sql="SELECT group_id FROM ".USER_GROUP_TABLE." 211 WHERE user_id = ".$user_id."";314 315 $sql="SELECT group_id FROM ".USER_GROUP_TABLE." WHERE user_id='".$user['id']."';"; 212 316 $result=pwg_query($sql); 213 317 if($result) … … 215 319 while($row=pwg_db_fetch_assoc($result)) 216 320 { 217 array_push($returned, $row['group_id']);321 $returned[]=$row['group_id']; 218 322 } 219 323 } … … 221 325 } 222 326 223 /* 224 return ajax content 225 */ 226 protected function return_ajax_content() 227 { 228 global $ajax, $template; 229 230 if(isset($_REQUEST['ajaxfct'])) 231 { 232 if($_REQUEST['ajaxfct']=='randompic') 233 { 234 $result="<p class='errors'>".l10n('g002_error_invalid_ajax_call')."</p>"; 235 switch($_REQUEST['ajaxfct']) 327 328 /** 329 * reordering blocks and manage access right 330 * 331 */ 332 private function manageBlocks($menu) 333 { 334 $this->registeredBlocks=$this->getRegisteredBlocks(true); 335 336 foreach($menu->get_registered_blocks() as $key => $block) 337 { 338 if(!isset($this->registeredBlocks[$block->get_id()])) 339 { 340 $menu->hide_block($block->get_id()); 341 } 342 } 343 344 } 345 346 347 /** 348 * sort menu blocks according to AMM rules (overriding piwigo's sort rules) 349 */ 350 public function blockmanagerSortBlocks($blocks) 351 { 352 $this->registeredBlocks=$this->getRegisteredBlocks(true); 353 354 foreach($blocks[0]->get_registered_blocks() as $key => $block) 355 { 356 if(isset($this->registeredBlocks[$block->get_id()])) 357 { 358 $blocks[0]->set_block_position($block->get_id(), $this->registeredBlocks[$block->get_id()]['order']); 359 } 360 } 361 } 362 363 364 365 366 367 368 369 /** 370 * return a list of thumbnails 371 * each array items is an array 372 * 'imageId' => (integer) 373 * 'imageFile' => (String) 374 * 'comment' => (String) 375 * 'path' => (String) 376 * 'tn_ext' => (String) 377 * 'catId' => (String) 378 * 'name' => (String) 379 * 'permalink' => (String) 380 * 'imageName' => (String) 381 * 382 * @param Integer $number : number of returned images 383 * @return Array 384 */ 385 private function getRandomPictures($num=25) 386 { 387 global $user; 388 389 $returned=array(); 390 391 $sql=array(); 392 393 $sql['select']="SELECT i.id as image_id, i.file as image_file, i.comment, i.path, i.tn_ext, c.id as catid, c.name, c.permalink, RAND() as rndvalue, i.name as imgname "; 394 $sql['from']="FROM ".CATEGORIES_TABLE." c, ".IMAGES_TABLE." i, ".IMAGE_CATEGORY_TABLE." ic "; 395 $sql['where']="WHERE c.id = ic.category_id 396 AND ic.image_id = i.id 397 AND i.level <= ".$user['level']." "; 398 399 if($user['forbidden_categories']!="") 400 { 401 $sql['where'].=" AND c.id NOT IN (".$user['forbidden_categories'].") "; 402 } 403 404 switch($this->config['amm_randompicture_selectMode']) 405 { 406 case 'f': 407 $sql['from'].=", ".USER_INFOS_TABLE." ui 408 LEFT JOIN ".FAVORITES_TABLE." f ON ui.user_id=f.user_id "; 409 $sql['where'].=" AND ui.status='webmaster' 410 AND f.image_id = i.id "; 411 break; 412 case 'c': 413 $sql['where'].="AND ("; 414 foreach($this->config['amm_randompicture_selectCat'] as $key => $val) 236 415 { 237 case 'randompic': 238 $result=$this->ajax_amm_get_random_picture(); 239 break; 416 $sql['where'].=($key==0?'':' OR ')." FIND_IN_SET($val, c.uppercats) "; 240 417 } 241 GPCAjax::returnResult($result); 242 } 243 } 244 } 245 246 247 // return the html content for the random picture block 248 private function ajax_amm_get_random_picture() 249 { 250 global $user; 251 252 $local_tpl = new Template(AMM_PATH."menu_templates/", ""); 253 $local_tpl->set_filename('body_page', 254 dirname($this->getFileLocation()).'/menu_templates/menubar_randompic_inner.tpl'); 255 256 $sql="SELECT i.id as image_id, i.file as image_file, i.comment, i.path, i.tn_ext, c.id as catid, c.name, c.permalink, RAND() as rndvalue, i.name as imgname 257 FROM ".CATEGORIES_TABLE." c, ".IMAGES_TABLE." i, ".IMAGE_CATEGORY_TABLE." ic 258 WHERE c.id = ic.category_id 259 AND ic.image_id = i.id 260 AND i.level <= ".$user['level']." "; 261 if($user['forbidden_categories']!="") 262 { 263 $sql.=" AND c.id NOT IN (".$user['forbidden_categories'].") "; 264 } 265 266 $sql.=" ORDER BY rndvalue 267 LIMIT 0,1"; 268 269 270 $result = pwg_query($sql); 271 if($result and $nfo = pwg_db_fetch_assoc($result)) 272 { 273 $nfo['section']='category'; 274 $nfo['category']=array( 275 'id' => $nfo['catid'], 276 'name' => $nfo['name'], 277 'permalink' => $nfo['permalink'] 418 $sql['where'].=") "; 419 break; 420 } 421 422 $sql=$sql['select'].$sql['from'].$sql['where']." ORDER BY rndvalue LIMIT 0,$num"; 423 424 425 $result = pwg_query($sql); 426 if($result) 427 { 428 while($row=pwg_db_fetch_assoc($result)) 429 { 430 $row['section']='category'; 431 $row['category']=array( 432 'id' => $row['catid'], 433 'name' => $row['name'], 434 'permalink' => $row['permalink'] 278 435 ); 279 436 280 $template_datas = array( 281 'LINK' => make_picture_url($nfo), 282 'IMG' => get_thumbnail_url($nfo), 283 'IMGNAME' => $nfo['imgname'], 284 'IMGCOMMENT' => $nfo['comment'], 285 'SHOWNAME' => $this->config['amm_randompicture_showname'], 286 'SHOWCOMMENT' => $this->config['amm_randompicture_showcomment'] 287 ); 288 } 289 else 290 { 291 $template_datas = array(); 292 } 293 294 $local_tpl->assign('datas', $template_datas); 295 $local_tpl->assign('plugin', array('PATH' => AMM_PATH)); 296 297 return($local_tpl->parse('body_page', true)); 298 } 437 $row['link']=make_picture_url($row); 438 $row['thumb']=get_thumbnail_url($row); 439 440 $returned[]=$row; 441 } 442 } 443 444 return($returned); 445 } 446 447 299 448 300 449 … … 315 464 } 316 465 466 317 467 if($this->displayRandomImageBlock && $page['body_id'] == 'theCategoryPage') 318 468 { … … 320 470 $local_tpl->set_filename('body_page', dirname($this->getFileLocation()).'/menu_templates/menubar_randompic.js.tpl'); 321 471 322 $data = array( 323 "delay" => $this->config['amm_randompicture_periodicchange'], 324 "blockHeight" => $this->config['amm_randompicture_height'], 325 "firstPicture" => $this->ajax_amm_get_random_picture() 326 ); 327 328 $local_tpl->assign('data', $data); 329 330 $template->append('footer_elements', $local_tpl->parse('body_page', true)); 331 } 332 472 $local_tpl->assign('data', $this->randomPictProp); 473 474 $template->append('head_elements', $local_tpl->parse('body_page', true)); 475 } 476 } 477 478 479 480 public function buildMenuFromCat($where) 481 { 482 global $user; 483 484 if($this->currentBuiltMenu>-1) 485 { 486 if($user['expand']) 487 { 488 $where=preg_replace('/id_uppercat\s+is\s+NULL/i', 'id_uppercat is NOT NULL', $where); 489 } 490 else 491 { 492 $where=preg_replace('/id_uppercat\s+is\s+NULL/i', 'id_uppercat is NULL OR id_uppercat IN ('.$this->currentBuiltMenu.')', $where); 493 } 494 495 $where.=" AND FIND_IN_SET(".$this->currentBuiltMenu.", uppercats) AND cat_id!=".$this->currentBuiltMenu." "; 496 } 497 498 return($where); 333 499 } 334 500
Note: See TracChangeset
for help on using the changeset viewer.