You are here

publication_date.install in Publication Date 7

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 publication.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    '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();
}

Functions

Namesort descending Description
publication_date_install Implements hook_install().
publication_date_schema Implements hook_schema().
_publication_date_update_existing Helper function to update the existing nodes on install.