You are here

uc_addresses_views_plugin_argument_address_access.inc in Ubercart Addresses 6.2

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

Definition of uatest_views_plugin_argument_address_access.

File

views/uc_addresses_views_plugin_argument_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_address_access extends views_plugin_argument_validate {

  /**
   * 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_address_access_type'])) {
      $this->argument->options['validate_uc_addresses_address_access_type'] = 'view';
    }
    $form['validate_uc_addresses_address_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_address_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);
    $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) {

    // Check for address access.
    $address = UcAddressesAddressBook::loadAddress($argument);
    if (!$address) {

      // Address does not exist.
      return FALSE;
    }
    $access_type = 'view';
    if (isset($this->argument->options['validate_uc_addresses_address_access_type'])) {
      $access_type = $this->argument->options['validate_uc_addresses_address_access_type'];
    }
    elseif (isset($this->options['access_type'])) {
      $access_type = $this->options['access_type'];
    }
    $address_user = user_load($address
      ->getUserId());
    switch ($access_type) {
      case 'view':
        return UcAddressesPermissions::canViewAddress($address_user, $address);
      case 'edit':
        return UcAddressesPermissions::canEditAddress($address_user, $address);
      case 'delete':
        return UcAddressesPermissions::canDeleteAddress($address_user, $address);
    }
    return FALSE;
  }

}

Classes

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