You are here

function layouter_choose_layout in Layouter - WYSIWYG layout templates 7

Callback for layouter/%ctools_js/% path.

Runs multistep wizard form.

1 string reference to 'layouter_choose_layout'
layouter_menu in ./layouter.module
Implements hook_menu().

File

includes/layouter.pages.inc, line 13
File with pages callbacks.

Code

function layouter_choose_layout($textarea_id = NULL, $step = NULL) {
  ctools_include('modal');
  ctools_include('ajax');
  require_once 'layouter.inc';
  $form_info = array(
    'id' => 'layouter',
    'path' => 'admin/layouter/' . $textarea_id . '/%step',
    'show trail' => FALSE,
    'show back' => FALSE,
    'show cancel' => TRUE,
    'show return' => FALSE,
    'next text' => t('Next'),
    'next callback' => 'layouter_wizard_next',
    'finish callback' => 'layouter_wizard_finish',
    'cancel callback' => 'layouter_wizard_cancel',
    // This controls order, as well as form labels.
    'order' => array(
      'start' => t('Choose layout template'),
    ),
    // Here we map a step to a form id.
    'forms' => array(
      // E.g. this for the step at wombat/create.
      'start' => array(
        'form id' => 'layouter_choose_layout_form',
      ),
    ),
  );
  $object_id = 1;
  if (empty($step)) {

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

  // This automatically gets defaults if there wasn't anything saved.
  $object = layouter_cache_get($object_id);
  $templates = layouter_templates();

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

  // Now that we have our object, dynamically add the template 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('Template content');
    $form_info['forms']['unknown'] = array(
      'form id' => 'nothing',
    );
  }
  else {

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

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

  // Send this all off to our form. This is like drupal_get_form.
  ctools_include('wizard');
  $form = ctools_wizard_multistep_form($form_info, $step, $form_state);
  $form['buttons']['cancel']['#attributes']['class'][] = 'layouter-close';
  $output = drupal_render($form);
  if ($output === FALSE || !empty($form_state['complete'])) {

    // This creates a string based upon the template and its setting using
    // function indirection.
    $template = $templates[$object->type]['output']($object);
  }
  $commands = array();
  if ($output === FALSE || !empty($form_state['complete']) || !empty($form_state['cancel'])) {

    // Dismiss the modal.
    $commands[] = ctools_modal_command_dismiss();
  }
  else {
    $commands = ctools_modal_form_render($form_state, $output);
  }
  print ajax_render($commands);
  exit;
}