You are here

scheduler.install in Scheduler 7

Installation file for Scheduler module.

File

scheduler.install
View source
<?php

/**
 * @file
 * Installation file for Scheduler module.
 */

/**
 * Implements hook_schema().
 */
function scheduler_schema() {
  return array(
    'scheduler' => array(
      'description' => 'The main table to hold the scheduler data.',
      'fields' => array(
        'nid' => array(
          'description' => 'The foreign key to node.nid',
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
        ),
        'publish_on' => array(
          'description' => 'The UNIX UTC timestamp when to publish',
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'default' => 0,
        ),
        'unpublish_on' => array(
          'description' => 'The UNIX UTC timestamp when to unpublish',
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => TRUE,
          'default' => 0,
        ),
      ),
      'indexes' => array(
        'scheduler_publish_on' => array(
          'publish_on',
        ),
        'scheduler_unpublish_on' => array(
          'unpublish_on',
        ),
      ),
      'primary key' => array(
        'nid',
      ),
    ),
  );
}

/**
 * Implements hook_uninstall().
 */
function scheduler_uninstall() {

  // Keep variables in alphabetic order for easier maintenance and patching.
  $variables = array(
    'scheduler_allow_date_only',
    'scheduler_cache_clear_all',
    'scheduler_date_format',
    'scheduler_date_only_format',
    'scheduler_default_time',
    'scheduler_extra_info',
    'scheduler_date_popup_minute_increment',
    'scheduler_field_type',
    'scheduler_lightweight_access_key',
    'scheduler_lightweight_log',
    'scheduler_time_only_format',
  );
  $types = node_type_get_types();
  foreach ($types as $type) {
    $type_name = $type->type;
    $variables[] = "scheduler_expand_fieldset_" . $type_name;
    $variables[] = "scheduler_publish_enable_" . $type_name;
    $variables[] = "scheduler_publish_touch_" . $type_name;
    $variables[] = "scheduler_publish_required_" . $type_name;
    $variables[] = "scheduler_publish_revision_" . $type_name;
    $variables[] = "scheduler_publish_past_date_" . $type_name;
    $variables[] = "scheduler_unpublish_enable_" . $type_name;
    $variables[] = "scheduler_unpublish_required_" . $type_name;
    $variables[] = "scheduler_unpublish_revision_" . $type_name;
    $variables[] = "scheduler_use_vertical_tabs_" . $type_name;
  }
  foreach ($variables as $variable) {
    variable_del($variable);
  }
}

/**
 * Implements hook_update_last_removed().
 */
function scheduler_update_last_removed() {

  // The updates for Scheduler 6.x are no longer required in this file.
  // This function shows that the last update was scheduler_update_6101.
  return 6101;
}

/**
 * Update roles with new 'View Scheduled Content' permission.
 */
function scheduler_update_7100() {

  // Retrieve all roles that have both the 'administer nodes' and 'schedule
  // (un)publishing of nodes' permissions.
  // @see http://www.drupal.org/node/2355401
  $query = db_select('role_permission', 'r');
  $query
    ->fields('r', array(
    'rid',
  ));
  $query
    ->condition('r.permission', 'administer nodes', '=');
  $query
    ->addJoin('INNER', 'role_permission', 'r2', 'r.rid = r2.rid');
  $query
    ->condition('r2.permission', 'schedule (un)publishing of nodes', '=');

  // Grant these roles the 'view scheduled content' permission.
  if ($roles_to_update = $query
    ->execute()
    ->fetchCol()) {
    foreach ($roles_to_update as $rid) {

      // Use db_merge not db_insert in case the role already has the permission.
      $query = db_merge('role_permission');
      $query
        ->key(array(
        'rid' => $rid,
        'permission' => 'view scheduled content',
        'module' => 'scheduler',
      ));
      $query
        ->fields(array(
        'rid' => $rid,
        'permission' => 'view scheduled content',
        'module' => 'scheduler',
      ));
      $query
        ->execute();
    }
  }
  return format_plural(count($roles_to_update), '1 role updated with view scheduled content permission.', '@count roles updated with view scheduled content permission.');
}

/**
 * Delete obsolete Scheduler rows for nodes already unpublished.
 */
function scheduler_update_7101() {

  // Retrieve all unpublished nodes which have a row in the Scheduler with an
  // unpublish-on date in the past and no publish-on date.
  // @see http://www.drupal.org/node/2355129
  $query = db_select('scheduler', 's');
  $query
    ->addField('s', 'nid');
  $query
    ->addJoin('INNER', 'node', 'n', 's.nid = n.nid');
  $query
    ->condition('n.status', NODE_NOT_PUBLISHED, '=');
  $query
    ->condition('s.publish_on', 0, '=');
  $query
    ->condition('s.unpublish_on', 0, '>');
  $query
    ->condition('s.unpublish_on', REQUEST_TIME, '<');

  // Delete these rows from the Scheduler table.
  if ($nids_to_delete = $query
    ->execute()
    ->fetchCol()) {
    db_delete('scheduler')
      ->condition('nid', $nids_to_delete, 'IN')
      ->execute();
  }
  return format_plural(count($nids_to_delete), '1 obsolete row deleted from scheduler table.', '@count obsolete rows deleted from scheduler table.');
}

/**
 * Function scheduler_update_7102() removed and replaced by 7103.
 *
 * @see http://www.drupal.org/node/2706119
 */

/**
 * Update role_permission table with clean machine name.
 */
function scheduler_update_7103() {

  // Change all values of 'schedule (un)publishing of nodes' to the cleaner
  // 'schedule publishing of nodes'.
  // @see http://www.drupal.org/node/2538002
  //
  // Updates done in two stages to avoid integrity constraint violation. First
  // select all role ids which already have the new permission value.
  $query = db_select('role_permission', 'rp')
    ->fields('rp', array(
    'rid',
    'permission',
  ))
    ->condition('permission', 'schedule publishing of nodes');

  // Delete the rows for these roles which also have the old permission value,
  // as these are no longer needed and should not be updated to the new value.
  $rows_deleted = 0;
  if ($rows_to_delete = $query
    ->execute()
    ->fetchCol()) {
    $rows_deleted = db_delete('role_permission')
      ->condition('rid', $rows_to_delete, 'IN')
      ->condition('permission', 'schedule (un)publishing of nodes', '=')
      ->execute();
  }

  // Now update any other rows which still have the old permission value.
  $rows_updated = db_update('role_permission')
    ->fields(array(
    'permission' => 'schedule publishing of nodes',
  ))
    ->condition('permission', 'schedule (un)publishing of nodes', '=')
    ->execute();
  return format_plural($rows_updated, '1 row updated', '@count rows updated') . ', ' . format_plural($rows_deleted, '1 row deleted', '@count rows deleted') . ' ' . t('in role_permission table');
}

Functions

Namesort descending Description
scheduler_schema Implements hook_schema().
scheduler_uninstall Implements hook_uninstall().
scheduler_update_7100 Update roles with new 'View Scheduled Content' permission.
scheduler_update_7101 Delete obsolete Scheduler rows for nodes already unpublished.
scheduler_update_7103 Update role_permission table with clean machine name.
scheduler_update_last_removed Implements hook_update_last_removed().