You are here

function uc_object_attributes_form in Ubercart 6.2

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

Form to associate attributes with products or classes.

See also

uc_object_attributes_form_submit()

uc_object_attributes_form_reset()

theme_uc_object_attributes_form()

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

File

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

Code

function uc_object_attributes_form($form_state, $object, $type, $view = 'overview') {
  switch ($type) {
    case 'class':
      $class = $object;
      $id = $class->pcid;
      if (empty($class->name)) {
        drupal_goto('admin/store/products/classes/' . $id);
      }
      drupal_set_title(check_plain($class->name));
      $attributes = uc_class_get_attributes($id);
      break;
    case 'product':
    default:
      $product = $object;
      $id = $product->nid;
      if (empty($product->title)) {
        drupal_goto('node/' . $id);
      }
      drupal_set_title(check_plain($product->title));
      $attributes = uc_product_get_attributes($id);
  }
  $used_aids = array();
  foreach ($attributes as $attribute) {
    $used_aids[] = $attribute->aid;
  }
  if ($view == 'overview') {
    $form['#tree'] = TRUE;
    $form['attributes'] = array();
    $context = array(
      'revision' => 'themed',
      'type' => 'attribute_option',
    );
    if (count($attributes) > 0) {
      foreach ($attributes as $attribute) {
        $option = isset($attribute->options[$attribute->default_option]) ? $attribute->options[$attribute->default_option] : NULL;
        $context['subject'] = array(
          'attribute' => $attribute,
          'option' => $option,
        );
        $form['attributes'][$attribute->aid] = array(
          'remove' => array(
            '#type' => 'checkbox',
            '#default_value' => 0,
          ),
          'name' => array(
            '#value' => check_plain($attribute->name),
          ),
          'label' => array(
            '#type' => 'textfield',
            '#default_value' => empty($attribute->label) ? $attribute->name : $attribute->label,
            '#size' => 6,
          ),
          'option' => array(
            '#value' => $option ? check_plain($option->name) . ' (' . uc_price($option->price, $context) . ')' : t('n/a'),
          ),
          'required' => array(
            '#type' => 'checkbox',
            '#default_value' => $attribute->required,
          ),
          'ordering' => array(
            '#type' => 'weight',
            '#delta' => 25,
            '#default_value' => $attribute->ordering,
            '#attributes' => array(
              'class' => 'uc-attribute-table-ordering',
            ),
          ),
          'display' => array(
            '#type' => 'select',
            '#default_value' => $attribute->display,
            '#options' => _uc_attribute_display_types(),
          ),
        );
      }
      $form['save'] = array(
        '#type' => 'submit',
        '#value' => t('Save changes'),
        '#weight' => -2,
      );
    }
    if ($type == 'product') {
      $form['reset'] = array(
        '#type' => 'submit',
        '#value' => t('Reset to defaults'),
        '#submit' => array(
          'uc_object_attributes_form_reset',
        ),
        '#weight' => -1,
      );
    }
  }
  elseif ($view == 'add') {

    // Get list of attributes not already assigned to this node or class.
    $unused_attributes = array();
    $result = db_query("SELECT a.aid, a.name, a.label FROM {uc_attributes} AS a LEFT JOIN {uc_attribute_options} AS ao ON a.aid = ao.aid GROUP BY a.aid, a.name, a.label ORDER BY a.name");
    while ($attribute = db_fetch_object($result)) {
      if (!in_array($attribute->aid, $used_aids)) {
        $unused_attributes[$attribute->aid] = $attribute->name;
      }
    }
    $form['add_attributes'] = array(
      '#type' => 'select',
      '#title' => t('Attributes'),
      '#description' => t('Hold Ctrl + click to select multiple attributes.'),
      '#options' => count($unused_attributes) > 0 ? $unused_attributes : array(
        t('No attributes left to add.'),
      ),
      '#disabled' => count($unused_attributes) == 0 ? TRUE : FALSE,
      '#multiple' => TRUE,
      '#weight' => -1,
    );
    $form['add'] = array(
      '#type' => 'submit',
      '#value' => t('Add attributes'),
      '#suffix' => l(t('Cancel'), $type == 'product' ? 'node/' . $id . '/edit/attributes' : 'admin/store/products/classes/' . $class->pcid . '/attributes'),
      '#weight' => 0,
    );
  }
  $form['id'] = array(
    '#type' => 'value',
    '#value' => $id,
  );
  $form['type'] = array(
    '#type' => 'value',
    '#value' => $type,
  );
  $form['view'] = array(
    '#type' => 'value',
    '#value' => $view,
  );
  return $form;
}