context_layouts_reaction_block.inc

Go to the documentation of this file.
00001 <?php
00002 
00003 class context_layouts_reaction_block extends context_reaction_block {
00004   /**
00005    * Override of is_enabled_region().
00006    * Check that there is an active layout and it supports the given region.
00007    */
00008   protected function is_enabled_region($region) {
00009     $layout = $this->get_active_layout();
00010     if ($layout && isset($layout['regions']) && is_array($layout['regions'])) {
00011       return in_array($region, $layout['regions'], TRUE) && parent::is_enabled_region($region);
00012     }
00013     return parent::is_enabled_region($region);
00014   }
00015 
00016   /**
00017    * Retrieve the first layout specified found by any active contexts.
00018    */
00019   function get_active_layout($info = TRUE) {
00020     $contexts = $this->get_contexts();
00021     $layouts = context_layouts_get_layouts();
00022     if (!empty($contexts) && !empty($layouts)) {
00023       foreach ($contexts as $context) {
00024         $values = $this->fetch_from_context($context);
00025         if (isset($values['layout']) && isset($layouts[$values['layout']])) {
00026           return $info ? $layouts[$values['layout']] : $values['layout'];
00027         }
00028       }
00029     }
00030     // Fallback to default layout if provided.
00031     if (isset($layouts['default'])) {
00032       return $info ? $layouts['default'] : 'default';
00033     }
00034     return FALSE;
00035   }
00036 
00037   /**
00038    * Add the layout template to page vars.
00039    */
00040   function add_layout_template(&$vars) {
00041     if ($layout = $this->get_active_layout()) {
00042       if (!empty($layout['template'])) {
00043         global $theme;
00044         $vars['theme_hook_suggestion'] = "page__context_layouts_{$theme}_{$layout['layout']}";
00045       }
00046     }
00047   }
00048 
00049   /**
00050    * Add the layout stylesheet to the CSS.
00051    */
00052   function add_layout_stylesheet() {
00053     if ($layout = $this->get_active_layout()) {
00054       if (!empty($layout['stylesheet'])) {
00055         drupal_add_css(drupal_get_path('theme', $layout['theme']) . '/' . $layout['stylesheet']);
00056       }
00057     }
00058   }
00059 
00060   /**
00061    * Override of editor form.
00062    */
00063   function editor_form($context) {
00064     drupal_add_css(drupal_get_path('module', 'context_layouts') . '/plugins/context_layouts_reaction_block.css');
00065 
00066     $form = parent::editor_form($context);
00067 
00068     if ($layouts = $this->get_layout_options()) {
00069       $options = $this->fetch_from_context($context);
00070       $form['layout'] = array(
00071         // #tree *must* be true for our values to be nested correctly.
00072         '#tree' => TRUE,
00073         '#prefix' => '<div class="context-editor-block-layouts">',
00074         '#suffix' => '</div>',
00075         '#weight' => -100,
00076         'layout' => array(
00077           '#title' => t('Layout'),
00078           '#options' => $layouts,
00079           '#type' => 'select',
00080           '#weight' => -100,
00081           '#default_value' => isset($options['layout']) ? $options['layout'] : NULL,
00082           '#required' => FALSE,
00083           '#empty_value' => 0,
00084           '#empty_option' => '- ' . t('Site default') . ' -',
00085         ),
00086         'update' => array(
00087           '#value' => t('Change layout'),
00088           '#type' => 'submit',
00089         ),
00090       );
00091     }
00092     return $form;
00093   }
00094 
00095   /**
00096    * Override of editor form submit.
00097    */
00098   function editor_form_submit(&$context, $values) {
00099     // Someone has changed the layout, assume that the block values are not actually usable here.
00100     if (isset($context->reactions['block']['layout']) && $context->reactions['block']['layout'] != $values['layout']['layout']) {
00101       $options = $context->reactions['block'];
00102     }
00103     else {
00104       $options = parent::editor_form_submit($context, $values);
00105     }
00106 
00107     if (!empty($values['layout']['layout'])) {
00108       $options['layout'] = $values['layout']['layout'];
00109     }
00110     else {
00111       unset($options['layout']);
00112     }
00113     return $options;
00114   }
00115 
00116   /**
00117    * Override of options form.
00118    */
00119   function options_form($context) {
00120     $form = parent::options_form($context);
00121     $options = $this->fetch_from_context($context);
00122 
00123     // Only alter the options form if the theme provides layouts.
00124     $theme_key = variable_get('theme_default', 'garland');
00125     $layouts = $this->get_layout_options();
00126     if (!empty($layouts)) {
00127       $form['layout'] = array(
00128         '#title' => t('Layout'),
00129         '#description' => t('Choose one of the layouts provided by the default theme.'),
00130         '#options' => $layouts,
00131         '#type' => 'select',
00132         '#weight' => -100,
00133         '#default_value' => !empty($options['layout']) ? $options['layout'] : NULL,
00134         '#attributes' => array('class' => array('context-blockform-layout')),
00135         '#required' => FALSE,
00136         '#empty_value' => 0,
00137         '#empty_option' => '- ' . t('Site default') . ' -',
00138       );
00139 
00140       // Add js.
00141       // @TODO: Move this to a theme function or somewhere that will get called even
00142       // if the form is using a cached version of itself (e.g. when validate fails).
00143       drupal_add_js(drupal_get_path('module', 'context_layouts') . '/plugins/context_layouts_reaction_block.js');
00144       drupal_add_js(array('contextLayouts' => array('layouts' => $this->get_layout_regions())), 'setting');
00145     }
00146     return $form;
00147   }
00148 
00149   /**
00150    * Override of submit handler.
00151    */
00152   function options_form_submit($values) {
00153     $options = parent::options_form_submit($values);
00154 
00155     // Only alter the options form if the theme provides layouts.
00156     $theme_key = variable_get('theme_default', 'garland');
00157     $layouts = context_layouts_get_layouts($theme_key);
00158 
00159     // Check that this is a valid layout.
00160     if (!empty($values['layout']) && isset($layouts[$values['layout']])) {
00161       $layout = $values['layout'];
00162       $options['layout'] = $layout;
00163 
00164       // Remove blocks that don't belong to regions in this layout.
00165       if (isset($layouts[$layout]['regions'])) {
00166         foreach ($options['blocks'] as $bid => $block) {
00167           if (!in_array($block['region'], $layouts[$layout]['regions'])) {
00168             unset($options['blocks'][$bid]);
00169           }
00170         }
00171       }
00172     }
00173     return $options;
00174   }
00175 
00176   /**
00177    * Get layout options for the given theme.
00178    */
00179   protected function get_layout_options($theme_key = NULL) {
00180     $theme_key = !isset($theme_key) ? variable_get('theme_default', 'garland') : $theme_key;
00181     $layouts = context_layouts_get_layouts($theme_key);
00182     $layout_options = array();
00183     if (!empty($layouts)) {
00184       foreach ($layouts as $layout => $info) {
00185         $layout_options[$layout] = isset($info['name']) ? $info['name'] : $layout_options;
00186       }
00187     }
00188     return $layout_options;
00189   }
00190 
00191   /**
00192    * Get a layout to region map for the given theme.
00193    */
00194   protected function get_layout_regions($theme_key = NULL) {
00195     $theme_key = !isset($theme_key) ? variable_get('theme_default', 'garland') : $theme_key;
00196     $layouts = context_layouts_get_layouts($theme_key);
00197     if (!empty($layouts)) {
00198       $layout_regions = array();
00199       foreach ($layouts as $layout => $info) {
00200         $layout_regions[$layout] = is_array($info['regions']) ? $info['regions'] : array();
00201       }
00202     }
00203     return $layout_regions;
00204   }
00205 }

Generated on Sat May 25 02:23:38 2013 for Context by  doxygen 1.4.7