diff options
author | Tim Almdal <tnalmdal@shaw.ca> | 2009-12-09 09:50:57 -0800 |
---|---|---|
committer | Tim Almdal <tnalmdal@shaw.ca> | 2009-12-09 09:50:57 -0800 |
commit | c3a0f419c6f53fa93b47fa76f5afdc3696d64720 (patch) | |
tree | 9f21902e35d7fec23a9b6b23d0cf558076cf2d12 /modules/gallery/helpers/item.php | |
parent | 367f2218f6bc278a7d0bd870e03a5d631155871a (diff) |
Refactor the code to check for name or slug conflicts and to update code out of {Albums|Photos|Movies)_Controller and into the item helper so we can reuse it from the rest put handler.
Diffstat (limited to 'modules/gallery/helpers/item.php')
-rw-r--r-- | modules/gallery/helpers/item.php | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/modules/gallery/helpers/item.php b/modules/gallery/helpers/item.php index f8e6534e..6348c256 100644 --- a/modules/gallery/helpers/item.php +++ b/modules/gallery/helpers/item.php @@ -96,6 +96,51 @@ class item_Core { } } + + static function update($item, $fields) { + $dirty = false; + if ($item->id != 1 && !empty($fields["name"]) && $fields["name"] != $item->name) { + $item->rename($fields["name"]); + unset($fields["name"]); + $dirty = true; + } + foreach ($fields as $field => $value) { + if ($value !== $item->$field) { + $item->$field = $value; + $dirty = true; + } + } + + if ($dirty) { + $item->save(); + } + } + + static function check_for_conflicts($item, $new_name, $new_slug) { + $errors = array(); + + if ($row = Database::instance() + ->select(array("name", "slug")) + ->from("items") + ->where("parent_id", $item->parent_id) + ->where("id <>", $item->id) + ->open_paren() + ->where("name", $new_name) + ->orwhere("slug", $new_slug) + ->close_paren() + ->get() + ->current()) { + if ($row->name == $new_name) { + $errors["name_conflict"] = 1; + } + if ($row->slug == $new_slug) { + $errors["slug_conflict"] = 1; + } + } + + return $errors; + } + /** * Sanitize a filename into something presentable as an item title * @param string $filename |