You are here

function uc_object_options_form in Ubercart 7.3

Same name and namespace in other branches
  1. 5 uc_attribute/uc_attribute.module \uc_object_options_form()
  2. 6.2 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 864
Attribute administration menu items.

Code

function uc_object_options_form($form, &$form_state, $object, $type) {
  if ($type == 'product') {
    $product = $object;
    $id = $product->nid;
    drupal_set_title($product->title);
    $attributes = uc_product_get_attributes($id);
    $table = 'uc_product_options';
    $id_type = 'nid';
  }
  elseif ($type == 'class') {
    $class = $object;
    $id = $class->pcid;
    drupal_set_title($class->name);
    $attributes = uc_class_get_attributes($id);
    $table = 'uc_class_attribute_options';
    $id_type = 'pcid';
  }
  foreach ($attributes as $aid => $attribute) {
    $form['attributes'][$aid]['name'] = array(
      '#markup' => 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();
      $query = db_select('uc_attribute_options', 'ao')
        ->fields('ao', array(
        'aid',
        'oid',
        'name',
      ));
      $query
        ->leftJoin($table, 'po', "ao.oid = po.oid AND po.{$id_type} = :id", array(
        ':id' => $id,
      ));
      $query
        ->addField('ao', 'cost', 'default_cost');
      $query
        ->addField('ao', 'price', 'default_price');
      $query
        ->addField('ao', 'weight', 'default_weight');
      $query
        ->addField('ao', 'ordering', 'default_ordering');
      $query
        ->fields('po', array(
        'cost',
        'price',
        'weight',
        'ordering',
      ))
        ->addExpression('CASE WHEN po.ordering IS NULL THEN 1 ELSE 0 END', 'null_order');
      $query
        ->condition('aid', $attribute->aid)
        ->orderBy('null_order')
        ->orderBy('po.ordering')
        ->orderBy('default_ordering')
        ->orderBy('ao.name');
      $result = $query
        ->execute();
      foreach ($result as $option) {
        $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' => 'uc_price',
          '#title' => t('Cost'),
          '#title_display' => 'invisible',
          '#default_value' => is_null($option->cost) ? $option->default_cost : $option->cost,
          '#size' => 6,
          '#allow_negative' => TRUE,
        );
        $form['attributes'][$aid]['options'][$oid]['price'] = array(
          '#type' => 'uc_price',
          '#title' => t('Price'),
          '#title_display' => 'invisible',
          '#default_value' => is_null($option->price) ? $option->default_price : $option->price,
          '#size' => 6,
          '#allow_negative' => TRUE,
        );
        $form['attributes'][$aid]['options'][$oid]['weight'] = array(
          '#type' => 'textfield',
          '#title' => t('Weight'),
          '#title_display' => 'invisible',
          '#default_value' => is_null($option->weight) ? $option->default_weight : $option->weight,
          '#size' => 5,
        );
        $form['attributes'][$aid]['options'][$oid]['ordering'] = array(
          '#type' => 'weight',
          '#title' => t('List position'),
          '#title_display' => 'invisible',
          '#delta' => 50,
          '#default_value' => is_null($option->ordering) ? $option->default_ordering : $option->ordering,
          '#attributes' => array(
            'class' => array(
              'uc-attribute-option-table-ordering',
            ),
          ),
        );
      }
      $form['attributes'][$aid]['default'] = array(
        '#type' => 'radios',
        '#title' => t('Default'),
        '#title_display' => 'invisible',
        '#options' => $options,
        '#default_value' => $attribute->default_option,
      );
    }
  }
  if (!empty($form['attributes'])) {
    $form['attributes']['#tree'] = TRUE;
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['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;
}