You are here

domain_rules.module in Domain Rules 7

Same filename and directory in other branches
  1. 6 domain_rules.module

domain_rules.module @description Port of 'Domain rules' by shushu for Drupal7

File

domain_rules.module
View source
<?php

/**
 * @file domain_rules.module
 * @description Port of 'Domain rules' by shushu for Drupal7
 */

/**
 * Get simple list of themes themename=>themename
 * @return array
 */
function __domain_rules_themes() {
  $themes = array();
  foreach (list_themes() as $theme) {
    $themes[$theme->name] = $theme->name;
  }
  return $themes;
}

/**
 * Get array of http and https schemes
 * @return array
 */
function __domain_rules_schemes() {
  return array(
    0 => t('http'),
    1 => t('https'),
  );
}

/**
 * Get array of active and inactive
 * @return array
 */
function __domain_rules_state() {
  return array(
    1 => t('Active'),
    0 => t('Inactive'),
  );
}

/**
 * Get array comparsion operators
 * @return array
 */
function __domain_rules_compare_operators() {
  return array(
    '=' => t('equals'),
    '!=' => t('not equals'),
    '~' => t('matching (possible * and ? wildcards)'),
    'regexp' => t('match with regexp (without modifiers)'),
  );
}

/**
 * Get user roles rid=>name
 * @return array
 */
function __domain_rules_user_roles() {
  return user_roles();
}

/**
 * Custom hook
 * Do on action 'create domain'
 * @param string $sitename
 * @param string $subdomain
 * @param int $domainscheme
 * @param int $valid
 */
function domain_rules_actions_create_domain($sitename, $subdomain, $scheme, $valid) {
  $domain = domain_lookup(NULL, $subdomain, TRUE);
  if ($domain['domain_id']) {
    drupal_set_message(t('The domain %domain already exists.', array(
      '%domain' => $subdomain,
    )), 'error');
  }
  else {
    $schemes = __domain_rules_schemes();
    $scheme = isset($schemes[$scheme]) ? $schemes[$scheme] : '';
    unset($schemes);
    $domain_settings = array(
      'subdomain' => $subdomain,
      'sitename' => $sitename,
      'scheme' => $scheme,
      'valid' => $valid,
    );
    $domain = domain_save($domain_settings, FALSE);
    unset($domain_settings);
    if ($domain['domain_id']) {
      drupal_set_message(t('The domain %domain created successfully.', array(
        '%domain' => $subdomain,
      )));
    }
    else {
      drupal_set_message(t('Failed to create the domain %domain.', array(
        '%domain' => $subdomain,
      )), 'error');
    }
  }
}

/**
 * Custom hook
 * Do on action 'change subdomain'
 * @param string $subdomain
 * @param string $new_subdomain
 */
function domain_rules_actions_change_subdomain($subdomain, $new_subdomain) {
  $domain = domain_lookup(NULL, $subdomain, TRUE);
  if ($domain['domain_id']) {
    $domain['subdomain'] = $new_subdomain;
    $domain = domain_save($domain, FALSE);
    if ($domain['domain_id']) {
      drupal_set_message(t('The domain %domain updated successfully.', array(
        '%domain' => $subdomain,
      )));
    }
    else {
      drupal_set_message(t('Failed to update the domain %domain.', array(
        '%domain' => $subdomain,
      )), 'error');
    }
  }
  else {
    drupal_set_message(t('The domain %domain does not exist.', array(
      '%domain' => $subdomain,
    )), 'error');
  }
}

/**
 * Custom hook
 * Do on action 'delete domain'
 * @param string $subdomain
 */
function domain_rules_actions_delete_domain($subdomain) {
  $domain = domain_lookup(NULL, $subdomain, TRUE);
  if ($domain['domain_id']) {
    domain_delete($domain);
    $domain = domain_lookup(NULL, $subdomain, TRUE);
    if ($domain['domain_id']) {
      drupal_set_message(t('Failed to delete the domain %domain.', array(
        '%domain' => $subdomain,
      )), 'error');
    }
    else {
      drupal_set_message(t('The domain %domain deleted successfully.', array(
        '%domain' => $subdomain,
      )));
    }
  }
  else {
    drupal_set_message(t('The domain %domain does not exist.', array(
      '%domain' => $subdomain,
    )), 'error');
  }
}

