From 418c0aab69a72da512646541be2d88a866cb9fdb Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sat, 8 Nov 2008 07:48:36 +0000 Subject: Create permanent owner_id column in the item table, and use a soft relationship to bind the two. To do this, I overrode __get in Item_Model so that $item->owner returns the appropriate User_Model. --- core/helpers/album.php | 4 ++-- core/helpers/core_installer.php | 1 + core/helpers/photo.php | 4 ++-- core/models/item.php | 25 ++++++++++++++++++++++++- modules/info/views/info_block.html.php | 12 +++++++----- modules/user/helpers/user_installer.php | 10 +++------- themes/default/views/album.html.php | 4 +++- 7 files changed, 42 insertions(+), 18 deletions(-) diff --git a/core/helpers/album.php b/core/helpers/album.php index 3c97f7e8..2801c570 100644 --- a/core/helpers/album.php +++ b/core/helpers/album.php @@ -32,13 +32,13 @@ class Album_Core { * @param string $description (optional) the longer description of this album * @return Item_Model */ - static function create($parent_id, $name, $title, $description=null, $user_id = null) { + static function create($parent_id, $name, $title, $description=null, $owner_id = null) { $album = ORM::factory("item"); $album->type = "album"; $album->title = $title; $album->description = $description; $album->name = $name; - $album->user_id = $user_id; + $album->owner_id = $owner_id; while (ORM::Factory("item") ->where("parent_id", $parent_id) diff --git a/core/helpers/core_installer.php b/core/helpers/core_installer.php index df194497..a3350f22 100644 --- a/core/helpers/core_installer.php +++ b/core/helpers/core_installer.php @@ -53,6 +53,7 @@ class core_installer { `thumbnail_height` int(9) default NULL, `resize_width` int(9) default NULL, `resize_height` int(9) default NULL, + `owner_id` int(9) default NULL, PRIMARY KEY (`id`), KEY `parent_id` (`parent_id`), KEY `type` (`type`)) diff --git a/core/helpers/photo.php b/core/helpers/photo.php index 231e3a9c..44350f2f 100644 --- a/core/helpers/photo.php +++ b/core/helpers/photo.php @@ -33,13 +33,13 @@ class Photo_Core { * @param string $description (optional) the longer description of this photo * @return Item_Model */ - static function create($parent_id, $filename, $name, $title, $description=null, $user_id = null) { + static function create($parent_id, $filename, $name, $title, $description=null, $owner_id = null) { $photo = ORM::factory("item"); $photo->type = "photo"; $photo->title = $title; $photo->description = $description; $photo->name = $name; - $photo->user_id = $user_id; + $photo->owner_id = $owner_id; $pi = pathinfo(basename($filename)); if (empty($pi["extension"])) { diff --git a/core/models/item.php b/core/models/item.php index c8d8d1e2..a32becc9 100644 --- a/core/models/item.php +++ b/core/models/item.php @@ -19,7 +19,8 @@ */ class Item_Model extends ORM_MPTT { protected $children = 'items'; - protected $has_one = array('user'); + + private $owner = null; /** * Is this item an album? @@ -166,4 +167,26 @@ class Item_Model extends ORM_MPTT { } return $path; } + + /** + * @see ORM::reload + */ + function reload() { + $this->owner = null; + return parent::reload(); + } + + /** + * @see ORM::__get() + */ + public function __get($column) { + if ($column == "owner") { + if (!isset($this->owner)) { + $this->owner = ORM::factory("user", $this->owner_id); + } + return $this->owner; + } else { + return parent::__get($column); + } + } } diff --git a/modules/info/views/info_block.html.php b/modules/info/views/info_block.html.php index 543ce3db..3fa9350f 100644 --- a/modules/info/views/info_block.html.php +++ b/modules/info/views/info_block.html.php @@ -2,21 +2,23 @@ - + - + - + + owner): ?> - - + + +
Title: title; ?>
Description: description; ?>
Name: name; ?>
Owner:user_id) ? $item->user->name : "anonymous"?>owner->name ?>
more \/ diff --git a/modules/user/helpers/user_installer.php b/modules/user/helpers/user_installer.php index 82221f10..93f36d99 100644 --- a/modules/user/helpers/user_installer.php +++ b/modules/user/helpers/user_installer.php @@ -19,7 +19,7 @@ */ class user_installer { protected $has_many = array('items'); - + public static function install() { Kohana::log("debug", "user_installer::install"); $db = Database::instance(); @@ -58,8 +58,6 @@ class user_installer { PRIMARY KEY (`group_id`, `user_id`), UNIQUE KEY(`user_id`, `group_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); - - $db->query("ALTER TABLE `items` ADD `user_id` INT(9) NULL"); $user_module = ORM::factory("module")->where("name", "user")->find(); $user_module->name = "user"; @@ -71,15 +69,14 @@ class user_installer { $user->display_name = "Gallery Administrator"; $user->save(); $id = $user->id; - $db->query("UPDATE `items` SET `user_id` = $id "); + $db->query("UPDATE `items` SET `owner_id` = $id WHERE `owner_id` IS NULL"); foreach (array("administrator", "registered") as $group_name) { $group = ORM::factory("group")->where("name", $group_name)->find(); $group->name = $group_name; $group->save(); if (!$group->add($user)) { - /** @todo replace this by throwing an exception once exceptions are implemented */ - Kohana::Log("debug", "{$user->name} was not added to {$group_name}"); + throw new Exception("@todo {$user->name} WAS_NOT_ADDED_TO {$group_name}"); } } } @@ -87,7 +84,6 @@ class user_installer { public static function uninstall() { $db = Database::instance(); - $db->query("ALTER TABLE `items` DROP `user_id`"); $db->query("DROP TABLE IF EXISTS `users`;"); $db->query("DROP TABLE IF EXISTS `groups`;"); $db->query("DROP TABLE IF EXISTS `groups_users`;"); diff --git a/themes/default/views/album.html.php b/themes/default/views/album.html.php index abac6d3f..8ec225cf 100644 --- a/themes/default/views/album.html.php +++ b/themes/default/views/album.html.php @@ -21,7 +21,9 @@

title ?>

-- cgit v1.2.3