diff -urp ubercart-2.x-dev-20110829/uc_attribute/uc_attribute.admin.inc ubercart-2.x-dev-20110829--298395/uc_attribute/uc_attribute.admin.inc --- ubercart-2.x-dev-20110829/uc_attribute/uc_attribute.admin.inc 2011-08-28 15:08:32.000000000 -0400 +++ ubercart-2.x-dev-20110829--298395/uc_attribute/uc_attribute.admin.inc 2011-09-08 18:42:29.000000000 -0400 @@ -186,6 +186,64 @@ function uc_attribute_delete_confirm_sub } } +function uc_attribute_bulk_update_form($form_state, $object) { + drupal_set_title(check_plain($object->name)); + $form = array(); + + $node_type = $object->pcid; + + $form['node_type'] = array( + '#type' => 'hidden', + '#value' => $node_type, + ); + + // Select all products with current class. + $result = pager_query("SELECT n.nid, n.title, n.status, nt.name FROM {node} n LEFT JOIN {node_type} nt ON nt.type = n.type WHERE nt.type = '%s'", 50, 0, NULL, $node_type); + $nodes = array(); + while ($node = db_fetch_object($result)) { + $nodes[$node->nid] = ''; + $form['title'][$node->nid] = array('#value' => l($node->title, 'node/' . $node->nid)); + $form['status'][$node->nid] = array('#value' => ($node->status ? t('published') : t('not published'))); + $form['type'][$node->nid] = array('#value' => check_plain($node->name)); + } + + $form['nodes'] = array('#type' => 'checkboxes', '#options' => $nodes); + + $form['flush'] = array( + '#type' => 'submit', + '#value' => t('Flush selected'), + '#submit' => array('uc_attribute_bulk_update_flush'), + ); + $form['flush_all'] = array( + '#type' => 'submit', + '#value' => t('Flush all'), + '#submit' => array('uc_attribute_bulk_update_flush_all'), + ); + + $form['pager'] = array('#value' => theme('pager', NULL, 50, 0)); + $form['#theme'] = 'uc_attribute_bulk_update'; + + return $form; +} + +function uc_attribute_bulk_update_flush($form, &$form_state) { + $nodes = array_filter($form_state['values']['nodes']); + foreach ($nodes as $nid) { + $node = node_load($nid); + uc_attribute_reset($node); + } + drupal_set_message(t("Bulk update completed successfully on selected products.")); +} + +function uc_attribute_bulk_update_flush_all($form, &$form_state) { + $result = db_query("SELECT nid FROM {node} WHERE type = '%s'", $form_state['values']['node_type']); + while ($item = db_fetch_object($result)) { + $node = node_load($item->nid); + uc_attribute_reset($node); + } + drupal_set_message(t("Bulk update completed successfully on all products in this class.")); +} + /** * Change the display of attribute option prices. * @@ -410,7 +468,15 @@ function uc_attribute_option_form($form_ '#default_value' => $option->weight, '#weight' => 3, ); - + if (isset($form['oid'])) { + $form['bulk_update'] = array( + '#type' => 'checkbox', + '#title' => t('Update existing products?'), + '#description' => t('If selected, existing products and product classes with this attribute will be updated with the values on this page.
Warning: any option adjustments set at the product level will be overridden!'), + '#default_value' => 0, + '#weight' => 4, + ); + } $form['submit'] = array( '#type' => 'submit', '#value' => t('Submit'), @@ -455,8 +521,16 @@ function uc_attribute_option_form_submit else { db_query("UPDATE {uc_attribute_options} SET name = '%s', cost = %f, price = %f, weight = %f, ordering = %d WHERE aid = %d AND oid = %d", $form_state['values']['name'], $form_state['values']['cost'], $form_state['values']['price'], $form_state['values']['weight'], $form_state['values']['ordering'], $form_state['values']['aid'], $form_state['values']['oid']); - drupal_set_message(t('Updated option %option.', array('%option' => $form_state['values']['name']))); - watchdog('uc_attribute', 'Updated option %option.', array('%option' => $form_state['values']['name']), WATCHDOG_NOTICE, 'admin/store/attributes/'. $form_state['values']['aid'] .'/options/'. $form_state['values']['oid']); + $message = t('Updated option %option.'); + if ($form_state['values']['bulk_update'] == 1) { + db_query("UPDATE {uc_product_options} SET cost = '%f', price = '%f', weight = '%f', ordering = '%d' WHERE oid = '%d'", + $form_state['values']['cost'], $form_state['values']['price'], $form_state['values']['weight'], $form_state['values']['ordering'], $form_state['values']['oid']); + db_query("UPDATE {uc_class_attribute_options} SET cost = '%f', price = '%f', weight = '%f', ordering = '%d' WHERE oid = '%d'", + $form_state['values']['cost'], $form_state['values']['price'], $form_state['values']['weight'], $form_state['values']['ordering'], $form_state['values']['oid']); + $message .= t(' Bulk updates applied to all products and classes which have this attribute.'); + } + drupal_set_message(t($message, array('%option' => $form_state['values']['name']))); + watchdog('uc_attribute', $message, array('%option' => $form_state['values']['name']), WATCHDOG_NOTICE, 'admin/store/attributes/'. $form_state['values']['aid'] .'/options/'. $form_state['values']['oid']); $form_state['redirect'] = 'admin/store/attributes/'. $form_state['values']['aid'] .'/options'; } } @@ -593,6 +667,14 @@ function uc_object_attributes_form($form '#value' => t('Save changes'), '#weight' => -2, ); + if ($type == 'product') { + $form['reset'] = array( + '#type' => 'submit', + '#value' => t('Reset to defaults'), + '#submit' => array('uc_attribute_product_attributes_reset'), + '#weight' => -1, + ); + } } } elseif ($view == 'add') { @@ -769,6 +851,18 @@ function uc_object_attributes_form_submi } /** + * Reset product attributes to defaults for product class. + * + * @see uc_object_attributes_form() + */ +function uc_attribute_product_attributes_reset($form, &$form_state) { + $nid = $form_state['values']['id']; + $node = node_load($nid); + uc_attribute_reset($node); + drupal_set_message(t('Product attributes and options reset to class defaults.')); +} + +/** * Form to assign and modify attribute options on products or classes. * * @ingroup forms @@ -1219,3 +1313,39 @@ function uc_product_adjustments_form_sub } $form_state['redirect'] = $goto; } + +function theme_uc_attribute_bulk_update($form) { + $output = ''; + + $has_products = isset($form['title']) && is_array($form['title']); + + $header[] = theme('table_select_header_cell'); + $header[] = t('Title'); + $header[] = t('Status'); + $header[] = t('Type'); + + if ($has_products) { + foreach (element_children($form['title']) as $key) { + $row = array(); + $row[] = drupal_render($form['nodes'][$key]); + $row[] = drupal_render($form['title'][$key]); + $row[] = drupal_render($form['status'][$key]); + $row[] = drupal_render($form['type'][$key]); + $rows[] = $row; + } + } + else { + $rows[] = array(array('data' => t('No products available.'), 'colspan' => '6')); + } + + $output .= theme('table', $header, $rows); + if ($form['pager']['#value']) { + $output .= drupal_render($form['pager']); + } + + $output .= drupal_render($form); + + $output .= '

' . t('Flush actions take place immediately and cannot be undone.') . '

'; + + return $output; +} diff -urp ubercart-2.x-dev-20110829/uc_attribute/uc_attribute.module ubercart-2.x-dev-20110829--298395/uc_attribute/uc_attribute.module --- ubercart-2.x-dev-20110829/uc_attribute/uc_attribute.module 2011-08-28 15:08:32.000000000 -0400 +++ ubercart-2.x-dev-20110829--298395/uc_attribute/uc_attribute.module 2011-09-06 01:56:37.000000000 -0400 @@ -42,6 +42,10 @@ function uc_attribute_help($path, $arg) // Help message for the product Adjustments tab. case 'node/%/edit/adjustments': return t('Enter an alternate SKU to be used when the specified set of options are chosen and the product is added to the cart. Warning: Adding or removing attributes from this product will reset all the SKUs on this page to the default product SKU.'); + + // Help message for bulk updates tab. + case 'admin/store/products/classes/%/attributes_bulk': + return t('Bulk updates performed here will reset all attributes and options on the selected products to the current defaults for this product class.

Warning: any product level customizations will be lost!

To perform bulk updates on option values for all products, go to Store administration->Attributes and edit the option, selecting the checkbox to update existing products.', array('!url' => url('admin/store/attributes'))); } } @@ -155,6 +159,15 @@ function uc_attribute_menu() { 'weight' => 2, 'file' => 'uc_attribute.admin.inc', ); + $items['admin/store/products/classes/%uc_product_class/attributes_bulk'] = array( + 'title' => 'Bulk', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('uc_attribute_bulk_update_form', 4, 'class'), + 'access callback' => 'uc_attribute_product_class_access', + 'type' => MENU_LOCAL_TASK, + 'weight' => 3, + 'file' => 'uc_attribute.admin.inc', + ); // Insert subitems into the edit node page for product types. $items['node/%node/edit/attributes'] = array( @@ -268,6 +281,10 @@ function uc_attribute_theme() { 'arguments' => array('product' => NULL), 'file' => 'uc_attribute.admin.inc', ), + 'uc_attribute_bulk_update' => array( + 'arguments' => array('form' => NULL), + 'file' => 'uc_attribute.admin.inc', + ), ); } @@ -346,23 +363,10 @@ function uc_attribute_nodeapi(&$node, $o } break; case 'insert': - switch ($GLOBALS['db_type']) { - case 'mysqli': - case 'mysql': - db_query("INSERT IGNORE INTO {uc_product_attributes} (nid, aid, label, ordering, required, display, default_option) SELECT %d, aid, label, ordering, required, display, default_option FROM {uc_class_attributes} WHERE pcid = '%s'", $node->nid, $node->type); - db_query("INSERT IGNORE INTO {uc_product_options} (nid, oid, cost, price, weight, ordering) SELECT %d, oid, cost, price, weight, ordering FROM {uc_class_attribute_options} WHERE pcid = '%s'", $node->nid, $node->type); - break; - case 'pgsql': - db_query("INSERT INTO {uc_product_attributes} (nid, aid, label, ordering, required, display, default_option) SELECT %d, aid, label, ordering, required, display, default_option FROM {uc_class_attributes} WHERE pcid = '%s'", $node->nid, $node->type); - db_query("INSERT INTO {uc_product_options} (nid, oid, cost, price, weight, ordering) SELECT %d, oid, cost, price, weight, ordering FROM {uc_class_attribute_options} WHERE pcid = '%s'", $node->nid, $node->type); - break; - } - + uc_attribute_insert($node); break; case 'delete': - db_query("DELETE FROM {uc_product_options} WHERE nid = %d", $node->nid); - db_query("DELETE FROM {uc_product_adjustments} WHERE nid = %d", $node->nid); - db_query("DELETE FROM {uc_product_attributes} WHERE nid = %d", $node->nid); + uc_attribute_remove($node); break; case 'update index': $output = ''; @@ -383,6 +387,40 @@ function uc_attribute_nodeapi(&$node, $o } } +/** + * Add default attributes to a product node. + */ +function uc_attribute_insert($node) { + switch ($GLOBALS['db_type']) { + case 'mysqli': + case 'mysql': + db_query("INSERT IGNORE INTO {uc_product_attributes} (nid, aid, label, ordering, required, display, default_option) SELECT %d, aid, label, ordering, required, display, default_option FROM {uc_class_attributes} WHERE pcid = '%s'", $node->nid, $node->type); + db_query("INSERT IGNORE INTO {uc_product_options} (nid, oid, cost, price, weight, ordering) SELECT %d, oid, cost, price, weight, ordering FROM {uc_class_attribute_options} WHERE pcid = '%s'", $node->nid, $node->type); + break; + case 'pgsql': + db_query("INSERT INTO {uc_product_attributes} (nid, aid, label, ordering, required, display, default_option) SELECT %d, aid, label, ordering, required, display, default_option FROM {uc_class_attributes} WHERE pcid = '%s'", $node->nid, $node->type); + db_query("INSERT INTO {uc_product_options} (nid, oid, cost, price, weight, ordering) SELECT %d, oid, cost, price, weight, ordering FROM {uc_class_attribute_options} WHERE pcid = '%s'", $node->nid, $node->type); + break; + } +} + +/** + * Remove all attributes from a product node. + */ +function uc_attribute_remove($node) { + db_query("DELETE FROM {uc_product_options} WHERE nid = %d", $node->nid); + db_query("DELETE FROM {uc_product_adjustments} WHERE nid = %d", $node->nid); + db_query("DELETE FROM {uc_product_attributes} WHERE nid = %d", $node->nid); +} + +/** + * Reset attributes to class defaults for a product node. + */ +function uc_attribute_reset($node) { + uc_attribute_remove($node); + uc_attribute_insert($node); +} + /****************************************************************************** * Ubercart Hooks * ******************************************************************************/