/**
 * Custom hook
 * Do on action 'set user default domain'
 * @param string $subdomain
 * @param int $state
 * @param int $role_id
 */
function domain_rules_action_set_user_defaults_domain($subdomain, $state, $role_id) {
  $domain = domain_lookup(NULL, $subdomain, TRUE);
  $domain_roles = variable_get('domain_roles', array());
  if ($domain['domain_id']) {
    $domain_roles[$role_id][$domain['domain_id']] = $state;
    variable_set('domain_roles', $domain_roles);
  }
  else {
    drupal_set_message(t('The domain %domain does not exist.', array(
      '%domain' => $subdomain,
    )), 'error');
  }
}

/**
 * Custom hook
 * Do on action 'set node domain'
 * @param string $subdomain
 * @param node $node
 */
function domain_rules_action_set_node_domain($subdomain, $node) {
  $domain = domain_lookup(NULL, $subdomain, TRUE);
  if (isset($domain['domain_id'])) {
    $domain_id = $domain['domain_id'];

    // Based on #893450 - use -1 in case of 0
    if ($domain_id == 0) {
      $domain_id = -1;
    }
    $node->domains[$domain_id] = $domain_id;
  }
  else {
    drupal_set_message(t('The domain %domain does not exist.', array(
      '%domain' => $subdomain,
    )), 'error');
  }
  return array(
    'node' => $node,
  );
}

/**
 * Custom hook
 * Do on action 'remove node domain'
 * @param string $subdomain
 * @param node $node
 */
function domain_rules_action_remove_node_domain($subdomain, $node) {
  $domain = domain_lookup(NULL, $subdomain, TRUE);
  if (isset($domain['domain_id'])) {
    $domain_id = $domain['domain_id'];
    if ($domain_id == 0) {
      $domain_id = -1;
    }
    unset($node->domains[$domain_id]);
  }
  else {
    drupal_set_message(t('The domain %domain does not exist.', array(
      '%domain' => $subdomain,
    )), 'error');
  }
  return array(
    'node' => $node,
  );
}
function domain_rules_action_set_entity_domain($subdomain, $entity) {
  $domain = domain_lookup(NULL, $subdomain, TRUE);
  $dwrapper = entity_metadata_wrapper('domain_entity', $domain);
  if (isset($domain['domain_id'])) {
    $domain_id = $domain['domain_id'];

    // Based on #893450 - use -1 in case of 0
    if ($domain_id == 0) {
      $domain_id = -1;
    }

    // Check if it's an allowed entity types:
    $allowed_entity_types = domain_entity_allowed_entity_types();
    if (in_array($entity
      ->type(), array_keys($allowed_entity_types))) {

      // Get domain_entity field type instances:
      if ($field_instance = domain_entity_entity_field_instance($entity
        ->type())) {
        $field_instance_name = $field_instance['name'];
      }
      $values = $entity->{$field_instance_name}
        ->value();
      $values[] = array(
        'domain_id' => $domain_id,
      );
      $entity->{$field_instance_name}
        ->set($values);
      $entity
        ->save();
    }
    else {
      drupal_set_message(t('Entity of type %type can\'t get domain.', array(
        '%type' => $entity
          ->type(),
      )), 'error');
    }
  }
  else {
    drupal_set_message(t('The domain %domain does not exist.', array(
      '%domain' => $subdomain,
    )), 'error');
  }
  return array(
    'entity' => $entity,
  );
}
function domain_rules_action_remove_user_domain($subdomain, $user) {
  $domain = domain_lookup(NULL, $subdomain, TRUE);
  if (isset($domain['domain_id'])) {
    $domains = domain_get_user_domains($user);
    unset($domains[$domain['domain_id']]);
    $user->domain_user = $domains;
  }
  else {
    drupal_set_message(t('The domain %domain does not exist.', array(
      '%domain' => $subdomain,
    )), 'error');
  }
  return array(
    'user' => $user,
  );
}
function domain_rules_action_set_user_domain($subdomain, $user) {
  $domain = domain_lookup(NULL, $subdomain, TRUE);
  if (isset($domain['domain_id'])) {
    $domains = domain_get_user_domains($user);
    $domains[$domain['domain_id']] = $domain['domain_id'];
    $user->domain_user = $domains;
  }
  else {
    drupal_set_message(t('The domain %domain does not exist.', array(
      '%domain' => $subdomain,
    )), 'error');
  }
  return array(
    'user' => $user,
  );
}
function _domain_rules_action_add_domain_alias($domain_id, $pattern, $redirect) {
  return db_insert('domain_alias')
    ->fields(array(
    'domain_id' => $domain_id,
    'pattern' => $pattern,
    'redirect' => intval($redirect),
  ))
    ->execute();
}
function domain_rules_action_add_domain_alias($subdomain, $pattern, $redirect) {
  $domain = domain_lookup(NULL, $subdomain, TRUE);
  if (isset($domain['domain_id'])) {
    _domain_rules_action_add_domain_alias($domain['domain_id'], $pattern, $redirect);
  }
  else {
    drupal_set_message(t('The domain %domain does not exist.', array(
      '%domain' => $subdomain,
    )), 'error');
  }
}
function domain_rules_action_variable_set($subdomain, $variable, $value) {
  $domain = domain_lookup(NULL, $subdomain, TRUE);
  if (isset($domain['domain_id'])) {
    domain_conf_variable_set($domain['domain_id'], $variable, $value);
  }
  else {
    drupal_set_message(t('The domain %domain does not exist.', array(
      '%domain' => $subdomain,
    )), 'error');
  }
}

