source: branches/1.2/libs/Widget.php @ 1284

Revision 1284, 7.5 KB checked in by nick_ramsay, 3 years ago (diff)

[Branch 1.2] Bug fix for widgets with attached arguments e.g. RSS show, not getting removed when the plugin is uninstalled.

Line 
1<?php
2/**
3 * The Widget class contains some useful methods when using widgets
4 *
5 * PHP version 5
6 *
7 * LICENSE: Hotaru CMS is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * Hotaru CMS is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with Hotaru CMS. If not, see http://www.gnu.org/licenses/.
18 *
19 * @category  Content Management System
20 * @package   HotaruCMS
21 * @author    Nick Ramsay <admin@hotarucms.org>
22 * @copyright Copyright (c) 2009, Hotaru CMS
23 * @license   http://www.gnu.org/copyleft/gpl.html GNU General Public License
24 * @link      http://www.hotarucms.org/
25 */
26   
27class Widget
28{   
29    /**
30     * Initialize widgets
31     */
32    public function initializeWidgets($h)
33    {
34        // Get settings from the database if they exist...
35        $widgets_settings = $h->getSerializedSettings('widgets');
36       
37        if ($widgets_settings) {
38            // delete completely any widgets from uninstalled plugins:
39            foreach ($widgets_settings as $ws => $plugins) {
40                foreach ($plugins as $plugin) {
41                    if (!$h->isInstalled($plugin['plugin'])) {
42                        $this->deleteWidget($h, $plugin['function']);
43                        unset($widgets_settings['widgets'][$plugin['function']]);
44                        // widget settings get updated at the end of this function
45                    }
46                }
47            }
48        }
49       
50        echo "<pre>"; print_r($widgets_settings['widgets']); echo "</pre>";
51       
52        $widgets = $this->getWidgets($h);
53       
54        if ($widgets) {
55            $count = 1;
56            foreach ($widgets as $widget) {
57           
58                // Assign order number if not already assigned one.
59                if (!isset($widgets_settings['widgets'][$widget->widget_function]['order'])) {
60                    $widgets_settings['widgets'][$widget->widget_function]['order'] = $count;
61                }
62               
63                // Assign widget number if not already assigned one.
64                if (!isset($widgets_settings['widgets'][$widget->widget_function]['block'])) {
65                    $widgets_settings['widgets'][$widget->widget_function]['block'] = 1;
66                }
67               
68                // Enable the widget if enabled status is not currently set...
69                if (!isset($widgets_settings['widgets'][$widget->widget_function]['enabled'])) {
70                    $widgets_settings['widgets'][$widget->widget_function]['enabled'] = true;
71                }
72               
73                // But! Disable it if the plugin for that widget is not currently active.
74                if (!$h->isActive($widget->widget_plugin) ) {
75                    $widgets_settings['widgets'][$widget->widget_function]['enabled'] = false;
76                }
77
78                // Add plugin name, function suffix and arguments to widget_settings:
79                $widgets_settings['widgets'][$widget->widget_function]['plugin'] = $widget->widget_plugin;
80                $widgets_settings['widgets'][$widget->widget_function]['class'] = $h->getPluginClass($widget->widget_plugin);
81                $widgets_settings['widgets'][$widget->widget_function]['function'] = $widget->widget_function;
82                $widgets_settings['widgets'][$widget->widget_function]['args'] = $widget->widget_args;
83
84                $count++;
85            }
86        }
87       
88        $h->updateSetting('widgets_settings', serialize($widgets_settings), 'widgets');
89    }
90
91
92    /**
93     * Add widget
94     *
95     * @param string $plugin
96     * @param string $function
97     * @param string $value
98     */
99    public function addWidget($h, $plugin = '', $function = '', $args = '')
100    {
101        // Check if it exists so we don't add a duplicate
102        $sql = "SELECT * FROM " . DB_PREFIX . "widgets WHERE widget_plugin = %s AND widget_function = %s AND widget_args = %s";
103        $results = $h->db->get_results($h->db->prepare($sql, $plugin, $function, $args));
104       
105        if (!$results) {
106            $sql = "INSERT INTO " . DB_PREFIX . "widgets (widget_plugin, widget_function, widget_args, widget_updateby) VALUES(%s, %s, %s, %d)";
107            $h->db->query($h->db->prepare($sql, $plugin, $function, $args, $h->currentUser->id));
108        }
109       
110        $h->db->query("OPTIMIZE TABLE " . DB_PREFIX . "widgets");
111    }
112   
113   
114    /**
115     * Get widgets from widget db table
116     *
117     * @return array - of widget settings
118     */
119    public function getWidgets($h)
120    {
121        $exists = $h->db->table_exists('widgets');
122       
123        if (!$exists) { return false; }
124       
125        // Get settings from the database if they exist...
126        $sql = "SELECT * FROM " . DB_PREFIX . 'widgets';
127        $widgets_settings = $h->db->get_results($h->db->prepare($sql));
128        return $widgets_settings;
129    }
130   
131
132    /**
133     * Get widgets from widgets_settings array
134     *
135     * USAGE: foreach ($widgets as $widget=>$details)
136     * { echo "Name: " . $widget; echo $details['order']; echo $details['args']; }
137     *
138     * @return array - of widgets
139     */
140    public function getArrayWidgets($h)
141    {
142        // Get settings from the database if they exist...
143        $widgets_settings = $h->getSerializedSettings('widgets');
144       
145        if ($widgets_settings['widgets']) {
146            $widgets = $widgets_settings['widgets'];    // associative array
147                   
148            $widgets = $this->orderWidgets($widgets);    // sorts plugins by "order"
149   
150            return $widgets;
151        }
152        return false;
153    }
154   
155   
156    /**
157     * Delete a widget from the widget db table
158     *
159     * @param string $function
160     */
161    public function deleteWidget($h, $function)
162    {
163        // Get settings from the database if they exist...
164        $sql = "DELETE FROM " . DB_PREFIX . "widgets WHERE widget_function = %s";
165        $h->db->query($h->db->prepare($sql, $function));
166       
167        $h->db->query("OPTIMIZE TABLE " . TABLE_WIDGETS);
168    }
169
170    /**
171     * Sort the widgets by order number
172     *
173     * @param array $widgets
174     * @return array - sorted widgets
175     */
176    public function orderWidgets($widgets = array())
177    {
178        if (!$widgets) { return false; }
179        return sksort($widgets, "order", "int", true);
180    }
181   
182
183    /**
184     * Get last block
185     *
186     * @param array $widgets
187     * @return int the highest block value of all the widgets, i.e. the number of blocks.
188     */
189    public function getLastWidgetBlock($widgets)
190    {
191        if (!$widgets) { return 1; }
192       
193        $highest = 1;
194        foreach ($widgets as $widget => $details) {
195            if (isset($details['block']) && ($details['block'] > $highest)) { $highest = $details['block']; }
196        }
197        return $highest;
198    }
199   
200   
201    /**
202     * Get plugin name from widget function name
203     *
204     * @return string
205     */
206    public function getPluginFromFunction($h, $function)
207    {
208        // Get settings from the database if they exist...
209        $sql = "SELECT widget_plugin FROM " . TABLE_WIDGETS . ' WHERE widget_function = %s';
210        $widget_plugin = $h->db->get_var($h->db->prepare($sql, $function));
211        return $widget_plugin;
212    }
213   
214}
215
216?>
Note: See TracBrowser for help on using the repository browser.