You are here

domain_source.module in Domain Access 7.3

Creates a source domain for linking to content from other domains.

File

domain_source/domain_source.module
View source
<?php

/**
 * @defgroup domain_source Domain Source: editor-defined links.
 *
 * Creates a canonical source domain for linking to content from other domains.
 */

/**
 * @file
 * Creates a source domain for linking to content from other domains.
 *
 * @ingroup domain_source
 */
define('DOMAIN_SOURCE_USE_ACTIVE', -5);
define('DOMAIN_SOURCE_IGNORE', -10);

/**
 * Implements hook_form_alter()
 */
function domain_source_form_alter(&$form, &$form_state, $form_id) {

  // Apply to all node editing forms only.
  if (empty($form['#node_edit_form'])) {
    return;
  }
  global $user;
  $_domain = domain_get_domain();
  if (!empty($form['#node']->nid)) {
    $default_source = db_query("SELECT domain_id FROM {domain_source} WHERE nid = :nid", array(
      ':nid' => $form['#node']->nid,
    ))
      ->fetchField();
  }
  if (!isset($default_source)) {
    $source = domain_load_domain_id(variable_get('domain_source_' . $form['#node']->type, DOMAIN_SOURCE_USE_ACTIVE));
    if ($source <= 0) {
      $default_source = DOMAIN_SOURCE_USE_ACTIVE;
    }
    else {
      $source_domain = domain_lookup($source);
      if ($source_domain == -1) {
        $default_source = $_domain['domain_id'];
      }
      else {
        $default_source = $source_domain['domain_id'];
      }
    }
  }

  // Prevent invalid domains from being used.
  $lookup = domain_lookup($default_source);
  if ($default_source != DOMAIN_SOURCE_USE_ACTIVE && empty($lookup['valid'])) {
    $default_source = NULL;
  }
  $account = $user;
  domain_user_set($account);

  // Only uses with 'set domain access' can assign content to all affiliates, so they get a new option.
  // This option allows domain source to be ignored on a per-node basis.
  $options = array();
  $domains = domain_domains();
  $show = FALSE;
  if (user_access('set domain access')) {
    $show = TRUE;
    $options[DOMAIN_SOURCE_USE_ACTIVE] = t('Use active domain');
    foreach ($domains as $domain) {
      if ($domain['valid'] || user_access('access inactive domains')) {
        $options[$domain['domain_id']] = $domain['sitename'];
      }
    }
  }
  elseif (user_access('publish to any assigned domain') && !empty($account->domain_user)) {
    $show = FALSE;
    $options[DOMAIN_SOURCE_USE_ACTIVE] = t('Use active domain');

    // Get the user's allowed domains.
    foreach ($domains as $domain) {
      $key = $domain['domain_id'];
      if (!empty($user->domain_user[$key]) && ($domain['valid'] || user_access('access inactive domains'))) {
        $options[$key] = $domain['sitename'];
      }
    }

    // Is this node assigned to a source that the user can control?
    if (isset($form['#node']->domain_source)) {
      $source = $form['#node']->domain_source;
    }
    else {
      $source = NULL;
      $show = TRUE;
    }
    if (!is_null($source) && isset($account->domain_user[$source])) {
      if ($account->domain_user[$source] == $source) {
        $show = TRUE;
      }
      else {
        $name = $source != -5 ? $domains[$source]['sitename'] : t('the active domain');
        $form['domain']['domain_source_notes'] = array(
          '#type' => 'item',
          '#title' => t('Source domain'),
          '#markup' => t('This content is assigned to %domain and cannot be reassigned.', array(
            '%domain' => $name,
          )),
        );
      }
    }
  }

  // Determine how to show the form element.
  if ($show) {
    $form['domain']['domain_source'] = array(
      '#type' => 'select',
      '#title' => t('Source domain'),
      '#options' => $options,
      '#default_value' => $default_source,
      '#description' => t('This affiliate will be used to write links to this content from other affiliates, as needed.'),
    );
  }
  else {
    $form['domain']['domain_source'] = array(
      '#type' => 'value',
      '#value' => $default_source,
    );
  }
}

/**
 * Integrate with Domain Content.
 */
function domain_source_form_domain_content_admin_alter(&$form, &$form_state) {
  $_domain = domain_get_domain();
  if (!user_access('set domain access') || isset($form['operation']['#value'])) {
    return;
  }
  $options = array();
  $domains = domain_domains();

  // Options.
  $options[DOMAIN_SOURCE_IGNORE] = t('Do not change');
  $options[DOMAIN_SOURCE_USE_ACTIVE] = t('Use active domain');
  foreach ($domains as $domain) {
    $options[$domain['domain_id']] = $domain['sitename'];
  }
  $form['domain']['domain_source'] = array(
    '#type' => 'select',
    '#title' => t('Source domain'),
    '#options' => $options,
    '#default_value' => $_domain['domain_id'],
    '#description' => t('The canonical domain for the selected content. This element replaces existing settings for all selections.'),
  );
  $form['admin']['options']['submit']['#validate'][] = 'domain_source_validate';
  $form['admin']['options']['submit']['#submit'][] = 'domain_source_update_nodes';
}

