1 | <?php |
---|
2 | /* ----------------------------------------------------------------------------- |
---|
3 | Plugin : Advanced Menu Manager |
---|
4 | Author : Grum |
---|
5 | email : grum@piwigo.org |
---|
6 | website : http://photos.grum.fr |
---|
7 | PWG user : http://forum.phpwebgallery.net/profile.php?id=3706 |
---|
8 | |
---|
9 | << May the Little SpaceFrog be with you ! >> |
---|
10 | ------------------------------------------------------------------------------ |
---|
11 | See main.inc.php for release information |
---|
12 | |
---|
13 | PIP classe => manage integration in public interface |
---|
14 | |
---|
15 | --------------------------------------------------------------------------- */ |
---|
16 | if (!defined('PHPWG_ROOT_PATH')) { die('Hacking attempt!'); } |
---|
17 | |
---|
18 | include_once(PHPWG_PLUGINS_PATH.'AMenuManager/amm_root.class.inc.php'); |
---|
19 | include_once(PHPWG_PLUGINS_PATH.'grum_plugins_classes-2/ajax.class.inc.php'); |
---|
20 | |
---|
21 | class AMM_PIP extends AMM_root |
---|
22 | { |
---|
23 | protected $ajax; |
---|
24 | protected $displayRandomImageBlock=true; |
---|
25 | |
---|
26 | function AMM_PIP($prefixeTable, $filelocation) |
---|
27 | { |
---|
28 | parent::__construct($prefixeTable, $filelocation); |
---|
29 | $this->ajax = new Ajax(); |
---|
30 | $this->css = new css(dirname($this->filelocation).'/'.$this->plugin_name_files."2.css"); |
---|
31 | |
---|
32 | $this->load_config(); |
---|
33 | $this->init_events(); |
---|
34 | } |
---|
35 | |
---|
36 | |
---|
37 | /* --------------------------------------------------------------------------- |
---|
38 | Public classe functions |
---|
39 | --------------------------------------------------------------------------- */ |
---|
40 | |
---|
41 | |
---|
42 | /* |
---|
43 | initialize events call for the plugin |
---|
44 | */ |
---|
45 | public function init_events() |
---|
46 | { |
---|
47 | //TODELETE: add_event_handler('loc_begin_menubar', array(&$this, 'modify_menu') ); |
---|
48 | parent::init_events(); |
---|
49 | add_event_handler('loading_lang', array(&$this, 'load_lang')); |
---|
50 | add_event_handler('blockmanager_apply', array(&$this, 'blockmanager_apply') ); |
---|
51 | add_event_handler('loc_end_page_header', array(&$this->css, 'apply_CSS')); |
---|
52 | add_event_handler('loc_end_page_tail', array(&$this, 'applyJS')); |
---|
53 | } |
---|
54 | |
---|
55 | /* |
---|
56 | load language file |
---|
57 | */ |
---|
58 | public function load_lang() |
---|
59 | { |
---|
60 | global $lang; |
---|
61 | |
---|
62 | //load_language('plugin.lang', AMM_PATH); |
---|
63 | |
---|
64 | // ajax is managed here ; this permit to use user&language properties inside |
---|
65 | // ajax content |
---|
66 | $this->return_ajax_content(); |
---|
67 | } |
---|
68 | |
---|
69 | public function blockmanager_apply( $menu_ref_arr ) |
---|
70 | { |
---|
71 | global $user, $page; |
---|
72 | $menu = & $menu_ref_arr[0]; |
---|
73 | |
---|
74 | |
---|
75 | /* |
---|
76 | Add a new random picture section |
---|
77 | */ |
---|
78 | if ( ( ($block = $menu->get_block( 'mbAMM_randompict' ) ) != null ) && ($user['nb_total_images'] > 0) ) |
---|
79 | { |
---|
80 | $block->set_title( base64_decode($this->my_config['amm_randompicture_title'][$user['language']]) ); |
---|
81 | $block->data = array( |
---|
82 | "delay" => $this->my_config['amm_randompicture_periodicchange'], |
---|
83 | "blockHeight" => $this->my_config['amm_randompicture_height'], |
---|
84 | "firstPicture" => $this->ajax_amm_get_random_picture() |
---|
85 | ); |
---|
86 | $block->template = dirname(__FILE__).'/menu_templates/menubar_randompic.tpl'; |
---|
87 | } |
---|
88 | else |
---|
89 | { |
---|
90 | $this->displayRandomImageBlock=false; |
---|
91 | } |
---|
92 | |
---|
93 | /* |
---|
94 | Add a new section (links) |
---|
95 | */ |
---|
96 | if ( ($block = $menu->get_block( 'mbAMM_links' ) ) != null ) |
---|
97 | { |
---|
98 | $urls=$this->get_urls(true); |
---|
99 | if ( count($urls)>0 ) |
---|
100 | { |
---|
101 | if($this->my_config['amm_links_show_icons']=='y') |
---|
102 | { |
---|
103 | for($i=0;$i<count($urls);$i++) |
---|
104 | { |
---|
105 | $urls[$i]['icon']=get_root_url().'plugins/'.AMM_DIR."/links_pictures/".$urls[$i]['icon']; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | $block->set_title( base64_decode($this->my_config['amm_links_title'][$user['language']]) ); |
---|
110 | $block->template = dirname(__FILE__).'/menu_templates/menubar_links.tpl'; |
---|
111 | |
---|
112 | $block->data = array( |
---|
113 | 'LINKS' => $urls, |
---|
114 | 'icons' => $this->my_config['amm_links_show_icons'] |
---|
115 | ); |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | /* |
---|
120 | Add personnal blocks random picture section |
---|
121 | */ |
---|
122 | $sections=$this->get_sections(true); |
---|
123 | |
---|
124 | if(count($sections)) |
---|
125 | { |
---|
126 | $id_done=array(); |
---|
127 | foreach($sections as $key => $val) |
---|
128 | { |
---|
129 | if(!isset($id_done[$val['id']])) |
---|
130 | { |
---|
131 | if ( ($block = $menu->get_block( 'mbAMM_personalised'.$val['id'] ) ) != null ) |
---|
132 | { |
---|
133 | $block->set_title( $val['title'] ); |
---|
134 | $block->template = dirname(__FILE__).'/menu_templates/menubar_personalised.tpl'; |
---|
135 | $block->data = stripslashes($val['content']); |
---|
136 | } |
---|
137 | $id_done[$val['id']]=""; |
---|
138 | } |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | /* manage items from special & menu sections |
---|
144 | * reordering items |
---|
145 | * grouping items |
---|
146 | * managing rights to access |
---|
147 | */ |
---|
148 | $blocks=Array(); |
---|
149 | |
---|
150 | if($menu->is_hidden('mbMenu')) |
---|
151 | { |
---|
152 | // if block is hidden, make a fake to manage AMM features |
---|
153 | // the fake block isn't displayed |
---|
154 | $blocks['menu']=new DisplayBlock('amm_mbMenu'); |
---|
155 | $blocks['menu']->data=Array(); |
---|
156 | } |
---|
157 | else |
---|
158 | { |
---|
159 | $blocks['menu']=$menu->get_block('mbMenu'); |
---|
160 | } |
---|
161 | |
---|
162 | if($menu->is_hidden('mbSpecials')) |
---|
163 | { |
---|
164 | // if block is hidden, make a fake to manage AMM features |
---|
165 | // the fake block isn't displayed |
---|
166 | $blocks['special']=new DisplayBlock('amm_mbSpecial'); |
---|
167 | $blocks['special']->data=Array(); |
---|
168 | } |
---|
169 | else |
---|
170 | { |
---|
171 | $blocks['special']=$menu->get_block('mbSpecials'); |
---|
172 | } |
---|
173 | |
---|
174 | $menuItems=array_merge($blocks['menu']->data, $blocks['special']->data); |
---|
175 | $this->sortSectionsItems(); |
---|
176 | |
---|
177 | $blocks['menu']->data=Array(); |
---|
178 | $blocks['special']->data=Array(); |
---|
179 | |
---|
180 | $users=new users(""); |
---|
181 | $groups=new groups(""); |
---|
182 | $user_groups=$this->get_user_groups($user['id']); |
---|
183 | |
---|
184 | foreach($this->my_config['amm_sections_items'] as $key => $val) |
---|
185 | { |
---|
186 | if(isset($menuItems[$key])) |
---|
187 | { |
---|
188 | $access=explode("/",$val['visibility']); |
---|
189 | $users->set_alloweds(str_replace(",", "/", $access[0])); |
---|
190 | $groups->set_alloweds(str_replace(",", "/", $access[1])); |
---|
191 | |
---|
192 | /* test if user status is allowed to access the menu item |
---|
193 | * if access is managed by group, the user have to be associated with an allowed group to access the menu item |
---|
194 | */ |
---|
195 | if($users->is_allowed($user['status']) && ( |
---|
196 | ($access[1]=='') || |
---|
197 | (($access[1]!='') && $groups->are_allowed($user_groups))) |
---|
198 | ) |
---|
199 | { |
---|
200 | $blocks[$val['container']]->data[$key]=$menuItems[$key]; |
---|
201 | } |
---|
202 | } |
---|
203 | } |
---|
204 | if(count($blocks['menu']->data)==0) $menu->hide_block('mbMenu'); |
---|
205 | if(count($blocks['special']->data)==0) $menu->hide_block('mbSpecials'); |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | protected function get_user_groups($user_id) |
---|
210 | { |
---|
211 | $returned=array(); |
---|
212 | $sql="SELECT group_id FROM ".USER_GROUP_TABLE." |
---|
213 | WHERE user_id = ".$user_id." "; |
---|
214 | $result=pwg_query($sql); |
---|
215 | if($result) |
---|
216 | { |
---|
217 | while($row=pwg_db_fetch_assoc($result)) |
---|
218 | { |
---|
219 | array_push($returned, $row['group_id']); |
---|
220 | } |
---|
221 | } |
---|
222 | return($returned); |
---|
223 | } |
---|
224 | |
---|
225 | /* |
---|
226 | return ajax content |
---|
227 | */ |
---|
228 | protected function return_ajax_content() |
---|
229 | { |
---|
230 | global $ajax, $template; |
---|
231 | |
---|
232 | if(isset($_REQUEST['ajaxfct'])) |
---|
233 | { |
---|
234 | if($_REQUEST['ajaxfct']=='randompic') |
---|
235 | { |
---|
236 | $result="<p class='errors'>".l10n('g002_error_invalid_ajax_call')."</p>"; |
---|
237 | switch($_REQUEST['ajaxfct']) |
---|
238 | { |
---|
239 | case 'randompic': |
---|
240 | $result=$this->ajax_amm_get_random_picture(); |
---|
241 | break; |
---|
242 | } |
---|
243 | $this->ajax->return_result($result); |
---|
244 | } |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | |
---|
249 | // return the html content for the random picture block |
---|
250 | private function ajax_amm_get_random_picture() |
---|
251 | { |
---|
252 | global $user; |
---|
253 | |
---|
254 | $local_tpl = new Template(AMM_PATH."menu_templates/", ""); |
---|
255 | $local_tpl->set_filename('body_page', |
---|
256 | dirname($this->filelocation).'/menu_templates/menubar_randompic_inner.tpl'); |
---|
257 | |
---|
258 | $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 |
---|
259 | FROM ".CATEGORIES_TABLE." c, ".IMAGES_TABLE." i, ".IMAGE_CATEGORY_TABLE." ic |
---|
260 | WHERE c.id = ic.category_id |
---|
261 | AND ic.image_id = i.id |
---|
262 | AND i.level <= ".$user['level']." "; |
---|
263 | if($user['forbidden_categories']!="") |
---|
264 | { |
---|
265 | $sql.=" AND c.id NOT IN (".$user['forbidden_categories'].") "; |
---|
266 | } |
---|
267 | |
---|
268 | $sql.=" ORDER BY rndvalue |
---|
269 | LIMIT 0,1"; |
---|
270 | |
---|
271 | |
---|
272 | $result = pwg_query($sql); |
---|
273 | if($result and $nfo = pwg_db_fetch_assoc($result)) |
---|
274 | { |
---|
275 | $nfo['section']='category'; |
---|
276 | $nfo['category']=array( |
---|
277 | 'id' => $nfo['catid'], |
---|
278 | 'name' => $nfo['name'], |
---|
279 | 'permalink' => $nfo['permalink'] |
---|
280 | ); |
---|
281 | |
---|
282 | $template_datas = array( |
---|
283 | 'LINK' => make_picture_url($nfo), |
---|
284 | 'IMG' => get_thumbnail_url($nfo), |
---|
285 | 'IMGNAME' => $nfo['imgname'], |
---|
286 | 'IMGCOMMENT' => $nfo['comment'], |
---|
287 | 'SHOWNAME' => $this->my_config['amm_randompicture_showname'], |
---|
288 | 'SHOWCOMMENT' => $this->my_config['amm_randompicture_showcomment'] |
---|
289 | ); |
---|
290 | } |
---|
291 | else |
---|
292 | { |
---|
293 | $template_datas = array(); |
---|
294 | } |
---|
295 | |
---|
296 | $local_tpl->assign('datas', $template_datas); |
---|
297 | $local_tpl->assign('plugin', array('PATH' => AMM_PATH)); |
---|
298 | |
---|
299 | return($local_tpl->parse('body_page', true)); |
---|
300 | } |
---|
301 | |
---|
302 | |
---|
303 | public function applyJS() |
---|
304 | { |
---|
305 | global $user, $template, $page; |
---|
306 | |
---|
307 | if(!array_key_exists('body_id', $page)) |
---|
308 | { |
---|
309 | /* |
---|
310 | * it seems the error message reported on mantis:1476 is displayed because |
---|
311 | * the 'body_id' doesn't exist in the $page |
---|
312 | * |
---|
313 | * not abble to reproduce the error, but initializing the key to an empty |
---|
314 | * value if it doesn't exist may be a sufficient solution |
---|
315 | */ |
---|
316 | $page['body_id']=""; |
---|
317 | } |
---|
318 | |
---|
319 | if($this->displayRandomImageBlock && $page['body_id'] == 'theCategoryPage') |
---|
320 | { |
---|
321 | $local_tpl = new Template(AMM_PATH."admin/", ""); |
---|
322 | $local_tpl->set_filename('body_page', dirname($this->filelocation).'/menu_templates/menubar_randompic.js.tpl'); |
---|
323 | |
---|
324 | $data = array( |
---|
325 | "delay" => $this->my_config['amm_randompicture_periodicchange'], |
---|
326 | "blockHeight" => $this->my_config['amm_randompicture_height'], |
---|
327 | "firstPicture" => $this->ajax_amm_get_random_picture() |
---|
328 | ); |
---|
329 | |
---|
330 | $local_tpl->assign('data', $data); |
---|
331 | |
---|
332 | $template->append('footer_elements', $local_tpl->parse('body_page', true)); |
---|
333 | } |
---|
334 | |
---|
335 | } |
---|
336 | |
---|
337 | } // AMM_PIP class |
---|
338 | |
---|
339 | |
---|
340 | ?> |
---|