You are here

function uc_addresses_user in Ubercart Addresses 6.2

Same name and namespace in other branches
  1. 5.2 uc_addresses.module \uc_addresses_user()
  2. 5 uc_addresses.module \uc_addresses_user()
  3. 6 uc_addresses.module \uc_addresses_user()

Implementation of hook_user().

Parameters

string $op: The action being performed.

array $edit: Form values submitted by the user.

object $account: The user on which the operation is being performed.

string $category: The active category of user information being edited.

Return value

A form array in case the operation is 'register'. void otherwise.

File

./uc_addresses.module, line 289
Adds user profile address support to Ubercart.

Code

function uc_addresses_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case 'register':

      // Check if we need to ask for an address upon registering.
      if (user_access('administer users')) {

        // User is admin.
        $require_address = variable_get('uc_addresses_require_address_admin', TRUE);
      }
      else {
        $require_address = variable_get('uc_addresses_require_address', TRUE);
      }
      if ($require_address) {
        $address = UcAddressesAddressBook::newAddress();
        $form['uc_addresses'] = array(
          '#type' => 'fieldset',
          '#title' => t('Address'),
        );
        $form['uc_addresses']['address'] = array(
          '#type' => 'uc_addresses_address',
          '#uc_addresses_address' => $address,
          '#uc_addresses_context' => 'register',
        );
        $form['uc_addresses']['uc_addresses_address'] = array(
          '#type' => 'value',
          '#value' => $address,
        );
        return $form;
      }
      break;
    case 'insert':

      // Save the address the user entered during registering.
      if (isset($edit['uc_addresses_address']) && $edit['uc_addresses_address'] instanceof UcAddressesAddress) {
        $address = $edit['uc_addresses_address'];
        $address
          ->setOwner($account->uid);

        // Mark this address as both the default shipping and billing address.
        $address
          ->setAsDefault('shipping');
        $address
          ->setAsDefault('billing');
        $address
          ->save();

        // Unset address from $edit to prevent it from being saved as user data in the user table.
        unset($edit['address']);
        unset($edit['uc_addresses_address']);
      }
      return;
    case 'delete':

      // We're deleting the user, so delete all his/her addresses as well.
      db_query("DELETE FROM {uc_addresses} WHERE uid = %d", $account->uid);
      return;
  }
}