~quam-plures-core/quam-plures/qp5_colls-blogs_chaps-cats

« back to all changes in this revision

Viewing changes to qp_inc/widgets/model/_widgetcache.class.php

http://forums.quamplures.net/viewtopic.php?p=9237#p9237

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
<?php
2
2
/**
3
 
 * This file implements the WidgetCache class.
4
 
 *
5
 
 * This file is part of Quam Plures - {@link http://quamplures.net/}
6
 
 * See also {@link https://launchpad.net/quam-plures}.
7
 
 *
8
 
 * @copyright (c) 2009 - 2011 by the Quam Plures developers - {@link http://quamplures.net/}
9
 
 * @copyright (c)2003-2009 by Francois PLANQUE - {@link http://fplanque.net/}
10
 
 *
11
 
 * {@internal License choice
12
 
 * - If you have received this file as part of a package, please find the license.txt file in
13
 
 *   the same folder or the closest folder above for complete license terms.
14
 
 * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)
15
 
 *   then you must choose one of the following licenses before using the file:
16
 
 *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
17
 
 *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
18
 
 * }}
19
 
 *
20
 
 * {@internal Open Source relicensing agreement:
21
 
 * }}
22
 
 *
23
 
 * @author fplanque: Francois PLANQUE
24
 
 *
 
3
 * This file implements the WidgetCache class
 
4
 *
 
5
 * This is a (partial?) list of authors who have contributed to this file:
 
6
 * @author {@link http://wonderwinds.com/ Ed Bennett}
 
7
 * @author {@link http://fplanque.net/ Francois PLANQUE}
 
8
 * @copyright (c) 2009 by {@link http://quamplures.net/ the Quam Plures project}
 
9
 * @copyright (c) 2003 by Francois PLANQUE - {@link http://fplanque.net/}
 
10
 * @license {@link http://www.gnu.org/licenses/gpl.txt GNU General Public License v3}
 
11
 * @todo (1111): vars type 'foo' need useful/valid descriptions
25
12
 * @package widgets
26
13
 */
27
 
