You are here

function wysiwyg_template_template_form_submit in Wysiwyg API template plugin 7.2

Wysiwyg template form submit - delete and save handlers

File

./wysiwyg_template.admin.inc, line 248
Administrative page callbacks for the Wysiwyg Template module.

Code

function wysiwyg_template_template_form_submit($form, &$form_state) {

  //if we're deleting the template
  if ($form_state['clicked_button']['#id'] == 'edit-delete') {

    //show the confirmation
    $form_state['rebuild'] = true;
    $form_state['confirm_delete'] = true;
    return;
  }

  // delete confirmation provided
  if (isset($form_state['values']['delete'])) {
    if ($form_state['values']['delete'] === TRUE) {

      // delete image if one was uploaded
      if (!empty($form_state['values']['template_image_fid'])) {
        file_delete($form_state['values']['template_image_fid']);
      }
      wysiwyg_template_delete_template($form_state['values']['name']);
      drupal_set_message(t('The template has been deleted.'));
      $form_state['redirect'] = 'admin/config/content/wysiwyg-templates';
      return;
    }
  }

  // drop image if selected and checked
  if (isset($form_state['values']['template_image_delete']) && $form_state['values']['template_image_delete']) {
    file_delete(file_load($form_state['values']['template_image_fid']));

    // unset the fid previously used
    $form_state['values']['template_image_fid'] = 0;
  }

  // prepare file if needed
  $filepath = 'public://wysiwyg_template_images/';
  file_prepare_directory($filepath, FILE_CREATE_DIRECTORY);

  // save the image, validate it against file_validate_extensions
  $file = file_save_upload('template_image', array(
    'file_validate_extensions' => array(
      'jpg png gif jpeg',
    ),
  ), $filepath);
  if ($file) {

    // set status to permanent
    $file->status = FILE_STATUS_PERMANENT;
    $file = file_save($file);
    if ($file) {
      $form_state['values']['fid'] = $file->fid;

      // delete previous file if it had one
      if ($form_state['values']['template_image_fid']) {
        file_delete(file_load($form_state['values']['template_image_fid']));
      }
    }
  }
  else {
    $form_state['values']['fid'] = $form_state['values']['template_image_fid'];
  }

  // save the template
  // Flatten body field data.
  $template = $form_state['values'];
  $template['body'] = $form_state['values']['body']['value'];
  $template['format'] = $form_state['values']['body']['format'];
  if (wysiwyg_template_save_template($template)) {
    drupal_set_message(t('The template has been saved.'));
  }
  else {
    drupal_set_message(t('There was an error saving the template to the database.'));
  }

  // redirect back to the overview page
  $form_state['redirect'] = 'admin/config/content/wysiwyg-templates';
}