You are here

function _uc_dropdown_attributes_form in Dropdown Attributes 7

Same name and namespace in other branches
  1. 6 dependent_dropdown.inc \_uc_dropdown_attributes_form()

Internal form constructor for administration of attribute dependencies.

This function takes the data retrieved for products and classes and constructs the dependencies administration form.

Parameters

array $form: The form array.

array $form_state: The values and state of the form.

array $attributes: The attributes for this node or product class.

object $dependencies: The dependency information for the node or product class from the database.

2 calls to _uc_dropdown_attributes_form()
uc_dropdown_attributes_class in ./dependent_dropdown.inc
Form constructor for the uc_dropdown_attributes classes form.
uc_dropdown_attributes_product in ./dependent_dropdown.inc
Form constructor for the uc_dropdown_attributes_product form.

File

./dependent_dropdown.inc, line 172
Administrative interface for specifying the attribute dependencies.

Code

function _uc_dropdown_attributes_form(&$form, $form_state, $attributes, $dependencies) {
  $parent = array();
  $values = array();
  $required = array();
  if (isset($form_state['values']['attributes'])) {
    foreach ($form_state['values']['attributes'] as $key => $attribute) {
      $parent[$key] = $attribute['parent'];
      $values[$key] = $attribute['values'];
      $required[$key] = $attribute['required'];
    }
  }
  else {
    foreach ($dependencies as $item) {
      $parent[$item->aid] = $item->parent_aid;
      $values[$item->aid] = unserialize($item->parent_values);
      $required[$item->aid] = $item->required;
    }
  }
  $form['#tree'] = TRUE;
  $form['intro'] = array(
    '#markup' => '<p>' . t('Since drop down attributes may not appear, they cannot be always required.  The required checkbox applies only when the dropdown attribute appears.  Any dropdown attribute is also checked under the attributes table to make sure it is not required there as this would cause validation errors.') . '</p><p>' . t('Unless you know what you are doing, all dependent (child) attributes should be marked as required on this page.') . '</p>',
  );
  foreach ($attributes as $attribute) {
    $form['attributes'][$attribute->aid]['attribute'] = array(
      '#markup' => $attribute->name,
    );
    $options = array();
    $options[0] = 'None';
    foreach ($attributes as $option) {
      if ($option->aid != $attribute->aid) {
        $options[$option->aid] = $option->name;
      }
    }
    $selected = array_key_exists($attribute->aid, $parent) ? $parent[$attribute->aid] : 0;
    $form['attributes'][$attribute->aid]['parent'] = array(
      '#type' => 'select',
      '#options' => $options,
      '#default_value' => $selected,
      '#ajax' => array(
        'callback' => 'uc_dropdown_attributes_dependent_callback',
        'wrapper' => 'dropdown-' . $attribute->aid . '-replace',
      ),
    );
    $options = array();
    if ($selected == 0) {
      $type = 'select';
    }
    else {
      $parent_attributes = uc_attribute_load($selected);
      if (count($parent_attributes->options) == 0) {
        $type = 'textfield';
      }
      else {
        $type = 'select';
        foreach ($parent_attributes->options as $oid => $option) {
          $options[$oid] = $option->name;
        }
      }
    }
    if ($type == 'select') {
      $form['attributes'][$attribute->aid]['values'] = array(
        '#type' => 'select',
        '#multiple' => TRUE,
        '#prefix' => '<div id="dropdown-' . $attribute->aid . '-replace">',
        '#suffix' => '</div>',
        '#options' => $options,
      );
      if (array_key_exists($attribute->aid, $values)) {
        $form['attributes'][$attribute->aid]['values']['#default_value'] = $values[$attribute->aid];
      }
    }
    else {
      $form['attributes'][$attribute->aid]['values'] = array(
        '#type' => 'textfield',
        '#prefix' => '<div id="dropdown-' . $attribute->aid . '-replace">',
        '#suffix' => '</div>',
      );
    }
    $form['attributes'][$attribute->aid]['required'] = array(
      '#type' => 'checkbox',
      '#returned_value' => 1,
      '#default_value' => array_key_exists($attribute->aid, $required) ? $required[$attribute->aid] : 0,
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('OK'),
  );
}