You are here

function domain_nodeapi in Domain Access 6.2

Same name and namespace in other branches
  1. 5 domain.module \domain_nodeapi()

Implement hook_nodeapi().

This function is used to provide debugging information and to prep values from the {domain_access} table when editing nodes. Since not all users can see the domain access editing checkboxes, we pass some node_access values as hidden elements.

File

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

Code

function domain_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'prepare':
    case 'load':

      // Cannot load if the node has not been created yet.
      if (!isset($node->nid)) {
        return;
      }

      // Append the domain grants to the node for editing.
      $domains = domain_get_node_domains($node->nid);
      $node->domains = $domains['domain_id'];
      $node->domain_site = $domains['domain_site'];
      $node->subdomains = array();
      if ($node->domain_site) {
        $node->subdomains[] = t('All affiliates');
      }
      foreach ($node->domains as $gid) {
        if ($gid > 0) {
          $domain = domain_lookup($gid);
          $node->subdomains[] = $domain['sitename'];
        }
        else {
          $node->subdomains[] = variable_get('domain_sitename', variable_get('site_name', 'Drupal'));
        }
      }
      break;
    case 'view':

      // Search module casts both $a3 and $a4 as FALSE, not NULL.
      // We check that to hide this data from search and other nodeapi
      // calls that are neither a teaser nor a page view.
      if ($a3 !== FALSE || $a4 !== FALSE) {
        $output = '';
        $debug = variable_get('domain_debug', 0);
        if ($debug && user_access('set domain access')) {
          if (!empty($node->subdomains)) {
            $items = array();
            foreach ($node->subdomains as $name) {
              $items[] = check_plain($name);
            }
            $output = theme('item_list', $items, t('Assigned domains'));
            $node->content['subdomains'] = array(
              '#value' => $output,
              '#weight' => 20,
            );
          }
          if (!empty($node->editors)) {
            $items = array();
            foreach ($node->editors as $name) {
              $items[] = check_plain($name);
            }
            $output = theme('item_list', $items, t('Editors'));
            $node->content['editors'] = array(
              '#value' => $output,
              '#weight' => 21,
            );
          }
          if (empty($output)) {
            $node->content['domain'] = array(
              '#value' => t('This node is not assigned to a domain.'),
              '#weight' => 22,
            );
          }
        }
      }
      break;
    case 'delete':

      // Remove records from the {domain_access} table.
      db_query("DELETE FROM {domain_access} WHERE nid = %d", $node->nid);
      break;
    case 'insert':
    case 'update':

      // We cannot use the global domain here, since it can be modified.
      $domain = domain_initial_domain();
      $_SESSION['domain_save_id'] = $domain['domain_id'];
      break;
    case 'presave':
      if (!empty($node->devel_generate)) {

        // 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;
          }
        }
      }
      break;
  }
}