You are here

class i18nviews_plugin_argument_validate_i18n_taxonomy_term in Internationalization Views 7.3

Same name and namespace in other branches
  1. 6.3 includes/i18nviews_plugin_argument_validate_i18n_taxonomy_term.inc \i18nviews_plugin_argument_validate_i18n_taxonomy_term
  2. 6.2 includes/i18nviews_plugin_argument_validate_i18n_taxonomy_term.inc \i18nviews_plugin_argument_validate_i18n_taxonomy_term

Validate whether an argument is a localized term.

Hierarchy

Expanded class hierarchy of i18nviews_plugin_argument_validate_i18n_taxonomy_term

1 string reference to 'i18nviews_plugin_argument_validate_i18n_taxonomy_term'
i18nviews_views_plugins in includes/i18nviews.views.inc
Implementation of hook_views_plugins().

File

includes/i18nviews_plugin_argument_validate_i18n_taxonomy_term.inc, line 10
Contains the 'Taxonomy term (i18n)' argument validator plugin.

View source
class i18nviews_plugin_argument_validate_i18n_taxonomy_term extends views_plugin_argument_validate_taxonomy_term {
  function option_definition() {
    $options = parent::option_definition();
    $options['type']['default'] = 'i18n_name';
    return $options;
  }
  function convert_options(&$options) {
    if (!isset($options['vids']) && !empty($this->argument->options['validate_argument_vocabulary_i18n'])) {
      $options['vids'] = $this->argument->options['validate_argument_vocabulary_i18n'];
      $options['type'] = $this->argument->options['validate_argument_type_i18n'];
      $options['transform'] = $this->argument->options['validate_argument_transform_i18n'];
    }
  }
  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    $options = array();

