You are here

function uc_addresses_list_addresses in Ubercart Addresses 5.2

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

Generate a list of one or all addresses defined by one user and then theme the list for display.

If the current user can edit the addresses, then provide an edit link for each address.

Parameters

$address_user The user whose address list we want to display.:

$aid The id of the address to display or null to display all.:

Return value

The themed list (as a string).

1 string reference to 'uc_addresses_list_addresses'
uc_addresses_menu in ./uc_addresses.module
Implementation of hook_menu().

File

./uc_addresses.module, line 641

Code

function uc_addresses_list_addresses($address_user, $aid = NULL) {
  global $user;

  // Save this for later use
  $saved_aid = $aid;

  // Determine if the user can view all addresses or just the default address
  $can_view_all_addresses = $user->uid == $address_user || user_access(UC_ADDRESSES_ACCESS_VIEW_ALL) || user_access(UC_ADDRESSES_ACCESS_ADD_EDIT);

  // Flag to determine error message
  $allowed_to_view_address = true;

  // If they can view all addresses, fetch the addresses
  if ($can_view_all_addresses) {
    $addresses = _uc_addresses_db_get_address($address_user, $aid);
  }
  else {
    if ($aid === NULL) {
      $aid = _uc_addresses_get_default_address_id($address_user);
      if ($aid === NULL) {
        $aid = 0;
      }

      // NULL would mean read all addresses
    }
    $addresses = _uc_addresses_db_get_address($address_user, $aid);
    if (is_object($addresses) && $addresses->is_default === false) {
      $addresses = FALSE;
      $allowed_to_view_address = FALSE;
    }
  }
  $output = '';

  // We have multiple addresses
  if (is_array($addresses)) {
    foreach ($addresses as $address) {
      $output .= _uc_addresses_list_one_address($address);
    }
  }
  elseif (is_object($addresses)) {
    $output .= _uc_addresses_list_one_address($addresses);
  }
  else {

    // If they asked for all addresses and got nothing, it's because
    // there is nothing
    if ($saved_aid === NULL) {
      $output .= t('No addresses have been saved.<br />');
    }
    elseif ($allowed_to_view_address) {
      $output .= t('This address does not exist.<br />');
    }
    else {
      $output .= t('You are not allowed to view this address.<br />');
    }
  }

  // Decide whether to include a link for adding a new address based
  // on whether the current user can edit the addresses
  if (user_access(UC_ADDRESSES_ACCESS_ADD_EDIT) || $user->uid == $address_user) {
    $link = l(t('Add a new address'), 'user/' . $address_user . '/addresses/add');
    $output .= $link;
  }
  return $output;
}