You are here

function patterns_save_pattern in Patterns 5

Same name and namespace in other branches
  1. 6.2 patterns.module \patterns_save_pattern()
  2. 6 patterns.module \patterns_save_pattern()
2 calls to patterns_save_pattern()
patterns_edit_submit in ./patterns.module
Submit edits to the pattern
patterns_get_patterns in ./patterns.module

File

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

Code

function patterns_save_pattern($pattern, $xmlpath = '') {
  $name = basename($xmlpath, '.xml');
  foreach ($pattern[0] as $index => $value) {
    if (is_numeric($index)) {
      switch ($value['tag']) {
        case 'title':
          $title = $value['value'];
          break;
        case 'description':
          $description = $value['value'];
          break;
        case 'author':
          $author = $value['value'];
          break;
      }
    }
  }
  if ($pid = db_result(db_query('SELECT pid FROM {patterns} WHERE name = "%s"', $name))) {
    $updated = db_result(db_query('SELECT updated FROM {patterns} WHERE pid = "%d"', $pid));
    if (($new_updated = filemtime(file_create_path($xmlpath))) > $updated) {
      db_query('UPDATE {patterns} SET pattern = "%s", title = "%s", file = "%s", updated = "%s", description = "%s" WHERE pid = %d', serialize($pattern), $title, $xmlpath, $new_updated, $description, $pid);
    }
    else {
      db_query('UPDATE {patterns} SET pattern = "%s", title = "%s", file = "%s", description = "%s" WHERE pid = %d', serialize($pattern), $title, $xmlpath, $description, $pid);
    }
  }
  else {
    $pid = db_next_id('{patterns}_pid');
    db_query('INSERT INTO {patterns} VALUES(%d, "%s", 0, "%s", "%s", 0, "%s", "%s", "%s")', $pid, $name, $xmlpath, time(), $title, $description, serialize($pattern));
  }
}