/**
 * Custom hook
 * Do on action 'set domain theme'
 * @param string $subdomain
 * @param string $theme
 */
function domain_rules_action_set_domain_theme($subdomain, $theme) {
  $domain = domain_lookup(NULL, $subdomain, TRUE);
  if (isset($domain['domain_id'])) {
    $domain_id = $domain['domain_id'];
    db_query('UPDATE {domain_theme} SET status = 0 WHERE domain_id = :domain_id', array(
      ':domain_id' => $domain_id,
    ));
    $check = domain_theme_lookup($domain_id, $theme);
    if ($check == -1) {
      db_query('INSERT INTO {domain_theme} (domain_id, theme, status) VALUES (:domain_id, :theme, 1)', array(
        ':domain_id' => $domain_id,
        ':theme' => $theme,
      ));
    }
    else {
      db_query('UPDATE {domain_theme} SET status = 1 WHERE domain_id = :domain_id AND theme = :theme', array(
        ':domain_id' => $domain_id,
        ':theme' => $theme,
      ));
    }
    cache_clear_all();
  }
  else {
    drupal_set_message(t('The domain %domain does not exist.', array(
      '%domain' => $subdomain,
    )), 'error');
  }
}

/**
 * Implements hook_rules_action_info().
 * Build select list and settings forms for actions
 */
