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

Last change on this file since 7541 was 7541, checked in by patdenice, 13 years ago

Correction of last commit.

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