Index: civicrm_drupal_username_sync.module
===================================================================
--- civicrm_drupal_username_sync.module	(revision 2022)
+++ civicrm_drupal_username_sync.module	(working copy)
@@ -1,195 +1,192 @@
 <?php
-  // $Id: civicrm_drupal_username_sync.module,v 1.0 2009/11/06 18:00:00 webaccess Exp $
-  /* @file
-   * A simple module that syncs a drupal user to CiviCRM custom field Username.
-   */
+// $Id: civicrm_drupal_username_sync.module,v 1.0 2009/11/06 18:00:00 webaccess Exp $
 
-  /**
-   * Implementation of hook_help()
-   */
+/**
+ * @file
+ *   A simple module that syncs a drupal user to CiviCRM custom field Username.
+ */
 
+/**
+ * Implementation of hook_help().
+ */
 function civicrm_drupal_username_sync_help($section, $args = array()) { 
-    switch ($section) { 
+  switch ($section) { 
     case 'admin/modules#description': 
-        return t('Synchronizes Drupal Username with the CiviCRM custom field - Username whenever user logs into Drupal.');
-    default :
-        return;
-    } 
+      return t('Synchronizes Drupal Username with the CiviCRM custom field - Username whenever user logs into Drupal.');
+    default:
+      return;
   } 
+} 
 
-
 /**
  * Implementation of hook_perm().
  */
 function civicrm_drupal_username_sync_perm() {
-    return array('access settings');
+  return array('access settings');
 }
 
