From c3a0f419c6f53fa93b47fa76f5afdc3696d64720 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Wed, 9 Dec 2009 09:50:57 -0800 Subject: 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. --- modules/gallery/helpers/item.php | 45 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'modules/gallery/helpers') 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 -- cgit v1.2.3