source: extensions/PWG_Stuffs/include/class.inc.php @ 7340

Last change on this file since 7340 was 7340, checked in by patdenice, 14 years ago

Added feature: hide main block on home page.

File size: 6.1 KB
Line 
1<?php
2
3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
4
5class stuffs
6{
7  var $user_groups = array();
8  var $modules = array();
9        var $blocks = array();
10        var $pos = 'begin';
11
12  function stuffs()
13  {
14    $this->get_user_groups();
15    $this->get_modules();
16    $this->process_modules();
17  }
18
19  /* Retrieve user groups  */
20  function get_user_groups()
21  {
22    global $user;
23
24    $query = 'SELECT group_id FROM ' . USER_GROUP_TABLE . ' WHERE user_id = ' . $user['id'] . ';';
25    $result = pwg_query($query);
26    while ($row = mysql_fetch_assoc($result))
27    {
28      array_push($this->user_groups, $row['group_id']);
29    }
30  }
31
32  /* Retrieve modules from table */
33  function get_modules()
34  {
35    global $page, $user;
36
37    $query = '
38SELECT id, name, type, datas, groups, show_title, id_line, width
39FROM ' . STUFFS_TABLE . '
40WHERE users LIKE "%' . $user['status'] . '%"
41';
42
43    $script = script_basename();
44    if ($script == 'index')
45    {
46      $query .= isset($page['category']) ?
47        'AND on_cats = "true"' :
48        'AND on_home = "true"' ;
49    }
50    elseif ($script == 'picture')
51    {
52      $query .= 'AND on_picture = "true"';
53    }
54    else return;
55
56    $query .= '
57ORDER BY pos ASC;';
58
59    $result = pwg_query($query);
60
61    while ($row = mysql_fetch_assoc($result))
62    {
63      array_push($this->modules, $row);
64    }
65  }
66
67  /* Process modules */
68  function process_modules()
69  {
70    foreach ($this->modules as $module)
71    {
72      if ($module['type'] == 'MainBlock')
73      {
74                $this->pos = 'end';
75        if (!empty($module['datas'])
76          and $datas = unserialize($module['datas'])
77          and $datas['hide'])
78        {
79          add_event_handler('loc_end_index', 'remove_main_block');
80        }
81                continue;
82        }
83        if (!empty($module['groups']))
84      {
85                $authorized_groups = explode(',', $module['groups']);
86                if (array_intersect($this->user_groups, $authorized_groups) == array()) continue;
87        }
88
89        $datas = (!empty($module['datas']) ? unserialize($module['datas']) : false);
90      $block = array();
91
92        if ((include(STUFFS_PATH . 'modules/' . $module['type'] . '/main.inc.php')) === false) continue;
93
94      $block['ID'] = $module['id'];
95      if ($module['show_title'] == 'true')
96      {
97        $block['TITLE'] = trigger_event('render_stuffs_name', $module['name']);
98      }
99      if (is_admin())
100      {
101        $block['U_EDIT'] = PHPWG_ROOT_PATH.'admin.php?page=plugin&amp;section='.STUFFS_DIR.'%2Fadmin%2Fadd_module.php&amp;type='.$module['type'].'&amp;edit='.$module['id'];
102      }
103      $block['TEMPLATE'] = 'stuffs_'.strtolower($module['type']).'.tpl';
104        $this->set_tpl_block($block, $module);
105    }
106  }
107  /* Set template blocks  */
108        function set_tpl_block($block, $module)
109        {
110                if (!empty($module['id_line']))
111    {
112      $block['id_line'] = $module['id_line'];
113      $block['given_width'] = !empty($module['width']) ? $module['width'] : '';
114
115      if (!empty($this->blocks[$this->pos]))
116      {
117        $last = end($this->blocks[$this->pos]);
118        $key = key($this->blocks[$this->pos]);
119        $penul = prev($this->blocks[$this->pos]);
120
121        if (isset($last['id_line']) and $last['id_line'] == $module['id_line'])
122        {
123          if (isset($penul['id_line']) and $penul['id_line'] == $module['id_line'])
124          {
125            $i = 3;
126            !$block['given_width'] or $i--;
127            !$last['given_width'] or $i--;
128            !$penul['given_width'] or $i--;
129
130            !$i or $default_width = intval((100 - $block['given_width'] - $last['given_width'] - $penul['given_width']) / $i);
131
132            $penul['WIDTH'] = $penul['given_width'] ? $penul['given_width'] : $default_width;
133            $block['WIDTH'] = $block['given_width'] ? $block['given_width'] : $default_width;
134
135            $block['CLASS'] = 'right_block';
136            $block['new_line'] = false;
137            $block['end_line'] = false;
138            $last['end_line'] = true;
139            $this->blocks[$this->pos][$key-1] = $penul;
140            $this->blocks[$this->pos][$key] = $block;
141            $this->blocks[$this->pos][] = $last;
142            return;
143          }
144          else
145          {
146            if (empty($block['given_width']) and empty($last['given_width']))
147            {
148              $last['WIDTH'] = 50;
149            }
150            elseif ($block['given_width']>0)
151            {
152              $last['WIDTH'] = 100 - $block['given_width'];
153            }
154            else
155            {
156              $last['WIDTH'] = $last['given_width'];
157            }
158            $block['CLASS'] = 'middle_block';
159            $last['CLASS'] = 'left_block';
160            $block['new_line'] = false;
161            $block['end_line'] = true;
162            $last['end_line'] = false;
163            $this->blocks[$this->pos][$key] = $last;
164            $this->blocks[$this->pos][] = $block;
165            return;
166          }
167        }
168      }
169    }
170
171    $block['new_line'] = true;
172    $block['end_line'] = true;
173    $block['CLASS'] = 'middle_block';
174        $this->blocks[$this->pos][] = $block;
175        }
176
177  static function prefilter_index_css($source, &$smarty)
178  {
179    $css = array(
180      '<link rel="stylesheet" type="text/css" href="{$ROOT_URL}plugins/PWG_Stuffs/theme/stuffs_index.css">'
181    );
182
183    foreach ($smarty->get_template_vars('themes') as $theme)
184    {
185      if (file_exists(PHPWG_THEMES_PATH.$theme['id'].'/stuffs_index.css'))
186      {
187        array_push($css, '<link rel="stylesheet" type="text/css" href="{$ROOT_URL}themes/'.$theme['id'].'/stuffs_index.css">');
188      }
189    }
190
191    $source = str_replace("\n</head>", "\n".implode( "\n", $css )."\n</head>", $source);
192
193    return $source;
194  }
195
196  static function prefilter_picture_css($source, &$smarty)
197  {
198    $css = array(
199      '<link rel="stylesheet" type="text/css" href="{$ROOT_URL}plugins/PWG_Stuffs/theme/stuffs_picture.css">'
200    );
201
202    foreach ($smarty->get_template_vars('themes') as $theme)
203    {
204      if (file_exists(PHPWG_THEMES_DIR.$theme['id'].'/stuffs_picture.css'))
205      {
206        array_push($css, '<link rel="stylesheet" type="text/css" href="{$ROOT_URL}themes/'.$theme['id'].'/stuffs_picture.css">');
207      }
208    }
209
210    if (!empty($css))
211    {
212      $source = str_replace("\n</head>", "\n".implode( "\n", $css )."\n</head>", $source);
213    }
214
215    return $source;
216  }
217}
218
219?>
Note: See TracBrowser for help on using the repository browser.