array( 'render element' => 'form', ), ); } /** * Implementation of hook_uc_product_feature(). */ function uc_restrict_qty_uc_product_feature() { $features[] = array( 'id' => 'restrict_qty', 'title' => t('Restrict Qty'), 'callback' => 'uc_restrict_qty_feature_form', 'delete' => 'uc_restrict_qty_feature_delete', 'settings' => 'uc_restrict_qty_settings', ); return $features; } // Adds settings to the product features form for UC Restrict Qty. function uc_restrict_qty_settings() { $form['#description'] = t('This feature is limited in scope to preventing a user from adding different products to the cart. This does not restrict the quantity of products in the cart if updated after being added, so this feature is best used on sites where all products have a restrict quantity feature on them.'); $form['uc_restrict_qty_global'] = array( '#title' => t('Global limit'), '#type' => 'textfield', '#size' => 5, '#maxlength' => 5, '#description' => t('The number of different products that can be added to a cart. Set to 0 for unlimited.'), '#default_value' => variable_get('uc_restrict_qty_global', 0), ); $form['uc_restrict_qty_global_replace'] = array( '#title' => t('Replace Contents'), '#type' => 'checkbox', '#description' => t('Enabling this feature will cause the users cart to be emptied if they add more items than the Global Limit set above. This is best used when you offer mutually exclusive content (such as subscription services) where purchasing more then one product is not a valid option.'), '#default_value' => variable_get('uc_restrict_qty_global_replace', FALSE), ); $form['defaults'] = array( '#title' => t('Defaults'), '#type' => 'fieldset', '#description' => t('These options will take action, only if product has the "Restrict quantity" feature activated.'), ); $form['defaults']['uc_restrict_qty_default_qty'] = array( '#title' => t("Default maximum limit for a product"), '#type' => 'textfield', '#size' => 5, '#maxlength' => 5, '#description' => t('The number of products of single type that can be added to a cart. Set to 0 for unlimited.'), '#default_value' => variable_get('uc_restrict_qty_default_qty', 0), ); $form['defaults']['uc_restrict_qty_default_lifetime'] = array( '#title' => t("Is restriction is the user's lifetime limit"), '#type' => 'checkbox', '#description' => t("Useful when you want to prevent double ordering of a product."), '#default_value' => variable_get('uc_restrict_qty_default_lifetime', FALSE), ); return $form; } // Validates the textfield on the product features settings form. function uc_restrict_qty_settings_validate($form, &$form_state) { if (!is_numeric($form_state['values']['uc_restrict_qty_global']) || $form_state['values']['uc_restrict_qty_global'] < 0) { form_set_error('uc_restrict_qty_global', t('You must enter 0 or a positive number value.')); } if (!is_numeric($form_state['values']['uc_restrict_qty_default_qty']) || $form_state['values']['uc_restrict_qty_default_qty'] < 0) { form_set_error('uc_restrict_qty_default_qty', t('You must enter 0 or a positive number value.')); } } // Builds the form to display for adding or editing a the restricted quantity feature. function uc_restrict_qty_feature_form($form, &$form_state, $node, $feature) { $models = uc_product_get_models($node->nid); if (!empty($feature)) { $product_qty = db_query("SELECT * FROM {uc_restrict_qty_products} WHERE pfid = :pfid", array(':pfid' => $feature['pfid']))->fetchObject(); $default_qty = $product_qty->qty; $default_model = $product_qty->model; $default_lifetime = $product_qty->lifetime; $form['pfid'] = array( '#type' => 'value', '#value' => $feature['pfid'], ); $form['rqpid'] = array( '#type' => 'value', '#value' => $product_qty->rqpid, ); } /* if ($result = db_result(db_query("SELECT COUNT(*) FROM {uc_product_features} WHERE nid = %d AND fid = '%s'", $node->nid, 'restrict_qty'))) { drupal_set_message(t('Adding more than one Restrict Qty. does nothing for now.', 'error')); $form['no_roles'] = array( '#value' => t('You need to create new roles before any can be added as product features.', array('!url' => url('admin/user/roles', array('query' => 'destination=admin/store/settings/products/edit/features')))), '#prefix' => '
', '#suffix' => '
', ); return $form; }*/ $form['nid'] = array( '#type' => 'value', '#value' => $node->nid, ); $form['model'] = array( '#type' => 'select', '#title' => t('SKU'), '#default_value' => isset($default_model) ? $default_model : 0, '#description' => t('This is the SKU of the product that will be restricted to this quantity.'), '#options' => $models, ); $form['quantity'] = array( '#title' => t('Quantity limit'), '#type' => 'textfield', '#size' => 5, '#maxlength' => 5, '#description' => t('The number of times this product can be added to a cart. Set to 0 for unlimited.'), '#default_value' => isset($default_qty) ? $default_qty : variable_get('uc_restrict_qty_default_qty', 0), ); $form['lifetime'] = array( '#title' => t('Is it a lifetime restriction?'), '#type' => 'checkbox', '#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."), '#default_value' => isset($default_lifetime) ? $default_lifetime : variable_get('uc_restrict_qty_default_lifetime', FALSE), ); return uc_product_feature_form($form, $form_state, $node, $feature); } // Validates the textfield on the product features settings form. function uc_restrict_qty_feature_form_validate($form, &$form_state) { // Invalid quantity? if (!is_numeric($form_state['values']['quantity']) || $form_state['values']['quantity'] < 0) { form_set_error('uc_restrict_qty_product_qty', t('You must enter 0 or a positive integer value.')); } // This feature is already set on this SKU? $product_roles = db_query("SELECT * FROM {uc_restrict_qty_products} WHERE nid = :nid AND model = :model", array( ':nid' => $form_state['values']['nid'], ':model' => $form_state['values']['model'], ))->fetchObject(); if ($product_roles && $form_state['values']['pfid'] == 0) { form_set_error('uc_roles_model', t('A quantity restriction has already been set up for this SKU')); } } /** * Form submit handler for the roles feature form. */ function uc_restrict_qty_feature_form_submit($form, &$form_state) {dpm($form_state); $product_qty = array( 'rqpid' => isset($form_state['values']['rqpid']) ? $form_state['values']['rqpid'] : NULL, 'pfid' => isset($form_state['values']['pfid']) ? $form_state['values']['pfid'] : NULL, 'nid' => $form_state['values']['nid'], 'model' => $form_state['values']['model'], 'qty' => $form_state['values']['quantity'], 'lifetime' => $form_state['values']['lifetime'], ); $description = ''. t('SKU') .': '. (empty($product_qty['model']) ? t('Any') : $product_qty['model']) .'