From: <bh...@us...> - 2009-01-29 03:22:04
|
Revision: 19930 http://gallery.svn.sourceforge.net/gallery/?rev=19930&view=rev Author: bharat Date: 2009-01-29 03:22:02 +0000 (Thu, 29 Jan 2009) Log Message: ----------- Add support for pretty urls. So now instead of urls like: http://example.com/gallery3/index.php/albums/2 http://example.com/gallery3/index.php/photos/9 You'll see urls like: http://example.com/gallery3/index.php/Family/Weddings http://example.com/gallery3/index.php/Family/Weddings/Bob.jpg Modified Paths: -------------- gallery3/trunk/core/helpers/MY_url.php gallery3/trunk/core/hooks/init_gallery.php gallery3/trunk/core/models/item.php gallery3/trunk/core/tests/Albums_Controller_Test.php gallery3/trunk/core/tests/Photos_Controller_Test.php Modified: gallery3/trunk/core/helpers/MY_url.php =================================================================== --- gallery3/trunk/core/helpers/MY_url.php 2009-01-29 03:17:09 UTC (rev 19929) +++ gallery3/trunk/core/helpers/MY_url.php 2009-01-29 03:22:02 UTC (rev 19930) @@ -18,6 +18,36 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class url extends url_Core { + static function site($uri, $protocol=false) { + list($controller, $arg1, $args) = explode("/", $uri, 3); + if ($controller == "albums" || $controller == "photos") { + $uri = ORM::factory("item", $arg1)->relative_path(); + } + return parent::site($uri, $protocol); + } + + static function parse_url() { + if (Router::$controller) { + return; + } + + $count = count(Router::$segments); + foreach (ORM::factory("item") + ->where("name", Router::$segments[$count - 1]) + ->where("level", $count + 1) + ->find_all() as $match) { + if ($match->relative_path() == Router::$current_uri) { + $item = $match; + } + } + + if (!empty($item)) { + Router::$controller = "{$item->type}s"; + Router::$controller_path = APPPATH . "controllers/{$item->type}s.php"; + Router::$method = $item->id; + } + } + /** * Just like url::file() except that it returns an absolute URI */ Modified: gallery3/trunk/core/hooks/init_gallery.php =================================================================== --- gallery3/trunk/core/hooks/init_gallery.php 2009-01-29 03:17:09 UTC (rev 19929) +++ gallery3/trunk/core/hooks/init_gallery.php 2009-01-29 03:22:02 UTC (rev 19930) @@ -20,3 +20,4 @@ Event::add("system.ready", array("I18n", "instance")); Event::add("system.post_routing", array("theme", "load_themes")); Event::add("system.ready", array("module", "load_modules")); +Event::add("system.post_routing", array("url", "parse_url")); Modified: gallery3/trunk/core/models/item.php =================================================================== --- gallery3/trunk/core/models/item.php 2009-01-29 03:17:09 UTC (rev 19929) +++ gallery3/trunk/core/models/item.php 2009-01-29 03:22:02 UTC (rev 19930) @@ -147,7 +147,7 @@ * photo: /var/albums/album1/album2/photo.jpg */ public function file_path() { - return VARPATH . "albums/" . $this->_relative_path(); + return VARPATH . "albums/" . $this->relative_path(); } /** @@ -156,8 +156,8 @@ */ public function file_url($full_uri=false) { return $full_uri ? - url::abs_file("var/albums/" . $this->_relative_path()) : - url::file("var/albums/" . $this->_relative_path()); + url::abs_file("var/albums/" . $this->relative_path()) : + url::file("var/albums/" . $this->relative_path()); } /** @@ -165,7 +165,7 @@ * photo: /var/albums/album1/photo.thumb.jpg */ public function thumb_path() { - return VARPATH . "thumbs/" . $this->_relative_path() . + return VARPATH . "thumbs/" . $this->relative_path() . ($this->type == "album" ? "/.album.jpg" : ""); } @@ -175,8 +175,8 @@ */ public function thumb_url($full_uri=true) { return ($full_uri ? - url::abs_file("var/thumbs/" . $this->_relative_path()) : - url::file("var/thumbs/" . $this->_relative_path())) . + url::abs_file("var/thumbs/" . $this->relative_path()) : + url::file("var/thumbs/" . $this->relative_path())) . ($this->type == "album" ? "/.album.jpg" : ""); } @@ -185,7 +185,7 @@ * photo: /var/albums/album1/photo.resize.jpg */ public function resize_path() { - return VARPATH . "resizes/" . $this->_relative_path() . + return VARPATH . "resizes/" . $this->relative_path() . ($this->type == "album" ? "/.album.jpg" : ""); } @@ -195,8 +195,8 @@ */ public function resize_url($full_uri=true) { return ($full_uri ? - url::abs_file("var/resizes/" . $this->_relative_path()) : - url::file("var/resizes/" . $this->_relative_path())) . + url::abs_file("var/resizes/" . $this->relative_path()) : + url::file("var/resizes/" . $this->relative_path())) . ($this->type == "album" ? "/.album.jpg" : ""); } @@ -204,7 +204,7 @@ * Return the relative path to this item's file. * @return string */ - private function _relative_path() { + public function relative_path() { if (empty($this->relative_path)) { foreach ($this->parents() as $parent) { if ($parent->id > 1) { Modified: gallery3/trunk/core/tests/Albums_Controller_Test.php =================================================================== --- gallery3/trunk/core/tests/Albums_Controller_Test.php 2009-01-29 03:17:09 UTC (rev 19929) +++ gallery3/trunk/core/tests/Albums_Controller_Test.php 2009-01-29 03:22:02 UTC (rev 19930) @@ -40,8 +40,7 @@ ob_end_clean(); $this->assert_equal( - json_encode(array("result" => "success", - "location" => "http://./index.php/albums/$album->id")), + json_encode(array("result" => "success", "location" => "http://./index.php/test")), $results); $this->assert_equal("new title", $album->title); $this->assert_equal("new description", $album->description); Modified: gallery3/trunk/core/tests/Photos_Controller_Test.php =================================================================== --- gallery3/trunk/core/tests/Photos_Controller_Test.php 2009-01-29 03:17:09 UTC (rev 19929) +++ gallery3/trunk/core/tests/Photos_Controller_Test.php 2009-01-29 03:22:02 UTC (rev 19930) @@ -41,7 +41,7 @@ $this->assert_equal( json_encode(array("result" => "success", - "location" => "http://./index.php/photos/$photo->id")), + "location" => "http://./index.php/test.jpeg")), $results); $this->assert_equal("new title", $photo->title); $this->assert_equal("new description", $photo->description); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |