You are here

views_showcase.module in Views Showcase 7

Same filename and directory in other branches
  1. 6.2 views_showcase.module
  2. 6 views_showcase.module

The implementation of Views Showcase module.

File

views_showcase.module
View source
<?php

/**
 * @file
 *  The implementation of Views Showcase module.
 */

/**
 * Implementation of hook_views_api().
 */
function views_showcase_views_api() {
  return array(
    'api' => '3.0',
  );
}

/**
 * Implementation of template preprocess for the view.
 */
function template_preprocess_views_showcase_view(&$vars) {
  $view = $vars['view'];
  $options = $view->style_plugin->options;

  // Values to pass to the template
  $vars['skin'] = $options['skin'];
  $vars['views_showcase_id'] = 'views-showcase-' . $view->name . '-' . $view->current_display;

  // Passing the display title, translated if i18nviews is enabled
  if (isset($view->display[$view->current_display]->display_options['title'])) {
    $untranslated_title = $view->display[$view->current_display]->display_options['title'];

    // For i18nviews integration
    $i18nstrings_context = 'views:' . $view->name . ':' . $view->current_display . ':title';
  }
  else {
    $untranslated_title = $view->display['default']->display_options['title'];

    // For i18nviews integration
    $i18nstrings_context = 'views:' . $view->name . ':default:title';
  }
  if (function_exists('i18nstrings')) {
    $vars['display_title'] = i18nstrings($i18nstrings_context, $untranslated_title);
  }
  else {
    $vars['display_title'] = $untranslated_title;
  }
  drupal_add_js(array(
    'views_showcase' => array(
      'easing' => $options['easing'],
      'cycle' => $options['cycle'],
      'sync' => $options['sync'],
      'timeout' => $options['timeout'],
      'listPause' => $options['listpause'],
      'pause' => $options['pause'],
    ),
  ), 'setting');
  $libs = views_showcase_get_libraries();
  drupal_add_js($libs['cycle']);
  if (!empty($options['easing'])) {
    drupal_add_js($libs['easing']);
  }
  drupal_add_js(drupal_get_path('module', 'views_showcase') . '/js/views_showcase.js', 'file');
  drupal_add_css(drupal_get_path('module', 'views_showcase') . '/css/views_showcase.css', array(
    'type' => 'file',
    'weight' => 5,
    'group' => CSS_THEME,
  ));
  $is_row_odd = TRUE;

  //Looping through the rows returned by the view to pass them in a proper structure to the template
  foreach ($view->result as $row_index => $row_to_render) {

    //Adding classes
    $classes = array();
    $classes[] = 'views-row';
    $classes[] = 'views-row-' . ($row_index + 1);
    if ($is_row_odd) {
      $classes[] = 'views-row-odd';
    }
    else {
      $classes[] = 'views-row-even';
    }
    if ($row_index === 0) {
      $classes[] = 'views-row-first';
    }
    if ($row_index === count($view->result) - 1) {
      $classes[] = 'views-row-last';
    }
    $nav_box_classes = $classes;
    $nav_box_classes[] = 'views-showcase-pager-item';
    $nav_box_classes = implode(' ', $nav_box_classes);
    $big_box_classes = $classes;
    $big_box_classes[] = 'views-showcase-item';
    $big_box_classes = implode(' ', $big_box_classes);
    $vars['structured_rows'][$row_index]['nav_box_classes'] = $nav_box_classes;
    $vars['structured_rows'][$row_index]['big_box_classes'] = $big_box_classes;
    $vars['structured_rows'][$row_index]['anchor_name'] = 'showcase-' . $vars['views_showcase_id'] . '-' . $row_index;
    $is_row_odd = !$is_row_odd;

    //Looping through the fields of each returned row
    foreach ($view->field as $id => $field) {

      // Render this even if set to exclude so it can be used elsewhere.
      $field_output = $view->field[$id]
        ->theme($row_to_render);
      if (empty($field->options['exclude'])) {
        $object = new stdClass();
        $object->content = $field_output;
        $object->handler =& $view->field[$id];
        $object->element_type = $object->handler
          ->element_type();

        //$object->class = views_css_safe($id);
        $object->label = check_plain($view->field[$id]
          ->label());

        //First assume this field doesn't appear anywhere
        $object->big_box = FALSE;
        $object->navigation_box = FALSE;
        $object->link_anchor = FALSE;

        //Setting the flags for each field in the row according to the what was chosen in the settings
        if (isset($options['showcase_display_options']['big_box_field'][$id]) && $options['showcase_display_options']['big_box_field'][$id] != '0') {
          $object->big_box = TRUE;
        }
        if (isset($options['showcase_display_options']['link_anchor_field'][$id]) && $options['showcase_display_options']['link_anchor_field'][$id] != '0') {
          $object->link_anchor = TRUE;
        }
        if (isset($options['showcase_display_options']['navigation_box_field'][$id]) && $options['showcase_display_options']['navigation_box_field'][$id] != '0') {
          $object->navigation_box = TRUE;
        }

        //Now send the structured row objects to the template
        $vars['structured_rows'][$row_index][$id] = $object;
      }
    }
  }
}

/**
 * Implementation of hook_theme().
 */
function views_showcase_theme() {
  return array(
    'views_showcase_checkbox_table' => array(
      'render element' => 'element',
    ),
  );
}

/**
 * Theming the style options form to render the table with the checkboxes used to choose where to display each field.
 */
function theme_views_showcase_checkbox_table($element) {

  //Build the header of the table to be rendered
  $header = array(
    t('Field ID'),
    t('Big Box'),
    t('Navigation Box'),
    t('Link to Anchor'),
  );
  $element = reset($element);

  //Build the content of the table
  foreach ($element['navigation_box_field']['#options'] as $field_name => $field) {
    $row = array();

    //First Column the name of the field
    $row[] = $field;

    //Emptying the labels so that they aren't rendered for each checkbox in the table
    $element['big_box_field'][$field_name]['#title'] = '';
    $element['navigation_box_field'][$field_name]['#title'] = '';
    $element['link_anchor_field'][$field_name]['#title'] = '';

    //Now using the rendered HTML of each field and add it to the table
    $row[] = drupal_render($element['big_box_field'][$field_name]);
    $row[] = drupal_render($element['navigation_box_field'][$field_name]);
    $row[] = drupal_render($element['link_anchor_field'][$field_name]);

    //Stack all rows in one array
    $rows[] = $row;
  }

  //Render as an HTML table and return
  return theme('table', array(
    'header' => $header,
    'rows' => $rows,
  ));
}

/**
 * Function to check and retreive libraries.
 */
function views_showcase_get_libraries() {
  $libraries['cycle'] = NULL;
  $libraries['easing'] = NULL;
  if ($path = libraries_get_path('cycle')) {
    $file = reset(file_scan_directory($path, "/.*.js/"));
    $libraries['cycle'] = $file->uri;
  }
  if ($path = libraries_get_path('easing')) {
    $file = reset(file_scan_directory($path, "/.*.js/"));
    $libraries['easing'] = $file->uri;
  }
  return $libraries;
}

Functions

Namesort descending Description
template_preprocess_views_showcase_view Implementation of template preprocess for the view.
theme_views_showcase_checkbox_table Theming the style options form to render the table with the checkboxes used to choose where to display each field.
views_showcase_get_libraries Function to check and retreive libraries.
views_showcase_theme Implementation of hook_theme().
views_showcase_views_api Implementation of hook_views_api().