/**
 * Form integration with Administer nodes.
 */
function domain_source_form_node_admin_content_alter(&$form, &$form_state) {

  // Do nothing on the delete screen.
  if (isset($form['operation']['#value']) && $form['operation']['#value'] == 'delete') {
    return;
  }
  domain_source_form_domain_content_admin_alter($form, $form_state);
}

/**
 * Implements hook_node_validate().
 */
function domain_source_node_validate($node, $form) {

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

  // 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) {

    // Any assignment is acceptable if sent to all affiliates.
    // I find this code easier to read than a compound IF statement.
  }
  elseif ($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.'));
  }
}

/**
 * Implements hook_node_load().
 */
function domain_source_node_load($nodes, $types) {
  $results = db_query("SELECT nid, domain_id FROM {domain_source} WHERE nid IN (:nid)", array(
    ':nid' => array_keys($nodes),
  ))
    ->fetchAllAssoc('nid');
  foreach ($nodes as $nid => $node) {

    // Cannot load if the node has not been created yet.
    if (!isset($results[$nid])) {
      $nodes[$node->nid]->domain_source = NULL;
    }
    else {
      $nodes[$node->nid]->domain_source = $results[$node->nid]->domain_id;
    }
  }
}

/**
 * Implements hook_node_insert()
 */
function domain_source_node_insert($node) {

  // If not set, we ignore.
  if (!isset($node->domain_source) || is_null($node->domain_source)) {
    return;
  }
  domain_source_node_delete($node);
  db_insert('domain_source')
    ->fields(array(
    'nid' => $node->nid,
    'domain_id' => $node->domain_source,
  ))
    ->execute();
}

/**
 * Implements hook_node_update()
 */
function domain_source_node_update($node) {
  domain_source_node_insert($node);
}

/**
 * Implements hook_node_delete()
 */
function domain_source_node_delete($node) {
  db_delete('domain_source')
    ->condition('nid', $node->nid)
    ->execute();
}

/**
 * Implements hook_node_view()
 *
 * Display debugging information for a node.
 */
function domain_source_node_view($node, $view_mode) {
  if (empty($node->nid) || !in_array($view_mode, array(
    'full',
    'teaser',
  )) || !variable_get('domain_debug', 0) || !user_access('set domain access')) {
    return;
  }
  $_domain = domain_get_domain();
  $source = domain_get_node_match($node->nid);
  if (is_null($node->domain_source)) {
    $node->content['domain']['#markup'] .= '<p>' . t('<strong>Source domain</strong>: %source (determined automatically)', array(
      '%source' => $source['sitename'],
    )) . '</p>';
  }
  elseif ($node->domain_source == DOMAIN_SOURCE_USE_ACTIVE) {
    $node->content['domain']['#markup'] .= '<p>' . t('<strong>Source domain</strong>: %source (using active domain)', array(
      '%source' => $_domain['sitename'],
    )) . '</p>';
  }
  else {
    $node->content['domain']['#markup'] .= '<p>' . t('<strong>Source domain</strong>: %source', array(
      '%source' => $source['sitename'],
    )) . '</p>';
  }
}

/**
 * Get the source domains for multiple matches, mimicking node_load().
 *
 * @param $nid
 *   A node id.
 *
 * @return
 *   The domain_id of the canonical source domain or FALSE if not set.
 */
function domain_source_lookup($nid) {
  $source = FALSE;
  if ($domain_source = db_query("SELECT domain_id FROM {domain_source} WHERE nid = :nid", array(
    ':nid' => $nid,
  ))
    ->fetchField()) {
    if ($domain_source == DOMAIN_SOURCE_USE_ACTIVE) {
      $source = $domain_source;
    }
    elseif ($domain = domain_lookup($domain_source)) {
      $source = $domain['domain_id'];
    }
  }
  return $source;
}

/**
 * FormAPI function that lets us update access rules.
 */
function domain_source_update_nodes($form, &$form_state) {

  // If our operation is flagged, then we have to manually change the
  // {node_access} table.  The rest of the process will clear the cache,
  // so this should be a safe operation.
  if ($form_state['values']['operation'] == 'domain') {
    $source = $form_state['values']['domain_source'];
    foreach ($form_state['values']['nodes'] as $key => $value) {
      if ($key == $value) {
        db_delete('domain_source')
          ->condition('nid', $key)
          ->execute();
        db_insert('domain_source')
          ->fields(array(
          'nid' => $key,
          'domain_id' => $source,
        ))
          ->execute();
      }
    }
  }
}

/**
 * Form validation step
 */
