Skip navigation
Help

uc_stock_edit_form

6 uc_stock.admin.inc uc_stock_edit_form($form_state, $node)
7 uc_stock.admin.inc uc_stock_edit_form($form, &$form_state, $node)

Form builder for product stock edit form.

See also

uc_stock_edit_form_submit()

theme_uc_stock_edit_form()

Related topics

2 string references to 'uc_stock_edit_form'

File

ubercart/uc_stock/uc_stock.admin.inc, line 193
Stock administration menu items.

Code

function uc_stock_edit_form($form, &$form_state, $node) {
  drupal_set_title($node->title);

  $form['stock'] = array('#tree' => TRUE);

  $skus = uc_product_get_models($node->nid);

  // Remove 'Any'.
  unset($skus[NULL]);

  if (!$skus) {
    drupal_set_message(t('No SKU found.'), 'error');
  }
  else {
    foreach (array_values($skus) as $id => $sku) {
      $stock = db_query("SELECT * FROM {uc_product_stock} WHERE sku = :sku", array(':sku' => $sku))->fetchAssoc();

      $form['stock'][$id]['sku'] = array(
        '#type' => 'value', 
        '#value' => $sku,
      );

      // Checkbox to mark this as active.
      $form['stock'][$id]['active'] = array(
        '#type' => 'checkbox', 
        '#default_value' => !empty($stock['active']) ? $stock['active'] : 0,
      );

      // Sanitized version of the SKU for display.
      $form['stock'][$id]['display_sku'] = array(
        '#markup' => check_plain($sku),
      );

      // Textfield for entering the stock level.
      $form['stock'][$id]['stock'] = array(
        '#type' => 'textfield', 
        '#default_value' => !empty($stock['stock']) ? $stock['stock'] : 0, 
        '#maxlength' => 9, 
        '#size' => 9,
      );

      // Textfield for entering the threshold level.
      $form['stock'][$id]['threshold'] = array(
        '#type' => 'textfield', 
        '#default_value' => !empty($stock['threshold']) ? $stock['threshold'] : 0, 
        '#maxlength' => 9, 
        '#size' => 9,
      );
    }
  }

  $form['nid'] = array(
    '#type' => 'value', 
    '#value' => $node->nid,
  );

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['save'] = array(
    '#type' => 'submit', 
    '#value' => t('Save changes'),
  );

  return $form;
}