You are here

function template_preprocess_draggableviews_view_draggabletable in DraggableViews 7

Same name and namespace in other branches
  1. 6.3 draggableviews_theme.inc \template_preprocess_draggableviews_view_draggabletable()
  2. 6 draggableviews_theme.inc \template_preprocess_draggableviews_view_draggabletable()
  3. 6.2 draggableviews_theme.inc \template_preprocess_draggableviews_view_draggabletable()

Implements template_preprocess_HOOK().

File

./draggableviews_theme.inc, line 12
Theme functions.

Code

function template_preprocess_draggableviews_view_draggabletable(&$vars, $style_plugin_name) {

  // Copy/Modification BEGIN from views/theme/theme.inc,v 1.84.2.14 (2010/06/16). Lines 304-404.
  $view = $vars['view'];

  // We need the raw data for this grouping, which is passed in as $vars['rows'].
  // However, the template also needs to use for the rendered fields.  We
  // therefore swap the raw data out to a new variable and reset $vars['rows']
  // so that it can get rebuilt.
  // Store rows so that they may be used by further preprocess functions.
  $result = $vars['result'] = $vars['rows'];
  $vars['rows'] = array();
  $options = $view->style_plugin->options;
  $handler = $view->style_plugin;
  $fields =& $view->field;
  $columns = $handler
    ->sanitize_columns($options['columns'], $fields);
  $active = !empty($handler->active) ? $handler->active : '';
  $order = !empty($handler->order) ? $handler->order : 'asc';
  $query = tablesort_get_query_parameters();

  // Fields must be rendered in order as of Views 2.3, so we will pre-render
  // everything.
  $renders = $handler
    ->render_fields($result);
  foreach ($columns as $field => $column) {

    // render the header labels
    if ($field == $column && empty($fields[$field]->options['exclude'])) {
      $label = check_plain(!empty($fields[$field]) ? $fields[$field]
        ->label() : '');
      if (empty($options['info'][$field]['sortable']) || !$fields[$field]
        ->click_sortable()) {
        $vars['header'][$field] = $label;
      }
      else {

        // @todo -- make this a setting
        $initial = 'asc';
        if ($active == $field && $order == 'asc') {
          $initial = 'desc';
        }
        $title = t('sort by @s', array(
          '@s' => $label,
        ));
        if ($active == $field) {
          $label .= theme('tablesort_indicator', array(
            'style' => $initial,
          ));
        }
        $query['order'] = urlencode($field);
        $query['sort'] = $initial;
        $link_options = array(
          'html' => TRUE,
          'attributes' => array(
            'title' => $title,
          ),
          'query' => $query,
        );
        $vars['header'][$field] = l($label, $_GET['q'], $link_options);
      }
    }

    // Create a second variable so we can easily find what fields we have and what the
    // CSS classes should be.
    $vars['fields'][$field] = drupal_clean_css_identifier($field);
    if ($active == $field) {
      $vars['fields'][$field] .= ' active';
    }

    // Add a CSS align class to each field if one was set
    if (!empty($options['info'][$field]['align'])) {
      $vars['fields'][$field] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']);
    }

    // Render each field into its appropriate column.
    foreach ($result as $num => $row) {
      if (!empty($fields[$field]) && empty($fields[$field]->options['exclude'])) {
        $field_output = $renders[$num][$field];

        // Don't bother with separators and stuff if the field does not show up.
        if (empty($field_output) && !empty($vars['rows'][$num][$column])) {
          continue;
        }

        // Place the field into the column, along with an optional separator.
        if (!empty($vars['rows'][$num][$column])) {
          if (!empty($options['info'][$column]['separator'])) {
            $vars['rows'][$num][$column] .= filter_xss_admin($options['info'][$column]['separator']);
          }
        }
        else {
          $vars['rows'][$num][$column] = '';
        }
        $vars['rows'][$num][$column] .= $field_output;
      }
    }
  }
  $count = 0;
  foreach ($vars['rows'] as $num => $row) {
    $vars['row_classes'][$num][] = $count++ % 2 == 0 ? 'odd' : 'even';
  }
  $vars['row_classes'][0][] = 'views-row-first';
  $vars['row_classes'][count($vars['row_classes']) - 1][] = 'views-row-last';
  $vars['classes_array'] = array(
    'views-table',
  );
  if (!empty($options['sticky'])) {
    drupal_add_js('misc/tableheader.js');
    $vars['classes_array'][] = "sticky-enabled";
  }
  $vars['classes_array'][] = 'cols-' . count($vars['rows']);

  // Copy/Modification END.
  // Draggableviews output begin
  $vars['tabledrag_table_id'] = 'draggableview_' . $view->name . '_' . $vars['id'];
  $info = $view->draggableviews_info;
  $form_elements =& $view->draggableviews_form_elements;
  if (!isset($info['order'])) {
    return;
  }
  $vars['tabledrag_type'] = array();

  // loop through all rows the view returned
  foreach ($vars['rows'] as $i => $row) {
    $nid = $result[$i]->{$view->base_field};

    // build indentation (as tabledrag requires)
    $indentation = theme('indentation', array(
      'size' => $info['nodes'][$nid]['depth'],
    ));
    if (strcmp($view->base_table, 'node') == 0) {

      // Get node object.
      $node = node_load($nid);
      if (isset($info['types'][$node->type])) {

        // set node type specification, if available (e.g. tabledrag-root)
        $vars['tabledrag_type'][$i] = 'tabledrag-' . $info['types'][$node->type];
      }
    }

    // Tabledrag needs all concerned fields to be input elements.
    // The values of the input elements will be changed by tabledrag while
    // dragging the rows. As we want to use these values we need to
    // give them names.
    //
    // Concerned fields are
    //  *) the order field
    //  *) the parent field (if hierarchy is used)
    // This for loop should prevent copy->paste'ing code.
    foreach (array(
      'hierarchy',
      'order',
    ) as $modus) {
      if (isset($info[$modus])) {
        if ($modus == 'hierarchy') {

          // get the field that should be rendered
          $field = $info['hierarchy']['field'];
        }
        else {

          // get the field that should be rendered
          $field = $info['order']['field'];
        }

        // Get the form element. The field_ prefix prevents a possible conflict
        // with the hidden_nid field.
        $form_element =& $form_elements[$i]['field_' . $field['field_name']];

        // Render the new form element.
        $vars['rows'][$i][$field['field_name']] = drupal_render($form_element);
        if (!user_access('Allow Reordering') || $info['locked']) {

          // The tabledrag js would hide the input fields (depending on the settings). But the
          // tabledrag js will not be added (either because of missing permissions or because
          // the hierarchy is locked), so we have to hide the input fields on our own.
          if (!$info[$modus]['visible']) {
            $vars['style'][$field['field_name']] = 'display: none;';
          }
        }
      }
    }
    if (isset($info['hierarchy'])) {

      // Put indentation in front of first field and append rendered hidden node id
      // that we need for tabledrag and expand/collapse.
      $hidden_nid =& $form_elements[$i]['hidden_nid'];
      $key = key($vars['rows'][$i]);
      $vars['rows'][$i][$key] = $indentation . $vars['rows'][$i][$key] . drupal_render($hidden_nid);
    }

    // Mark extended nodes.
    $vars['draggableviews_extended'][$i] = !empty($info['nodes'][$nid]['extension']);

    // Let extension modules alter the output.
    foreach (module_implements('draggableviews_row_alter') as $module) {
      $function = $module . '_draggableviews_row_alter';
      $function($vars['rows'][$i], $nid, $info, $view);
    }
  }
  if (user_access('Allow Reordering') && !$info['locked']) {

    // Prepare tabledrag settings for output.
    $tabledrag = array();
    if (isset($info['order'])) {
      $tabledrag[] = array(
        'action' => 'order',
        'relationship' => 'sibling',
        'source' => $info['order']['field']['field_name'],
        'group' => $info['order']['field']['field_name'],
        'subgroup' => $info['order']['field']['field_name'],
        'hidden' => !$info['order']['visible'],
        'limit' => 0,
      );
    }
    if (isset($info['hierarchy'])) {
      $tabledrag[] = array(
        'action' => 'match',
        'relationship' => 'parent',
        'source' => 'hidden_nid',
        'group' => $info['hierarchy']['field']['field_name'],
        'subgroup' => $info['hierarchy']['field']['field_name'],
        'hidden' => !$info['hierarchy']['visible'],
        'limit' => $info['depth_limit'],
      );
    }

    // Add tabledrag behavior.
    foreach ($tabledrag as $drag) {
      drupal_add_tabledrag($vars['tabledrag_table_id'], $drag['action'], $drag['relationship'], $drag['group'], $drag['subgroup'], $drag['source'], $drag['hidden'], $drag['limit']);
    }
  }
  if (isset($info['hierarchy'])) {

    // Fetch expand information from database.
    $uid = $info['expand_links']['by_uid'] ? $user->uid : 0;
    $result = db_query("SELECT parent_nid, collapsed FROM {draggableviews_collapsed} WHERE uid = :uid AND view_name = :view_name", array(
      ':uid' => $uid,
      ':view_name' => $view->name,
    ));
    $states = array();
    foreach ($result as $state) {
      $states[$state->parent_nid] = $state->collapsed;
    }

    // Check if "expand" links should be shown.
    if ($info['expand_links']['show']) {
      drupal_add_js(drupal_get_path('module', 'draggableviews') . '/draggableviews.js');

      // Let javascript know about the parent field.
      drupal_add_js(array(
        'draggableviews' => array(
          $vars['tabledrag_table_id'] => array(
            'parent' => $info['hierarchy']['field']['field_name'],
          ),
        ),
      ), array(
        'type' => 'setting',
        'scope' => JS_DEFAULT,
      ));
      if (empty($states)) {

        // let js know whether child nodes should be expanded or not
        drupal_add_js(array(
          'draggableviews' => array(
            $vars['tabledrag_table_id'] => array(
              'expand_default' => $options['tabledrag_expand']['collapsed'] ? 1 : 0,
              'states' => array(),
            ),
          ),
        ), array(
          'type' => 'setting',
          'scope' => JS_DEFAULT,
        ));
      }
      else {
        drupal_add_js(array(
          'draggableviews' => array(
            $vars['tabledrag_table_id'] => array(
              'states' => $states,
            ),
          ),
        ), array(
          'type' => 'setting',
          'scope' => JS_DEFAULT,
        ));
      }
    }
  }
  drupal_add_css(drupal_get_path('module', 'draggableviews') . '/styles.css');
}