You are here

function flag_plugin_argument_validate_flaggability::validate_argument in Flag 6.2

Same name and namespace in other branches
  1. 6 includes/flag_plugin_argument_validate_flaggability.inc \flag_plugin_argument_validate_flaggability::validate_argument()
  2. 7.3 includes/views/flag_plugin_argument_validate_flaggability.inc \flag_plugin_argument_validate_flaggability::validate_argument()
  3. 7.2 includes/flag_plugin_argument_validate_flaggability.inc \flag_plugin_argument_validate_flaggability::validate_argument()

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'.

File

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

Class

flag_plugin_argument_validate_flaggability
Validates whether an argument is a flaggable/flagged object.

Code

function validate_argument($argument) {
  $flag_test = $this
    ->_get_option('flag_test', 'flaggable');
  $id_type = $this
    ->_get_option('flag_id_type', 'id');
  $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);
}