if( !defined('QP_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
28
 
 
29
 
load_class('_core/model/dataobjects/_dataobjectcache.class.php');
30
 
 
31
 
load_class( 'widgets/model/_widget.class.php' );
 
14
if(!defined('QP_MAIN_INIT')) die('fail');
32
15
 
33
16
/**
34
 
 * Widget Cache Class
35
 
 *
 
17
 * WidgetCache class
36
18
 * @package widgets
37
19
 */
38
20
class WidgetCache extends DataObjectCache
42
24
         * @var array of coll_ID => array of container_name => array of Widget
43
25
         */
44
26
        var $cache_container_Widget_array = array();
45
 
 
46
27
        /**
47
 
         * Indicates whether to load enabled widgets only.
 
28
         * Indicates whether to load enabled widgets only
48
29
         * @var boolean
49
30
         */
50
31
        var $load_enabled_only;
51
32
 
52
33
        /**
53
34
         * Constructor
54
 
         *
55
35
         * @param boolean Load enabled widgets only?
56
36
         */
57
37
        function WidgetCache( $enabled_only = false )
62
42
 
63
43
 
64
44
        /**
 
45
         * Get Widget by Blog ID
65
46
         * @param integer Collection (blog) ID
66
47
         * @return array of coll_ID => array of container_name => array of Widget
67
48
         */
68
 
        function & get_by_coll_ID( $coll_ID )
 
49
        function & get_by_blogID( $coll_ID )
69
50
        {
70
51
                global $DB;
71
52
 
72
53
                if( ! isset( $this->cache_container_Widget_array[$coll_ID] ) )
73
 
                {       // Not in Cache yet:
74
 
                        $sql = 'SELECT *
75
 
                                              FROM T_widget
76
 
                                             WHERE wi_coll_ID = '.$coll_ID;
77
 
                        if ( $this->load_enabled_only )
78
 
                        {       // We want to load enabled widgets only:
 
54
                {
 
55
                        // Not in Cache yet
 
56
                        $sql = 'SELECT * FROM T_widget WHERE wi_coll_ID = '.$coll_ID;
 
57
                        if( $this->load_enabled_only )
 
58
                        {
 
59
                                // We want to load enabled widgets only
79
60
                                $sql .= ' AND wi_enabled = 1';
80
61
                        }
81
62
                        $sql .= ' ORDER BY wi_sco_name, wi_order';
86
67
                        $count = count( $widget_rs );
87
68
                        for( $i = 0; $i < $count; $i++ )
88
69
                        {
89
 
                                // fp> NOTE: object COPYing is weird here but it needs to be like this in PHP4 or all abjects from the loop will look the same
90
 
                                if( $ComponentWidget = & $this->new_obj( $widget_rs[$i] ) ) // fp> NOTE: no copy because we need copy on the next line anyway!!
91
 
                                {       // We were able to instantiate the widget:
92
 
                                        // Add to regular cache (but not with $this->add() because we need a COPY!!):
 
70
                                // fp> NOTE: object COPYing is weird here but it needs to be like
 
71
                                // this in PHP4 or all objects from the loop will look the same
 
72
                                if( $ComponentWidget = & $this->new_obj( $widget_rs[$i] ) )
 
73
                                {
 
74
                                        // We were able to instantiate the widget, add to regular cache
 
75
                                        // (but not with $this->add() because we need a COPY!!)
93
76
                                        $this->cache[$ComponentWidget->ID] = $ComponentWidget; // COPY!!!! WEIRD BUT NECESSARY / PHP 4 (fp)
94
 
                                        // This is the cache we're interested in:
 
77
                                        // This is the cache we're interested in
95
78
                                        $this->cache_container_Widget_array[$ComponentWidget->coll_ID][$ComponentWidget->sco_name][] = & $this->cache[$ComponentWidget->ID];
96
79
                                }
97
 
 
98
 
                                // TODO: dh> try the next line, and you may be able to assign by reference to $cache or use add()
99
 
                                unset($ComponentWidget);
 
80
                                unset( $ComponentWidget );
100
81
                        }
101
 
 
102
82
                }
103
 
 
104
83
                return $this->cache_container_Widget_array[$coll_ID];
105
84
        }
106
85
 
107
86
 
108
87
        /**
109
88
         * Instanciate a new object within this cache
 
89
         * @todo (0000): from TODO_HERE, replace with dummy widget in order to give a chance to clean up
110
90
         */
111
91
        function & new_obj( $row = NULL )
112
92
        {
113
 
 
114
93
                if( $row->wi_type == 'widget' )
115
94
                {
116
 
                        if( !$this->load_widget( $row->wi_code ) )
 
95
                        if( ! $this->load_widget( $row->wi_code ) )
117
96
                        {
118
 
                                // TODO: replace with dummy widget in order to give a chance to clean up.
 
97
                                // TODO_HERE
119
98
                                $r = NULL;
120
99
                                return $r;
121
100
                        }
134
113
 
135
114
 
136
115
        /**
137
 
         * Loads appropriate widget class file.
138
 
         *
 
116
         * Loads appropriate widget class file
139
117
         * @param string $wi_code widget to load
140
 
         * @return boolean Whether the class file could be loaded. Note that this method will return true
141
 
         *                 if the file has been included before (there is no way to find out whether require_once()
142
 
         *                 really included a file).
 
118
         * @return boolean Whether the class file could be loaded. Note this method will return true if the file
 
119
         * has been included before (there is no way to find out whether require_once() really included a file)
143
120
         */
144
121
        function load_widget( $wi_code )
145
122
        {
146
 
                global $plugins_path, $Debuglog;
 
123
                global $inc_path;
147
124
 
148
 
                // Note: file_exists() should not have a big impact on IO performance since PHP caches the
149
 
                //       results.
150
 
                if( file_exists( $plugins_path.$wi_code.'_widget/_'.$wi_code.'.widget.php' ) )
151
 
                {       // widget is in its own folder
152
 
                        //$Debuglog->add( 'Loading class: '.$wi_code, 'widgets' );
153
 
                        // require_once makes sure each widget class is only loaded once.
154
 
                        require_once $plugins_path.$wi_code.'_widget/_'.$wi_code.'.widget.php';
 
125
                // file_exists() should not have a big impact on IO performance since PHP caches the results
 
126
                if( file_exists( $inc_path.'widgets/_'.$wi_code.'.widget.php' ) )
 
127
                {
 
128
                        // widget is in its own folder ... require_once makes sure each widget class is only loaded once
 
129
                        require_once $inc_path.'widgets/_'.$wi_code.'.widget.php';
155
130
                        return true;
156
131
                }
157
 
 
158
132
                return false;
159
133
        }
160
134
 
161
135
 
162
136
        /**
163
 
         * @param integer Collection (blog) ID
164
 
         * @param string Container
 
137
         * Function without a Title
 
138
         * @param integer blog ID
 
139
         * @param string Container name
165
140
         * @return array of Widget
166
141
         */
167
142
        function & get_by_coll_container( $coll_ID, $container )
168
143
        {
169
 
                // Make sure collection is loaded:
170
 
                $this->get_by_coll_ID( $coll_ID );
171
 
 
 
144
                // Make sure collection is loaded
 
145
                $this->get_by_blogID( $coll_ID );
172
146
                return $this->cache_container_Widget_array[$coll_ID][$container];
173
147
        }
 
148
 
174
149
}
 
150
 
175
151
?>