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

Revision 981, 9.2 KB checked in by nick_ramsay, 3 years ago (diff)

[Branch] Fix for moving widgets into other widget blocks and switched RSS icon from left to right of RSS Show widget header.

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 (but only if greater than 1)
141                                if ($widgets_settings['widgets'][$this_widget_function]['block'] > 1) {
142                                    $widgets_settings['widgets'][$this_widget_function]['block']--;
143                                }
144                            }
145                        }
146                    }
147                           
148                } else {
149                    // prevent moving into block 0:
150                    if (($h->vars['widgets']->getLastWidgetBlock($widgets) > 1) && ($widgets_settings['widgets'][$this_widget_function]['block'] > 1)) {
151                        $widgets_settings['widgets'][$this_widget_function]['block']--;
152                    } else {
153                        $h->messages[$h->lang['widgets_order_already_first']] = 'red';
154                    }
155                }
156               
157            } elseif ($h->cage->get->testAlpha('action') == 'orderdown') {
158                if ($this_widget_order < $last) {
159                    // find widget in the target spot...
160                    foreach ($widgets as $widget => $details) {
161                        if ($details['order'] == ($this_widget_order + 1)) {
162                            // just increase the block?
163                            if ($widgets_settings['widgets'][$widget]['block'] > $this_widget_block) {
164                                $widgets_settings['widgets'][$this_widget_function]['block']++;
165                            // or increase the order?
166                            } else {
167                                $widgets_settings['widgets'][$widget]['order'] = $details['order'] - 1;
168                                $widgets_settings['widgets'][$this_widget_function]['order'] = $this_widget_order + 1;
169                            }
170                            $h->messages[$h->lang['widgets_order_updated']] = 'green';
171                            break;
172                        }
173                    }
174                } else {
175                    $widgets_settings['widgets'][$this_widget_function]['block']++;
176                    //$h->messages[$h->lang['widgets_order_already_last']] = 'red';
177                }       
178            }
179            elseif ($h->cage->get->testAlpha('action') == 'enable')
180            {
181                // enable a widget
182                if ($h->isActive($this_widget_name)) {
183                    $widgets_settings['widgets'][$this_widget_function]['enabled'] = true;
184                    $h->messages[$h->lang['widgets_order_enabled']] = 'green';
185                } else {
186                    // don't enable it if the plugin is inactive
187                    $h->messages[$h->lang['widgets_order_not_active']] = 'red';
188                }
189            }
190            elseif ($h->cage->get->testAlpha('action') == 'disable')
191            {
192                $widgets_settings['widgets'][$this_widget_function]['enabled'] = false;
193                $h->messages[$h->lang['widgets_order_disabled']] = 'green';
194            }
195           
196            // Save updated widgets settings
197            $h->updateSetting('widgets_settings', serialize($widgets_settings));
198           
199        }
200       
201        $h->showMessages();
202        $h->displayTemplate('widget_ordering', 'widgets');
203        return true;
204    }
205
206}
207
208?>
Note: See TracBrowser for help on using the repository browser.