You are here

legal.install in Legal 5

File

legal.install
View source
<?php

/**
 * Implementation of hook_install()
 *
 * This will automatically install the database tables for the legal module for the MySQL database.
 *
 * 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 legal_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysqli':
    case 'mysql':
      $query1 = db_query("CREATE TABLE IF NOT EXISTS {legal_accepted} (\n                        legal_id int(10) NOT NULL auto_increment,\n                        uid int(10) NOT NULL default '0',\n                        tc_id int(10) NOT NULL default '0',\n                        accepted int(11) NOT NULL default '0',\n                        PRIMARY KEY  (legal_id),\n                        KEY uid (uid)\n                        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      $query2 = db_query("CREATE TABLE IF NOT EXISTS {legal_conditions} (\n                        tc_id int(10) NOT NULL auto_increment,\n                        conditions longtext NOT NULL,\n                        date int(11) NOT NULL default '0',\n                        extras text,\n                        changes text,\n                        PRIMARY KEY  (tc_id)\n                        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      if ($query1 && $query2) {
        $created = TRUE;
      }
      break;
    default:
      break;
  }
  if ($created) {
    drupal_set_message(t('Legal module installed successfully.'));
  }
  else {
    drupal_set_message(t('Table installation for the Legal module was unsuccessful. The tables may need to be installed by hand. See legal.install file for a list of the installation queries.'), 'error');
  }
  return;
}
function legal_update_1() {
  _system_update_utf8(array(
    'legal_accepted',
    'legal_conditions',
  ));
  switch ($GLOBALS['db_type']) {
    case 'mysqli':
    case 'mysql':
      $ret[] = update_sql("ALTER TABLE {legal_conditions} ADD extras TEXT");
      $ret[] = update_sql("ALTER TABLE {legal_conditions} ADD changes TEXT");
      break;
    default:
      break;
  }
  return $ret;
}

/**
 * Implementation of hook_uninstall().
 */
function legal_uninstall() {
  db_query('DROP TABLE {legal_accepted}');
  db_query('DROP TABLE {legal_conditions}');
  variable_del('legal_display');
}

/**
 * Remove conditions and extras from users.data field.
*/
function legal_update_2() {

  // How many users to process each time.
  $limit = 50;

  // Set-up multipart update.
  if (!isset($_SESSION['legal_update_2'])) {
    $conditions = db_fetch_array(db_query_range("SELECT * FROM {legal_conditions} ORDER BY tc_id DESC", 0, 1));
    if (empty($conditions['conditions'])) {
      return;
    }
    $extras = empty($conditions['extras']) ? NULL : array_keys(unserialize($conditions['extras']));
    $_SESSION['legal_update_2'] = array(
      'uid' => 0,
      'max' => db_result(db_query('SELECT MAX(uid) FROM {users}')),
      'extras' => $extras,
    );
  }

  // Fetch up to N users and fix their data field.
  $result = db_query_range("SELECT uid, data FROM {users} WHERE uid > %d AND data LIKE '%%s:10:\"conditions\"%%'", $_SESSION['legal_update_2']['uid'], 0, $limit);
  while ($user = db_fetch_object($result)) {
    $data = unserialize($user->data);
    unset($data['conditions']);
    if (!empty($_SESSION['legal_update_2']['extras'])) {
      foreach ($_SESSION['legal_update_2']['extras'] as $extra) {
        unset($data[$extra]);
      }
    }
    db_query("UPDATE {users} SET data = '%s' WHERE uid = %d", serialize($data), $user->uid);
    $_SESSION['legal_update_2']['uid'] = $user->uid;
  }
  if (db_num_rows($result) == $limit) {

    // Return progress (never return 100% here to ensure clean-up is still run last).
    return array(
      '#finished' => $_SESSION['legal_update_2']['uid'] / ($_SESSION['legal_update_2']['max'] + 1),
    );
  }
  else {

    // Clean-up.
    unset($_SESSION['legal_update_2']);
    return array();
  }
}

Functions

Namesort descending Description
legal_install Implementation of hook_install()
legal_uninstall Implementation of hook_uninstall().
legal_update_1
legal_update_2 Remove conditions and extras from users.data field.