You are here

function patterns_db_save_pattern in Patterns 7

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

Writes the pattern metadata (and the actual pattern) to the database.

Parameters

array $pattern: The pattern represented as an array.

mixed $path: The new path where the file has just been saved or read from.

mixed $name: The name of the pattern file without the extension.

mixed $format the format of the pattern (YAML, XML, etc.):

2 calls to patterns_db_save_pattern()
patterns_io_save_pattern in includes/io/io.inc
Saves a pattern string or array into the database AND in the file system.
patterns_io_scan_directories in includes/io/io.inc
Scan directories looking for patterns files.

File

includes/db.inc, line 140
Retrieve, save, and remove patterns from the database.

Code

function patterns_db_save_pattern($pattern, $path, $name, $format = NULL) {
  $title = patterns_utils_init_from_pattern('title', $pattern);
  $description = patterns_utils_init_from_pattern('description', $pattern);
  $format = patterns_utils_init_from_pattern('format', $pattern, $format, PATTERNS_FORMAT_YAML);

  // Check if it is valid pattern: only syntax is validated, not the tags
  if (patterns_validate_pattern($pattern, $format, PATTERNS_VALIDATE_SYNTAX)) {
    $status = PATTERNS_STATUS_OK;
  }
  else {
    $status = PATTERNS_STATUS_INVALID;
  }
  $pattern_data = db_select('patterns', 'p')
    ->fields('p', array(
    'pid',
    'file',
    'updated',
    'status',
  ))
    ->condition('p.name', $name)
    ->condition('p.format', $format)
    ->execute()
    ->fetchAssoc();

  // Pattern already in the DB, data needs to be updated.
  if (!empty($pattern_data)) {

    // TODO: how to update the status correctly? Improve!
    if ($pattern_data['status'] == PATTERNS_STATUS_TRASHED) {
      $status = PATTERNS_STATUS_TRASHED;
    }
    elseif ($pattern_data['status'] == PATTERNS_STATUS_ENABLED) {
      if ($status == PATTERNS_STATUS_OK) {
        $status = PATTERNS_STATUS_ENABLED;
      }
    }
    $pid = $pattern_data['pid'];
    $updated = $pattern_data['updated'];
    $file = $pattern_data['file'];

    // Change of location.
    if ($path != $file) {
      $query_params = array(
        'file' => $path,
        'updated' => time(),
        'pid' => $pid,
      );
      db_query("UPDATE {patterns} SET file = :file, updated = :updated WHERE pid = :pid", $query_params);
    }
    if (($new_updated = filemtime($path)) > $updated) {
      $query_params = array(
        'pattern' => serialize($pattern),
        'title' => $title,
        'file' => $path,
        'updated' => $new_updated,
        'descr' => $description,
        'pid' => $pid,
        'status' => $status,
      );
      db_query("UPDATE {patterns} SET pattern = :pattern, title = :title, file = :file, updated = :updated, status = :status, description = :descr WHERE pid = :pid", $query_params);
    }
    else {
      $query_params = array(
        'pattern' => serialize($pattern),
        'title' => $title,
        'file' => $path,
        'descr' => $description,
        'pid' => $pid,
        'status' => $status,
      );
      db_query("UPDATE {patterns} SET pattern = :pattern, title = :title, file = :file, status = :status, description = :descr WHERE pid = :pid", $query_params);
    }
  }
  else {

    // Note: time() != REQUEST_TIME since it's a long process.
    $query_params = array(
      'name' => $name,
      'pattern' => serialize($pattern),
      'format' => $format,
      'title' => $title,
      'file' => $path,
      'descr' => $description,
      'time' => time(),
      'status' => $status,
    );
    db_query("INSERT INTO {patterns} (name, format, status, file, updated, enabled, title, description, pattern) VALUES ( :name, :format, :status, :file, :time, 0, :title, :descr, :pattern)", $query_params);
  }
  return TRUE;
}