/**
  *
  */
 public function Save()
 {
     // Field to user profile preference mapping
     $va_mapping = array('billing_organization' => 'user_profile_organization', 'billing_address1' => 'user_profile_address1', 'billing_address2' => 'user_profile_address2', 'billing_city' => 'user_profile_city', 'billing_zone' => 'user_profile_state', 'billing_postal_code' => 'user_profile_postalcode', 'billing_country' => 'user_profile_country', 'billing_phone' => 'user_profile_phone', 'billing_fax' => 'user_profile_fax', 'shipping_organization' => 'user_profile_organization', 'shipping_address1' => 'user_profile_address1', 'shipping_address2' => 'user_profile_address2', 'shipping_city' => 'user_profile_city', 'shipping_zone' => 'user_profile_state', 'shipping_postal_code' => 'user_profile_postalcode', 'shipping_country' => 'user_profile_country', 'shipping_phone' => 'user_profile_phone', 'shipping_fax' => 'user_profile_fax');
     $va_errors = array();
     $va_fields = $this->opt_order->getFormFields();
     foreach ($va_fields as $vs_f => $va_field_info) {
         switch ($vs_f) {
             case 'transaction_id':
                 // noop
                 break;
             case 'order_type':
                 // noop
                 break;
             default:
                 if (isset($_REQUEST[$vs_f])) {
                     if (!$this->opt_order->set($vs_f, $this->request->getParameter($vs_f, pString))) {
                         $va_errors[$vs_f] = $this->opt_order->errors();
                     }
                 }
                 break;
         }
     }
     // Set additional fees for order
     $va_fees = $this->opo_client_services_config->getAssoc('additional_loan_fees');
     if (is_array($va_fees)) {
         if (!is_array($va_fee_values = $this->opt_order->get('additional_fees'))) {
             $va_fee_values = array();
         }
         foreach ($va_fees as $vs_code => $va_info) {
             $va_fee_values[$vs_code] = (double) $this->request->getParameter("additional_fee_{$vs_code}", pString);
         }
         $this->opt_order->set('additional_fees', $va_fee_values);
     }
     $this->opt_order->setMode(ACCESS_WRITE);
     if ($this->opt_order->getPrimaryKey()) {
         $this->opt_order->set('order_type', 'L');
         // L=loan
         $this->opt_order->update();
         $vn_transaction_id = $this->opt_order->get('transaction_id');
     } else {
         // Set transaction
         if (!($vn_transaction_id = $this->request->getParameter('transaction_id', pInteger))) {
             if ($vn_user_id = $this->request->getParameter('transaction_user_id', pInteger)) {
                 // try to create transaction
                 $t_trans = new ca_commerce_transactions();
                 $t_trans->setMode(ACCESS_WRITE);
                 $t_trans->set('user_id', $vn_user_id);
                 $t_trans->set('short_description', "Created on " . date("c"));
                 $t_trans->set('set_id', null);
                 $t_trans->insert();
                 if ($t_trans->numErrors()) {
                     $this->notification->addNotification(_t('Errors occurred when creating commerce transaction: %1', join('; ', $t_trans->getErrors())), __NOTIFICATION_TYPE_ERROR__);
                 } else {
                     $vn_transaction_id = $t_trans->getPrimaryKey();
                 }
             }
         }
         $this->opt_order->set('transaction_id', $vn_transaction_id);
         $this->opt_order->set('order_type', 'L');
         // L=loan
         $this->opt_order->insert();
         $this->request->setParameter('order_id', $this->opt_order->getPrimaryKey());
     }
     // set user profile if not already set
     $t_trans = new ca_commerce_transactions($vn_transaction_id);
     $t_user = new ca_users($t_trans->get('user_id'));
     $t_user->setMode(ACCESS_WRITE);
     foreach ($va_mapping as $vs_field => $vs_pref) {
         if (!strlen($t_user->getPreference($vs_pref))) {
             $t_user->setPreference($vs_pref, $this->opt_order->get($vs_field));
         }
     }
     $t_user->update();
     if (!$this->opt_order->numErrors()) {
         $this->notification->addNotification(_t('Saved changes'), __NOTIFICATION_TYPE_INFO__);
     } else {
         $va_errors['general'] = $this->opt_order->errors();
         $this->notification->addNotification(_t('Errors occurred: %1', join('; ', $this->opt_order->getErrors())), __NOTIFICATION_TYPE_ERROR__);
     }
     $this->view->setVar('errors', $va_errors);
 }
 function register()
 {
     if ($this->request->config->get('dont_allow_registration_and_login')) {
         $this->notification->addNotification(_t("Registration is not enabled"), __NOTIFICATION_TYPE_ERROR__);
         $this->redirect(caNavUrl($this->request, '', 'Front', 'Index'));
         return;
     }
     MetaTagManager::setWindowTitle($this->request->config->get("app_display_name") . ": " . _t("Register"));
     # logout user in case is already logged in
     $this->request->deauthenticate();
     $t_user = new ca_users();
     $t_user->purify(true);
     # --- process incoming registration attempt
     $ps_email = $this->request->getParameter("email", pString);
     $ps_fname = $this->request->getParameter("fname", pString);
     $ps_lname = $this->request->getParameter("lname", pString);
     $ps_password = $this->request->getParameter("password", pString);
     $ps_password2 = $this->request->getParameter("password2", pString);
     $ps_security = $this->request->getParameter("security", pString);
     $va_errors = array();
     if (!caCheckEmailAddress($ps_email)) {
         $va_errors["email"] = _t("E-mail address is not valid.");
     } else {
         $t_user->set("email", $ps_email);
     }
     if (!$ps_fname) {
         $va_errors["fname"] = _t("Please enter your first name");
     } else {
         $t_user->set("fname", $ps_fname);
     }
     if (!$ps_lname) {
         $va_errors["lname"] = _t("Please enter your last name");
     } else {
         $t_user->set("lname", $ps_lname);
     }
     if (!$ps_password || !$ps_password2) {
         $va_errors["password"] = _t("Please enter and re-type your password.");
     } else {
         if ($ps_password != $ps_password2) {
             $va_errors["password"] = _t("Passwords do not match");
         } else {
             $t_user->set("password", $ps_password);
         }
     }
     if (!$ps_security) {
         $va_errors["security"] = _t("Please answer the security question.");
     } else {
         if ($ps_security != $_REQUEST["sum"]) {
             $va_errors["security"] = _t("Your answer was incorrect, please try again");
         }
     }
     // Check user profile responses
     $va_profile_prefs = $t_user->getValidPreferences('profile');
     if (is_array($va_profile_prefs) && sizeof($va_profile_prefs)) {
         foreach ($va_profile_prefs as $vs_pref) {
             $vs_pref_value = $this->request->getParameter('pref_' . $vs_pref, pString);
             if (!$t_user->isValidPreferenceValue($vs_pref, $vs_pref_value)) {
                 $va_errors[$vs_pref] = join("; ", $t_user->getErrors());
                 $t_user->clearErrors();
             }
         }
     }
     # --- does deleted user login record for this user already exist?
     # --- (look for active records only; inactive records will effectively block reregistration)
     $vb_user_exists_but_is_deleted = false;
     if ($t_user->load(array('user_name' => $ps_email))) {
         if ((int) $t_user->get('userclass') == 255) {
             if ($t_user->get('active') == 1) {
                 // yeah... so allow registration
                 $vb_user_exists_but_is_deleted = true;
             } else {
                 // existing inactive user record blocks registration
                 $va_errors["email"] = _t("User cannot register");
             }
         } else {
             // already valid login with this user name
             $va_errors["email"] = _t("A user has already registered with this email address");
         }
     }
     # get names of form fields
     $va_fields = $t_user->getFormFields();
     # loop through fields
     foreach ($va_fields as $vs_f => $va_attr) {
         switch ($vs_f) {
             case "user_name":
                 if (!$vb_user_exists_but_is_deleted && !sizeof($va_errors)) {
                     # set field value
                     $t_user->set("user_name", $ps_email);
                     if ($t_user->numErrors() > 0) {
                         $va_errors[$vs_f] = join("; ", $t_user->getErrors());
                     }
                 }
                 break;
                 # -------------
             # -------------
             case "active":
                 if ($this->request->config->get('dont_approve_logins_on_registration')) {
                     $t_user->set("active", 0);
                 } else {
                     $t_user->set("active", 1);
                 }
                 break;
                 # -------------
             # -------------
             case "userclass":
                 $t_user->set("userclass", 1);
                 // 1=public-only
                 break;
                 # -------------
             # -------------
             default:
                 if (!$va_errors[$vs_f]) {
                     $t_user->set($vs_f, $_REQUEST[$vs_f]);
                     # set field values
                     if ($t_user->numErrors() > 0) {
                         $va_errors[$vs_f] = join("; ", $t_user->getErrors());
                     }
                 }
                 break;
                 # -------------
         }
     }
     // Save user profile responses
     if (is_array($va_profile_prefs) && sizeof($va_profile_prefs)) {
         foreach ($va_profile_prefs as $vs_pref) {
             $t_user->setPreference($vs_pref, $this->request->getParameter('pref_' . $vs_pref, pString));
         }
     }
     if (sizeof($va_errors) == 0) {
         # --- there are no errors so make new user record
         $t_user->setMode(ACCESS_WRITE);
         if ($vb_user_exists_but_is_deleted) {
             $t_user->update();
         } else {
             $t_user->insert();
         }
         $pn_user_id = $t_user->get("user_id");
         if ($t_user->numErrors()) {
             $va_errors["register"] = join("; ", $t_user->getErrors());
         } else {
             # --- add default roles
             if (($va_default_roles = $this->request->config->getList('registration_default_roles')) && sizeof($va_default_roles)) {
                 $t_user->addRoles($va_default_roles);
             }
             # --- user is joining a user group from a supplied link
             if ($this->request->session->getVar("join_user_group_id")) {
                 if (!$t_user->inGroup($this->request->session->getVar("join_user_group_id"))) {
                     $t_user->addToGroups($this->request->session->getVar("join_user_group_id"));
                     $this->request->session->setVar("join_user_group_id", "");
                     $vs_group_message = _t(" You were added to the group");
                 } else {
                     $this->request->session->setVar("join_user_group_id", "");
                     $vs_group_message = _t(" You are already a member of the group");
                 }
             }
             # --- send email confirmation
             $o_view = new View($this->request, array($this->request->getViewsDirectoryPath()));
             # -- generate email subject line from template
             $vs_subject_line = $o_view->render("mailTemplates/reg_conf_subject.tpl");
             # -- generate mail text from template - get both the text and the html versions
             $vs_mail_message_text = $o_view->render("mailTemplates/reg_conf.tpl");
             $vs_mail_message_html = $o_view->render("mailTemplates/reg_conf_html.tpl");
             caSendmail($t_user->get('email'), $this->request->config->get("ca_admin_email"), $vs_subject_line, $vs_mail_message_text, $vs_mail_message_html);
             if ($this->request->config->get("email_notification_for_new_registrations")) {
                 # --- send email to admin
                 $o_view = new View($this->request, array($this->request->getViewsDirectoryPath()));
                 $o_view->setVar("t_user", $t_user);
                 # -- generate email subject line from template
                 $vs_subject_line = $o_view->render("mailTemplates/reg_admin_notification_subject.tpl");
                 # -- generate mail text from template - get both the text and the html versions
                 $vs_mail_message_text = $o_view->render("mailTemplates/reg_admin_notification.tpl");
                 $vs_mail_message_html = $o_view->render("mailTemplates/reg_admin_notification_html.tpl");
                 caSendmail($this->request->config->get("ca_admin_email"), $this->request->config->get("ca_admin_email"), $vs_subject_line, $vs_mail_message_text, $vs_mail_message_html);
             }
             $t_user = new ca_users();
             $vs_action = $vs_controller = $vs_module_path = '';
             if ($vs_default_action = $this->request->config->get('default_action')) {
                 $va_tmp = explode('/', $vs_default_action);
                 $vs_action = array_pop($va_tmp);
                 if (sizeof($va_tmp)) {
                     $vs_controller = array_pop($va_tmp);
                 }
                 if (sizeof($va_tmp)) {
                     $vs_module_path = join('/', $va_tmp);
                 }
             } else {
                 $vs_controller = 'Splash';
                 $vs_action = 'Index';
             }
             $vs_url = caNavUrl($this->request, $vs_module_path, $vs_controller, $vs_action);
             if ($t_user->get("active")) {
                 # log in the new user
                 $this->request->doAuthentication(array('dont_redirect' => true, 'user_name' => $ps_email, 'password' => $ps_password));
                 if ($this->request->isLoggedIn()) {
                     if ($this->request->isAjax()) {
                         $this->view->setVar("message", _t('Thank you for registering!  You are now logged in.') . $vs_group_message);
                         $this->render("Form/reload_html.php");
                         return;
                     } else {
                         $this->notification->addNotification(_t('Thank you for registering!  You are now logged in.') . $vs_group_message, __NOTIFICATION_TYPE_INFO__);
                         $this->response->setRedirect($vs_url);
                     }
                 } else {
                     $va_errors["register"] = _t("Login failed.");
                 }
             } else {
                 # --- registration needs approval
                 $this->notification->addNotification(_t('Thank you for registering!  Your account will be activated after review.') . $vs_group_message, __NOTIFICATION_TYPE_INFO__);
                 $this->response->setRedirect($vs_url);
             }
         }
     }
     if (sizeof($va_errors) > 0) {
         $this->view->setVar('errors', $va_errors);
         $this->registerForm($t_user);
     }
 }
 public function Save()
 {
     // Field to user profile preference mapping
     $va_mapping = array('billing_organization' => 'user_profile_organization', 'billing_address1' => 'user_profile_address1', 'billing_address2' => 'user_profile_address2', 'billing_city' => 'user_profile_city', 'billing_zone' => 'user_profile_state', 'billing_postal_code' => 'user_profile_postalcode', 'billing_country' => 'user_profile_country', 'billing_phone' => 'user_profile_phone', 'billing_fax' => 'user_profile_fax', 'shipping_organization' => 'user_profile_organization', 'shipping_address1' => 'user_profile_address1', 'shipping_address2' => 'user_profile_address2', 'shipping_city' => 'user_profile_city', 'shipping_zone' => 'user_profile_state', 'shipping_postal_code' => 'user_profile_postalcode', 'shipping_country' => 'user_profile_country', 'shipping_phone' => 'user_profile_phone', 'shipping_fax' => 'user_profile_fax');
     $va_errors = array();
     $va_failed_insert_list = array();
     $va_fields = $this->opt_order->getFormFields();
     foreach ($va_fields as $vs_f => $va_field_info) {
         switch ($vs_f) {
             case 'transaction_id':
                 // noop
                 break;
             default:
                 if (isset($_REQUEST[$vs_f])) {
                     if (!$this->opt_order->set($vs_f, $this->request->getParameter($vs_f, pString))) {
                         $va_errors[$vs_f] = $this->opt_order->errors();
                     }
                 }
                 break;
         }
     }
     // Set additional fees for order
     $va_fees = $this->opo_client_services_config->getAssoc('additional_order_fees');
     if (is_array($va_fees)) {
         if (!is_array($va_fee_values = $this->opt_order->get('additional_fees'))) {
             $va_fee_values = array();
         }
         foreach ($va_fees as $vs_code => $va_info) {
             $va_fee_values[$vs_code] = (double) $this->request->getParameter("additional_fee_{$vs_code}", pString);
         }
         $this->opt_order->set('additional_fees', $va_fee_values);
     }
     $this->opt_order->setMode(ACCESS_WRITE);
     if ($this->opt_order->getPrimaryKey()) {
         $this->opt_order->update();
         $vn_transaction_id = $this->opt_order->get('transaction_id');
     } else {
         // Set transaction
         if (!($vn_transaction_id = $this->request->getParameter('transaction_id', pInteger))) {
             if (!($vn_user_id = $this->request->getParameter('transaction_user_id', pInteger))) {
                 if ($vs_user_name = $this->request->getParameter('billing_email', pString)) {
                     // Try to create user in-line
                     $t_user = new ca_users();
                     if ($t_user->load(array('user_name' => $vs_user_name))) {
                         if ($t_user->get('active') == 1) {
                             // user is active - if not active don't use
                             if ($t_user->get('userclass') == 255) {
                                 // user is deleted
                                 $t_user->setMode(ACCESS_WRITE);
                                 $t_user->set('userclass', 1);
                                 // 1=public user (no back-end login)
                                 $t_user->update();
                                 if ($t_user->numErrors()) {
                                     $this->notification->addNotification(_t('Errors occurred when undeleting user: %1', join('; ', $t_user->getErrors())), __NOTIFICATION_TYPE_ERROR__);
                                 } else {
                                     $vn_user_id = $t_user->getPrimaryKey();
                                 }
                             } else {
                                 $vn_user_id = $t_user->getPrimaryKey();
                             }
                         } else {
                             $t_user->setMode(ACCESS_WRITE);
                             $t_user->set('active', 1);
                             $t_user->set('userclass', 1);
                             // 1=public user (no back-end login)
                             $t_user->update();
                             if ($t_user->numErrors()) {
                                 $this->notification->addNotification(_t('Errors occurred when reactivating user: %1', join('; ', $t_user->getErrors())), __NOTIFICATION_TYPE_ERROR__);
                             } else {
                                 $vn_user_id = $t_user->getPrimaryKey();
                             }
                         }
                     } else {
                         $t_user->setMode(ACCESS_WRITE);
                         $t_user->set('user_name', $vs_user_name);
                         $t_user->set('password', $vs_password = substr(md5(uniqid(microtime())), 0, 6));
                         $t_user->set('userclass', 1);
                         // 1=public user (no back-end login)
                         $t_user->set('fname', $vs_fname = $this->request->getParameter('billing_fname', pString));
                         $t_user->set('lname', $vs_lname = $this->request->getParameter('billing_lname', pString));
                         $t_user->set('email', $vs_user_name);
                         $t_user->insert();
                         if ($t_user->numErrors()) {
                             $this->notification->addNotification(_t('Errors occurred when creating new user: %1', join('; ', $t_user->getErrors())), __NOTIFICATION_TYPE_ERROR__);
                         } else {
                             $vn_user_id = $t_user->getPrimaryKey();
                             $this->notification->addNotification(_t('Created new client login for <em>%1</em>. Login name is <em>%2</em> and password is <em>%3</em>', $vs_fname . ' ' . $vs_lname, $vs_user_name, $vs_password), __NOTIFICATION_TYPE_INFO__);
                             // Create related entity?
                         }
                     }
                 }
             }
             if ($vn_user_id) {
                 // try to create transaction
                 $t_trans = new ca_commerce_transactions();
                 $t_trans->setMode(ACCESS_WRITE);
                 $t_trans->set('user_id', $vn_user_id);
                 $t_trans->set('short_description', "Created on " . date("c"));
                 $t_trans->set('set_id', null);
                 $t_trans->insert();
                 if ($t_trans->numErrors()) {
                     $this->notification->addNotification(_t('Errors occurred when creating commerce transaction: %1', join('; ', $t_trans->getErrors())), __NOTIFICATION_TYPE_ERROR__);
                 } else {
                     $vn_transaction_id = $t_trans->getPrimaryKey();
                 }
             } else {
                 $this->notification->addNotification(_t('You must specify a client'), __NOTIFICATION_TYPE_ERROR__);
                 $va_errors['general'][] = new Error(1100, _t('You must specify a client'), 'CheckOutController->Save()', false, false, false);
             }
         }
         $this->opt_order->set('transaction_id', $vn_transaction_id);
         if ($vn_transaction_id) {
             $this->opt_order->set('order_type', 'L');
             // L = loan (as opposed to 'O' for sales orders)
             $this->opt_order->set('order_status', 'OPEN');
             $this->opt_order->insert();
             $this->request->setParameter('order_id', $x = $this->opt_order->getPrimaryKey());
         }
     }
     if ($vn_transaction_id) {
         // set user profile if not already set
         $t_trans = new ca_commerce_transactions($vn_transaction_id);
         $t_user = new ca_users($t_trans->get('user_id'));
         $t_user->setMode(ACCESS_WRITE);
         foreach ($va_mapping as $vs_field => $vs_pref) {
             if (!strlen($t_user->getPreference($vs_pref))) {
                 $t_user->setPreference($vs_pref, $this->opt_order->get($vs_field));
             }
         }
         $t_user->update();
         $va_additional_fee_codes = $this->opo_client_services_config->getAssoc('additional_loan_fees');
         // Look for newly added items
         $vn_items_added = 0;
         $vn_item_errors = 0;
         $vs_errors = '';
         foreach ($_REQUEST as $vs_k => $vs_v) {
             if (preg_match("!^item_list_idnew_([\\d]+)\$!", $vs_k, $va_matches)) {
                 if ($vn_object_id = (int) $vs_v) {
                     // add item to order
                     $va_values = array();
                     foreach ($_REQUEST as $vs_f => $vs_value) {
                         if (preg_match("!^item_list_([A-Za-z0-9_]+)_new_" . $va_matches[1] . "\$!", $vs_f, $va_matches2)) {
                             $va_values[$va_matches2[1]] = $vs_value;
                         }
                     }
                     // Set additional fees
                     //
                     $va_fee_values = array();
                     foreach ($va_additional_fee_codes as $vs_code => $va_info) {
                         $va_fee_values[$vs_code] = $_REQUEST['additional_order_item_fee_' . $vs_code . '_new_' . $va_matches[1]];
                     }
                     $t_item = $this->opt_order->addItem($vn_object_id, $va_values, array('additional_fees' => $va_fee_values));
                     if ($t_item && $t_item->getPrimaryKey()) {
                         $vn_items_added++;
                     } else {
                         if ($this->opt_order->numErrors()) {
                             $t_object = new ca_objects($vn_object_id);
                             $this->notification->addNotification(_t('Could not check-out item <em>%1</em> (%2) due to errors: %3', $t_object->get('ca_objects.preferred_labels.name'), $t_object->get('idno'), join("; ", $this->opt_order->getErrors())), __NOTIFICATION_TYPE_ERROR__);
                             $vn_item_errors++;
                             $va_fee_values_proc = array();
                             foreach ($va_fee_values as $vs_k => $vs_v) {
                                 $va_fee_values_proc['ADDITIONAL_FEE_' . $vs_k] = $vs_v;
                             }
                             $va_failed_insert_list[] = array_merge($va_values, $va_fee_values_proc, array('autocomplete' => $_REQUEST['item_list_autocompletenew_' . $va_matches[1]], 'id' => $vn_object_id));
                         }
                     }
                 }
             }
         }
         if (!$this->opt_order->numErrors() && $vn_items_added) {
             $this->notification->addNotification(_t('Checked out %1 %2 for %3 (order %4)', $vn_items_added, $vn_items_added == 1 ? _t('item') : _t('items'), $t_user->get('fname') . ' ' . $t_user->get('lname'), $this->opt_order->getOrderNumber()), __NOTIFICATION_TYPE_INFO__);
             $this->opt_order->set('order_status', 'PROCESSED');
             $this->opt_order->update();
             $this->opt_order = new ca_commerce_orders();
             $this->request->setParameter('order_id', null);
             $this->view->setVar('t_order', $this->opt_order);
             $this->view->setVar('order_id', $this->opt_order->getPrimaryKey());
             $this->view->setVar('t_item', $this->opt_order);
         } else {
             if ($vn_items_added == 0 && $this->opt_order->numErrors() == 0) {
                 $vs_errors = _t('No items were specified');
             } else {
                 if ($vn_item_errors == 0) {
                     $vs_errors = join('; ', $this->opt_order->getErrors());
                 }
             }
             if ($vs_errors) {
                 $va_errors['general'] = $this->opt_order->errors();
                 $this->notification->addNotification(_t('Errors occurred: %1', $vs_errors), __NOTIFICATION_TYPE_ERROR__);
             }
         }
     }
     $this->view->setVar('errors', $va_errors);
     $this->view->setVar('failed_insert_list', $va_failed_insert_list);
     $this->Index();
 }
