You are here

function _uc_dropdown_attributes_form in Dropdown Attributes 6

Same name and namespace in other branches
  1. 7 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.

$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
Specify the attribute dependencies.

File

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

Code

function _uc_dropdown_attributes_form(&$form, $form_state, $attributes, $dependencies) {
  $form['#cache'] = TRUE;

  // the contents will either come from the db or from $form_state
  if (isset($form_state['my_values'])) {
    $my_values = $form_state['my_values'];
  }
  else {
    while ($item = db_fetch_object($dependencies)) {
      $my_values['parent-' . $item->aid] = $item->parent_aid;
      $my_values['required-' . $item->aid] = $item->required;
      $my_values['values-' . $item->aid . '-wrapper']['values-' . $item->aid] = unserialize($item->parent_values);
    }
  }
  $form['intro'] = array(
    '#type' => 'markup',
    '#value' => '<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('If Javascript is not enabled, a Choose button appears at the bottom of the page to allow this page to be reloaded with the updated values.') . '</p>',
  );
  $form['prefix'] = array(
    '#type' => 'markup',
    '#value' => '<table><tr><th>' . t('Attribute') . '</th><th>' . t('Depends On') . '</th><th>' . t('With Values') . '</th><th>' . t('Required') . '</th></tr>',
  );
  $index = 1;
  foreach ($attributes as $attribute) {
    $form['attributes'][$attribute->aid]['attribute-' . $attribute->aid] = array(
      '#type' => 'markup',
      '#prefix' => '<tr><td>',
      '#value' => $attribute->name,
      '#suffix' => '</td>',
    );
    $options = array();
    $options[0] = 'None';
    foreach ($attributes as $option) {
      if ($option->aid != $attribute->aid) {
        $options[$option->aid] = $option->name;
      }
    }
    $selected = isset($my_values['parent-' . $attribute->aid]) ? $my_values['parent-' . $attribute->aid] : 0;
    $form['attributes'][$attribute->aid]['parent-' . $attribute->aid] = array(
      '#type' => 'select',
      '#prefix' => '<td>',
      '#options' => $options,
      '#default_value' => $selected,
      '#ahah' => array(
        'path' => 'dropdown/dependencies/' . $attribute->aid . '/callback',
        'wrapper' => 'values-' . $attribute->aid . '-replace',
      ),
    );
    $form['attributes'][$attribute->aid]['values-' . $attribute->aid . '-wrapper'] = array(
      '#tree' => TRUE,
      '#prefix' => '<td><div id="values-' . $attribute->aid . '-replace">',
      '#suffix' => '</div></td>',
    );
    $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-' . $attribute->aid . '-wrapper']['values-' . $attribute->aid] = array(
        '#type' => $type,
        '#options' => $options,
        '#multiple' => TRUE,
        '#default_value' => isset($my_values['values-' . $attribute->aid . '-wrapper']['values-' . $attribute->aid]) ? $my_values['values-' . $attribute->aid . '-wrapper']['values-' . $attribute->aid] : '',
      );
    }
    else {
      $form['attributes'][$attribute->aid]['values-' . $attribute->aid . '-wrapper']['values-' . $attribute->aid] = array(
        '#type' => $type,
        '#size' => 30,
        '#default_value' => isset($my_values['values-' . $attribute->aid]) ? $my_values['values-' . $attribute->aid] : '',
      );
    }
    if (isset($my_values['required-' . $attribute->aid])) {
      $default_value = $my_values['required-' . $attribute->aid];
    }
    else {
      $default_value = 0;
    }
    $form['attributes'][$attribute->aid]['required-' . $attribute->aid] = array(
      '#type' => 'checkbox',
      '#prefix' => '<td>',
      '#returned_value' => 1,
      '#default_value' => $default_value,
      '#suffix' => '</td></tr>',
    );
    $index++;
  }
  $form['suffix'] = array(
    '#type' => 'markup',
    '#value' => '</table>',
  );

  // The CSS for this module hides this next button if JS is enabled.
  $form['continue'] = array(
    '#type' => 'submit',
    '#value' => t('Choose'),
    '#suffix' => '</td>',
    '#attributes' => array(
      'class' => 'next-button',
    ),
    '#submit' => array(
      'uc_dropdown_attributes_dropdown_submit_handler',
    ),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return;
}