# This patch file was generated by NetBeans IDE
# This patch can be applied using context Tools: Apply Diff Patch action on respective folder.
# It uses platform neutral UTF-8 encoding.
# Above lines and this line are ignored by the patching process.
Index: contributions/modules/millennium/millennium_auth.module
--- contributions/modules/millennium/millennium_auth.module Base (1.1.2.8)
+++ contributions/modules/millennium/millennium_auth.module Locally Modified (Based On 1.1.2.8)
@@ -47,6 +47,15 @@
     '#description' => t('Note: your OPAC must have the appropriate configuration in place for this to work.'),
     '#default_value' => variable_get('millennium_auth_use_https', FALSE),
   );
+  $form['login']['millennium_auth_login_fields'] = array(
+    '#type' => 'radios',
+    '#title' => t('Number of login fields configured on Millennium system'),
+    '#options' => array(
+          '2' => t('2 boxes: e.g. name + password, barcode + password (or similar)'),
+          '3' => t('3 boxes: e.g. name + barcode + password, name + last name + password (or similar)'),
+        ),
+    '#default_value' => variable_get('millennium_auth_login_fields', '2'),
+  );
   $form['login']['millennium_auth_id_message'] = array(
     '#type' => 'textfield',
     '#title' => t('Library ID field help message'),
@@ -105,15 +114,17 @@
           $msg = t('You have no checked out items.');
         }
 
-        $msg2 = t('@num-holds holds, @num-ready ready for pickup', 
-        array(
-          '@num-holds' => intval($patroninfo_data["number_holds"]),
-          '@num-ready' => intval($patroninfo_data["number_holds_ready"])
-          ) );
+        $msg2 = t(
+          '@num-holds holds, @num-ready ready for pickup.', 
+          array(
+            '@num-holds' => intval($patroninfo_data["number_holds"]),
+            '@num-ready' => intval($patroninfo_data["number_holds_ready"])
+          )
+        );
 
         $account->content['millennium'] = array(
          '#type' => 'user_profile_category',
-         '#title' => t("Library account status"),
+         '#title' => t('Library account status'),
          '#attributes' => array('class' => 'millennium items'),
          '#weight' => 1,
         );
@@ -139,7 +150,7 @@
           '#attributes' => array('class' => 'millennium items'),
         );
         
