<?php
// $Id: domain_content.module,v 1.29 2009/08/22 21:56:25 agentken Exp $

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

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

/**
 * Implement hook_menu()
 */
function domain_content_menu() {
  $items = array();
  $items['admin/domain/content'] = array(
    'title' => 'Affiliated content',
    '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
  $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' => filter_xss_admin($domain['sitename']) .' content',
        'page callback' => 'domain_content_view',
        'page arguments' => array($domain['domain_id'], FALSE),
        'access callback' => 'domain_content_check',
        'access arguments' => array($domain),
        '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;
}

/**
 * Implement hook_perm().
 */
function domain_content_perm() {
  return array('configure domain content', 'review content for all domains');
}

/**
 * Implement hook_theme().
 */
function domain_content_theme() {
  $themes = array(
    'domain_content_admin_nodes' => array(
      'arguments' => array('form' => array()),
      'file'      => 'domain_content.admin.inc',
    ),
  );
  return $themes;
}

/**
 * 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('administer nodes')) {
    return TRUE;
  }
  if (user_access('edit domain nodes') || user_access('review content for all domains')) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Access checking routine for menu and node editing checks.
 *
 * @param $domain
 *  An array representing the currently active domain record.
 * @param $all
 *  A boolean flag indicating whether this user can access all domains.
 */
function domain_content_check($domain, $all = FALSE) {
  global $user;
  // If the user can administer nodes, just return TRUE.
  if ($all || user_access('administer nodes')) {
    return TRUE;
  }
  $rule = user_access('edit domain nodes');
  // Otherwise, the user must be able to edit domain nodes.
  if (!$rule) {
    return FALSE;
  }
  $domains = domain_get_user_domains($user);
  $check = FALSE;
  $editor = FALSE;
  // Can this user see the default site?
  if ($rule && $domain['domain_id'] == 0 && $domains['-1'] == -1) {
    $editor = TRUE;
  }
  // Can this user see the active site?
  else if ($rule && $domain['domain_id'] > 0 && $domain['domain_id'] == $domains[$domain['domain_id']]) {
    $editor = TRUE;
  }
  if ($editor) {
    $check = TRUE;
  }
  return $check;
}

/**
 * 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 (arg(0) == 'admin' &&  arg(1) == 'domain' && user_access('set domain access')) {
    $operations = array(
      'domain' => array(
      'label' => t('Change affiliate publishing options'),
      'callback' => 'domain_content_node_operations_access',
      )
    );
    return $operations;
  }
}

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

/**
 * Implement hook_domainupdate()
 */
function domain_content_domainupdate($op, $domain, $form_state = array()) {
  // We rebuild the menu, since the domain records have changed.
  // Clear the page cache, so that changed menus are reflected for anonymous users.
  cache_clear_all('*', 'cache_page', TRUE);
  // Also clear the menu cache.
  cache_clear_all('*', 'cache_menu', TRUE);
}

/**
 * The multiple delete form issues a redirect in its submit function.
 * So we must supply the proper path as a form element.
 *
 * @link http://drupal.org/node/336218
 */
function domain_content_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'node_multiple_delete_confirm') {
    $form['#redirect'] = $_GET['q'];
  }
}

/**
 * Implement hook_domainlinks()
 */
function domain_content_domainlinks($domain) {
  $links[] = array(
    'title' => t('content'),
    'path' => 'admin/domain/content/'. $domain['domain_id']
  );
  return $links;

}
