You are here

subscriptions_taxonomy.install in Subscriptions 7

Same filename and directory in other branches
  1. 5.2 subscriptions_taxonomy.install
  2. 6 subscriptions_taxonomy.install

Subscriptions Taxonomy module installation.

File

subscriptions_taxonomy.install
View source
<?php

/**
 * @file
 * Subscriptions Taxonomy module installation.
 */

/**
 * Implements hook_install().
 */
function subscriptions_taxonomy_install() {
}

/**
 * Implements hook_update_last_removed().
 *
 * @return int
 */
function subscriptions_taxonomy_update_last_removed() {
  return 2;
}

/**
 * Implements hook_uninstall().
 */
function subscriptions_taxonomy_uninstall() {
}

/**
 * Remove orphaned taxonomy term subscriptions records.
 */
function subscriptions_taxonomy_update_7001() {

  // Fetch all existing term ids.
  $taxonomy_tids = db_select('taxonomy_term_data', 'ttd')
    ->fields('ttd', array(
    'tid',
  ))
    ->execute()
    ->fetchCol();

  // Fetch all tids from subscriptions table.
  $subscriptions_tids = db_select('subscriptions', 'sub')
    ->fields('sub', array(
    'value',
  ))
    ->distinct('sub.value')
    ->condition('module', 'node')
    ->condition('field', 'tid')
    ->execute()
    ->fetchCol();

  // Fetch all tids from subscriptions_queue table.
  $subscriptions_queue_tids = db_select('subscriptions_queue', 'subq')
    ->fields('subq', array(
    'value',
  ))
    ->distinct('subq.value')
    ->condition('module', 'node')
    ->condition('field', 'tid')
    ->execute()
    ->fetchCol();

  // Delete orphaned entries from subscriptions table.
  $subscriptions_tids_diff = array_diff($subscriptions_tids, $taxonomy_tids);
  if (!empty($subscriptions_tids_diff)) {
    db_delete('subscriptions')
      ->condition('module', 'node')
      ->condition('field', 'tid')
      ->condition('value', $subscriptions_tids_diff)
      ->execute();
  }

  // Delete orphaned entries from subscriptions_queue table.
  $subscriptions_queue_tids_diff = array_diff($subscriptions_queue_tids, $taxonomy_tids);
  if (!empty($subscriptions_queue_tids_diff)) {
    db_delete('subscriptions_queue')
      ->condition('module', 'node')
      ->condition('field', 'tid')
      ->condition('value', $subscriptions_tids_diff)
      ->execute();
  }
}