-
 /**
  * Implementation of hook_user().
- * This provides sync from Drupal -> CiviCRM
- *
+ * This provides syncronization from Drupal into a CiviCRM custom field.
  */
 function civicrm_drupal_username_sync_user($op, $edit, &$user, $category = NULL) {
-    civicrm_initialize(true);
-    if ($op == 'login') {
-        //find the contact record
-        require_once 'api/v2/UFGroup.php';
-        $contact = civicrm_uf_match_id_get($user->uid);
-        $cfObject = db_fetch_object(db_query('SELECT cf_id FROM {civicrm_drupal_username_sync}'));
-        global $civicrm_root;
-        require_once $civicrm_root . '/CRM/Core/DAO.php';
-        $customData = array();
-        $params = array();
-        
-        if ($cfObject->cf_id) {
-            $query  = 'SELECT custom_group_id, column_name FROM civicrm_custom_field WHERE id='. $cfObject->cf_id;
-            $dao    = & CRM_Core_DAO::executeQuery($query, $params);
-            while ($dao->fetch()) {
-                $customData['column'] = $dao->column_name;
-                $customData['group']  = $dao->custom_group_id;
-            }   
-            $tableName = CRM_Core_DAO::getFieldValue( 'CRM_Core_DAO_CustomGroup',
-                                                      $customData['group'],
-                                                      'table_name' );
+  civicrm_initialize(true);
+  if ($op == 'login') {
+    // Find the contact record.
+    global $civicrm_root;
+    require_once 'api/v2/UFGroup.php';
+    require_once $civicrm_root . '/CRM/Core/DAO.php';
+    $contact = civicrm_uf_match_id_get($user->uid);
+    $cfObject = db_fetch_object(db_query('SELECT cf_id FROM {civicrm_drupal_username_sync}'));
+    $customData = array();
+    $params = array();
 
-            // To check if civicrm contact present
-            if ($contact) {
-                // To check the entry is present in the custom table
-                $querySel = "SELECT {$customData['column']} FROM {$tableName} WHERE entity_id={$contact}";
-                $row = array();
-                $daoSel  = & CRM_Core_DAO::executeQuery($querySel, $row);
-                                
-                $row = array();
-                if ($daoSel->fetch()) {
-                    $queryUp = "UPDATE {$tableName} 
-                                SET    {$customData['column']} = '{$user->name}' 
-                                WHERE  entity_id = {$contact}";
-                    $daoUp   = & CRM_Core_DAO::executeQuery($queryUp, $row);
-                }
-                else {
-                    $queryIns = "INSERT INTO {$tableName} (entity_id,{$customData['column']} ) 
-                                 VALUES ({$contact},'{$user->name}')";
-                    $daoIns   = & CRM_Core_DAO::executeQuery($queryIns, $row);
-                }
-            }
+    if ($cfObject->cf_id) {
+      $query = 'SELECT custom_group_id, column_name FROM civicrm_custom_field WHERE id = %1';
+      $params[1] = array($cfObject->cf_id, "Integer");
+      $dao = & CRM_Core_DAO::executeQuery($query, $params);
+      while ($dao->fetch()) {
+        $customData['column'] = $dao->column_name;
+        $customData['group'] = $dao->custom_group_id;
+      }
+
+      $tableName = CRM_Core_DAO::getFieldValue( 'CRM_Core_DAO_CustomGroup',
+                                                $customData['group'],
+                                                'table_name' );
+
+      // To check if the CiviCRM contact present.
+      if ($contact) {
+        // To check the entry is present in the custom table.
+        $querySel = "SELECT %1 FROM %2 WHERE entity_id = %3";
+        $selectParams = array(1 => array($customData['column'], 'String'), 2 => array($tableName, 'String'), 3 => array($contact, 'Integer'));
+        $daoSel  = & CRM_Core_DAO::executeQuery($querySel, $selectParams);
+                        
+        $row = array();
+        if ($daoSel->fetch()) {
+          $queryUp = "UPDATE %1 SET %2 = %3 WHERE entity_id = %4";
+          $upParams = array(1 => array($tableName, 'String'), 2 => array($customData['column'], 'String'), 3 => array($user->name, 'String'), 4 => array($contact, 'Integer'));
+          $daoUp   = & CRM_Core_DAO::executeQuery($queryUp, $upParams);
         }
+        else {
+          $queryIns = "INSERT INTO %1 (entity_id, %2) VALUES (%3, %4)";
+          $insParams = array(1 => array($tableName, 'String'), 2 => array($customData['column'], 'String'), 3 => array($contact, 'Integer'), 4 => array($user->name, 'String'));
+          $daoIns   = & CRM_Core_DAO::executeQuery($queryIns, $insParams);
+        }
+      }
     }
+  }
 }
 
 /**
  * Implementation of hook_menu().
  */
 function civicrm_drupal_username_sync_menu() {
-    $items[] = array();
-    $items['admin/settings/civicrm_drupal_username_sync/add_rule'] = 
-        array(
-              'title' => t('Drupal Username to CiviCRM Sync'), 
-              'description' => t('Update Drupal Username with CiviCRM Custom Field.'),
-              'page callback' => 'drupal_get_form',
-              'page arguments' => array('civicrm_drupal_username_sync_add_rule_form'),
-              'access callback' => 'user_access',
-              'access arguments' => array('access settings'),
-              'type' => MENU_NORMAL_ITEM
-              ); 
-    return $items;
+  $items[] = array();
+  $items['admin/settings/civicrm-drupal-username-sync/add-rule'] = array(
+    'title' => 'Drupal Username to CiviCRM Sync',
+    'description' => 'Update Drupal Username with CiviCRM Custom Field.',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('civicrm_drupal_username_sync_add_rule_form'),
+    'access callback' => 'user_access',
+    'access arguments' => array('access settings'),
+    'type' => MENU_NORMAL_ITEM,
+  ); 
+  return $items;
 }
 
 /**
- * Function to retrive custom field data
+ * Function to retrieve custom field data.
+ *
+ * @return
+ *   An array with keys matching field ID's and values matching the associated
+ *   field label. 
  */
 function _civicrm_drupal_username_sync_get_data() {
-    civicrm_initialize(TRUE);
-    global $civicrm_root;
-    require_once $civicrm_root . '/CRM/Core/DAO.php';
-    $params = array();
-    $query  = 'SELECT id, label FROM civicrm_custom_field WHERE 1';
-    $dao    = & CRM_Core_DAO::executeQuery($query, $params);
-    $customList = array();
-    while ($dao->fetch()) {
-        $customList[$dao->id] = $dao->label;
-    }
-    return $customList;
+  civicrm_initialize(TRUE);
+  global $civicrm_root;
+  require_once $civicrm_root . '/CRM/Core/DAO.php';
+  $params = array();
+  $query  = "SELECT id, label FROM civicrm_custom_field WHERE data_type = 'String'";
+  $dao    = & CRM_Core_DAO::executeQuery($query, $params);
+  $customList = array();
+  while ($dao->fetch()) {
+      $customList[$dao->id] = $dao->label;
+  }
+  return $customList;
 }
 
 /**
- * Implementation of hook_form(). Add/edit Custom data sync rules.
+ * Forms API callback to add and edit Custom data sync rules.
  *
  * @ingroup forms
  */
 function civicrm_drupal_username_sync_add_rule_form($form = NULL, $id = NULL) {
-    $hasValue = _civicrm_drupal_username_sync_get_data();
-    if ($hasValue) {
-        $civisyncOptions = $hasValue;
-    }
-    else {
-        $civisyncOptions = array(0 => '--------');
-    }
-    // To set the default value of the custom field.
-    $selectCustomField = db_fetch_object(db_query('SELECT cf_id FROM {civicrm_drupal_username_sync}'));
-    //Begin add form
-    $form = array();      
-    $form['add_rule'] = array(
-                              '#type' => 'fieldset',
-                              '#title' => t('Drupal Username to CiviCRM Sync'),
-                              '#description' => t('Choose a CiviCRM custom Field that will associated with the Drupal Username.'),
-                              '#tree' => TRUE,
-                              '#parents' => array('add_rule'), 
-                              );   
-    $form['add_rule']['select_customdata'] = array(
-                                                   '#type' => 'select',
-                                                   '#title' => t('Select a CiviCRM Custom Field'),
-                                                   '#options' => $civisyncOptions,
-                                                   '#required' => TRUE,
-                                                   '#default_value' => $selectCustomField->cf_id
-                                                   );    
-    
-    
-    $form['submit'] = array(
-                            '#type' => 'submit',
-                            '#value' => t('Save Rule'),
-                            );
-    
-    return $form;
+  $hasValue = _civicrm_drupal_username_sync_get_data();
+  if ($hasValue) {
+    $civisyncOptions = $hasValue;
+  }
+  else {
+    $civisyncOptions = array(0 => '--------');
+  }
+  // To set the default value of the custom field.
+  $selectCustomField = db_fetch_object(db_query('SELECT cf_id FROM {civicrm_drupal_username_sync}'));
+
+  // Begin add form.
+  $form = array();      
+  $form['add_rule'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Drupal Username to CiviCRM Sync'),
+    '#description' => t('Choose a CiviCRM custom Field that will associated with the Drupal Username.'),
+    '#tree' => TRUE,
+    '#parents' => array('add_rule'),
+  );
+  $form['add_rule']['select_customdata'] = array(
+    '#type' => 'select',
+    '#title' => t('Select a CiviCRM Custom Field'),
+    '#options' => $civisyncOptions,
+    '#required' => TRUE,
+    '#default_value' => $selectCustomField->cf_id
+  );    
+
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => t('Save rule'),
+  );
+
+  return $form;
 }
 
 
 /**
- * Implementation of hook_validate() for the add/edit rule form.
+ * Forms API validation handler for civicrm_drupal_username_sync_add_rule_form.
  */
 function civicrm_drupal_username_sync_add_rule_form_validate($form, &$form_state) {
-    //Make sure there is a CiviCRM custom data is selected.
-    
-    if (is_numeric($form_state['values']['add_rule']['select_customdata'])) {
-        if ($form_state['values']['add_rule']['select_customdata'] == 0) {
-            form_set_error('add_rule', t('No custom data is available.'));
-        }
-    } 
+  // Make sure there is a CiviCRM custom data is selected.
+  if (is_numeric($form_state['values']['add_rule']['select_customdata'])) {
+    if ($form_state['values']['add_rule']['select_customdata'] == 0) {
+      form_set_error('add_rule', t('No custom data is available.'));
+    }
+  } 
 }
 
-
 /**
- * Implementation of hook_submit() for the add/edit rule form.
+ * Forms API submission handler for civicrm_drupal_username_sync_add_rule_form.
  */
 function civicrm_drupal_username_sync_add_rule_form_submit($form, &$form_state) {
-    
-    $id     = $form_state['values']['add_rule']['select_customdata'];
-    $selectSync = db_fetch_object(db_query('SELECT cf_id FROM {civicrm_drupal_username_sync}'));
+  $id = $form_state['values']['add_rule']['select_customdata'];
+  $selectSync = db_fetch_object(db_query('SELECT cf_id FROM {civicrm_drupal_username_sync}'));
+
+  if ($selectSync) {
+    $addRule = db_query('UPDATE {civicrm_drupal_username_sync} SET cf_id = %d', array($id));
+  }
+  else {
+    $addRule = db_query('INSERT INTO {civicrm_drupal_username_sync} (cf_id) VALUES(%d)', array((int) $id));
+  }
   
-    if ($selectSync) {
-        $addRule = db_query('UPDATE {civicrm_drupal_username_sync} SET cf_id = '. $id );
-    }
-    else {
-        $addRule = db_query('INSERT INTO {civicrm_drupal_username_sync} (cf_id)
-                             VALUES( %d )', 
-                             (int) $id );
-    }
-    
-    if ($addRule) {
-        drupal_set_message(t('Rule has been added.'));
-    }
-    else {
-        drupal_set_message(t('There was an error adding this rule. Please check your database settings and try again. If you continue to get this error message then try to reinstall CiviCRM <> Drupal Username Sync.'), $type = 'error');
-    }
+  if ($addRule) {
+    drupal_set_message(t('Rule has been added.'));
+  }
+  else {
+    drupal_set_message(t('There was an error adding this rule. Please check your database settings and try again. If you continue to get this error message then try to reinstall the CiviCRM <> Drupal Username Sync module.'), $type = 'error');
+  }
 }
Index: civicrm_drupal_username_sync.install
===================================================================
--- civicrm_drupal_username_sync.install	(revision 2022)
+++ civicrm_drupal_username_sync.install	(working copy)
@@ -1,39 +1,40 @@
 <?php
-  // $Id$ civicrm_drupal_username_sync.install,v 1.0 2009/11/06 18:00:00 webaccess Exp $
-  /**
-   * @file
-   * Install CiviCRM Nickname and Drupal username Sync database table and provide hook_uninstall.
-   */
+// $Id$ civicrm_drupal_username_sync.install,v 1.0 2009/11/06 18:00:00 webaccess Exp $
 
-  /**
-   * Implementation of hook_install().
-   */
+/**
+ * @file
+ * Install CiviCRM Nickname and Drupal username Sync database table and provide hook_uninstall.
+ */
+
+/**
+ * Implementation of hook_install().
+ */
 function civicrm_drupal_username_sync_install() {
-    //Create table.
-    drupal_install_schema('civicrm_drupal_username_sync');
-  }
+  // Create table.
+  drupal_install_schema('civicrm_drupal_username_sync');
+}
 
 /**
  * Implementation of hook_schema().
  */
 function civicrm_drupal_username_sync_schema() {
-$schema['civicrm_drupal_username_sync'] = array(
-  'fields' => array(
-                   'rule_id' => array(
-                   'type' => 'serial', 
-                   'unsigned' => TRUE, 
-                   'not null' => TRUE,
-                   ),
-  'cf_id' => array(
-                   'type' => 'int', 
-                   'unsigned' => TRUE, 
-                   'not null' => TRUE,
-                  ),
-                ),
-  'primary key' => array('rule_id'),
+  $schema['civicrm_drupal_username_sync'] = array(
+    'fields' => array(
+       'rule_id' => array(
+       'type' => 'serial',
+       'unsigned' => TRUE,
+       'not null' => TRUE,
+     ),
+    'cf_id' => array(
+       'type' => 'int',
+       'unsigned' => TRUE,
+       'not null' => TRUE,
+      ),
+    ),
+    'primary key' => array('rule_id'),
   );
-  
-    return $schema;
+
+  return $schema;
 }
 
 /**
@@ -42,4 +43,5 @@
 function civicrm_drupal_username_sync_uninstall() {
     drupal_uninstall_schema('civicrm_drupal_username_sync');
     variable_del('civicrm_drupal_username_sync_method');
-}
\ No newline at end of file
+}
+