You are here

signup.install in Signup 5.2

Same filename and directory in other branches
  1. 5 signup.install
  2. 6.2 signup.install
  3. 6 signup.install
  4. 7 signup.install

File

signup.install
View source
<?php

/**
 * Implementation of hook_install().
 *
 * This will automatically install the database tables for the Signup
 * module for both the MySQL and PostgreSQL databases.
 *
 * If you are using another database, you will have to install the
 * tables by hand, using the queries below as a reference.
 *
 * Note that the curly braces around table names are a drupal-specific
 * feature to allow for automatic database table prefixing, and will
 * need to be removed.
 */
function signup_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysqli':
    case 'mysql':
      $q1 = db_query("CREATE TABLE IF NOT EXISTS {signup} (\n                nid int(10) unsigned NOT NULL default '0',\n                forwarding_email varchar(64) NOT NULL default '',\n                send_confirmation int(2) NOT NULL default '0',\n                confirmation_email longtext NOT NULL,\n                send_reminder int(2) NOT NULL default '0',\n                reminder_days_before int(4) unsigned NOT NULL default '0',\n                reminder_email longtext NOT NULL,\n                close_in_advance_time int(10) unsigned NOT NULL default '0',\n                close_signup_limit int(10) unsigned NOT NULL default '0',\n                status int(2) NOT NULL default '1',\n                PRIMARY KEY  (nid)\n            ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      $q2 = db_query("CREATE TABLE IF NOT EXISTS {signup_log} (\n                uid int(10) unsigned NOT NULL default '0',\n                nid int(10) unsigned NOT NULL default '0',\n                anon_mail varchar(255) NOT NULL default '',\n                signup_time int(10) unsigned NOT NULL default '0',\n                form_data longtext NOT NULL,\n                KEY uid (uid),\n                KEY nid (nid)\n            ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      $q3 = signup_insert_default_signup_info();
      if ($q1 && $q2 && $q3) {
        $created = TRUE;
      }
      break;
    case 'pgsql':
      $q1 = db_query("CREATE TABLE {signup} (\n                nid SERIAL,\n                forwarding_email text NOT NULL default '',\n                send_confirmation integer NOT NULL default '0',\n                confirmation_email text NOT NULL default '',\n                send_reminder integer NOT NULL default '0',\n                reminder_days_before integer NOT NULL default '0',\n                reminder_email text NOT NULL default '',\n                close_in_advance_time integer NOT NULL default '0',\n                close_signup_limit integer NOT NULL default '0',\n                status integer NOT NULL default '1',\n                PRIMARY KEY (nid)\n            );");
      $q2 = db_query("CREATE TABLE {signup_log} (\n                uid integer NOT NULL default '0',\n                nid integer NOT NULL default '0',\n                anon_mail text NOT NULL default '',\n                signup_time integer NOT NULL default '0',\n                form_data text NOT NULL default ''\n            );");
      $q3 = db_query("CREATE INDEX {signup_log}_uid_idx ON {signup_log}(uid);");
      $q4 = db_query("CREATE INDEX {signup_log}_nid_idx ON {signup_log}(nid);");
      $q5 = signup_insert_default_signup_info();
      if ($q1 && $q2 && $q3 && $q4 && $q5) {
        $created = TRUE;
      }
      break;
  }
  if ($created) {
    drupal_set_message(t('Signup module installed successfully.'));
  }
  else {
    drupal_set_message(t('Table installation for the Signup module was unsuccessful. The tables may need to be installed by hand. See the signup.install file for a list of the installation queries.'), 'error');
  }
}
function signup_uninstall() {
  if (db_table_exists('signup')) {
    db_query("DROP TABLE {signup}");
  }
  if (db_table_exists('signup_log')) {
    db_query("DROP TABLE {signup_log}");
  }
  $variables = db_query("SELECT name FROM {variable} WHERE name LIKE 'signup%%'");
  while ($variable = db_fetch_object($variables)) {
    variable_del($variable->name);
  }
}

