diff --git uc_attribute.admin.inc uc_attribute.admin.inc index 243e10a..878e8b6 100644 --- uc_attribute.admin.inc +++ uc_attribute.admin.inc @@ -174,6 +174,58 @@ function uc_attribute_delete_confirm_submit($form, &$form_state) { } } +function uc_bulk_attributes_form($form_state, $object) +{ + $form = array(); + // select all products with current class + $node_type = $object->pcid; + + $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['submit'] = array( + '#type' => 'submit', + '#value' => t('Flush attributes'), + '#submit' => array('uc_bulk_attributes_flush'), + ); + $form['pager'] = array('#value' => theme('pager', NULL, 50, 0)); + $form['#theme'] = 'uc_bulk_attributes'; + + return $form; +} + +function uc_bulk_attributes_flush($form, &$form_state) { + $nodes = array_filter($form_state['values']['nodes']); + foreach($nodes as $nid) { + $node = node_load($nid); + // DELETE all attributes and options + 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); + //Iterate through nodes and add class attributes and options + + 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; + } + } + drupal_set_message(t("Bulk update completed successfully")); +} + /** * Change the display of attribute option prices. * @@ -1187,3 +1239,38 @@ function uc_product_adjustments_form_submit($form, &$form_state) { $form_state['redirect'] = $goto; } +function theme_uc_bulk_attributes($form) { + + $output = ''; + + $has_posts = isset($form['title']) && is_array($form['title']); + + $header[] = theme('table_select_header_cell'); + $header[] = t('Title'); + $header[] = t('Status'); + $header[] = t('Type'); + + if ($has_posts) { + 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 posts available.'), 'colspan' => '6')); + } + + $output .= theme('table', $header, $rows); + if ($form['pager']['#value']) { + $output .= drupal_render($form['pager']); + } + + $output .= drupal_render($form); + + return $output; +} diff --git uc_attribute.module uc_attribute.module index 3e301a0..bd04da7 100644 --- uc_attribute.module +++ uc_attribute.module @@ -156,6 +156,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_bulk_attributes_form', 4), + '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( @@ -269,6 +278,10 @@ function uc_attribute_theme() { 'arguments' => array('product' => NULL), 'file' => 'uc_attribute.admin.inc', ), + 'uc_bulk_attributes' => array( + 'arguments' => array('form' => NULL), + 'file' => 'uc_attribute.admin.inc', + ), ); }