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

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

Add display option. Optimizations.

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