<?php

require '../../../setup.php';
require_once __CA_LIB_DIR__ . "/core/Db.php";
require_once __CA_MODELS_DIR__ . "/ca_users.php";
$o_db = new Db();
$q_users = $o_db->query("select user_id from ca_users");
$t_user = new ca_users();
while ($q_users->nextRow()) {
    $t_user->load($q_users->get("user_id"));
    $t_user->setMode(ACCESS_WRITE);
    $t_user->setPreference("user_profile_field_of_research", $t_user->getVar("field_of_research"));
    $t_user->update();
}
 function register()
 {
     # logout user in case is already logged in
     $this->request->deauthenticate();
     $t_user = new ca_users();
     # --- process incoming registration attempt
     $ps_email = strip_tags($this->request->getParameter("email", pString));
     $ps_fname = strip_tags($this->request->getParameter("fname", pString));
     $ps_lname = strip_tags($this->request->getParameter("lname", pString));
     $ps_password = $this->request->getParameter("password", pString);
     $ps_password2 = $this->request->getParameter("password2", pString);
     $ps_security = $this->request->getParameter("security", pString);
     $va_errors = array();
     if (!caCheckEmailAddress($ps_email)) {
         $va_errors["email"] = _t("E-mail address is not valid.");
     } else {
         $t_user->set("email", $ps_email);
     }
     if (!$ps_fname) {
         $va_errors["fname"] = _t("Please enter your first name");
     } else {
         $t_user->set("fname", $ps_fname);
     }
     if (!$ps_lname) {
         $va_errors["lname"] = _t("Please enter your last name");
     } else {
         $t_user->set("lname", $ps_lname);
     }
     if (!$ps_password || !$ps_password2) {
         $va_errors["password"] = _t("Please enter and re-type your password.");
     } else {
         if ($ps_password != $ps_password2) {
             $va_errors["password"] = _t("Passwords do not match");
         } else {
             $t_user->set("password", $ps_password);
         }
     }
     if (!$ps_security) {
         $va_errors["security"] = _t("Please answer the security question.");
     } else {
         if ($ps_security != $_REQUEST["sum"]) {
             $va_errors["security"] = _t("Your answer was incorrect, please try again");
         }
     }
     // Check user profile responses
     $va_profile_prefs = $t_user->getValidPreferences('profile');
     if (is_array($va_profile_prefs) && sizeof($va_profile_prefs)) {
         foreach ($va_profile_prefs as $vs_pref) {
             $vs_pref_value = $this->request->getParameter('pref_' . $vs_pref, pString);
             if (!$t_user->isValidPreferenceValue($vs_pref, $vs_pref_value)) {
                 $va_errors[$vs_pref] = join("; ", $t_user->getErrors());
                 $t_user->clearErrors();
             }
         }
     }
     # --- does deleted user login record for this user already exist?
     # --- (look for active records only; inactive records will effectively block reregistration)
     $vb_user_exists_but_is_deleted = false;
     if ($t_user->load(array('user_name' => $ps_email))) {
         if ((int) $t_user->get('userclass') == 255) {
             if ($t_user->get('active') == 1) {
                 // yeah... so allow registration
                 $vb_user_exists_but_is_deleted = true;
             } else {
                 // existing inactive user record blocks registration
                 $va_errors["email"] = _t("User cannot register");
             }
         } else {
             // already valid login with this user name
             $va_errors["email"] = _t("A user has already registered with this email address");
         }
     }
     # get names of form fields
     $va_fields = $t_user->getFormFields();
     # loop through fields
     foreach ($va_fields as $vs_f => $va_attr) {
         switch ($vs_f) {
             case "user_name":
                 if (!$vb_user_exists_but_is_deleted && !sizeof($va_errors)) {
                     # set field value
                     $t_user->set("user_name", $ps_email);
                     if ($t_user->numErrors() > 0) {
                         $va_errors[$vs_f] = join("; ", $t_user->getErrors());
                     }
                 }
                 break;
                 # -------------
             # -------------
             case "active":
                 $t_user->set("active", 1);
                 break;
                 # -------------
             # -------------
             case "userclass":
                 $t_user->set("userclass", 1);
                 // 1=public-only
                 break;
                 # -------------
             # -------------
             default:
                 if (!$va_errors[$vs_f]) {
                     $t_user->set($vs_f, $_REQUEST[$vs_f]);
                     # set field values
                     if ($t_user->numErrors() > 0) {
                         $va_errors[$vs_f] = join("; ", $t_user->getErrors());
                     }
                 }
                 break;
                 # -------------
         }
     }
     // Save user profile responses
     if (is_array($va_profile_prefs) && sizeof($va_profile_prefs)) {
         foreach ($va_profile_prefs as $vs_pref) {
             $t_user->setPreference($vs_pref, $this->request->getParameter('pref_' . $vs_pref, pString));
         }
     }
     if (sizeof($va_errors) == 0) {
         # --- there are no errors so make new user record
         $t_user->setMode(ACCESS_WRITE);
         if ($vb_user_exists_but_is_deleted) {
             $t_user->update();
         } else {
             $t_user->insert();
         }
         $pn_user_id = $t_user->get("user_id");
         if ($t_user->numErrors()) {
             $va_errors["register"] = join("; ", $t_user->getErrors());
         } else {
             # --- add default roles
             if (($va_default_roles = $this->request->config->getList('registration_default_roles')) && sizeof($va_default_roles)) {
                 $t_user->addRoles($va_default_roles);
             }
             # --- send email confirmation
             # -- generate email subject line from template
             ob_start();
             require $this->request->getViewsDirectoryPath() . "/mailTemplates/reg_conf_subject.tpl";
             $vs_subject_line = ob_get_contents();
             ob_end_clean();
             # -- generate mail text from template - get both the text and the html versions
             ob_start();
             require $this->request->getViewsDirectoryPath() . "/mailTemplates/reg_conf.tpl";
             $vs_mail_message_text = ob_get_contents();
             ob_end_clean();
             ob_start();
             require $this->request->getViewsDirectoryPath() . "/mailTemplates/reg_conf_html.tpl";
             $vs_mail_message_html = ob_get_contents();
             ob_end_clean();
             caSendmail($t_user->get('email'), $this->request->config->get("ca_admin_email"), $vs_subject_line, $vs_mail_message_text, $vs_mail_message_html);
             $t_user = new ca_users();
             # log in the new user
             $this->request->doAuthentication(array('dont_redirect' => true, 'user_name' => $ps_email, 'password' => $ps_password));
             if ($this->request->isLoggedIn()) {
                 # --- login successful so redirect to search page
                 $this->notification->addNotification(_t('Thank you for registering!  You are now logged in.'), __NOTIFICATION_TYPE_INFO__);
                 $vo_session = $this->request->getSession();
                 $vs_last_page = $vo_session->getVar('site_last_page');
                 $vo_session->setVar('site_last_page', "");
                 switch ($vs_last_page) {
                     case "Sets":
                         $this->response->setRedirect(caNavUrl($this->request, "", "Sets", "addItem", array("object_id" => $vo_session->getVar('site_last_page_object_id'))));
                         break;
                         # --------------------
                     # --------------------
                     case "ObjectDetail":
                         $this->response->setRedirect(caNavUrl($this->request, "Detail", "Object", "Show", array("object_id" => $vo_session->getVar('site_last_page_object_id'))));
                         break;
                         # --------------------
                     # --------------------
                     default:
                         $this->response->setRedirect(caNavUrl($this->request, "", "", ""));
                         break;
                         # --------------------
                 }
             } else {
                 $va_errors["register"] = _t("Login failed.");
             }
         }
     } else {
         $this->view->setVar('reg_errors', $va_errors);
     }
     $this->form($t_user);
 }