/[drupal]/contributions/modules/simplelist/SimpleListController.php
ViewVC logotype

Contents of /contributions/modules/simplelist/SimpleListController.php

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Mon May 19 03:44:37 2008 UTC (18 months, 1 week ago) by jcfiala
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +0 -0 lines
File MIME type: text/x-php
#259764 by jcfiala: Fixed badly broken caching engine, and added tests to verify the fix.
1 <?php
2 // $Id$
3
4 /**
5 * This is the controller class - it's called by the simplelist.module to load data from the simplelist tables and call the various Filter and Display
6 * classes as needed.
7 *
8 */
9 class SimpleListController {
10 private $simplelist;
11
12 /**
13 * Generates a 'blank' simplelist object - used when creating a new one.
14 *
15 * @return stdClass
16 * Blank Simplelist
17 */
18 public static function getDefault() {
19 $sl = new stdClass();
20
21 $sl->slid = 0;
22 $sl->name = '';
23 $sl->description = '';
24 $sl->access = array();
25 $sl->filter_name = 'SimpleListNodeFilter';
26 $sl->filter_data = '';
27 $sl->sort_name = 'created';
28 $sl->sort_data = 'DESC';
29 $sl->cache = BLOCK_CACHE_PER_ROLE;
30 $sl->published = SIMPLELIST_PUBLISHED_NODES;
31
32 $sl->node_types = array();
33 $sl->terms = array();
34
35 $sl->displays = array();
36
37 return $sl;
38 }
39
40 /**
41 * Loads a simplelist from the database and stores it in a simplelist object. It only gets the required data based on the current context.
42 *
43 * @param mixed $name
44 * Either the name of the simplelist, or the slid of it. Either can be used to load.
45 * @param string $context
46 * If 'block' or 'page', loads the simplelist with only those display options in the display field.
47 * If 'all', then we're modifying this list, and we load all of the display options in the displays field.
48 * @return stdClass
49 * Simplelist.
50 */
51 public static function load_simple_list($name, $context) {
52 if (is_numeric($name)) {
53 $query = "SELECT slid, name, description, access, filter_name, filter_data, sort_name, sort_data, cache, published FROM {simplelist} WHERE slid = %d";
54 }
55 else {
56 $query = "SELECT slid, name, description, access, filter_name, filter_data, sort_name, sort_data, cache, published FROM {simplelist} WHERE name = '%s'";
57 }
58 $result = db_query($query, $name);
59 if ($temp_list = db_fetch_object($result)) {
60 $temp_list->access = unserialize($temp_list->access);
61 $temp_list->node_types = array(); // default
62 $temp_list->terms = array(); // default
63
64 $query = "SELECT node_type FROM {simplelist_types} WHERE slid = %d";
65 $result = db_query($query, $temp_list->slid);
66 while ($row = db_fetch_object($result)) {
67 $temp_list->node_types[] = $row->node_type;
68 }
69
70 $query = "SELECT tid FROM {simplelist_terms} WHERE slid = %d";
71 $result = db_query($query, $temp_list->slid);
72 while ($row = db_fetch_object($result)) {
73 $temp_list->terms[] = taxonomy_get_term($row->tid);
74 }
75
76 if ($context != 'all') {
77 $query = "SELECT sldid, display_name, display_context, display_count, display_title, display_pager, display_more, display_format, display_path FROM {simplelist_display} WHERE slid = %d AND display_context = '%s'";
78 $result = db_query($query, $temp_list->slid, $context);
79 $temp_list->display = db_fetch_object($result);
80 $temp_list->display->display_more_path = ''; // initialize.
81
82 if ($context == 'block' && isset($temp_list->display->display_more) && $temp_list->display->display_more == 1) {
83 $query = "SELECT display_path FROM {simplelist_display} WHERE slid = %d AND display_context = 'page'";
84 $result = db_query($query, $temp_list->slid);
85
86 if ($row = db_fetch_object($result)) {
87 $temp_list->display->display_more_path = $row->display_path;
88 }
89 }
90 }
91 else {
92 // context is all - we're editing or doing something to this simplelist.
93 $temp_list->displays = array();
94 $query = "SELECT sldid, display_name, display_context, display_count, display_title, display_pager, display_more, display_format, display_path FROM {simplelist_display} WHERE slid = %d";
95 $result = db_query($query, $temp_list->slid, $context);
96 while ($row = db_fetch_object($result)) {
97 $temp_list->displays[$row->display_context] = $row;
98 }
99 }
100
101 return $temp_list;
102 }
103 return NULL;
104 }
105
106 /**
107 * Does a comparison between the role ids that are assigned to the simplelist->access field, and the roles that the current user has.
108 *
109 * Note that if the simplelist's access list is empty, then this simply returns true.
110 *
111 * @param array $access
112 * array of role ids
113 * @param array $roles
114 * array of role names
115 * @return boolean
116 */
117 public static function checkSimpleListAccess($access, $roles) {
118 if (!count($access)) {
119 return TRUE;
120 }
121 $access_roles = array();
122 $result = db_query("SELECT name FROM role WHERE rid IN (". db_placeholders($access) .")", $access);
123 while($row = db_fetch_object($result)){
124 $access_roles[] = $row->name;
125 }
126
127 return count(array_intersect($access_roles, $roles));
128 }
129
130 /**
131 * Constructor of SimpleListController.
132 *
133 * @param mixed $simplelist
134 * Either a simplelist that's already been constructed, or a name of a list.
135 * @param string $context
136 * The context in which we're loading the list.
137 */
138 public function __construct($simplelist, $context) {
139 if ($simplelist instanceof stdClass) {
140 $this->simplelist = $simplelist;
141 }
142 else {
143 $this->simplelist = SimpleListController::load_simple_list($simplelist, $context);
144 }
145 }
146
147 /**
148 * For testing - returns the current list.
149 *
150 * @return stdClass
151 * Simplelist
152 */
153 public function getSimpleList() {
154 return $this->simplelist;
155 }
156
157 /**
158 * The main workhorse of the process - calls the Filter and Display classes to generate the needed html.
159 *
160 * @param int $offset
161 * The offset to fetch the data by.
162 * @return mixed
163 * Either an array for blocks, or HTML for pages.
164 */
165 public function getRenderedList($offset = 0) {
166 global $user;
167 if ($this->checkSimpleListAccess($this->simplelist->access, $user->roles)) {
168 require_once(drupal_get_path('module', 'simplelist') .'/SimpleListCachingEngine.php');
169
170 // Allow our users to interrupt our work.
171 $this->callSimpleListHook('simplelist_pre_query');
172
173 $paged = $this->simplelist->display->display_context == 'page' && $this->simplelist->display->display_pager == 1;
174
175 //Get the nodes we need to display.
176 $filter = $this->getFilter($this->simplelist->filter_name, new SimpleListCachingEngine());
177 if ($filter != NULL) {
178 $data_array = $filter->get_node_list($this->simplelist, $this->simplelist->display->display_count, $offset, $paged);
179 }
180 else {
181 $data_array = array();
182 }
183
184 // Generate the html from the list of nodes.
185 $display = $this->getDisplay($this->simplelist->display->display_name);
186 if ($display != NULL) {
187 $prefix = $this->callSimpleListHookReturnString('simplelist_pre_display');
188 $data_rendered = $display->render($this->simplelist, $data_array);
189 $postfix = $this->callSimpleListHookReturnString('simplelist_post_display');
190
191 $data_rendered = $prefix . $data_rendered . $postfix;
192 }
193
194 // return the data in the format the context requires.
195 if ($this->simplelist->display->display_context == 'block') {
196 return array('subject' => $this->simplelist->display->display_title,
197 'content' => $data_rendered);
198 }
199 else {
200 return $data_rendered;
201 }
202 }
203 return '';
204 }
205
206 /**
207 * Allows other functions to impose arguments on the existing simplelist.
208 *
209 */
210 public function setArgument($name, $value) {
211 if ($name == 'tid') {
212 // set taxonomy argument.
213 if (is_numeric($value)) {
214 $term = taxonomy_get_term($value);
215 if ($term) {
216 $this->simplelist->terms = array($term);
217 }
218 }
219 else {
220 $termlist = explode(' ', $value);
221 $termarray = array();
222 foreach($termlist as $tid) {
223 $term = taxonomy_get_term($tid);
224 if ($term) {
225 $termarray[] = $term;
226 }
227 }
228 if (count($termarray)) {
229 $this->simplelist->terms = $termarray;
230 }
231 }
232 return;
233 }
234
235 if ($name == 'node_type') {
236 // Set type argument
237 $this->simplelist->node_types = array($value->type);
238 }
239 }
240
241 /**
242 * Dynamically loads the Filter, making sure that it can be loaded and that the loaded class is descended from SimpleListFilterParent
243 *
244 * @param string $filter_name
245 * The name of the class
246 * @param stdClass $caching_engine
247 * A caching engine object to pass to the new class on construct.
248 * @return SimpleListFilterParent
249 * The filter object to use.
250 */
251 public function getFilter($filter_name, $caching_engine) {
252 simplelist_require_all_filter_classes();
253 /*require_once(drupal_get_path('module', 'simplelist') .'/SimpleListNodeFilter.php');
254 require_once(drupal_get_path('module', 'simplelist') .'/SimpleListFilterNodeByUser.php');*/
255
256 if (class_exists($filter_name)) {
257 $filter = new $filter_name($caching_engine);
258 return $this->checkDynamicClass($filter, 'SimpleListFilterParent');
259 }
260 else {
261 drupal_set_message("Class $filter_name for simplelist filtering is not defined!", 'error');
262 watchdog('simplelist', 'Class @filter_name for simplelist filtering is not defined.', array('@filter_name' => $filter_name), WATCHDOG_ERROR);
263 }
264 return NULL;
265 }
266
267 /**
268 * Dynamically loads the filter, making sure that it can be loaded and that the loaded class is descended from SimpleListDisplayParent
269 *
270 * @param string $display_name
271 * Name of the class to load
272 * @return SimpleListDisplayParent
273 * The display object to use.
274 */
275 public function getDisplay($display_name) {
276 require_once(drupal_get_path('module', 'simplelist') .'/SimpleListDisplay.php');
277 require_once(drupal_get_path('module', 'simplelist') .'/SimpleListDisplayCascade.php');
278
279 if (class_exists($display_name)) {
280 $display = new $display_name();
281 return $this->checkDynamicClass($display, 'SimpleListDisplayParent');
282 }
283 else {
284 drupal_set_message("Class $display_name for simplelist display is not defined!", 'error');
285 watchdog('simplelist', 'Class @filter_name for simplelist display is not defined.', array('@filter_name' => $display_name), WATCHDOG_ERROR);
286 }
287 return NULL;
288 }
289
290 /**
291 * Checks to make sure that a given object is the class we want. If not, errors are written to dsm and watchdog.
292 *
293 * @param object $object
294 * Object to inspect
295 * @param string $class_name
296 * Desired class name
297 * @return object
298 * Either the object, or NULL, based on the result.
299 */
300 private function checkDynamicClass($object, $class_name) {
301 if (is_a($object, $class_name)) {
302 return $object;
303 }
304 else {
305 $actual_class_name = get_class($object);
306 drupal_set_message("Class $actual_class_name for simplelist is not a $class_name!", 'error');
307 watchdog('simplelist', 'Class @class_name for simplelist is not a @parent_name.', array('@class_name' => $actual_class_name, '@parent_name' => $class_name), WATCHDOG_ERROR);
308 }
309 return NULL;
310 }
311
312 /**
313 * Invokes a hook among different modules, passing in this list.
314 *
315 * @param string $hook
316 * Name of the hook to invoke.
317 */
318 private function callSimpleListHook($hook) {
319 foreach(module_implements($hook) as $module) {
320 module_invoke($module, $hook, $this->simplelist);
321 }
322 }
323
324 /**
325 * Invoke a hook among different modules, passing in this list, and accumulating the returned strings.
326 *
327 * @param string $hook
328 * Name of the hook to invoke
329 * @return string
330 * The accumulated string.
331 */
332 private function callSimpleListHookReturnString($hook) {
333 $return_string = '';
334 foreach(module_implements($hook) as $module) {
335 $return_string .= module_invoke($module, $hook, $this->simplelist);
336 }
337 return $return_string;
338 }
339 }
340 ?>

  ViewVC Help
Powered by ViewVC 1.1.2