function domain_rules_rules_action_info() {
  $actions = array(
    'domain_rules_actions_create_domain' => array(
      'label' => t('Create a domain'),
      'group' => t('Domains'),
      'parameter' => array(
        'sitename' => array(
          'type' => 'text',
          'label' => t('Site name'),
          'description' => t('The new domain site name.'),
          'default value' => '',
        ),
        'subdomain' => array(
          'type' => 'text',
          'label' => t('Domain address'),
          'description' => t('The new domain address.'),
          'default value' => '',
        ),
        'domain_scheme' => array(
          'type' => 'text',
          'label' => t('Schema'),
          'options list' => '__domain_rules_schemes',
          'default value' => 0,
        ),
        'valid' => array(
          'type' => 'text',
          'label' => t('Domain status'),
          'description' => t('The new domain site name.'),
          'options list' => '__domain_rules_state',
          'default value' => 1,
        ),
      ),
    ),
    'domain_rules_actions_change_subdomain' => array(
      'label' => t('Change a subdomain'),
      'group' => t('Domains'),
      'parameter' => array(
        'subdomain' => array(
          'type' => 'text',
          'label' => t('Current Domain address'),
          'description' => t('The current domain address.'),
          'default value' => '',
        ),
        'new_subdomain' => array(
          'type' => 'text',
          'label' => t('New Domain address.'),
          'description' => t('The new domain address.'),
          'default value' => '',
        ),
      ),
    ),
    'domain_rules_actions_delete_domain' => array(
      'label' => t('Delete a domain'),
      'group' => t('Domains'),
      'parameter' => array(
        'subdomain' => array(
          'type' => 'text',
          'label' => t('Domain address'),
          'description' => t('The domain address to delete.'),
          'default value' => '',
        ),
      ),
    ),
    'domain_rules_action_set_user_defaults_domain' => array(
      'label' => t('Set user defaults for a domain'),
      'group' => t('Domains'),
      'parameter' => array(
        'subdomain' => array(
          'type' => 'text',
          'label' => t('Domain address'),
          'description' => t('Domain address.'),
          'default value' => '',
        ),
        'state' => array(
          'type' => 'text',
          'label' => t('State'),
          'options list' => '__domain_rules_state',
          'default value' => 0,
        ),
        'role_id' => array(
          'type' => 'text',
          'label' => t('Role ID'),
          'options list' => '__domain_rules_user_roles',
          'default value' => '',
        ),
      ),
    ),
    'domain_rules_action_set_node_domain' => array(
      'label' => t('Set domain to a node'),
      'group' => t('Domains'),
      'parameter' => array(
        'subdomain' => array(
          'node' => array(),
          'type' => 'text',
          'label' => t('Domain address'),
          'description' => t('Domain address.'),
          'default value' => '',
        ),
        'node' => array(
          'type' => 'node',
          'label' => t('Content'),
          'save' => TRUE,
        ),
      ),
    ),
    'domain_rules_action_remove_node_domain' => array(
      'label' => t('Remove domain from a node'),
      'group' => t('Domains'),
      'parameter' => array(
        'subdomain' => array(
          'node' => array(),
          'type' => 'text',
          'label' => t('Domain address'),
          'description' => t('Domain address.'),
          'default value' => '',
        ),
        'node' => array(
          'type' => 'node',
          'label' => t('Content'),
          'save' => TRUE,
        ),
      ),
    ),
    'domain_rules_action_set_domain_theme' => array(
      'label' => t('Set domain theme'),
      'group' => t('Domains'),
      'parameter' => array(
        'subdomain' => array(
          'node' => array(),
          'type' => 'text',
          'label' => t('Domain address'),
          'description' => t('Domain address.'),
          'default value' => '',
        ),
        'theme' => array(
          'type' => 'text',
          'label' => t('Theme name'),
          'description' => t('The Theme name'),
          'options list' => '__domain_rules_themes',
          'default value' => '',
        ),
      ),
    ),
    'domain_rules_action_remove_user_domain' => array(
      'label' => t('Remove domain from a user'),
      'group' => t('Domains'),
      'parameter' => array(
        'subdomain' => array(
          'node' => array(),
          'type' => 'text',
          'label' => t('Domain address'),
          'description' => t('Domain address.'),
          'default value' => '',
        ),
        'user' => array(
          'type' => 'user',
          'label' => t('User'),
          'save' => TRUE,
        ),
      ),
    ),
    'domain_rules_action_set_user_domain' => array(
      'label' => t('Set domain to a user'),
      'group' => t('Domains'),
      'parameter' => array(
        'subdomain' => array(
          'node' => array(),
          'type' => 'text',
          'label' => t('Domain address'),
          'description' => t('Domain address.'),
          'default value' => '',
        ),
        'user' => array(
          'type' => 'user',
          'label' => t('User'),
          'save' => TRUE,
        ),
      ),
    ),
    'domain_rules_action_add_domain_alias' => array(
      'label' => t('Add alias to a domain'),
      'group' => t('Domains'),
      'parameter' => array(
        'subdomain' => array(
          'type' => 'text',
          'label' => t('Domain address'),
          'description' => t('Domain address.'),
          'default value' => '',
        ),
        'pattern' => array(
          'type' => 'text',
          'label' => t('Pattern'),
          'default value' => '',
        ),
        'redirect' => array(
          'type' => 'boolean',
          'label' => t('Redirect'),
          'description' => t('Check the redirect box if you would like requests made to the alias to redirect to the registered domain'),
          'default value' => false,
        ),
      ),
    ),
    'domain_rules_action_variable_set' => array(
      'label' => t('Set a domain variable'),
      'group' => t('Domains'),
      'parameter' => array(
        'subdomain' => array(
          'type' => 'text',
          'label' => t('Domain address'),
          'description' => t('Domain address.'),
          'default value' => '',
        ),
        'variable' => array(
          'type' => 'text',
          'label' => t('Variable'),
          'description' => t('Any variable set by domain_batch_actions(). Known variables: site_name, site_mail, site_slogan, site_frontpage, anonymous'),
          'default value' => '',
        ),
        'value' => array(
          'type' => 'text',
          'label' => t('Valule'),
          'default value' => '',
        ),
      ),
    ),
  );
  if (module_exists('domain_entity')) {
    $actions['domain_rules_action_set_entity_domain'] = array(
      'label' => t('Set domain to an entity'),
      'group' => t('Domains'),
      'parameter' => array(
        'subdomain' => array(
          'node' => array(),
          'type' => 'text',
          'label' => t('Domain address'),
          'description' => t('Domain address.'),
          'default value' => '',
        ),
        'entity' => array(
          'type' => 'entity',
          'label' => t('Entity'),
          'save' => TRUE,
        ),
      ),
    );
  }
  return $actions;
}

