You are here

ed_classified.install in Classified Ads 6.2

File

ed_classified.install
View source
<?php

/*
 * @file
 * Installer
 */
if (function_exists('module_load_include')) {
  module_load_include('module', 'ed_classified', 'ed_classified');
}
else {
  require_once dirname(drupal_get_filename('module', 'ed_classified')) . '/ed_classified_module';
}
module_load_include('inc', 'ed_classified', 'ed_classified_utils');

/**
 * Implements hook_install().
 */
function ed_classified_install() {
  $DRUPAL_VERSION = variable_get('ed_classified_drupal_version', 'UNKNOWN');
  if ('UNKNOWN' == $DRUPAL_VERSION) {
    if (defined('VERSION')) {
      drupal_set_message(t("!module: recording Drupal version as '!version.'", array(
        '!module' => EDI_CLASSIFIED_MODULE_NAME,
        '!version' => VERSION,
      )));
      variable_set('ed_classified_drupal_version', $DRUPAL_VERSION = VERSION);
    }
    else {
      if (!empty($theme_info) && !empty($theme_info->info)) {
        $DRUPAL_VERSION = $theme_info->info['core'];
        drupal_set_message(t("!module: detected Drupal version is '!version' (recording).", array(
          '!module' => EDI_CLASSIFIED_MODULE_NAME,
          '!version' => $DRUPAL_VERSION,
        )));
        variable_set('ed_classified_drupal_version', $DRUPAL_VERSION);
      }
      else {
        $msg = t('!module: failed to detect Drupal version during installation (aborting install).', array(
          '!module' => EDI_CLASSIFIED_MODULE_NAME,
        ));
        drupal_set_message($msg);
        watchdog($msg);
        return;

        // Give up
      }
    }
  }
  switch (reset(explode('.', $DRUPAL_VERSION))) {
    case 5:
      ed_classified_schema_drupal5();
      break;
    case 6:
    case 7:
      drupal_install_schema(EDI_CLASSIFIED_MODULE_NAME);
      break;
    default:
      break;
  }

  //ed_classified_init_taxonomy();

  // By requesting the vid the taxonomy will be automatically created and associated
  _ed_classified_get_vid();
}

/**
 * Helper function for hook_schema() on Drupal 5.
 */
function ed_classified_schema_drupal5() {
  drupal_set_message(t('Creating classified ads table'));
  $success = FALSE;
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $success = db_query("CREATE TABLE {edi_classified_nodes} (\n        nid int(10) unsigned NOT NULL default '0',\n        vid int(10) unsigned NOT NULL default '0',\n        expires_on int(10) unsigned NOT NULL default '0' COMMENT 'Unix timestamp of expiration',\n        expiration_notify_last_sent int(10) unsigned NOT NULL default '0',\n        PRIMARY KEY (nid,vid));");
      break;
    case 'pgsql':
      $success = db_query("CREATE TABLE {edi_classified_nodes} (\n        nid serial CHECK (nid >=0),\n        vid int_unsigned NOT NULL default '0',\n        expires_on int_unsigned NOT NULL default '0',\n        expiration_notify_last_sent int_unsigned NOT NULL default '0',\n        PRIMARY KEY (nid));");
      break;
  }
  if ($success) {
    drupal_set_message(t('Classified module table creation successful.'));
  }
  else {
    drupal_set_message(t('Classified module table creation was unsuccessful. Check the watchdog logs.'), 'error');
  }
}

/**
 * Implements hook_schema().
 *
 * As of Drupal 7 schema tags do not need to be translated with t()
 * Strings should be non-markup plain text
 */
function ed_classified_schema() {
  $schema['edi_classified_nodes'] = array(
    'description' => 'Stores the ed_classified node-related type.',
    'fields' => array(
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Node nid of related node.',
      ),
      'vid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Node vid (version id).',
      ),
      'expires_on' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'expiration_notify_last_sent' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'nid',
      'vid',
    ),
  );
  return $schema;
}

/**
 * Implementation of hook_uninstall().
 */
function ed_classified_uninstall() {
  switch (reset(explode('.', VERSION))) {
    case 5:
      db_query('DROP TABLE {edi_classified_nodes}');
      db_query('DELETE FROM {vocabulary} WHERE module = "%s"', EDI_CLASSIFIED_MODULE_NAME);
      db_query('DELETE FROM {variable} WHERE name LIKE "ed_classified%";');
      break;
    case 6:
      drupal_uninstall_schema(EDI_CLASSIFIED_MODULE_NAME);
      db_query('DELETE FROM {vocabulary} WHERE module = "%s"', EDI_CLASSIFIED_MODULE_NAME);
      db_query('DELETE FROM {variable} WHERE name LIKE "ed_classified%";');
      break;
    case 7:

      // TODO: is the schema automagically uninstalled in Drupal 7?
      drupal_uninstall_schema(EDI_CLASSIFIED_MODULE_NAME);

      // db_query('DELETE FROM {taxonomy_vocabulary} WHERE module = "%s"', EDI_CLASSIFIED_MODULE_NAME);
      // db_query('DELETE FROM {variable} WHERE name LIKE "ed_classified%";');
      $fields = array(
        'module' => EDI_CLASSIFIED_MODULE_NAME,
      );
      db_delete('taxonomy_vocabulary')
        ->fields($fields)
        ->execute();

      // TODO - Drupal 7 - set up the fields to do a LIKE % ...
      $fields = array(
        'name' => EDI_CLASSIFIED_MODULE_NAME,
      );
      db_delete('variable')
        ->fields($fields)
        ->execute();
      break;
  }
}

Functions

Namesort descending Description
ed_classified_install Implements hook_install().
ed_classified_schema Implements hook_schema().
ed_classified_schema_drupal5 Helper function for hook_schema() on Drupal 5.
ed_classified_uninstall Implementation of hook_uninstall().