/[drupal]/contributions/modules/uc_restrict_qty/uc_restrict_qty.module
ViewVC logotype

Diff of /contributions/modules/uc_restrict_qty/uc_restrict_qty.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1.4.5.2.2, Mon Apr 13 11:21:15 2009 UTC revision 1.1.4.5.2.3, Sun May 17 09:10:45 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: uc_restrict_qty.module,v 1.1.4.5.2.1 2009/04/13 11:13:42 mrfelton Exp $  // $Id: uc_restrict_qty.module,v 1.1.4.5.2.2 2009/04/13 11:21:15 mrfelton Exp $
3    
4  /**  /**
5   * @file   * @file
6   * Restrict the quantity on specified products so that only one may be purchased   * Restrict the quantity on specified products so that only specified quantity may be purchased
7   * at a time.   * at a time.
8   */   */
9    
# Line 43  function uc_restrict_qty_settings() { Line 43  function uc_restrict_qty_settings() {
43      '#size' => 5,      '#size' => 5,
44      '#maxlength' => 5,      '#maxlength' => 5,
45      '#description' => t('The number of different products that can be added to a cart. Set to 0 for unlimited.'),      '#description' => t('The number of different products that can be added to a cart. Set to 0 for unlimited.'),
46      '#default_value' => variable_get('uc_restrict_qty_global', '0'),      '#default_value' => variable_get('uc_restrict_qty_global', 0),
47      );
48    
49      $form['defaults'] = array(
50        '#title' => t('Defaults'),
51        '#type' => 'fieldset',
52        '#description' => t('These options will take action, only if product has the "Restrict quantity" feature activated.'),
53      );
54      $form['defaults']['uc_restrict_qty_default_qty'] = array(
55        '#title' => t("Default maximum limit for a product"),
56        '#type' => 'textfield',
57        '#size' => 5,
58        '#maxlength' => 5,
59        '#description' => t('The number of products of single type that can be added to a cart. Set to 0 for unlimited.'),
60        '#default_value' => variable_get('uc_restrict_qty_default_qty', 0),
61      );
62      $form['defaults']['uc_restrict_qty_default_lifetime'] = array(
63        '#title' => t("Is restriction is the user's lifetime limit"),
64        '#type' => 'checkbox',
65        '#description' => t("Useful when you want to prevent double ordering of a product."),
66        '#default_value' => variable_get('uc_restrict_qty_default_lifetime', FALSE),
67    );    );
68    
69    return $form;    return $form;
# Line 54  function uc_restrict_qty_settings_valida Line 74  function uc_restrict_qty_settings_valida
74    if (!is_numeric($form_state['values']['uc_restrict_qty_global']) || $form_state['values']['uc_restrict_qty_global'] < 0) {    if (!is_numeric($form_state['values']['uc_restrict_qty_global']) || $form_state['values']['uc_restrict_qty_global'] < 0) {
75      form_set_error('uc_restrict_qty_global', t('You must enter 0 or a positive number value.'));      form_set_error('uc_restrict_qty_global', t('You must enter 0 or a positive number value.'));
76    }    }
77      if (!is_numeric($form_state['values']['uc_restrict_qty_default_qty']) || $form_state['values']['uc_restrict_qty_default_qty'] < 0) {
78        form_set_error('uc_restrict_qty_default_qty', t('You must enter 0 or a positive number value.'));
79      }
80  }  }
81    
82  // Builds the form to display for adding or editing a the restricted quantity feature.  // Builds the form to display for adding or editing a the restricted quantity feature.
# Line 61  function uc_restrict_qty_feature_form($f Line 84  function uc_restrict_qty_feature_form($f
84    $models = uc_product_get_models($node);    $models = uc_product_get_models($node);
85    if (!empty($feature)) {    if (!empty($feature)) {
86      $product_qty = db_fetch_object(db_query("SELECT * FROM {uc_restrict_qty_products} WHERE pfid = %d", $feature['pfid']));      $product_qty = db_fetch_object(db_query("SELECT * FROM {uc_restrict_qty_products} WHERE pfid = %d", $feature['pfid']));
87    
88      $default_qty = $product_qty->qty;      $default_qty = $product_qty->qty;
89      $default_model = $product_qty->model;      $default_model = $product_qty->model;
90        $default_lifetime = $product_qty->lifetime;
91    
92      $form['pfid'] = array(      $form['pfid'] = array(
93        '#type' => 'value',        '#type' => 'value',
94        '#value' => $feature['pfid'],        '#value' => $feature['pfid'],
# Line 74  function uc_restrict_qty_feature_form($f Line 98  function uc_restrict_qty_feature_form($f
98        '#value' => $product_qty->rqpid,        '#value' => $product_qty->rqpid,
99      );      );
100    }    }
   else {  
     $default_model = 0;  
     $default_qty = variable_get('uc_restrict_qty_default_qty', '1');  
   }  
