root/devel/mod/sidebar/lib.php

Revision 1539, 9.4 kB (checked in by renato, 10 months ago)

Setting prop svn:eol-style in LOTS of files.

  • Property svn:eol-style set to native
Line 
1 <?php
2 /**
3  * Sidebar plugin
4  * $id$
5  *
6  * @copyright Copyright (c) 2007 Pro Soft Resources Inc. http://www.prosoftpeople.com
7  * @author Rolando Espinoza La fuente <rho@prosoftpeople.com>
8  * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
9  */
10
11 /**
12  * Plugin initialization
13  */
14 function sidebar_init() {
15     global $CFG;
16     global $function;
17     global $PAGE;
18
19     if (!isset($PAGE->sidebar)) {
20         $PAGE->sidebar = array();
21     }
22
23     // base path
24     $base_path = $CFG->dirroot . 'mod/sidebar/';
25
26     //templates
27     $function['init'][] = $base_path . 'default_templates.php';
28     // use details, enable/disable sidebar blocks
29     $function['userdetails:edit:details'][] = $base_path . 'lib/userdetails_edit.php';
30
31     // legacy sidebar elements
32     $PAGE->sidebar_legacy = array();
33
34     $PAGE->sidebar_legacy['sidebar-profile'] = array(10, true);
35     $function['sidebar-profile'][] = $CFG->dirroot . 'mod/display/lib/function_log_on_pane.php';
36
37     $PAGE->sidebar_legacy['sidebar-stats'] = 50;
38     $function['sidebar-stats'][] = $CFG->dirroot . 'mod/users/lib/function_number_of_users.php';
39
40     $PAGE->sidebar_legacy['sidebar-blog'] = array(30, true);
41     $function['sidebar-blog'][] = $CFG->dirroot . 'mod/blog/lib/weblogs_user_info_menu.php';
42
43     $PAGE->sidebar_legacy['sidebar-friends'] = array(40, true);
44     $function['sidebar-friends'][] = $CFG->dirroot . 'mod/friend/lib/profile_friends.php';
45
46     $PAGE->sidebar_legacy['sidebar-communities'] = array(50, true);
47     $function['sidebar-communities'][] = $CFG->dirroot . 'mod/community/lib/community_memberships.php';
48
49     $PAGE->sidebar_legacy['sidebar-owned-communities'] = array(60, true);
50     $function['sidebar-owned-communities'][] = $CFG->dirroot . 'mod/community/lib/communities_owned.php';
51
52     $PAGE->sidebar_legacy['sidebar-files'] = array(60, true);
53     $function['sidebar-files'][] = $CFG->dirroot . 'mod/file/lib/files_user_info_menu.php';
54
55     // others plugins
56     if (is_readable($CFG->dirroot . 'mod/category/category_sidebar.php')) {
57         $PAGE->sidebar_legacy['sidebar-blogcats'] = array(30, true);
58         $function['sidebar-blogcats'][] = $CFG->dirroot . 'mod/category/category_sidebar.php';
59     }
60
61     if (is_readable($CFG->dirroot . 'mod/vanillaforum/lib/user_info_menu.php')) {
62         $PAGE->sidebar_legacy['sidebar-vanilla'] = 25;
63         $function['sidebar-vanilla'][] = $CFG->dirroot . 'mod/vanillaforum/lib/user_info_menu.php';
64     }
65
66 }
67
68 /**
69  * Plugin page setup
70  */
71 function sidebar_pagesetup() {
72     global $function;
73     global $PAGE;
74
75     $runned = array();
76     // legacy sidebar blocks
77     if (is_array($PAGE->sidebar_legacy)) {
78         foreach ($PAGE->sidebar_legacy as $key => $block) {
79             if (is_array($block)) {
80                 $weight = $block[0];
81                 $userdetails = $block[1];
82             } else {
83                 $weight = $block;
84                 $userdetails = false;
85             }
86             $label = str_replace('sidebar', '', str_replace('-', ' ', $key));
87             sidebar_add($weight, $key, sidebar_legacy_wrap(run($key)), true, $label);
88             $runned[] = $function[$key];
89             //print_object($key);
90         }
91     }
92
93     // filter legacy sidebar code already included
94     if (isset($function['display:sidebar'])) {
95         foreach ($function['display:sidebar'] as $n => $script) {
96             if (!in_arrayr($script, $runned)) {
97                 // temp function()
98                 $function["sidebar:$n"][] = $script;
99                 // include as generic block
100                 sidebar_add(99, "sidebar-$n", sidebar_legacy_wrap(run("sidebar:$n")));
101                 // unset
102                 unset($function["sidebar:$n"]);
103             }
104         }
105         //print_object($runned);
106     }
107
108     // replace old code
109     $function['display:sidebar'] = array(dirname(__FILE__) . '/lib/sidebar_display.php');
110 }
111
112 /**
113  * Adds new block to sidebar stack
114  *
115  * @param integer $weigth the weight/position of the block
116  * @param string $id unique block identificator
117  * @param bool $userdetails allow users to show/hide (true or false)
118  * @param string $label label to show on account settings
119  * @param string $class optional css class
120  */
121 function sidebar_add($weight, $id, $body, $userdetails=false, $label=null, $class=null) {
122     global $PAGE;
123
124     if (isset($PAGE->sidebar_locked) && !in_array($id, $PAGE->sidebar_locked)) {
125         return; // no add anything
126     }
127
128     if (!isset($PAGE->sidebar[$weight])) {
129         $PAGE->sidebar[$weight] = array();
130     }
131
132     if (!isset($PAGE->sidebar_blacklist)) {
133         $PAGE->sidebar_blacklist = array();
134     }
135
136     if (!in_array($id, $PAGE->sidebar_blacklist) && (!empty($body) || is_callable($id))) {
137         // add sidebar block
138         $PAGE->sidebar[$weight][] = array(
139             'id'  => $id,
140             'class' => $class,
141             'body' => $body,
142             'userdetails' => $userdetails,
143             'label' => $label,
144             );
145     }
146 }
147
148 /**
149  * Removes a block or blocks from sidebar
150  *
151  * @param mixed $id block identificator to remove, string or array of id's
152  * @param bool $overrideall if all blocks will be removed except provided $id
153  */
154 function sidebar_remove($id, $overrideall=false) {
155     global $PAGE;
156
157     if (empty($id)) {
158         trigger_error(__FUNCTION__.": invalid argument (id: empty)", E_USER_ERROR);
159     }
160
161     if ($overrideall) {
162         // clear sidebar
163         $block = array();
164         if (!is_array($id)) $id = array($id);
165         // exists?
166         foreach ($PAGE->sidebar as $w => $v) {
167             foreach ($v as $e) {
168                 // found
169                 if (in_array($e['id'], $id)) {
170                     while (isset($block[$w])) {$w++;}
171                     $block[$w] = $e;
172                 }
173             }
174         }
175         if (!empty($block)) {
176             unset($PAGE->sidebar);
177             foreach ($block as $w => $b) {
178                 sidebar_add($w, $b['id'], $b['body'], $b['userdetails'], $b['label'], $b['class']);
179             }
180         }
181         $PAGE->sidebar_locked = $id;
182         // end
183         return;
184     }
185
186     if (!isset($PAGE->sidebar_blacklist)) {
187         $PAGE->sidebar_blacklist = array();
188     }
189
190     if (is_array($id)) {
191         foreach ($id as $e) {
192             sidebar_remove($e);
193         }
194     }
195     // proceed if not blacklisted before
196     elseif (!in_array($id, $PAGE->sidebar_blacklist)) {
197         $PAGE->sidebar_blacklist[] = $id;
198
199         // prevent further inclusions
200         if (!empty($PAGE->sidebar) && is_array($PAGE->sidebar)) {
201
202             // remove block if exists
203             $sidebar = array();
204
205             foreach ($PAGE->sidebar as $w => $blocks) {
206                 if (!isset($sidebar[$w])) {
207                     $sidebar[$w] = array();
208                 }
209
210                 foreach ($blocks as $block) {
211                     if ($block['id'] == $id) {
212                         continue;
213                     } else {
214                         $sidebar[$w][] = $block;
215                     }
216                 }
217             }
218
219             // override sidebar
220             $PAGE->sidebar = $sidebar;
221             if (isset($PAGE->sidebar_locked)) {
222                 // unlock
223                 unset($PAGE->sidebar_locked);
224             }
225         }
226     }
227 }
228
229 /**
230  * Get all blocks registered
231  *
232  * @return mixed
233  */
234 function sidebar_get_blocks() {
235     global $PAGE;
236
237     $result = array();
238     if (!empty($PAGE->sidebar) && is_array($PAGE->sidebar)) {
239         foreach ($PAGE->sidebar as $w => $blocks) {
240             foreach ($blocks as $b) {
241                 if ($b['userdetails'] === true) {
242                     $result[] = $b;
243                 }
244             }
245         }
246     }
247
248     return $result;
249 }
250
251 /**
252  * Render sidebar
253  *
254  * @return string
255  */
256 function sidebar_display() {
257     global $PAGE;
258
259     $body = '';
260
261     if (!empty($PAGE->sidebar) && is_array($PAGE->sidebar)) {
262         $sidebar = $PAGE->sidebar;
263
264         // sort if needed
265         ksort($sidebar);
266
267         foreach ($sidebar as $w => $blocks) {
268             foreach ($blocks as $block) {
269                 if (user_flag_get('sidebar'.$block['id'], page_owner()) != 'no') {
270                     // print_object($block['id']);
271                     if (empty($block['body']) && is_callable($block['id'])) {
272                         // call function that returns sidebar body
273                         $block_body = $block['id']();
274                     } else {
275                         $block_body = $block['body'];
276                     }
277
278                     $body .= templates_draw(array(
279                         'context' => 'sidebar:block',
280                         'id' => $block['id'],
281                         'class' => $block['class'],
282                         'body' => $block_body,
283                         ));
284                 }
285             }
286         }
287     }
288
289     return templates_draw(array(
290         'context' => 'sidebar:wrap',
291         'body' => $body,
292     ));
293 }
294
295 /**
296  * Removed legacy code from old blocks format
297  *
298  * @param string $body legacy block
299  * @return string stripped body
300  */
301 function sidebar_legacy_wrap($body) {
302     // remove <li></li>
303     return preg_replace("#^\s*(<li|<li\s+[\"\'\w-_\.\s\=]*\s*)>(.*)</li>\s*$#si", "\${2}", $body);
304 }
305
306 // recursive array search
307 if (!function_exists('in_arrayr')) {
308     function in_arrayr($needle, $haystack) {
309         foreach ($haystack as $v) {
310             if ($needle == $v) {
311                 return true;
312             } elseif (is_array($v)) {
313                 if (in_arrayr($needle, $v) === true) {
314                     return true;
315                 }
316             }
317         }
318
319         return false;
320     }
321 }
322
323 ?>
324
Note: See TracBrowser for help on using the browser.