You are here

class flag_plugin_argument_validate_flaggability in Flag 7.3

Same name and namespace in other branches
  1. 6.2 includes/flag_plugin_argument_validate_flaggability.inc \flag_plugin_argument_validate_flaggability
  2. 6 includes/flag_plugin_argument_validate_flaggability.inc \flag_plugin_argument_validate_flaggability
  3. 7.2 includes/flag_plugin_argument_validate_flaggability.inc \flag_plugin_argument_validate_flaggability

Validates whether an argument is a flaggable/flagged object.

Hierarchy

Expanded class hierarchy of flag_plugin_argument_validate_flaggability

1 string reference to 'flag_plugin_argument_validate_flaggability'
flag_views_plugins in includes/views/flag.views.inc
Implements hook_views_plugins().

File

includes/views/flag_plugin_argument_validate_flaggability.inc, line 13
Contains the flaggability validator handler.

View source
class flag_plugin_argument_validate_flaggability extends views_plugin_argument_validate {
  function construct() {
    parent::construct();
    $this->flag_type = $this->definition['flag type'];
  }
  function option_definition() {
    $options = parent::option_definition();
    $options['flag_name'] = array(
      'default' => '*relationship*',
    );
    $options['flag_test'] = array(
      'default' => 'flaggable',
    );
    $options['flag_id_type'] = array(
      'default' => 'id',
    );
    return $options;
  }
  function options_form(&$form, &$form_state) {

    // If there are no flag relationships set, skip this form of validation.
    $flags = flag_get_flags($this->flag_type);
    if (!$flags) {
      return array();
    }
    $options = $this
      ->flags_options();
    $form['flag_name'] = array(
      '#type' => 'radios',
      '#title' => t('Flag'),
      '#options' => $options,
      '#default_value' => $this->options['flag_name'],
      '#description' => t('Select the flag to validate against.'),
    );
    if (!$options) {
      $form['flag_name']['#description'] = '<p class="warning">' . t('No %type flags exist. You must first <a href="@create-url">create a %type flag</a> before being able to use one.', array(
        '%type' => $this->flag_type,
        '@create-url' => FLAG_ADMIN_PATH,
      )) . '</p>';
    }
    $form['flag_test'] = array(
      '#type' => 'select',
      '#title' => t('Validate the @type only if', array(
        '@type' => $this->flag_type,
      )),
      '#options' => $this
        ->tests_options(),
      '#default_value' => $this->options['flag_test'],
    );

    // This validator supports the "multiple IDs" syntax. It may not be
    // extremely useful, but similar validators do support this and it's a good
    // thing to be compatible.
    $form['flag_id_type'] = array(
      '#type' => 'select',
      '#title' => t('Argument type'),
      '#options' => array(
        'id' => t('ID'),
        'ids' => t('IDs separated by , or +'),
      ),
      '#default_value' => $this->options['flag_id_type'],
    );
  }

  /**
   * Returns form #options for the flags.
   *
   * Returns empty array if no flags were found.
   */
  function flags_options() {
    $flags = flag_get_flags($this->flag_type);
    if (!$flags) {
      return array();
    }
    else {
      foreach ($flags as $flag) {
        $options[$flag->name] = $flag
          ->get_title();
      }
      $options['*relationship*'] = t('<em>Pick the first flag mentioned in the relationships.</em>');
      return $options;
    }
  }

  /**
   * Migrate existing Views 2 options to Views 3.
   */
  function convert_options(&$options) {
    $prefix = 'validate_argument_' . $this->flag_type . '_';
    if (!isset($options['flag_name']) && !empty($this->argument->options[$prefix . 'flag_name'])) {
      $options['flag_name'] = $this->argument->options[$prefix . 'flag_name'];
      $options['flag_test'] = $this->argument->options[$prefix . 'flag_test'];
      $options['flag_id_type'] = $this->argument->options[$prefix . 'flag_id_type'];
    }
  }

  /**
   * Declares all tests.
   *
   * This scheme makes it easy for derived classes to add and remove tests.
   */
  function tests_info($which = NULL) {
    return array(
      'flaggable' => array(
        'title' => t('It is flaggable'),
        'callback' => 'test_flaggable',
      ),
      'flagged' => array(
        'title' => t('It is flagged at least once'),
        'callback' => 'test_flagged',
      ),
      'flagged_by_current_user' => array(
        'title' => t('It is flagged by the current user'),
        'callback' => 'test_flagged_by_current_user',
      ),
    );
  }
  function tests_options() {
    $options = array();
    foreach ($this
      ->tests_info() as $id => $info) {
      $options[$id] = $info['title'];
    }
    return $options;
  }
  function get_flag() {
    $flag_name = $this->options['flag_name'];
    if ($flag_name == '*relationship*') {

      // Pick the first flag mentioned in the relationships.
      foreach ($this->view->relationship as $id => $handler) {

        // Note: we can't do $handler->field, because the relationship handler's
        // init() may overwrite it.
        if (strpos($handler->options['field'], 'flag') !== FALSE && !empty($handler->options['flag'])) {
          $flag = flag_get_flag($handler->options['flag']);
          if ($flag && $flag->entity_type == $this->flag_type) {
            return $flag;
          }
        }
      }
    }
    return flag_get_flag($flag_name);
  }

