You are here

function uc_object_options_form in Ubercart 6.2

Same name and namespace in other branches
  1. 5 uc_attribute/uc_attribute.module \uc_object_options_form()
  2. 7.3 uc_attribute/uc_attribute.admin.inc \uc_object_options_form()

Form to assign and modify attribute options on products or classes.

See also

uc_object_options_form_validate()

uc_object_options_form_submit()

theme_uc_object_options_form()

1 string reference to 'uc_object_options_form'
uc_attribute_menu in uc_attribute/uc_attribute.module
Implements hook_menu().

File

uc_attribute/uc_attribute.admin.inc, line 976
Attribute administration menu items.

Code

function uc_object_options_form($form_state, $object, $type) {
  if ($type == 'product') {
    $product = $object;
    $id = $product->nid;
    drupal_set_title(check_plain($product->title));
    $attributes = uc_product_get_attributes($id);
    $table = '{uc_product_options}';
    $id_type = 'nid';
    $sql_type = db_type_placeholder('int');
  }
  elseif ($type == 'class') {
    $class = $object;
    $id = $class->pcid;
    drupal_set_title(check_plain($class->name));
    $attributes = uc_class_get_attributes($id);
    $table = '{uc_class_attribute_options}';
    $id_type = 'pcid';
    $sql_type = db_type_placeholder('varchar');
  }
  foreach ($attributes as $aid => $attribute) {
    $form['attributes'][$aid]['name'] = array(
      '#value' => check_plain($attribute->name),
    );
    $form['attributes'][$aid]['aid'] = array(
      '#type' => 'hidden',
      '#value' => $attribute->aid,
    );
    $form['attributes'][$aid]['ordering'] = array(
      '#type' => 'value',
      '#value' => $attribute->ordering,
    );
    $form['attributes'][$aid]['options'] = array(
      '#weight' => 2,
    );
    $base_attr = uc_attribute_load($attribute->aid);
    if ($base_attr->options) {
      $options = array();
      $result = db_query("SELECT ao.aid, ao.oid, ao.name, ao.cost AS default_cost, ao.price AS default_price, ao.weight AS default_weight, ao.ordering AS default_ordering, po.cost, po.price, po.weight, po.ordering, po.ordering IS NULL AS null_order FROM {uc_attribute_options} AS ao LEFT JOIN {$table} AS po ON ao.oid = po.oid AND po." . $id_type . " = " . $sql_type . " WHERE aid = %d ORDER BY null_order, po.ordering, default_ordering, ao.name", $id, $attribute->aid);
      while ($option = db_fetch_object($result)) {
        $oid = $option->oid;
        $options[$oid] = '';
        $form['attributes'][$aid]['options'][$oid]['select'] = array(
          '#type' => 'checkbox',
          '#default_value' => isset($attribute->options[$oid]) ? TRUE : FALSE,
          '#title' => check_plain($option->name),
        );
        $form['attributes'][$aid]['options'][$oid]['cost'] = array(
          '#type' => 'textfield',
          '#default_value' => uc_store_format_price_field_value(is_null($option->cost) ? $option->default_cost : $option->cost),
          '#size' => 6,
        );
        $form['attributes'][$aid]['options'][$oid]['price'] = array(
          '#type' => 'textfield',
          '#default_value' => uc_store_format_price_field_value(is_null($option->price) ? $option->default_price : $option->price),
          '#size' => 6,
        );
        $form['attributes'][$aid]['options'][$oid]['weight'] = array(
          '#type' => 'textfield',
          '#default_value' => is_null($option->weight) ? $option->default_weight : $option->weight,
          '#size' => 5,
        );
        $form['attributes'][$aid]['options'][$oid]['ordering'] = array(
          '#type' => 'weight',
          '#delta' => 50,
          '#default_value' => is_null($option->ordering) ? $option->default_ordering : $option->ordering,
          '#attributes' => array(
            'class' => 'uc-attribute-option-table-ordering',
          ),
        );
      }
      $form['attributes'][$aid]['default'] = array(
        '#type' => 'radios',
        '#options' => $options,
        '#default_value' => $attribute->default_option,
      );
    }
    else {
      $form['attributes'][$aid]['default'] = array(
        '#value' => t('This attribute does not have any options.'),
      );
    }
  }
  if (!empty($form['attributes'])) {
    $form['attributes']['#tree'] = TRUE;
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Submit'),
      '#weight' => 10,
    );
  }
  $form['id'] = array(
    '#type' => 'value',
    '#value' => $id,
  );
  $form['type'] = array(
    '#type' => 'value',
    '#value' => $type,
  );
  return $form;
}