From 2c2f92bc8e18a7b46af60752f6b9d8b87558cce4 Mon Sep 17 00:00:00 2001
From: Tim Steiner <tsteiner2@unl.edu>
Date: Thu, 31 May 2012 15:34:26 -0500
Subject: [PATCH] [gh-390] Update Eric's code to not rely on new database
 fields

---
 sites/all/modules/unl/unl.install           | 20 -------------
 sites/all/modules/unl/unl_site_creation.php | 31 +++++++++++----------
 2 files changed, 16 insertions(+), 35 deletions(-)

diff --git a/sites/all/modules/unl/unl.install b/sites/all/modules/unl/unl.install
index 395d5d12..fea21a3b 100644
--- a/sites/all/modules/unl/unl.install
+++ b/sites/all/modules/unl/unl.install
@@ -402,23 +402,3 @@ function unl_update_7112() {
   );
   variable_set('unl_module_whitelist', $modules);
 }
-
-/**
- * Add name and access fields to unl_sites table to hold site name and last access timestamp.
- */
-function unl_update_7113() {
-  db_add_field('unl_sites', 'name', array(
-    'description' => 'Site name entered on the site configuration page.',
-    'type'     => 'varchar',
-    'length'   => 255,
-    'not null' => TRUE,
-    'default'  => '',
-  ));
-  db_add_field('unl_sites', 'access', array(
-    'description' => 'Timestamp for previous time a non-admin role accessed the site.',
-    'type'     => 'int',
-    'length'   => 11,
-    'not null' => TRUE,
-    'default'  => 0,
-  ));
-}
diff --git a/sites/all/modules/unl/unl_site_creation.php b/sites/all/modules/unl/unl_site_creation.php
index 5b5b2186..52d130c0 100644
--- a/sites/all/modules/unl/unl_site_creation.php
+++ b/sites/all/modules/unl/unl_site_creation.php
@@ -92,12 +92,10 @@ function unl_site_create_submit($form, &$form_state) {
 }
 
 /**
- * Updates the name and access fields in the default site unl_sites table.
+ * Adds virtual name and access fields to a result set from the unl_sites table.
+ * @param $sites The result of db_select()->fetchAll() on the unl_sites table.
  */
-function unl_update_unl_sites() {
-  // Get all sites in production
-  $query = db_query('SELECT * FROM {unl_sites} WHERE installed=2');
-
+function unl_add_extra_site_info($sites) {
   // Get all custom made roles (roles other than authenticated, anonymous, administrator)
   $roles = user_roles(TRUE);
   unset($roles[DRUPAL_AUTHENTICATED_RID]);
@@ -117,17 +115,22 @@ function unl_update_unl_sites() {
   // The master prefix that was specified during initial drupal install
   $master_prefix = $GLOBALS['databases']['default']['default']['prefix'];
 
-  while ($row = $query->fetchAssoc()) {
+  foreach ($sites as $row) {
+    // Skip over any sites that aren't properly installed.
+    if ($row->installed != 2) {
+      continue;
+    }
+    
     // Switch to alt db connection
     db_set_active('UNLNoPrefix');
 
     // Get site name
-    $table = $row['db_prefix'].'_'.$master_prefix.'variable';
+    $table = $row->db_prefix.'_'.$master_prefix.'variable';
     $name = db_query("SELECT value FROM ".$table." WHERE name = 'site_name'")->fetchField();
 
     // Get last access timestamp (by a non-administrator)
-    $table_users = $row['db_prefix'].'_'.$master_prefix.'users u';
-    $table_users_roles = $row['db_prefix'].'_'.$master_prefix.'users_roles r';
+    $table_users = $row->db_prefix.'_'.$master_prefix.'users u';
+    $table_users_roles = $row->db_prefix.'_'.$master_prefix.'users_roles r';
     if (!empty($roles)) {
       $access = db_query('SELECT u.access FROM '.$table_users.', '.$table_users_roles.' WHERE u.uid = r.uid AND u.access > 0 AND r.rid IN (' . implode(',', array_keys($roles)) . ') ORDER BY u.access DESC')->fetchColumn();
     } else {
@@ -138,10 +141,8 @@ function unl_update_unl_sites() {
     db_set_active();
 
     // Update unl_sites table of the default site
-    db_update('unl_sites')
-      ->fields(array('name' => @unserialize($name), 'access' => (int)$access))
-      ->condition('site_id', $row['site_id'])
-      ->execute();
+    $row->name = @unserialize($name);
+    $row->access = (int)$access;
   }
 }
 
@@ -149,8 +150,6 @@ function unl_update_unl_sites() {
  * Site List appears on admin/sites/unl, admin/sites/unl/sites
  */
 function unl_site_list($form, &$form_state) {
-  unl_update_unl_sites();
-
   $header = array(
     'uri' => array(
       'data' => t('Default Path'),
@@ -177,6 +176,8 @@ function unl_site_list($form, &$form_state) {
     ->orderByHeader($header)
     ->execute()
     ->fetchAll();
+  
+  unl_add_extra_site_info($sites);
 
   $options = array();
   foreach ($sites as $site) {
-- 
GitLab