/[drupal]/contributions/modules/context/context_ui/context_ui_admin.inc
ViewVC logotype

Contents of /contributions/modules/context/context_ui/context_ui_admin.inc

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


Revision 1.9 - (show annotations) (download) (as text)
Wed Sep 3 02:12:27 2008 UTC (14 months, 3 weeks ago) by jmiccolis
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.8: +27 -12 lines
File MIME type: text/x-php
Adding clone & override functionality to context_ui, for drupal 6 this time. Also made code E_ALL compliant and fixed other minor style issues.
1 <?php
2 // $Id: context_ui_admin.inc,v 1.8 2008/08/10 09:25:32 yhahn Exp $
3 /**
4 * Page callback for context_ui admin landing page.
5 */
6 function context_ui_admin() {
7 // Add css
8 drupal_add_css(drupal_get_path("module", "context_ui") ."/context_ui.css");
9
10 // rebuild blocks
11 _block_rehash();
12
13 // rebuild default contexts
14 context_ui_rebuild();
15
16 // User defined contexts
17 $output = "<h3>". t('User context definitions') ."</h3>";
18 $contexts = context_ui_tree('ui');
19 if ($contexts) {
20 $output .= theme('context_ui_admin', $contexts);
21 }
22 else {
23 $output .= "<p>". t('Please !add_context to get started.', array('!add_context' => l(t('add a context'), 'admin/build/context/add'))) ."</p>";
24 }
25
26 // Module defined contexts
27 $output .= "<h3>". t('Module context definitions') ."</h3>";
28 $contexts = context_ui_tree('system');
29 if ($contexts) {
30 $output .= theme('context_ui_admin', $contexts);
31 }
32 else {
33 $output .= "<p>". t('There are currently no module defined contexts.') ."</p>";
34 }
35
36 return $output;
37 }
38
39 /**
40 * Generates the omnibus context definition editing form.
41 * Note: submission and validation handlers are in context_ui_admin.inc
42 *
43 * @param $op
44 * The type of form to build. Either "add", "view" or "edit"
45 * @param $cid
46 * The db context identifier - required when $op == "edit"
47 *
48 * @return
49 * A Drupal form array.
50 */
51 function context_ui_form(&$form_state, $op, $cid = NULL, $context = NULL) {
52 drupal_add_css(drupal_get_path("module", "context_ui") ."/context_ui.css");
53 drupal_add_js(drupal_get_path("module", "context_ui") ."/context_ui.js");
54
55 switch ($op) {
56 case 'add':
57 if (is_object($context)) {
58 $context->system = true;
59 if ($exists = context_ui_context('load', $context)) {
60 drupal_set_message(t('A module is already providing a context with this space/key/value identifier. Your context definition will override its settings.'));
61 }
62 $context->system = false;
63 }
64 break;
65 case 'view':
66 if (is_numeric($cid) && $context = context_ui_context('load', $cid)) {
67 drupal_set_title(t('View %title', array('%title' => $context->value)));
68 }
69 else {
70 drupal_goto('admin/build/context'); return;
71 }
72 break;
73 case 'edit':
74 if (is_numeric($cid) && $context = context_ui_context('load', $cid)) {
75 if (!$context->system) {
76 drupal_set_title(t('Edit context: %title', array('%title' => $context->value)));
77 }
78 else {
79 drupal_goto('admin/build/context'); return;
80 }
81 }
82 break;
83 case 'clone':
84 if (is_numeric($cid) && $context = context_ui_context('load', $cid)) {
85 drupal_set_title(t('Clone context: %title', array('%title' => $context->value)));
86 $context->system = 0;
87 $context->cid = null;
88 $cid = null;
89 }
90 else {
91 drupal_goto('admin/build/context'); return;
92 }
93 break;
94 }
95
96 // Core context definition
97 $form = array(
98 '#base' => 'context_ui_form',
99 '#theme' => 'context_ui_form',
100 );
101
102 $form['value'] =
103 $form['attribute'] =
104 $form['namespace'] = array(
105 '#type' => 'textfield',
106 '#required' => true,
107 '#maxlength' => 64,
108 '#size' => 20,
109 );
110
111 $form['value']['#title'] = t('Value');
112 $form['value']['#description'] = t('A system name for this context. May only contain lowercase letters, underscores, and numbers. Example: <b>science_blog</b>');
113
114 $form['attribute']['#title'] = t('Attribute');
115 $form['attribute']['#default_value'] = 'section';
116 $form['attribute']['#description'] = t('The type of context information provided in this namespace. Example: <b>section</b>');
117
118 $form['namespace']['#title'] = t('Namespace');
119 $form['namespace']['#default_value'] = 'context_ui';
120 $form['namespace']['#description'] = t('The namespace for this context definition. Example: <b>context_ui</b>');
121
122 $form['items'] = array(
123 '#tree' => true,
124 );
125
126 // Generate settings for context item associations
127 foreach (context_ui_types('full') as $type => $item) {
128 if (in_array($item['#type'], array('select', 'radios', 'checkboxes', 'textfield'))) {
129 $form['items'][$type] = $item;
130 }
131 }
132
133 // Control block visibility
134 init_theme(); // we need to initialize theme in order to deal with blocks
135 global $theme_key;
136 $block_options =
137 $block_defaults = array();
138 $blocks = _context_ui_get_blocks();
139 $regions = system_region_list($theme_key);
140 // $blocks in [0] have not been assigned a region
141 foreach ($blocks[0] as $block) {
142 if (!isset($context->block[$block->bid])) {
143 $block_options[$block->module][$block->bid] = $block->label ." ($block->bid)";
144 }
145 }
146 ksort($block_options);
147
148 $form['block'] = array(
149 '#tree' => true,
150 );
151
152 $form['block']['selector'] = array(
153 '#description' => t('Control block visibility using context. Selected blocks will be shown when this context is set provided that custom block visibility settings and/or throttling do not hide them. Grayed out blocks are those provided by Drupal\'s standard block settings. These settings apply to the current theme and any enabled themes with regions in common.'),
154 '#type' => 'item',
155 '#tree' => true,
156 '#prefix' => '<div class="context-ui-block-selector">',
157 '#suffix' => '</div>',
158 'blocks' => array(
159 '#type' => 'select',
160 '#multiple' => true,
161 '#size' => 15,
162 '#title' => t('Blocks'),
163 '#options' => $block_options,
164 ),
165 'regions' => array(
166 '#type' => 'select',
167 '#title' => t('Regions'),
168 '#options' => $regions,
169 ),
170 'add' => array(
171 '#type' => 'markup',
172 '#value' => "<input id='edit-block-selector-add' class='form-submit' type='button' value='+ ". t('Add') ."'/>",
173 ),
174 );
175 $form['block']['regions'] = array(
176 '#type' => 'item',
177 '#tree' => true,
178 '#prefix' => '<div class="context-ui-block-regions">',
179 '#suffix' => '</div>',
180 '#value' => theme('context_ui_block_ui', $regions, $context),
181 );
182 foreach (array_keys($regions) as $region) {
183 $defaults = array();
184 $midpoint = false;
185 foreach (_context_ui_get_blocks($region, $context) as $block) {
186 if ($block->type == 'context_ui') {
187 $defaults[] = $block->bid;
188 }
189 else if (!$midpoint) {
190 $midpoint = true;
191 $defaults[] = 'system';
192 }
193 }
194 if (!$defaults) {
195 $defaults = array('system');
196 }
197 $defaults = implode(',', $defaults);
198 $form['block']['regions'][$region] = array(
199 '#type' => 'hidden',
200 '#default_value' => $defaults,
201 );
202 }
203
204 if ($op == 'view') {
205 $form['back'] = array(
206 '#type' => 'item',
207 '#value' => l(t('Back'), 'admin/build/context'),
208 );
209 }
210
211 if ($op != 'view') {
212 $form['submit'] = array(
213 '#type' => 'submit',
214 '#value' => t('Save'),
215 );
216 }
217
218 if ($op == 'edit') {
219 $form['delete'] = array(
220 '#type' => 'submit',
221 '#value' => t('Delete'),
222 );
223 }
224
225 if ($op == 'view' || $op == 'edit' || $op == 'clone' || ($op == 'add' && $context)) {
226 if ($context) {
227 $form['value']['#default_value'] = $context->value;
228 $form['attribute']['#default_value'] = $context->attribute;
229 $form['namespace']['#default_value'] = $context->namespace;
230 $form['cid'] = array(
231 '#type' => 'value',
232 '#value' => $cid,
233 );
234 $form['system'] = array(
235 '#type' => 'value',
236 '#value' => $context->system,
237 );
238 if ($op == 'view' || $context->system) {
239 $form['value']['#disabled'] =
240 $form['attribute']['#disabled'] =
241 $form['namespace']['#disabled'] =
242 $form['block']['selector']['blocks']['#disabled'] =
243 $form['block']['selector']['regions']['#disabled'] = true;
244 }
245 // Set default values for each item type (except blocks)
246 foreach (context_ui_types('full') as $type => $item) {
247 if (isset($context->{$type}) && is_array($context->{$type})) {
248 if ($item['#type'] == 'checkboxes' || ($item['#type'] == 'select' && $item['#multiple'] == true)) {
249 $defaults = array();
250 foreach ($context->{$type} as $id) {
251 $defaults[$id] = $id;
252 }
253 }
254 else {
255 $defaults = current($context->{$type});
256 }
257 $form['items'][$type]['#default_value'] = $defaults;
258 }
259 $form['items'][$type]['#disabled'] = $op == 'view' ? true : false;
260 }
261 // Blocks must be selected by region
262 if (is_array($context->block)) {
263 foreach ($regions as $region => $label) {
264 if (isset($form['block'][$region]) && is_array($form['block'][$region])) {
265 $defaults = array();
266 foreach ($form['block'][$region]['#options'] as $block => $label) {
267 if (array_search($block, $context->block) !== false) {
268 $defaults[$block] = $block;
269 }
270 }
271 $form['block'][$region]['#default_value'] = $defaults;
272 }
273 $form['block'][$region]['#disabled'] = $op == 'view' ? true : false;
274 }
275 }
276 }
277 else {
278 return drupal_goto('admin/build/context');
279 }
280 }
281
282 return $form;
283 }
284
285 /**
286 * Theme function for context_ui_form()
287 */
288 function theme_context_ui_form($form) {
289 $output = '';
290
291 // Render space / key / value trio in a 3-column table
292 $header = array(t('Namespace'), t('Attribute'), t('Value'));
293 unset($form['namespace']['#title']);
294 unset($form['attribute']['#title']);
295 unset($form['value']['#title']);
296 $rows = array(
297 array(
298 drupal_render($form['namespace']),
299 drupal_render($form['attribute']),
300 drupal_render($form['value']),
301 ),
302 );
303 $output .= theme('table', $header, $rows, array('class' => 'context-ui-3col'));
304
305 // Render setters / getters as a two column split
306 $header = array(t('Set context'), t('Respond to context'));
307 $setters = $getters = '';
308 foreach (context_ui_types('full') as $type => $item) {
309 if ($item['#context_ui'] == 'getter') {
310 $getters .= drupal_render($form['items'][$type]);
311 }
312 else {
313 $setters .= drupal_render($form['items'][$type]);
314 }
315 }
316 $rows = array(
317 array(
318 array('data' => $setters, 'class' => 'setters left'),
319 array('data' => $getters, 'class' => 'getters right'),
320 ),
321 );
322 $output .= theme('table', $header, $rows, array('class' => 'context-ui-2col'));
323
324 // Block visibility
325 $header = array(t('Blocks'), t('Regions'));
326 $rows = array(
327 array(
328 array('data' => drupal_render($form['block']['selector']), 'class' => 'left'),
329 array('data' => drupal_render($form['block']['regions']), 'class' => 'right'),
330 ),
331 );
332 $output .= theme('table', $header, $rows, array('id' => 'context-ui-blocks', 'class' => 'context-ui-2col'));
333
334 $output .= drupal_render($form);
335 return $output;
336 }
337
338 /**
339 * Provide a form to confirm deletion of a context definition.
340 */
341 function context_ui_delete_confirm(&$form_state, $cid) {
342 $context = context_ui_context('load', $cid);
343 if (!$context) {
344 return drupal_goto('admin/build/context');
345 }
346 $form['cid'] = array('#type' => 'value', '#value' => $cid);
347 $form = confirm_form($form,
348 t('Are you sure you want to delete %title?', array('%title' => $context->value)),
349 'admin/build/context',
350 t('This action cannot be undone.'),
351 t('Delete'), t('Cancel')
352 );
353 return $form;
354 }
355
356 /**
357 * Submit hook for context_ui delete confirmation form.
358 */
359 function context_ui_delete_confirm_submit($form, &$form_state) {
360 context_ui_context('delete', $form_state['values']['cid']);
361 context_ui_rebuild();
362 $form_state['redirect'] = 'admin/build/context';
363 }
364
365 /**
366 * Page callback for import form. Switches form output to context form
367 * if import submission has occurred.
368 */
369 function context_ui_import_page() {
370 if (!empty($_POST) && $_POST['form_id'] == 'context_ui_form') {
371 return drupal_get_form('context_ui_form', 'add');
372 }
373 return drupal_get_form('context_ui_import');
374 }
375
376 /**
377 * Import form. Provides simple helptext instructions and textarea for
378 * pasting a context definition.
379 */
380 function context_ui_import() {
381 drupal_set_title(t('Import context'));
382 $help = t('You can import a context definition by pasting the exported context object code into the field below.');
383 $form = array();
384 $form['help'] = array(
385 '#type' => 'item',
386 '#value' => $help,
387 );
388 $form['import'] = array(
389 '#title' => t('Context Object'),
390 '#type' => 'textarea',
391 '#rows' => 10,
392 '#required' => true,
393 );
394 $form['submit'] = array(
395 '#type' => 'submit',
396 '#value' => t('Import'),
397 );
398
399 return $form;
400 }
401
402 /**
403 * Import form submit handler. Evaluates import code and transfers to
404 * context definition form.
405 */
406 function context_ui_import_submit($form, &$form_state) {
407 $items = array();
408 if ($import = $form_state['values']['import']) {
409 ob_start();
410 eval($import);
411 ob_end_clean();
412 }
413 if (is_array($items) && count($items)) {
414 $context = current($items);
415 }
416 if (is_array($context)) {
417 $context = (object) $context;
418 $context->system = FALSE;
419 if ($exists = context_ui_context('load', $context)) {
420 drupal_set_message(t('A user-defined context definition with this space/key/value identifier already exists. Please remove the existing context before importing this definition.'), 'error');
421 $form_state['redirect'] = 'admin/build/context';
422 }
423 else {
424 drupal_set_title(t('Add context'));
425 $output = drupal_get_form('context_ui_form', 'add', null, (object) $context);
426 print theme('page', $output);
427 exit;
428 }
429 }
430 else {
431 drupal_set_message(t('An error occurred while importing. Please check your context definition.', 'error'));
432 $form_state['redirect'] = 'admin/build/context';
433 }
434 }
435
436 /**
437 * Provides a form with an exported context definition for use in modules.
438 *
439 * @param $cid
440 * A context id.
441 *
442 * @return
443 * A FormAPI array.
444 */
445 function context_ui_export(&$form_state, $cid = NULL) {
446 if (is_numeric($cid) && $context = context_ui_context('load', $cid)) {
447 drupal_set_title(t('Export %title', array('%title' => $context->value)));
448
449 // help text -- too bad the help module in 5 doesn't take wildcards
450 $help = t('You can use exported contexts in your modules by returning an array of defined contexts in <code>hook_context_define()</code>.');
451
452 // prune system specific information and cast for Drupal's AOP (array oriented programming)
453 unset($context->cid);
454 unset($context->status);
455 unset($context->system);
456 $context = (array) $context;
457
458 // clean up blocks
459 foreach ($context['block'] as $bid => $block) {
460 unset($block->bid);
461 $context['block'][$bid] = (array) $block;
462 }
463
464 // export
465 $export = '$items[] = '. var_export($context, true) .';';
466
467 // build the form
468 $form = array();
469 $form['help'] = array(
470 '#type' => 'item',
471 '#value' => $help,
472 );
473 $form['export'] = array(
474 '#type' => 'textarea',
475 '#rows' => 24,
476 '#default_value' => $export,
477 );
478 return $form;
479 }
480 else {
481 drupal_goto('admin/build/context'); return;
482 }
483 }
484
485 /**
486 * Generates an array tree representation of available space/key/value context definitions.
487 */
488 function context_ui_tree($op = '') {
489 static $tree;
490 if (!$tree) {
491 $tree = array(
492 'system' => array(),
493 'ui' => array(),
494 );
495 $result = db_query("
496 SELECT *
497 FROM {context_ui}
498 ORDER BY system ASC, namespace ASC, attribute ASC, value ASC");
499 while ($context = db_fetch_object($result)) {
500 $branch = $context->system ? 'system' : 'ui';
501 $tree[$branch][$context->namespace][$context->attribute][$context->value] = $context;
502 }
503 }
504 switch ($op) {
505 case 'ui':
506 return $tree['ui'];
507 case 'system':
508 return $tree['system'];
509 default:
510 return $tree;
511 }
512 }
513
514 /**
515 * Cache system contexts
516 */
517 function context_ui_rebuild() {
518 $default_contexts = context_ui_defaults();
519 $types = context_ui_types();
520
521 // Grab existing user defined contexts
522 $ui = context_ui_tree('ui');
523
524 // Retrieve existing system contexts
525 $system = array();
526 $result = db_query("SELECT * FROM {context_ui} WHERE system = 1");
527 while ($existing = db_fetch_object($result)) {
528 $system[$existing->cid] = true;
529 }
530
531 // Insert or update system contexts
532 foreach ($default_contexts as $c) {
533 // Check that the context definition is reasonable
534 if (!empty($c->namespace) && !empty($c->attribute)) {
535 $context = context_ui_context('load', $c);
536
537 // Check that the providing module has not explicitly set system + status
538 // If it has, we trust that the module knows what it is doing. Otherwise,
539 // flag this context's type + status.
540 if (!isset($c->system) && !isset($c->status)) {
541 $c->system = 1;
542 $c->status = isset($ui[$c->namespace][$c->attribute][$c->value]) ? 0 : 1;
543 }
544
545 if ($context) {
546 $c->cid = $context->cid;
547 context_ui_context('update', $c);
548 unset($system[$c->cid]); // remove this context from unused list
549 }
550 else {
551 context_ui_context('insert', $c);
552 }
553 }
554 }
555
556 // Remove any unused contexts
557 foreach ($system as $c => $dummy) {
558 context_ui_context('delete', $c);
559 }
560 }
561
562 /**
563 * Generates the main context_ui admin page with a tiered context listing.
564 */
565 function theme_context_ui_admin($context_tree) {
566 foreach ($context_tree as $namespace => $attributes) {
567 $rows[] = array(
568 "<span class='context-namespace'>". $namespace ."</span>",
569 null
570 );
571 foreach ($attributes as $attribute => $contexts) {
572 if (is_array($contexts)) {
573 $rows[] = array(
574 "<span class='context-attribute'>". $attribute ."</span>",
575 null
576 );
577 foreach ($contexts as $value => $context) {
578 $links = array();
579 $class = '';
580 if ($context->system) {
581 $links[] = l(t('View'), "admin/build/context/view/$context->cid");
582 $links[] = l(t('Override'), 'admin/build/context/clone/'. $context->cid);
583 }
584 else {
585 $links[] = l(t('Edit'), 'admin/build/context/edit/'. $context->cid);
586 $links[] = l(t('Export'), "admin/build/context/export/$context->cid");
587 $links[] = l(t('Clone'), 'admin/build/context/clone/'. $context->cid);
588 $links[] = l(t('Delete'), 'admin/build/context/delete/'. $context->cid);
589 }
590 if (!$context->status) {
591 $class = 'overridden';
592 $value = "$value (". t('Overridden') .")";
593 }
594 $rows[] = array(
595 "<span class='context-value $class'>". $value ."</span>",
596 implode(' | ', $links),
597 );
598 }
599 }
600 else {
601 $links = array();
602 $class = '';
603 if ($contexts->system) {
604 $links[] = l(t('View'), "admin/build/context/view/$contexts->cid");
605 }
606 else {
607 $links[] = l(t('Edit'), 'admin/build/context/edit/'. $contexts->cid);
608 $links[] = l(t('Delete'), 'admin/build/context/delete/'. $contexts->cid);
609 }
610 if ($contexts->overridden) {
611 $class = 'overridden';
612 $key = "$key (". t('Overridden') .")";
613 }
614 $rows[] = array(
615 "<span class='context-key'>". $key ."</span>",
616 implode(' | ', $links),
617 );
618 }
619 }
620 }
621 return theme('table', array(t('Context'), t('Actions')), $rows, array('class' => 'context-ui'));
622 }
623
624 /**
625 * Generates the AJAX enabled block administration portion of the context_ui admin form.
626 */
627 function theme_context_ui_block_ui($regions, $context = null) {
628 $output = '';
629
630 $tools = "<div class='tools'><span class='up'></span><span class='down'></span><span class='remove'></span></div>";
631 foreach ($regions as $region => $label) {
632 $items = array();
633
634 $system = _context_ui_get_blocks($region);
635 $system_item = array();
636 foreach ($system as $block) {
637 $system_item[] = $block->label ." ($block->bid)";
638 }
639 $system_item = implode("<br/>", $system_item);
640
641 $options = _context_ui_get_blocks($region, $context);
642 $midpoint = false;
643 if ($options) {
644 foreach ($options as $block) {
645 if ($block->type == 'context_ui' && !$context->system) {
646 $items[] = array(
647 'data' => $tools . $block->label ." ($block->bid)",
648 'title' => $block->bid,
649 );
650 }
651 else if ($block->type == 'context_ui' && $context->system) {
652 $items[] = array(
653 'data' => $block->label ." ($block->bid)",
654 'title' => $block->bid,
655 'class' => 'default',
656 );
657 }
658 else if (!$midpoint) {
659 $midpoint = true;
660 $items[] = array(
661 'data' => $system_item,
662 'title' => 'system',
663 'class' => 'disabled',
664 );
665 }
666 }
667 }
668 else if ($system_item) {
669 $items[] = array(
670 'data' => $system_item,
671 'title' => 'system',
672 'class' => 'disabled',
673 );
674 }
675 else {
676 $items[] = array(
677 'data' => '',
678 'class' => 'dummy',
679 );
680 }
681 $output .= theme('item_list', $items, $label, 'ul', array('class' => $region));
682 }
683 return $output;
684 }
685
686 /**
687 * hook_validate()
688 */
689 function context_ui_form_validate($form, &$form_state) {
690 if ($form_state['clicked_button']['#id'] == 'edit-submit' && $form_state['values']['value']) {
691 // Check for string identifier sanity
692 foreach (array('value', 'attribute', 'namespace') as $elem) {
693 if (!preg_match('!^[a-z0-9_]+$!', $form_state['values'][$elem])) {
694 form_set_error($elem, t('The context !elem can only consist of lowercase letters, underscores, and numbers.', array('!elem' => $elem)));
695 }
696 }
697 if (!isset($form_state['values']['cid'])) {
698 // Check that no other user-defined context definition has taken this identifier already
699 $context = new StdClass();
700 $context->namespace = $form_state['values']['namespace'];
701 $context->attribute = $form_state['values']['attribute'];
702 $context->value = $form_state['values']['value'];
703 $context->system = 0;
704 if ($exists = context_ui_context('load', $context)) {
705 form_set_error($form_state['values']['value'], t('A user-defined context with this space/key/value identifier already exists. Please delete the existing definition before creating a new one.'));
706 }
707 }
708 }
709 }
710
711 /**
712 * Produces a context object from submitted form values.
713 *
714 * @param $form
715 * A form array with submitted values
716 *
717 * @return
718 * A context object
719 */
720 function context_ui_form_process($form) {
721 $context = new stdClass();
722
723 // Context ns/attr/value definition
724 $context->cid = isset($form['cid']) ? $form['cid'] : null;
725 $context->system = isset($form['system']) ? $form['system'] : null;
726 $context->namespace = isset($form['namespace']) ? $form['namespace'] : null;
727 $context->attribute = isset($form['attribute']) ? $form['attribute'] : null;
728 $context->value = isset($form['value']) ? $form['value'] : null;
729 $context->status = 1; // all user defined contexts have status 1
730
731 // Values for most item types
732 $item_types = context_ui_types();
733 foreach ($item_types as $element_id) {
734 if (is_array($form['items'][$element_id])) {
735 foreach ($form['items'][$element_id] as $option => $value) {
736 if ($value) {
737 $context->{$element_id}[] = $option;
738 }
739 }
740 }
741 else if (isset($form['items'][$element_id]) && !empty($form['items'][$element_id])) {
742 $context->{$element_id}[] = $form['items'][$element_id];
743 }
744 }
745
746 // Blocks must be done by region
747 $context->block = array();
748 global $theme_key;
749 // Get list of "valid" available blocks
750 $valid = _context_ui_get_blocks();
751 $valid = $valid[0];
752 foreach (system_region_list($theme_key) as $region => $label) {
753 if ($blocks = $form['block']['regions'][$region]) {
754 $blocks = explode(',', $blocks);
755 $midpoint = array_search('system', $blocks);
756 foreach ($blocks as $position => $bid) {
757 if ($bid != 'system') {
758 $block = $valid[$bid];
759 $modifier = $position < $midpoint ? -10 : 10;
760 $block->weight = $position - $midpoint + $modifier;
761 $block->region = $region;
762 $block->type = 'context_ui';
763 $context->block[$block->bid] = $block;
764 }
765 }
766 }
767 }
768 return $context;
769 }
770
771 /**
772 * Submit handler for main context_ui form.
773 */
774 function context_ui_form_submit($form, &$form_state) {
775 switch (t($form_state['values']['op'])) {
776 // Send user to delete confirmation page
777 case 'Delete':
778 $form_state['redirect'] = 'admin/build/context/delete/'. $form_state['values']['cid'];
779 return;
780 // Process form values and save and/or update the context in the db
781 case 'Save':
782 $context = context_ui_form_process($form_state['values']);
783 if (!$context->cid) {
784 $result = context_ui_context('insert', $context);
785 if ($result) {
786 drupal_set_message(t('The context %title was saved successfully.', array('%title' => $context->value)));
787 break;
788 }
789 }
790 else if (context_ui_context('load', $context->cid)) {
791 $result = context_ui_context('update', $context);
792 if ($result) {
793 drupal_set_message(t('The context %title was saved successfully.', array('%title' => $context->value)));
794 break;
795 }
796 }
797 drupal_set_message(t('An error occurred while attempting to save your context information.'), 'error');
798 break;
799 }
800 // rebuild cache
801 context_ui_rebuild();
802 $form_state['redirect'] = 'admin/build/context';
803 }
804
805 /**
806 * Helper function to generate a list of blocks from a specified region. If provided a context object,
807 * will generate a full list of blocks for that region distinguishing between system blocks and
808 * context-provided blocks.
809 *
810 * @param $region
811 * The string identifier for a theme region. e.g. "left"
812 * @param $context
813 * A context object.
814 *
815 * @return
816 * A keyed (by "module_delta" convention) array of blocks.
817 */
818 function _context_ui_get_blocks($region = null, $context = null) {
819 global $theme_key;
820 static $block_info, $valid, $system_blocks;
821 // we don't static cache context blocks
822 $context_blocks = $blocks = array();
823
824 if (!$system_blocks) {
825 // initialize regions
826 foreach (system_region_list($theme_key) as $r => $l) {
827 $system_blocks[$r] = array();
828 }
829 // load blocks from database
830 $result = db_query("SELECT module, delta, weight, region, status FROM {blocks} WHERE theme = '%s' ORDER BY weight, module, delta", $theme_key);
831 while ($block = db_fetch_object($result)) {
832 // load block info
833 $block_info[$block->module] = isset($block_info[$block->module]) ? $block_info[$block->module] : module_invoke($block->module, 'block', 'list');
834 $block->label = $block_info[$block->module][$block->delta]['info'];
835 $block->type = 'system';
836 $block->bid = $block->module .'_'. $block->delta;
837 // add block to region
838 if ($block->region && $block->status) {
839 $system_blocks[$block->region][$block->bid] = $block;
840 }
841 else {
842 $system_blocks[0][$block->bid] = $block;
843 }
844 // mark block as available in DB
845 $valid[$block->module ."_". $block->delta] = true;
846 }
847 }
848
849 // load system blocks into main block array
850 $blocks = $system_blocks;
851
852 // load context blocks if provided
853 if (is_object($context) && is_array($context->block)) {
854 // iterate over context-associated blocks
855 foreach ($context->block as $block) {
856 $block = (object) $block;
857 // check that this is a valid block
858 if ($valid[$block->module ."_". $block->delta]) {
859 // if region has been specified, ensure that block belongs to it
860 if (!$region || (isset($region) && $block->region == $region)) {
861 // load block info
862 $block_info[$block->module] = $block_info[$block->module] ? $block_info[$block->module] : module_invoke($block->module, 'block', 'list');
863 $block->label = $block_info[$block->module][$block->delta]['info'];
864 $block->type = 'context_ui';
865 $block->bid = $block->module .'_'. $block->delta;
866 // add block to region
867 if ($block->region) {
868 $blocks[$block->region][$block->bid] = $block;
869 }
870 else {
871 $blocks[0][$block->bid] = $block;
872 }
873 }
874 }
875 }
876 }
877
878 foreach ($blocks as $r => $sort_region) {
879 if ($r !== 0) {
880 uasort($sort_region, "_context_ui_block_compare");
881 $blocks[$r] = $sort_region;
882 }
883 }
884
885 return $region ? $blocks[$region] : $blocks;
886 }

  ViewVC Help
Powered by ViewVC 1.1.2