Index: account_expiry.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/account_expiry/account_expiry.module,v
retrieving revision 1.3
diff -u -r1.3 account_expiry.module
--- account_expiry.module	2 May 2008 04:04:17 -0000	1.3
+++ account_expiry.module	18 Jan 2009 17:47:48 -0000
@@ -1,435 +1,440 @@
-<?php
-// $Id:
-
-/**
-* @file
-* Allows to maintain an expiry date for a user account
-*/
-
-function account_expiry_help($section = null) {
-  switch ($section) {
-    case 'admin/modules#description':
-    case 'admin/help#account_expiry':
-      return t('Allows to maintain an expiry date for a user account.');
-      break;
-  }
-}
-
-function account_expiry_perm() {
-  return array('administer account expiry');
-}
-
-function account_expiry_menu($may_cache) {
-  $items[] = array(
-    'path' => 'admin/settings/account_expiry',
-    'title' => t('Account Expiry'),
-    'description' => t('Change settings for the Account Expiry module.'),
-    'callback' => 'drupal_get_form',
-    'callback arguments' => 'account_expiry_admin_settings',
-    'access' => user_access('administer account expiry'),
-  );
-  $items[] = array(
-    'path' => 'admin/user/account_expiry',
-    'title' => t('User Account Expiry'),
-    'description' => t('Change expiry date for user account.'),
-    'callback' => 'drupal_get_form',
-    'callback arguments' => 'account_expiry_user_settings',
-    'access' => user_access('administer account expiry'),
-  );
-  return $items;
-}
-
-/**
- * Implementation of hook_block().
- */
-function account_expiry_block($op = 'list', $delta = 0, $edit = array()) {
-  global $user;
-  switch ($op) {
-    case 'list':
-      $blocks[0]['info'] = t('Account Expiry');
-      return $blocks;
-    case 'view':
-      switch ($delta) {
-        case 0:
-          $block['subject'] = t('Account Expiry');                    
-          $sql = "SELECT expiry_date FROM {account_expiry} WHERE uid = %d";
-          $result = db_query($sql, $user->uid);                            
-          while ($existing = db_fetch_object($result)) { 
-            $expirydate = $existing->expiry_date;
-          }                    
-          $block['content'] = format_date($expirydate);
-          break;
-      }
-      return $block;    
-  }
-}
-
-function account_expiry_admin_settings() {
-  $form['defaults'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Account expiry default settings'),
-  );
-  $form['defaults']['account_expiry_default_days'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Default number of days until account expires'),
-    '#default_value' => variable_get('account_expiry_default_days', 30),
-    '#description' => t('Expiry date for particular users can be altered from this default'),
-    '#size' => 2,
-    '#maxlength' => 4,
-  );
-    
-  $form['types'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Account types'),
-  );
-
-  if (module_exists('accounttypes')) {
-    $typeselector = "Account";
-    $message = t('AccountTypes module is active - select account types that will have expiry date');
-    $selector = array();
-  }
-  else {
-    $typeselector = "";
-    $message = t('AccountTypes module is not active - ');
-    $selector = array();
-  }
-  $form['types']['message'] = array(    
-    '#value' => $message,    
-  );
-  switch ($typeselector) {
-    case 'Account':
-      //Get account types
-      $accounttypes = array();
-      $sql = "SELECT atid, name from {accounttypes}";
-      $result = db_query($sql);
-      while ($at = db_fetch_object($result)) {
-        $accounttypes[$at->atid] = $at->name;
-      }
-      $defaulttypes = variable_get('account_expiry_accounttypes', array(0));
-      $form['types']['account_expiry_accounttypes'] = array(
-        '#type' => 'checkboxes',
-        '#title' => t('Account types'),
-        '#options' => $accounttypes,
-        '#default_value' => $defaulttypes,                
-      );
-      break;
-  }        
-  return system_settings_form($form);
-}
-
-function account_expiry_form_alter($form_id, &$form) {
-  if ($form_id == 'user_edit' && user_access('administer account expiry')) {
-    $atid = $form['_account']['#value']->selectAT;
-    if (_account_expiry_isexpiryaccount($atid)) {            
-      $uid = $form['_account']['#value']->uid;
-      $sql = "SELECT expiry_date FROM {account_expiry} WHERE uid = %d";
-      $result = db_query($sql, $uid);                            
-      while ($existing = db_fetch_object($result)) { 
-        $expirydate = $existing->expiry_date;
-      }
-      $form['account']['expiry_date'] = array(
-        '#type' => 'date',
-        '#title' => t('Account expiry date'),  
-        '#weight' => 2,          
-        '#default_value' => array(
-          'day'   => format_date($expirydate, 'custom', 'j'),
-          'month' => format_date($expirydate, 'custom', 'n'),    
-          'year'  => format_date($expirydate, 'custom', 'Y')    
-        ),
-        '#description' => t('Set accordingly'),
-      );
-    }
-  }    
-}
-
-function account_expiry_user($op, $edit, &$account, $category = NULL) {
-  if (!$edit['selectAT']) {
-    if (isset($account->selectAT)) {
-      $select_at = $account->selectAT;
-    }
-  } 
-  else {
-    $select_at = $edit['selectAT'];
-  }
-  switch ($op) {
-    case 'view':        
-      //dsm($account);
-      if (_account_expiry_isexpiryaccount($select_at)) {
-        $sql = "SELECT expiry_date FROM {account_expiry} WHERE uid = %d";
-        $result = db_query($sql, $account->uid);                            
-        while ($existing = db_fetch_object($result)) { 
-          $expirydate = $existing->expiry_date;
-        }
-        $items[] = array(
-          'value' => format_date($expirydate),
-          'class' => 'accountexpiry',
-        );
-        return array(t('Account Expiry Date') => $items);
-      }  
-      break;
-	  
-    case 'insert':
-      if (_account_expiry_isexpiryaccount($select_at)) {
-        account_expiry_edit($edit['uid']);
-      }
-      break;
-	  
-    case 'after_update': 
-      //dsm($account);
-      //dsm($edit); 
-      if (_account_expiry_isexpiryaccount($select_at)) {              
-        $day = $account->expiry_date['day'];
-        $month = $account->expiry_date['month'];
-        $year = $account->expiry_date['year'];    
-        $expiry_timestamp = mktime(0, 0, 0, $month, $day, $year);            
-        account_expiry_edit($account->uid, $expiry_timestamp);
-      }
-      else {
-        account_expiry_delete($account->uid);
-      }        
-      break;
-	  
-    case 'delete': 
-      account_expiry_delete($account->uid);
-      break;
-  }
-}
-
-
-function account_expiry_user_settings() {     
-  $form['header'] = array(
-    '#type' => 'value',
-    '#value' => array(
-      array('data' => t('Name')),   
-      array('data' => t('Expiry')),
-      array('data' => t('Status')),  
-      array('data' => t('UID')),
-      array('data' => t('Account Type')),  
-    )
-  );
-  $user_accounttypes = _account_expiry_getuseraccounttypes();
-  
-  $sql = 'SELECT a.uid, u.name, a.expiry_date, u.status 
-            FROM {account_expiry} a 
-      INNER JOIN {users} u
-              ON a.uid = u.uid
-           WHERE u.uid > %d
-           ORDER BY %s';
-    
-  $result = db_query($sql, 1, 'expiry_date');
-  
-  while ($existing = db_fetch_object($result)) {     
-    $atid = $user_accounttypes[$existing->uid]['atid'];     
-    $form['atid'][$atid]['uid'][$existing->uid] = array(    
-      '#value' => $existing->uid,    
-    );
-    $form['atid'][$atid]['name'][$existing->uid] = array(    
-      '#value' => $existing->name,    
-    );        
-    $form['atid'][$atid]['status'][$existing->uid] = array(    
-      '#value' => $existing->status,    
-    );    
-    $form['atid'][$atid]['accounttype'][$existing->uid] = array(    
-      '#value' => $user_accounttypes[$existing->uid]['name'],    
-    );            
-    $form['atid'][$atid]['expiry_date'][$existing->uid] = array(
-      '#type' => 'date',            
-      '#default_value' => array(
-        'day'   => format_date($existing->expiry_date, 'custom', 'j'),
-        'month' => format_date($existing->expiry_date, 'custom', 'n'),    
-        'year'  => format_date($existing->expiry_date, 'custom', 'Y')    
-      ),
-    ); 
-    $form['atid'][$atid]['expiry_date_orig'][$existing->uid] = array(
-      '#type' => 'value', 
-      '#value' => $existing->expiry_date,
-    );    
-  }
-  $form['expiry_date_edit'] = array(
-    '#type' => 'value', 
-    '#value' => array(),
-  );
-  $form['submit'] = array(
-    '#type' => 'submit',
-    '#value' => t('Submit')
-  );
-  $form['#tree'] = TRUE;
-    return $form;
-}
-
-function theme_account_expiry_user_settings($form) {
-   $status = array('Blocked', 'Active');
-   $output = "";
-   foreach (element_children($form['atid']) as $atid) {
-     $rows = array();
-     $legend = t('Account expiry settings for %atname accounts', array('%atname' => _account_expiry_getaccounttypename($atid)));
-     $output .= "<div><fieldset><legend>". $legend ."</legend>";
-     foreach (element_children($form['atid'][$atid]['expiry_date']) as $key) {
-       $row = array();
-       $row['data'][0] = l(drupal_render($form['atid'][$atid]['name'][$key]), 'user/'.  drupal_render($form['atid'][$atid]['uid'][$key]));
-       $row['data'][1] = drupal_render($form['atid'][$atid]['expiry_date'][$key]); 
-       $row['data'][2] = $status[drupal_render($form['atid'][$atid]['status'][$key])]; 
-       $row['data'][3] = $key;
-       $row['data'][4] = drupal_render($form['atid'][$atid]['accounttype'][$key]);
-       $rows[] = $row;
-     }
-     $output .= theme('table', $form['header']['#value'], $rows);
-     $output .= "</fieldset></div>"; 
-   }
-   $output .= drupal_render($form); // Process any other fields and display them
-   return $output;
-}
-
-function account_expiry_user_settings_submit($form_id, $form_values) {
-  foreach (element_children($form_values['atid']) as $atid ) {
-    foreach (element_children($form_values['atid'][$atid]['expiry_date']) as $uid ) {
-      $old_timestamp = $form_values['atid'][$atid]['expiry_date_orig'][$uid];
-      $day = $form_values['atid'][$atid]['expiry_date'][$uid]['day'];
-      $month = $form_values['atid'][$atid]['expiry_date'][$uid]['month'];
-      $year = $form_values['atid'][$atid]['expiry_date'][$uid]['year'];    
-      $expiry_timestamp = mktime(0, 0, 0, $month, $day, $year);
-      account_expiry_edit($uid, $expiry_timestamp);   
-    }
-  }
-}
-
-function account_expiry_edit($uid, $expiry_timestamp = NULL) {
-  if (is_null($expiry_timestamp)) {
-    $expiry_timestamp = _account_expiry_getdefaultexpiry();
-  }  
-  $sql = "SELECT ae.uid, ae.expiry_date, u.status
-            FROM {users} u 
-      INNER JOIN {account_expiry} ae ON u.uid = ae.uid 
-           WHERE u.uid = %d";
-  $result = db_query($sql, $uid);                        
-  if ($existing = db_fetch_object($result)) {    
-    account_expiry_update($uid, $existing, $expiry_timestamp);        
-  }
-  else {
-    account_expiry_insert($uid, $expiry_timestamp);
-  }
-}
-
-function account_expiry_insert($uid, $expiry_timestamp) {    
-  $sql = "INSERT INTO {account_expiry} (uid, expiry_date) VALUES (%d, %d)";
-  if (db_query($sql, $uid, $expiry_timestamp)) {
-    drupal_set_message(t('User expiry date set.'));
-  }
-  else {
-    drupal_set_message(t('User expiry date has NOT been set.'), 'error');
-  }
-}
-
-function account_expiry_update($uid, $existing, $expiry_timestamp) {
-  //Convert the date array to a unix timestamp
-  //http://drupal.org/node/61580 & http://drupal.org/node/46805
-  $now = time();      
-  if ($existing->expiry_date != $expiry_timestamp) {    // Date got changed on the form
-    $sql = "UPDATE {account_expiry} SET expiry_date = %d WHERE uid = %d";
-    if (db_query($sql, $expiry_timestamp, $uid)) {
-      if (($expiry_timestamp < $now) AND ($existing->status == 1)) {
-        // Active user has expiry now in past
-        account_expiry_expire($uid);
-      }
-      else if (($expiry_timestamp > $now) AND ($existing->status == 0)) {
-        // Blocked user has expiry now in future
-        account_expiry_activate($uid);
-      }
-      else {// only date updated, no change of status
-        drupal_set_message(t('Expiry date updated for user ') . $uid);    
-      }
-    }
-    else {
-      drupal_set_message(t('Account Expiry update failed for user ') . $uid, 'error');
-    }            
-  }
-}
-      
-function account_expiry_expire($uid) {
-  $sql = "UPDATE {users} SET status = 0 WHERE uid = %d";
-  $result = FALSE;
-  if (db_query($sql, $uid)) {
-    drupal_set_message(t('Account blocked for user '. $uid));
-    $result = TRUE;
-  }
-  else {
-    drupal_set_message(t('Account has NOT been blocked for user '. $uid), 'error');
-  }
-  return $result;
-}
-
-function account_expiry_activate($uid) {
-  $sql = "UPDATE {users} SET status = 1 WHERE uid = %d";
-  if (db_query($sql, $uid)) {
-    drupal_set_message(t('Account set to Active for user '. $uid));
-  }
-  else {
-    drupal_set_message(t('Account has NOT been set to Active for user '. $uid), 'error');
-  }
-}
-
-function account_expiry_delete($uid) {
-  $sql = "DELETE FROM {account_expiry} WHERE uid = %d";
-  if (db_query($sql, $uid)) {
-    drupal_set_message(t('Expiry date removed for user '. $uid));
-  }
-}
-
-//http://drupal.org/node/195244
-function account_expiry_cron() {
-  $now = time();
-  $sql = "SELECT u.uid, u.name 
-            FROM {users} u 
-      INNER JOIN {account_expiry} ae ON u.uid = ae.uid 
-           WHERE u.status = 1
-             AND ae.expiry_date < %d";
-  $result = db_query($sql, $now);
-  while ($expiry = db_fetch_object($result)) {
-    if (account_expiry_expire ($expiry->uid)) {
-      $message = t('Account blocked for user: @name ', array('@name' => $expiry->name));
-      $link = l('user '. $expiry->uid, 'user/'. $expiry->uid);
-      watchdog('Account Expiry', $message, WATCHDOG_NOTICE, $link);
-    }
-  }
-}
-
-function _account_expiry_getdefaultexpiry() {
-  $default_days = variable_get('account_expiry_default_days', 30);
-  $expiry_date = time() + ($default_days * 24 * 60 * 60);
-                         // default_days;  24 hours; 60 mins; 60secs
-  return $expiry_date;
-}
-
-function _account_expiry_getaccounttypename($atid) {
-  $sql = "SELECT name FROM {accounttypes} WHERE atid = %d";
-  $result = db_query($sql, $atid);
-  while ($accounttype = db_fetch_object($result)) {
-    $accounttypename = $accounttype->name;                
-  }
-  return $accounttypename;
-}
-
-function _account_expiry_getuseraccounttypes() {
-  $useraccounttypes = array();
-  $sql = "SELECT a.name, au.atid, au.uid
-            FROM {accounttypes_users} au 
-      INNER JOIN {accounttypes} a ON au.atid = a.atid WHERE au.uid > %d
-        ORDER BY %s";
-  $result = db_query($sql, 1, 'uid');
-  while ($useraccount = db_fetch_object($result)) {
-    $useraccounttypes[$useraccount->uid]['atid'] = $useraccount->atid;
-    $useraccounttypes[$useraccount->uid]['name'] = $useraccount->name;                
-  }
-  return $useraccounttypes;
-}
-
-function _account_expiry_isexpiryaccount($accountid) {
-  $accounttypes = variable_get('account_expiry_accounttypes', array(0));
-  $accountexpire = FALSE;
-  foreach ($accounttypes as $atid => $expire) {
-    if ($accountid == $atid && $expire) {
-      $accountexpire = TRUE;
-    }
-  }
-  return $accountexpire;
+<?php
+// $Id:
+
+/**
+* @file
+* Allows to maintain an expiry date for a user account
+*/
+
+function account_expiry_help($section = null) {
+  switch ($section) {
+    case 'admin/modules#description':
+    case 'admin/help#account_expiry':
+      return t('Allows to maintain an expiry date for a user account.');
+      break;
+  }
+}
+
+function account_expiry_perm() {
+  return array('administer account expiry');
+}
+
+function account_expiry_menu($may_cache) {
+  $items[] = array(
+    'path' => 'admin/settings/account_expiry',
+    'title' => t('Account Expiry'),
+    'description' => t('Change settings for the Account Expiry module.'),
+    'callback' => 'drupal_get_form',
+    'callback arguments' => 'account_expiry_admin_settings',
+    'access' => user_access('administer account expiry'),
+  );
+  $items[] = array(
+    'path' => 'admin/user/account_expiry',
+    'title' => t('User Account Expiry'),
+    'description' => t('Change expiry date for user account.'),
+    'callback' => 'drupal_get_form',
+    'callback arguments' => 'account_expiry_user_settings',
+    'access' => user_access('administer account expiry'),
+  );
+  return $items;
+}
+
+/**
+ * Implementation of hook_block().
+ */
+function account_expiry_block($op = 'list', $delta = 0, $edit = array()) {
+  global $user;
+  switch ($op) {
+    case 'list':
+      $blocks[0]['info'] = t('Account Expiry');
+      return $blocks;
+    case 'view':
+      switch ($delta) {
+        case 0:
+          $block['subject'] = t('Account Expiry');                    
+          $sql = "SELECT expiry_date FROM {account_expiry} WHERE uid = %d";
+          $result = db_query($sql, $user->uid);                            
+          while ($existing = db_fetch_object($result)) { 
+            $expirydate = $existing->expiry_date;
+          }                    
+          $block['content'] = format_date($expirydate);
+          break;
+      }
+      return $block;    
+  }
+}
+
+function account_expiry_admin_settings() {
+    
+  $form['defaults'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Account expiry default settings'),
+  );
+  $form['defaults']['account_expiry_default_days'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Default number of days until account expires'),
+    '#default_value' => variable_get('account_expiry_default_days', 30),
+    '#description' => t('Expiry date for particular users can be altered from this default'),
+    '#size' => 2,
+    '#maxlength' => 4,
+  );	
+	
+  $form['types'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Account types'),
+  );
+
+  if (module_exists('accounttypes')) {
+    $typeselector = "Account";
+    $message = t('AccountTypes module is active - set number of days until each account type will expire');
+    $selector = array();
+  }
+  else {
+    $typeselector = "";
+    $message = t('AccountTypes module is not active - ');
+    $selector = array();
+  }
+  $form['types']['message'] = array(    
+    '#value' => $message,    
+  );
+  switch ($typeselector) {
+    case 'Account':
+      //Get account types
+      $accounttypes = array();
+      $sql = "SELECT atid, name from {accounttypes}";
+      $result = db_query($sql);
+      
+  	  while ($at = db_fetch_object($result)) {
+        $form['types']['account_expiry_' . $at->atid] = array(
+	        '#type' => 'textfield',
+	        '#title' => t($at->name),
+	        '#default_value' => variable_get('account_expiry_' . $at->atid,''),                
+	      );
+      }
+      
+      break;
+  }        
+  return system_settings_form($form);
+}
+
+function account_expiry_form_alter($form_id, &$form) {
+  if ($form_id == 'user_edit' && user_access('administer account expiry')) {
+    $atid = $form['_account']['#value']->selectAT;
+    if (_account_expiry_isexpiryaccount($atid)) {            
+      $uid = $form['_account']['#value']->uid;
+      $sql = "SELECT expiry_date FROM {account_expiry} WHERE uid = %d";
+      $result = db_query($sql, $uid);                            
+      while ($existing = db_fetch_object($result)) { 
+        $expirydate = $existing->expiry_date;
+      }
+      $form['account']['expiry_date'] = array(
+        '#type' => 'date',
+        '#title' => t('Account expiry date'),  
+        '#weight' => 2,          
+        '#default_value' => array(
+          'day'   => format_date($expirydate, 'custom', 'j'),
+          'month' => format_date($expirydate, 'custom', 'n'),    
+          'year'  => format_date($expirydate, 'custom', 'Y')    
+        ),
+        '#description' => t('Set accordingly'),
+      );
+    }
+  }    
+}
+
+function account_expiry_user($op, $edit, &$account, $category = NULL) {
+  if (!$edit['selectAT']) {
+    if (isset($account->selectAT)) {
+      $select_at = $account->selectAT;
+    }
+  } 
+  else {
+    $select_at = $edit['selectAT'];
+  }
+  switch ($op) {
+    case 'view':        
+
+      if (_account_expiry_isexpiryaccount($select_at)) {
+        $sql = "SELECT expiry_date FROM {account_expiry} WHERE uid = %d";
+        $result = db_query($sql, $account->uid);                            
+        while ($existing = db_fetch_object($result)) { 
+          $expirydate = $existing->expiry_date;
+        }
+        $items[] = array(
+          'value' => format_date($expirydate),
+          'class' => 'accountexpiry',
+        );
+        return array(t('Account Expiry Date') => $items);
+      }  
+      break;
+	  
+    case 'insert':
+      if (_account_expiry_isexpiryaccount($select_at)) {
+        account_expiry_edit($edit['uid'],$select_at);
+      }
+      break;
+	  
+    case 'after_update': 
+      //dsm($account);
+      //dsm($edit); 
+      if (_account_expiry_isexpiryaccount($select_at)) {              
+        $day = $account->expiry_date['day'];
+        $month = $account->expiry_date['month'];
+        $year = $account->expiry_date['year'];    
+        $expiry_timestamp = mktime(0, 0, 0, $month, $day, $year);            
+        account_expiry_edit($account->uid, $select_at,$expiry_timestamp);
+      }
+      else {
+        account_expiry_delete($account->uid);
+      }        
+      break;
+	  
+    case 'delete': 
+      account_expiry_delete($account->uid);
+      break;
+  }
+}
+
+
+function account_expiry_user_settings() {     
+  $form['header'] = array(
+    '#type' => 'value',
+    '#value' => array(
+      array('data' => t('Name')),   
+      array('data' => t('Expiry')),
+      array('data' => t('Status')),  
+      array('data' => t('UID')),
+      array('data' => t('Account Type')),  
+    )
+  );
+  $user_accounttypes = _account_expiry_getuseraccounttypes();
+  
+  $sql = 'SELECT a.uid, u.name, a.expiry_date, u.status 
+            FROM {account_expiry} a 
+      INNER JOIN {users} u
+              ON a.uid = u.uid
+           WHERE u.uid > %d
+           ORDER BY %s';
+    
+  $result = db_query($sql, 1, 'expiry_date');
+  
+  while ($existing = db_fetch_object($result)) {     
+    $atid = $user_accounttypes[$existing->uid]['atid'];     
+    $form['atid'][$atid]['uid'][$existing->uid] = array(    
+      '#value' => $existing->uid,    
+    );
+    $form['atid'][$atid]['name'][$existing->uid] = array(    
+      '#value' => $existing->name,    
+    );        
+    $form['atid'][$atid]['status'][$existing->uid] = array(    
+      '#value' => $existing->status,    
+    );    
+    $form['atid'][$atid]['accounttype'][$existing->uid] = array(    
+      '#value' => $user_accounttypes[$existing->uid]['name'],    
+    );            
+    $form['atid'][$atid]['expiry_date'][$existing->uid] = array(
+      '#type' => 'date',            
+      '#default_value' => array(
+        'day'   => format_date($existing->expiry_date, 'custom', 'j'),
+        'month' => format_date($existing->expiry_date, 'custom', 'n'),    
+        'year'  => format_date($existing->expiry_date, 'custom', 'Y')    
+      ),
+    ); 
+    $form['atid'][$atid]['expiry_date_orig'][$existing->uid] = array(
+      '#type' => 'value', 
+      '#value' => $existing->expiry_date,
+    );    
+  }
+  $form['expiry_date_edit'] = array(
+    '#type' => 'value', 
+    '#value' => array(),
+  );
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Submit')
+  );
+  $form['#tree'] = TRUE;
+    return $form;
+}
+
+function theme_account_expiry_user_settings($form) {
+   $status = array('Blocked', 'Active');
+   $output = "";
+   foreach (element_children($form['atid']) as $atid) {
+     $rows = array();
+     $legend = t('Account expiry settings for %atname accounts', array('%atname' => _account_expiry_getaccounttypename($atid)));
+     $output .= "<div><fieldset><legend>". $legend ."</legend>";
+     foreach (element_children($form['atid'][$atid]['expiry_date']) as $key) {
+       $row = array();
+       $row['data'][0] = l(drupal_render($form['atid'][$atid]['name'][$key]), 'user/'.  drupal_render($form['atid'][$atid]['uid'][$key]));
+       $row['data'][1] = drupal_render($form['atid'][$atid]['expiry_date'][$key]); 
+       $row['data'][2] = $status[drupal_render($form['atid'][$atid]['status'][$key])]; 
+       $row['data'][3] = $key;
+       $row['data'][4] = drupal_render($form['atid'][$atid]['accounttype'][$key]);
+       $rows[] = $row;
+     }
+     $output .= theme('table', $form['header']['#value'], $rows);
+     $output .= "</fieldset></div>"; 
+   }
+   $output .= drupal_render($form); // Process any other fields and display them
+   return $output;
+}
+
+function account_expiry_user_settings_submit($form_id, $form_values) {
+  foreach (element_children($form_values['atid']) as $atid ) {
+    foreach (element_children($form_values['atid'][$atid]['expiry_date']) as $uid ) {
+      $old_timestamp = $form_values['atid'][$atid]['expiry_date_orig'][$uid];
+      $day = $form_values['atid'][$atid]['expiry_date'][$uid]['day'];
+      $month = $form_values['atid'][$atid]['expiry_date'][$uid]['month'];
+      $year = $form_values['atid'][$atid]['expiry_date'][$uid]['year'];    
+      $expiry_timestamp = mktime(0, 0, 0, $month, $day, $year);
+      account_expiry_edit($uid,$atid, $expiry_timestamp);   
+    }
+  }
+}
+
+function account_expiry_edit($uid, $atid,$expiry_timestamp = NULL) {
+  if (is_null($expiry_timestamp)) {
+  		if(is_null($atid))
+  			$expiry_timestamp = _account_expiry_getdefaultexpiry();
+  		else
+    		$expiry_timestamp = _account_expiry_getexpiry($atid);
+  }  
+  $sql = "SELECT ae.uid, ae.expiry_date, u.status
+            FROM {users} u 
+      INNER JOIN {account_expiry} ae ON u.uid = ae.uid 
+           WHERE u.uid = %d";
+  $result = db_query($sql, $uid);                        
+  if ($existing = db_fetch_object($result)) {    
+    account_expiry_update($uid, $existing, $expiry_timestamp);        
+  }
+  else {
+    account_expiry_insert($uid, $expiry_timestamp);
+  }
+}
+
+function account_expiry_insert($uid, $expiry_timestamp) {    
+  $sql = "INSERT INTO {account_expiry} (uid, expiry_date) VALUES (%d, %d)";
+  if (db_query($sql, $uid, $expiry_timestamp)) {
+    drupal_set_message(t('User expiry date set.'));
+  }
+  else {
+    drupal_set_message(t('User expiry date has NOT been set.'), 'error');
+  }
+}
+
+function account_expiry_update($uid, $existing, $expiry_timestamp) {
+  //Convert the date array to a unix timestamp
+  //http://drupal.org/node/61580 & http://drupal.org/node/46805
+  $now = time();      
+  if ($existing->expiry_date != $expiry_timestamp) {    // Date got changed on the form
+    $sql = "UPDATE {account_expiry} SET expiry_date = %d WHERE uid = %d";
+    if (db_query($sql, $expiry_timestamp, $uid)) {
+      if (($expiry_timestamp < $now) AND ($existing->status == 1)) {
+        // Active user has expiry now in past
+        account_expiry_expire($uid);
+      }
+      else if (($expiry_timestamp > $now) AND ($existing->status == 0)) {
+        // Blocked user has expiry now in future
+        account_expiry_activate($uid);
+      }
+      else {// only date updated, no change of status
+        drupal_set_message(t('Expiry date updated for user ') . $uid);    
+      }
+    }
+    else {
+      drupal_set_message(t('Account Expiry update failed for user ') . $uid, 'error');
+    }            
+  }
+}
+      
+function account_expiry_expire($uid) {
+  $sql = "UPDATE {users} SET status = 0 WHERE uid = %d";
+  $result = FALSE;
+  if (db_query($sql, $uid)) {
+    drupal_set_message(t('Account blocked for user '. $uid));
+    $result = TRUE;
+  }
+  else {
+    drupal_set_message(t('Account has NOT been blocked for user '. $uid), 'error');
+  }
+  return $result;
+}
+
+function account_expiry_activate($uid) {
+  $sql = "UPDATE {users} SET status = 1 WHERE uid = %d";
+  if (db_query($sql, $uid)) {
+    drupal_set_message(t('Account set to Active for user '. $uid));
+  }
+  else {
+    drupal_set_message(t('Account has NOT been set to Active for user '. $uid), 'error');
+  }
+}
+
+function account_expiry_delete($uid) {
+  $sql = "DELETE FROM {account_expiry} WHERE uid = %d";
+  if (db_query($sql, $uid)) {
+    drupal_set_message(t('Expiry date removed for user '. $uid));
+  }
+}
+
+//http://drupal.org/node/195244
+function account_expiry_cron() {
+  $now = time();
+  $sql = "SELECT u.uid, u.name 
+            FROM {users} u 
+      INNER JOIN {account_expiry} ae ON u.uid = ae.uid 
+           WHERE u.status = 1
+             AND ae.expiry_date < %d";
+  $result = db_query($sql, $now);
+  while ($expiry = db_fetch_object($result)) {
+    if (account_expiry_expire ($expiry->uid)) {
+      $message = t('Account blocked for user: @name ', array('@name' => $expiry->name));
+      $link = l('user '. $expiry->uid, 'user/'. $expiry->uid);
+      watchdog('Account Expiry', $message, WATCHDOG_NOTICE, $link);
+    }
+  }
+}
+
+function _account_expiry_getdefaultexpiry() {
+  $default_days = variable_get('account_expiry_default_days', 30);
+  $expiry_date = time() + ($default_days * 24 * 60 * 60);
+                         // default_days;  24 hours; 60 mins; 60secs
+  return $expiry_date;
+}
+
+function _account_expiry_getexpiry($atid) {
+  $expiry_days = variable_get('account_expiry_' . $atid, FALSE);
+  $expiry_date = time() + ($expiry_days * 24 * 60 * 60);
+                         // expiry_days;  24 hours; 60 mins; 60secs
+  return $expiry_date;
+}
+
+function _account_expiry_getaccounttypename($atid) {
+  $sql = "SELECT name FROM {accounttypes} WHERE atid = %d";
+  $result = db_query($sql, $atid);
+  while ($accounttype = db_fetch_object($result)) {
+    $accounttypename = $accounttype->name;                
+  }
+  return $accounttypename;
+}
+
+function _account_expiry_getuseraccounttypes() {
+  $useraccounttypes = array();
+  $sql = "SELECT a.name, au.atid, au.uid
+            FROM {accounttypes_users} au 
+      INNER JOIN {accounttypes} a ON au.atid = a.atid WHERE au.uid > %d
+        ORDER BY %s";
+  $result = db_query($sql, 1, 'uid');
+  while ($useraccount = db_fetch_object($result)) {
+    $useraccounttypes[$useraccount->uid]['atid'] = $useraccount->atid;
+    $useraccounttypes[$useraccount->uid]['name'] = $useraccount->name;                
+  }
+  return $useraccounttypes;
+}
+
+function _account_expiry_isexpiryaccount($accountid) {
+	
+  $accountexpire = 	variable_get('account_expiry_' . $accountid, FALSE);
+  return $accountexpire;
 }
\ No newline at end of file
