You are here

gathercontent.install in GatherContent 7.3

File

gathercontent.install
View source
<?php

/**
 * @file
 * Install and uninstall script for GatherContent module.
 */
require_once 'includes/Account.inc';
require_once 'includes/Template.inc';

/**
 * Implements hook_install().
 */
function gathercontent_install() {
  db_add_field('node', 'gathercontent_mapping_id', array(
    'definition' => 'The ID of GatherContent mapping.',
    'type' => 'int',
    'unsigned' => TRUE,
  ));
  db_add_field('node', 'gathercontent_id', array(
    'definition' => 'The ID of content in GatherContent.',
    'type' => 'int',
    'unsigned' => TRUE,
  ));
  db_add_field('node', 'gathercontent_import_status', array(
    'definition' => 'The ID of content in GatherContent.',
    'type' => 'text',
  ));
  db_add_field('file_managed', 'gathercontent_id', array(
    'definition' => 'The ID of file in GatherContent.',
    'type' => 'int',
    'unsigned' => TRUE,
  ));
  _gathercontent_create_text_formats();
}
function _gathercontent_create_text_formats() {
  $filtered_html_format = array(
    'format' => 'filtered_gathercontent_html',
    'name' => 'Filtered GatherContent HTML',
    'weight' => 0,
    'filters' => array(
      // URL filter.
      'filter_url' => array(
        'weight' => 0,
        'status' => 1,
      ),
      // HTML filter.
      'filter_html' => array(
        'weight' => 1,
        'status' => 1,
      ),
      // Line break filter.
      'filter_autop' => array(
        'weight' => 2,
        'status' => 1,
      ),
      // HTML corrector filter.
      'filter_htmlcorrector' => array(
        'weight' => 10,
        'status' => 1,
      ),
    ),
  );
  $filtered_html_format = (object) $filtered_html_format;
  filter_format_save($filtered_html_format);
}

/**
 * Implements hook_uninstall().
 */
function gathercontent_uninstall() {
  variable_del('gathercontent_username');
  variable_del('gathercontent_api_key');
  variable_del('gathercontent_account');
  variable_del('gathercontent_node_update_method');
  db_drop_field('node', 'gathercontent_mapping_id');
  db_drop_field('node', 'gathercontent_id');
  db_drop_field('node', 'gathercontent_import_status');
  db_drop_field('file_managed', 'gathercontent_id');
}

/**
 * Implements schema for our custom entity.
 *
 * @inheritdoc
 */
