Index: product.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/ecommerce/product/product.module,v retrieving revision 1.110 diff -u -u -F^f -r1.110 product.module --- product.module 18 May 2006 12:42:24 -0000 1.110 +++ product.module 26 May 2006 22:09:01 -0000 @@ -303,7 +303,7 @@ function product_menu($may_cache) { 'path' => 'node/'. arg(1) .'/product', 'title' => t('product'), 'callback' => 'product_to_product', - 'access' => user_access('administer products'), + 'access' => user_access('nobody'), // XXX product tab to be deleted 'type' => MENU_LOCAL_TASK, 'weight' => 2 ); @@ -311,7 +311,7 @@ function product_menu($may_cache) { 'path' => 'node/'. arg(1) .'/product/delete', 'title' => t('delete product'), 'callback' => 'product_to_product_delete', - 'access' => user_access('administer products'), + 'access' => user_access('nobody'), // XXX product tab to be deleted 'type' => MENU_CALLBACK, ); } @@ -340,13 +340,87 @@ function product_node_name($node) { */ function product_form_alter($form_id, &$form) { if (isset($form['type']) && $form_id == $form['type']['#value'] .'_node_form') { + $ntype = $form['type']['#value']; + $node = $form['#node']; foreach (array('ptype') as $key) { $form[$key] = array('#type' => 'value', '#value' => $form['#node']->$key); } + + if (user_access('administer products') && $ntype != 'product') { + $ptype = null; + if ($_POST['edit']['product_type']) + $ptype = $_POST['edit']['product_type']; + else if ($form['ptype']['#value']) + $ptype = $form['ptype']['#value']; + else + $ptype = $form['#node']->ptype; + + if ($_POST['product_op'] == t('Remove product')) { + $ptype = null; + $_POST['edit']['product_type'] = null; + // data will be deleted on submit. + } + + $form['ptype'] = array('#type' => 'value', + '#value' => $ptype); + + $form['product_transform'] = + array('#type' => 'fieldset', + '#title' => t('Product'), + '#collapsible' => true, + '#collapsed' => false, + ); + + if (!$ptype) { + $form['product_transform']['#collapsed'] = true; + // Choose a product type + $options = array_merge(array('not_a_product' => t('not a product')), + product_get_ptypes()); + $form['product_transform']['product_type'] = + array('#type' => 'radios', + '#title' => t('Product type'), + '#multiple' => TRUE, + '#options' => $options, + '#default_value' => $ptype ? $ptype:'not_a_product', + '#description' => t('Select a product type to add this item to the store.'), + ); + $form['product_transform']['product_op'] = + array('#type' => 'button', + '#value' => t('Add to store'), + '#name' => 'product_op'); + } + else { // ptype is known. + $form['product_transform']['content'] = + _product_transform_node_form($node, $ptype); + $form['product_transform']['product_op'] = + array('#type' => 'button', + '#value' => t('Remove product'), + '#name' => 'product_op'); + + } + } } } /** + * Build the form fields for product details. + * + * The returned form may be included in node edit form, or product tab. + */ +function _product_transform_node_form($node, $ptype) { + // prime the form + $form = product_base_form_elements($node); + + $form['ptype'] = array('#type' => 'value', '#value' => $ptype); + $form['product_type'] = array('#type' => 'hidden', '#value' => $ptype); + // fields specific to this product type + $form = array_merge($form, (array)module_invoke($ptype, 'productapi', $node, 'form')); + + return $form; +} + + +/** * Implementation of hook_nodeapi(). * * Provide product functionality for nodes that are not products @@ -368,17 +442,13 @@ function product_nodeapi(&$node, $op, $a case 'insert': case 'update': - // copy the product to the next revision. - if ($node->type != 'product' && $node->revision) { - $last_vid = db_result(db_query('SELECT p.vid AS pvid FROM {node_revisions} r LEFT JOIN {ec_product} p ON r.vid = p.vid WHERE r.nid = %d AND r.vid <> %d ORDER BY r.vid DESC LIMIT 1', $node->nid, $node->vid)); - if ($last_vid) { - $product->nid = $node->nid; - $product->vid = $last_vid; - if ($product = product_load($product)) { - $product->vid = $node->vid; - product_save($product); - } - } + if ($node->ptype && + $node->ptype != 'not_a_product') { + product_save($node); + } + else { + // delete info for nodes that were products, but are not anymore + product_delete($node); } break; @@ -394,6 +464,7 @@ function product_nodeapi(&$node, $op, $a product_delete($node); } break; + } }