    // Get the localized vocabularies.
    foreach (taxonomy_get_vocabularies() as $vocab) {
      if (i18n_taxonomy_vocabulary_mode($vocab->vid, I18N_MODE_LOCALIZE)) {
        $options[$vocab->machine_name] = check_plain($vocab->name);
      }
    }
    $form['vocabularies']['#description'] = t('Limit this validator to vocabularies that have been localized');
    $form['vocabularies']['#options'] = $options;
    $form['type']['#options'] = array(
      'i18n_tid' => t('Term ID'),
      'i18n_tids' => t('Term IDs separated by , or +'),
      'i18n_name' => t('Localised Term name or synonym'),
      'i18n_convert' => t('Localised Term name/synonym converted to Term ID'),
    );
    $form['type']['#description'] = t('Select the form of this argument; if using term name, it is generally more efficient to convert it to a term ID and use Taxonomy: Term ID rather than Taxonomy: Term Name" as an argument.');
  }
  function validate_argument($argument) {
    $vocabularies = $this->options['vocabularies'];
    $type = $this->options['type'];
    $transform = $this->options['transform'];
    switch ($type) {
      case 'i18n_tid':
      case 'i18n_tids':
      case 'i18n_name':
      case 'i18n_convert':

        // Check to see if the term is in fact localised
        $localised = i18nviews_locale_source($argument, 'taxonomy');
        if (!empty($localised)) {

          // If it is localised I set the $argument to the orginal and tell the view that the argument has been localized and to use the source
          $argument = $localised->source;
          $this->argument->argument = $localised->source;
        }
        if ($type == 'i18n_tid' || $type == 'i18n_tids') {
          return $this
            ->{"validate_argument_{$type}"}($argument, $vocabularies);
        }
        $query = db_select('taxonomy_term_data', 'td');
        $query
          ->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
        $query
          ->fields('td');
        $query
          ->fields('tv', array(
          'machine_name',
        ));
        if (!empty($vocabularies)) {
          $query
            ->condition('tv.machine_name', $vocabularies);
        }
        if ($transform) {
          $query
            ->where("replace(td.name, ' ', '-') = :name", array(
            ':name' => $argument,
          ));
        }
        else {
          $query
            ->condition('td.name', $argument);
        }
        $term = $query
          ->execute()
          ->fetchObject();
        if ($term && (empty($vocabularies) || !empty($vocabularies[$term->machine_name]))) {
          if ($type == 'i18n_convert') {
            $this->argument->argument = $term->tid;
          }

          // ToDo: check vocabulary translation mode (if localization needed)
          $this->argument->validated_title = check_plain(i18n_taxonomy_term_name($term));
          return TRUE;
        }
        return FALSE;
    }
  }

  /**
   * Validate taxonomy terms - case i18n_tid
   */
  function validate_argument_i18n_tid($argument, $vocabularies) {
    if (!is_numeric($argument)) {
      return FALSE;
    }
    $query = db_select('taxonomy_term_data', 'td');
    $query
      ->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
    $query
      ->fields('td');
    $query
      ->fields('tv', array(
      'machine_name',
    ));
    $query
      ->condition('td.tid', $argument);
    $query
      ->addTag('term_access');
    $term = $query
      ->execute()
      ->fetchObject();
    if (!$term) {
      return FALSE;
    }
    $this->argument->validated_title = check_plain(i18n_taxonomy_term_name($term));
    return empty($vocabularies) || !empty($vocabularies[$term->machine_name]);
  }

  /**
   * Validate taxonomy term - case i18n_tids
   */
  function validate_argument_i18n_tids($argument, $vocabularies) {

    // An empty argument is not a term so doesn't pass.
    if (empty($argument)) {
      return FALSE;
    }
    $tids = new stdClass();
    $tids->value = $argument;
    $tids = views_break_phrase($argument, $tids);
    if ($tids->value == array(
      -1,
    )) {
      return FALSE;
    }
    $test = drupal_map_assoc($tids->value);
    $titles = array();

    // check, if some tids already verified
    static $validated_cache = array();
    foreach ($test as $tid) {
      if (isset($validated_cache[$tid])) {
        if ($validated_cache[$tid] === FALSE) {
          return FALSE;
        }
        else {
          $titles[] = $validated_cache[$tid];
          unset($test[$tid]);
        }
      }
    }

    // if unverified tids left - verify them and cache results
    if (count($test)) {
      $query = db_select('taxonomy_term_data', 'td');
      $query
        ->leftJoin('taxonomy_vocabulary', 'tv', 'td.vid = tv.vid');
      $query
        ->fields('td');
      $query
        ->fields('tv', array(
        'machine_name',
      ));
      $query
        ->condition('td.tid', $test);
      $result = $query
        ->execute();
      foreach ($result as $term) {
        if ($vocabularies && empty($vocabularies[$term->machine_name])) {
          $validated_cache[$term->tid] = FALSE;
          return FALSE;
        }
        $titles[] = $validated_cache[$term->tid] = check_plain(i18n_taxonomy_term_name($term));
        unset($test[$term->tid]);
      }
    }

    // Remove duplicate titles
    $titles = array_unique($titles);
    $this->argument->validated_title = implode($tids->operator == 'or' ? ' + ' : ', ', $titles);

    // If this is not empty, we did not find a tid.
    return empty($test);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
i18nviews_plugin_argument_validate_i18n_taxonomy_term::convert_options function Convert options from the older style. Overrides views_plugin_argument_validate_taxonomy_term::convert_options
i18nviews_plugin_argument_validate_i18n_taxonomy_term::options_form function Provide the default form for setting options. Overrides views_plugin_argument_validate_taxonomy_term::options_form
i18nviews_plugin_argument_validate_i18n_taxonomy_term::option_definition function Retrieve the options when this is a new access control plugin. Overrides views_plugin_argument_validate_taxonomy_term::option_definition
i18nviews_plugin_argument_validate_i18n_taxonomy_term::validate_argument function Overrides views_plugin_argument_validate_taxonomy_term::validate_argument
i18nviews_plugin_argument_validate_i18n_taxonomy_term::validate_argument_i18n_tid function Validate taxonomy terms - case i18n_tid
i18nviews_plugin_argument_validate_i18n_taxonomy_term::validate_argument_i18n_tids function Validate taxonomy term - case i18n_tids
views_object::$definition public property Handler's definition.
views_object::$options public property Except for displays, options for the object will be held here. 1
views_object::altered_option_definition function Collect this handler's option definition and alter them, ready for use.
views_object::construct public function Views handlers use a special construct function. 4
views_object::destroy public function Destructor. 2
views_object::export_option public function 1
views_object::export_options public function
views_object::export_option_always public function Always exports the option, regardless of the default value.
views_object::options Deprecated public function Set default options on this object. 1
views_object::set_default_options public function Set default options.
views_object::set_definition public function Let the handler know what its full definition is.
views_object::unpack_options public function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away.
views_object::unpack_translatable public function Unpack a single option definition.
views_object::unpack_translatables public function Unpacks each handler to store translatable texts.
views_object::_set_option_defaults public function
views_plugin::$display public property The current used views display.
views_plugin::$plugin_name public property The plugin name of this plugin, for example table or full.
views_plugin::$plugin_type public property The plugin type of this plugin, for example style or query.
views_plugin::$view public property The top object of a view. Overrides views_object::$view 1
views_plugin::additional_theme_functions public function Provide a list of additional theme functions for the theme info page.
views_plugin::plugin_title public function Return the human readable name of the display.
views_plugin::query public function Add anything to the query that we might need to. 7
views_plugin::summary_title public function Returns the summary of the settings in the display. 8
views_plugin::theme_functions public function Provide a full list of possible theme templates used by this style.
views_plugin::validate public function Validate that the plugin is correct and can be saved. 3
views_plugin_argument_validate::access public function Determine if the administrator has the privileges to use this plugin. 1
views_plugin_argument_validate::check_access public function If we don't have access to the form but are showing it anyway, ensure that the form is safe and cannot be changed from user input.
views_plugin_argument_validate::options_validate public function Provide the default form form for validating options. Overrides views_plugin::options_validate
views_plugin_argument_validate_taxonomy_term::init public function Initialize this plugin with the view and the argument it is linked to. Overrides views_plugin_argument_validate::init
views_plugin_argument_validate_taxonomy_term::options_submit public function Provide the default form form for submitting options Overrides views_plugin_argument_validate::options_submit
views_plugin_argument_validate_taxonomy_term::process_summary_arguments public function Process the summary arguments for displaying. Overrides views_plugin_argument_validate::process_summary_arguments