| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* fee administration menu items.
|
| 7 |
*
|
| 8 |
*/
|
| 9 |
|
| 10 |
// Builds a paged list and overview of existing product fees.
|
| 11 |
function uc_fee_admin_form() {
|
| 12 |
|
| 13 |
$header = _uc_fee_admin_header();
|
| 14 |
$form = array();
|
| 15 |
|
| 16 |
$result = pager_query("SELECT f.fid, f.name, f.price, f.ordering FROM {uc_fees} AS f GROUP BY f.fid, f.name, f.ordering, f.price". tablesort_sql($header), 30, 0, "SELECT COUNT(fid) FROM {uc_fees}");
|
| 17 |
while ($attr = db_fetch_object($result)) {
|
| 18 |
$form['fees'][$attr->fid] = array(
|
| 19 |
'name' => array(
|
| 20 |
'#value' => $attr->name,
|
| 21 |
),
|
| 22 |
'ordering' => array(
|
| 23 |
'#type' => 'weight',
|
| 24 |
'#default_value' => $attr->ordering,
|
| 25 |
'#fees' => array('class' => 'uc-fee-table-weight'),
|
| 26 |
),
|
| 27 |
'price' => array(
|
| 28 |
'#value' => $attr->price,
|
| 29 |
),
|
| 30 |
'ops' => array(
|
| 31 |
'#value' => l(t('edit'), 'admin/store/fees/'. $attr->fid .'/edit') .' '.
|
| 32 |
l(t('delete'), 'admin/store/fees/'. $attr->fid .'/delete') .' ',
|
| 33 |
),
|
| 34 |
);
|
| 35 |
}
|
| 36 |
|
| 37 |
if (!empty($form['fees'])) {
|
| 38 |
$form['fees']['#tree'] = TRUE;
|
| 39 |
|
| 40 |
$form['submit'] = array(
|
| 41 |
'#type' => 'submit',
|
| 42 |
'#value' => t('Save changes'),
|
| 43 |
'#weight' => 10,
|
| 44 |
);
|
| 45 |
}
|
| 46 |
|
| 47 |
return $form;
|
| 48 |
}
|
| 49 |
|
| 50 |
// Displays a paged list and overview of existing product fees.
|
| 51 |
function theme_uc_fee_admin_form($form) {
|
| 52 |
$header = _uc_fee_admin_header();
|
| 53 |
|
| 54 |
if (count(element_children($form['fees'])) > 0) {
|
| 55 |
foreach (element_children($form['fees']) as $fid) {
|
| 56 |
$row = array(
|
| 57 |
drupal_render($form['fees'][$fid]['name']),
|
| 58 |
drupal_render($form['fees'][$fid]['ordering']),
|
| 59 |
drupal_render($form['fees'][$fid]['price']),
|
| 60 |
drupal_render($form['fees'][$fid]['ops']),
|
| 61 |
);
|
| 62 |
|
| 63 |
$rows[] = array(
|
| 64 |
'data' => $row,
|
| 65 |
'class' => 'draggable',
|
| 66 |
);
|
| 67 |
}
|
| 68 |
}
|
| 69 |
else {
|
| 70 |
$rows[] = array(
|
| 71 |
array('data' => t('No fees have been added yet.'), 'colspan' => 6),
|
| 72 |
);
|
| 73 |
}
|
| 74 |
|
| 75 |
drupal_add_tabledrag('uc-fee-table', 'order', 'sibling', 'uc-fee-table-weight');
|
| 76 |
|
| 77 |
$output = theme('table', $header, $rows, array('id' => 'uc-fee-table'));
|
| 78 |
|
| 79 |
$output .= drupal_render($form);
|
| 80 |
|
| 81 |
$output .= l(t('Add a fee'), 'admin/store/fees/add');
|
| 82 |
|
| 83 |
return $output;
|
| 84 |
}
|
| 85 |
|
| 86 |
function uc_fee_admin_form_submit($form, &$form_state) {
|
| 87 |
|
| 88 |
foreach ($form_state['values']['fees'] as $fid => $fee) {
|
| 89 |
db_query("UPDATE {uc_fees} SET ordering = %d WHERE fid = %d", $fee['ordering'], $fid);
|
| 90 |
}
|
| 91 |
|
| 92 |
drupal_set_message(t('The changes have been saved.'));
|
| 93 |
}
|
| 94 |
|
| 95 |
function _uc_fee_admin_header() {
|
| 96 |
|
| 97 |
return array(
|
| 98 |
array('data' => t('Name'), 'field' => 'f.name'),
|
| 99 |
array('data' => t('Order'), 'field' => 'f.ordering', 'sort' => 'asc'),
|
| 100 |
array('data' => t('Price'), 'field' => 'f.price'),
|
| 101 |
t('Operations'),
|
| 102 |
);
|
| 103 |
|
| 104 |
}
|
| 105 |
|
| 106 |
|
| 107 |
/**
|
| 108 |
* Form builder for product fees.
|
| 109 |
*
|
| 110 |
* @ingroup forms
|
| 111 |
* @see uc_fee_form_validate()
|
| 112 |
* @see uc_fee_form_submit()
|
| 113 |
*/
|
| 114 |
function uc_fee_form($form_state, $fee = NULL) {
|
| 115 |
// If an fee specified, add its ID as a hidden value.
|
| 116 |
|
| 117 |
if (!empty($fee)) {
|
| 118 |
$form['fid'] = array('#type' => 'hidden', '#value' => $fee->fid);
|
| 119 |
drupal_set_title(t('Edit fee: %name', array('%name' => $fee->name)));
|
| 120 |
}
|
| 121 |
|
| 122 |
$form['name'] = array(
|
| 123 |
'#type' => 'textfield',
|
| 124 |
'#title' => t('Name'),
|
| 125 |
'#description' => t('This name will appear to customers on product add to cart forms.'),
|
| 126 |
'#default_value' => $fee->name,
|
| 127 |
'#required' => TRUE,
|
| 128 |
);
|
| 129 |
$form['description'] = array(
|
| 130 |
'#type' => 'textfield',
|
| 131 |
'#title' => t('Description'),
|
| 132 |
'#description' => t('<b>Optional.</b> Enter the description that will display beneath the fee in the checkout and order panes.'),
|
| 133 |
'#default_value' => $fee->description,
|
| 134 |
'#maxlength' => 255,
|
| 135 |
);
|
| 136 |
$form['price'] = array(
|
| 137 |
'#type' => 'textfield',
|
| 138 |
'#title' => t('Price'),
|
| 139 |
'#default_value' => is_null($fee->price) ? $fee->default_price : $fee->price,
|
| 140 |
'#size' => 6,
|
| 141 |
);
|
| 142 |
$form['ordering'] = array(
|
| 143 |
'#type' => 'weight',
|
| 144 |
'#title' => t('Order'),
|
| 145 |
'#description' => t('Multiple fees are sorted by this value and then by their name.<br />May be overridden at the product level.'),
|
| 146 |
'#default_value' => isset($fee->ordering) ? $fee->ordering : 0,
|
| 147 |
);
|
| 148 |
|
| 149 |
$form['submit'] = array(
|
| 150 |
'#type' => 'submit',
|
| 151 |
'#value' => t('Submit'),
|
| 152 |
'#suffix' => l(t('Cancel'), 'admin/store/fees'),
|
| 153 |
);
|
| 154 |
|
| 155 |
return $form;
|
| 156 |
}
|
| 157 |
|
| 158 |
/**
|
| 159 |
* Submit function for uc_fee_add_form().
|
| 160 |
*/
|
| 161 |
function uc_fee_form_submit($form, &$form_state) {
|
| 162 |
if (!empty($form_state['values']['fid'])) {
|
| 163 |
db_query("UPDATE {uc_fees} SET name = '%s', ordering = %d, description = '%s', price = %d WHERE fid = %d", $form_state['values']['name'], $form_state['values']['ordering'], $form_state['values']['description'], $form_state['values']['price'], $form_state['values']['fid']);
|
| 164 |
}
|
| 165 |
else {
|
| 166 |
db_query("INSERT INTO {uc_fees} (name, ordering, description, price) VALUES ('%s', %d, '%s', %d)", $form_state['values']['name'], $form_state['values']['ordering'], $form_state['values']['description'], $form_state['values']['price']);
|
| 167 |
}
|
| 168 |
|
| 169 |
$form_state['redirect'] = 'admin/store/fees';
|
| 170 |
}
|
| 171 |
|
| 172 |
// Confirms the deletion of the given fee.
|
| 173 |
function uc_fee_delete_confirm($form_state, $fee) {
|
| 174 |
// If we got a bunk fee, kick out an error message.
|
| 175 |
if (empty($fee)) {
|
| 176 |
drupal_set_message(t('There is no fee with that ID.'), 'error');
|
| 177 |
drupal_goto('admin/store/fees');
|
| 178 |
}
|
| 179 |
|
| 180 |
$form['fid'] = array('#type' => 'value', '#value' => $fee->fid);
|
| 181 |
$form['#redirect'] = 'admin/store/fees';
|
| 182 |
|
| 183 |
$count = db_result(db_query("SELECT COUNT(*) FROM {uc_product_fees} WHERE fid = %d", $fee->fid));
|
| 184 |
|
| 185 |
$output = confirm_form($form, t('Are you sure you want to delete the fee %name?', array('%name' => $fee->name)),
|
| 186 |
'admin/store/fees', format_plural($count, 'There is @count product with this fee.', 'There are @count products with this fee.'),
|
| 187 |
t('Delete'), t('Cancel'));
|
| 188 |
|
| 189 |
return $output;
|
| 190 |
}
|
| 191 |
|
| 192 |
/**
|
| 193 |
* Change the display of fee option prices.
|
| 194 |
*
|
| 195 |
* @ingroup forms
|
| 196 |
*/
|
| 197 |
function uc_fee_admin_settings() {
|
| 198 |
$form = array();
|
| 199 |
|
| 200 |
$form['uc_fee_option_price_format'] = array('#type' => 'radios',
|
| 201 |
'#title' => t('Option price format'),
|
| 202 |
'#default_value' => variable_get('uc_fee_option_price_format', 'adjustment'),
|
| 203 |
'#options' => array('none' => t('Do not display'),
|
| 204 |
'adjustment' => t('Display price adjustment'),
|
| 205 |
'total' => t('Display total price'),
|
| 206 |
),
|
| 207 |
'#description' => t('Formats the price in the fee selection form when the customer adds a product to their cart. The total price will only be displayed on products with only one price affecting fee.'),
|
| 208 |
);
|
| 209 |
|
| 210 |
return system_settings_form($form);
|
| 211 |
}
|
| 212 |
|
| 213 |
function uc_fee_delete_confirm_submit($form, &$form_state) {
|
| 214 |
if ($form_state['values']['confirm']) {
|
| 215 |
db_query("DELETE FROM {uc_class_fees} WHERE fid = %d", $form_state['values']['fid']);
|
| 216 |
db_query("DELETE FROM {uc_product_fees} WHERE fid = %d", $form_state['values']['fid']);
|
| 217 |
db_query("DELETE FROM {uc_fees} WHERE fid = %d", $form_state['values']['fid']);
|
| 218 |
drupal_set_message(t('Product fee deleted.'));
|
| 219 |
}
|
| 220 |
}
|
| 221 |
|
| 222 |
// Form to associate fees with products or classes.
|
| 223 |
function uc_object_fees_form($form_state, $object, $type, $view = 'overview') {
|
| 224 |
switch ($type) {
|
| 225 |
case 'class':
|
| 226 |
$class = $object;
|
| 227 |
$id = $class->pcid;
|
| 228 |
if (empty($class->name)) {
|
| 229 |
drupal_goto('admin/store/products/classes/'. $id);
|
| 230 |
}
|
| 231 |
drupal_set_title(check_plain($class->name));
|
| 232 |
$fees = uc_class_get_fees($id);
|
| 233 |
break;
|
| 234 |
case 'product':
|
| 235 |
default:
|
| 236 |
$product = $object;
|
| 237 |
$id = $product->nid;
|
| 238 |
if (empty($product->title)) {
|
| 239 |
drupal_goto('node/'. $id);
|
| 240 |
}
|
| 241 |
drupal_set_title(check_plain($product->title));
|
| 242 |
$fees = uc_product_get_fees($id);
|
| 243 |
}
|
| 244 |
|
| 245 |
$used_fids = array();
|
| 246 |
foreach ($fees as $fee) {
|
| 247 |
$used_fids[] = $fee->fid;
|
| 248 |
}
|
| 249 |
|
| 250 |
if ($view == 'overview') {
|
| 251 |
$form['#tree'] = TRUE;
|
| 252 |
if (count($fees) > 0) {
|
| 253 |
foreach ($fees as $fee) {
|
| 254 |
|
| 255 |
$form['fees'][$fee->fid] = array(
|
| 256 |
'remove' => array(
|
| 257 |
'#type' => 'checkbox',
|
| 258 |
'#default_value' => 0,
|
| 259 |
),
|
| 260 |
'name' => array(
|
| 261 |
'#value' => $fee->name,
|
| 262 |
),
|
| 263 |
'ordering' => array(
|
| 264 |
'#type' => 'weight',
|
| 265 |
'#default_value' => $fee->ordering,
|
| 266 |
'#fees' => array('class' => 'uc-fee-table-weight'),
|
| 267 |
),
|
| 268 |
'price' => array(
|
| 269 |
'#type' => 'textfield',
|
| 270 |
'#default_value' => is_null($fee->price) ? $fee->default_price : $fee->price,
|
| 271 |
'#size' => 6,
|
| 272 |
),
|
| 273 |
);
|
| 274 |
}
|
| 275 |
|
| 276 |
$form['save'] = array(
|
| 277 |
'#type' => 'submit',
|
| 278 |
'#value' => t('Save changes'),
|
| 279 |
'#weight' => -2,
|
| 280 |
);
|
| 281 |
}
|
| 282 |
}
|
| 283 |
elseif ($view == 'add') {
|
| 284 |
// Get list of fees not already assigned to this node or class.
|
| 285 |
$unused_fees = array();
|
| 286 |
$result = db_query("SELECT f.fid, f.name FROM {uc_fees} AS f GROUP BY f.fid, f.name ORDER BY f.name");
|
| 287 |
while ($fee = db_fetch_object($result)) {
|
| 288 |
if (!in_array($fee->fid, $used_fids)) {
|
| 289 |
$unused_fees[$fee->fid] = $fee->name;
|
| 290 |
}
|
| 291 |
}
|
| 292 |
|
| 293 |
$form['add_fees'] = array(
|
| 294 |
'#type' => 'select',
|
| 295 |
'#title' => t('fees'),
|
| 296 |
'#description' => t('Hold Ctrl + click to select multiple fees.'),
|
| 297 |
'#options' => count($unused_fees) > 0 ? $unused_fees : array(t('No fees left to add.')),
|
| 298 |
'#disabled' => count($unused_fees) == 0 ? TRUE : FALSE,
|
| 299 |
'#multiple' => TRUE,
|
| 300 |
'#weight' => -1
|
| 301 |
);
|
| 302 |
$form['add'] = array(
|
| 303 |
'#type' => 'submit',
|
| 304 |
'#value' => t('Add fees'),
|
| 305 |
'#suffix' => l(t('Cancel'), $type == 'product' ? 'node/'. $id .'/edit/fees' : 'admin/store/products/classes/'. $class->pcid .'/fees'),
|
| 306 |
'#weight' => 0,
|
| 307 |
);
|
| 308 |
}
|
| 309 |
|
| 310 |
$form['id'] = array(
|
| 311 |
'#type' => 'value',
|
| 312 |
'#value' => $id,
|
| 313 |
);
|
| 314 |
$form['type'] = array(
|
| 315 |
'#type' => 'value',
|
| 316 |
'#value' => $type,
|
| 317 |
);
|
| 318 |
$form['view'] = array(
|
| 319 |
'#type' => 'value',
|
| 320 |
'#value' => $view,
|
| 321 |
);
|
| 322 |
|
| 323 |
return $form;
|
| 324 |
}
|
| 325 |
|
| 326 |
/**
|
| 327 |
* Display the formatted fee form.
|
| 328 |
*
|
| 329 |
* @ingroup themeable
|
| 330 |
*/
|
| 331 |
function theme_uc_object_fees_form($form) {
|
| 332 |
if ($form['view']['#value'] == 'overview') {
|
| 333 |
$header = array(t('Remove'), t('Name'), t('Price'), t('Order'));
|
| 334 |
|
| 335 |
if (count(element_children($form['fees'])) > 0) {
|
| 336 |
foreach (element_children($form['fees']) as $fid) {
|
| 337 |
$row = array(
|
| 338 |
|
| 339 |
drupal_render($form['fees'][$fid]['remove']),
|
| 340 |
drupal_render($form['fees'][$fid]['name']),
|
| 341 |
drupal_render($form['fees'][$fid]['price']),
|
| 342 |
drupal_render($form['fees'][$fid]['ordering']),
|
| 343 |
);
|
| 344 |
|
| 345 |
$rows[] = array(
|
| 346 |
'data' => $row,
|
| 347 |
'class' => 'draggable',
|
| 348 |
);
|
| 349 |
}
|
| 350 |
}
|
| 351 |
else {
|
| 352 |
$rows[] = array(
|
| 353 |
array('data' => t('You must first <a href="!url">add fees to this !type</a>.', array('!url' => request_uri() .'/add', '!type' => $form['type']['#value'])), 'colspan' => 6),
|
| 354 |
);
|
| 355 |
}
|
| 356 |
|
| 357 |
drupal_add_tabledrag('uc-fee-table', 'order', 'sibling', 'uc-fee-table-weight');
|
| 358 |
$output = theme('table', $header, $rows, array('id' => 'uc-fee-table'));
|
| 359 |
}
|
| 360 |
|
| 361 |
$output .= drupal_render($form);
|
| 362 |
|
| 363 |
return $output;
|
| 364 |
}
|
| 365 |
|
| 366 |
function uc_object_fees_form_submit($form, &$form_state) {
|
| 367 |
if ($form_state['values']['type'] == 'product') {
|
| 368 |
$fee_table = '{uc_product_fees}';
|
| 369 |
$id = 'nid';
|
| 370 |
$sql_type = '%d';
|
| 371 |
}
|
| 372 |
elseif ($form_state['values']['type'] == 'class') {
|
| 373 |
$fee_table = '{uc_class_fees}';
|
| 374 |
$id = 'pcid';
|
| 375 |
$sql_type = "'%s'";
|
| 376 |
}
|
| 377 |
|
| 378 |
if ($form_state['values']['view'] == 'overview' && is_array($form_state['values']['fees'])) {
|
| 379 |
foreach ($form_state['values']['fees'] as $fid => $fee) {
|
| 380 |
if ($fee['remove']) {
|
| 381 |
$remove_fids[] = $fid;
|
| 382 |
}
|
| 383 |
else {
|
| 384 |
db_query("UPDATE $fee_table SET ordering = %d, default_price = %f WHERE fid = %d AND $id = $sql_type", $form_state['values']['fees'][$fid]['ordering'], $form_state['values']['fees'][$fid]['price'], $fid, $form_state['values']['id']);
|
| 385 |
$changed = TRUE;
|
| 386 |
}
|
| 387 |
}
|
| 388 |
|
| 389 |
if (count($remove_fids) > 0) {
|
| 390 |
$values = array($form_state['values']['id'], implode(', ', $remove_fids));
|
| 391 |
|
| 392 |
db_query("DELETE FROM $fee_table WHERE $id = $sql_type AND fid IN (%s)", $values);
|
| 393 |
|
| 394 |
drupal_set_message(format_plural(count($remove_fids), '@count fee has been removed.', '@count fees have been removed.'));
|
| 395 |
}
|
| 396 |
|
| 397 |
if ($changed) {
|
| 398 |
drupal_set_message(t('The changes have been saved.'));
|
| 399 |
}
|
| 400 |
}
|
| 401 |
elseif ($form_state['values']['view'] == 'add') {
|
| 402 |
foreach ($form_state['values']['add_fees'] as $fid) {
|
| 403 |
// Enable all options for added fees.
|
| 404 |
$fee = uc_fee_load($fid);
|
| 405 |
db_query("INSERT INTO $fee_table ($id, fid, ordering, default_price) VALUES ($sql_type, %d, %d, %f)", $form_state['values']['id'], $fee->fid, $fee->ordering, $fee->price);
|
| 406 |
}
|
| 407 |
if (count($form_state['values']['add_fees']) > 0) {
|
| 408 |
drupal_set_message(format_plural(count($form_state['values']['add_fees']), '@count fee has been added.', '@count fees have been added.'));
|
| 409 |
}
|
| 410 |
}
|
| 411 |
|
| 412 |
if ($form_state['values']['type'] == 'product') {
|
| 413 |
$form_state['redirect'] = 'node/'. $form_state['values']['id'] .'/edit/fees';
|
| 414 |
}
|
| 415 |
else {
|
| 416 |
$form_state['redirect'] = 'admin/store/products/classes/'. $form_state['values']['id'] .'/fees';
|
| 417 |
}
|
| 418 |
}
|