You are here

instagram_feeds.install in Instagram Feeds 7

Same filename and directory in other branches
  1. 8 instagram_feeds.install

Instagram Feeds feature updates. Update hooks - enable modules, etc.

File

instagram_feeds.install
View source
<?php

/**
 * @file
 * Instagram Feeds feature updates.
 * Update hooks - enable modules, etc.
 */

/**
 * Implements hook_install().
 */
function instagram_feeds_install() {
  $t = get_t();
  db_query("update {system} SET weight = 6 where name = 'instagram_feeds'");
  drupal_set_message($t('Instagram Feeds module installed.'));
  $access_token = variable_get('instagram_feeds_access_token', '');
  if (empty($access_token)) {
    drupal_set_message($t('You must setup <a rhef="@instagram_settings">Instagram Feeds settings</a>.', array(
      '@instagram_settings' => url('admin/config/services/instagram'),
    )), 'warning');
  }
}

/**
 * Implements hook_uninstall().
 */
function instagram_feeds_uninstall() {
  _instagram_feeds_uninstall();
}

/**
 * Check installation requirements.
 *
 * Check installation requirements and do status reporting if Instagram
 * settings are wrong.
 */
function instagram_feeds_requirements($phase) {
  $requirements = array();
  $t = get_t();
  $client_id = variable_get('instagram_feeds_client_id', '');
  $access_token = variable_get('instagram_feeds_access_token', '');
  if (empty($client_id) && 'runtime' == $phase) {
    $requirements['instagram_feeds_client_id'] = array(
      'title' => $t('Instagram Feeds Client ID'),
      'severity' => REQUIREMENT_ERROR,
      'value' => $t('Empty'),
      'description' => $t('You must setup <a href="@client_id">client ID</a> from Instagram API.', array(
        '@client_id' => url('admin/config/services/instagram'),
      )),
    );
  }
  if (empty($access_token) && 'runtime' == $phase) {
    $requirements['instagram_feeds_token'] = array(
      'title' => $t('Instagram Feeds Access Token'),
      'severity' => REQUIREMENT_ERROR,
      'value' => $t('Empty'),
      'description' => $t('You must setup <a href="@access_token">Access Token</a> from Instagram API.', array(
        '@access_token' => url('admin/config/services/instagram'),
      )),
    );
  }
  return $requirements;
}

/**
 * Helper function to run batch operations for removing all Instagram content.
 */
function _instagram_feeds_uninstall() {
  require_once drupal_get_path('module', 'instagram_feeds') . '/instagram_feeds.module';
  $operations = array();

  // Delete flags.
  $operations[] = array(
    'instagram_feeds_uninstall_process',
    array(
      'delete_flags',
      array(
        'flag_as_inappropriate',
        'image_archived',
      ),
    ),
  );

  // Delete Instagram blocks.
  $operations[] = array(
    'instagram_feeds_uninstall_process',
    array(
      'blocks_delete',
      array(),
    ),
  );

  // Delete Instagram settings.
  $operations[] = array(
    'instagram_feeds_uninstall_process',
    array(
      'variables_delete',
      array(),
    ),
  );

  // Delete Instagram Feeds Importer.
  $operations[] = array(
    'instagram_feeds_uninstall_process',
    array(
      'feeds_importer_delete',
      INSTAGRAM_FEEDS_FEED_ID,
    ),
  );

  // Delete fields for taxonomy.
  $operations[] = array(
    'instagram_feeds_uninstall_process',
    array(
      'fields_delete',
      array(
        'entity_type' => 'taxonomy_term',
        'bundle' => INSTAGRAM_FEEDS_USERS_VOCABULARY_NAME,
      ),
    ),
  );

  // Delete hashtags.
  $operations[] = array(
    'instagram_feeds_uninstall_process',
    array(
      'taxonomy_delete',
      INSTAGRAM_FEEDS_TAGS_VOCABULARY_NAME,
    ),
  );

  // Delete Instagram users.
  $operations[] = array(
    'instagram_feeds_uninstall_process',
    array(
      'taxonomy_delete',
      INSTAGRAM_FEEDS_USERS_VOCABULARY_NAME,
    ),
  );

  // Delete nodes.
  $node_types = array(
    INSTAGRAM_FEEDS_IMAGE_NODE_TYPE,
    INSTAGRAM_FEEDS_FEEDS_NODE_TYPE,
    INSTAGRAM_FEEDS_SETTINGS_NODE_TYPE,
  );
  $nids = db_select('node', 'n')
    ->fields('n', array(
    'nid',
  ))
    ->condition('type', $node_types, 'IN')
    ->execute()
    ->fetchCol();
  if (count($nids)) {
    $nids_to_delete_chunks = array_chunk($nids, 50);
    foreach ($nids_to_delete_chunks as $nids_to_delete) {
      $operations[] = array(
        'instagram_feeds_uninstall_process',
        array(
          'node_delete',
          $nids_to_delete,
        ),
      );
    }
  }
  foreach ($node_types as $node_type) {
    $operations[] = array(
      'instagram_feeds_uninstall_process',
      array(
        'node_type_delete',
        $node_type,
      ),
    );
    $operations[] = array(
      'instagram_feeds_uninstall_process',
      array(
        'fields_delete',
        array(
          'entity_type' => 'node',
          'bundle' => $node_type,
        ),
      ),
    );
  }
  $operations[] = array(
    'instagram_feeds_uninstall_process',
    array(
      'cache_clear',
      array(),
    ),
  );

  // If run unistall with drush.
  if (drupal_is_cli()) {
    foreach ($operations as $operation) {
      call_user_func_array($operation['0'], $operation[1]);
    }
  }
  else {
    $batch = array(
      'operations' => $operations,
      'title' => t('Uninstalling Instagram Feeds'),
      'init_message' => t('Batch is starting...'),
      'progress_message' => t('Processed @current out of @total.'),
      'error_message' => t('Batch has encountered an error.'),
      'file' => drupal_get_path('module', 'instagram_feeds') . '/instagram_feeds.install',
    );
    batch_set($batch);
  }
}

