| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* $Id: $
|
| 4 |
* @author Bruno Massa
|
| 5 |
* @file address_user.inc
|
| 6 |
* You can associate a geographic address with users.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Manage the user Addresses: list them, create and delete operaions
|
| 11 |
*
|
| 12 |
* @param $op
|
| 13 |
* String (optional). The action that should be executed:
|
| 14 |
* - NULL (default) => list all user addresses
|
| 15 |
* - 'add' => Address form to add a new address
|
| 16 |
* - 'edit' => Address form to modify an existing address
|
| 17 |
* - 'delete' => It will promptly delete the address
|
| 18 |
* @param aid
|
| 19 |
* Number (optional). The Address ID
|
| 20 |
*/
|
| 21 |
function _address_user_address($op = NULL, $aid = NULL) {
|
| 22 |
include_once ADDRESS_PATH .'/address.inc';
|
| 23 |
|
| 24 |
global $user;
|
| 25 |
$uid = $user->uid;
|
| 26 |
|
| 27 |
// If the user has the permission to administer
|
| 28 |
// other users, he can see others address pages too
|
| 29 |
$access = user_access('administer users');
|
| 30 |
if (!empty($access)) {
|
| 31 |
$uid = arg(1);
|
| 32 |
}
|
| 33 |
|
| 34 |
switch ($op) {
|
| 35 |
case 'add':
|
| 36 |
case 'edit':
|
| 37 |
// Its time to include the address form!
|
| 38 |
|
| 39 |
// If its a existing address, get the whole
|
| 40 |
// address information
|
| 41 |
$edit = arg(4) ? _address_address_get(arg(4), 'address') : array();
|
| 42 |
$edit = $edit[0];
|
| 43 |
|
| 44 |
if (empty($edit)) {
|
| 45 |
// Set the Element ID as User ID form new addresses
|
| 46 |
$edit['eid'] = $uid;
|
| 47 |
}
|
| 48 |
elseif (empty($access) or $edit['eid'] != $uid) {
|
| 49 |
// If the user has no permission or the user from
|
| 50 |
// address is not the same as the user that are
|
| 51 |
// accessing, deny the access
|
| 52 |
return drupal_access_denied();
|
| 53 |
}
|
| 54 |
|
| 55 |
return drupal_get_form('_address_user_form', array(), $edit);
|
| 56 |
|
| 57 |
case 'delete':
|
| 58 |
/// @todo Generates a confirm page before delete
|
| 59 |
_address_address_delete(arg(4));
|
| 60 |
|
| 61 |
// Send the user to the address book
|
| 62 |
drupal_goto("user/$uid/address");
|
| 63 |
break;
|
| 64 |
|
| 65 |
default:
|
| 66 |
$addresses = _address_address_get($user->uid, 'user');
|
| 67 |
return theme('address_user_overview', $uid, $addresses);
|
| 68 |
}
|
| 69 |
}
|
| 70 |
|
| 71 |
/**
|
| 72 |
* Create a address form for users
|
| 73 |
*/
|
| 74 |
function _address_user_form(&$form_state, $fields = array(), $values = array()) {
|
| 75 |
$form = array();
|
| 76 |
|
| 77 |
// Get the address form builder
|
| 78 |
_address_form($form, 'user', $values);
|
| 79 |
|
| 80 |
// Add the submit button
|
| 81 |
if (empty($values['aid'])) {
|
| 82 |
$form['submit'] = array(
|
| 83 |
'#type' => 'submit',
|
| 84 |
'#value' => t('Create new address')
|
| 85 |
);
|
| 86 |
}
|
| 87 |
else {
|
| 88 |
$form['submit'] = array(
|
| 89 |
'#type' => 'submit',
|
| 90 |
'#value' => t('Update address')
|
| 91 |
);
|
| 92 |
}
|
| 93 |
|
| 94 |
return $form;
|
| 95 |
}
|
| 96 |
|
| 97 |
/**
|
| 98 |
* Submit the address
|
| 99 |
*/
|
| 100 |
function _address_user_form_submit($form, &$form_state) {
|
| 101 |
global $user;
|
| 102 |
_address_address_save($form_state['values']['address'][0], $user, 'user');
|
| 103 |
}
|
| 104 |
|
| 105 |
/**
|
| 106 |
* List all addresses from a given user
|
| 107 |
*
|
| 108 |
* @ingroup themeable
|
| 109 |
* @param $uid
|
| 110 |
* Number. The user ID
|
| 111 |
* @param &$addresses
|
| 112 |
* Array. A list of user addresses
|
| 113 |
*/
|
| 114 |
function theme_address_user_overview($uid, &$addresses) {
|
| 115 |
// Get the current page URL, to force the forms
|
| 116 |
// to return here.
|
| 117 |
$destination = drupal_get_destination();
|
| 118 |
|
| 119 |
// The option to add a new address is always visible
|
| 120 |
$output = "<p>". l(t('Click here to add a new address'),
|
| 121 |
"user/$uid/address/add", array('query' => $destination)) .'.</p>';
|
| 122 |
|
| 123 |
// Create a table showing the addresses
|
| 124 |
if (!empty($addresses)) {
|
| 125 |
|
| 126 |
// Add each address
|
| 127 |
foreach ($addresses as $address) {
|
| 128 |
$colunm = array();
|
| 129 |
|
| 130 |
// Display a single address
|
| 131 |
$colunm[] = theme('address', $address);
|
| 132 |
|
| 133 |
// More readable variable
|
| 134 |
$aid = $address['aid'];
|
| 135 |
|
| 136 |
// Add the Edit and Delete options
|
| 137 |
$colunm[] = l(t('edit'),
|
| 138 |
"user/$uid/address/edit/$aid", array('query' => $destination)) .' / '. l(t('delete'),
|
| 139 |
"user/$uid/address/delete/$aid");
|
| 140 |
|
| 141 |
// Each line has two colunms
|
| 142 |
$rows[] = $colunm;
|
| 143 |
}
|
| 144 |
|
| 145 |
// Create the addresses table
|
| 146 |
$header = array(t('address'), t('options'));
|
| 147 |
$output .= theme('table', $header, $rows);
|
| 148 |
}
|
| 149 |
|
| 150 |
// Show the resulting output
|
| 151 |
return $output;
|
| 152 |
}
|