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

Revision 1285, 7.4 KB checked in by nick_ramsay, 3 years ago (diff)

[Branch 1.2] Removed unwanted print_r.

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        $widgets = $this->getWidgets($h);
51       
52        if ($widgets) {
53            $count = 1;
54            foreach ($widgets as $widget) {
55           
56                // Assign order number if not already assigned one.
57                if (!isset($widgets_settings['widgets'][$widget->widget_function]['order'])) {
58                    $widgets_settings['widgets'][$widget->widget_function]['order'] = $count;
59                }
60               
61                // Assign widget number if not already assigned one.
62                if (!isset($widgets_settings['widgets'][$widget->widget_function]['block'])) {
63                    $widgets_settings['widgets'][$widget->widget_function]['block'] = 1;
64                }
65               
66                // Enable the widget if enabled status is not currently set...
67                if (!isset($widgets_settings['widgets'][$widget->widget_function]['enabled'])) {
68                    $widgets_settings['widgets'][$widget->widget_function]['enabled'] = true;
69                }
70               
71                // But! Disable it if the plugin for that widget is not currently active.
72                if (!$h->isActive($widget->widget_plugin) ) {
73                    $widgets_settings['widgets'][$widget->widget_function]['enabled'] = false;
74                }
75
76                // Add plugin name, function suffix and arguments to widget_settings:
77                $widgets_settings['widgets'][$widget->widget_function]['plugin'] = $widget->widget_plugin;
78                $widgets_settings['widgets'][$widget->widget_function]['class'] = $h->getPluginClass($widget->widget_plugin);
79                $widgets_settings['widgets'][$widget->widget_function]['function'] = $widget->widget_function;
80                $widgets_settings['widgets'][$widget->widget_function]['args'] = $widget->widget_args;
81
82                $count++;
83            }
84        }
85       
86        $h->updateSetting('widgets_settings', serialize($widgets_settings), 'widgets');
87    }
88
89
90    /**
91     * Add widget
92     *
93     * @param string $plugin
94     * @param string $function
95     * @param string $value
96     */
97    public function addWidget($h, $plugin = '', $function = '', $args = '')
98    {
99        // Check if it exists so we don't add a duplicate
100        $sql = "SELECT * FROM " . DB_PREFIX . "widgets WHERE widget_plugin = %s AND widget_function = %s AND widget_args = %s";
101        $results = $h->db->get_results($h->db->prepare($sql, $plugin, $function, $args));
102       
103        if (!$results) {
104            $sql = "INSERT INTO " . DB_PREFIX . "widgets (widget_plugin, widget_function, widget_args, widget_updateby) VALUES(%s, %s, %s, %d)";
105            $h->db->query($h->db->prepare($sql, $plugin, $function, $args, $h->currentUser->id));
106        }
107       
108        $h->db->query("OPTIMIZE TABLE " . DB_PREFIX . "widgets");
109    }
110   
111   
112    /**
113     * Get widgets from widget db table
114     *
115     * @return array - of widget settings
116     */
117    public function getWidgets($h)
118    {
119        $exists = $h->db->table_exists('widgets');
120       
121        if (!$exists) { return false; }
122       
123        // Get settings from the database if they exist...
124        $sql = "SELECT * FROM " . DB_PREFIX . 'widgets';
125        $widgets_settings = $h->db->get_results($h->db->prepare($sql));
126        return $widgets_settings;
127    }
128   
129
130    /**
131     * Get widgets from widgets_settings array
132     *
133     * USAGE: foreach ($widgets as $widget=>$details)
134     * { echo "Name: " . $widget; echo $details['order']; echo $details['args']; }
135     *
136     * @return array - of widgets
137     */
138    public function getArrayWidgets($h)
139    {
140        // Get settings from the database if they exist...
141        $widgets_settings = $h->getSerializedSettings('widgets');
142       
143        if ($widgets_settings['widgets']) {
144            $widgets = $widgets_settings['widgets'];    // associative array
145                   
146            $widgets = $this->orderWidgets($widgets);    // sorts plugins by "order"
147   
148            return $widgets;
149        }
150        return false;
151    }
152   
153   
154    /**
155     * Delete a widget from the widget db table
156     *
157     * @param string $function
158     */
159    public function deleteWidget($h, $function)
160    {
161        // Get settings from the database if they exist...
162        $sql = "DELETE FROM " . DB_PREFIX . "widgets WHERE widget_function = %s";
163        $h->db->query($h->db->prepare($sql, $function));
164       
165        $h->db->query("OPTIMIZE TABLE " . TABLE_WIDGETS);
166    }
167
168    /**
169     * Sort the widgets by order number
170     *
171     * @param array $widgets
172     * @return array - sorted widgets
173     */
174    public function orderWidgets($widgets = array())
175    {
176        if (!$widgets) { return false; }
177        return sksort($widgets, "order", "int", true);
178    }
179   
180
181    /**
182     * Get last block
183     *
184     * @param array $widgets
185     * @return int the highest block value of all the widgets, i.e. the number of blocks.
186     */
187    public function getLastWidgetBlock($widgets)
188    {
189        if (!$widgets) { return 1; }
190       
191        $highest = 1;
192        foreach ($widgets as $widget => $details) {
193            if (isset($details['block']) && ($details['block'] > $highest)) { $highest = $details['block']; }
194        }
195        return $highest;
196    }
197   
198   
199    /**
200     * Get plugin name from widget function name
201     *
202     * @return string
203     */
204    public function getPluginFromFunction($h, $function)
205    {
206        // Get settings from the database if they exist...
207        $sql = "SELECT widget_plugin FROM " . TABLE_WIDGETS . ' WHERE widget_function = %s';
208        $widget_plugin = $h->db->get_var($h->db->prepare($sql, $function));
209        return $widget_plugin;
210    }
211   
212}
213
214?>
Note: See TracBrowser for help on using the repository browser.