You are here

sharerich.install in Sharerich 7.3

Same filename and directory in other branches
  1. 8 sharerich.install
  2. 7 sharerich.install
  3. 7.2 sharerich.install

Install hooks for Sharerich project.

File

sharerich.install
View source
<?php

/**
 * @file
 * Install hooks for Sharerich project.
 */

/**
 * Implements hook_schema().
 */
function sharerich_schema() {
  $schema['sharerich_sets'] = array(
    'description' => 'Stores Sharerich Sets.',
    'export' => array(
      'key' => 'machinename',
      'primary key' => 'swid',
      'identifier' => 'sharerich_set',
      // Exports will be available as $sharerich_set
      'default hook' => 'default_sharerich_sets',
      // Function hook name.
      'api' => array(
        'owner' => 'sharerich',
        'api' => 'default_sharerich_sets',
        // Base name for api include files.
        'minimum_version' => 1,
        'current_version' => 1,
      ),
      'load callback' => 'sharerich_set_load',
    ),
    'fields' => array(
      'swid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Primary ID field for the table. Not used for anything except internal lookups.',
        'no export' => TRUE,
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => "The unique name.",
      ),
      'machinename' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => "The machine readable name.",
      ),
      'title' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
        'description' => "The title that appears above the share buttons.",
      ),
      'block' => array(
        'type' => 'int',
        'default' => 0,
        'description' => 'If true, a block will be created in which to display this buttons set.',
      ),
      'services' => array(
        'type' => 'blob',
        'size' => 'big',
        'serialize' => TRUE,
        'description' => "Serialized array of enabled Services.",
      ),
    ),
    'primary key' => array(
      'swid',
    ),
    'unique keys' => array(
      'machinename' => array(
        'machinename',
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_requirements().
 */
function sharerich_requirements($phase) {
  $requirements = array();
  $t = get_t();
  if ($phase == 'runtime') {
    module_load_include('module', 'libraries', 'libraries');

    // require.js library.
    $library = libraries_detect('rrssb');
    $requirements['sharerich_rrssb'] = array(
      'title' => $t('@name library', array(
        '@name' => $library['title'],
      )),
      'value' => $t('Unknown'),
    );
    $requirements['sharerich_rrssb']['severity'] = REQUIREMENT_OK;
    if (!$library['installed']) {
      $requirements['sharerich_rrssb']['value'] = ucfirst($library['error']);
      $requirements['sharerich_rrssb']['description'] = $library['error message'];
      $options = array(
        '!url' => $library['download url'],
        '@name' => $library['name'],
        '%path' => 'libraries/' . $library['machine name'],
      );
      $requirements['sharerich_rrssb']['description'] .= ' ' . $t('Download the library from the <a href="!url">download page</a> and make sure it is installed in the %path folder.', $options);
      $requirements['sharerich_rrssb']['severity'] = REQUIREMENT_ERROR;
    }
    else {
      $requirements['sharerich_rrssb']['value'] = $t('@version', array(
        '@version' => $library['version'],
      ));
    }
  }
  return $requirements;
}

/**
 * Implements hook_install().
 */
function sharerich_install() {

  // Set paths to libraries.
  if (libraries_load('rrssb')) {
    variable_set('sharerich_add_js', libraries_get_path('rrssb') . '/js/rrssb.min.js');
    variable_set('sharerich_add_css', libraries_get_path('rrssb') . '/css/rrssb.css' . PHP_EOL . drupal_get_path('module', 'sharerich') . '/css/sharerich.css');
  }

  // Create default button set.
  $default_set = ctools_export_crud_new('sharerich_sets');
  $default_set->name = 'Default set';
  $default_set->machinename = 'default_set';
  $default_set->title = t('Share this');
  $default_set->block = TRUE;
  module_load_include('module', 'sharerich', 'sharerich');
  $services = array(
    'email',
    'facebook',
    'twitter',
    'googleplus',
  );
  foreach (sharerich_get_services() as $service_name) {
    if (in_array($service_name, $services)) {
      $default_set->services[$service_name]['enabled'] = TRUE;
      $content = sharerich_get_service_content($service_name);
      $default_set->services[$service_name]['markup'] = $content;
      $default_set->services[$service_name]['weight'] = 0;
    }
  }
  ctools_export_crud_save('sharerich_sets', $default_set);
}

/**
 * Implements hook_uninstall().
 */
function sharerich_uninstall() {
  drupal_uninstall_schema('sharerich');

  // Clean entity configurations.
  foreach (node_type_get_types() as $node) {
    variable_del('sharerich_node_' . $node->type);
  }
  module_load_include('module', 'sharerich', 'sharerich');
  foreach (sharerich_get_services() as $service_name) {
    $var_name = 'sharerich_custom_' . $service_name;
    variable_del($var_name);
  }

  // Clean all the rest.
  variable_del('sharerich_youtube_username');
  variable_del('sharerich_github_username');
  variable_del('sharerich_facebook_app_id');
  variable_del('sharerich_facebook_site_url');
  variable_del('sharerich_add_js');
  variable_del('sharerich_add_css');
  variable_del('sharerich_block_hashes');
  variable_del('sharerich_services');
  variable_del('sharerich_title');
  variable_del('sharerich_library_variant');
  variable_del('sharerich_skip_js');
}

/**
 * Sharerich 2.x upgrade.
 */
function sharerich_update_7200() {
  module_load_include('inc', 'ctools', 'includes/export');

  // Install schema.
  if (!db_table_exists('sharerich_sets')) {
    drupal_install_schema('sharerich');
  }

  // Convert global sets into new ctools storage.
  // Create a new object.
  $imported = ctools_export_crud_new('sharerich_sets');
  $imported->machinename = 'imported_set';
  $imported->title = variable_get('sharerich_title', t('Share this'));
  $imported->block = TRUE;

  // Port legacy services.
  foreach (variable_get('sharerich_services', array()) as $service_name => $value) {
    $imported->services[$service_name]['enabled'] = $service_name === $value ? TRUE : FALSE;
    $var_name = 'sharerich_custom_' . $service_name;
    if ($content = variable_get($var_name, FALSE)) {

      // Import legacy widgets and remove it.
      variable_del($var_name);
    }
    else {

      // Create new widgets.
      $content = sharerich_get_service_content($service_name);
    }
    $imported->services[$service_name]['markup'] = $content;
    $imported->services[$service_name]['weight'] = 0;
  }

  // Save configuration.
  ctools_export_crud_save('sharerich_sets', $imported);

  // Set default set for each content type that has Sharerich enabled.
  foreach (node_type_get_types() as $node) {
    if (variable_get('sharerich_node_' . $node->type, FALSE)) {
      $config = field_bundle_settings('node', $node->type);
      $display =& $config['extra_fields']['display']['sharerich'];
      foreach ($display as $display_name => $item) {
        $display[$display_name]['settings']['sharerich_sets'] = 'imported_set';
      }

      // Save extra field config.
      field_bundle_settings('node', $node->type, $config);
    }
  }

  // Remove old variables.
  variable_del('sharerich_custom_0');
  variable_del('sharerich_services');
  variable_del('sharerich_title');
  variable_del('sharerich_add_css');
  variable_del('sharerich_add_js');
  field_info_cache_clear();
}

Functions

Namesort descending Description
sharerich_install Implements hook_install().
sharerich_requirements Implements hook_requirements().
sharerich_schema Implements hook_schema().
sharerich_uninstall Implements hook_uninstall().
sharerich_update_7200 Sharerich 2.x upgrade.