You are here

publication_date.install in Publication Date 7.2

Installation functions for the Publication Date module.

File

publication_date.install
View source
<?php

/**
 * @file
 * Installation functions for the Publication Date module.
 */

/**
 * Implements hook_schema().
 */
function publication_date_schema() {
  $schema['publication_date'] = array(
    'description' => 'Keep the publication timestamp for each node.',
    'fields' => array(
      'nid' => array(
        'description' => 'The {node}.nid of the node.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'published_at' => array(
        'description' => 'The timestamp of the node\'s first publication.',
        'type' => 'int',
        'not null' => FALSE,
        'default' => NULL,
      ),
    ),
    'indexes' => array(
      'published_at' => array(
        'published_at',
      ),
    ),
    'primary key' => array(
      'nid',
    ),
  );
  return $schema;
}

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

  // Set publication dates for existing nodes.
  _publication_date_update_existing();

  // This module must be called after some other modules (i.e. Scheduler).
  db_update('system')
    ->fields(array(
    'weight' => 99,
  ))
    ->condition('name', 'publication_date')
    ->execute();
}

/**
 * Helper function to update the existing nodes on install.
 *
 * We can not know the exact date of publication, so $node->published_at will
 * initially contain the creation date for already publisned nodes.
 */
function _publication_date_update_existing() {
  $query = db_select('node');
  $query
    ->addField('node', 'nid');
  $query
    ->addField('node', 'created', 'published_at');
  $nids = $query
    ->condition('status', 1);
  db_insert('publication_date')
    ->from($nids)
    ->execute();
}

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

  // Remove Publication Date configuration from the variables table.
  variable_del('publication_date_popup_enable');
  variable_del('publication_date_popup_year_start');
  variable_del('publication_date_popup_year_end');
}

/**
 * Change default published_at value from 0 to 2147483647.
 */
function publication_date_update_7001() {
  db_update('publication_date')
    ->fields(array(
    'published_at' => 2147483647,
  ))
    ->condition('published_at', 0)
    ->execute();
}

/**
 * Change default published_at value to NULL.
 */
function publication_date_update_7002() {
  db_drop_index('publication_date', 'published_at');
  db_change_field('publication_date', 'published_at', 'published_at', array(
    'description' => 'The timestamp of the node\'s first publication.',
    'type' => 'int',
    'not null' => FALSE,
    'default' => NULL,
  ), array(
    'indexes' => array(
      'published_at' => array(
        'published_at',
      ),
    ),
  ));
  db_update('publication_date')
    ->fields(array(
    'published_at' => NULL,
  ))
    ->condition('published_at', 2147483647)
    ->execute();
}

Functions

Namesort descending Description
publication_date_install Implements hook_install().
publication_date_schema Implements hook_schema().
publication_date_uninstall Implements hook_uninstall().
publication_date_update_7001 Change default published_at value from 0 to 2147483647.
publication_date_update_7002 Change default published_at value to NULL.
_publication_date_update_existing Helper function to update the existing nodes on install.