You are here

function domain_node_presave in Domain Access 7.3

Same name and namespace in other branches
  1. 7.2 domain.module \domain_node_presave()

Implements hook_node_presave().

Allows devel generate to add domains.

File

./domain.module, line 1798
Core module functions for the Domain Access suite.

Code

function domain_node_presave($node) {
  if (empty($node->devel_generate['domains'])) {
    return;
  }

  // Build $domains array based on domains checked in the generate form
  // and shuffle for randomization
  $checked = array_filter($node->devel_generate['domains']);
  if (empty($checked)) {
    return;
  }
  shuffle($checked);
  $domains = array_combine(array_values($checked), array_values($checked));

  // Add the domains and supporting data to the node
  if (!empty($domains)) {

    // Remove some domains from the shuffled array (if more than one domain
    // is chosen) for randomization (-1 guarantees at least one domain).
    if (count($domains) > 1) {
      $howmany = rand(0, count($domains) - 1);
      for ($i = 0; $i < $howmany; $i++) {
        array_pop($domains);
      }
    }

    // Add the domains to the node and grab the first domain as the source.
    // The source is random because the array has been shuffled.
    $node->domains = $domains;
    $node->domain_source = current($domains);

    // domain_site is set to TRUE or FALSE based on "all", "never" or "random flag"
    $node->domain_site = $node->devel_generate['domain_site'] == 'all' ? 1 : ($node->devel_generate['domain_site'] == 'random' ? rand(0, 1) == 1 : 0);

    // Set subdomains according to the domains in $domains
    $node->domains = array();
    foreach ($domains as $id) {
      $node->domains[$id] = $id;
    }
  }
}