101  /*  if ($result = db_result(db_query("SELECT COUNT(*) FROM {uc_product_features} WHERE nid = %d AND fid = '%s'", $node->nid, 'restrict_qty'))) {  /*  if ($result = db_result(db_query("SELECT COUNT(*) FROM {uc_product_features} WHERE nid = %d AND fid = '%s'", $node->nid, 'restrict_qty'))) {
102      drupal_set_message(t('Adding more than one Restrict Qty. does nothing for now.', 'error'));      drupal_set_message(t('Adding more than one Restrict Qty. does nothing for now.', 'error'));
103      $form['no_roles'] = array(      $form['no_roles'] = array(
# Line 95  function uc_restrict_qty_feature_form($f Line 115  function uc_restrict_qty_feature_form($f
115    $form['model'] = array(    $form['model'] = array(
116      '#type' => 'select',      '#type' => 'select',
117      '#title' => t('SKU'),      '#title' => t('SKU'),
118      '#default_value' => $default_model,      '#default_value' => isset($default_model) ? $default_model : 0,
119      '#description' => t('This is the SKU of the product that will be restricted to this quantity.'),      '#description' => t('This is the SKU of the product that will be restricted to this quantity.'),
120      '#options' => $models,      '#options' => $models,
121    );    );
# Line 105  function uc_restrict_qty_feature_form($f Line 125  function uc_restrict_qty_feature_form($f
125      '#size' => 5,      '#size' => 5,
126      '#maxlength' => 5,      '#maxlength' => 5,
127      '#description' => t('The number of times this product can be added to a cart. Set to 0 for unlimited.'),      '#description' => t('The number of times this product can be added to a cart. Set to 0 for unlimited.'),
128      '#default_value' => $default_qty,      '#default_value' => isset($default_qty) ? $default_qty : variable_get('uc_restrict_qty_default_qty', 0),
129      );
130      $form['lifetime'] = array(
131        '#title' => t('Is it a lifetime restriction?'),
132        '#type' => 'checkbox',
133        '#description' => t("If checked, user's ordering history will be taken into account too. Useful when you want to prevent double ordering of a product."),
134        '#default_value' => isset($default_lifetime) ? $default_lifetime : variable_get('uc_restrict_qty_default_lifetime', FALSE),
135    );    );
136    return uc_product_feature_form($form);    return uc_product_feature_form($form);
137  }  }
138    
139  // Validates the textfield on the product features settings form.  // Validates the textfield on the product features settings form.
140  function uc_restrict_qty_feature_form_validate($form, &$form_state) {  function uc_restrict_qty_feature_form_validate($form, &$form_state) {
   
141    // Invalid quantity?    // Invalid quantity?
142    if (!is_numeric($form_state['values']['quantity']) || $form_state['values']['quantity'] < 0) {    if (!is_numeric($form_state['values']['quantity']) || $form_state['values']['quantity'] < 0) {
143      form_set_error('uc_restrict_qty_product_qty', t('You must enter 0 or a positive integer value.'));      form_set_error('uc_restrict_qty_product_qty', t('You must enter 0 or a positive integer value.'));
# Line 134  function uc_restrict_qty_feature_form_su Line 159  function uc_restrict_qty_feature_form_su
159      'nid'         => $form_state['values']['nid'],      'nid'         => $form_state['values']['nid'],
160      'model'       => $form_state['values']['model'],      'model'       => $form_state['values']['model'],
161      'qty'         => $form_state['values']['quantity'],      'qty'         => $form_state['values']['quantity'],
162        'lifetime'    => $form_state['values']['lifetime'],
163    );    );
164    
165    $description = empty($product_qty['model']) ? t('<strong>SKU:</strong> Any<br/>') : t('<strong>SKU:</strong> !sku<br/>', array('!sku' => $product_qty['model']));    $description = '<strong>'. t('SKU') .':</strong> '. (empty($product_qty['model']) ? t('Any') : $product_qty['model']) .'<br/>';
166    $description .=  t('<strong>Quantity restriction:</strong> !qty<br/>', array('!qty' => $form_state['values']['quantity']));    $description .= '<strong>'. t('Quantity restriction') .':</strong> '. $product_qty['qty'] .'<br/>';
167      $description .= '<strong>'. t('Type') .':</strong> '. ($product_qty['lifetime'] ? t('Lifetime') : t('Cart max.')) .'<br/>';
168    
169    $data = array(    $data = array(
170      'pfid' => $product_qty['pfid'],      'pfid' => $product_qty['pfid'],
171      'nid' => $product_qty['nid'],      'nid' => $product_qty['nid'],
# Line 166  function uc_restrict_qty_feature_form_su Line 193  function uc_restrict_qty_feature_form_su
193   */   */
194  function uc_restrict_qty_add_to_cart($nid, $qty, $data) {  function uc_restrict_qty_add_to_cart($nid, $qty, $data) {
195    $limit = variable_get('uc_restrict_qty_global', 0);    $limit = variable_get('uc_restrict_qty_global', 0);
   
196    // If a global restriction on the number of items has been made.    // If a global restriction on the number of items has been made.
197    if ($limit > 0) {    if ($limit > 0) {
198      if (count(uc_cart_get_contents()) >= $limit) {      if (count(uc_cart_get_contents()) >= $limit) {
199        $message = format_plural($limit, 'Sorry, you may only have 1 item in your cart. You must checkout or remove the item in your cart before adding a different item.',        $message = format_plural($limit, "Sorry, you may only have a total of 1 item in your cart. You must <a href='@checkout'>checkout</a> or remove the item in <a href='@cart'>your cart</a> before adding a different item.",
200          'Sorry, you may only have a total of @count items in your cart.  You must checkout or remove items from your cart before adding others.');          "Sorry, you may only have a total of @count items in your cart. You must <a href='@checkout'>checkout</a> or remove items from <a href='@cart'>your cart</a> before adding others.", array('@cart' => url('cart'), '@checkout' => url('cart/checkout')));
201    
202          $result[] = array(
203            'success' => FALSE,
204            'message' => $message,
205          );
206        }
207      }
208    
209      // Check lifetime product-level limit.
210      if ($data['restrict_qty']['lifetime']) {
211        if (!$data['restrict_qty']['rest']) {
212          $message = t('Sorry, you have reached the quantity limit for this product. You can not order more items of this product.');
213    
214        $result[] = array(        $result[] = array(
215          'success' => FALSE,          'success' => FALSE,
# Line 195  function uc_restrict_qty_add_to_cart_dat Line 233  function uc_restrict_qty_add_to_cart_dat
233  function uc_restrict_qty_cart_item($op, &$item) {  function uc_restrict_qty_cart_item($op, &$item) {
234    if ($op == 'load') {    if ($op == 'load') {
235      // If this item has a quantity restriction on it...      // If this item has a quantity restriction on it...
236      if ($item->data['restrict_qty'] > 0 && $item->qty > $item->data['restrict_qty']) {      if ($item->data['restrict_qty']['qty'] > 0) {
237        // Reduce the quantity to 1 if necessary.        $is_lifetime = isset($item->data['restrict_qty']['lifetime']) && $item->data['restrict_qty']['lifetime'];
238        db_query("UPDATE {uc_cart_products} SET qty = %d, changed = %d WHERE cart_id = '%s' AND nid = %d AND data = '%s'", $item->data['restrict_qty'], time(), uc_cart_get_id(), $item->nid, serialize($item->data));        $restrict_qty = $is_lifetime ? $item->data['restrict_qty']['rest'] : $item->data['restrict_qty']['qty'];
239        $item->qty = $item->data['restrict_qty'];        if ($item->qty > $restrict_qty) {
240        drupal_set_message(t('You may only add !qty !item. to your cart. Quantity has been restricted.', array('!qty' => $item->qty, '!item' => $item->title)), 'warning');          // Reduce the quantity to 1 if necessary.
241            $item->qty = $restrict_qty;
242            db_query("UPDATE {uc_cart_products} SET qty = %d, changed = %d WHERE cart_id = '%s' AND nid = %d AND data = '%s'", $restrict_qty, time(), uc_cart_get_id(), $item->nid, serialize($item->data));
243            drupal_set_message(format_plural($restrict_qty, 'You may only add 1 !item to your cart. Quantity has been restricted.', 'You may only add @count !item to your cart. Quantity has been restricted.', array('!item' => $item->title)), 'warning');
244          }
245      }      }
246    }    }
247  }  }
# Line 214  function uc_restrict_qty_form_alter(&$fo Line 256  function uc_restrict_qty_form_alter(&$fo
256        $data = unserialize($form['items'][$i]['data']['#value']);        $data = unserialize($form['items'][$i]['data']['#value']);
257    
258        // If this item has a quantity restriction on it...        // If this item has a quantity restriction on it...
259        if ($data['restrict_qty'] == 1) {        $is_lifetime = isset($data['restrict_qty']['lifetime']) && $data['restrict_qty']['lifetime'];
260          $restrict_qty = $is_lifetime ? $data['restrict_qty']['rest'] : $data['restrict_qty']['qty'];
261          if ($restrict_qty == 1) {
262          $form['items'][$i]['qty']['#type'] = 'value';          $form['items'][$i]['qty']['#type'] = 'value';
263          $form['items'][$i]['qty']['#theme'] = 'restrict_qty_field';          $form['items'][$i]['qty']['#theme'] = 'restrict_qty_field';
264        }        }
# Line 233  function theme_restrict_qty_field($eleme Line 277  function theme_restrict_qty_field($eleme
277    
278  // Returns the number of restrict_qty features on a product node.  // Returns the number of restrict_qty features on a product node.
279  function uc_restrict_qty_count($form_values) {  function uc_restrict_qty_count($form_values) {
280    return db_result(db_query("SELECT qty FROM {uc_restrict_qty_products} WHERE nid = %d", $form_values['nid']));    global $user;
281    
282      $data = db_fetch_array(db_query("SELECT qty, lifetime FROM {uc_restrict_qty_products} WHERE nid = %d", $form_values['nid']));
283      if ($data['lifetime']) {
284        $bought_qty = db_result(db_query('SELECT SUM(uop.qty) FROM {uc_orders} uo LEFT JOIN {uc_order_products} uop ON uo.order_id = uop.order_id WHERE uo.order_status = "completed" AND uo.uid = %d AND uop.nid = %d ORDER BY uop.nid', $user->uid, $form_values['nid']));
285        $data['rest'] = $data['qty'] - $bought_qty;
286      }
287    
288      return $data;
289  }  }
290    
291  /* ************************************************************************* *  /* ************************************************************************* *

Legend:
Removed from v.1.1.4.5.2.2  
changed lines
  Added in v.1.1.4.5.2.3

  ViewVC Help
Powered by ViewVC 1.1.2