You are here

function uc_object_attributes_form in Ubercart 7.3

Same name and namespace in other branches
  1. 5 uc_attribute/uc_attribute.module \uc_object_attributes_form()
  2. 6.2 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()

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 547
Attribute administration menu items.

Code

function uc_object_attributes_form($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($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($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();
    if (count($attributes) > 0) {
      foreach ($attributes as $attribute) {
        $option = isset($attribute->options[$attribute->default_option]) ? $attribute->options[$attribute->default_option] : NULL;
        $form['attributes'][$attribute->aid] = array(
          'remove' => array(
            '#type' => 'checkbox',
            '#title' => t('Remove'),
            '#title_display' => 'invisible',
            '#default_value' => 0,
          ),
          'name' => array(
            '#markup' => check_plain($attribute->name),
          ),
          'label' => array(
            '#type' => 'textfield',
            '#title' => t('Label'),
            '#title_display' => 'invisible',
            '#default_value' => empty($attribute->label) ? $attribute->name : $attribute->label,
            '#size' => 20,
            '#maxlength' => 255,
          ),
          'option' => array(
            '#markup' => $option ? check_plain($option->name) . ' (' . theme('uc_price', array(
              'price' => $option->price,
            )) . ')' : t('n/a'),
          ),
          'required' => array(
            '#type' => 'checkbox',
            '#title' => t('Required'),
            '#title_display' => 'invisible',
            '#default_value' => $attribute->required,
          ),
          'ordering' => array(
            '#type' => 'weight',
            '#title' => t('List position'),
            '#title_display' => 'invisible',
            '#delta' => 25,
            '#default_value' => $attribute->ordering,
            '#attributes' => array(
              'class' => array(
                'uc-attribute-table-ordering',
              ),
            ),
          ),
          'display' => array(
            '#type' => 'select',
            '#title' => t('Display'),
            '#title_display' => 'invisible',
            '#default_value' => $attribute->display,
            '#options' => _uc_attribute_display_types(),
          ),
        );
      }
      $form['actions'] = array(
        '#type' => 'actions',
      );
      $form['actions']['save'] = array(
        '#type' => 'submit',
        '#value' => t('Save changes'),
        '#weight' => -2,
      );
    }
  }
  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} a LEFT JOIN {uc_attribute_options} ao ON a.aid = ao.aid GROUP BY a.aid, a.name, a.label ORDER BY a.name");
    foreach ($result as $attribute) {
      if (!in_array($attribute->aid, $used_aids)) {
        $unused_attributes[$attribute->aid] = $attribute->name;
      }
    }
    $form['add_attributes'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Attributes'),
      '#options' => count($unused_attributes) > 0 ? $unused_attributes : array(
        t('No attributes left to add.'),
      ),
      '#disabled' => count($unused_attributes) == 0 ? TRUE : FALSE,
      '#weight' => -1,
    );
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['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;
}