You are here

function uc_product_features in Ubercart 5

Same name and namespace in other branches
  1. 6.2 uc_product/uc_product.admin.inc \uc_product_features()
  2. 7.3 uc_product/uc_product.admin.inc \uc_product_features()

Displays the project features tab on a product node edit form.

1 string reference to 'uc_product_features'
uc_product_menu in uc_product/uc_product.module
Implementation of hook_menu().

File

uc_product/uc_product.module, line 2617
The product module for Ubercart.

Code

function uc_product_features($node) {
  drupal_set_title(check_plain($node->title));
  if (arg(4)) {

    // First check to see if we're trying to remove a feature.
    if (intval(arg(5)) > 0 && arg(6) == 'delete') {
      $result = db_query("SELECT * FROM {uc_product_features} WHERE pfid = %d AND fid = '%s'", intval(arg(5)), arg(4));
      if ($feature = db_fetch_array($result)) {

        // If the user confirmed the delete, process it!
        if ($_POST['pf_delete']) {

          // Call the delete function for this product feature if it exists.
          $func = uc_product_feature_data($feature['fid'], 'delete');
          if (function_exists($func)) {
            $func($feature);
          }

          // Remove the product feature data from the database.
          db_query("DELETE FROM {uc_product_features} WHERE pfid = %d", intval(arg(5)));
          drupal_set_message(t('The product feature has been deleted.'));
          drupal_goto('node/' . arg(1) . '/edit/features');
        }

        // Show the confirmation form for deleting this feature.
        $question = $node->title;
        $description = t('Are you sure you wish to delete this %feature?', array(
          '%feature' => uc_product_feature_data($feature['fid'], 'title'),
        )) . '<div><b>' . t('Description') . ':</b><br />' . $feature['description'] . '</div><br />';
        return drupal_get_form('confirm_form', NULL, $question, 'node/' . arg(1) . '/edit/features', $description, t('Delete'), t('Cancel'), 'pf_delete');
      }
      else {
        drupal_set_message(t("That product feature doesn't exist."), 'error');
        drupal_goto('node/' . arg(1) . '/edit/features');
      }
    }

    // Handle adding or editing product features.
    $func = uc_product_feature_data(arg(4), 'callback');
    if (function_exists($func)) {
      if (arg(5) == 'add') {
        $output = drupal_get_form($func, $node, array());
      }
      elseif (intval(arg(5)) > 0) {
        $result = db_query("SELECT * FROM {uc_product_features} WHERE pfid = %d AND fid = '%s'", intval(arg(5)), arg(4));
        if ($feature = db_fetch_array($result)) {
          $output = drupal_get_form($func, $node, $feature);
        }
      }
    }
    else {
      drupal_set_message(t('Error: Attempted to add a non-existent product feature type.'), 'error');
      drupal_goto('node/' . $node->nid . '/edit/features');
    }
    if (empty($output)) {
      drupal_set_message(t('Error: No form data was returned for that operation.'), 'error');
      drupal_goto('node/' . $node->nid . '/edit/features');
    }
    return $output;
  }
  $header = array(
    t('Type'),
    t('Description'),
    t('Operations'),
  );
  $result = db_query("SELECT * FROM {uc_product_features} WHERE nid = %d ORDER BY pfid ASC", $node->nid);
  while ($feature = db_fetch_object($result)) {
    $operations = array(
      l(t('edit'), 'node/' . $node->nid . '/edit/features/' . $feature->fid . '/' . $feature->pfid),
      l(t('delete'), 'node/' . $node->nid . '/edit/features/' . $feature->fid . '/' . $feature->pfid . '/delete'),
    );
    $rows[] = array(
      'data' => array(
        array(
          'data' => uc_product_feature_data($feature->fid, 'title'),
          'nowrap' => 'nowrap',
        ),
        array(
          'data' => $feature->description,
          'width' => '100%',
        ),
        array(
          'data' => implode(' ', $operations),
          'nowrap' => 'nowrap',
        ),
      ),
      'valign' => 'top',
    );
  }
  if (empty($rows)) {
    $rows[] = array(
      array(
        'data' => t('No features found for this product.'),
        'colspan' => 3,
      ),
    );
  }
  $output = theme('table', $header, $rows) . drupal_get_form('uc_product_feature_add_form');
  return $output;
}