function domain_source_validate($form, &$form_state) {
  if ($form_state['values']['operation'] == 'domain') {
    $default = domain_default_id();
    $key = $form_state['values']['domain_source'];
    if (!empty($form_state['values']['domain_site']) || $key == DOMAIN_SOURCE_USE_ACTIVE) {

      // These cases are acceptable, so we let them pass.
      // I find this code easier to read than a compound negative IF statement.
    }
    elseif ($key == DOMAIN_SOURCE_IGNORE) {

      // In this case, we need to check all the selected nodes, which is resource intensive.
      $nodes = array_filter($form_state['values']['nodes']);
      foreach ($nodes as $nid) {
        $key = domain_source_lookup($nid);

        // Test for the first two behaviors, which add domains to the node.
        $behavior = $form_state['values']['behavior'];
        if ($behavior < 2) {
          if (empty($form_state['values']['domains'][$key])) {
            $node = node_load($nid);
            form_set_error('domain_source', t('The source affiliate must be selected as a publishing option. %title is assigned to %domain.', array(
              '%title' => $node->title,
              '%domain' => $source['sitename'],
            )));
          }
        }
        elseif (!empty($form_state['values']['domains'][$key])) {
          $node = node_load($nid);
          form_set_error('domain_source', t('The source affiliate must be selected as a publishing option. %title is assigned to %domain.', array(
            '%title' => $node->title,
            '%domain' => $source['sitename'],
          )));
        }
      }
    }
    elseif (empty($form_state['values']['domains'][$key])) {
      form_set_error('domain_source', t('The source affiliate must be selected as a publishing option.'));
    }
  }
}

/**
 * Implements hook_views_api().
 */
function domain_source_views_api() {
  if (module_exists('domain_views')) {
    return array(
      'api' => 2.0,
    );
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Allows default settings for domain source when setting the default domain
 * selection per content type.
 */
function domain_source_form_domain_nodes_form_alter(&$form, &$form_state, $form_id) {
  $options = array();
  $domains = domain_domains();
  $options[DOMAIN_SOURCE_USE_ACTIVE] = t('Use active domain');
  foreach ($domains as $domain) {
    $options[$domain['machine_name']] = $domain['sitename'];
  }
  $form['domain_source'] = array(
    '#type' => 'fieldset',
    '#title' => t('Default source domains'),
  );
  foreach (node_type_get_names() as $type => $name) {
    $form['domain_source']['domain_source_' . $type] = array(
      '#type' => 'select',
      '#title' => t('%name default source domain', array(
        '%name' => $name,
      )),
      '#options' => $options,
      '#default_value' => variable_get('domain_source_' . $type, DOMAIN_SOURCE_USE_ACTIVE),
    );
  }
  $form['#validate'][] = 'domain_source_validate_domain_nodes_form';
}

/**
 * Validates default source selections.
 */
function domain_source_validate_domain_nodes_form($form, &$form_state) {

  // Handle the differences between our form and the node type form.
  $source = '';
  if (isset($form_state['values']['type'])) {
    $types = array(
      $form_state['values']['type'] => node_type_get_name($form_state['values']['type']),
    );
    $source = 'node_type_form';
  }
  else {
    $types = node_type_get_names();
  }
  foreach ($types as $type => $name) {
    $item = 'domain_node_' . $type;
    $key = 'domain_source_' . $type;

    // Using the content type form requires specific form element names.
    if ($source == 'node_type_form') {
      $item = 'domain_node';
      $key = 'domain_source';
    }
    $value = $form_state['values'][$key];

    // Use active is always valid.
    if ($value == DOMAIN_SOURCE_USE_ACTIVE) {
      continue;
    }
    $parent = $form_state['values'][$item];

    // Any assignment is acceptable if sent to all affiliates.
    if (!empty($parent['DOMAIN_ALL'])) {
      continue;
    }

    // Else, we must select the source domain as an option.
    if (empty($parent[$value])) {
      form_set_error('domain_source_' . $type, t('The default %name source domain must be selected as a default publishing option.', array(
        '%name' => $name,
      )));
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Allows default settings for domain source when setting the default domain
 * selection per content type.
 */
function domain_source_form_node_type_form_alter(&$form, &$form_state, $form_id) {
  $options = array();
  $domains = domain_domains();
  $options[DOMAIN_SOURCE_USE_ACTIVE] = t('Use active domain');
  foreach ($domains as $domain) {
    $options[$domain['machine_name']] = $domain['sitename'];
  }
  $form['domain']['domain_source'] = array(
    '#type' => 'select',
    '#title' => t('%name default source domain', array(
      '%name' => $form['#node_type']->name,
    )),
    '#options' => $options,
    '#default_value' => variable_get('domain_source_' . $form['#node_type']->type, DOMAIN_SOURCE_USE_ACTIVE),
  );
  $form['#validate'][] = 'domain_source_validate_domain_nodes_form';
}

Related topics

Functions

Constants

Namesort descending Description
DOMAIN_SOURCE_IGNORE
DOMAIN_SOURCE_USE_ACTIVE @file Creates a source domain for linking to content from other domains.