| 1 |
<?php
|
| 2 |
// $Id: uc_out_of_stock.module,v 1.2.2.6 2009/01/07 13:46:06 hanoii Exp $
|
| 3 |
|
| 4 |
define ('UC_OUT_OF_STOCK_DEFAULT_HTML', t('<span style="color: red;">Out of stock</span>'));
|
| 5 |
|
| 6 |
function uc_out_of_stock_form_alter($form_id, &$form) {
|
| 7 |
$id = 'uc_product_add_to_cart_form';
|
| 8 |
if ( substr($form_id, 0, strlen($id)) == $id ) {
|
| 9 |
drupal_add_js(drupal_get_path('module', 'uc_out_of_stock') . '/uc_out_of_stock.js');
|
| 10 |
drupal_add_css(drupal_get_path('module', 'uc_out_of_stock') . '/uc_out_of_stock.css');
|
| 11 |
}
|
| 12 |
}
|
| 13 |
|
| 14 |
function uc_out_of_stock_menu($may_cache) {
|
| 15 |
$items = array();
|
| 16 |
if ($may_cache) {
|
| 17 |
|
| 18 |
}
|
| 19 |
else {
|
| 20 |
$items[] = array(
|
| 21 |
'path' => 'admin/store/settings/uc_out_of_stock',
|
| 22 |
'title' => t('Out of Stock Settings'),
|
| 23 |
'access' => user_access('administer store'),
|
| 24 |
'description' => t('Configure out of stock settings.'),
|
| 25 |
'callback' => 'drupal_get_form',
|
| 26 |
'callback arguments' => array('uc_out_of_stock_settings'),
|
| 27 |
'type' => MENU_NORMAL_ITEM,
|
| 28 |
);
|
| 29 |
|
| 30 |
$items[] = array(
|
| 31 |
'path' => 'uc_out_of_stock/query',
|
| 32 |
'title' => 'stock query',
|
| 33 |
'callback' => 'uc_out_of_stock_query',
|
| 34 |
'access' => true,
|
| 35 |
'type' => MENU_CALLBACK,
|
| 36 |
);
|
| 37 |
}
|
| 38 |
|
| 39 |
return $items;
|
| 40 |
}
|
| 41 |
|
| 42 |
function uc_out_of_stock_query() {
|
| 43 |
$attrs = array();
|
| 44 |
$response = array();
|
| 45 |
|
| 46 |
$nid = $_POST['nid'];
|
| 47 |
foreach ( $_POST as $key => $value ) {
|
| 48 |
if ( substr($key, 0, 4) == 'attr' ) {
|
| 49 |
$attrs[substr($key, 4)] = $value;
|
| 50 |
}
|
| 51 |
}
|
| 52 |
|
| 53 |
// if attributes module exists, and product has attributes first search for attributes
|
| 54 |
if (module_exists('uc_attribute') && count($attrs) && db_num_rows($result = db_query("SELECT * FROM {uc_product_adjustments} upa LEFT JOIN {uc_product_stock} ups ON ups.sku = upa.model WHERE upa.nid = %d", $nid)) > 0) {
|
| 55 |
while ($row = db_fetch_object($result)) {
|
| 56 |
$combination = unserialize($row->combination);
|
| 57 |
if ( $combination == $attrs ) {
|
| 58 |
$combination_found = TRUE;
|
| 59 |
// Only check if active
|
| 60 |
if ($row->active) {
|
| 61 |
$response['stock'] = $row->stock;
|
| 62 |
if ( $row->stock <= 0 ) {
|
| 63 |
$response['html'] = check_markup(variable_get('uc_out_of_stock_text', UC_OUT_OF_STOCK_DEFAULT_HTML), variable_get('uc_out_of_stock_format', FILTER_FORMAT_DEFAULT), FALSE);
|
| 64 |
}
|
| 65 |
}
|
| 66 |
}
|
| 67 |
}
|
| 68 |
|
| 69 |
// If the combination was not found, and all attributes were indeed selected, we are assuming that some
|
| 70 |
// combination shares an SKU with the actual product, thus, the product have to be queried as well.
|
| 71 |
if (!$combination_found) {
|
| 72 |
$query_main_sku = TRUE;
|
| 73 |
}
|
| 74 |
}
|
| 75 |
else {
|
| 76 |
$query_main_sku = TRUE;
|
| 77 |
}
|
| 78 |
|
| 79 |
if ($query_main_sku) {
|
| 80 |
// seach for main product
|
| 81 |
$result = db_query("SELECT * FROM {uc_products} up LEFT JOIN {uc_product_stock} ups ON ups.sku = up.model WHERE up.nid = %d AND ups.active = 1", $nid);
|
| 82 |
while ($row = db_fetch_object($result)) {
|
| 83 |
$response['stock'] = $row->stock;
|
| 84 |
if ( $row->stock <= 0 ) {
|
| 85 |
$response['html'] = check_markup(variable_get('uc_out_of_stock_text', UC_OUT_OF_STOCK_DEFAULT_HTML), variable_get('uc_out_of_stock_format', FILTER_FORMAT_DEFAULT), FALSE);
|
| 86 |
}
|
| 87 |
}
|
| 88 |
}
|
| 89 |
|
| 90 |
// if there is some response, print it
|
| 91 |
if (count($response)){
|
| 92 |
print implode('|', $response);
|
| 93 |
}
|
| 94 |
}
|
| 95 |
|
| 96 |
/**
|
| 97 |
* the settings form
|
| 98 |
*/
|
| 99 |
function uc_out_of_stock_settings_submit($form_id, $form_values) {
|
| 100 |
$op = isset($form_values['op']) ? $form_values['op'] : '';
|
| 101 |
|
| 102 |
if ($op == t('Reset to defaults')) {
|
| 103 |
variable_del('uc_out_of_stock_text');
|
| 104 |
variable_del('uc_out_of_stock_format');
|
| 105 |
}
|
| 106 |
else {
|
| 107 |
variable_set('uc_out_of_stock_text', $form_values['uc_out_of_stock_text']);
|
| 108 |
variable_set('uc_out_of_stock_format', $form_values['format']);
|
| 109 |
}
|
| 110 |
|
| 111 |
if ($op == t('Reset to defaults')) {
|
| 112 |
drupal_set_message(t('The configuration options have been reset to their default values.'));
|
| 113 |
}
|
| 114 |
else {
|
| 115 |
drupal_set_message(t('The configuration options have been saved.'));
|
| 116 |
}
|
| 117 |
|
| 118 |
menu_rebuild();
|
| 119 |
}
|
| 120 |
|
| 121 |
|
| 122 |
function uc_out_of_stock_settings() {
|
| 123 |
$text = check_markup(variable_get('uc_out_of_stock_text', UC_OUT_OF_STOCK_DEFAULT_HTML), variable_get('uc_out_of_stock_format', FILTER_FORMAT_DEFAULT), FALSE);
|
| 124 |
$description = '<div class="description">This is the value below rendered as you would expect to see it</div>';
|
| 125 |
$text = '<div style="border: 1px solid lightgrey; padding: 10px;">' . $text . '</div>' . $description;
|
| 126 |
|
| 127 |
$form['uc_out_of_stock_demo'] = array(
|
| 128 |
'#type' => 'markup',
|
| 129 |
'#value' => $text,
|
| 130 |
);
|
| 131 |
|
| 132 |
$form['uc_out_of_stock_text'] = array(
|
| 133 |
'#type' => 'textarea',
|
| 134 |
'#title' => t('Out of stock replacement HTML'),
|
| 135 |
'#default_value' => variable_get('uc_out_of_stock_text', UC_OUT_OF_STOCK_DEFAULT_HTML),
|
| 136 |
'#description' => t('The HTML that will replace the Add To Cart button if no stock is available.'),
|
| 137 |
);
|
| 138 |
|
| 139 |
$form['uc_out_of_stock_format'] = filter_form(variable_get('uc_out_of_stock_format', FILTER_FORMAT_DEFAULT));
|
| 140 |
|
| 141 |
$form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration') );
|
| 142 |
$form['buttons']['reset'] = array('#type' => 'submit', '#value' => t('Reset to defaults') );
|
| 143 |
|
| 144 |
return $form;
|
| 145 |
}
|