removed too over-cleverness from the fetch* functions
[project/panels.git] / panels_page / panels_page.read.inc
CommitLineData
e92b022b
SB
1<?php
2// $Id$
3
4/**
5 * @file panels_page.write.inc
6 *
7 * Functions responsible for writing panels_page data to the database.
8 */
9
10/**
11 * Fetch all panel pages in the system.
12 *
13 * For the moment, this function does not cache. It may be integrated into the
14 * panels_page_loaded_cache() caching mechanism at some point, but its utility
15 * in D6 is significantly lessened vs. D5, so there is much less need to do so.
16 */
17function panels_page_load_all($names = array(), $page_size = 0) {
18 $pages = $dids = array();
19 $query = "SELECT * FROM {panels_page}";
20
21 if (!empty($names)) {
22 $query .= " WHERE name IN (" . implode(', ', array_fill(0, count($names), "'%s'")) . ")";
23 }
24
25 if ($page_size) {
26 $result = pager_query($query, $page_size, 0, $names);
27 }
28 else {
29 $result = db_query($query, $names);
30 }
31
32 while ($page = db_fetch_object($result)) {
33 $page->access = ($page->access ? explode(', ', $page->access) : array());
34 $page->arguments = (!empty($page->arguments)) ? unserialize($page->arguments) : array();
35 $page->displays = (!empty($page->displays)) ? unserialize($page->displays) : array();
36 $page->contexts = (!empty($page->contexts)) ? unserialize($page->contexts) : array();
37 $page->relationships = (!empty($page->relationships)) ? unserialize($page->relationships) : array();
38 $page->switcher_options = (!empty($page->switcher_options)) ? unserialize($page->switcher_options) : array();
39 $page->type = t('Local');
40 $page->loader_flags = (int) $page->loader_flags;
41
42 $pages[$page->name] = panels_page_sanitize($page);
43 }
44
45 $status = variable_get('panel_page_defaults', array());
46 foreach (panels_page_default_panels() as $page) {
47 // Determine if default panel is enabled or disabled.
48 if (isset($status[$page->name])) {
49 $page->disabled = $status[$page->name];
50 }
51
52 if (!empty($pages[$page->name])) {
53 $pages[$page->name]->type = t('Overridden');
54 }
55 else {
56 $page->type = t('Default');
57 $pages[$page->name] = $page;
58 }
59 }
60 return $pages;
61}
62
63/**
64 * Load a panel page.
65 */
66function panels_page_load($id, $load_display = FALSE) {
67 $cache = panels_page_loaded_cache($id);
68
69 if ($cache) {
f04127d7
SB
70 // This is deprecated; ideally the fetcher should be called directly
71 if ($load_display && empty($cache->primary)) {
72 panels_page_fetch_primary_display($cache);
e92b022b
SB
73 }
74 return $cache;
75 }
76
77 if (!is_numeric($id)) {
78 $where = "name = '%s'";
79 }
80 else {
81 $where = 'pid = %d';
82 }
83 $page = db_fetch_object(db_query("SELECT * FROM {panels_page} WHERE $where", $id));
84 if (!$page) {
85 $defaults = panels_page_default_panels();
86 if (isset($defaults[$id])) {
87 // Default panel pages will always only be identified by name, so no need
88 // for the both-ids-point-to-same-object trick. And, they're in code, so
89 // no real need to statically cache them, either.
90 $page = $defaults[$id];
91 return $page;
92 }
93 return;
94 }
95
96 $page->access = ($page->access ? explode(', ', $page->access) : array());
97 $page->arguments = (!empty($page->arguments)) ? unserialize($page->arguments) : array();
98 $page->displays = (!empty($page->displays)) ? unserialize($page->displays) : array();
99 $page->contexts = (!empty($page->contexts)) ? unserialize($page->contexts) : array();
100 $page->relationships = (!empty($page->relationships)) ? unserialize($page->relationships) : array();
101 $page->switcher_options = (!empty($page->switcher_options)) ? unserialize($page->switcher_options) : array();
102 $page->loader_flags = (int) $page->loader_flags;
103
104 if ($load_display) {
105 $page->primary = panels_load_display($page->did);
f04127d7 106 // By default, we set the primary display as the current display.
4553a213 107 $page->display = &$page->primary;
e92b022b
SB
108 }
109
110 panels_page_loaded_cache($id, $page);
111 return $page;
112}
113
114function panels_page_loaded_cache($id, $panel_page = NULL) {
115 static $cache = array();
116 if (is_object($panel_page)) {
117 $cache[$id] = $panel_page;
118 // Make sure that we've statically cached the loaded page for both possible
119 // unique identifiers - $page->pid AND $page->name.
120 $other_id = is_numeric($id) ? $panel_page->name : $panel_page->pid;
4553a213 121 $cache[$other_id] = &$cache[$id];
e92b022b
SB
122 }
123 return array_key_exists($id, $cache) ? $cache[$id] : FALSE;
124}
125
e92b022b
SB
126/**
127 * Get all 'default' panels.
128 *
129 * @ingroup HookInvokers
130 */
131function panels_page_default_panels() {
132 $panels = module_invoke_all('default_panel_pages');
133 if (!is_array($panels)) {
134 $panels = array();
135 }
136
137 return $panels;
138}
139
140
141function panels_page_set_current(&$panel_page) {
142 static $cache = NULL;
143 // Only allow it to be set once per request.
144 if (!isset($cache) && isset($panel_page)) {
4553a213 145 $cache = &$panel_page;
e92b022b
SB
146 }
147 return $cache;
148}
149
150/**
b2d16575 151 * Load a display into the 'current display' position, $panel_page->current.
4ea1e488
SB
152 *
153 * The family of panels_page_fetch*() functions are specifically dedicated to
b2d16575
SB
154 * retrieving a particular display and placing them in $panels_page->display,
155 * which is the 'current' display upon which all other operations act.
4ea1e488
SB
156 * via reference to its permanent home in the $panels_page object. The
157 * permanent homes vary:
158 *
159 * -# For the primary display, that location is $panels_page->primary.
160 * -# For alternate default displays, that location is in
b098869b 161 * $panels_page->alternates['defaults'][$did]. TODO this isn't true right now
4ea1e488
SB
162 * -# For alternate non-default displays, that location is in
163 * $panels_page->alternates['all'][$did].
b098869b 164 *
b2d16575
SB
165 * The structure of this function family essentially means that no other
166 * panels_page function should ever act on anything except $panel_page->display.
167 *
b098869b
SB
168 * @param object $panel_page
169 * @param string $id
170 * A string of the format generated by panels_argument_get_display().
171 * @return mixed
172 * Can return NULL or TRUE. Most of the substantial operations are performed
173 * by reference. NULL returns indicate that the fetched display corresponds
174 * exactly to the one requested by $id; TRUE return values indicate that the
175 * requested $id does not have its own display associated with it, and so
176 * a default (either an alternate default or the primary display) were
177 * returned in its stead.
e92b022b 178 */
4ea1e488 179function panels_page_fetch_display(&$panel_page, $id = NULL) {
e8f3a038
SB
180 // Redirect to the appropriate fetcher depending on whether $id is provided
181 empty($id) ? panels_page_fetch_primary_display($panel_page) : panels_page_fetch_alternate_display($panel_page, $id);
b2d16575 182}
e92b022b 183
b2d16575
SB
184/**
185 * Internal panels_page API function; directs the current display
186 * ($panel_page->display) to the primary display ($panel_page->primary),
187 * loading the primary display if necessary.
188 *
189 * @param object $panel_page
190 */
191function panels_page_fetch_primary_display(&$panel_page) {
192 // Commented out is a more precise way of telling, but it's probably moot.
193 // if (empty($panel_page->primary) || !is_a($panel_page->primary, 'panels_display')) {
194 if (empty($panel_page->primary) || !is_object($panel_page->primary)) {
195 $panel_page->primary = panels_load_display($panel_page->did);
e92b022b 196 }
b2d16575
SB
197 $panel_page->display = &$panel_page->primary;
198 $panel_page->current = 'primary';
199 // Update the cache.
200 panels_page_loaded_cache($panel_page->name, $panel_page);
201}
e92b022b 202
b2d16575
SB
203function panels_page_fetch_alternate_display(&$panel_page, $id) {
204 $info = $panel_page->displays[$id];
205 $requested_display_exists = panels_page_fetch_display_from_info($panel_page, $info, $id);
206 if (!$requested_display_exists) {
207 // The requested display does not exist. First, we try to fall back to the
208 // default display for the context we're switching on.
209 if (!empty($info['default']) && !empty($panel_page->displays[$info['default']])) {
210 panels_page_fetch_display_from_info($panel_page, $panel_page->displays[$info['default']], $id);
211 }
212 // Otherwise, use the primary display. Load it if it's not already loaded.
213 else {
214 panels_page_fetch_primary_display($panel_page);
215 }
e92b022b 216 }
e92b022b
SB
217 return TRUE;
218}
219
220/**
221 * Get a display based on whether it's already in code or needs to be loaded.
222 */
b2d16575 223function panels_page_fetch_display_from_info(&$panel_page, $info, $id) {
4ea1e488 224 $ret = NULL;
e92b022b
SB
225 // If the 'display' is set it's the result of an export/default
226 if (isset($info['display'])) {
b2d16575
SB
227 $panel_page->display = $info['display'];
228 $panel_page->current = $id;
229 return TRUE;
e92b022b
SB
230 }
231
232 if (is_numeric($info['did'])) {
b2d16575
SB
233 $panel_page->display = panels_load_display($info['did']);
234 $panel_page->current = $id;
4ea1e488
SB
235 return TRUE;
236 }
e92b022b
SB
237 return FALSE;
238}
239
240/**
241 * Determine if the specified user has access to a panel.
242 */
243function panels_page_access($panel_page, $account = NULL) {
244 if (!$account) {
245 global $user;
246 $account = $user;
247 }
248
249 // Administrator privileges
250 if (user_access('access all panel-pages', $account)) {
251 return TRUE;
252 }
253
254 // All panels displays with an empty access setting are available to all roles.
255 if (empty($panel_page->access) || !is_array($panel_page->access)) {
256 return TRUE;
257 }
258
259 // Otherwise, check roles
260 static $roles = array();
261 if (!isset($roles[$account->uid])) {
262 $roles[$account->uid] = array_keys($account->roles);
263 $roles[$account->uid][] = $account->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID;
264 }
265
266 return array_intersect($panel_page->access, $roles[$account->uid]);
267}
268