/**
 * Helper method to insert the default signup information into the
 * {signup} table (stored in a row for nid 0).  These are the default
 * settings for new signup-enabled nodes.
 */
function signup_insert_default_signup_info() {
  return db_query("INSERT INTO {signup} (nid, forwarding_email,\n    send_confirmation, confirmation_email,\n    send_reminder, reminder_days_before, reminder_email,\n    close_in_advance_time, close_signup_limit, status) VALUES (0, '',\n    1, 'Enter your default confirmation email message here',\n    1, 1, 'Enter your default reminder email message here',\n    0, 0, 1)");
}

/**
 * UTF8 table update
 */
function signup_update_1() {
  return _system_update_utf8(array(
    'signup',
    'signup_log',
  ));
}
function signup_update_2() {
  $ret = array();
  $ret[] = update_sql("ALTER TABLE {signup} DROP permissions");
  return $ret;
}
function signup_update_3() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {signup_log} ADD anon_mail VARCHAR( 255 ) NOT NULL default '' AFTER nid;");
      $ret[] = update_sql("ALTER TABLE {signup_log} DROP INDEX uid_nid;");
      $ret[] = update_sql("ALTER TABLE {signup_log} ADD INDEX (uid);");
      $ret[] = update_sql("ALTER TABLE {signup_log} ADD INDEX (nid);");
      break;
    case 'pgsql':
      db_add_column($ret, 'signup_log', 'anon_mail', 'text', array(
        'not null' => TRUE,
        'default' => "''",
      ));
      $ret[] = update_sql("DROP INDEX {signup_log}_uid_nid_idx;");
      $ret[] = update_sql("CREATE INDEX {signup_log}_uid_idx ON {signup_log}(uid);");
      $ret[] = update_sql("CREATE INDEX {signup_log}_nid_idx ON {signup_log}(nid);");
      break;
  }
  return $ret;
}

/**
 * Rename the signup permissions.
 * See http://drupal.org/node/69283 for details.
 * Also, remove the 'signup_user_view' setting in favor of a permission.
 * See http://drupal.org/node/69367 for details.
 */
function signup_update_4() {
  $ret = array();

  // Setup arrays holding regexps to match and the corresponding
  // strings to replace them with, for use with preg_replace().
  $old_perms = array(
    '/allow signups/',
    '/admin signups/',
    '/admin own signups/',
  );
  $new_perms = array(
    'sign up for content',
    'administer all signups',
    'administer signups for own content',
  );

  // Now, loop over all the roles, and do the necessary transformations.
  $query = db_query("SELECT rid, perm FROM {permission} ORDER BY rid");
  while ($role = db_fetch_object($query)) {
    $fixed_perm = preg_replace($old_perms, $new_perms, $role->perm);
    if ($role->rid == 2 && variable_get('signup_user_view', 0)) {

      // The setting is currently enabled, so add the new permission to
      // the "authenticated user" role as a reasonable default.
      if (!strpos($fixed_perm, 'view all signups')) {
        $fixed_perm .= ', view all signups';
        drupal_set_message(t('The old %signup_user_view setting was enabled on your site, so the %view_all_signups permission has been added to the %authenticated_user role. Please consider customizing what roles have this permission on the !access_control page.', array(
          '%signup_user_view' => t('Users can view signups'),
          '%view_all_signups' => 'view all signups',
          '%authenticated_user' => 'Authenticated user',
          '!access_control' => l(t('Access control'), '/admin/user/access'),
        )));
      }
    }
    $ret[] = update_sql("UPDATE {permission} SET perm = '{$fixed_perm}' WHERE rid = {$role->rid}");
  }

  // Remove the stale setting from the {variable} table in the DB.
  variable_del('signup_user_view');
  drupal_set_message(t('The %signup_user_view setting has been removed.', array(
    '%signup_user_view' => t('Users can view signups'),
  )));
  return $ret;
}

/**
 * Convert the misnamed "completed" column to "status" (and swap all
 * the values: 0 == closed, 1 == open).
 */
