You are here

uc_addresses_views_plugin_argument_user_address_access.inc in Ubercart Addresses 6.2

Same filename and directory in other branches
  1. 7 views/uc_addresses_views_plugin_argument_user_address_access.inc

Definition of uatest_views_plugin_argument_address_access.

File

views/uc_addresses_views_plugin_argument_user_address_access.inc
View source
<?php

/**
 * @file
 * Definition of uatest_views_plugin_argument_address_access.
 */

/**
 * Checks if the current user has access to addresses of the given user.
 *
 * This inherits checks from views_plugin_argument_validate_user.
 */
class uc_addresses_views_plugin_argument_user_address_access extends views_plugin_argument_validate_user {

  /**
   * Implementation of views_plugin_argument_validate#validate_form().
   *
   * Used by Views 6.x-2.x.
   */
  public function validate_form(&$form, &$form_state) {

    // We are unable to rely on options having already been set, so let's make
    // sure defaults are here:
    if (!isset($this->argument->options['validate_uc_addresses_user_argument_type'])) {
      $this->argument->options['validate_uc_addresses_user_argument_type'] = 'uid';
      $this->argument->options['validate_uc_addresses_user_roles'] = array();
      $this->argument->options['validate_uc_addresses_user_access_type'] = 'view';
    }

    // Get form from parent.
    $form_parent = array();
    parent::validate_form($form_parent, $form_state);

    // Overwrite ID's of parent form elements.
    $form['validate_uc_addresses_user_argument_type'] = array(
      '#prefix' => '<div id="edit-options-validate-uc-addresses-user-argument-type-wrapper">',
      '#dependency' => array(
        'edit-options-validate-type' => array(
          $this->id,
        ),
      ),
      '#default_value' => $this->argument->options['validate_uc_addresses_user_argument_type'],
    );
    $form['validate_uc_addresses_user_argument_type'] += $form_parent['validate_user_argument_type'];
    $form['validate_uc_addresses_user_restrict_roles'] = array(
      '#dependency' => array(
        'edit-options-validate-type' => array(
          $this->id,
        ),
      ),
      '#default_value' => $this->argument->options['validate_uc_addresses_user_restrict_roles'],
    );
    $form['validate_uc_addresses_user_restrict_roles'] += $form_parent['validate_user_restrict_roles'];
    $form['validate_uc_addresses_user_roles'] = array(
      '#prefix' => '<div id="edit-options-validate-uc-addresses-user-roles-wrapper">',
      '#dependency' => array(
        'edit-options-validate-type' => array(
          $this->id,
        ),
        'edit-options-validate-uc-addresses-user-restrict-roles' => array(
          1,
        ),
      ),
      '#default_value' => $this->argument->options['validate_uc_addresses_user_roles'],
    );
    $form['validate_uc_addresses_user_roles'] += $form_parent['validate_user_roles'];

    // Access type.
    $form['validate_uc_addresses_user_access_type'] = array(
      '#type' => 'select',
      '#title' => t('Address access type'),
      '#description' => t('Select for which type of operation permissions should be checked. Usually "!access_type".', array(
        '!access_type' => t('View'),
      )),
      '#options' => array(
        'view' => t('View'),
        'edit' => t('Edit'),
        'delete' => t('Delete'),
      ),
      '#default_value' => $this->argument->options['validate_uc_addresses_user_access_type'],
      '#process' => array(
        'views_process_dependency',
      ),
      '#dependency' => array(
        'edit-options-validate-type' => array(
          $this->id,
        ),
      ),
    );
  }

  /**
   * Implementation of views_plugin_argument_validate#option_definition().
   *
   * Used by Views 6.x-3.x.
   */
  public function option_definition() {
    $options = parent::option_definition();
    $options['access_type'] = array(
      'default' => 'view',
    );
    return $options;
  }

  /**
   * Implementation of views_plugin_argument_validate#options_form().
   *
   * Used by Views 6.x-3.x.
   */
  public function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);

    // Overwrite ID's of parent form elements.
    $form['roles']['#prefix'] = '<div id="edit-options-argument-validate-uc-addresses-user-address-access-roles-wrapper">';
    $form['roles']['#dependency'] = array(
      'edit-options-argument-validate-uc-addresses-user-address-access-restrict-roles' => array(
        1,
      ),
    );

    // Access type.
    $form['access_type'] = array(
      '#type' => 'select',
      '#title' => t('Address access type'),
      '#description' => t('Select for which type of operation permissions should be checked. Usually "!access_type".', array(
        '!access_type' => t('View'),
      )),
      '#options' => array(
        'view' => t('View'),
        'edit' => t('Edit'),
        'delete' => t('Delete'),
      ),
      '#default_value' => $this->options['access_type'],
    );
  }

  /**
   * Validates if argument is a valid user
   * and if the current user has access to addresses of
   * the given user.
   */
  public function validate_argument($argument) {

    // Map options to how the parent calls them in Views 6.x-2.x.
    if (isset($this->argument->options['validate_uc_addresses_user_argument_type'])) {
      $this->argument->options['validate_user_argument_type'] = $this->argument->options['validate_uc_addresses_user_argument_type'];
      $this->argument->options['validate_user_restrict_roles'] = $this->argument->options['validate_uc_addresses_user_restrict_roles'];
      $this->argument->options['validate_user_roles'] = $this->argument->options['validate_uc_addresses_user_roles'];
    }

    // Let the parent validate the argument first.
    if (!parent::validate_argument($argument)) {

      // If the parent says the argument is invalid, then
      // there is no need to check further for address access.
      return FALSE;
    }

    // Check for address access.
    $access_type = 'view';
    if (isset($this->argument->options['validate_uc_addresses_user_access_type'])) {
      $access_type = $this->argument->options['validate_uc_addresses_user_access_type'];
    }
    elseif (isset($this->options['access_type'])) {
      $access_type = $this->options['access_type'];
    }
    $address_user = user_load($this->argument->argument);
    switch ($access_type) {
      case 'view':
        return UcAddressesPermissions::canViewAddress($address_user);
      case 'edit':
        return UcAddressesPermissions::canEditAddress($address_user);
      case 'delete':
        return UcAddressesPermissions::canDeleteAddress($address_user);
    }
    return FALSE;
  }

}

Classes

Namesort descending Description
uc_addresses_views_plugin_argument_user_address_access Checks if the current user has access to addresses of the given user.