You are here

function patterns_edit_submit in Patterns 6

Same name and namespace in other branches
  1. 5 patterns.module \patterns_edit_submit()
  2. 6.2 patterns.module \patterns_edit_submit()
  3. 7.2 includes/forms/editor.inc \patterns_edit_submit()
  4. 7 includes/forms/editor.inc \patterns_edit_submit()

Submit edits to the pattern

File

./patterns.module, line 827
Enables extremely simple adding/removing features to your site with minimal to no configuration

Code

function patterns_edit_submit($form, &$form_state) {

  // If this is an enabled pattern, make sure the enabled pattern is saved in its current state
  if ($file = db_result(db_query("SELECT file FROM {patterns} WHERE status = 1 AND name = '%s'", $form_state['values']['name']))) {
    $dir = file_directory_path() . '/' . variable_get('patterns_save_xml', 'patterns') . '/enabled';
    file_check_directory($dir, true);
    $path = $dir . '/' . $form_state['values']['name'] . '.' . $form_state['values']['format'];
    if (!file_exists($path)) {
      file_copy($file, $path, FILE_EXISTS_ERROR);
    }
  }

  // Save the new pattern into the pattern files dir.
  $path = file_directory_path() . '/' . variable_get('patterns_save_xml', 'patterns') . '/' . $form_state['values']['name'] . '.' . $form_state['values']['format'];
  file_save_data($form_state['values']['xml'], $path, FILE_EXISTS_REPLACE);
  $old = db_result(db_query("SELECT file FROM {patterns} WHERE name = '%s'", $form_state['values']['name']));

  // Load and save pattern
  $load_func = 'patterns_load_' . $form_state['values']['format'];
  if ($pattern = $load_func($path)) {
    if ($old) {
      db_query("UPDATE {patterns} SET file = '%s', updated = '%s' WHERE pid = '%d'", $path, time(), $form_state['values']['pid']);
    }
    patterns_save_pattern($pattern, $path, $form_state['values']['name']);
    drupal_set_message(t('%name was saved.', array(
      '%name' => $form_state['values']['name'],
    )));
    $form_state['redirect'] = 'admin/build/patterns';
  }
  else {
    drupal_set_message(t("Pattern '%name' couldn't be saved. Make sure edited code is well-formed.", array(
      '%name' => $form_state['values']['name'],
    )), 'error');
  }
}