From 6dbf5ae172e8336caa0971024ffdfa0148bcf718 Mon Sep 17 00:00:00 2001 From: Felix Rabinovich Date: Sat, 15 Nov 2008 08:19:14 +0000 Subject: Clean user / item relationship. Upload files gets the ownership of current user; and the views don't break if user module is not installed --- core/controllers/item.php | 15 +++++++++++++-- core/models/item.php | 9 ++++++++- themes/default/views/album.html.php | 8 +++++--- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/core/controllers/item.php b/core/controllers/item.php index 78ee7b0b..8bed14b4 100644 --- a/core/controllers/item.php +++ b/core/controllers/item.php @@ -44,13 +44,24 @@ class Item_Controller extends REST_Controller { case "photo": if (is_array($_FILES["file"]["name"])) { - for ($i = 0; $i < count($_FILES["file"]["name"]); $i++) { + $user = Session::instance()->get('user'); + if ($user) { + $user_id = $user->id; + } else { + try { + $user_id = ORM::factory("user")->find()->id; + } catch (Exception $e) { + $user_id = null; + } + } + for ($i = 0; $i < count($_FILES["file"]["name"]) - 1; $i++) { if ($_FILES["file"]["error"][$i] == 0) { $photo = photo::create( $item->id, $_FILES["file"]["tmp_name"][$i], $_FILES["file"]["name"][$i], - $_FILES["file"]["name"][$i]); + $_FILES["file"]["name"][$i], + '', $user_id); } else { // @todo return a reasonable error throw new Exception("@todo ERROR_IN_UPLOAD_FILE"); diff --git a/core/models/item.php b/core/models/item.php index 7209a46a..b592b706 100644 --- a/core/models/item.php +++ b/core/models/item.php @@ -19,7 +19,14 @@ */ class Item_Model extends ORM_MPTT { protected $children = 'items'; - protected $has_one = array('owner' => 'user'); + + function __construct($id) { + parent::__construct($id); + $module = ORM::factory("module")->where("name", 'user')->find(); + if ($module->loaded) { + $this->has_one = array('owner' => 'user'); + } + } /** * Is this item an album? diff --git a/themes/default/views/album.html.php b/themes/default/views/album.html.php index b59dba01..08a67b7b 100644 --- a/themes/default/views/album.html.php +++ b/themes/default/views/album.html.php @@ -21,9 +21,11 @@

title_edit ?>

-- cgit v1.2.3