You are here

function ctools_ajax_sample_animal in Chaos Tool Suite (ctools) 7

Same name and namespace in other branches
  1. 6 ctools_ajax_sample/ctools_ajax_sample.module \ctools_ajax_sample_animal()

A modal login callback.

1 string reference to 'ctools_ajax_sample_animal'
ctools_ajax_sample_menu in ctools_ajax_sample/ctools_ajax_sample.module
Implementation of hook_menu()

File

ctools_ajax_sample/ctools_ajax_sample.module, line 309
Sample AJAX functionality so people can see some of the CTools AJAX features in use.

Code

function ctools_ajax_sample_animal($js = NULL, $step = NULL) {
  if ($js) {
    ctools_include('modal');
    ctools_include('ajax');
  }
  $form_info = array(
    'id' => 'animals',
    'path' => "ctools_ajax_sample/" . ($js ? 'ajax' : 'nojs') . "/animal/%step",
    'show trail' => TRUE,
    'show back' => TRUE,
    'show cancel' => TRUE,
    'show return' => FALSE,
    'next callback' => 'ctools_ajax_sample_wizard_next',
    'finish callback' => 'ctools_ajax_sample_wizard_finish',
    'cancel callback' => 'ctools_ajax_sample_wizard_cancel',
    // This controls order, as well as form labels.
    'order' => array(
      'start' => t('Choose animal'),
    ),
    // Here we map a step to a form id.
    'forms' => array(
      // e.g. this for the step at wombat/create.
      'start' => array(
        'form id' => 'ctools_ajax_sample_start',
      ),
    ),
  );

  // We're not using any real storage here, so we're going to set our
  // object_id to 1. When using wizard forms, id management turns
  // out to be one of the hardest parts. Editing an object with an id
  // is easy, but new objects don't usually have ids until somewhere
  // in creation.
  //
  // We skip all this here by just using an id of 1.
  $object_id = 1;
  if (empty($step)) {

    // We reset the form when $step is NULL because that means they have
    // for whatever reason started over.
    ctools_ajax_sample_cache_clear($object_id);
    $step = 'start';
  }

  // This automatically gets defaults if there wasn't anything saved.
  $object = ctools_ajax_sample_cache_get($object_id);
  $animals = ctools_ajax_sample_animals();

  // Make sure we can't somehow accidentally go to an invalid animal.
  if (empty($animals[$object->type])) {
    $object->type = 'unknown';
  }

  // Now that we have our object, dynamically add the animal's form.
  if ($object->type == 'unknown') {

    // If they haven't selected a type, add a form that doesn't exist yet.
    $form_info['order']['unknown'] = t('Configure animal');
    $form_info['forms']['unknown'] = array(
      'form id' => 'nothing',
    );
  }
  else {

    // Add the selected animal to the order so that it shows up properly in the trail.
    $form_info['order'][$object->type] = $animals[$object->type]['config title'];
  }

  // Make sure all animals forms are represented so that the next stuff can
  // work correctly:
  foreach ($animals as $id => $animal) {
    $form_info['forms'][$id] = array(
      'form id' => $animals[$id]['form'],
    );
  }
  $form_state = array(
    'ajax' => $js,
    // Put our object and ID into the form state cache so we can easily find
    // it.
    'object_id' => $object_id,
    'object' => &$object,
  );

  // Send this all off to our form. This is like drupal_get_form only wizardy.
  ctools_include('wizard');
  $form = ctools_wizard_multistep_form($form_info, $step, $form_state);
  $output = drupal_render($form);
  if ($output === FALSE || !empty($form_state['complete'])) {

    // This creates a string based upon the animal and its setting using
    // function indirection.
    $animal = $animals[$object->type]['output']($object);
  }

  // If $output is FALSE, there was no actual form.
  if ($js) {

    // If javascript is active, we have to use a render array.
    $commands = array();
    if ($output === FALSE || !empty($form_state['complete'])) {

      // Dismiss the modal.
      $commands[] = ajax_command_html('#ctools-sample', $animal);
      $commands[] = ctools_modal_command_dismiss();
    }
    elseif (!empty($form_state['cancel'])) {

      // If cancelling, return to the activity.
      $commands[] = ctools_modal_command_dismiss();
    }
    else {
      $commands = ctools_modal_form_render($form_state, $output);
    }
    print ajax_render($commands);
    exit;
  }
  else {
    if ($output === FALSE || !empty($form_state['complete'])) {
      return $animal;
    }
    elseif (!empty($form_state['cancel'])) {
      drupal_goto('ctools_ajax_sample');
    }
    else {
      return $output;
    }
  }
}