You are here

notifications.install in Notifications 6

File

notifications.install
View source
<?php

/**
 * Implementation of hook_schema()
 */
function notifications_schema() {
  $schema['notifications'] = array(
    'fields' => array(
      'sid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'disp-width' => '10',
      ),
      'uid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'disp-width' => '11',
      ),
      'type' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'event_type' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'conditions' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'disp-width' => '10',
      ),
      'send_interval' => array(
        'type' => 'int',
        'not null' => FALSE,
        'disp-width' => '11',
      ),
      'send_method' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
      ),
      'cron' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '3',
      ),
      'module' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'status' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
        'disp-width' => '11',
      ),
      'destination' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
    ),
    'primary key' => array(
      'sid',
    ),
  );
  $schema['notifications_fields'] = array(
    'fields' => array(
      'sid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'disp-width' => '10',
      ),
      'field' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
      ),
      'value' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
      ),
      'intval' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '11',
      ),
    ),
    'primary key' => array(
      'sid',
      'field',
    ),
  );
  $schema['notifications_event'] = array(
    'fields' => array(
      'eid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'disp-width' => '11',
      ),
      'module' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'type' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'action' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'oid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '11',
      ),
      'language' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'uid' => array(
        'type' => 'int',
        'not null' => FALSE,
        'disp-width' => '11',
      ),
      'params' => array(
        'type' => 'text',
        'not null' => FALSE,
      ),
      'created' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '11',
      ),
      'counter' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '11',
      ),
    ),
    'primary key' => array(
      'eid',
    ),
  );
  $schema['notifications_queue'] = array(
    'fields' => array(
      'sqid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'disp-width' => '10',
      ),
      'eid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '11',
      ),
      'sid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '11',
      ),
      'uid' => array(
        'type' => 'int',
        'not null' => FALSE,
        'disp-width' => '11',
      ),
      'language' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'type' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'send_interval' => array(
        'type' => 'int',
        'not null' => FALSE,
        'disp-width' => '11',
      ),
      'send_method' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'sent' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'created' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'cron' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '3',
      ),
      'conditions' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'module' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
      'destination' => array(
        'type' => 'varchar',
        'length' => '255',
        'not null' => FALSE,
      ),
    ),
    'primary key' => array(
      'sqid',
    ),
  );
  $schema['notifications_sent'] = array(
    'fields' => array(
      'uid' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '11',
      ),
      'send_interval' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'send_method' => array(
        'type' => 'varchar',
        'length' => '50',
        'not null' => TRUE,
      ),
      'sent' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
    ),
    'primary key' => array(
      'uid',
      'send_interval',
      'send_method',
    ),
  );
  return $schema;
}

/**
 * Implementation of hook_install().
 */
function notifications_install() {
  drupal_install_schema('notifications');
}

/**
 * Implementation of hook_uninstall().
 */
function notifications_uninstall() {
  drupal_uninstall_schema('notifications');
  foreach (array(
    'events',
    'send_intervals',
    'sender',
    'sendself',
    'send_immediate',
  ) as $name) {
    variable_del("notifications_{$name}");
  }
}

/**
 * Update: Add cron flag for processing
 */
function notifications_update_1() {
  $ret = array();

  // Add field
  $ret[] = update_sql("ALTER TABLE {notifications} ADD COLUMN `cron` TINYINT UNSIGNED NOT NULL DEFAULT 0");
  $ret[] = update_sql("ALTER TABLE {notifications_queue} ADD COLUMN `cron` TINYINT UNSIGNED NOT NULL DEFAULT 0");

  // Populate field, this is new so all stored rows till now should be intended for cron processing
  $ret[] = update_sql("UPDATE {notifications} SET cron = 1");
  $ret[] = update_sql("UPDATE {notifications_queue} SET cron = 1");
  return $ret;
}

/**
 * Update:
 * - Remove unused table and fields
 * - Add conditions field for mysql4 compatibility
 * - Updated variable name
 */
function notifications_update_2() {
  $ret = array();
  $ret[] = update_sql("DROP TABLE {notifications_user}");
  $ret[] = update_sql("ALTER TABLE {notifications_queue} DROP COLUMN `name`;");
  $ret[] = update_sql("ALTER TABLE {notifications_queue} DROP COLUMN `field`;");
  $ret[] = update_sql("ALTER TABLE {notifications_queue} DROP COLUMN `value`;");
  $ret[] = update_sql("ALTER TABLE {notifications_queue} DROP COLUMN `author`;");
  $ret[] = update_sql("ALTER TABLE {notifications_queue} ADD COLUMN `conditions` INTEGER UNSIGNED NOT NULL DEFAULT 0 AFTER `cron`");
  variable_set('notifications_default_auto', variable_get('notifications_autoset', 0));
  variable_del('notifications_autoset');
  return $ret;
}

