/[drupal]/contributions/modules/color_soc08/color.database.inc
ViewVC logotype

Contents of /contributions/modules/color_soc08/color.database.inc

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


Revision 1.13 - (show annotations) (download) (as text)
Fri Aug 22 10:21:56 2008 UTC (15 months ago) by skiquel
Branch: MAIN
CVS Tags: HEAD
Changes since 1.12: +320 -44 lines
File MIME type: text/x-php
head commit
1 <?php
2 // $Id: color.database.inc,v 1.6.2.4 2008/08/22 08:15:30 skiquel Exp $
3 /**
4 * @file
5 * Database magician functions
6 */
7
8 /**
9 * Retrieve schemes through various methods
10 * @param $method: can be set to 'scheme_id', 'hex', 'uid', 'theme' for default scheme
11 * @param $vars: the ID, unserialized hex sep. by comma, uid, theme name.
12 * 'hex' and 'scheme_name' require array('hex' => $hex, 'theme' => $theme), so on.
13 * @param $select: can be used to retrieve only selected columns
14 */
15 function color_get_scheme($method, $vars, $columns = NULL) {
16 static $static = array();
17
18 $vars_serialized = (is_array($vars)) ? implode(', ', $vars) : $vars;
19 if (isset($columns)) {
20 $select = (is_array($columns)) ? implode(', ', $columns) : $columns;
21 }
22 else {
23 $select = '*';
24 }
25
26 if (isset($static[$method][$vars_serialized][$select])) {
27 return $static[$method][$vars_serialized][$select];
28 }
29
30 // Switch between our scheme methods
31 switch ($method):
32
33 case 'scheme_id':
34 $result = db_query("SELECT %s FROM {color_schemes} cs ".
35 "WHERE cs.id = '%s'", $select, $vars);
36 break;
37 case 'uid':
38 $result = db_query("SELECT %s FROM {color_users} cu ".
39 "RIGHT JOIN {color_schemes} cs ON cu.scheme_id = cs.id ".
40 "WHERE cu.uid = '%s'", $select, $vars);
41 break;
42 case 'hex':
43 $result = db_query('SELECT '.$select.' FROM {color_schemes} cs '.
44 'WHERE cs.theme = "'.$vars['theme'].'" AND cs.hex = \''.$vars['hex'].'\'');
45 break;
46 case 'scheme_name':
47 $result = db_query('SELECT '.$select.' FROM {color_schemes} cs '.
48 'WHERE cs.theme = "'.$vars['theme'].'" AND cs.name = \''.$vars['scheme_name'].'\'');
49 break;
50 case 'theme':
51 $result = db_query("SELECT %s FROM {color_schemes} cs ".
52 "WHERE cs.id IN ".
53 "(SELECT c.value FROM {color} c WHERE ".
54 "c.name = 'default_scheme' and c.theme = '%s')", $select, $vars);
55 break;
56 endswitch;
57
58 if ($rows = db_fetch_array($result)) {
59 $static[$method][$vars_serialized][$select] = $rows;
60 $scheme = TRUE;
61 }
62
63
64 return $scheme ? $rows : FALSE;
65 }
66
67 /**
68 * Set UID or anonymous user to a scheme
69 * @param $scheme_id
70 * Integer of scheme ID.
71 * @param $vars
72 * Optional array. Scheme info to update or add.
73 * @param $method
74 * Optional string. 'update', 'delete', 'insert'
75 */
76 function color_set_scheme($scheme_id, $vars = NULL, $method = NULL) {
77 if (!isset($method)) {
78 $results = db_fetch_array(db_query("SELECT COUNT(*) AS count FROM {color_schemes} WHERE id = '%s'", $scheme_id));
79 if ($results['count'] > 0) {
80 $method = 'update';
81 }
82 else {
83 $method = 'insert';
84 }
85 }
86
87 if ($method == 'insert' && isset($vars)) {
88 db_query('INSERT INTO {color_schemes} (name, theme, hex, status) VALUES (\'%s\', \'%s\', \'%s\', \'%s\')', $vars['name'], $vars['theme'], serialize($vars['hex']), '2');
89 $last_id = db_last_insert_id('color_schemes', 'id');
90 color_add_scheme($vars['theme'], $vars['hex'], $last_id, $vars['name']);
91 drupal_set_message("Scheme <em>".$vars['name']."</em> added.", "info");
92 return $last_id;
93 }
94 elseif ($method == 'update') {
95 db_query('UPDATE {color_schemes} SET extra_attr = \'%s\', hex = \'%s\', name = \'%s\' WHERE id = \'%s\'', serialize($vars['extra_attr']), serialize($vars['hex']), $vars['schemename'], $scheme_id);
96 }
97 elseif ($method == 'delete' && isset($scheme_id)) {
98 db_query('DELETE FROM {color_schemes} WHERE id = \'%s\'', $scheme_id);
99 drupal_set_message('Scheme deleted.', "info");
100 return "deleted";
101 }
102
103 }
104
105 /**
106 * Set UID or anonymous user to a scheme
107 * @param $scheme_id: the scheme ID
108 * @param $uid: User's ID
109 * @param $method: 'update', 'delete', 'insert'
110 */
111 function color_set_user_scheme($scheme_id, $uid = 0, $method = NULL) {
112 if (!isset($method)) {
113 $results = db_fetch_array(db_query("SELECT COUNT(*) AS count FROM {color_users} WHERE uid = '%s'", $uid));
114 if ($results['count'] > 0) {
115 $method = 'update';
116 }
117 else {
118 $method = 'insert';
119 }
120 }
121
122 if ($uid == 0) {
123 if ($method == 'delete') {
124 unset($_SESSION['scheme_id']);
125 }
126 else {
127 $_SESSION['scheme_id'] = $scheme_id;
128 }
129 }
130 elseif (isset($scheme_id) && $uid != 0) {
131 if ($method == 'update') {
132 db_query('UPDATE {color_users} SET scheme_id = \'%s\' WHERE uid = \'%s\'', $scheme_id, $uid);
133 drupal_set_message(t('Your color scheme selection has been modified in the DB.'), "info");
134 }
135 elseif ($method == 'insert') {
136 db_query('INSERT INTO {color_users} (uid, scheme_id) VALUES (\'%s\', \'%s\')', $uid, $scheme_id);
137 drupal_set_message('Your color scheme selection has been added to the DB.', "info");
138 }
139 elseif ($method == 'delete') {
140 db_query('DELETE FROM {color_users} WHERE uid = \'%s\'', $uid);
141 drupal_set_message('Your color scheme selection has been removed.', "info");
142 }
143 }
144 }
145
146 /**
147 * Check if scheme_id matches up with a real scheme
148 * @param $scheme_id: the scheme ID
149 * @param $theme: the theme name, used if trying to match scheme_id to theme
150 */
151 function color_scheme_id_valid($scheme_id, $theme = NULL) {
152 static $static;
153
154 if (!isset($theme) && isset($static[$scheme_id])) {
155 return $static[$scheme_id];
156 }
157 elseif (isset($theme) && isset($static[$scheme_id][$theme])) {
158 return $static[$scheme_id][$theme];
159 }
160
161 if ($scheme = color_get_scheme('scheme_id', $scheme_id, 'theme')) {
162 if ((isset($theme)) && ($theme != $scheme['theme'])) {
163 $static[$scheme_id][$theme] = FALSE;
164 return $static[$scheme_id][$theme];
165 }
166 else {
167 $static[$scheme_id][$theme] = TRUE;
168 return $static[$scheme_id][$theme];
169 }
170 }
171 else {
172 $static[$scheme_id] = FALSE;
173 return FALSE;
174 }
175 }
176
177 /**
178 * Is system info for theme generated in {color}?
179 * Returns TRUE or FALSE
180 */
181 function color_theme_info_generated($theme) {
182 static $static;
183
184 if (isset($static[$theme])) {
185 return $static[$theme];
186 }
187
188 $result = db_fetch_array(db_query("SELECT COUNT(*) AS count FROM {color} WHERE theme = '%s'", $theme));
189 $rows = $result['count'];
190
191 if ($rows == 0) {
192 $static[$theme] = FALSE;
193 }
194 else {
195 $static[$theme] = TRUE;
196 }
197
198 return $static[$theme];
199 }
200
201 /**
202 * Are default schemes in color.inc generated for theme?
203 * Returns TRUE or FALSE.
204 */
205 function color_theme_scheme_generated($theme) {
206 static $static;
207
208 if (isset($static[$theme])) {
209 return $static[$theme];
210 }
211
212 $result = db_fetch_array(db_query("SELECT COUNT(*) AS count FROM {color_schemes} WHERE theme = '%s'", $theme));
213 $schemes = $result['count'];
214
215 if ($schemes == 0) {
216 $static[$theme] = FALSE;
217 }
218 else {
219 $static[$theme] = TRUE;
220 }
221
222 return $static[$theme];
223 }
224
225 /**
226 * Check if color info for color themes are installed in DB and generated.
227 * Used in color.install for all themes and in admin/build/themes/settings/{theme}
228 * Alternate use: check to see if schemes are generated. If not, returns false.
229 * @param $theme: Theme name
230 */
231 function color_check_themes($theme = '') {
232 static $checked;
233
234 module_load_include('inc', 'color', 'color.misc');
235
236 if (isset($checked) && $checked == TRUE) {
237 return;
238 }
239
240 // Check to see if colortheme are in DB for theme yet
241 if (empty($theme)) {
242 $themes = color_list_colorable_themes();
243 }
244 else {
245 $themes[] = $theme;
246 }
247
248 module_load_include('module', 'color', 'color');
249
250 foreach ($themes as $theme) {
251 $theme_generated = color_theme_info_generated($theme);
252 $scheme_generated = color_theme_scheme_generated($theme);
253
254 if ($theme_generated === FALSE || $scheme_generated === FALSE) {
255 $info = color_get_theme_info($theme, 'inc');
256
257 if (!color_validate($info)) {
258 return;
259 }
260 else {
261 if ($theme_generated === FALSE) {
262 color_add_theme($theme);
263 }
264 if ($scheme_generated === FALSE) {
265 color_add_schemes($theme);
266 }
267 }
268 }
269
270 }
271
272 $checked = TRUE;
273 }
274
275 /**
276 * Add color theme info to database
277 * @param $theme: Theme name
278 */
279 function color_add_theme($theme) {
280 module_load_include('module', 'color', 'color');
281 $info = color_get_theme_info($theme, "inc");
282
283 // Add fields to db
284 db_query('INSERT INTO {color} (name, theme, value) VALUES (\'%s\', \'%s\', \'%s\')', "fields", $theme, serialize($info['fields']));
285
286 // Add other info to db
287 $otherkeys = array('engine', 'css', 'copy', 'blend_target', 'preview', 'img');
288 foreach ($otherkeys as $key) {
289 if (isset($info[$key])) {
290 $storage[$key] = $info[$key];
291 db_query('INSERT INTO {color} (name, theme, value) VALUES (\'%s\', \'%s\', \'%s\')', $key, $theme, serialize($storage[$key]));
292 }
293 }
294
295 db_query('INSERT INTO {color} (name, theme, value) VALUES (\'%s\', \'%s\', \'%s\')', 'default_scheme', $theme, $info['default_scheme']);
296
297 db_query('INSERT INTO {color} (name, theme, value) VALUES (\'%s\', \'%s\', \'%s\')', 'reference_scheme', $theme, serialize($info['schemes'][$info['reference_scheme']]));
298
299 drupal_set_message("Added information for <em>{$theme}</em> to color database", "status");
300 }
301
302 /**
303 * Add pre-made themes into DB
304 * @param $theme: Theme name
305 */
306 function color_add_schemes($theme) {
307 module_load_include('module', 'color', 'color');
308 $info = color_get_theme_info($theme, "inc");
309
310 foreach ($info['schemes'] as $name => $hex) {
311
312 // Add colors to database
313 db_query('INSERT INTO {color_schemes} (name, theme, hex, status) VALUES (\'%s\', \'%s\', \'%s\', \'%s\')', $name, $theme, serialize($hex), '2');
314 $last_id = db_last_insert_id('color_schemes', 'id');
315 // drupal_set_message($info['default_scheme']. " == {$name} == {$last_id}");
316
317 if (strstr($info['default_scheme'], $name)) {
318 db_query('UPDATE {color} SET value = \'%s\' WHERE theme = \'%s\' AND name = \'default_scheme\'', $last_id, $theme);
319 }
320 else {
321 $result = db_fetch_array(db_query("SELECT COUNT(*) AS count FROM {color} WHERE theme = '%s' AND name = 'default_scheme'", $theme));
322 $count = $result['count'];
323
324 if ($count < 1) {
325 db_query('INSERT INTO {color} (name, theme, value) VALUES (\'%s\', \'%s\', \'%s\')', "default_scheme", $theme, $last_id);
326 }
327 }
328
329 $schemes[] = array(
330 'theme' => $theme,
331 'hex' => $hex,
332 'last_id' => $last_id,
333 'name' => $name,
334 'palette' => '',
335 );
336
337 //$theme, $schemehex, $scheme_id, $palette = ''
338 }
339 foreach ($schemes as $value) {
340 color_add_scheme($value['theme'], $value['hex'], $value['last_id'], $value['name']);
341 }
342
343 drupal_set_message("Added schemes for <em>{$theme}</em> to color schemes table.", "status");
344 }
345
346 /**
347 * Retrieve the color info for a theme.
348 *
349 * @param $theme
350 * A string for the theme name
351 * @param $source
352 * An optional string. 'sql' or 'inc'.
353 * Typically 'inc' is used for generated scheme before DB is populated for schemes
354 * Typically 'sql' is for current, generated schemes
355 * @param $columns
356 * An optional array or string of what columns to return. If none, will return all rows.
357 * Currently not fully implemented, will default to all rows.
358 * @return
359 * Array of theme info. Referred to in other functions as $info.
360 */
361 function color_get_theme_info($theme, $source = "sql", $columns = NULL) {
362 static $static;
363
364 module_load_include('inc', 'color', 'color.misc');
365
366 // Cache results for these parameters.
367 if (isset($columns)) {
368 $select = (is_array($columns)) ? implode(', ', $columns) : $columns;
369 }
370 else {
371 $select = '*';
372 }
373
374 if (isset($static[$theme][$source][$select])) {
375 return $static[$theme][$source][$select];
376 }
377
378 if ($source == "inc") {
379 $path = drupal_get_path('theme', $theme);
380 $file = $path .'/color/color.inc';
381 if ($path && file_exists($file)) {
382 include $file;
383 }
384
385 // For flexibility, we're allowing certain schemes to work without color replacement and rely on base changing. Can be removed. Designed to work specially for 'shift' mode where only one scheme (the reference) needs all fields, the rest just need a base.
386 foreach ($info['schemes'] as $key => $v) {
387 $fieldcount = count($info['fields']);
388 $commas = substr_count($v, ',');
389 if ($commas < $fieldcount - 1) {
390 $commastoadd = ($fieldcount - 1) - $commas;
391 for ($count = 0; $count < $commastoadd; $count += 1) {
392 $v .= ",";
393 }
394 }
395 $info['schemes'][$key] = explode(',', $v);
396 }
397 // End flexi-code
398 }
399 elseif ($source == "sql") {
400 if (isset($theme)) {
401 $info['theme'] = $theme;
402 }
403
404 // Get information regarding theme
405 $result = db_query("SELECT %s FROM {color} WHERE theme = '%s'", $select, $theme);
406 while ($row = db_fetch_array($result)) {
407
408 if ($row['name'] == 'default_scheme') {
409 $info[$row['name']] = $row['value'];
410 }
411 else {
412 $info[$row['name']] = unserialize($row['value']);
413 }
414 }
415
416 // Get color schemes for theme
417 $result = db_query("SELECT cs.id, cs.name, cs.hex FROM {color_schemes} cs WHERE theme = '%s'", $theme);
418 while ($row = db_fetch_array($result)) {
419 // Get color schemes
420 $info['schemes'][$row['name']] = unserialize($row['hex']);
421 $info['scheme_ids'][$row['name']] = $row['id'];
422 }
423 ksort($info['schemes']);
424 }
425
426 $info['theme'] = $theme;
427 $static[$theme][$source][$select] = $info;
428 return $info;
429 }
430
431 /**
432 * Return scheme information via UID and theme
433 *
434 * @param $uid
435 * Optional drupal user ID. UID 0 is unregistered user.
436 * @return
437 * ARRAY. Scheme information.
438 */
439 function color_find_scheme_by_UID($uid = 0, $theme, $columns = NULL) {
440
441 // Cache results for these parameters.
442 if (isset($columns)) {
443 $select = (is_array($columns)) ? implode(', ', $columns) : $columns;
444 }
445 else {
446 $select = '*';
447 }
448
449 if (isset($static[$uid][$theme][$select])) {
450 return $static[$uid][$theme][$select];
451 }
452
453 if ($uid == 0) { // Unregistered users
454 if (isset($_SESSION['scheme_id'])) {
455 $scheme_id = $_SESSION['scheme_id'];
456 }
457 else {
458 $scheme_id = 0;
459 }
460
461 }
462 elseif ($uid != 0) { // Registered users
463 if ($scheme = color_get_scheme('uid', $uid)) {
464 $scheme_id = $scheme['id'];
465 }
466 else {
467 $scheme_id = 0;
468 }
469 }
470
471 // $scheme_id 0 is default site scheme for theme
472 if ($scheme_id == 0 || !color_scheme_id_valid($scheme_id, $theme)) {
473 $scheme = color_get_scheme('theme', $theme);
474 $page_scheme_id = $scheme['id'];
475 }
476 else {
477 $page_scheme_id = $scheme_id;
478 }
479
480 $scheme = color_get_scheme('scheme_id', $page_scheme_id);
481 $scheme['id'] = $scheme_id;
482 $scheme['page_id'] = $page_scheme_id;
483
484 $static[$uid][$theme][$select] = $scheme;
485 return $static[$uid][$theme][$select];
486 }
487
488 /**
489 * Delete theme schemes, in DB and in scheme files.
490 *
491 * @param $theme
492 * String of theme name
493 * @param $regen
494 * Optional boolean, if you do not want to regenerate schemes.
495 */
496 function color_store_reset($theme, $regen = TRUE) {
497 module_load_include('inc', 'color', 'color.misc');
498
499 $info = color_get_theme_info($theme, 'inc');
500 if (!color_validate($info)) {
501 return;
502 }
503
504 db_query("DELETE FROM {color} WHERE theme = '%s'", $theme);
505 db_query("DELETE FROM {color_schemes} WHERE theme = '%s'", $theme);
506 db_query("DELETE FROM {variable} WHERE name LIKE 'color_%s%'", $theme);
507
508 // Check color_users table. Clean out picks that don't match schemes.
509 $result = db_query("SELECT scheme_id FROM {color_users}", $theme);
510 while ($row = db_fetch_array($result)) {
511 $scheme = color_get_scheme('scheme_id', $row['scheme_id'], 'theme');
512 if ($scheme['theme'] != $theme) {
513 db_query("DELETE FROM {color_users} WHERE scheme_id = '%s'", $row['scheme_id']);
514 }
515 }
516
517 module_load_include('inc', 'color', 'color.misc');
518 color_rm_dir(file_directory_path() .'/color', $theme);
519 drupal_set_message(t('Colors Reset.'), 'status');
520
521 if ($regen == TRUE) {
522 color_check_themes($theme);
523 }
524 }
525
526 /**
527 * Retrieve the reference scheme palette for a particular theme.
528 *
529 * @param $theme
530 * String of theme name
531 * @param $info
532 * Optional array of theme info (usually OK to leave this blank)
533 * @return
534 * Array of reference palette. Field names as keys, with corresponding hex code.
535 */
536 function color_get_reference($theme, $info = NULL) {
537 static $palette = array();
538 if (isset($palette[$which][$theme])) {
539 return $palette[$which][$theme];
540 }
541
542 if (isset($info)) {
543 $info = color_get_theme_info($theme);
544 }
545
546 $scheme_name = ($scheme = color_get_scheme('scheme_id', $info['reference_scheme'], 'name')) ? $scheme['name'] : NULL;
547
548 $palette[$theme] = array_combine($info['fields'], $info['reference_scheme']);
549
550 return $palette[$theme];
551 }

  ViewVC Help
Powered by ViewVC 1.1.2