/**
 * Callback function to implement batch process operations.
 */
function instagram_feeds_uninstall_process($operation, $data, &$context = array()) {
  switch ($operation) {
    case 'node_type_delete':
      $types = node_type_get_types();
      node_type_delete($data);
      $context['message'] = t('Removing node type !type', array(
        '!type' => $types[$data]->name,
      ));
      break;
    case 'fields_delete':
      $fields = field_info_instances($data['entity_type'], $data['bundle']);
      foreach ($fields as $field) {
        $instance = field_info_instance($data['entity_type'], $field['field_name'], $data['bundle']);
        field_delete_instance($instance);
      }
      $context['message'] = t('Removing fields for !entity_type', array(
        '!entity_type' => $data['entity_type'],
      ));
      break;
    case 'blocks_delete':
      $context['message'] = t('Removing blocks...');
      db_delete('block')
        ->condition('module', 'instagram_feeds')
        ->execute();
      db_delete('block_role')
        ->condition('module', 'instagram_feeds')
        ->execute();
      break;
    case 'node_delete':
      node_delete_multiple($data);
      $context['message'] = t('Removing nodes...');
      break;
    case 'taxonomy_delete':
      $vocabulary = taxonomy_vocabulary_machine_name_load($data);
      taxonomy_vocabulary_delete($vocabulary->vid);
      $context['message'] = t('Terms and vocabulary !vocabulary were deleted.', array(
        '!vocabulary' => $vocabulary->name,
      ));
      break;
    case 'variables_delete':
      variable_del('instagram_feeds_client_id');
      variable_del('instagram_feeds_client_secret');
      variable_del('instagram_feeds_access_token');
      variable_del('instagram_feeds_items_expiration_time');
      variable_del('instagram_feeds_block_ids');
      variable_del('instagram_feeds_remove_unused_terms');
      variable_del('instagram_feeds_download_attempts');
      variable_del('instagram_feeds_limit_imgs_per_feed');
      $context['message'] = t('Instagram Feeds settings deleted.');
      break;
    case 'feeds_importer_delete':
      feeds_importer($data)
        ->delete();
      $context['message'] = t('Instagram Feed Importer deleted.');
      break;
    case 'cache_clear':
      cache_clear_all();
      $context['message'] = t('All cache was cleared.');
      break;
    case 'delete_flags':
      if (module_exists('flag')) {
        $context['message'] = t('Deleting flags.');
        foreach ($data as $flag_name) {
          $flag = flag_load($flag_name, TRUE);
          if ($flag) {
            $flag
              ->delete();
          }
        }
      }
      break;
  }
  if (drupal_is_cli() && isset($context['message']) && !empty($context['message'])) {
    echo $context['message'], "\n";
  }
}

Functions

Namesort descending Description
instagram_feeds_install Implements hook_install().
instagram_feeds_requirements Check installation requirements.
instagram_feeds_uninstall Implements hook_uninstall().
instagram_feeds_uninstall_process Callback function to implement batch process operations.
_instagram_feeds_uninstall Helper function to run batch operations for removing all Instagram content.