function signup_update_5200() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {signup} ADD status int NOT NULL default '1'");
      break;
    case 'pgsql':
      db_add_column($ret, 'signup', 'status', 'integer', array(
        'not null' => TRUE,
        'default' => "'1'",
      ));
      break;
  }
  $ret[] = update_sql("UPDATE {signup} SET status = (1 - completed)");
  $ret[] = update_sql("ALTER TABLE {signup} DROP completed");
  return $ret;
}

/**
 * Add the close_signup_limit field to the {signup} table to allow
 * signup limits for sites that upgraded from 4.6.x.  The original
 * signup.install for 4.7.x accidentally included this column in the
 * DB, but it's never been used in the code until now.  However, sites
 * that upgraded from 4.6.x need this column for the module to work,
 * so just to be safe, we also add that here.
 */
function signup_update_5201() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      if (!_signup_db_column_exists('signup', 'close_signup_limit')) {
        $ret[] = update_sql("ALTER TABLE {signup} ADD close_signup_limit int(10) unsigned NOT NULL default '0'");
      }
      break;
    case 'pgsql':
      if (!_signup_db_column_exists('signup', 'close_signup_limit')) {
        db_add_column($ret, 'signup', 'close_signup_limit', 'integer', array(
          'not null' => TRUE,
          'default' => "'0'",
        ));
      }
      break;
  }
  return $ret;
}

/**
 * Add "cancel own signups" permission to all roles that have "sign up
 * for content" permission.
 */
function signup_update_5202() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("UPDATE {permission} SET perm = CONCAT(perm, ', cancel own signups') WHERE CONCAT(perm, ', ') LIKE '%%sign up for content, %%'");
      break;
    case 'pgsql':
      $ret[] = update_sql("UPDATE {permission} SET perm = perm || ', cancel own signups' WHERE perm || ', ' LIKE '%%sign up for content, %%'");
      break;
  }
  drupal_set_message(t("Added the 'cancel own signups' permission to all roles that have the 'sign up for content' permission.") . '<br />' . t('If you do not want your users to cancel their own signups, go to the <a href="@access_url">Access control</a> page and unset this permission.', array(
    '@access_url' => url('/admin/user/access'),
  )));
  return $ret;
}

/**
 * Migrate signup settings per content type so that signups can be disabled
 * completely for a content type.
 */
function signup_update_5203() {
  $old_prefix = 'signup_form_';
  $result = db_query("SELECT name FROM {variable} WHERE name LIKE '{$old_prefix}%%'");
  while ($row = db_fetch_object($result)) {
    $old_name = $row->name;
    $new_name = 'signup_node_default_state_' . substr($old_name, strlen($old_prefix));
    $new_value = variable_get($old_name, 0) == 1 ? 'enabled_on' : 'disabled';
    variable_del($old_name);
    variable_set($new_name, $new_value);
  }
  drupal_set_message(t('Migrated signup settings per content type.'));
  return array();
}

/**
 * Rename all the tokens in existing email templates and the global settings.
 */
