You are here

function uc_shipping_package_edit in Ubercart 7.3

Same name and namespace in other branches
  1. 5 shipping/uc_shipping/uc_shipping.module \uc_shipping_package_edit()
  2. 6.2 shipping/uc_shipping/uc_shipping.admin.inc \uc_shipping_package_edit()

Rearranges the products in or out of a package.

See also

uc_shipping_package_edit_submit()

theme_uc_shipping_edit_package_fieldset()

1 string reference to 'uc_shipping_package_edit'
uc_shipping_menu in shipping/uc_shipping/uc_shipping.module
Implements hook_menu().

File

shipping/uc_shipping/uc_shipping.admin.inc, line 312
Shipping administration menu items.

Code

function uc_shipping_package_edit($form, &$form_state, $order, $package) {
  $products = array();
  $shipping_types_products = array();
  foreach ($order->products as $product) {
    if ($product->data['shippable']) {
      $product->shipping_type = uc_product_get_shipping_type($product);
      $shipping_types_products[$product->shipping_type][$product->order_product_id] = $product;
      $products[$product->order_product_id] = $product;
    }
  }
  $result = db_query("SELECT order_product_id, SUM(qty) AS quantity FROM {uc_packaged_products} pp LEFT JOIN {uc_packages} p ON pp.package_id = p.package_id WHERE p.order_id = :id GROUP BY order_product_id", array(
    ':id' => $order->order_id,
  ));
  foreach ($result as $packaged_product) {

    // Make already packaged products unavailable, except those in this package.
    $products[$packaged_product->order_product_id]->qty -= $packaged_product->quantity;
    if (isset($package->products[$packaged_product->order_product_id])) {
      $products[$packaged_product->order_product_id]->qty += $package->products[$packaged_product->order_product_id]->qty;
    }
  }
  $form['#tree'] = TRUE;
  $form['package_id'] = array(
    '#type' => 'hidden',
    '#value' => $package->package_id,
  );
  $form['products'] = array();
  foreach ($products as $product) {
    if ($product->qty > 0) {
      $product_row = array();
      $product_row['checked'] = array(
        '#type' => 'checkbox',
        '#default_value' => isset($package->products[$product->order_product_id]),
      );
      $product_row['model'] = array(
        '#markup' => check_plain($product->model),
      );
      $product_row['name'] = array(
        '#markup' => filter_xss_admin($product->title),
      );
      $product_row['qty'] = array(
        '#type' => 'select',
        '#options' => drupal_map_assoc(range(1, $product->qty)),
        '#default_value' => isset($package->products[$product->order_product_id]) ? $package->products[$product->order_product_id]->qty : 1,
      );
      $form['products'][$product->order_product_id] = $product_row;
    }
  }
  $form['products']['#theme'] = 'uc_shipping_edit_package_fieldset';
  $options = array();
  $shipping_type_options = uc_quote_shipping_type_options();
  foreach (array_keys($shipping_types_products) as $type) {
    $options[$type] = isset($shipping_type_options[$type]) ? $shipping_type_options[$type] : ucwords(str_replace('_', ' ', $type));
  }
  $form['shipping_type'] = array(
    '#type' => 'select',
    '#title' => t('Shipping type'),
    '#options' => $options,
    '#default_value' => $package->shipping_type,
  );
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}