source: branches/1.0/content/plugins/widgets/widgets.php @ 861

Revision 861, 8.7 KB checked in by nick_ramsay, 3 years ago (diff)

Categories Widget - I took the *sidebar* categories out of the Categories plugin, and made it a separate plugin. This simplifies the code because there's no need for a settings page to choose between "menu bar" or "sidebar".

Line 
1<?php
2/**
3 * name: Widgets
4 * description: Manages the contents of the widget blocks
5 * version: 0.6
6 * folder: widgets
7 * class: Widgets
8 * hooks: theme_index_top, admin_theme_index_top, header_include, admin_header_include, admin_plugin_settings, admin_sidebar_plugin_settings, widget_block
9 * author: Nick Ramsay
10 * authorurl: http://hotarucms.org/member.php?1-Nick
11 *
12 * PHP version 5
13 *
14 * LICENSE: Hotaru CMS is free software: you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation, either version 3 of
17 * the License, or (at your option) any later version.
18 *
19 * Hotaru CMS is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 * FITNESS FOR A PARTICULAR PURPOSE.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with Hotaru CMS. If not, see http://www.gnu.org/licenses/.
25 *
26 * @category  Content Management System
27 * @package   HotaruCMS
28 * @author    Nick Ramsay <admin@hotarucms.org>
29 * @copyright Copyright (c) 2009, Hotaru CMS
30 * @license   http://www.gnu.org/copyleft/gpl.html GNU General Public License
31 * @link      http://www.hotarucms.org/
32 */
33
34
35class Widgets
36{
37    /**
38     * Set things up when the page is first loaded
39     */
40    public function admin_theme_index_top($h) { $this->theme_index_top($h); }
41    public function theme_index_top($h)
42    {
43        // Create a new global object called "widget_block".
44        require_once(LIBS . 'Widget.php');
45        $h->vars['widgets'] = new Widget();
46        $h->vars['widgets']->initializeWidgets($h);
47    }
48
49   
50    /**
51     * This is the hook in the widget_block (sidebar) template.
52     *
53     * It builds a new function name from the widget name and calls it.
54     */
55    public function widget_block($h, $block_id = array(1))
56    {
57        $block_id = $block_id[0];
58           
59        $widgets = $h->vars['widgets']->getArrayWidgets($h);
60       
61        if (!$widgets) { return false; }
62       
63        foreach ($widgets as $widget => $details) {
64            $function_name = "widget_" . $widget;
65           
66            // Only show widgets intended for this block
67            if (($details['block'] == $block_id) && $details['enabled'])
68            {
69                /*  include the plugin class if not already. This is usually done in the pluginHook
70                    function, but if no other functions are used, we need to include it here: */
71                require_once(PLUGINS . $details['plugin'] . "/" . $details['plugin'] . ".php");
72                $h->includeLanguage($details['plugin']);  // same for language
73               
74                if ($details['class'] && method_exists($details['class'], $function_name))
75                {   
76                    // must be a class object with a method that matches!
77                    $class = new $details['class']($widget);
78                    $class->$function_name($h, $details['args']);
79                }
80                else
81                {
82                    /* For multiple instances of widgets, we need to strip the id off the end and use the argument as the identifier.
83                       E.g. CHANGE widget_rss_show_1(1);
84                            TO     widget_rss_show(1); */
85                   
86                    $function_name_array = explode('_', $function_name);
87                    array_pop($function_name_array);
88                    $function_name = implode('_', $function_name_array);
89                    if ($details['class'])
90                    {
91                        // must be a class object!
92                        $class = new $details['class']($widget);
93                        $class->$function_name($h, $details['args']);
94                    }
95                }
96            }
97        }
98    }
99   
100
101    /**
102     * Widget Settings Page
103     */
104    public function admin_plugin_settings($h)
105    {
106        if ($h->cage->get->testAlpha('plugin') != 'widgets') { return false; }
107       
108        echo "<h1>" . $h->lang["widgets_settings_header"] . "</h1>\n";
109       
110        if ($h->cage->get->testAlpha('action')) {
111       
112            // Get widget settings from the database...
113            $widgets_settings = $h->getSerializedSettings('widgets');
114           
115            // Get the list of widgets...
116            $widgets = $h->vars['widgets']->getArrayWidgets($h);
117           
118            $last = count($widgets);
119               
120            $this_widget_function = $h->cage->get->testAlnumLines('widget');
121            $this_widget_order = $h->cage->get->testInt('order');
122            $this_widget_block = $h->cage->get->testInt('block');
123           
124            $this_widget_name = $h->vars['widgets']->getPluginFromFunction($h, $this_widget_function);
125           
126            if ($h->cage->get->testAlpha('action') == 'orderup') {
127                if ($this_widget_order > 1) {
128                    // find widget in the target spot...
129                    foreach ($widgets as $widget => $details) {
130                        if ($details['order'] == ($this_widget_order - 1)) {
131                       
132                            //Check if this widget and the target are in the same block
133                            if ($widgets_settings['widgets'][$widget]['block'] == $widgets_settings['widgets'][$this_widget_function]['block']) {
134                           
135                                $widgets_settings['widgets'][$widget]['order'] = $details['order'] + 1;
136                                $widgets_settings['widgets'][$this_widget_function]['order'] = $this_widget_order - 1;
137                                $h->messages[$h->lang['widgets_order_updated']] = 'green';
138                                break;
139                            } else {
140                                // In different blocks so don't change the order, just the block value
141                                $widgets_settings['widgets'][$this_widget_function]['block']--;
142                            }
143                        }
144                    }
145                           
146                } else {
147                    // prevent moving into block 0:
148                    if (($h->vars['widgets']->getLastWidgetBlock($widgets) > 1) && ($widgets_settings['widgets'][$this_widget_function]['block'] > 1)) {
149                        $widgets_settings['widgets'][$this_widget_function]['block']--;
150                    } else {
151                        $h->messages[$h->lang['widgets_order_already_first']] = 'red';
152                    }
153                }
154               
155            } elseif ($h->cage->get->testAlpha('action') == 'orderdown') {
156                if ($this_widget_order < $last) {
157                    // find widget in the target spot...
158                    foreach ($widgets as $widget => $details) {
159                        if ($details['order'] == ($this_widget_order + 1)) {
160                            $widgets_settings['widgets'][$widget]['order'] = $details['order'] - 1;
161                            $widgets_settings['widgets'][$this_widget_function]['order'] = $this_widget_order + 1;
162                            $h->messages[$h->lang['widgets_order_updated']] = 'green';
163                            break;
164                        }
165                    }
166                } else {
167                    $widgets_settings['widgets'][$this_widget_function]['block']++;
168                    //$h->messages[$h->lang['widgets_order_already_last']] = 'red';
169                }       
170            }
171            elseif ($h->cage->get->testAlpha('action') == 'enable')
172            {
173                // enable a widget
174                if ($h->isActive($this_widget_name)) {
175                    $widgets_settings['widgets'][$this_widget_function]['enabled'] = true;
176                    $h->messages[$h->lang['widgets_order_enabled']] = 'green';
177                } else {
178                    // don't enable it if the plugin is inactive
179                    $h->messages[$h->lang['widgets_order_not_active']] = 'red';
180                }
181            }
182            elseif ($h->cage->get->testAlpha('action') == 'disable')
183            {
184                $widgets_settings['widgets'][$this_widget_function]['enabled'] = false;
185                $h->messages[$h->lang['widgets_order_disabled']] = 'green';
186            }
187           
188            // Save updated widgets settings
189            $h->updateSetting('widgets_settings', serialize($widgets_settings));
190           
191        }
192       
193        $h->showMessages();
194        $h->displayTemplate('widget_ordering', 'widgets');
195        return true;
196    }
197
198}
199
200?>
Note: See TracBrowser for help on using the repository browser.