You are here

domain_content.module in Domain Access 7.3

Editorial overview module.

Provides batch node editing for users with 'edit domain content' permission but without the 'administer nodes' permission.

File

domain_content/domain_content.module
View source
<?php

/**
 * @defgroup domain_content Domain Content : administer nodes for affiliate sites
 *
 * Allows for the batch editing of select node settings.  Re-factors the default content
 * editing screen to show content only from selected domains.
 */

/**
 * @file
 * Editorial overview module.
 *
 * Provides batch node editing for users with 'edit domain content' permission
 * but without the 'administer nodes' permission.
 *
 * @ingroup domain_content
 */

/**
 * Implements hook_menu()
 */
function domain_content_menu() {
  $items = array();
  $items['admin/domain/content'] = array(
    'title' => 'Affiliated content',
    'description' => 'Administer content by domain.',
    'page callback' => 'domain_content_list',
    'access callback' => 'domain_content_menu_check',
    'file' => 'domain_content.admin.inc',
  );
  $items['admin/domain/content/all'] = array(
    'title' => 'Content assigned to all affiliates',
    'page callback' => 'domain_content_view',
    'page arguments' => array(
      NULL,
      TRUE,
    ),
    'access callback' => 'domain_content_menu_check',
    'file' => 'domain_content.admin.inc',
    'description' => 'View content assigned to all affiliate sites.',
    'weight' => -10,
  );

  // Generate the list of active domains as menu items.
  // We must wrap this check, otherwise the uninstall fails.
  // See http://drupal.org/node/1003430.
  if (module_exists('domain')) {
    $domains = domain_domains();
    if (count($domains) <= variable_get('domain_list_size', DOMAIN_LIST_SIZE)) {
      foreach ($domains as $domain) {
        $items['admin/domain/content/' . $domain['domain_id']] = array(
          'title' => check_plain($domain['sitename']) . ' content',
          'page callback' => 'domain_content_view',
          'page arguments' => array(
            $domain['domain_id'],
            FALSE,
          ),
          'access callback' => 'domain_content_check',
          'access arguments' => array(
            $domain['domain_id'],
          ),
          'file' => 'domain_content.admin.inc',
          'description' => 'View content assigned to ' . filter_xss_admin($domain['subdomain']),
          'weight' => $domain['domain_id'],
        );
      }
    }
    else {
      $items['admin/domain/content/list'] = array(
        'title' => 'Affiliate site list',
        'page callback' => 'domain_content_list',
        'access callback' => 'domain_content_menu_check',
        'file' => 'domain_content.admin.inc',
        'description' => 'View your list of affiliates',
        'weight' => -10,
      );
      $items['admin/domain/content/%'] = array(
        'title' => 'Affiliate site list',
        'page callback' => 'domain_content_view',
        'page arguments' => array(
          3,
          TRUE,
        ),
        'access callback' => 'domain_content_check',
        'access arguments' => array(
          3,
        ),
        'file' => 'domain_content.admin.inc',
        'description' => 'Content list for a domain',
        'weight' => -10,
      );
    }
  }
  return $items;
}

/**
 * Implements hook_permission().
 */
function domain_content_permission() {
  $permissions = array(
    'access the domain content page' => array(
      'title' => t('Access lists of affiliated content'),
      'description' => t('Allows users to see an overview of content visibile on their assigned domains.'),
    ),
    'review content for all domains' => array(
      'title' => t('Review content for all domains'),
      'description' => t('Allows users to see an overview of all content, listed by domain.'),
      'restrict access' => TRUE,
    ),
  );
  return $permissions;
}

/**
 * Access control for menu items.  There may be another way to do this in Drupal 6.
 *
 * @param $check
 *   The access check value passed from hook_menu().
 */