/**
 * - Added status and module fields
 */
function notifications_update_3() {
  $ret[] = update_sql("ALTER TABLE {notifications} ADD COLUMN `module` VARCHAR(255) AFTER `cron`;");
  $ret[] = update_sql("ALTER TABLE {notifications} ADD COLUMN `status` INT  NOT NULL DEFAULT 1 AFTER `module`;");
  $ret[] = update_sql("ALTER TABLE {notifications_queue} ADD COLUMN `module` VARCHAR(255);");

  // Set default module to 'notifications'
  $ret[] = update_sql("UPDATE {notifications} SET module = 'notifications'");
  $ret[] = update_sql("UPDATE {notifications_queue} SET module = 'notifications'");
  return $ret;
}

/**
 * Change module weight
 */
function notifications_update_4() {
  $ret[] = update_sql("UPDATE {system} SET weight = 100 WHERE name = 'notifications_content' AND type = 'module'");
  return $ret;
}

/**
 * Update content type and taxonomy options
 */
function notifications_update_5() {
  $ret = array();

  // Content types
  if ($omitted = variable_get('notifications_omitted_content_types', array())) {
    $allowed = array();
    $types = node_get_types();
    foreach ($types as $type => $info) {
      if (!isset($omitted[$type])) {
        $allowed[$type] = $type;
      }
    }
    variable_set('notifications_content_types', $allowed);
  }

  // Vocabularies
  if ($omitted = variable_get('notifications_omitted_taxa', array())) {
    $vocabularies = taxonomy_get_vocabularies();
    foreach ($omitted as $vid) {
      unset($vocabularies[$vid]);
    }
    variable_set('notifications_tags_vocabularies', array_combine(array_keys($vocabularies), array_keys($vocabularies)));
  }
  return $ret;
}

/**
 * Update ui display options from plaintext to array
 */
function notifications_update_6() {
  $ret = array();
  foreach (node_get_types() as $type => $info) {
    $option = variable_get('notifications_node_ui_' . $type, 0);
    if ($option && !is_array($option)) {
      variable_set('notifications_node_ui_' . $type, array(
        $option,
      ));
    }
  }
  return $ret;
}

/**
 * Multiple fixes
 */
function notifications_update_6001() {
  $ret = array();

  // Fixed typo with variable name
  variable_set('notifications_send_immediate', variable_get('notifications_send_inmediate', 0));
  variable_del('notifications_send_inmediate');
  return $ret;
}

/**
 * Add some fields
 */
function notifications_update_6002() {
  $ret = array();
  db_add_field($ret, 'notifications_event', 'counter', array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
    'disp-width' => '11',
  ));
  db_add_field($ret, 'notifications', 'destination', array(
    'type' => 'varchar',
    'length' => '255',
    'not null' => FALSE,
  ));
  db_add_field($ret, 'notifications_queue', 'destination', array(
    'type' => 'varchar',
    'length' => '255',
    'not null' => FALSE,
  ));

  // Update event counter, this may take a while
  $ret[] = update_sql("UPDATE {notifications_event} e SET counter = (SELECT COUNT(*) FROM {notifications_queue} q WHERE q.eid = e.eid)");
  return $ret;
}

/**
 * Add integer value to fields table
 */
function notifications_update_6003() {
  $ret = array();
  db_add_field($ret, 'notifications_fields', 'intval', array(
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
    'disp-width' => '11',
  ));

  // Populate intval, this should depend on db type. Is there any way that works for all?
  // For both, CAST produces an error when it is no integer that's why the regexp...
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql('UPDATE {notifications_fields} SET intval = CAST(value AS UNSIGNED) WHERE value REGEXP \'^-?[0-9]+$\'');
      break;
    case 'pgsql':
      $ret[] = update_sql('UPDATE {notifications_fields} SET intval = CAST(value AS INTEGER) WHERE value SIMILAR TO \'^-?[0-9]+$\'');
      break;
  }
  return $ret;
}

Functions

Namesort descending Description
notifications_install Implementation of hook_install().
notifications_schema Implementation of hook_schema()
notifications_uninstall Implementation of hook_uninstall().
notifications_update_1 Update: Add cron flag for processing
notifications_update_2 Update:
notifications_update_3 Added status and module fields
notifications_update_4 Change module weight
notifications_update_5 Update content type and taxonomy options
notifications_update_6 Update ui display options from plaintext to array
notifications_update_6001 Multiple fixes
notifications_update_6002 Add some fields
notifications_update_6003 Add integer value to fields table