You are here

domain_rules.rules.inc in Domain Rules 6

The rules hook and action

Define each action, messages to users, etc.

File

domain_rules.rules.inc
View source
<?php

/**
 * @file
 * The rules hook and action
 *
 * Define each action, messages to users, etc.
 */

/**
 * Implementation of hook_rules_action_info().
 */
function domain_rules_action_info() {
  return array(
    'rules_action_create_domain' => array(
      'label' => t('Create a domain'),
      'module' => 'Domain',
      'eval input' => array(
        'sitename',
        'subdomain',
        'valid',
        'domain_scheme',
      ),
    ),
    'rules_action_delete_domain' => array(
      'label' => t('Delete a domain'),
      'module' => 'Domain',
      'eval input' => array(
        'subdomain',
      ),
    ),
    'rules_action_set_user_defaults_domain' => array(
      'label' => t('Set user defaults for a domain'),
      'module' => 'Domain',
      'eval input' => array(
        'subdomain',
        'state',
        'role_id',
      ),
    ),
    'rules_action_set_node_domain' => array(
      'label' => t('Set domain to a node'),
      'module' => 'Domain',
      'arguments' => array(
        'node' => array(
          'type' => 'node',
          'label' => t('Content'),
        ),
      ),
      'eval input' => array(
        'subdomain',
      ),
    ),
    'rules_action_set_user_domain' => array(
      'label' => t('Set domain to a user'),
      'module' => 'Domain',
      'arguments' => array(
        'user' => array(
          'type' => 'user',
          'label' => t('User'),
        ),
      ),
      'eval input' => array(
        'subdomain',
      ),
    ),
    'rules_action_set_domain_theme' => array(
      'label' => t('Set domain theme'),
      'module' => 'Domain',
      'eval input' => array(
        'subdomain',
        'theme',
      ),
    ),
  );
}
function rules_action_create_domain($settings) {

  // Get form value
  $form_state['values']['sitename'] = $settings['sitename'];
  $form_state['values']['subdomain'] = $settings['subdomain'];
  $form_state['values']['valid'] = $settings['valid'];
  $form_state['values']['domain_scheme'] = $settings['domain_scheme'];
  $form_state['values']['user_submitted'] = TRUE;
  $arguments = array(
    'user_submitted' => TRUE,
    'ignore_domain_status_check' => TRUE,
  );
  $domain = domain_lookup(NULL, $form_state['values']['subdomain'], TRUE);

  // In case the domain already exist
  if (isset($domain['domain_id'])) {
    drupal_set_message(t('The domain @domain already exists.', array(
      '@domain' => $form_state['values']['subdomain'],
    )), 'error');
  }
  else {

    // Include the form file.
    module_load_include('inc', 'domain', 'domain.admin');
    drupal_execute('domain_form', $form_state, array(), $arguments);
    $domain = domain_lookup(NULL, $form_state['values']['subdomain'], TRUE);
    if (isset($domain['domain_id'])) {
      drupal_set_message(t('The domain @domain created successfully.', array(
        '@domain' => $form_state['values']['subdomain'],
      )));
    }
    else {
      drupal_set_message(t('Failed to create the domain @domain.', array(
        '@domain' => $form_state['values']['subdomain'],
      )), 'error');
    }
  }
}
function rules_action_delete_domain($settings) {

  // Get form values
  $form_state['values']['subdomain'] = $settings['subdomain'];
  $form_state['values']['user_submitted'] = TRUE;
  $arguments = array(
    'user_submitted' => TRUE,
    'ignore_domain_status_check' => TRUE,
  );
  $domain = domain_lookup(NULL, $form_state['values']['subdomain'], TRUE);

  // In case the domain exists - delete it
  if (isset($domain['domain_id'])) {
    $form_state['values']['domain_id'] = $domain['domain_id'];

    // Include the form file.
    module_load_include('inc', 'domain', 'domain.admin');
    drupal_execute('domain_delete_form', $form_state, $domain, $arguments);
    $domain = domain_lookup(NULL, $form_state['values']['subdomain'], TRUE);
    if (isset($domain['domain_id'])) {
      drupal_set_message(t('Failed to delete the domain @domain.', array(
        '@domain' => $form_state['values']['subdomain'],
      )), 'error');
    }
    else {
      drupal_set_message(t('The domain @domain deleted successfully.', array(
        '@domain' => $form_state['values']['subdomain'],
      )));
    }
  }
  else {
    drupal_set_message(t('The domain @domain does not exist.', array(
      '@domain' => $form_state['values']['subdomain'],
    )), 'error');
  }
}
function rules_action_set_user_defaults_domain($settings) {
  $domain = domain_lookup(NULL, $settings['subdomain'], TRUE);
  $domain_roles = variable_get('domain_roles', array());
  $state = $settings['state'];
  $role_id = $settings['role_id'];
  if (isset($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' => $form_state['values']['subdomain'],
    )), 'error');
  }
}
function rules_action_set_node_domain($node, $settings) {
  $domain = domain_lookup(NULL, $settings['subdomain'], TRUE);
  if (isset($domain['domain_id'])) {

    // Based on #893450 - use -1 in case of 0
    $domain_id = $domain['domain_id'];
    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' => $settings['subdomain'],
    )), 'error');
  }
  return array(
    'node' => $node,
  );
}
function rules_action_set_user_domain($user, $settings) {
  $domain = domain_lookup(NULL, $settings['subdomain'], TRUE);
  if (isset($domain['domain_id'])) {
    $domains = domain_get_user_domains($user);
    $domains[$domain['domain_id']] = $domain['domain_id'];
    user_save($user, array(
      "domain_user" => $domains,
    ));
  }
  else {
    drupal_set_message(t('The domain @domain does not exist.', array(
      '@domain' => $settings['subdomain'],
    )), 'error');
  }
  return array(
    'user' => $user,
  );
}
function rules_action_set_domain_theme($settings) {
  $theme = $settings['theme'];
  $domain = domain_lookup(NULL, $settings['subdomain'], TRUE);
  if (isset($domain['domain_id'])) {
    $domain_id = $domain['domain_id'];
    $settings = NULL;
    db_query("UPDATE {domain_theme} SET status = 0 WHERE domain_id = %d", $domain_id);
    $check = domain_theme_lookup($domain_id, $theme);
    if ($check == -1) {
      db_query("INSERT INTO {domain_theme} (domain_id, theme, settings, status) VALUES (%d, '%s', %b, 1)", $domain_id, $theme, $settings);
    }
    else {
      db_query("UPDATE {domain_theme} SET status = 1 WHERE domain_id = %d AND theme = '%s'", $domain_id, $theme);
    }

    // Clear the cache.
    cache_clear_all();
  }
  else {
    drupal_set_message(t('The domain @domain does not exist.', array(
      '@domain' => $settings['subdomain'],
    )), 'error');
  }
}