/**
 *
 * Implements hook_rules_event_info()
 */
function domain_rules_rules_event_info() {
  return array(
    'domain_create' => array(
      'label' => t('After creating new domain'),
      'group' => t('Domains'),
      'variables' => array(
        'domain' => array(
          'type' => 'domain',
          'label' => t('Domain created'),
        ),
      ),
    ),
    'domain_update' => array(
      'label' => t('After domain is updated'),
      'group' => t('Domains'),
      'variables' => array(
        'domain' => array(
          'type' => 'domain',
          'label' => t('Domain updated'),
        ),
      ),
    ),
    'domain_delete' => array(
      'label' => t('After deleting domain'),
      'group' => t('Domains'),
      'variables' => array(
        'domain' => array(
          'type' => 'domain',
          'label' => t('Domain deleted'),
        ),
      ),
    ),
    'domain_view' => array(
      'label' => t('Domain is going to be viewed'),
      'group' => t('Domains'),
      'variables' => array(
        'domain' => array(
          'type' => 'domain',
          'label' => t('Domain viewed'),
        ),
      ),
    ),
  );
}

/**
 * Implements hook_domain_insert()
 */
function domain_rules_domain_insert($domain = '', $form_values = array()) {
  rules_invoke_event('domain_create', $domain);
}

/**
 * Implements hook_domain_update()
 */
function domain_rules_domain_update($domain = '', $form_values = array()) {
  rules_invoke_event('domain_update', $domain);
}

/**
 * Implements hook_domain_delete()
 */
function domain_rules_domain_delete($domain = '', $form_values = array()) {
  rules_invoke_event('domain_delete', $domain);
}

/**
 * Implements hook_domainview
 */
function domain_rules_domainview($op, $domain = array(), $query = NULL) {
  rules_invoke_event('domain_view', $domain);
  return array();
}

/**
 * Implements hook_rules_condition_info()
 */
