You are here

function domain_source_nodeapi in Domain Access 6.2

Same name and namespace in other branches
  1. 5 domain_source/domain_source.module \domain_source_nodeapi()

Implement hook_nodeapi()

File

domain_source/domain_source.module, line 154
Creates a source domain for linking to content from other domains.

Code

function domain_source_nodeapi(&$node, $op, $a3, $a4) {
  global $_domain;
  switch ($op) {
    case 'validate':

      // If not set, we ignore.
      if (!isset($node->domain_source)) {
        return;
      }

      // Cast the key from zero to -1 to match the data coming from the input.
      $node->domain_source == 0 ? $key = -1 : ($key = $node->domain_source);

      // Check the domain and domains_raw variables to set up the allowed source list.
      $allowed = array();
      if (!empty($node->domains)) {
        $allowed = $node->domains;
      }
      if (!empty($node->domains_raw)) {
        $allowed = array_merge($allowed, $node->domains_raw);
      }
      if ($node->domain_site && $key == -1) {

        // This case is acceptable, so we let it pass.
        // I find this code easier to read than a compound IF statement.
      }
      else {
        if ($node->domain_source != DOMAIN_SOURCE_USE_ACTIVE && !in_array($key, $allowed)) {
          form_set_error('domain_source', t('The source affiliate must be selected as a publishing option.'));
        }
      }
      break;
    case 'insert':
    case 'update':
      global $_domain;

      // If not set, we came from a drupal_execute() or other external call, so use the current domain.
      if (!isset($node->domain_source)) {
        $node->domain_source = $_domain['domain_id'];
      }

      // Prevent invalid domains from being used.
      $lookup = domain_lookup($node->domain_source);
      if ($node->domain_source != DOMAIN_SOURCE_USE_ACTIVE && empty($lookup['valid'])) {
        $node->domain_source = NULL;
      }
      db_query("DELETE FROM {domain_source} WHERE nid = %d", $node->nid);
      if (!is_null($node->domain_source)) {
        db_query("INSERT INTO {domain_source} (nid, domain_id) VALUES (%d, %d)", $node->nid, $node->domain_source);
      }
      break;
    case 'delete':
      db_query("DELETE FROM {domain_source} WHERE nid = %d", $node->nid);
      break;
    case 'load':
      if ($node->nid) {
        $source = domain_source_lookup($node->nid, $node->domains, $node->domain_site);
        $node->domain_source = $source['domain_id'];
      }
      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) {
        if (variable_get('domain_debug', 0) && user_access('set domain access') && isset($node->domain_source)) {
          $source = domain_get_node_match($node->nid);
          $extra = ' ';
          $use_active = db_result(db_query("SELECT domain_id FROM {domain_source} WHERE nid = %d", $node->nid));
          if ($use_active === FALSE) {
            $extra .= t('(determined automatically)');
          }
          else {
            if ($use_active == DOMAIN_SOURCE_USE_ACTIVE) {
              $extra .= t('(using active domain)');
            }
          }
          $node->content['domain_source'] = array(
            '#value' => '<p>' . t('<strong>Source domain</strong>: %source', array(
              '%source' => $source['sitename'],
            )) . $extra . '</p>',
            '#weight' => 25,
          );
        }
      }
      break;
  }
}