function signup_update_5204() {
  $ret = array();

  // Multi-part update.
  if (!isset($_SESSION['signup_update_5204'])) {

    // We need to start at nid 0 for the site-wide defaults, so
    // initialize our variable to the value below that.
    $_SESSION['signup_update_5204'] = -1;
    $_SESSION['signup_update_5204_max'] = db_result(db_query("SELECT MAX(nid) FROM {signup}"));
  }

  // Array of replacements mapping old names to the new names.
  // NOTE: To avoid trouble with db_query() trying to interpret the
  // '%', we escape all of them as '%%' to get % literals.
  $replacements = array(
    '%%eventurl' => '%%node_url',
    '%%event' => '%%node_title',
    '%%time' => '%%node_start_time',
    '%%username' => '%%user_name',
    '%%useremail' => '%%user_mail',
    '%%info' => '%%user_signup_info',
  );

  // Build up a nested REPLACE() fragment to have the DB do all the string
  // conversions for us in a single query, instead of pulling the records out
  // of the DB, doing the string manipulation in PHP, and writing back the
  // values in a bunch of separate queries.  According to the docs, REPLACE()
  // works the same way on both MySQL and PgSQL.
  $reminder_replace = 'reminder_email';
  $confirmation_replace = 'confirmation_email';
  foreach ($replacements as $from => $to) {
    $reminder_replace = "REPLACE({$reminder_replace}, '{$from}', '{$to}')";
    $confirmation_replace = "REPLACE({$confirmation_replace}, '{$from}', '{$to}')";
  }

  // Do the next batch of the deed.
  // Find the next N records to update, or do the final batch.
  $next = min($_SESSION['signup_update_5204'] + 2000, $_SESSION['signup_update_5204_max']);

  // Perform the UPDATE in our specified range of nid values.
  db_query("UPDATE {signup} SET reminder_email = {$reminder_replace}, confirmation_email = {$confirmation_replace} WHERE nid > %d AND nid <= %d", $_SESSION['signup_update_5204'], $next);

  // Remember where we left off.
  $_SESSION['signup_update_5204'] = $next;
  if ($_SESSION['signup_update_5204'] == $_SESSION['signup_update_5204_max']) {

    // We're done, clear these out.
    unset($_SESSION['signup_update_5204']);
    unset($_SESSION['signup_update_5204_max']);

    // Provide a human-readable explaination of what we did.
    $tokens = array(
      '%event' => '%event',
      '%eventurl' => '%eventurl',
      '%time' => '%time',
      '%username' => '%username',
      '%useremail' => '%useremail',
      '%info' => '%info',
      '%node_title' => '%node_title',
      '%node_url' => '%node_url',
      '%node_start_time' => '%node_start_time',
      '%user_name' => '%user_name',
      '%user_mail' => '%user_mail',
      '%user_signup_info' => '%user_signup_info',
    );
    $ret[] = array(
      'success' => TRUE,
      'query' => t('Replaced %event, %eventurl, %time, %username, %useremail, and %info tokens with %node_title, %node_url, %node_start_time, %user_name, %user_mail, and %user_signup_info in the reminder and confirmation email templates.', $tokens),
    );
  }
  else {

    // Report how much is left to complete.
    $ret['#finished'] = $_SESSION['signup_update_5204'] / $_SESSION['signup_update_5204_max'];
  }
  return $ret;
}
function _signup_db_column_exists($table, $column) {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      return db_num_rows(db_query("SHOW COLUMNS FROM {%s} LIKE '%s'", $table, $column));
    case 'pgsql':
      return db_result(db_query("SELECT COUNT(pg_attribute.attname) FROM pg_class, pg_attribute WHERE pg_attribute.attrelid = pg_class.oid AND pg_class.relname = '{%s}' AND attname='%s'", $table, $column));
  }
}

Functions

Namesort descending Description
signup_insert_default_signup_info Helper method to insert the default signup information into the {signup} table (stored in a row for nid 0). These are the default settings for new signup-enabled nodes.
signup_install Implementation of hook_install().
signup_uninstall
signup_update_1 UTF8 table update
signup_update_2
signup_update_3
signup_update_4 Rename the signup permissions. See http://drupal.org/node/69283 for details. Also, remove the 'signup_user_view' setting in favor of a permission. See http://drupal.org/node/69367 for details.
signup_update_5200 Convert the misnamed "completed" column to "status" (and swap all the values: 0 == closed, 1 == open).
signup_update_5201 Add the close_signup_limit field to the {signup} table to allow signup limits for sites that upgraded from 4.6.x. The original signup.install for 4.7.x accidentally included this column in the DB, but it's never been used in the code until now. …
signup_update_5202 Add "cancel own signups" permission to all roles that have "sign up for content" permission.
signup_update_5203 Migrate signup settings per content type so that signups can be disabled completely for a content type.
signup_update_5204 Rename all the tokens in existing email templates and the global settings.
_signup_db_column_exists