diff options
author | Bharat Mediratta <bharat@menalto.com> | 2008-11-08 07:48:36 +0000 |
---|---|---|
committer | Bharat Mediratta <bharat@menalto.com> | 2008-11-08 07:48:36 +0000 |
commit | 418c0aab69a72da512646541be2d88a866cb9fdb (patch) | |
tree | fea9ea900fbf33d3e94082b901922ad74b832f50 | |
parent | ea7cc4f46edc730ff873c579a6c63490d24496a6 (diff) |
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.
-rw-r--r-- | core/helpers/album.php | 4 | ||||
-rw-r--r-- | core/helpers/core_installer.php | 1 | ||||
-rw-r--r-- | core/helpers/photo.php | 4 | ||||
-rw-r--r-- | core/models/item.php | 25 | ||||
-rw-r--r-- | modules/info/views/info_block.html.php | 12 | ||||
-rw-r--r-- | modules/user/helpers/user_installer.php | 10 | ||||
-rw-r--r-- | 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 @@ <table class="gMetadata"> <tbody> <tr> - <th>Title:</th> + <th><?= _("Title:") ?></th> <td><?= $item->title; ?></td> </tr> <tr> - <th>Description:</th> + <th><?= _("Description:") ?></th> <td><?= $item->description; ?></td> </tr> <tr> - <th>Name:</th> + <th><?= _("Name:") ?></th> <td><?= $item->name; ?></td> </tr> + <? if ($item->owner): ?> <tr> - <th>Owner:</th> - <td><a href="#"><?= isset($item->user_id) ? $item->user->name : "anonymous"?></a></td> + <th><?= _("Owner:") ?></th> + <td><a href="#"><?= $item->owner->name ?></a></td> </tr> + <? endif ?> <tr> <td colspan="2" class="toggle"> <a href="#">more \/</a> 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 @@ <h2><?= $child->title ?></h2> <ul class="gMetadata"> <li>Views: 321</li> - <li>By: <a href="#"><?= isset($child->user_id) ? $child->user->name : "anonymous"?></a></li> + <? if ($child->owner): ?> + <li><?= _("By:") ?><a href="#"><?= $child->owner->name ?></a></li> + <? endif ?> </ul> </li> <? endforeach ?> |