You are here

class ServicesClientUserRoleCondition in Services Client 7.2

Hierarchy

Expanded class hierarchy of ServicesClientUserRoleCondition

1 string reference to 'ServicesClientUserRoleCondition'
services_client_services_client_condition in ./services_client.plugins.inc
List availalable condition plugins.

File

include/condition.inc, line 351

View source
class ServicesClientUserRoleCondition extends ServicesClientConditionPlugin {

  /**
   * Retrieve default configuration.
   */
  protected function getDefaultConfiguration() {
    return array(
      'roles' => array(),
      'intersect' => FALSE,
      'reverse' => FALSE,
    );
  }

  /**
   * Retrieve condition summary for UI.
   *
   * @return string
   *   UI short summary.
   */
  public function getSummary() {
    if (empty($this->config['roles'])) {
      return '[ ' . t('User roles condition - not configured') . ' ]';
    }
    else {
      $intersect = $this->config['intersect'] ? t('all') : t('at least one');
      $roles = array();
      foreach ($this->config['roles'] as $rid => $role) {
        $role = user_role_load($role);
        $roles[] = $role->name;
      }
      return format_string('User has @intersect of these roles: @roles', array(
        '@intersect' => $intersect,
        '@roles' => implode(', ', $roles),
      ));
    }
  }

  /**
   * Check if entity matches condition.
   *
   * @param stdClass $entity
   *   Drupal entity that should be matched.
   *
   * @return bool
   *   TRUE if matches condition.
   */
  public function match($entity) {
    $roles_intersect = array_intersect($this->config['roles'], $entity->roles);
    if ($this->config['intersect']) {
      $matched = $this->config['roles'] == $roles_intersect;
    }
    else {
      $matched = !empty($roles_intersect);
    }
    return $this->config['reverse'] ? !$matched : $matched;
  }

  /**
   * Configuration form.
   */
  public function configForm(&$form, &$form_state) {
    $form['roles'] = array(
      '#type' => 'select',
      '#multiple' => TRUE,
      '#title' => t('User Roles'),
      '#required' => TRUE,
      '#options' => user_roles(),
      '#description' => t('All users with such roles will be picked'),
      '#default_value' => isset($this->config['roles']) ? $this->config['roles'] : NULL,
    );
    $form['intersect'] = array(
      '#type' => 'checkbox',
      '#title' => t('All roles should match?'),
      '#description' => t('Otherwise user has to have at least one role.'),
      '#default_value' => isset($this->config['intersect']) ? $this->config['intersect'] : NULL,
    );
    $form['reverse'] = array(
      '#type' => 'checkbox',
      '#title' => t('Reverse'),
      '#description' => t('All users without such roles will be picked.'),
      '#default_value' => isset($this->config['reverse']) ? $this->config['reverse'] : NULL,
    );
  }

  /**
   * Submit handler.
   */
  public function configFormSubmit(&$form, &$form_state) {
    $this->config['roles'] = $form_state['values']['roles'];
    $this->config['intersect'] = $form_state['values']['intersect'];
    $this->config['reverse'] = $form_state['values']['reverse'];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ServicesClientConditionPlugin::__construct public function Constructor. Overrides ServicesClientPlugin::__construct
ServicesClientPlugin::$config protected property Plugin specific configuration
ServicesClientPlugin::$event protected property Event definition
ServicesClientPlugin::configFormValidate public function Validate configuration form. Overrides ServicesClientConfigurableInterface::configFormValidate 1
ServicesClientPlugin::getConfiguration public function Retrieve current plugin configuration. Overrides ServicesClientConfigurableInterface::getConfiguration
ServicesClientPlugin::setConfiguration public function Set configuration of plugin. Overrides ServicesClientConfigurableInterface::setConfiguration
ServicesClientUserRoleCondition::configForm public function Configuration form. Overrides ServicesClientPlugin::configForm
ServicesClientUserRoleCondition::configFormSubmit public function Submit handler. Overrides ServicesClientPlugin::configFormSubmit
ServicesClientUserRoleCondition::getDefaultConfiguration protected function Retrieve default configuration. Overrides ServicesClientConditionPlugin::getDefaultConfiguration
ServicesClientUserRoleCondition::getSummary public function Retrieve condition summary for UI. Overrides ServicesClientConditionInterface::getSummary
ServicesClientUserRoleCondition::match public function Check if entity matches condition. Overrides ServicesClientConditionInterface::match