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

Last change on this file since 5545 was 5545, checked in by grum, 14 years ago

Update the plugin for compatibility with Piwigo 2.1

  • Property svn:executable set to *
File size: 9.8 KB
Line 
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  --------------------------------------------------------------------------- */
16if (!defined('PHPWG_ROOT_PATH')) { die('Hacking attempt!'); }
17
18include_once(PHPWG_PLUGINS_PATH.'AMenuManager/amm_root.class.inc.php');
19include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCAjax.class.inc.php');
20
21class AMM_PIP extends AMM_root
22{
23  protected $displayRandomImageBlock=true;
24
25  function AMM_PIP($prefixeTable, $filelocation)
26  {
27    parent::__construct($prefixeTable, $filelocation);
28    $this->css = new GPCCss(dirname($this->getFileLocation()).'/'.$this->getPluginNameFiles()."2.css");
29
30    $this->loadConfig();
31    $this->initEvents();
32  }
33
34
35  /* ---------------------------------------------------------------------------
36  Public classe functions
37  --------------------------------------------------------------------------- */
38
39
40  /*
41    initialize events call for the plugin
42  */
43  public function initEvents()
44  {
45    //TODELETE: add_event_handler('loc_begin_menubar', array(&$this, 'modify_menu') );
46    parent::initEvents();
47    add_event_handler('loading_lang', array(&$this, 'load_lang'));
48    add_event_handler('blockmanager_apply', array(&$this, 'blockmanager_apply') );
49    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()
83      );
84      $block->template = dirname(__FILE__).'/menu_templates/menubar_randompic.tpl';
85    }
86    else
87    {
88      $this->displayRandomImageBlock=false;
89    }
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      {
99        if($this->config['amm_links_show_icons']=='y')
100        {
101          for($i=0;$i<count($urls);$i++)
102          {
103            $urls[$i]['icon']=get_root_url().'plugins/'.AMM_DIR."/links_pictures/".$urls[$i]['icon'];
104          }
105        }
106
107        $block->set_title( base64_decode($this->config['amm_links_title'][$user['language']]) );
108        $block->template = dirname(__FILE__).'/menu_templates/menubar_links.tpl';
109
110        $block->data = array(
111          'LINKS' => $urls,
112          'icons' => $this->config['amm_links_show_icons']
113        );
114      }
115    }
116
117    /*
118      Add personnal blocks random picture section
119    */
120    $sections=$this->get_sections(true);
121
122    if(count($sections))
123    {
124      $id_done=array();
125      foreach($sections as $key => $val)
126      {
127        if(!isset($id_done[$val['id']]))
128        {
129          if ( ($block = $menu->get_block( 'mbAMM_personalised'.$val['id'] ) ) != null )
130          {
131            $block->set_title( $val['title'] );
132            $block->template = dirname(__FILE__).'/menu_templates/menubar_personalised.tpl';
133            $block->data = stripslashes($val['content']);
134          }
135          $id_done[$val['id']]="";
136        }
137      }
138    }
139
140
141    /* manage items from special & menu sections
142     *  reordering items
143     *  grouping items
144     *  managing rights to access
145    */
146    $blocks=Array();
147
148    if($menu->is_hidden('mbMenu'))
149    {
150      // if block is hidden, make a fake to manage AMM features
151      // the fake block isn't displayed
152      $blocks['menu']=new DisplayBlock('amm_mbMenu');
153      $blocks['menu']->data=Array();
154    }
155    else
156    {
157      $blocks['menu']=$menu->get_block('mbMenu');
158    }
159
160    if($menu->is_hidden('mbSpecials'))
161    {
162      // if block is hidden, make a fake to manage AMM features
163      // the fake block isn't displayed
164      $blocks['special']=new DisplayBlock('amm_mbSpecial');
165      $blocks['special']->data=Array();
166    }
167    else
168    {
169      $blocks['special']=$menu->get_block('mbSpecials');
170    }
171
172    $menuItems=array_merge($blocks['menu']->data, $blocks['special']->data);
173    $this->sortSectionsItems();
174
175    $blocks['menu']->data=Array();
176    $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)
183    {
184      if(isset($menuItems[$key]))
185      {
186        $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
191         * 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        )
197        {
198          $blocks[$val['container']]->data[$key]=$menuItems[$key];
199        }
200      }
201    }
202    if(count($blocks['menu']->data)==0) $menu->hide_block('mbMenu');
203    if(count($blocks['special']->data)==0) $menu->hide_block('mbSpecials');
204  }
205
206
207  protected function get_user_groups($user_id)
208  {
209    $returned=array();
210    $sql="SELECT group_id FROM ".USER_GROUP_TABLE."
211          WHERE user_id = ".$user_id." ";
212    $result=pwg_query($sql);
213    if($result)
214    {
215      while($row=pwg_db_fetch_assoc($result))
216      {
217        array_push($returned, $row['group_id']);
218      }
219    }
220    return($returned);
221  }
222
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'])
236        {
237          case 'randompic':
238            $result=$this->ajax_amm_get_random_picture();
239            break;
240        }
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']
278        );
279
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  }
299
300
301  public function applyJS()
302  {
303    global $user, $template, $page;
304
305    if(!array_key_exists('body_id', $page))
306    {
307      /*
308       * it seems the error message reported on mantis:1476 is displayed because
309       * the 'body_id' doesn't exist in the $page
310       *
311       * not abble to reproduce the error, but initializing the key to an empty
312       * value if it doesn't exist may be a sufficient solution
313       */
314      $page['body_id']="";
315    }
316
317    if($this->displayRandomImageBlock && $page['body_id'] == 'theCategoryPage')
318    {
319      $local_tpl = new Template(AMM_PATH."admin/", "");
320      $local_tpl->set_filename('body_page', dirname($this->getFileLocation()).'/menu_templates/menubar_randompic.js.tpl');
321
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
333  }
334
335} // AMM_PIP class
336
337
338?>
Note: See TracBrowser for help on using the repository browser.