You are here

class space_customizer_block in Spaces 6.2

Same name and namespace in other branches
  1. 6 spaces.module \space_customizer_block

Customizer for feature blocks.

Hierarchy

Expanded class hierarchy of space_customizer_block

1 string reference to 'space_customizer_block'
spaces_spaces_customizers in ./spaces.module
Implementation of hook_spaces_customizers().

File

./spaces.spaces.inc, line 127

View source
class space_customizer_block implements space_customizer {
  var $name = 'Blocks';
  var $info = array();

  /**
   * Helper method to return the block info.
   */
  function get_block_info($module, $delta) {
    if (!isset($this->info[$module])) {
      $this->info[$module] = module_invoke($module, 'block', 'list');

      // If the block is provided by Views, doctor the info to strip out the
      // view name leaving only the block's display name.
      if ($module == 'views') {
        foreach ($this->info[$module] as $k => $v) {
          $viewname = strpos($v['info'], ':');
          if ($viewname !== FALSE) {
            $v['info'] = substr($v['info'], $viewname + 2);
            $this->info[$module][$k] = $v;
          }
        }
      }
    }
    return !empty($this->info[$module][$delta]['info']) ? $this->info[$module][$delta]['info'] : '';
  }

  /**
   * Implementation of form().
   */
  function form($space, $feature) {
    global $theme_key;
    init_theme();
    $regions = system_region_list($theme_key);

    // Collect all contexts associated with this feature
    $feature_contexts = drupal_map_assoc(spaces_features_items('context', $feature));
    $contexts = context_enabled_contexts();
    $contexts = array_intersect_key($contexts, $feature_contexts);
    $master_contexts = array();
    $master_defaults = array();
    foreach ($contexts as $identifier => $context) {
      if (!empty($context->block)) {

        // Get customized values -- unfortunately we can't use our own customization
        // methods as block customization is a bit more complex.
        $customizer = !empty($space->customizer['block'][$identifier]) ? $space->customizer['block'][$identifier] : array();
        $subject = !empty($space->customizer['block']['subject']) ? $space->customizer['block']['subject'] : array();
        $form = array(
          '#title' => $identifier,
          '#tree' => TRUE,
        );
        $defaults = array(
          '#tree' => TRUE,
        );
        foreach ($context->block as $i => $block) {

          // @TODO: remove this once context is less confused about itself.
          $block = (array) $block;
          $bid = "{$block['module']}-{$block['delta']}";

          // Sanity check that this region exists
          if (!empty($block['region']) && !empty($regions[$block['region']])) {
            $region = $block['region'];
            if (!isset($form[$region])) {
              $form[$region] = array(
                '#title' => $regions[$region],
                '#tree' => TRUE,
              );
            }
            $block_details = module_invoke($block['module'], 'block', 'view', $block['delta']);
            $default_subject = !empty($block_details['subject']) ? $block_details['subject'] : '';
            $default_weight = isset($block[$i]['weight']) ? $block[$i]['weight'] : 0;
            $default_status = isset($block[$i]['status']) ? $block[$i]['status'] : 1;

            // Store defaults to compare against changed values in submit handler
            $defaults[$region][$bid]['weight'] = array(
              '#type' => 'value',
              '#value' => $default_weight,
            );
            $defaults[$region][$bid]['status'] = array(
              '#type' => 'value',
              '#value' => $default_status,
            );
            $defaults[$region][$bid]['subject'] = array(
              '#type' => 'value',
              '#value' => $default_subject,
            );
            $form[$region][$bid] = array(
              '#tree' => TRUE,
              '#weight' => isset($customizer[$region][$bid]['weight']) ? $customizer[$region][$bid]['weight'] : 0,
            );
            $form[$region][$bid]['weight'] = array(
              '#type' => 'weight',
              '#delta' => 25,
              '#default_value' => isset($customizer[$region][$bid]['weight']) ? $customizer[$region][$bid]['weight'] : 0,
            );
            $form[$region][$bid]['status'] = array(
              '#type' => 'checkbox',
              '#default_value' => isset($customizer[$region][$bid]['status']) ? $customizer[$region][$bid]['status'] : 1,
            );

            // If the block subject is empty, it's likely to be for a good reason
            // e.g. the subject is generated dynamically or the block is slimmed
            // down. Let's respect that.
            if (!empty($default_subject)) {
              $form[$region][$bid]['subject'] = array(
                '#type' => 'textfield',
                '#default_value' => !empty($subject[$bid]) ? $subject[$bid] : $default_subject,
                '#description' => $this
                  ->get_block_info($block['module'], $block['delta']),
              );
            }
            else {
              $form[$region][$bid]['subject'] = array(
                '#type' => 'markup',
                '#value' => $this
                  ->get_block_info($block['module'], $block['delta']),
              );
            }
          }
        }
        $master_contexts[$identifier] = $form;
        $master_defaults[$identifier] = $defaults;
      }
    }
    if (!empty($master_contexts) && !empty($master_defaults)) {
      $master_form = array(
        '#theme' => 'spaces_block_customizer_settings_form',
        'contexts' => array_merge(array(
          '#tree' => TRUE,
        ), $master_contexts),
        'defaults' => array_merge(array(
          '#tree' => TRUE,
        ), $master_defaults),
      );
      return $master_form;
    }
    else {
      return NULL;
    }
  }

