| 1 |
<?php |
<?php |
| 2 |
// $Id$ |
// $Id: uc_restrict_qty.module,v 1.1.4.2 2008/10/17 14:41:03 mrfelton Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 11 |
*/ |
*/ |
| 12 |
|
|
| 13 |
/** |
/** |
| 14 |
|
* Implementation of hook_help(). |
| 15 |
|
*/ |
| 16 |
|
function uc_restrict_qty_help($path, $arg) { |
| 17 |
|
switch ($path) { |
| 18 |
|
case 'admin/store/settings/restrict_qty': |
| 19 |
|
return t('You may use this page to set a global limit on the number of items that a shopping cart may contain.'); |
| 20 |
|
} |
| 21 |
|
} |
| 22 |
|
|
| 23 |
|
/** |
| 24 |
|
* Implementation of hook_menu(). |
| 25 |
|
*/ |
| 26 |
|
function uc_restrict_qty_menu() { |
| 27 |
|
$items['admin/store/settings/restrict_qty'] = array( |
| 28 |
|
'title' => 'Restrict Qty', |
| 29 |
|
'description' => 'Adjust settings for Ubercart Restrict Qty feature.', |
| 30 |
|
'page callback' => 'drupal_get_form', |
| 31 |
|
'page arguments' => array('uc_restrict_qty_admin_form'), |
| 32 |
|
'access arguments' => array('administer products'), |
| 33 |
|
'type' => MENU_NORMAL_ITEM, |
| 34 |
|
'weight' => 1, |
| 35 |
|
); |
| 36 |
|
return $items; |
| 37 |
|
} |
| 38 |
|
|
| 39 |
|
/** |
| 40 |
* Implementation of hook_theme(). |
* Implementation of hook_theme(). |
| 41 |
*/ |
*/ |
| 42 |
function uc_restrict_qty_theme() { |
function uc_restrict_qty_theme() { |
| 83 |
} |
} |
| 84 |
|
|
| 85 |
/** |
/** |
| 86 |
|
* Implementation of hook_add_to_cart(). |
| 87 |
|
*/ |
| 88 |
|
function uc_restrict_qty_add_to_cart($nid, $qty, $data) { |
| 89 |
|
if (variable_get('uc_restrict_qty_global', 0)) { |
| 90 |
|
$cart_items = uc_cart_get_contents(uc_cart_get_id()); |
| 91 |
|
if (sizeof($cart_items) >= variable_get('uc_restrict_qty_global', 0)) { |
| 92 |
|
$result[] = array( |
| 93 |
|
'success' => FALSE, |
| 94 |
|
'message' => t('Sorry, you may only have @qty in your cart. Please remove something from your cart first.', |
| 95 |
|
array( |
| 96 |
|
'@qty' => format_plural(variable_get('uc_restrict_qty_global', 0), '1 item', '@count items'), |
| 97 |
|
)), |
| 98 |
|
); |
| 99 |
|
} |
| 100 |
|
} |
| 101 |
|
return $result; |
| 102 |
|
} |
| 103 |
|
|
| 104 |
|
/** |
| 105 |
* Implementation of hook_add_to_cart_data(). |
* Implementation of hook_add_to_cart_data(). |
| 106 |
*/ |
*/ |
| 107 |
function uc_restrict_qty_add_to_cart_data($form_values) { |
function uc_restrict_qty_add_to_cart_data($form_values) { |
| 113 |
*/ |
*/ |
| 114 |
function uc_restrict_qty_cart_item($op, &$item) { |
function uc_restrict_qty_cart_item($op, &$item) { |
| 115 |
if ($op == 'load') { |
if ($op == 'load') { |
|
|
|
| 116 |
// If this item has a quantity restriction on it... |
// If this item has a quantity restriction on it... |
| 117 |
if ($item->data['restrict_qty'] > 0 && $item->qty > 1) { |
if ($item->data['restrict_qty'] > 0 && $item->qty > 1) { |
| 118 |
// Reduce the quantity to 1 if necessary. |
// Reduce the quantity to 1 if necessary. |
| 128 |
function uc_restrict_qty_form_alter(&$form, &$form_state, $form_id) { |
function uc_restrict_qty_form_alter(&$form, &$form_state, $form_id) { |
| 129 |
// Disable the appropriate Qty. fields on the cart view form. |
// Disable the appropriate Qty. fields on the cart view form. |
| 130 |
if ($form_id == 'uc_cart_view_form') { |
if ($form_id == 'uc_cart_view_form') { |
| 131 |
foreach ($form['items'] as $key => $value) { |
foreach ($form['#parameters'][2] as $key => $item) { |
|
$data = unserialize($value['data']['#value']); |
|
| 132 |
// If this item has a quantity restriction on it... |
// If this item has a quantity restriction on it... |
| 133 |
if ($data['restrict_qty'] > 0) { |
if ($item->data['restrict_qty'] > 0) { |
| 134 |
//$form['items'][$key]['qty']['#theme'] = 'restrict_qty_field'; |
$form['items'][$key]['qty']['#theme'] = 'restrict_qty_field'; |
|
$form['items'][$key]['qty']['#attributes'] = array('readonly' => 'readonly'); |
|
| 135 |
} |
} |
| 136 |
} |
} |
| 137 |
} |
} |
| 138 |
} |
} |
| 139 |
|
|
| 140 |
|
// builds the admin settings form. |
| 141 |
|
function uc_restrict_qty_admin_form() { |
| 142 |
|
$form['uc_restrict_qty_global'] = array( |
| 143 |
|
'#title' => t('Global limit'), |
| 144 |
|
'#type' => 'textfield', |
| 145 |
|
'#size' => 5, |
| 146 |
|
'#maxlength' => 5, |
| 147 |
|
'#description' => t('The total number of products that can be added to a cart. Set to 0 for unlimited.'), |
| 148 |
|
'#default_value' => variable_get('uc_restrict_qty_global', NULL), |
| 149 |
|
); |
| 150 |
|
$form['#validate'][] = 'uc_restrict_qty_admin_form_validate'; |
| 151 |
|
return system_settings_form($form); |
| 152 |
|
} |
| 153 |
|
|
| 154 |
|
// Builds a paged list and overview of existing product fees. |
| 155 |
|
function uc_restrict_qty_admin_form_validate(&$form, $form_state) { |
| 156 |
|
if (!is_numeric($form_state['values']['uc_restrict_qty_global'])) { |
| 157 |
|
form_set_error('uc_restrict_qty_global', t('Please enter a numeric value.')); |
| 158 |
|
} |
| 159 |
|
if ($form_state['values']['uc_restrict_qty_global'] < 0) { |
| 160 |
|
form_set_error('uc_restrict_qty_global', t('Please enter a positive integer.')); |
| 161 |
|
} |
| 162 |
|
} |
| 163 |
|
|
| 164 |
// Themes cart Qty. boxes so they can't be changed. (currently not in use) |
// Themes cart Qty. boxes so they can't be changed. (currently not in use) |
| 165 |
function theme_restrict_qty_field($element) { |
function theme_restrict_qty_field($element) { |
| 166 |
//$element['#attributes']['readonly'] = 'readonly'; |
//$element['#attributes']['readonly'] = 'readonly'; |
| 169 |
|
|
| 170 |
// Returns the number of restrict_qty features on a product node. |
// Returns the number of restrict_qty features on a product node. |
| 171 |
function uc_restrict_qty_count($nid) { |
function uc_restrict_qty_count($nid) { |
| 172 |
|
if (variable_get('uc_restrict_qty_global', 0)) { |
| 173 |
|
return variable_get('uc_restrict_qty_global', 0); |
| 174 |
|
} |
| 175 |
return db_result(db_query("SELECT COUNT(*) FROM {uc_product_features} WHERE nid = %d AND fid = '%s'", $nid, 'restrict_qty')); |
return db_result(db_query("SELECT COUNT(*) FROM {uc_product_features} WHERE nid = %d AND fid = '%s'", $nid, 'restrict_qty')); |
| 176 |
} |
} |