You are here

function draggableviews_plugin_style_draggabletable::validate in DraggableViews 7

Same name and namespace in other branches
  1. 6.3 views/draggableviews_plugin_style_draggabletable.inc \draggableviews_plugin_style_draggabletable::validate()

Validate that the plugin is correct and can be saved.

Return value

array An array of error strings to tell the user what is wrong with this plugin.

Overrides views_plugin_style::validate

File

views/draggableviews_plugin_style_draggabletable.inc, line 39
Draggableviews style plugin definition.

Class

draggableviews_plugin_style_draggabletable
Style plugin to render each item as a row in a draggable table; Inherits all from views_plugin_table.

Code

function validate() {
  $errors = parent::validate();
  if (!isset($this->options['tabledrag_order'])) {
    return $errors;
  }
  $fields = $this->view
    ->get_items('field', $this->display->id);
  $sorts = $this->view
    ->get_items('sort', $this->display->id);
  $handlers = $this->display->handler
    ->get_handlers('field');

  // Check if all configured fields are available.
  if (isset($this->options['tabledrag_order'])) {
    if ($this->options['tabledrag_order']['field'] != 'none' && !isset($fields[$this->options['tabledrag_order']['field']])) {
      $errors[] = t('Display "@display": Draggableviews: order field <i>@field</i> could not be found (..afterwards removed from Fields section?). Check your settings.', array(
        '@display' => $this->display->display_title,
        '@field' => $this->options['tabledrag_order']['field'],
      ));
      return $errors;
    }
  }
  if (isset($this->options['tabledrag_hierarchy'])) {
    if ($this->options['tabledrag_hierarchy']['field'] != 'none' && !isset($fields[$this->options['tabledrag_hierarchy']['field']])) {
      $errors[] = t('Display "@display": Draggableviews: hierarchy field <i>@field</i> could not be found (..afterwards removed from Fields section?). Check your settings.', array(
        '@display' => $this->display->display_title,
        '@field' => $this->options['tabledrag_hierarchy']['field'],
      ));
      return $errors;
    }
  }

  // Check if first field is draggableviews handled and hidden.
  $found = FALSE;

  // Get the first configured field.
  $first_field = key($fields);
  if (isset($this->options['tabledrag_order'])) {
    if ($this->options['tabledrag_order']['field'] == $first_field && $this->options['tabledrag_order_visible']['visible'] !== 'visible') {
      $found = TRUE;
    }
  }
  if (isset($this->options['tabledrag_hierarchy'])) {
    if ($this->options['tabledrag_hierarchy']['field'] == $first_field && $this->options['tabledrag_hierarchy_visible']['visible'] !== 'visible') {
      $found = TRUE;
    }
  }
  if (!empty($fields[$first_field]['exclude'])) {
    $found = TRUE;
  }
  if ($found) {
    $errors[] = t('Display "@display": Draggableviews: The drag-handles will be attached to the first field. But the currently configured first field will not be shown. Move the currently configured first field to another position or choose <i>Show input fields?</i> on the settings page.', array(
      '@display' => $this->display->display_title,
    ));
  }

  // Check sort criteria.
  if (isset($this->options['tabledrag_order']['field']) && $this->options['tabledrag_order']['field'] != 'none') {
    $order_field = $fields[$this->options['tabledrag_order']['field']];

    // Change the name for next validation check.
    if (!strcmp($this->options['tabledrag_order']['handler'], 'fieldapi')) {
      $order_field['field'] .= '_value';
    }

    // Get the first sort criteria.
    $sort = current($sorts);
    if (strcmp($order_field['table'] . $order_field['field'], $sort['table'] . $sort['field']) != 0 || isset($sort['order']) && $sort['order'] == 'DESC') {
      $errors[] = t('Display "@display": Draggableviews: You must sort by <i>@group: @title</i> ascending as the first sort criteria to display the structure correctly.', array(
        '@display' => $this->display->display_title,
        '@group' => $handlers[$this->options['tabledrag_order']['field']]->definition['group'],
        '@title' => $handlers[$this->options['tabledrag_order']['field']]->definition['title'],
      ));
    }
  }

  // Check depth limit.
  $limit = $this->options['draggableviews_depth_limit'];
  if (!(is_numeric($limit) && $limit >= -1)) {
    $errors[] = t('Depth limit must be a numeric value >= 1');
  }

  // Check page extensions.
  if (!is_numeric($this->options['draggableviews_extensions']['extension_top']) || $this->options['draggableviews_extensions']['extension_top'] < 0) {
    $errors[] = t('Display "@display": Draggableviews: Number rows of last page must be a numeric value >= 0.', array(
      '@display' => $this->display->display_title,
    ));
  }
  if (!is_numeric($this->options['draggableviews_extensions']['extension_bottom']) || $this->options['draggableviews_extensions']['extension_bottom'] < 0) {
    $errors[] = t('Display "@display": Draggableviews: Number rows of previous page must be a numeric value >= 0.', array(
      '@display' => $this->display->display_title,
    ));
  }

  // Check button text.
  if (!(isset($this->options['draggableviews_button_text']) && strlen($this->options['draggableviews_button_text']) > 0)) {
    $errors[] = t('Display "@display": Draggableviews: Button text must be a valid string.', array(
      '@display' => $this->display->display_title,
    ));
  }
  return $errors;
}