You are here

function views_patterns in Patterns 6.2

Same name and namespace in other branches
  1. 5 components/views.inc \views_patterns()
  2. 6 components/views.inc \views_patterns()

File

components/views.inc, line 4

Code

function views_patterns($op, $id = null, &$data = null) {
  switch ($op) {

    // Return the valid tags that this component can prepare and process
    case 'tags':
      return array(
        'view',
      );
      break;

    // Return a list of forms/actions this component can handle
    case 'actions':
      return array();
      break;

    // Prepare data for processing
    case 'prepare':

      // In fact, this step is all we'll do, as we're totally
      // sidestepping FAPI
      views_include('view');

      // legacy: support details format
      if ($details = $data['details']) {
        $data = array_merge($data, $details);
      }

      // "tag" is reserved in patterns
      $data['tag'] = $data['tags'];

      // load the view, or create a new one
      if (!($view = views_get_view($data['name']))) {
        $view = views_new_view();
        $view->name = $data['name'];
      }

      // prepare the displays so we can work with them
      $view
        ->init_display();

      // allowed attributes
      $attributes = array(
        'description' => 'description',
        'tag' => 'tag',
        'base' => 'base_table',
      );

      // set values on the view
      foreach ($attributes as $data_key => $key) {
        if (!($value = $data[$data_key])) {
          continue;
        }
        if (is_array($value)) {
          $value = array_merge((array) $view->{$key}, $value);
        }
        $view->{$key} = $value;
      }

      // displays
      foreach ($data['display'] as &$display_data) {

        // initialize data arrays
        foreach (explode(' ', 'section item options filters sorts') as $key) {
          if (!$display_data[$key]) {
            $display_data[$key] = array();
          }
        }

        // if there's no display_id, the display_id is the name
        $display_id = $display_data['display_id'] ? $display_data['display_id'] : $display_data['name'];

        // the display plugin is the id without the _N
        $display_plugin = $display_id;
        if ($pos = strpos($display_plugin, "_")) {
          $display_plugin = substr($display_plugin, 0, $pos);
        }

        // what shall the title be?
        $default_title = $display_id == 'default' ? 'Defaults' : ucfirst($display_plugin);

        // legacy: if display_title is set, use that
        $display_title = $display_data['display_title'] ? $display_data['display_title'] : $default_title;

        // if title is set, use that
        $display_title = $display_data['options']['title'] ? $display_data['options']['title'] : $display_title;

        // load the view, or create a new one
        if ($display = $view->display[$display_id]) {
          $handler = $display->handler;
          $display->display_title = $display_title;
        }
        else {
          $handler = $view
            ->new_display($display_plugin, $display_title, $display_id);
        }

        // legacy: support section syntax
        foreach ($display_data['section'] as $section) {
          $display_data['options'][$section['section']] = $section['value'];
        }

        // override options with supplied data
        foreach ($display_data['options'] as $key => $value) {
          $handler
            ->override_option($key, $value);
        }

        // legacy: support item syntax
        foreach ($display_data['item'] as &$item) {

          // name stores table and field together as table.field
          list($table, $field) = explode('.', $item['name']);

          // collect the table and field into the passed data
          $item['data'] = array_merge((array) $item['data'], array(
            'table' => $table,
            'field' => $field,
          ));

          // valid item types: sort, filter, argument, relationship, field
          $collection_name = $item['type'] . 's';
          $display_data[$collection_name][$field] = $item['data'];
        }

        // apply complex display options
        foreach (explode(' ', 'filters sorts relationships arguments fields') as $key) {
          if ($option = $display_data[$key]) {
            $handler
              ->override_option($key, $option);
          }
        }
      }

      // persist the view
      $view
        ->save();
      break;

    // Pre validate actions
    case 'pre-validate':

      // if (empty($data['view_object'])) {
      //   return t('Failed to find the view definition.');
      // }
      break;

    // Return the form_id('s) for each action
    case 'form_id':
      return array();
      break;

    // Validate the values for an action before running the pattern
    case 'validate':
      break;

    // Prepare for valid processing of this type of component
    case 'build':
      return null;
      break;

    // Build a patterns actions and parameters
    case 'params':
      break;
    case 'cleanup':
      break;
    case 'identifier':
      break;
  }
}