function patterns_db_save_pattern in Patterns 7.2
Same name and namespace in other branches
- 7 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.)
mixed $username (Optional): A name for the author/uploader of the pattern. Defaults, logged user
string $uuuid (Optional): Optional argument to force the use of certain UUUID (i.e.: while clonning)
2 calls to patterns_db_save_pattern()
- patterns_io_scan_directories in includes/
io/ io.inc - Scan directories looking for patterns files.
- _patterns_io_save_pattern in includes/
io/ io.inc - Lower level primitive for patterns_io_save_pattern. Includes an optional argument to force the UUUID
File
- includes/
db.inc, line 145 - Retrieve, save, and remove patterns from the database.
Code
function patterns_db_save_pattern($pattern, $path, $name, $format = NULL, $username = NULL, $uuuid = 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);
if (is_null($username)) {
global $user;
$username = $user->name;
}
// 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,
'uuuid' => patterns_utils_get_uuuid(),
'author' => $username,
);
db_query("UPDATE {patterns} SET file = :file, updated = :updated , uuuid = :uuuid , author = :author 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,
'uuuid' => is_null($uuuid) ? patterns_utils_get_uuuid() : $uuuid,
'author' => $username,
);
db_query("UPDATE {patterns} SET pattern = :pattern, title = :title, file = :file, updated = :updated, status = :status, description = :descr , uuuid = :uuuid, author = :author WHERE pid = :pid", $query_params);
}
else {
$query_params = array(
'pattern' => serialize($pattern),
'title' => $title,
'file' => $path,
'descr' => $description,
'pid' => $pid,
'status' => $status,
'uuuid' => is_null($uuuid) ? patterns_utils_get_uuuid() : $uuuid,
'author' => $username,
);
db_query("UPDATE {patterns} SET pattern = :pattern, title = :title, file = :file, status = :status, description = :descr , uuuid = :uuuid , author = :author 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,
'uuuid' => is_null($uuuid) ? patterns_utils_get_uuuid() : $uuuid,
'author' => $username,
);
db_query("INSERT INTO {patterns} (name, format, status, file, updated, enabled, title, description, pattern, uuuid, author) VALUES ( :name, :format, :status, :file, :time, 0, :title, :descr, :pattern, :uuuid, :author)", $query_params);
}
return TRUE;
}