You are here

hosting_client.install in Hosting 5

File

client/hosting_client.install
View source
<?php

function hosting_client_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {hosting_client} (\n        vid int(10) unsigned NOT NULL default '0',\n        nid int(10) unsigned NOT NULL default '0',\n        name longtext,\n        organization longtext,\n        email varchar(255) NOT NULL default '',\n        PRIMARY KEY  (vid)\n      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
      db_query("CREATE UNIQUE INDEX hosting_client_email_idx ON hosting_client (email)");
      db_query("CREATE TABLE {hosting_client_user} (\n         user int(10) unsigned NOT NULL default '0',\n         client int(10) unsigned NOT NULL default '0',\n         contact_type longtext NOT NULL,\n         PRIMARY KEY (user, client)\n       ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
      db_query("INSERT INTO {hosting_client_user} VALUES (1, 1, 'admin')");
      break;
  }
}

/**
 * Add the unique index to the client table
 *
 * This will also run through all existing clients and merge / delete the ones that
 * don't belong.
 */
function hosting_client_update_1() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret = array();
      $result = db_query("SELECT email, count(distinct nid) as count FROM {hosting_client} GROUP BY email");
      while ($distinct = db_fetch_object($result)) {
        if ($distinct->count > 1) {

          # we have found duplicates.
          $result2 = db_query("SELECT nid FROM {hosting_client} WHERE email = '%s' ORDER BY nid", $distinct->email);
          $first = false;
          while ($client = db_fetch_object($result2)) {
            if (!$first) {

              // this is the key all the others will be assigned to.
              $first = $client->nid;
            }
            else {

              // reset nodes to the first occurrence, and delete the duplicate
              db_query("UPDATE {hosting_site} SET client=%d WHERE client=%d", $first, $client->nid);
              node_delete($client->nid);
            }
          }
        }
      }
      $ret[] = update_sql("CREATE UNIQUE INDEX hosting_client_email_idx ON hosting_client (email)");
      break;
  }
  return $ret;
}

/**
 * Create the hosting_client_user relationship table
 */
function hosting_client_update_2() {
  $ret = array();
  $ret[] = update_sql("CREATE TABLE {hosting_client_user} (\n         user int(10) unsigned NOT NULL default '0',\n         client int(10) unsigned NOT NULL default '0',\n         PRIMARY KEY (user)\n       ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");

  // Insert the uid 1 user into the admin client record.
  $ret[] = update_sql("INSERT INTO {hosting_client_user} VALUES (1, 1)");
  node_access_rebuild();
  return $ret;
}

/**
 * Rebuild node access table
 */
function hosting_client_update_3() {
  $ret = array();
  node_access_rebuild();
  return $ret;
}

/**
 * Rebuild node access table
 */
function hosting_client_update_4() {
  $ret = array();
  node_access_rebuild();
  return $ret;
}

/**
 * Rebuild node access table
 */
function hosting_client_update_5() {
  $ret = array();
  node_access_rebuild();
  return $ret;
}

/**
 * Make it possible to have many clients per user and keep track of the contact type (admin/tech/billing/etc.) between users and clients
 */
function hosting_client_update_6() {
  $ret = array();
  $ret[] = update_sql("ALTER TABLE {hosting_client_user} DROP PRIMARY KEY, ADD PRIMARY KEY (user, client)");
  $ret[] = update_sql("ALTER TABLE {hosting_client_user} ADD contact_type LONGTEXT NOT NULL");
  node_access_rebuild();
  return $ret;
}

/**
 * Rebuild the node access table now that we fixed the hook_access properly
 */
function hosting_client_update_7() {
  node_access_rebuild();
  return array();
}

Functions

Namesort descending Description
hosting_client_install
hosting_client_update_1 Add the unique index to the client table
hosting_client_update_2 Create the hosting_client_user relationship table
hosting_client_update_3 Rebuild node access table
hosting_client_update_4 Rebuild node access table
hosting_client_update_5 Rebuild node access table
hosting_client_update_6 Make it possible to have many clients per user and keep track of the contact type (admin/tech/billing/etc.) between users and clients
hosting_client_update_7 Rebuild the node access table now that we fixed the hook_access properly