You are here

smartqueue_domain.module in Smartqueue Domain 7

File

smartqueue_domain.module
View source
<?php

/**
 * Implements hook_views_api().
 */
function smartqueue_domain_views_api() {
  return array(
    'api' => 2,
  );
}

/**
 * Implements hook_init().
 */
function smartqueue_domain_init() {
  global $_domain;

  // Check to see if we are administering a smartqueue.
  if (arg(0) == 'admin' && arg(1) == 'structure' && arg(2) == 'nodequeue' && is_numeric(arg(3)) && arg(4) == 'view' && is_numeric(arg(5))) {

    // If "Force domain editing from the primary domain" is enabled, redirect to
    // primary domain; otherwise redirect to smartqueue's domain.
    if (variable_get('domain_edit_on_primary', 1)) {
      domain_check_primary();
    }
    else {
      $subqueue_id = arg(5);
      $queue = nodequeue_load_subqueues(array(
        $subqueue_id,
      ));
      $queue_info = explode(':', $queue[$subqueue_id]->reference);
      $domains = domain_domains();
      if (isset($queue_info[3])) {
        $domain = $domains[$queue_info[3]];
        if (!isset($domain['domain_id'])) {
          return;
        }
        $path = isset($_GET['q']) ? $_GET['q'] : '';
        if ($_domain['domain_id'] != $domain['domain_id']) {
          drupal_goto($domain['path'] . $path);
        }
      }
    }
  }
}

/**
 * Implements hook_nodequeue_info().
 */
function smartqueue_domain_nodequeue_info() {
  return array(
    'smartqueue_domain' => array(
      'title' => t('Domain smartqueue'),
      'description' => t('Domain smartqueue is a queue that has subqueues for each domain inside your site'),
    ),
  );
}

/**
 * Implements hook_nodequeue_subqueues().
 */
function smartqueue_domain_nodequeue_subqueues(&$queue, $node) {
  smartqueue_domain_check_subqueues($queue);
  $domain = domain_get_domain();
  $domain = domain_lookup($domain['domain_id']);
  $refs = array(
    $queue->name . ':domain:' . $domain['machine_name'] . ':' . $domain['domain_id'],
  );
  return $refs;
}

/**
 * Check the current queue and add the domains subqueues if they don't exist.
 */
function smartqueue_domain_check_subqueues(&$queue) {
  $domains = domain_domains(TRUE);
  foreach ($domains as $id => $domain) {
    $ref = $queue->name . ':domain:' . $domain['machine_name'] . ':' . $domain['domain_id'];
    $subqueue = nodequeue_load_subqueues_by_reference(array(
      $queue->qid => array(
        $ref,
      ),
    ));
    if (empty($subqueue)) {
      nodequeue_add_subqueue($queue, $queue->title . ' - ' . $domain['sitename'], $ref);
    }
  }
}

/**
 * Implements hook_domain_insert().
 */
function smartqueue_domain_domain_insert($domain) {
  $smartqueue_queues = smartqueue_domain_get_queues();
  foreach ($smartqueue_queues as $queue) {
    smartqueue_domain_check_subqueues($queue);
  }
}

/**
 * Implements hook_domain_delete().
 */
function smartqueue_domain_domain_delete($domain, $form_values = array()) {
  $subqueues = smartqueue_domain_get_subqueues_by_domain($domain);
  if (!empty($subqueues)) {
    foreach ($subqueues as $subqueue) {
      nodequeue_remove_subqueue($subqueue->sqid);
    }
  }
}

/**
 * Helper function. Get all "smartqueue" queues belonging to this module.
 *
 * @return
 *   An array of queues, keyed by queue id.
 */
function smartqueue_domain_get_queues() {
  $smartqueue_queues = array();
  $queues = nodequeue_load_queues(nodequeue_get_all_qids());
  foreach ($queues as $qid => $queue) {
    if ($queue->owner == 'smartqueue_domain') {
      $smartqueue_queues[$qid] = $queue;
    }
  }
  return $smartqueue_queues;
}

/**
 * Helper function. Get all subqueues belonging to a domain.
 *
 * @param $domain
 *   A domain object.
 * @return
 *   An array of subqueues, keyed by subqueue id.
 */
function smartqueue_domain_get_subqueues_by_domain($domain) {
  $smartqueue_queues = smartqueue_domain_get_queues();
  $references = array();
  foreach ($smartqueue_queues as $queue) {
    $references[$queue->qid] = array(
      $queue->name . ':domain:' . $domain['machine_name'] . ':' . $domain['domain_id'],
    );
  }
  return nodequeue_load_subqueues_by_reference($references);
}

/**
 * Implements hook_nodequeue_form_submit_finish().
 */
function smartqueue_domain_nodequeue_form_submit_finish($queue, &$form_state) {
  smartqueue_domain_check_subqueues($queue);
}

/**
 * Check the current queue and add the domains subqueues if they don't exist.
 */
function smartqueue_domain_views_check_subqueue($queue) {
  $domain = domain_get_domain();
  $queue_info = explode(':', $queue->reference);
  if (isset($queue_info[3])) {
    $domain_id = $queue_info[3];
    if ($domain['domain_id'] == $domain_id) {
      return TRUE;
    }
  }
  return FALSE;
}

/**
 * Implements hook_subqueue_access().
 *
 * Nodequeue module has already granted access to users with 'manipulate all
 * queues' permission, and denied access to users who don't belong to at least
 * one role assigned to this subqueue. All that is left is to check if the
 * user is allowed to edit content for this domain.
 */
function smartqueue_domain_subqueue_access($subqueue, $account, $queue) {
  $user_domains = domain_get_user_domains($account);
  $subqueue_info = explode(':', $subqueue->reference);
  if (isset($user_domains[-1])) {
    $user_domains[0] = 0;
    unset($user_domains[-1]);
  }
  if (in_array($subqueue_info[3], $user_domains) && user_access('edit domain content') || user_access('set domain access')) {
    return TRUE;
  }
  return FALSE;
}

Functions

Namesort descending Description
smartqueue_domain_check_subqueues Check the current queue and add the domains subqueues if they don't exist.
smartqueue_domain_domain_delete Implements hook_domain_delete().
smartqueue_domain_domain_insert Implements hook_domain_insert().
smartqueue_domain_get_queues Helper function. Get all "smartqueue" queues belonging to this module.
smartqueue_domain_get_subqueues_by_domain Helper function. Get all subqueues belonging to a domain.
smartqueue_domain_init Implements hook_init().
smartqueue_domain_nodequeue_form_submit_finish Implements hook_nodequeue_form_submit_finish().
smartqueue_domain_nodequeue_info Implements hook_nodequeue_info().
smartqueue_domain_nodequeue_subqueues Implements hook_nodequeue_subqueues().
smartqueue_domain_subqueue_access Implements hook_subqueue_access().
smartqueue_domain_views_api Implements hook_views_api().
smartqueue_domain_views_check_subqueue Check the current queue and add the domains subqueues if they don't exist.