| 1 |
<?php |
<?php |
| 2 |
// $Id: uc_restrict_qty.module,v 1.1.4.2 2008/10/17 14:41:03 mrfelton Exp $ |
// $Id: uc_restrict_qty.module,v 1.1.4.3 2008/12/04 22:46:39 mrfelton Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 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. |
db_query("UPDATE {uc_cart_products} SET qty = 1, changed = %d WHERE nid = %d AND cart_id = '%s' AND data = '%s'", time(), $item->nid, uc_cart_get_id(), serialize($item->data)); |
|
db_query("UPDATE {uc_cart_products} SET qty = 1 WHERE cart_id = '%s' AND nid = %d AND data = '%s'", uc_cart_get_id(), $item->nid, serialize($item->data)); |
|
| 119 |
$item->qty = 1; |
$item->qty = 1; |
| 120 |
} |
} |
| 121 |
|
// If this item quantity has exceeded the global restriction... |
| 122 |
|
elseif (variable_get('uc_restrict_qty_global', 0) && $item->qty >= variable_get('uc_restrict_qty_global', 0)) { |
| 123 |
|
db_query("UPDATE {uc_cart_products} SET qty = %d, changed = %d WHERE nid = %d AND cart_id = '%s' AND data = '%s'", variable_get('uc_restrict_qty_global', 0), time(), $item->nid, uc_cart_get_id(), serialize($item->data)); |
| 124 |
|
$item->qty = variable_get('uc_restrict_qty_global', 0); |
| 125 |
|
} |
| 126 |
} |
} |
| 127 |
} |
} |
| 128 |
|
|
| 133 |
// Disable the appropriate Qty. fields on the cart view form. |
// Disable the appropriate Qty. fields on the cart view form. |
| 134 |
if ($form_id == 'uc_cart_view_form') { |
if ($form_id == 'uc_cart_view_form') { |
| 135 |
foreach ($form['#parameters'][2] as $key => $item) { |
foreach ($form['#parameters'][2] as $key => $item) { |
| 136 |
// If this item has a quantity restriction on it... |
// If this item has a quantity restriction feature on it... |
| 137 |
if ($item->data['restrict_qty'] > 0) { |
if ($item->data['restrict_qty'] > 0) { |
| 138 |
$form['items'][$key]['qty']['#theme'] = 'restrict_qty_field'; |
$form['items'][$key]['qty']['#attributes']['readonly'] = 'readonly'; |
| 139 |
|
//$form['items'][$key]['qty']['#theme'] = 'restrict_qty_field'; |
| 140 |
} |
} |
| 141 |
} |
} |
| 142 |
} |
} |
| 150 |
'#size' => 5, |
'#size' => 5, |
| 151 |
'#maxlength' => 5, |
'#maxlength' => 5, |
| 152 |
'#description' => t('The total number of products that can be added to a cart. Set to 0 for unlimited.'), |
'#description' => t('The total number of products that can be added to a cart. Set to 0 for unlimited.'), |
| 153 |
'#default_value' => variable_get('uc_restrict_qty_global', NULL), |
'#default_value' => variable_get('uc_restrict_qty_global', 0), |
| 154 |
); |
); |
| 155 |
$form['#validate'][] = 'uc_restrict_qty_admin_form_validate'; |
$form['#validate'][] = 'uc_restrict_qty_admin_form_validate'; |
| 156 |
return system_settings_form($form); |
return system_settings_form($form); |
| 157 |
} |
} |
| 158 |
|
|
| 159 |
// Builds a paged list and overview of existing product fees. |
// Ensure user has entered a sensible value. |
| 160 |
function uc_restrict_qty_admin_form_validate(&$form, $form_state) { |
function uc_restrict_qty_admin_form_validate(&$form, $form_state) { |
| 161 |
if (!is_numeric($form_state['values']['uc_restrict_qty_global'])) { |
if (!is_numeric($form_state['values']['uc_restrict_qty_global'])) { |
| 162 |
form_set_error('uc_restrict_qty_global', t('Please enter a numeric value.')); |
form_set_error('uc_restrict_qty_global', t('Please enter a numeric value.')); |
| 168 |
|
|
| 169 |
// 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) |
| 170 |
function theme_restrict_qty_field($element) { |
function theme_restrict_qty_field($element) { |
| 171 |
//$element['#attributes']['readonly'] = 'readonly'; |
return check_plain($element['#value']); |
|
return check_plain($element['#value']); |
|
| 172 |
} |
} |
| 173 |
|
|
| 174 |
// Returns the number of restrict_qty features on a product node. |
// Returns the number of restrict_qty features on a product node. |
| 175 |
function uc_restrict_qty_count($nid) { |
function uc_restrict_qty_count($nid) { |
|
if (variable_get('uc_restrict_qty_global', 0)) { |
|
|
return variable_get('uc_restrict_qty_global', 0); |
|
|
} |
|
| 176 |
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')); |
| 177 |
} |
} |