<?php

// $Id$

// FAPI Wierdness Test Case
// 
// To use this demo, install it (admin -> modules), and look for a "FAPI Test Case"
// link in your navigation menu. Click this, and you will see a simple form, none
// of which has any default values filled in. Enter some text in the text box, and
// tick the checkbox. Then proceed to "next".
// The second page shows you what you entered on the previous page. Click "back"
// and you will be returned to the first page.
//

function testcase_help($section='') {
  $output = '';
  switch ($section) {
    case "admin/help#testcase":
      $output = '<p>'.  t("text found in testcase function"). '</p>';
      break;
  }
  return $output;
} // function testcase_help

function testcase_perm() {
  return array('use testcase module');
} // function testcase_perm()

function testcase_menu() {

  $items = array(); 

    $items['testcase'] = array(
      'title' => t('FAPI Test Case'),
      'description' => t('Demonstrate A Problem with Checkboxes'),
      'page callback' => 'drupal_get_form',
      'page arguments' => array('testcase_form'),
      'access arguments' => array('use testcase module'),
      'type' => MENU_NORMAL_ITEM,
    );

  return $items;
}

// testcase_first_page()
// This function simply returns a form fragment. In the real world,
// it would presumably be building the form depending on some
// parameters. 
function _testcase_first_page($storage = array('testcase_checks' => array())) {
  $form = array();
  $form['mytext'] = array(
    '#type' => 'textfield',
    '#title' => t('Demo Text'),
  );
  return $form;
}

// testcase_form()
// The real fun happens here. The function looks to see if it's been called
// freshly (ie. by a user following a link to it, rather than via a form submit).
// If called freshly, it cleans out any previous information stored in the session
// and returns the first page form.
// If this function is called as a result of a previous form submission, it
// works out which page the user was on when they submitted. It then returns
// an appropriate form to them. If it's returning the first page form, it
// fills in #default_value with the information stored in the session.
function testcase_form(&$form_state) {

  $form = array();

  $notice='';

  if(!isset($form_state['values'])) {
    // We're starting the testcase from scratch.

    // Store something to demonstrate it's not actually stored!
    if(!isset($form_state['storage']) || (!is_array($form_state['storage']))) {
      $form_state['storage'] = array('fruit' => 'banana');
    } else {
      $form_state['storage']['fruit'] = 'banana';
    }

    // Now get the first page form...
    $form['wizard'] = _testcase_first_page();

    // Now say we're on page 1
    $form['step'] = array(
      '#type' => 'hidden',
      '#value' => 1,
    );
    // And have a submit button
    $form['next_button'] = array(
      '#type' => 'submit',
      '#value' => 'Next',
    );

    $notice = "You are on step 1 of the wizard, but have not submitted any information before";
  } else {
    // See where we are. We're returning the form after a submission,
    // so find out where we just were:

    $storage = $form_state['storage'];
    switch($storage['step']) {
      default:
      case 1:
        // Here, we were on step 2, and have gone back to step 1. We
        // now need to display the same step 1 form, but with any previously
        // entered values set as defaults. We can't use $form_values for these
        // defaults, because the second step of the form didn't submit any
        // values to us.
        // Now get the first page form...
        $form['wizard'] = _testcase_first_page($storage);

        // Insert defaults...
        // I appreciate this is a wierd looking way to do this. You can pass values to
        // _testcase_first_page() if you wish - the outcome will be the same.
        $form['wizard']['mytext']['#default_value'] = $storage['testcase_text'];

        // Now say we're on page 1
        $form['step'] = array(
          '#type' => 'hidden',
          '#value' => 1,
        );
        // And have a submit button
        $form['next_button'] = array(
          '#type' => 'submit',
          '#value' => 'Next',
        );
        $notice = "You have returned to step 1, having visited step 2, but gone backwards in the wizard. ";

        // Set something that only gets set on the second view of step 1
        $form_state['storage']['food'] = 'sandwich';
        break;
      case 2:
        // We were on step 1, now on step 2. So just display what
        // we've received. We could do this with $form_values, but
        // for this test case, we're going to confirm what's in the
        // session.
        $form['results']['textbox'] = array(
          '#type' => 'markup',
          '#value' => "<div>Text box contained " . htmlspecialchars($storage['testcase_text']) . "</div>",
        );
        // And put in a new step number
        $form['step'] = array(
          '#type' => 'hidden',
          '#value' => 2,
        );
        // And a submit button to go back
        $form['back_button'] = array(
          '#type' => 'submit',
          '#value' => 'Back',
        );
        $notice = "You are now on step 2 of the wizard, having submitted information in step 1";
        break;
    }
  }

  $form['notice'] = array(
    '#type' => 'markup',
    '#weight' => 10,
    '#value' => "<div>$notice</div>",
  );

  $notice = '';

  if(isset($form_state['storage']['fruit'])) {
    $notice .= "Storage entry set in first run of form builder function is set to '" . $form_state['storage']['fruit'] . "'. ";
  } else {
    $notice .= "Storage entry set in first run of form_builder not set. ";
  }
  if(isset($form_state['storage']['drink'])) {
    $notice .= "Storage entry set in submit handler is set to '" . $form_state['storage']['drink'] . "'. ";
  } else {
    $notice .= "Storage entry set in submit handler not yet set. ";
  }
  if(isset($form_state['storage']['food'])) {
    $notice .= "Storage entry set in second run of form builder is set to '" . $form_state['storage']['food'] . "'. ";
  } else {
    $notice .= "Storage entry set in second run of form builder not yet set. ";
  }


  $form['notice2'] = array(
    '#type' => 'markup',
    '#weight' => 11,
    '#value' => "<div>$notice</div>",
  );


  return $form;
}

function testcase_form_submit($form, &$form_state) {

  $new_step = $form_state['values']['step'];
  if($form_state['values']['op'] == 'Next') {
    $new_step++;
  } elseif($form_state['values']['op'] == 'Back') {
    $new_step--;
  }

  if($form_state['values']['step'] == 1) {
    // Must have moved forwards in the "wizard". Save the values we've been given
    $form_state['storage']['step'] = $new_step;
    $form_state['storage']['testcase_text'] = $form_state['values']['mytext'];
  } else {
    $form_state['storage']['step'] = $new_step;
  }

  // Always store this - to show storage working
  $form_state['storage']['drink'] = 'beer';
  return;
}

// The following line is for Vim users - please don't delete it.
// vim: set filetype=php expandtab tabstop=2 shiftwidth=2:
