You are here

function scald_atom_add_page in Scald: Media Management made easy 7

Atom add page callback.

1 call to scald_atom_add_page()
scald_atom_edit_page in includes/scald.pages.inc
Atom edit page callback.
1 string reference to 'scald_atom_add_page'
scald_menu in ./scald.module
Implements hook_menu().

File

includes/scald.pages.inc, line 40
This file contains the various callbacks related to Scald defined pages.

Code

function scald_atom_add_page($js, $type, $step = NULL, $atom_id = NULL) {
  if ($js) {
    ctools_include('modal');
    ctools_include('ajax');
  }
  ctools_include('object-cache');
  ctools_include('wizard');

  // If we are not currently edit an atom and there is a temporary saved atom,
  // reopen it.
  $cache = ctools_object_cache_get('scald_atom', 'edit:-1');
  if (!$atom_id && $cache && $cache['atoms'][0]->type === $type->type) {
    $atom_id = -1;
    $step = 'options';
  }
  $cache_id = isset($atom_id) ? 'edit:' . $atom_id : 'add';

  // Start by getting the list of all the modules that said they can provide
  // this atom type.
  $providers = scald_atom_providers_opt();
  $sources = $providers[$type->type];
  $source = key($sources);
  $provider = current($sources);

  // If there's more than one, provide a choice between them. Otherwise, skip
  // a step and select the only provider upfront.
  if (empty($step)) {
    if (count($sources) < 2) {
      if (!empty($provider['starting_step'])) {
        $step = $provider['starting_step'];
      }
      else {
        $step = 'add';
      }
    }
    else {
      $step = 'source';
    }
    ctools_object_cache_clear('scald_atom', $cache_id);
  }
  $form_state = array(
    'ajax' => $js,
    'scald' => ctools_object_cache_get('scald_atom', $cache_id),
  );

  // Entity Translation workaround when the fix https://drupal.org/node/2027513
  // is not corporated in a stable release.
  if (isset($form_state['scald']['atoms'][0])) {
    $form_state['atom'] = $form_state['scald']['atoms'][0];
  }
  if (empty($form_state['scald'])) {
    $form_state['scald'] = array(
      'type' => $type,
      'source' => isset($source) ? $source : NULL,
      'provider' => $provider,
    );
  }
  $form_state['scald']['step'] = $step;
  $form_info = array(
    'id' => 'scald-atom-add',
    'path' => 'atom/add/' . $type->type . '/' . ($js ? 'ajax' : 'nojs') . '/%step',
    'show trail' => TRUE,
    'show back' => FALSE,
    'show cancel' => TRUE,
    'show return' => FALSE,
    'next callback' => 'scald_atom_add_wizard_next',
    'finish callback' => 'scald_atom_add_wizard_finish',
    'cancel callback' => 'scald_atom_add_wizard_cancel',
    'order' => array(
      'source' => t('Source'),
      'add' => t('Add'),
      'options' => t('Options'),
    ),
    'forms' => array(
      'source' => array(
        'form id' => 'scald_atom_add_form_source',
      ),
      'add' => array(
        'form id' => 'scald_atom_add_form_add',
      ),
      'options' => array(
        'form id' => 'scald_atom_add_form_options',
      ),
    ),
  );

  // Send this all off to our form. This is like drupal_get_form only wizardy.
  $form = ctools_wizard_multistep_form($form_info, $step, $form_state);
  $output = drupal_render($form);

  // 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[] = array(
        'command' => 'dnd_refresh',
      );
      $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'])) {
      $atom = $form_state['scald']['atoms'][0];
      drupal_goto('atom/' . $atom->sid);
    }
    elseif (!empty($form_state['cancel'])) {
      drupal_goto('atom/add');
    }
    else {
      return $output;
    }
  }
}