-        #number_holds_ready
+        #TODO: number_holds_ready
 
       } else {
         drupal_set_message(t('Could not fetch checkout information from the library system. Please try again later.'), "error");
@@ -176,7 +187,7 @@
         // Log new user
         watchdog(
           'millennium auth', 
-          'New user from millennium auth: %name', 
+          'New user from millennium_auth: %name', 
           array('%name' => $account->name) 
         );
         $account->roles = array();
@@ -207,7 +218,7 @@
   if (!is_array($items)) {
     return "";
   }
-  foreach($items as $item) {
+  foreach ($items as $item) {
     // Has record been imported into Drupal?
     $nid = db_result(db_query("SELECT nid FROM {millennium_bib_item} where item_recnum='%s' AND base_url = '%s'", $item["item_recnum"], variable_get('millennium_auth_default_base_url', '')));
     if ($nid) {
@@ -236,7 +247,7 @@
                 
     $list[] = $item_html;
   }
-  return theme('item_list', $list, "");
+  return theme('item_list', $list);
 }
 
 
@@ -248,15 +259,22 @@
   #dsm($form_values);
   
   #$username, $password, $server
-  $username = $form_values["name"];
-  $password = $form_values["pass"];
-  $lastname = $form_values["lastname"];
+  if (variable_get('millennium_auth_login_fields', '2') == '3') {
+    $name = $form_values["lastname"];
+    $password = $form_values["pass"];
+    $code = $form_values["name"];
+  }
+  else {
+    $name = $form_values["name"];
+    $code = $form_values["pass"];
+  }
   
-  $lastname = $_POST["lastname"];
-  _millennium_auth_debug("millennium_auth_authenticate('$username', '$password', '$server'). Lastname = '$lastname'");
+  #$code = $_POST["lastname"];
+  #drupal_set_message("POST lastname = $code"); // DEBUG
+  _millennium_auth_debug("millennium_auth_authenticate('$name', '$password', '$server'). Lastname = '$code'");
     
   // Check user data against Millennium
-  $patroninfo_data = _millennium_auth_get_patroninfo_by_pindata($username, $lastname, $password);
+  $patroninfo_data = _millennium_auth_get_patroninfo_by_pindata($name, $code, $password);
   $result = false;
   if ($patroninfo_data["success"] === true) {
     _millennium_auth_debug("millennium_auth_authenticate(): _millennium_auth_get_patroninfo_by_pindata()[success] returned true");
@@ -264,30 +282,30 @@
     foreach($patroninfo_data as $index => $value) {
       _millennium_auth_debug("millennium_auth_auth(): $index = " . $value);
     }
-    $millennium_patroninfo[$username] = $patroninfo_data;
+    $millennium_patroninfo[$name] = $patroninfo_data;
     
     // Flag a successful attempt at authentication, for hook_user::insert ... new users will still not have their emails
     global $millennium_auth_newuser;
     $millennium_auth_newuser = array(
-      "lastname" => $lastname, 
-      "username" => $username, 
+      "lastname" => $code, 
+      "username" => $name, 
       "password" => $password
     );
     $result = true;
     
     // Log user in, or register new user if not already present.
-    user_external_login_register($username, 'millennium_auth');
+    user_external_login_register($name, 'millennium_auth');
 
     // Write session, update timestamp, run user 'login' hook.
     user_authenticate_finalize($form_values);
     
   } else {
-    
+    _millennium_auth_debug("Failed: millennium_auth_authenticate('$name', '$password', '$server')");
     $result = false;
     watchdog(
       'millennium auth', 
       'Error authenticating millennium user @lastname, @username: @errormsg', 
-      array('@username' => $username, '@errormsg' => $patroninfo_data['errormsg']) 
+      array('@username' => $name, '@errormsg' => $patroninfo_data['errormsg']) 
     );
   }
   patroninfo_end_session($patroninfo_data["session_id"]);
@@ -331,10 +349,10 @@
 /**
  * Fetch patroninfo using name, lastname and pin
  */
-function _millennium_auth_get_patroninfo_by_pindata($username, $lastname, $pin) {
+function _millennium_auth_get_patroninfo_by_pindata($name, $code, $pin) {
   static $patroninfo_data_cache;
   
-  $key = "$username|$lastname|" . crypt($pin, "salt");
+  $key = "$name|$code|" . crypt($pin, "salt");
   #drupal_set_message("Trying to get info for $key");
   
   if ($patroninfo_data_cache[$key]) {
@@ -359,7 +377,7 @@
   }
 
   // Connect to Millennium and get the patron's information
-  $patroninfo_data = patroninfo_start_session($base_url, $username, $lastname, $pin);
+  $patroninfo_data = patroninfo_start_session($base_url, $name, $code, $pin);
   
   if ($patroninfo_data["success"] != true) {
   	_millennium_auth_debug("_millennium_auth_get_patroninfo_by_pindata(): Error: ". $patroninfo_data["errormsg"]);
@@ -404,36 +422,39 @@
   }
   
   // Login form
-  if ($form_id == 'user_login_block' || $form_id == 'user_login') { 
-    #drupal_set_message(var_export($form,TRUE));
-    $form['lastname'] = array(
-      '#type' => 'textfield',
-      '#title' => t('Your last name'),
-      '#size' => 15,
-      '#description' => t('Example: smith'),
-      '#default_value' => '',
-      '#weight' => -50,
-      '#attributes' => array('tabindex' => 1),
-      #'#required' => 1,
-    );
-    $form['name'] = array(
-      '#type' => 'textfield',
-      '#title' => t('Your Library ID'),
-      '#size' => 10,
-      '#description' => check_plain(variable_get('millennium_auth_id_message', t(MILLENNIUM_AUTH_DEFAULT_ID_MSG))),
-      '#default_value' => '',
-      '#attributes' => array('tabindex' => 2),
-      '#required' => 1,
-      #'#weight' => -50,
-    );
+  
+  if ($form_id == 'user_login_block' || $form_id == 'user_login') {
+    if (variable_get('millennium_auth_login_fields', '2') == '3') {
+      $form['lastname'] = array(
+        '#type' => 'textfield',
+        '#title' => t('Your last name'),
+        '#size' => 15,
+        '#description' => t('Example: smith'),
+        '#default_value' => '',
+        '#weight' => -50,
+        '#attributes' => array('tabindex' => 1),
+        #'#required' => 1,
+      );
+      // Add tabindex to elements.
+      $form['name']['#attributes']['tabindex'] = 2;
+      $form['pass']['#attributes']['tabindex'] = 3;
+      $form['submit']['#attributes']['tabindex'] = 4;
+    }
+    else {
+      $form['lastname'] = array(
+        '#type' => 'hidden',
+        '#value' => '',
+      );
+    }
     
+    // Alter username element.
+    $form['name']['#title'] = t('Your Library ID');
+    $form['name']['#description'] = check_plain(variable_get('millennium_auth_id_message', t(MILLENNIUM_AUTH_DEFAULT_ID_MSG)));
+    
+    // Alter password element.
     $form['pass']['#title'] = t('Library PIN');
     $form['pass']['#size'] = 12;
     $form['pass']['#description'] = check_plain(variable_get('millennium_auth_pin_message', t(MILLENNIUM_AUTH_DEFAULT_PIN_MSG)));
-    $form['pass']['#attributes'] = array('tabindex' => 3);
-    
-    // Add tabindex to "submit" button
-    $form['submit']['#attributes'] = array('tabindex' => 4);
         
     // Add our own validation and submit function to the node_form
     $final_validator = array_pop($form['#validate']);
@@ -453,7 +474,6 @@
  * Validation function for user login form.
  */
 function millennium_auth_validate($form, &$form_state) {
-  global $millennium_auth_patronlastname;
   global $user;
   
   if (!empty($user->uid)) {
@@ -469,8 +489,8 @@
   // Check if user is not in database... then the lastname is required
   $uid_mil = db_result(db_query("SELECT uid FROM {millennium_auth_users} WHERE name='%s'", $form_state['values']["name"] ) );
   $uid_dru = db_result(db_query("SELECT uid FROM {users} WHERE name='%s'", $form_state['values']["name"] ) );
-  if (!$uid && !$uid_dru && trim($form_state['values']["lastname"])=="" ) {
-    form_set_error('name', t('The last name is required for all first-time users') );
+  if (!$uid && !$uid_dru && variable_get('millennium_auth_login_fields', '2') == '3' && trim($form_state['values']["lastname"])=="" ) {
+    form_set_error('lastname', t('The last name is required for all first-time users') );
   }
   
   if (!millennium_auth_authenticate($form_state['values'])) {
Index: contributions/modules/millennium/patroninfo.inc
--- contributions/modules/millennium/patroninfo.inc Base (1.1.2.6)
+++ contributions/modules/millennium/patroninfo.inc Locally Modified (Based On 1.1.2.6)
@@ -125,7 +125,7 @@
 		);
 	}
 
-	if (preg_match('/patActionsLinks/', $response)) {
+	if (preg_match('/patActionsLinks|patStatusLinks|patnavRow/', $response)) {
 		return array(
 			"success" => true,
 			"response" => $response
@@ -142,7 +142,7 @@
 /**
  * Begins an authenticated user session with Millennium through http.
  */
-function patroninfo_start_session($millennium_baseurl, $id, $lastname, $pin) {
+function patroninfo_start_session($millennium_baseurl, $name, $code, $pin) {
 global $PI_post_data;
 
 	// Generate a new sessionid (random number)
@@ -150,7 +150,7 @@
 	register_shutdown_function('patroninfo_end_session', $session_id);
 	
 	// Store name/code/pin for future callse
-	$post_data = "name={$lastname}&code={$id}&pin={$pin}";
+	$post_data = "name={$name}&code={$code}&pin={$pin}";
 	$PI_post_data[$session_id] = $post_data;
 	
 	// TODO: Make this configurable?
@@ -179,17 +179,17 @@
 	$patroninfo_data["session_id"] = $session_id;
 	#$patroninfo_data["postdata"] = $post_data;
 
-	$ok = preg_match("/<a href=\"\/patroninfo.*?\*[a-z]+\/([0-9]+)\/top\/\">/", $fetched_html, $matches);
+	$ok = preg_match("/<a href=\"\/patroninfo.*?\/([0-9]+)\/(top|holds|items|overdues)\/\">/", $fetched_html, $matches);
 	if (!$ok) {
 		return array(
 		  "success" => false, 
 		  "errormsg" => t("Could not get URL for patron information")
 		);
 	}
-	$id = $matches[1];
-	$url_checkouts = "$login_page/$id/items";
-	$url_holds = "$login_page/$id/holds";
-	$url_fines = "$login_page/$id/overdues";
+	$name = $matches[1];
+	$url_checkouts = "$login_page/$name/items";
+	$url_holds = "$login_page/$name/holds";
+	$url_fines = "$login_page/$name/overdues";
 	
 	// Scrape items screen
 	$r = patroninfo_request($session_id, $url_checkouts);
@@ -388,7 +388,7 @@
           [7] =>  LC5803.C65 G84 2008  
       )
 		*/
-		print_r($match);
+		#print_r($match);
 		$item["item_recnum"] = $match[1];
 		$item["title"] = trim(utf8_encode($match[2]));
 		$item["barcode"] = trim($match[3]);
@@ -509,6 +509,8 @@
 	function _cmp($a, $b) {
 		return ($a[duedate_timestamp]<$b[duedate_timestamp]) ? -1 : 1;
 	}
-	usort($items, "_cmp");
+  if (is_array($items)) {
+    usort($items, "_cmp");
+  }
 	return $items;
 }
