You are here

organigrams.rules.inc in Organigrams 7

Rules integration for the organigrams module.

File

organigrams.rules.inc
View source
<?php

/**
 * @file
 * Rules integration for the organigrams module.
 *
 * @addtogroup rules
 *
 * @{
 */

/**
 * Implements hook_rules_data_info_alter().
 */
function organigrams_rules_data_info_alter(&$data_info) {

  // Add our own wrappers to the organigrams entity.
  $data_info['organigrams']['wrapper class'] = 'RulesOrganigramsWrapper';
  $data_info['organigrams']['ui class'] = 'RulesDataUIOrganigrams';
}

/**
 * Implements hook_rules_event_info().
 */
function organigrams_rules_event_info() {
  $defaults_organigrams_item = array(
    'group' => t('Organigrams'),
    'access callback' => 'rules_organigrams_item_integration_access',
    'module' => 'organigrams',
    'class' => 'RulesOrganigramsEventHandler',
  );
  $defaults_organigrams = array(
    'group' => t('Organigrams'),
    'access callback' => 'rules_organigrams_integration_access',
    'module' => 'organigrams',
  );
  return array(
    'organigrams_item_insert' => $defaults_organigrams_item + array(
      'label' => t('After saving a new organigrams item'),
      'variables' => array(
        'item' => array(
          'type' => 'organigrams_item',
          'label' => t('created organigrams item'),
        ),
      ),
    ),
    'organigrams_item_update' => $defaults_organigrams_item + array(
      'label' => t('After updating an existing organigrams item'),
      'variables' => array(
        'item' => array(
          'type' => 'organigrams_item',
          'label' => t('updated organigrams item'),
        ),
        'item_unchanged' => array(
          'type' => 'organigrams_item',
          'label' => t('unchanged organigrams item'),
          'handler' => 'rules_events_entity_unchanged',
        ),
      ),
    ),
    'organigrams_item_presave' => $defaults_organigrams_item + array(
      'label' => t('Before saving an organigrams item'),
      'variables' => array(
        'item' => array(
          'type' => 'organigrams_item',
          'label' => t('saved organigrams item'),
          'skip save' => TRUE,
        ),
        'item_unchanged' => array(
          'type' => 'organigrams_item',
          'label' => t('unchanged organigrams item'),
          'handler' => 'rules_events_entity_unchanged',
        ),
      ),
    ),
    'organigrams_item_delete' => $defaults_organigrams_item + array(
      'label' => t('After deleting an organigrams item'),
      'variables' => array(
        'item' => array(
          'type' => 'organigrams_item',
          'label' => t('deleted organigrams item'),
        ),
      ),
    ),
    'organigrams_insert' => $defaults_organigrams + array(
      'label' => t('After saving a new organigram'),
      'variables' => array(
        'organigrams' => array(
          'type' => 'organigrams',
          'label' => t('created organigram'),
        ),
      ),
    ),
    'organigrams_update' => $defaults_organigrams + array(
      'label' => t('After updating an existing organigram'),
      'variables' => array(
        'organigrams' => array(
          'type' => 'organigrams',
          'label' => t('updated organigram'),
        ),
        'organigrams_unchanged' => array(
          'type' => 'organigrams',
          'label' => t('unchanged organigram'),
          'handler' => 'rules_events_entity_unchanged',
        ),
      ),
    ),
    'organigrams_presave' => $defaults_organigrams + array(
      'label' => t('Before saving an organigram'),
      'variables' => array(
        'organigrams' => array(
          'type' => 'organigrams',
          'label' => t('saved organigram'),
          'skip save' => TRUE,
        ),
        'organigrams_unchanged' => array(
          'type' => 'organigrams',
          'label' => t('unchanged organigram'),
          'handler' => 'rules_events_entity_unchanged',
        ),
      ),
    ),
    'organigrams_delete' => $defaults_organigrams + array(
      'label' => t('After deleting an organigram'),
      'variables' => array(
        'organigrams' => array(
          'type' => 'organigrams',
          'label' => t('deleted organigram'),
        ),
      ),
    ),
  );
}

/**
 * Organigrams item integration access callback.
 *
 * @param string $type
 *   Contains an action type.
 *
 * @return bool
 *   TRUE if access allowed, otherwise FALSE.
 */
function rules_organigrams_item_integration_access($type) {
  if ($type == 'event' || $type == 'condition') {
    return entity_access('view', 'organigrams_item');
  }
}

/**
 * Organigrams integration access callback.
 *
 * @param string $type
 *   Contains an action type.
 *
 * @return bool
 *   TRUE if access allowed, otherwise FALSE.
 */
function rules_organigrams_integration_access($type) {
  if ($type == 'event' || $type == 'condition') {
    return entity_access('view', 'organigrams');
  }
}

/**
 * Event handler support organigrams bundle event settings.
 */
class RulesOrganigramsEventHandler extends RulesEventHandlerEntityBundle {

  /**
   * Returns the label to use for the bundle property.
   *
   * @return string
   *   The name of the bundle.
   */
  protected function getBundlePropertyLabel() {
    return t('organigrams');
  }

}

/**
 * Custom wrapper class for organigrams for loading organigrams by machine name.
 */
class RulesOrganigramsWrapper extends EntityDrupalWrapper {

  /**
   * Overridden to support identifying organigrams by machine names.
   *
   * @param string $data
   *   Contains a machine name.
   */
  protected function setEntity($data) {
    if (isset($data) && $data !== FALSE && !is_object($data) && !is_numeric($data)) {

      // The organigrams name has been passed.
      parent::setEntity(organigrams_machine_name_load($data));
    }
    else {
      parent::setEntity($data);
    }
  }

  /**
   * Overridden to permit machine names as values.
   *
   * @param string $value
   *   String to validate.
   *
   * @return bool
   *   TRUE is $value is a string, otherwise FALSE.
   */
  public function validate($value) {
    if (isset($value) && is_string($value)) {
      return TRUE;
    }
    return parent::validate($value);
  }

}

/**
 * UI for taxonomy vocabularies.
 *
 * @see RulesOrganigramsWrapper
 */
class RulesDataUIOrganigrams extends RulesDataUIEntity implements RulesDataInputOptionsListInterface {

  /**
   * Set a default mode.
   *
   * @return string
   *   Default mode is 'input'.
   */
  public static function getDefaultMode() {
    return 'input';
  }

  /**
   * Implements RulesDataInputOptionsListInterface::optionsList().
   *
   * @param \RulesPlugin $element
   *   Contains a rules plugin.
   * @param string $name
   *   Contains an Organigrams name.
   *
   * @return array
   *   Array containing organigram names.
   */
  public static function optionsList(RulesPlugin $element, $name) {
    $options = array();
    foreach (organigrams_get_names() as $machine_name => $organigram) {
      $options[$machine_name] = $organigram->name;
    }
    return $options;
  }

}

/**
 * @}
 */

Functions

Namesort descending Description
organigrams_rules_data_info_alter Implements hook_rules_data_info_alter().
organigrams_rules_event_info Implements hook_rules_event_info().
rules_organigrams_integration_access Organigrams integration access callback.
rules_organigrams_item_integration_access Organigrams item integration access callback.

Classes

Namesort descending Description
RulesDataUIOrganigrams UI for taxonomy vocabularies.
RulesOrganigramsEventHandler Event handler support organigrams bundle event settings.
RulesOrganigramsWrapper Custom wrapper class for organigrams for loading organigrams by machine name.