You are here

function uc_object_attributes_form in Ubercart 5

Same name and namespace in other branches
  1. 6.2 uc_attribute/uc_attribute.admin.inc \uc_object_attributes_form()
  2. 7.3 uc_attribute/uc_attribute.admin.inc \uc_object_attributes_form()
1 string reference to 'uc_object_attributes_form'
uc_attribute_menu in uc_attribute/uc_attribute.module
Implementation of hook_menu().

File

uc_attribute/uc_attribute.module, line 729

Code

function uc_object_attributes_form($id, $type, $view = 'overview') {
  switch ($type) {
    case 'class':
      $class = uc_product_class_load($id);
      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 = node_load($id);
      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;
    if (count($attributes) > 0) {
      foreach ($attributes as $attribute) {
        $option = $attribute->options[$attribute->default_option];
        $form['attributes'][$attribute->aid] = array(
          'remove' => array(
            '#type' => 'checkbox',
            '#default_value' => 0,
          ),
          'name' => array(
            '#value' => $attribute->name,
          ),
          'option' => array(
            '#value' => $option ? $option->name . ' (' . uc_currency_format($option->price) . ')' : t('n/a'),
          ),
          'required' => array(
            '#type' => 'checkbox',
            '#default_value' => $attribute->required,
          ),
          'ordering' => array(
            '#type' => 'weight',
            '#default_value' => $attribute->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,
      );
    }
  }
  else {
    if ($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 FROM {uc_attributes} AS a LEFT JOIN {uc_attribute_options} AS ao ON a.aid = ao.aid GROUP BY a.aid, a.name 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;
}