You are here

function scald_atom_add_form_options_submit in Scald: Media Management made easy 7

Handles the final atom creation step form submission.

File

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

Code

function scald_atom_add_form_options_submit(&$form, &$form_state) {
  if (!($atoms = $form_state['scald']['atoms'])) {
    return;
  }
  $atom_keys = array();
  foreach (array_keys($atoms) as $delta) {
    $atom_keys[] = 'atom' . $delta;
  }
  $values_excluding_atoms = array_diff_key($form_state['values'], array_flip($atom_keys));
  foreach ($atoms as $delta => $atom) {
    $index = 'atom' . $delta;
    $values =& $form_state['values'][$index];
    if (is_array($values['scald_actions'])) {
      $bitstream = 0;
      $actions = scald_actions();
      foreach ($actions as $name => $action) {
        if (!empty($values['scald_actions'][$name])) {
          $bitstream |= $action['bitmask'];
        }
      }
      $values['actions'] = $bitstream;
    }
    $op = empty($atom->sid) ? t('created') : t('updated');

    // Prepare a form state that is suitable for modules that provide extra fields.
    $atom_form_state = array(
      'values' => $values + $values_excluding_atoms,
    ) + $form_state;

    // Add back the structure required for fields and maintain a reference to
    // the original form state values.
    $atom_form_state['values'][$index] =& $values;

    // Let entity add its properties to the atom.
    entity_form_submit_build_entity('scald_atom', $atom, $form[$index], $atom_form_state);

    // Then save it...
    scald_atom_save($atom);
  }

  // Add a message confirming the creation.
  $type = scald_type_property_translate(scald_type_load($atoms[0]->type));
  if (count($atoms) == 1) {
    drupal_set_message(t('Atom %title, of type %type has been @op.', array(
      '%title' => $atoms[0]->title,
      '%type' => $type,
      '@op' => $op,
    )));
  }
  else {
    drupal_set_message(t('%count atoms of type %type have been @op.', array(
      '%count' => count($atoms),
      '%type' => $type,
      '@op' => $op,
    )));
  }
}