source: extensions/PWG_Stuffs/class.inc.php @ 9410

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

Some optimizations.

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