Checkboxes and default_value from bldmtn

  1. /**
  2.  * Implementation of hook_form(). A multistep form that pulls in content from the database and renders it into the form based
  3.  * on that locations id. Checkboxes should be populated with their defaults as stored in the database. Checkbox values are
  4.  * converted from a string to an array for processing by default_value.
  5.  */
  6. function locations_locations_form(&$lid, $form_values = NULL)
  7. {
  8.         $this_location = $lid;
  9.         if (isset($this_location) && strspn($this_location, '1234567890') == strlen($this_location)) {
  10.                 $sql = "SELECT sid, fid, rid, lid, name, symbol, phone, fax, tty, email, url, handicapped_access, public_note, staff_note, units, activities FROM {locations_locations} WHERE lid = ". $this_location;
  11.                 $result = db_query(db_rewrite_sql($sql));
  12.                 $location = db_fetch_object($result);
  13.         }
  14.         $form = array();
  15.  
  16.         $form['#multistep'] = TRUE;
  17.         $form['#redirect'] = FALSE;
  18.        
  19.         $step = isset($form_values) ? (int) $form_values['step'] : 1;
  20.  
  21.         $form['locations_locations'] = array(
  22.                 '#type' => 'fieldset',
  23.                 '#title' => t('Location: Step @number', array('@number' => $step))
  24.         );
  25.        
  26.         $form['locations_locations']['lid'] = array(
  27.                 '#type' => 'value',
  28.                 '#value' => $location->lid
  29.         );
  30.        
  31.         $form['locations_locations']['step'] = array(
  32.                 '#type' => 'hidden',
  33.                 '#value' => $step + 1,
  34.                 );
  35.        
  36.         switch($step) {
  37.                 case 1:
  38.                         $form['locations_locations']['sid'] = array(
  39.                                 '#type' => 'select',
  40.                                 '#title' => 'Space',
  41.                                 '#default_value' => $location->sid,
  42.                                 '#options' => _spaces_dropdown(),
  43.                                 '#required' => TRUE,
  44.                                 );
  45.                 $button_text = t('Next');
  46.                 break;
  47.                
  48.                 case 2:
  49.                 $form['locations_locations']['sid'] = array(
  50.                         '#type' => 'hidden',
  51.                         '#value' => isset($form_values) ? $form_values['sid'] : ''
  52.                         );
  53.                
  54.                 $form['locations_locations']['fid'] = array(
  55.                                 '#type' => 'select',
  56.                                 '#title' => 'Level',
  57.                                 '#default_value' => $location->fid,
  58.                                 '#options' => _levels_dropdown($form_values['sid'])
  59.                                 );
  60.                 $button_text = t('Next');
  61.                 break;
  62.                        
  63.                 case 3:
  64.                         $form['locations_locations']['sid'] = array(
  65.                                 '#type' => 'hidden',
  66.                                 '#value' => isset($form_values) ? $form_values['sid'] : ''
  67.                                 );
  68.                
  69.                         $form['locations_locations']['fid'] = array(
  70.                                 '#type' => 'hidden',
  71.                                 '#value' => isset($form_values) ? $form_values['fid'] : ''
  72.                                 );
  73.  
  74.                         $form['locations_locations']['rid'] = array(
  75.                                         '#type' => 'select',
  76.                                         '#title' => 'Room',
  77.                                         '#default_value' => $location->rid,
  78.                                         '#options' => _rooms_dropdown($form_values['fid'], $form_values['sid'])
  79.                                         );
  80.  
  81.                         $form['locations_locations']['name'] = array(
  82.                                 '#type' => 'textfield',
  83.                                 '#title' => t('Location Name'),
  84.                                 '#default_value' => $location->name,
  85.                                 '#required' => TRUE
  86.                                 );
  87.  
  88.                         $form['locations_locations']['symbol'] = array(
  89.                                 '#type' => 'textfield',
  90.                                 '#title' => t('Symbol'),
  91.                                 '#default_value' => $location->symbol,
  92.                                 '#size' => '10',
  93.                                 '#maxlength' => '10'
  94.                                 );
  95.                                                
  96.                         $form['locations_locations']['phone_areacode'] = array(
  97.                                 '#type' => 'textfield',
  98.                                 '#title' => t('Phone'),
  99.                                 '#default_value' => substr($location->phone, 0, 3),
  100.                                 '#size' => '3',
  101.                                 '#maxlength' => '3'
  102.                                 );
  103.  
  104.                         $form['locations_locations']['phone_prefix'] = array(
  105.                                 '#type' => 'textfield',
  106.                                 '#default_value' => substr($location->phone, 3, 3),
  107.                                 '#size' => '3',
  108.                                 '#maxlength' => '3'
  109.                                 );
  110.  
  111.                         $form['locations_locations']['phone_number'] = array(
  112.                                 '#type' => 'textfield',
  113.                                 '#default_value' => substr($location->phone, 6, 4),
  114.                                 '#size' => '4',
  115.                                 '#maxlength' => '4'
  116.                                 );
  117.  
  118.                         $form['locations_locations']['fax_areacode'] = array(
  119.                                 '#type' => 'textfield',
  120.                                 '#title' => t('Fax'),
  121.                                 '#default_value' => substr($location->fax, 0, 3),
  122.                                 '#size' => '3',
  123.                                 '#maxlength' => '3'
  124.                                 );
  125.  
  126.                         $form['locations_locations']['fax_prefix'] = array(
  127.                                 '#type' => 'textfield',
  128.                                 '#default_value' => substr($location->fax, 3, 3),
  129.                                 '#size' => '3',
  130.                                 '#maxlength' => '3'
  131.                                 );
  132.  
  133.                         $form['locations_locations']['fax_number'] = array(
  134.                                 '#type' => 'textfield',
  135.                                 '#default_value' => substr($location->fax, 6, 4),
  136.                                 '#size' => '4',
  137.                                 '#maxlength' => '4'
  138.                                 );
  139.  
  140.                         $form['locations_locations']['tty_areacode'] = array(
  141.                                 '#type' => 'textfield',
  142.                                 '#title' => t('TTY'),
  143.                                 '#default_value' => substr($location->tty, 0, 3),
  144.                                 '#size' => '3',
  145.                                 '#maxlength' => '3'
  146.                                 );
  147.  
  148.                         $form['locations_locations']['tty_prefix'] = array(
  149.                                 '#type' => 'textfield',
  150.                                 '#default_value' => substr($location->tty, 3, 3),
  151.                                 '#size' => '3',
  152.                                 '#maxlength' => '3'
  153.                                 );
  154.  
  155.                         $form['locations_locations']['tty_number'] = array(
  156.                                 '#type' => 'textfield',
  157.                                 '#default_value' => substr($location->tty, 6, 4),
  158.                                 '#size' => '4',
  159.                                 '#maxlength' => '4'
  160.                                 );
  161.  
  162.                         $form['locations_locations']['email'] = array(
  163.                                 '#type' => 'textfield',
  164.                                 '#title' => t('Email'),
  165.                                 '#default_value' => $location->email
  166.                                 );
  167.  
  168.                         $form['locations_locations']['url'] = array(
  169.                                 '#type' => 'textfield',
  170.                                 '#title' => t('URL'),
  171.                                 '#default_value' => $location->url
  172.                                 );
  173.                         $form['locations_locations']['handicapped_access'] = array(
  174.                                 '#type' => 'radios',
  175.                                 '#title' => t('Wheelchair Access'),
  176.                                 '#options' => array(
  177.                                         '0' => t('Not Accessible'),
  178.                                         '1' => t('Partially Accessible'),
  179.                                         '2' => t('Fully Accessible')
  180.                                         ),
  181.                                 '#default_value' => $location->handicapped_access
  182.                                 );
  183.                         $form['locations_locations']['public_note'] = array(
  184.                                 '#type' => 'textarea',
  185.                                 '#title' => t('Public Note'),
  186.                                 '#rows' => '',
  187.                                 '#columns' => '',
  188.                                 '#default_value' => isset($location->public_note) ? $location->public_note : NULL
  189.                                 );
  190.                         $form['locations_locations']['staff_note'] = array(
  191.                                 '#type' => 'textarea',
  192.                                 '#title' => t('Staff Note'),
  193.                                 '#rows' => '',
  194.                                 '#columns' => '',
  195.                                 '#default_value' =>  isset($location->staff_note) ? $location->staff_note : NULL
  196.                                 );
  197.                         $unit_options = array(
  198.                                 'B' => t('Branch Unit'),
  199.                                 'R' => t('Research Unit'),
  200.                                 'C' => t('Central Unit')
  201.                                 );
  202.                         $units = explode(';', "'". $location->units ."'");
  203.                         $form['locations_locations']['units'] = array(
  204.                                 '#type' => 'checkboxes',
  205.                                 '#title' => 'Unit Affiliations',
  206.                                 '#options' => $unit_options,
  207.                                 '#default_value' => $units,
  208.                                 '#required' => TRUE
  209.                                 );
  210.                         $activities_options = array(
  211.                                 'E' => t('Has Events'),
  212.                                 'C' => t('Has Classes'),
  213.                                 'B' => t('Has Book Sales')
  214.                                 );
  215.                         $activities = explode(';', $location->activities);
  216.                         $form['locations_locations']['activities'] = array(
  217.                                 '#type' => 'checkboxes',
  218.                                 '#title' => 'Activities',
  219.                                 '#options' => $activities_options,
  220.                                 '#default_value' => $activities
  221.                                 );
  222.  
  223.                 $button_text = t('Submit');
  224.                 break;
  225.         }
  226.        
  227.         $form['submit'] = array(
  228.                 '#type' => 'submit',
  229.                 '#value' => $button_text
  230.                 );
  231.  
  232.         return $form;
  233. }