  /**
   * Implementation of validate().
   */
  function validate($space, $feature, $value) {
    return;
  }

  /**
   * Implementation of submit().
   * Iterate through and only record the aspects of each block that have been customized.
   */
  function submit($space, $feature, $value) {
    $customizer = $space->customizer['block'];
    foreach ($value['contexts'] as $identifier => $context) {
      foreach ($context as $region => $blocks) {
        foreach ($blocks as $bid => $block) {
          foreach ($block as $k => $v) {

            // Clear out values that haven't changed from defaults
            if ($v === $value['defaults'][$identifier][$region][$bid][$k]) {
              unset($block[$k]);
            }
            else {
              if ($k == 'subject') {
                unset($block[$k]);
                $customizer['subject'][$bid] = $v;
              }
            }
          }
          if (!empty($block)) {
            $customizer[$identifier][$region][$bid] = $block;
          }
        }
      }
    }
    return $customizer;
  }

  /**
   * Implementation of customize().
   */
  function customize($space, $identifier, &$block = NULL) {

    // Unset disabled blocks and change weights based on customizer settings. See spaces_context_active_contexts_alter.
    if (!empty($space->customizer['block'][$identifier])) {
      $customizer = $space->customizer['block'][$identifier];
      foreach ($block as $key => $b) {

        // @TODO: remove this once context is less confused about itself.
        $b = (array) $b;
        $block[$key] = (array) $block[$key];
        $bid = "{$b['module']}-{$b['delta']}";

        // If the block is not enabled, yank it out of the blocks array
        if (!empty($customizer[$b['region']][$bid])) {
          if (isset($customizer[$b['region']][$bid]['status']) && $customizer[$b['region']][$bid]['status'] === 0) {
            unset($block[$key]);
          }
          if (isset($customizer[$b['region']][$bid]['weight'])) {
            $block[$key]['weight'] = $customizer[$b['region']][$bid]['weight'];
          }
        }
      }
    }
  }

  /**
   * Additional method: customize_subject() for customizing a block subject.
   */
  function customize_subject($space, &$block) {
    $bid = "{$block->module}-{$block->delta}";
    if (!empty($space->customizer['block']['subject'][$bid])) {
      $block->subject = $space->customizer['block']['subject'][$bid];
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
space_customizer_block::$info property
space_customizer_block::$name property
space_customizer_block::customize function Implementation of customize().
space_customizer_block::customize_subject function Additional method: customize_subject() for customizing a block subject.
space_customizer_block::form function Implementation of form(). Overrides space_customizer::form
space_customizer_block::get_block_info function Helper method to return the block info.
space_customizer_block::submit function Implementation of submit(). Iterate through and only record the aspects of each block that have been customized. Overrides space_customizer::submit
space_customizer_block::validate function Implementation of validate(). Overrides space_customizer::validate