summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2009-09-08 13:44:52 -0700
committerBharat Mediratta <bharat@menalto.com>2009-09-08 13:44:52 -0700
commit2aad580f53dbc06bb170c710467b47a5a532c6c8 (patch)
treefc6e91313a1eb594976990b51451d376c14e880b
parent60848480887594e61339000259e5322340518071 (diff)
Move specialized (pretty) url generation back into Item_Model so that
we're not relying on overriding url::site() to do tricks around item urls. This means that you won't get item urls by doing url::site("albums/37"), for example, but it also means that we won't get pretty urls where we don't expect them (like in the action of a <form> element). Incidentally, this will help us move over to using the slug format because if you've got a bad character in a url, the edit forms will now work on it since they'll be id based.
-rw-r--r--modules/gallery/controllers/admin_themes.php4
-rw-r--r--modules/gallery/controllers/after_install.php2
-rw-r--r--modules/gallery/controllers/albums.php16
-rw-r--r--modules/gallery/controllers/move.php2
-rw-r--r--modules/gallery/controllers/movies.php2
-rw-r--r--modules/gallery/controllers/photos.php2
-rw-r--r--modules/gallery/controllers/quick.php2
-rw-r--r--modules/gallery/helpers/MY_url.php19
-rw-r--r--modules/gallery/helpers/gallery.php44
-rw-r--r--modules/gallery/helpers/gallery_rss.php4
-rw-r--r--modules/gallery/helpers/item.php8
-rw-r--r--modules/gallery/models/item.php21
-rw-r--r--modules/gallery/views/admin_block_photo_stream.html.php2
-rw-r--r--modules/gallery/views/upgrader.html.php2
-rw-r--r--modules/notification/views/comment_published.html.php4
-rw-r--r--modules/notification/views/item_deleted.html.php4
-rw-r--r--modules/search/views/search.html.php2
-rw-r--r--modules/user/controllers/login.php2
-rw-r--r--modules/user/controllers/logout.php2
-rw-r--r--modules/user/controllers/password.php2
-rw-r--r--themes/admin_default/views/admin.html.php6
-rw-r--r--themes/default/views/page.html.php4
22 files changed, 80 insertions, 76 deletions
diff --git a/modules/gallery/controllers/admin_themes.php b/modules/gallery/controllers/admin_themes.php
index da001c55..24f91aba 100644
--- a/modules/gallery/controllers/admin_themes.php
+++ b/modules/gallery/controllers/admin_themes.php
@@ -38,7 +38,7 @@ class Admin_Themes_Controller extends Admin_Controller {
$theme_info = new ArrayObject(parse_ini_file($file), ArrayObject::ARRAY_AS_PROPS);
$theme_info->description = t($theme_info->description);
$theme_info->name = t($theme_info->name);
-
+
$themes[$theme_name] = $theme_info;
}
return $themes;
@@ -54,7 +54,7 @@ class Admin_Themes_Controller extends Admin_Controller {
if ($type == "admin") {
$view->url = url::site("admin?theme=$theme_name");
} else {
- $view->url = url::site("albums/1?theme=$theme_name");
+ $view->url = item::root()->url("theme=$theme_name");
}
print $view;
}
diff --git a/modules/gallery/controllers/after_install.php b/modules/gallery/controllers/after_install.php
index f066afe4..b640092f 100644
--- a/modules/gallery/controllers/after_install.php
+++ b/modules/gallery/controllers/after_install.php
@@ -20,7 +20,7 @@
class After_Install_Controller extends Controller {
public function index() {
if (!user::active()->admin) {
- url::redirect("albums/1");
+ url::redirect(item::root()->url());
}
$v = new View("after_install.html");
diff --git a/modules/gallery/controllers/albums.php b/modules/gallery/controllers/albums.php
index b7a9f339..abcabfa6 100644
--- a/modules/gallery/controllers/albums.php
+++ b/modules/gallery/controllers/albums.php
@@ -42,9 +42,9 @@ class Albums_Controller extends Items_Controller {
$index = $album->get_position($show);
$page = ceil($index / $page_size);
if ($page == 1) {
- url::redirect("albums/$album->id");
+ url::redirect($album->url());
} else {
- url::redirect("albums/$album->id?page=$page");
+ url::redirect($album->url("page=$page"));
}
}
@@ -55,9 +55,9 @@ class Albums_Controller extends Items_Controller {
// Make sure that the page references a valid offset
if ($page < 1) {
- url::redirect("albums/$album->id");
+ url::redirect($album->url());
} else if ($page > $max_pages) {
- url::redirect("albums/$album->id?page=$max_pages");
+ url::redirect($album->url("page=$max_pages"));
}
$template = new Theme_View("page.html", "album");
@@ -116,8 +116,8 @@ class Albums_Controller extends Items_Controller {
print json_encode(
array("result" => "success",
- "location" => url::site("albums/$new_album->id"),
- "resource" => url::site("albums/$new_album->id")));
+ "location" => $new_album->url(),
+ "resource" => $new_album->url()));
} else {
print json_encode(
array(
@@ -149,8 +149,8 @@ class Albums_Controller extends Items_Controller {
print json_encode(
array("result" => "success",
- "resource" => url::site("photos/$photo->id"),
- "location" => url::site("photos/$photo->id")));
+ "resource" => $photo->url(),
+ "location" => $photo->url()));
} else {
print json_encode(
array("result" => "error",
diff --git a/modules/gallery/controllers/move.php b/modules/gallery/controllers/move.php
index 93ef05a6..87b73436 100644
--- a/modules/gallery/controllers/move.php
+++ b/modules/gallery/controllers/move.php
@@ -43,7 +43,7 @@ class Move_Controller extends Controller {
print json_encode(
array("result" => "success",
- "location" => url::site("albums/{$target->id}")));
+ "location" => $target->url()));
}
public function show_sub_tree($source_id, $target_id) {
diff --git a/modules/gallery/controllers/movies.php b/modules/gallery/controllers/movies.php
index c549dbf8..1c266cc8 100644
--- a/modules/gallery/controllers/movies.php
+++ b/modules/gallery/controllers/movies.php
@@ -105,7 +105,7 @@ class Movies_Controller extends Items_Controller {
$photo->save();
module::event("item_edit_form_completed", $photo, $form);
- log::success("content", "Updated photo", "<a href=\"photos/$photo->id\">view</a>");
+ log::success("content", "Updated photo", "<a href=\"{$photo->url()}\">view</a>");
message::success(
t("Saved photo %photo_title", array("photo_title" => $photo->title)));
diff --git a/modules/gallery/controllers/photos.php b/modules/gallery/controllers/photos.php
index 959097b2..79ad674a 100644
--- a/modules/gallery/controllers/photos.php
+++ b/modules/gallery/controllers/photos.php
@@ -97,7 +97,7 @@ class Photos_Controller extends Items_Controller {
$photo->save();
module::event("item_edit_form_completed", $photo, $form);
- log::success("content", "Updated photo", "<a href=\"photos/$photo->id\">view</a>");
+ log::success("content", "Updated photo", "<a href=\"{$photo->url()}\">view</a>");
message::success(
t("Saved photo %photo_title",
array("photo_title" => html::purify($photo->title))));
diff --git a/modules/gallery/controllers/quick.php b/modules/gallery/controllers/quick.php
index 20731f9c..2ac54754 100644
--- a/modules/gallery/controllers/quick.php
+++ b/modules/gallery/controllers/quick.php
@@ -121,7 +121,7 @@ class Quick_Controller extends Controller {
print json_encode(array("result" => "success", "reload" => 1));
} else {
print json_encode(array("result" => "success",
- "location" => url::site("albums/$parent->id")));
+ "location" => $parent->url()));
}
}
diff --git a/modules/gallery/helpers/MY_url.php b/modules/gallery/helpers/MY_url.php
index 1ca9a58f..368c947e 100644
--- a/modules/gallery/helpers/MY_url.php
+++ b/modules/gallery/helpers/MY_url.php
@@ -18,25 +18,6 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class url extends url_Core {
- static function site($uri, $protocol=false) {
- if (($pos = strpos($uri, "?")) !== false) {
- list ($uri, $query) = explode("?", $uri, 2);
- $query = "?$query";
- } else {
- $query = "";
- }
-
- // @todo if we're only doing this for Item_Model, why not just put this
- // all into Item_Model::url()? It'd make url::site() faster. Downside is that
- // anywhere we refer to an item by id, eg url::site("albums/123") would have
- // to load the item and do $item->url();
- $parts = explode("/", $uri, 3);
- if ($parts[0] == "albums" || $parts[0] == "photos" || $parts[0] == "movies") {
- $uri = model_cache::get("item", $parts[1])->relative_url();
- }
- return parent::site($uri . $query, $protocol);
- }
-
static function parse_url() {
if (Router::$controller) {
return;
diff --git a/modules/gallery/helpers/gallery.php b/modules/gallery/helpers/gallery.php
index f72ef982..813134eb 100644
--- a/modules/gallery/helpers/gallery.php
+++ b/modules/gallery/helpers/gallery.php
@@ -82,9 +82,9 @@ class gallery_Core {
static function site_menu($menu, $theme) {
if ($theme->page_type != "login") {
$menu->append(Menu::factory("link")
- ->id("home")
- ->label(t("Home"))
- ->url(url::site("albums/1")));
+ ->id("home")
+ ->label(t("Home"))
+ ->url(item::root()->url()));
$item = $theme->item();
@@ -92,39 +92,39 @@ class gallery_Core {
$can_add = $item && access::can("add", $item);
if ($can_add) {
- $menu->append($add_menu = Menu::factory("submenu")
- ->id("add_menu")
- ->label(t("Add")));
+ $menu->append($add_menu = Menu::factory("submenu")
+ ->id("add_menu")
+ ->label(t("Add")));
$add_menu->append(Menu::factory("dialog")
- ->id("add_photos_item")
- ->label(t("Add photos"))
- ->url(url::site("simple_uploader/app/$item->id")));
+ ->id("add_photos_item")
+ ->label(t("Add photos"))
+ ->url(url::site("simple_uploader/app/$item->id")));
if ($item->is_album()) {
- $add_menu->append(Menu::factory("dialog")
- ->id("add_album_item")
- ->label(t("Add an album"))
- ->url(url::site("form/add/albums/$item->id?type=album")));
- }
+ $add_menu->append(Menu::factory("dialog")
+ ->id("add_album_item")
+ ->label(t("Add an album"))
+ ->url(url::site("form/add/albums/$item->id?type=album")));
+ }
}
$menu->append($options_menu = Menu::factory("submenu")
- ->id("options_menu")
- ->label(t("Photo options")));
+ ->id("options_menu")
+ ->label(t("Photo options")));
if ($item && ($can_edit || $can_add)) {
if ($can_edit) {
$options_menu->append(Menu::factory("dialog")
- ->id("edit_item")
- ->label($item->is_album() ? t("Edit album") : t("Edit photo"))
- ->url(url::site("form/edit/{$item->type}s/$item->id")));
+ ->id("edit_item")
+ ->label($item->is_album() ? t("Edit album") : t("Edit photo"))
+ ->url(url::site("form/edit/{$item->type}s/$item->id")));
}
if ($item->is_album()) {
$options_menu->label(t("Album options"));
if ($can_edit) {
$options_menu->append(Menu::factory("dialog")
- ->id("edit_permissions")
- ->label(t("Edit permissions"))
- ->url(url::site("permissions/browse/$item->id")));
+ ->id("edit_permissions")
+ ->label(t("Edit permissions"))
+ ->url(url::site("permissions/browse/$item->id")));
}
}
}
diff --git a/modules/gallery/helpers/gallery_rss.php b/modules/gallery/helpers/gallery_rss.php
index dee6ae40..f30df092 100644
--- a/modules/gallery/helpers/gallery_rss.php
+++ b/modules/gallery/helpers/gallery_rss.php
@@ -40,7 +40,7 @@ class gallery_rss_Core {
$feed->max_pages = ceil($all_children->find_all()->count() / $limit);
$feed->title = t("Recent Updates");
- $feed->link = url::abs_site("albums/1");
+ $feed->link = item::root()->abs_url();
$feed->description = t("Recent Updates");
return $feed;
@@ -54,7 +54,7 @@ class gallery_rss_Core {
$feed->max_pages = ceil(
$item->viewable()->descendants_count(array("type" => "photo")) / $limit);
$feed->title = html::purify($item->title);
- $feed->link = url::abs_site("albums/{$item->id}");
+ $feed->link = $item->abs_url();
$feed->description = nl2br(html::purify($item->description));
return $feed;
diff --git a/modules/gallery/helpers/item.php b/modules/gallery/helpers/item.php
index 8da88b6e..d907a177 100644
--- a/modules/gallery/helpers/item.php
+++ b/modules/gallery/helpers/item.php
@@ -184,4 +184,12 @@ class item_Core {
return $model;
}
+
+ /**
+ * Return the root Item_Model
+ * @return Item_Model
+ */
+ static function root() {
+ return model_cache::get("item", 1);
+ }
} \ No newline at end of file
diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php
index 0ec5d048..6e9debea 100644
--- a/modules/gallery/models/item.php
+++ b/modules/gallery/models/item.php
@@ -168,9 +168,24 @@ class Item_Model extends ORM_MPTT {
*
* @param string $query the query string (eg "show=3")
*/
- public function url($query=array(), $full_uri=false) {
- $url = ($full_uri ? url::abs_site("{$this->type}s/$this->id")
- : url::site("{$this->type}s/$this->id"));
+ public function url($query=null) {
+ $relative_url = $this->relative_url();
+ $url = url::site($relative_url);
+ if ($query) {
+ $url .= "?$query";
+ }
+ return $url;
+ }
+
+ /**
+ * album: url::abs_site("albums/2")
+ * photo: url::abs_site("photos/3")
+ *
+ * @param string $query the query string (eg "show=3")
+ */
+ public function abs_url($query=null) {
+ $relative_url = $this->relative_url();
+ $url = url::abs_site($relative_url);
if ($query) {
$url .= "?$query";
}
diff --git a/modules/gallery/views/admin_block_photo_stream.html.php b/modules/gallery/views/admin_block_photo_stream.html.php
index 1b9d8ff5..4968c39b 100644
--- a/modules/gallery/views/admin_block_photo_stream.html.php
+++ b/modules/gallery/views/admin_block_photo_stream.html.php
@@ -2,7 +2,7 @@
<ul>
<? foreach ($photos as $photo): ?>
<li class="gItem gPhoto">
- <a href="<?= url::site("photos/$photo->id") ?>" title="<?= html::purify($photo->title)->for_html_attr() ?>">
+ <a href="<?= $photo->url() ?>" title="<?= html::purify($photo->title)->for_html_attr() ?>">
<img <?= photo::img_dimensions($photo->width, $photo->height, 72) ?>
src="<?= $photo->thumb_url() ?>" alt="<?= html::purify($photo->title)->for_html_attr() ?>" />
</a>
diff --git a/modules/gallery/views/upgrader.html.php b/modules/gallery/views/upgrader.html.php
index 04200920..5f93c2d5 100644
--- a/modules/gallery/views/upgrader.html.php
+++ b/modules/gallery/views/upgrader.html.php
@@ -18,7 +18,7 @@
<h1> <?= t("That's it!") ?> </h1>
<p>
<?= t("Your <a href=\"%url\">Gallery</a> is up to date.",
- array("url" => html::mark_clean(url::site("albums/1")))) ?>
+ array("url" => html::mark_clean(item::root()->url()))) ?>
</p>
</div>
</div>
diff --git a/modules/notification/views/comment_published.html.php b/modules/notification/views/comment_published.html.php
index e39e39c6..a8ca1899 100644
--- a/modules/notification/views/comment_published.html.php
+++ b/modules/notification/views/comment_published.html.php
@@ -25,8 +25,8 @@
<tr>
<td><?= t("Url:") ?></td>
<td>
- <a href="<?= $comment->item()->url(array(), true) ?>#comments">
- <?= $comment->item()->url(array(), true) ?>#comments
+ <a href="<?= $comment->item()->abs_url() ?>#comments">
+ <?= $comment->item()->abs_url() ?>#comments
</a>
</td>
</tr>
diff --git a/modules/notification/views/item_deleted.html.php b/modules/notification/views/item_deleted.html.php
index a51782ff..a95cdd89 100644
--- a/modules/notification/views/item_deleted.html.php
+++ b/modules/notification/views/item_deleted.html.php
@@ -15,8 +15,8 @@
<tr>
<td><?= t("Url:") ?></td>
<td>
- <a href="<?= $item->parent()->url(array(), true) ?>">
- <?= $item->parent()->url(array(), true) ?>
+ <a href="<?= $item->parent()->abs_url() ?>">
+ <?= $item->parent()->abs_url() ?>
</a>
</td>
</tr>
diff --git a/modules/search/views/search.html.php b/modules/search/views/search.html.php
index 7963948d..91d9eec8 100644
--- a/modules/search/views/search.html.php
+++ b/modules/search/views/search.html.php
@@ -28,7 +28,7 @@
<? $item_class = "gAlbum"; ?>
<? endif ?>
<li class="gItem <?= $item_class ?>">
- <a href="<?= url::site("items/$item->id") ?>">
+ <a href="<?= $item->url() ?>">
<?= $item->thumb_img() ?>
<p>
<?= html::purify($item->title) ?>
diff --git a/modules/user/controllers/login.php b/modules/user/controllers/login.php
index b81b17b2..dcac0723 100644
--- a/modules/user/controllers/login.php
+++ b/modules/user/controllers/login.php
@@ -48,7 +48,7 @@ class Login_Controller extends Controller {
list ($valid, $form) = $this->_auth("login/auth_html");
if ($valid) {
- url::redirect("albums/1");
+ url::redirect(item::root()->url());
} else {
print $form;
}
diff --git a/modules/user/controllers/logout.php b/modules/user/controllers/logout.php
index fc3ced56..3bf274a6 100644
--- a/modules/user/controllers/logout.php
+++ b/modules/user/controllers/logout.php
@@ -31,7 +31,7 @@ class Logout_Controller extends Controller {
// Don't use url::redirect() because it'll call url::site() and munge the continue url.
header("Location: $continue_url");
} else {
- url::redirect("albums/1");
+ url::redirect(item::root()->url());
}
}
}
diff --git a/modules/user/controllers/password.php b/modules/user/controllers/password.php
index a6522369..8c18916e 100644
--- a/modules/user/controllers/password.php
+++ b/modules/user/controllers/password.php
@@ -130,7 +130,7 @@ class Password_Controller extends Controller {
$user->hash = null;
$user->save();
message::success(t("Password reset successfully"));
- url::redirect("albums/1");
+ url::redirect(item::root()->url());
} else {
print $view;
}
diff --git a/themes/admin_default/views/admin.html.php b/themes/admin_default/views/admin.html.php
index 9c5f1413..5564484c 100644
--- a/themes/admin_default/views/admin.html.php
+++ b/themes/admin_default/views/admin.html.php
@@ -43,10 +43,10 @@
<div id="gHeader">
<?= $theme->admin_header_top() ?>
<ul id="gLoginMenu">
- <li class="first"><?= html::anchor("albums/1", "&larr; ".t("Back to the Gallery")) ?></li>
- <li id="gLogoutLink"><a href="<?= url::site("logout?continue=albums/1&amp;csrf=$csrf") ?>"><?= t("Logout") ?></a></li>
+ <li class="first"><?= html::anchor(item::root()->url(), "&larr; ".t("Back to the Gallery")) ?></li>
+ <li id="gLogoutLink"><a href="<?= url::site("logout?continue=items/1&amp;csrf=$csrf") ?>"><?= t("Logout") ?></a></li>
</ul>
- <a id="gLogo" href="<?= url::site("albums/1") ?>" title="<?= t("go back to the Gallery")->for_html_attr() ?>">
+ <a id="gLogo" href="<?= item::root()->url() ?>" title="<?= t("go back to the Gallery")->for_html_attr() ?>">
&larr; <?= t("back to the ...") ?>
</a>
<div id="gSiteAdminMenu" style="display: none;">
diff --git a/themes/default/views/page.html.php b/themes/default/views/page.html.php
index 350ba41c..733534ea 100644
--- a/themes/default/views/page.html.php
+++ b/themes/default/views/page.html.php
@@ -81,7 +81,7 @@
<? if ($header_text = module::get_var("gallery", "header_text")): ?>
<?= $header_text ?>
<? else: ?>
- <a id="gLogo" href="<?= url::site("albums/1") ?>" title="<?= t("go back to the Gallery home")->for_html_attr() ?>">
+ <a id="gLogo" href="<?= item::root()->url() ?>" title="<?= t("go back to the Gallery home")->for_html_attr() ?>">
<img width="107" height="48" alt="<?= t("Gallery logo: Your photos on your web site")->for_html_attr() ?>" src="<?= url::file("lib/images/logo.png") ?>" />
</a>
<? endif ?>
@@ -95,7 +95,7 @@
<ul class="gBreadcrumbs">
<? foreach ($parents as $parent): ?>
<li>
- <a href="<?= url::site("albums/{$parent->id}?show={$theme->item()->id}") ?>">
+ <a href="<?= $parent->url("show={$theme->item()->id}") ?>">
<?= html::purify($parent->title) ?>
</a>
</li>