  /**
   * Tests whether the argument is flaggable, or flagged, or flagged by current
   * user. These are three possible tests, and which of the three to actually
   * carry out is determined by 'flag_test'.
   */
  function validate_argument($argument) {
    $flag_test = $this->options['flag_test'];
    $id_type = $this->options['flag_id_type'];
    $flag = $this
      ->get_flag();
    if (!$flag) {

      // Validator is misconfigured somehow.
      return TRUE;
    }
    if ($id_type == 'id') {
      if (!is_numeric($argument)) {

        // If a user is being smart and types several IDs where only one is
        // expected, we invalidate this.
        return FALSE;
      }
    }
    $ids = views_break_phrase($argument, $this);
    if ($ids->value == array(
      -1,
    )) {

      // Malformed argument syntax. Invalidate.
      return FALSE;
    }
    $ids = $ids->value;

    // Delegate the testing to the particual test method. $passed then
    // holds the IDs that passed the test.
    $tests_info = $this
      ->tests_info();
    $method = $tests_info[$flag_test]['callback'];
    if (method_exists($this, $method)) {
      $passed = $this
        ->{$method}($ids, $flag);
    }
    else {
      $passed = array();
    }

    // If some IDs exist that haven't $passed our test then validation fails.
    $failed = array_diff($ids, $passed);
    return empty($failed);
  }

  //
  // The actual tests. They return the IDs that passed.
  //
  function test_flaggable($ids, $flag) {
    return array_filter($ids, array(
      $flag,
      'applies_to_entity_id',
    ));
  }
  function test_flagged($ids, $flag) {

    // view_break_phrase() is guaranteed to return only integers, so this is SQL
    // safe.
    $flattened_ids = implode(',', $ids);
    return $this
      ->_test_by_sql("SELECT entity_id FROM {flag_counts} WHERE fid = :fid AND entity_id IN ({$flattened_ids}) AND count > 0", array(
      ':fid' => $flag->fid,
    ));
  }
  function test_flagged_by_current_user($ids, $flag) {
    global $user;
    if (!$user->uid) {

      // Anonymous user.
      return array();
    }
    $flattened_ids = implode(',', $ids);
    return $this
      ->_test_by_sql("SELECT entity_id FROM {flagging} WHERE fid = :fid AND entity_id IN ({$flattened_ids}) AND uid = :uid", array(
      ':fid' => $flag->fid,
      ':uid' => $user->uid,
    ));
  }

  // Helper: executes an SQL query and returns all the entity_id's.
  function _test_by_sql($sql, $args) {
    $passed = array();
    $result = db_query($sql, $args);
    foreach ($result as $row) {
      $passed[] = $row->entity_id;
    }
    return $passed;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
flag_plugin_argument_validate_flaggability::construct function Views handlers use a special construct function. Overrides views_object::construct
flag_plugin_argument_validate_flaggability::convert_options function Migrate existing Views 2 options to Views 3. Overrides views_plugin_argument_validate::convert_options
flag_plugin_argument_validate_flaggability::flags_options function Returns form #options for the flags.
flag_plugin_argument_validate_flaggability::get_flag function
flag_plugin_argument_validate_flaggability::options_form function Provide the default form for setting options. Overrides views_plugin_argument_validate::options_form
flag_plugin_argument_validate_flaggability::option_definition function Retrieve the options when this is a new access control plugin. Overrides views_plugin_argument_validate::option_definition
flag_plugin_argument_validate_flaggability::tests_info function Declares all tests.
flag_plugin_argument_validate_flaggability::tests_options function
flag_plugin_argument_validate_flaggability::test_flaggable function
flag_plugin_argument_validate_flaggability::test_flagged function
flag_plugin_argument_validate_flaggability::test_flagged_by_current_user function
flag_plugin_argument_validate_flaggability::validate_argument function Tests whether the argument is flaggable, or flagged, or flagged by current user. These are three possible tests, and which of the three to actually carry out is determined by 'flag_test'. Overrides views_plugin_argument_validate::validate_argument
flag_plugin_argument_validate_flaggability::_test_by_sql function
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::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::init public function Initialize this plugin with the view and the argument it is linked to. 1
views_plugin_argument_validate::options_submit public function Provide the default form form for submitting options Overrides views_plugin::options_submit 3
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::process_summary_arguments public function Process the summary arguments for displaying. 2