function domain_rules_rules_condition_info() {
  return array(
    'domain_rules_condition_domain_compare' => array(
      'label' => t('Domain comparison'),
      'group' => t('Domains'),
      'parameter' => array(
        'operator' => array(
          'type' => 'text',
          'label' => t('Compare operator'),
          'options list' => '__domain_rules_compare_operators',
          'default value' => '=',
        ),
        'subdomain' => array(
          'type' => 'text',
          'label' => t('Domain address'),
          'description' => t('Domain address.'),
          'default value' => '',
        ),
      ),
    ),
    'domain_rules_condition_default_domain_compare' => array(
      'label' => t('Default domain comparison'),
      'group' => t('Domains'),
      'parameter' => array(
        'operator' => array(
          'type' => 'text',
          'label' => t('Compare operator'),
          'options list' => '__domain_rules_compare_operators',
          'default value' => '=',
        ),
        'subdomain' => array(
          'type' => 'text',
          'label' => t('Domain address'),
          'description' => t('Domain address.'),
          'default value' => '',
        ),
      ),
    ),
  );
}

/**
 * Custom hook
 * Test domain for matching
 * @param string $operator
 * @param string $subdomain
 * @return bool
 */
function domain_rules_condition_domain_compare($operator, $subdomain) {
  $current_domain = domain_get_domain();
  $current_domain = $current_domain['subdomain'];
  $subdomain = trim($subdomain);
  switch ($operator) {
    case '!=':
      return $subdomain != $current_domain;
    case '~':
      return fnmatch($subdomain, $current_domain);
    case 'regexp':
      return preg_match('#' . $subdomain . '#Ui', $current_domain);
    case '=':
    default:
      return $subdomain == $current_domain;
  }
}

/**
 * Custom hook
 * Test domain for matching with default domain.
 * @param string $operator
 * @param string $subdomain
 * @return bool
 */
function domain_rules_condition_default_domain_compare($operator, $subdomain) {
  $default_domain = domain_default_machine_name();
  $default_domain = domain_machine_name_load($default_domain);
  $default_domain = $default_domain['subdomain'];
  $subdomain = trim($subdomain);
  switch ($operator) {
    case '!=':
      return $subdomain != $default_domain;
    case '~':
      return fnmatch($subdomain, $default_domain);
    case 'regexp':
      return preg_match('#' . $subdomain . '#Ui', $default_domain);
    case '=':
    default:
      return $subdomain == $default_domain;
  }
}

Functions

Namesort descending Description
domain_rules_actions_change_subdomain Custom hook Do on action 'change subdomain'
domain_rules_actions_create_domain Custom hook Do on action 'create domain'
domain_rules_actions_delete_domain Custom hook Do on action 'delete domain'
domain_rules_action_add_domain_alias
domain_rules_action_remove_node_domain Custom hook Do on action 'remove node domain'
domain_rules_action_remove_user_domain
domain_rules_action_set_domain_theme Custom hook Do on action 'set domain theme'
domain_rules_action_set_entity_domain
domain_rules_action_set_node_domain Custom hook Do on action 'set node domain'
domain_rules_action_set_user_defaults_domain Custom hook Do on action 'set user default domain'
domain_rules_action_set_user_domain
domain_rules_action_variable_set
domain_rules_condition_default_domain_compare Custom hook Test domain for matching with default domain.
domain_rules_condition_domain_compare Custom hook Test domain for matching
domain_rules_domainview Implements hook_domainview
domain_rules_domain_delete Implements hook_domain_delete()
domain_rules_domain_insert Implements hook_domain_insert()
domain_rules_domain_update Implements hook_domain_update()
domain_rules_rules_action_info Implements hook_rules_action_info(). Build select list and settings forms for actions
domain_rules_rules_condition_info Implements hook_rules_condition_info()
domain_rules_rules_event_info Implements hook_rules_event_info()
_domain_rules_action_add_domain_alias
__domain_rules_compare_operators Get array comparsion operators
__domain_rules_schemes Get array of http and https schemes
__domain_rules_state Get array of active and inactive
__domain_rules_themes Get simple list of themes themename=>themename
__domain_rules_user_roles Get user roles rid=>name