function gathercontent_schema() {
  $schema = array();
  $schema['gathercontent_mapping'] = array(
    'description' => 'The base table for the Mapping entity',
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'gathercontent_project_id' => array(
        'type' => 'int',
        'length' => 11,
        'not null' => TRUE,
      ),
      'gathercontent_project' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'gathercontent_template_id' => array(
        'type' => 'int',
        'length' => 11,
        'not null' => TRUE,
      ),
      'gathercontent_template' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'content_type' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
      ),
      'data' => array(
        'type' => 'blob',
        'not null' => FALSE,
        'size' => 'big',
        'serialize' => TRUE,
      ),
      'created' => array(
        'type' => 'int',
        'not null' => TRUE,
      ),
      'updated_drupal' => array(
        'type' => 'int',
        'not null' => FALSE,
      ),
      'template' => array(
        'type' => 'blob',
        'not null' => FALSE,
        'size' => 'big',
        'serialize' => TRUE,
      ),
    ),
    'primary key' => array(
      'id',
    ),
  );
  $schema['gathercontent_operation'] = array(
    'description' => 'The base table for the GC Operation entity',
    'fields' => array(
      'uuid' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'type' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array(
      'uuid',
    ),
  );
  $schema['gathercontent_operation_item'] = array(
    'description' => 'The base table for the GC Operation Item entity',
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'operation_uuid' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the GatherContent operation item was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'changed' => array(
        'description' => 'The Unix timestamp when the GatherContent operation item was most recently saved.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'item_status' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'item_status_color' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'item_name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'template_name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'status' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'gathercontent_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array(
      'id',
    ),
    'indexes' => array(
      'gc_id_created' => array(
        'gathercontent_id',
        'created',
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_schema_alter().
 *
 * Adding extra properties to node and file_managed.
 *
 * @inheritdoc
 */
function gathercontent_schema_alter(&$schema) {
  $schema['node']['fields']['gathercontent_mapping_id'] = array(
    'definition' => 'The ID of GatherContent mapping.',
    'type' => 'int',
    'unsigned' => TRUE,
  );
  $schema['node']['fields']['gathercontent_id'] = array(
    'definition' => 'The ID of content in GatherContent.',
    'type' => 'int',
    'unsigned' => TRUE,
  );
  $schema['node']['fields']['gathercontent_import_status'] = array(
    'definition' => 'The ID of content in GatherContent.',
    'type' => 'text',
  );
  $schema['file_managed']['fields']['gathercontent_id'] = array(
    'definition' => 'The ID of file in GatherContent.',
    'type' => 'int',
    'unsigned' => TRUE,
  );
  if (isset($schema['gathercontent_mapping']['fields']['updated_gathercontent'])) {
    unset($schema['gathercontent_mapping']['fields']['updated_gathercontent']);
  }
}

/**
 * Change `content_type` and `data` fields from `gathercontent_mapping` table.
 */
function gathercontent_update_7301() {
  db_change_field('gathercontent_mapping', 'content_type', 'content_type', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => FALSE,
    'default' => '',
  ));
  db_change_field('gathercontent_mapping', 'data', 'data', array(
    'type' => 'blob',
    'not null' => FALSE,
    'size' => 'big',
    'serialize' => TRUE,
  ));
}

/**
 * Add field `gathercontent_import_status` to `node` table.
 */
function gathercontent_update_7302() {
  db_add_field('node', 'gathercontent_import_status', array(
    'definition' => 'The ID of content in GatherContent.',
    'type' => 'text',
  ));
}

/**
 * Set `gathercontent_urlkey` variable.
 */
function gathercontent_update_7303() {

  // Load all accounts.
  $acc_obj = new GatherContent\Account();
  $accounts = $acc_obj
    ->getAccounts();

  // Load local account and extract account id.
  $local_account = variable_get('gathercontent_account');
  reset($local_account);
  $local_account_id = key($local_account);

  // Find urlkey for local account id and store it in variable.
  foreach ($accounts as $account) {
    if ($local_account_id === $account->id) {
      variable_set('gathercontent_urlkey', $account->slug);
      break;
    }
  }
}

/**
 * Remove `updated_gathercontent` field from DB.
 */
function gathercontent_update_7304() {
  db_drop_field('gathercontent_mapping', 'updated_gathercontent');
}

/**
 * Add field `template` to `gathercontent_mapping` table.
 */
function gathercontent_update_7305() {
  db_add_field('gathercontent_mapping', 'template', array(
    'type' => 'blob',
    'not null' => FALSE,
    'size' => 'big',
    'serialize' => TRUE,
  ));
}

/**
 * Set templates to existing mappings.
 */
function gathercontent_update_7306() {
  $mappings = entity_load('gathercontent_mapping');
  $tmp = new GatherContent\Template();
  foreach ($mappings as $mapping) {
    if (empty($mapping->template)) {
      $template = $tmp
        ->getTemplate($mapping->gathercontent_template_id);
      $mapping->template = serialize($template);
      $mapping
        ->save();
    }
  }
}

/**
 * Create tables for gathercontent_operation and gathercontent_operation_item entities.
 */
function gathercontent_update_7307() {
  db_create_table('gathercontent_operation', drupal_get_schema_unprocessed('gathercontent', 'gathercontent_operation'));
  db_create_table('gathercontent_operation_item', drupal_get_schema_unprocessed('gathercontent', 'gathercontent_operation_item'));
}

/**
 * Create custom GatherContent formats and revert views.
 */
function gathercontent_update_7308() {
  _gathercontent_create_text_formats();
  $views = views_get_all_views();

  // Find all overridden views.
  foreach ($views as $view) {
    if ($view->disabled) {
      continue;
    }
    if ($view->type == t('Overridden')) {

      // Revert the view.
      $view
        ->delete();

      // Clear its cache.
      ctools_include('object-cache');
      ctools_object_cache_clear('view', $view->name);
    }
  }
}

/**
 * Add default sort for Mapping view.
 */
function gathercontent_update_7309() {
  ctools_include('object-cache');
  ctools_object_cache_clear('view', 'mapping');
}

/**
 * Add created and changed field for OperationItem.
 */
function gathercontent_update_7310() {
  $created = array(
    'description' => 'The Unix timestamp when the GatherContent operation item was created.',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
  );
  $changed = array(
    'description' => 'The Unix timestamp when the GatherContent operation item was most recently saved.',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
  );
  db_add_field('gathercontent_operation_item', 'created', $created);
  db_add_field('gathercontent_operation_item', 'changed', $changed);
  db_add_index('gathercontent_operation_item', 'gc_id_created', array(
    'gathercontent_id',
    'created',
  ));
}

/**
 * Fix permissions on views.
 */
function gathercontent_update_7311() {
  ctools_include('object-cache');
  ctools_object_cache_clear('view', 'mapping');
}

/**
 * Implements hook_requirements().
 */
function gathercontent_requirements($phase) {
  if ($phase !== 'runtime') {
    return array();
  }
  $t = get_t();
  $requirements = array(
    'gathercontent_tablesorter' => array(
      'title' => $t('GatherContent: Tablesorter plugin'),
      'value' => $t('Unavailable.'),
      'description' => $t('Install <a href="@libraries-page">Libraries</a> module.', array(
        '@libraries-page' => 'https://www.drupal.org/project/libraries',
      )),
      'severity' => REQUIREMENT_INFO,
    ),
  );
  if (module_exists('libraries')) {
    $tablesorter_lib_info = libraries_detect('tablesorter-mottie');
    if ($tablesorter_lib_info['installed'] && $tablesorter_lib_info['version']) {
      $requirements['gathercontent_tablesorter']['value'] = $tablesorter_lib_info['version'];
      $requirements['gathercontent_tablesorter']['severity'] = REQUIREMENT_OK;
      unset($requirements['gathercontent_tablesorter']['description']);
    }
    else {
      $requirements['gathercontent_tablesorter']['description'] = $t('Download <a href="@tablesorter-github">Mottie\'s tablesorter plugin</a> to <code>sites/all/libraries</code> toget sortable table headers at GatherContent admin UI. Rename it\'s folder to <code>tablesorter-mottie</code>.', array(
        '@tablesorter-github' => 'https://github.com/Mottie/tablesorter/archive/v2.25.8.zip',
      ));
    }
  }
  return $requirements;
}

Functions

Namesort descending Description
gathercontent_install Implements hook_install().
gathercontent_requirements Implements hook_requirements().
gathercontent_schema Implements schema for our custom entity.
gathercontent_schema_alter Implements hook_schema_alter().
gathercontent_uninstall Implements hook_uninstall().
gathercontent_update_7301 Change `content_type` and `data` fields from `gathercontent_mapping` table.
gathercontent_update_7302 Add field `gathercontent_import_status` to `node` table.
gathercontent_update_7303 Set `gathercontent_urlkey` variable.
gathercontent_update_7304 Remove `updated_gathercontent` field from DB.
gathercontent_update_7305 Add field `template` to `gathercontent_mapping` table.
gathercontent_update_7306 Set templates to existing mappings.
gathercontent_update_7307 Create tables for gathercontent_operation and gathercontent_operation_item entities.
gathercontent_update_7308 Create custom GatherContent formats and revert views.
gathercontent_update_7309 Add default sort for Mapping view.
gathercontent_update_7310 Add created and changed field for OperationItem.
gathercontent_update_7311 Fix permissions on views.
_gathercontent_create_text_formats