function domain_content_menu_check() {
  if (user_access('access the domain content page') || user_access('review content for all domains')) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Access checking routine for menu and node editing checks.
 *
 * @param $domain_id
 *   An id representing the currently active domain record.
 *
 * @return
 *  Boolean true or false.
 */
function domain_content_check($domain_id) {
  global $user;

  // If the user can bypass node access, just return TRUE.
  if (user_access('bypass node access') || user_access('review content for all domains')) {
    return TRUE;
  }

  // Otherwise, the user must be able to edit domain content.
  $rule = user_access('access the domain content page');
  if (!$rule) {
    return FALSE;
  }
  $domains = domain_get_user_domains($user);

  // Can this user see the requested site?
  if (!empty($domains[$domain_id])) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Implement hook_node_operations()
 */
function domain_content_node_operations() {

  // Only privileged users can perform this operation.
  // Do not show this on the default node editing form.
  if (user_access('set domain access')) {
    $operations = array(
      'domain' => array(
        'label' => t('Change affiliate publishing options'),
        'callback' => 'domain_content_node_operations_process',
      ),
    );
    return $operations;
  }
  return array();
}

/**
 * Callback for domain_content_node_operations().
 *
 * This callback is required, but we actually do our action inside
 * of domain_content_process_nodes().
 */
function domain_content_node_operations_process($nodes) {
}

/**
 * Allow domain assignment to be made from the default content form.
 */
function domain_content_form_node_admin_content_alter(&$form, &$form_state) {

  // Privileged users can make global changes to Domain Access permissions.
  if (!user_access('set domain access') || isset($form['operation']['#value'])) {
    return;
  }

  // Add our form elements.
  domain_content_add_form_widget($form);
  $form['admin']['#weight'] = 4;
  $form['domain']['#weight'] = 2;
  $form['admin']['options']['submit']['#submit'][] = 'domain_content_process_nodes';
}

/**
 * Abstraction function for selecting domains for batch operations.
 */
function domain_content_add_form_widget(&$form) {
  global $_domain;

  // We have different settings for the two form contexts.
  // A blank form comes from core.
  $prefix = '<div class="description">' . t('If you select <em>Change affiliate publishing options</em>, you should confirm the <em>Affiliate publishing options</em> settings below.') . '</div>';
  $collapsed = TRUE;
  if (empty($form)) {
    $prefix = '';
    $collapsed = FALSE;
  }
  $options = array();
  $format = domain_select_format();
  foreach (domain_domains() as $data) {

    // The domain must be valid.
    if ($data['valid'] || user_access('access inactive domains')) {

      // Filter checkboxes but not select lists.
      $options[$data['domain_id']] = empty($format) ? check_plain($data['sitename']) : $data['sitename'];
    }
  }
  $form['domain'] = array(
    '#type' => 'fieldset',
    '#title' => t('Affiliate publishing options'),
    '#collapsible' => TRUE,
    '#collapsed' => $collapsed,
    '#prefix' => $prefix,
  );
  $form['domain']['behavior'] = array(
    '#type' => 'radios',
    '#title' => t('Update behavior'),
    '#options' => array(
      0 => t('Replace old values with new settings'),
      1 => t('Add new settings to existing values'),
      2 => t('Remove selected domains from existing values'),
    ),
    '#description' => t('Defines how new grants will be applied to the updated nodes.'),
    '#default_value' => 0,
  );
  $form['domain']['domain_site'] = array(
    '#type' => 'checkbox',
    '#prefix' => t('<p><b>Publishing options:</b>'),
    '#suffix' => '</p>',
    '#title' => t('Send to all affiliates'),
    '#required' => FALSE,
    '#description' => t('Select if this content can be shown to all affiliates.  This setting will override the options below.'),
    '#default_value' => DOMAIN_INSTALL_RULE,
  );
  $form['domain']['domains'] = array(
    '#type' => empty($format) ? 'checkboxes' : 'select',
    '#title' => t('Publish to'),
    '#options' => $options,
    '#required' => FALSE,
    '#description' => t('Select which affiliates can access this content.'),
    '#default_value' => array(
      $_domain['domain_id'],
    ),
  );
  if ($format) {
    $form['domain']['domains']['#multiple'] = TRUE;
    $form['domain']['domains']['#size'] = count($options) > 10 ? 10 : count($options);
  }
}

/**
 * Process the form submission.
 *
 * This callback works for the normal operations callback.
 */
function domain_content_process_nodes($form, &$form_state) {
  if ($form_state['values']['operation'] != 'domain') {
    return;
  }
  $options = array();
  $options['domain_site'] = FALSE;
  if ($form_state['values']['domain_site']) {
    $options['domain_site'] = TRUE;
  }
  $options['domain_id'] = array_filter($form_state['values']['domains']);
  $nids = array_filter($form_state['values']['nodes']);
  $options['behavior'] = $form_state['values']['behavior'];
  domain_content_update_nodes($nids, $options);
}

/**
 * Abstraction function that lets us update access rules.
 */
function domain_content_update_nodes($nids, $options) {

  // If our operation is run, 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.
  $domain_site = $options['domain_site'];
  $behavior = $options['behavior'];
  foreach ($nids as $nid) {
    $domains = $options['domain_id'];

    // Other modules need to respond, we have to load the full node.
    $node = node_load($nid);

    // Make sure the node is valid.
    if ($node->nid > 0) {

      // If modifying values, do so here.
      if (!empty($behavior)) {
        $current = domain_get_node_domains($node->nid);

        // Add values to the current set.
        if ($behavior == 1) {
          if (!empty($current['domain_site'])) {
            $domain_site = TRUE;
          }
          $domains += $current['domain_id'];
        }
        else {
          foreach ($domains as $domain_id) {
            if (isset($current['domain_id'][$domain_id])) {
              unset($current['domain_id'][$domain_id]);
            }
          }
          $domains = $current['domain_id'];

          // If all affiliates is selected, remove it.
          if ($domain_site) {
            $domain_site = FALSE;
          }
        }
      }

      // Use our new options, as set above.
      $node->domain_site = $domain_site;
      $node->domains = $domains;

      // Delete our grants and rebuild.
      node_access_acquire_grants($node);
    }
  }

  // Clear the cache.
  cache_clear_all();
}

Related topics

Functions

Namesort descending Description
domain_content_add_form_widget Abstraction function for selecting domains for batch operations.
domain_content_check Access checking routine for menu and node editing checks.
domain_content_form_node_admin_content_alter Allow domain assignment to be made from the default content form.
domain_content_menu Implements hook_menu()
domain_content_menu_check Access control for menu items. There may be another way to do this in Drupal 6.
domain_content_node_operations Implement hook_node_operations()
domain_content_node_operations_process Callback for domain_content_node_operations().
domain_content_permission Implements hook_permission().
domain_content_process_nodes Process the form submission.
domain_content_update_nodes Abstraction function that lets us update access rules.