Index: product.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/ecommerce/product/product.module,v retrieving revision 1.115 diff -u -F^f -r1.115 product.module --- product.module 31 May 2006 05:11:27 -0000 1.115 +++ product.module 31 May 2006 15:56:57 -0000 @@ -291,28 +291,8 @@ function product_menu($may_cache) { 'type' => MENU_CALLBACK ); } - if (arg(0) == 'node' && is_numeric(arg(1)) && user_access('administer products')) { - // Only add the product-tab for non-product pages: - if (db_result(db_query(db_rewrite_sql("SELECT COUNT(n.nid) FROM {node} n WHERE n.nid = %d AND n.type != 'product'"), arg(1))) > 0) { - $items[] = array( - 'path' => 'node/'. arg(1) .'/product', - 'title' => t('product'), - 'callback' => 'product_to_product', - 'access' => user_access('administer products'), - 'type' => MENU_LOCAL_TASK, - 'weight' => 2 - ); - $items[] = array( - 'path' => 'node/'. arg(1) .'/product/delete', - 'title' => t('delete product'), - 'callback' => 'product_to_product_delete', - 'access' => user_access('administer products'), - 'type' => MENU_CALLBACK, - ); - } - } } - + return $items; } @@ -337,6 +317,76 @@ function product_form_alter($form_id, &$ if (isset($form['type'])) { if ($form_id == $form['type']['#value'] .'_node_form') { $form['ptype'] = array('#type' => 'value', '#value' => $form['#node']->ptype); + $ntype = $form['type']['#value']; + $node = $form['#node']; + if (user_access('administer products') && $ntype != 'product') { + $ptype = null; + // determine product type. + if ($_POST['edit']['product_type']) + $ptype = $_POST['edit']['product_type']; + else if ($form['ptype']['#value']) + $ptype = $form['ptype']['#value']; + else + $ptype = $form['#node']->ptype; + + $form['ptype'] = array('#type' => 'value', + '#value' => $ptype); + + if (!$ptype || $ptype == 'not_a_product') { + // choose product type from collapsed fieldset + $form['product_transform'] = + array('#type' => 'fieldset', + '#title' => t('Product'), + '#collapsible' => true, + '#collapsed' => true, + ); + + // Choose a product type + $ptypes['not_a_product'] = array('name' => t('not a product'), + 'help' => t('This item not for sale.')); + foreach (product_get_ptypes() as $key => $value) { + $ptypes[$key] = + array('name' => $value, + 'help' => implode("\n", module_invoke_all('help', 'node/add/product#'. $key))); + } + $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' => NULL, + '#default_value' => $ptype ? $ptype:'not_a_product', + '#description' => t('Select from the product types above to add this item to the store.'), + ); + // display each radio option, with descriptive text + foreach ($ptypes as $key => $data) { + $form['product_transform']['product_type'][$key] = + array('#type' => 'radio', + '#title' => $data['name'], + '#return_value' => $key, + '#default_value' => $ptype ? $ptype:'not_a_product', + '#description' => $data['help'], + '#parents' => array('product_type'), + ); + } + + $form['product_transform']['product_op'] = + array('#type' => 'button', + '#value' => t('Add to store'), + '#name' => 'product_op'); + } + else { // ptype is known. Display product fields (not in fieldset) + $form['product_transform']['content'] = + _product_transform_node_form($node, $ptype); + // include a check box to make this node not a product + $form['product_transform']['product_not_a_product'] = + array('#type' => 'checkbox', + '#title' => t('Remove from store'), + '#description' => t('Check here to delete product information. Takes effect when changes are submitted.')); + } + } + } // Add a fieldset with links for configuring individual product types. else if ($form_id == $form['type']['#value'] .'_node_settings') { @@ -352,6 +402,24 @@ function product_form_alter($form_id, &$ } /** + * 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; +} + + +/** * Menu callback; presents each product type configuration page. */ function product_types_configure() { @@ -403,23 +471,20 @@ 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->product_not_a_product) { + // user has checked 'remove this item from store'. + product_delete($node); + } + else if ($node->ptype && + $node->ptype != 'not_a_product') { + product_save($node); } break; case 'view': /* If we have a pseudo product, add some product specific theming to it. */ - if ($node->type != 'product' && $node->ptype) { + if ($node->type != 'product' && $node->ptype && + !$node->product_not_a_product) { $node = theme('node_product', $node, 0, 1); } break; @@ -429,6 +494,7 @@ function product_nodeapi(&$node, $op, $a product_delete($node); } break; + } } @@ -670,98 +736,6 @@ function product_page() { return theme('product_view_collection'); } -/** - * Handles all product operations for non-product nodes. - */ -function product_to_product() { - $op = $_POST['op']; - $node = node_load(arg(1)); - - if ($node->nid) { - drupal_set_title(t('%node-title (product)', array('%node-title' => $node->title))); - $product = db_fetch_object(db_query('SELECT * FROM {ec_product} WHERE vid = %d', $node->vid)); - $ptypes = product_get_ptypes(); - if ($product || (arg(3) && in_array(arg(3), array_keys($ptypes)))) { - $node->ptype = $node->ptype ? $node->ptype : arg(3); - $form = product_base_form_elements($node); - $form['#node'] = $node; - foreach (array('nid', 'vid', 'ptype') as $key) { - $form[$key] = array('#type' => 'value', '#value' => $node->$key); - } - - $form = array_merge($form, (array)module_invoke($node->ptype, 'productapi', $node, 'form')); - if ($product) { - $form['update'] = array('#type' => 'submit', '#value' => t('Update product'), '#weight' => 40); - $form['remove'] = array('#type' => 'submit', '#value' => t('Remove product'), '#weight' => 45); - } - else { - $form['create'] = array('#type' => 'submit', '#value' => t('Create product'), '#weight' => 40); - } - return drupal_get_form('product_to_product', $form); - } - else { - return product_types_listing("node/$node->nid/product"); - } - } -} - -function product_to_product_validate($form_id, $form_values) { - $op = $_POST['op']; - $edit = (object) $form_values; - - switch ($op) { - case t('Create product'): - case t('Update product'): - product_form_validate($edit); - break; - } -} - -function product_to_product_submit($form_id, $form_values) { - $op = $_POST['op']; - $edit = (object) $form_values; - - switch ($op) { - case t('Create product'): - case t('Update product'): - product_save($edit); - drupal_set_message(t('The product has been saved.')); - return "node/$edit->nid"; - break; - case t('Remove product'): - //product_delete($edit, true); - //drupal_set_message(t('Removed the post from the product listings.')); - return "node/$edit->nid/product/delete"; - break; - } -} - -function product_to_product_delete() { - $node = node_load(arg(1)); - - $form['node'] = array('#type' => 'value', '#value' => $node); - - $output.= confirm_form( - 'product_to_product_delete', - $form, - t('Are you sure that you want to delete the product information for %title', array('%title' => $node->title)), - "node/{$node->nid}/product", - t('The product information will be permenantly removed.'), - t('Delete Product'), - t('Cancel') - ); - return $output; -} - -function product_to_product_delete_submit($form_id, $form_values) { - if ($form_values['confirm']) { - product_delete($form_values['node'], true); - drupal_set_message(t('Removed the post from the product listings.')); - return "node/{$form_values['node']->nid}"; - } - return "node/{$form_values['node']->nid}/product"; -} - function product_elements() { $items['product_price'] = array('#input' => true, '#validate' => array('valid_product_price' => array()));