You are here

function patterns_io_save_pattern in Patterns 7

Same name and namespace in other branches
  1. 7.2 includes/io/io.inc \patterns_io_save_pattern()

Saves a pattern string or array into the database AND in the file system.

Updates the same file to the newer version, but does not replace an existing file (e.g. if we are moving a file to a new location).

Produces error messages if the pattern could not be saved.

Parameters

mixed $content The content of the pattern to be saved. Can be a string the: or an array represeting the pattern. In the latter case @param $original can be contain the string. If @param $original is missing the pattern is saved as a PHP array.

mixed $name The name of the pattern file.:

mixed $format file format (notice: it is not the extension!): (optional) The format of the pattern. Defaults to PATTERNS_FORMAT_YAML.

mixed $dir the destination directory:

mixed $original the string representation of the pattern. Optional.:

Return value

bool TRUE on success, FALSE otherwise.

TODO: if content is array and original is null, dump the array into the correct original file

6 calls to patterns_io_save_pattern()
patterns_d2d_push_patterns_server in patterns_d2d/includes/patterns_d2d.srpc.inc
patterns_edit_submit in includes/forms/editor.inc
Form submission handler for patterns_edit_form().
patterns_export_to_db in patterns_export/finalize.inc
Exports a pattern by importing it into the database
patterns_import_submit in includes/forms/import.inc
patterns_io_import_file in includes/io/import.inc

... See full list

File

includes/io/io.inc, line 393
Functions related to input/output operations.

Code

function patterns_io_save_pattern($content = NULL, $name = NULL, $format = PATTERNS_FORMAT_YAML, $dir = NULL, $original = NULL) {
  if (is_null($name)) {
    drupal_set_message(t('Cannot save pattern with \'NULL\' identifier.'), 'error');
    return FALSE;
  }
  if (!patterns_validate_pattern($content, $format, PATTERNS_VALIDATE_SYNTAX)) {
    drupal_set_message(t("Pattern '%name' could not be saved. Make sure edited code is well-formed.", array(
      '%name' => $name,
    )), 'error');
    return FALSE;
  }
  if (is_null($dir)) {
    $dir = patterns_path_get_files_dir();
  }
  if (!file_prepare_directory($dir, FILE_CREATE_DIRECTORY)) {
    drupal_set_message(t('Error: the pattern is not writable. Please check the file system permissions.'), 'error');
    return FALSE;
  }

  // Check if the file has a valid extension
  // and in case add the format at the end
  if (!_patterns_io_file_has_valid_extension($name)) {
    $name = $name . '.' . $format;
  }
  $path_original = $dir . '/' . $name;
  if (is_null($original)) {
    if (is_array($content)) {
      $original = patterns_parser_dump($content, $format);
    }
    else {
      $original = $content;
    }
  }
  $path = file_unmanaged_save_data($original, $path_original, FILE_EXISTS_REPLACE);
  if (!$path) {
    drupal_set_message(t('An error occurred while saving the file to %path. A file with the same name exists.', array(
      '%path' => $path_original,
    )), 'error');
    return FALSE;
  }

  // Load and save pattern.
  $load_function = patterns_parser_get_parser_function($format, PATTERNS_PARSER_LOAD);
  if (!$load_function) {
    drupal_set_message(t('Could not find a parser for ', array(
      '%name' => $name,
    )), 'error');

    // TODO: proper t()
    return FALSE;
  }
  $pattern = $load_function($path);
  if (!$pattern) {
    drupal_set_message(t("Pattern '%name' could not be saved into the database. Make sure edited code is well-formed.", array(
      '%name' => $name,
    )), 'error');
    patterns_io_remove_pattern_from_fs($path);
    return FALSE;
  }
  patterns_db_save_pattern($pattern, $path, $name, $format);
  $link = l($name, 'admin/patterns/edit/' . $name);
  drupal_set_message(t('Pattern !name was saved in %path.', array(
    '!name' => $link,
    '%path' => $path,
  )));
  return TRUE;
}