domain_source_nodeapi

Definition

domain_source_nodeapi(&$node, $op, $a3, $a4)
domain_source/domain_source.module, line 101

Description

Implements hook_nodeapi()

Code

function domain_source_nodeapi(&$node, $op, $a3, $a4) {
  switch ($op) {
    case 'validate':
      // 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;
      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.
      }
      // Here we account for both the 'domains_raw' and 'domains' options.
      // If selected, these clauses will not be zero.
      else if (!$node->domains[$key] && (!empty($node->domains_raw) && !in_array($key, $node->domains_raw))) {
        form_set_error('domain_source', t('The source affiliate must be selected as a publishing option.'));
      }
      break;
    case 'insert':
    case 'update':
      db_query("DELETE FROM {domain_source} WHERE nid = %d", $node->nid);
      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 = array();
        $source = db_fetch_array(db_query("SELECT domain_id FROM {domain_source} WHERE nid = %d", $node->nid));
      }
      if (empty($source)) {
        $node->domain_source = variable_get('domain_default_source', 0);
      }
      else {
        $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_lookup($node->domain_source);
        $node->content['domain_source'] = array('#value' => '<p>'. t('<b>Source domain</b>: %source', array('%source' => $source['sitename'])) .'</b></p>', '#weight' => 25);
        }
      }
      break;
  }
}