From f77b26e988048bfd31d1e59695fe7ca015696a24 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Thu, 26 Nov 2009 14:38:01 -0800 Subject: Add kohana23_compat module to bring along functionality that got deleted, but we still want. Start off with the Pagination library. --- modules/kohana23_compat/config/pagination.php | 25 +++ modules/kohana23_compat/libraries/Pagination.php | 234 +++++++++++++++++++++++ 2 files changed, 259 insertions(+) create mode 100644 modules/kohana23_compat/config/pagination.php create mode 100644 modules/kohana23_compat/libraries/Pagination.php (limited to 'modules/kohana23_compat') diff --git a/modules/kohana23_compat/config/pagination.php b/modules/kohana23_compat/config/pagination.php new file mode 100644 index 00000000..808fc315 --- /dev/null +++ b/modules/kohana23_compat/config/pagination.php @@ -0,0 +1,25 @@ + 'pagination', + 'style' => 'classic', + 'uri_segment' => 3, + 'query_string' => '', + 'items_per_page' => 20, + 'auto_hide' => FALSE, +); diff --git a/modules/kohana23_compat/libraries/Pagination.php b/modules/kohana23_compat/libraries/Pagination.php new file mode 100644 index 00000000..8ff8bf94 --- /dev/null +++ b/modules/kohana23_compat/libraries/Pagination.php @@ -0,0 +1,234 @@ +initialize($config); + } + + /** + * Sets config values. + * + * @throws Kohana_Exception + * @param array configuration settings + * @return void + */ + public function initialize($config = array()) + { + // Load config group + if (isset($config['group'])) + { + // Load and validate config group + if ( ! is_array($group_config = Kohana::config('pagination.'.$config['group']))) + throw new Kohana_Exception('pagination.undefined_group: ' . $config['group']); + + // All pagination config groups inherit default config group + if ($config['group'] !== 'default') + { + // Load and validate default config group + if ( ! is_array($default_config = Kohana::config('pagination.default'))) + throw new Kohana_Exception('pagination.undefined_group: default'); + + // Merge config group with default config group + $group_config += $default_config; + } + + // Merge custom config items with config group + $config += $group_config; + } + + // Assign config values to the object + foreach ($config as $key => $value) + { + if (property_exists($this, $key)) + { + $this->$key = $value; + } + } + + // Clean view directory + $this->directory = trim($this->directory, '/').'/'; + + // Build generic URL with page in query string + if ($this->query_string !== '') + { + // Extract current page + $this->current_page = isset($_GET[$this->query_string]) ? (int) $_GET[$this->query_string] : 1; + + // Insert {page} placeholder + $_GET[$this->query_string] = '{page}'; + + // Create full URL + $base_url = ($this->base_url === '') ? Router::$current_uri : $this->base_url; + $this->url = url::site($base_url).'?'.str_replace('%7Bpage%7D', '{page}', http_build_query($_GET)); + + // Reset page number + $_GET[$this->query_string] = $this->current_page; + } + + // Build generic URL with page as URI segment + else + { + // Use current URI if no base_url set + $this->url = ($this->base_url === '') ? Router::$segments : explode('/', trim($this->base_url, '/')); + + // Convert uri 'label' to corresponding integer if needed + if (is_string($this->uri_segment)) + { + if (($key = array_search($this->uri_segment, $this->url)) === FALSE) + { + // If uri 'label' is not found, auto add it to base_url + $this->url[] = $this->uri_segment; + $this->uri_segment = count($this->url) + 1; + } + else + { + $this->uri_segment = $key + 2; + } + } + + // Insert {page} placeholder + $this->url[$this->uri_segment - 1] = '{page}'; + + // Create full URL + $this->url = url::site(implode('/', $this->url)).Router::$query_string; + + // Extract current page + $this->current_page = URI::instance()->segment($this->uri_segment); + } + + // Core pagination values + $this->total_items = (int) max(0, $this->total_items); + $this->items_per_page = (int) max(1, $this->items_per_page); + $this->total_pages = (int) ceil($this->total_items / $this->items_per_page); + $this->current_page = (int) min(max(1, $this->current_page), max(1, $this->total_pages)); + $this->current_first_item = (int) min((($this->current_page - 1) * $this->items_per_page) + 1, $this->total_items); + $this->current_last_item = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items); + + // If there is no first/last/previous/next page, relative to the + // current page, value is set to FALSE. Valid page number otherwise. + $this->first_page = ($this->current_page === 1) ? FALSE : 1; + $this->last_page = ($this->current_page >= $this->total_pages) ? FALSE : $this->total_pages; + $this->previous_page = ($this->current_page > 1) ? $this->current_page - 1 : FALSE; + $this->next_page = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE; + + // SQL values + $this->sql_offset = (int) ($this->current_page - 1) * $this->items_per_page; + $this->sql_limit = sprintf(' LIMIT %d OFFSET %d ', $this->items_per_page, $this->sql_offset); + } + + /** + * Generates the HTML for the chosen pagination style. + * + * @param string pagination style + * @return string pagination html + */ + public function render($style = NULL) + { + // Hide single page pagination + if ($this->auto_hide === TRUE AND $this->total_pages <= 1) + return ''; + + if ($style === NULL) + { + // Use default style + $style = $this->style; + } + + // Return rendered pagination view + return View::factory($this->directory.$style, get_object_vars($this))->render(); + } + + /** + * Magically converts Pagination object to string. + * + * @return string pagination html + */ + public function __toString() + { + return $this->render(); + } + + /** + * Magically gets a pagination variable. + * + * @param string variable key + * @return mixed variable value if the key is found + * @return void if the key is not found + */ + public function __get($key) + { + if (isset($this->$key)) + return $this->$key; + } + + /** + * Adds a secondary interface for accessing properties, e.g. $pagination->total_pages(). + * Note that $pagination->total_pages is the recommended way to access properties. + * + * @param string function name + * @return string + */ + public function __call($func, $args = NULL) + { + return $this->__get($func); + } + +} // End Pagination Class \ No newline at end of file -- cgit v1.2.3 From 54be15191b983e72b4643f3559303b35b7c48795 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Thu, 26 Nov 2009 18:47:40 -0800 Subject: Overload Database_Builder to add merge_where() which takes predefined where clauses and adds them to the existing query. Update all existing queries that take an additional where clause to use it. --- modules/gallery/controllers/movies.php | 2 +- modules/gallery/controllers/photos.php | 2 +- modules/gallery/libraries/ORM_MPTT.php | 8 +++--- modules/gallery/models/item.php | 16 ++++++----- .../libraries/MY_Database_Builder.php | 31 ++++++++++++++++++++++ 5 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 modules/kohana23_compat/libraries/MY_Database_Builder.php (limited to 'modules/kohana23_compat') diff --git a/modules/gallery/controllers/movies.php b/modules/gallery/controllers/movies.php index 7ceeefdf..157c388f 100644 --- a/modules/gallery/controllers/movies.php +++ b/modules/gallery/controllers/movies.php @@ -21,7 +21,7 @@ class Movies_Controller extends Items_Controller { public function _show($movie) { access::required("view", $movie); - $where = array("type != " => "album"); + $where = array(array("type", "!=", "album")); $position = $movie->parent()->get_position($movie, $where); if ($position > 1) { list ($previous_item, $ignore, $next_item) = diff --git a/modules/gallery/controllers/photos.php b/modules/gallery/controllers/photos.php index 0d7daac4..478447b1 100644 --- a/modules/gallery/controllers/photos.php +++ b/modules/gallery/controllers/photos.php @@ -21,7 +21,7 @@ class Photos_Controller extends Items_Controller { public function _show($photo) { access::required("view", $photo); - $where = array("type != " => "album"); + $where = array(array("type", "!=", "album")); $position = $photo->parent()->get_position($photo, $where); if ($position > 1) { list ($previous_item, $ignore, $next_item) = diff --git a/modules/gallery/libraries/ORM_MPTT.php b/modules/gallery/libraries/ORM_MPTT.php index 01b2d7b7..90b9d38a 100644 --- a/modules/gallery/libraries/ORM_MPTT.php +++ b/modules/gallery/libraries/ORM_MPTT.php @@ -152,7 +152,7 @@ class ORM_MPTT_Core extends ORM { */ function children($limit=null, $offset=0, $where=null, $order_by=array("id" => "ASC")) { if ($where) { - $this->where($where); + $this->merge_where($where); } return $this @@ -170,7 +170,7 @@ class ORM_MPTT_Core extends ORM { */ function children_count($where=null) { if ($where) { - $this->where($where); + $this->merge_where($where); } return $this @@ -189,7 +189,7 @@ class ORM_MPTT_Core extends ORM { */ function descendants($limit=null, $offset=0, $where=null, $order_by=array("id" => "ASC")) { if ($where) { - $this->where($where); + $this->merge_where($where); } return $this @@ -207,7 +207,7 @@ class ORM_MPTT_Core extends ORM { */ function descendants_count($where=null) { if ($where) { - $this->where($where); + $this->merge_where($where); } return $this diff --git a/modules/gallery/models/item.php b/modules/gallery/models/item.php index c8d25cc5..acc4e96f 100644 --- a/modules/gallery/models/item.php +++ b/modules/gallery/models/item.php @@ -428,14 +428,14 @@ class Item_Model extends ORM_MPTT { } else { $comp = "<"; } - $db = Database::instance(); + $db = db::build(); // If the comparison column has NULLs in it, we can't use comparators on it and will have to // deal with it the hard way. $count = $db->from("items") ->where("parent_id", "=", $this->id) ->where($this->sort_column, "=", NULL) - ->where($where) + ->merge_where($where) ->count_records(); if (empty($count)) { @@ -445,7 +445,7 @@ class Item_Model extends ORM_MPTT { $position = $db->from("items") ->where("parent_id", "=", $this->id) ->where($sort_column, $comp, $child->$sort_column) - ->where($where) + ->merge_where($where) ->count_records(); // We stopped short of our target value in the sort (notice that we're using a < comparator @@ -456,12 +456,14 @@ class Item_Model extends ORM_MPTT { // // Fix this by doing a 2nd query where we iterate over the equivalent columns and add them to // our base value. - foreach ($db->from("items") + foreach ($db + ->select("id") + ->from("items") ->where("parent_id", "=", $this->id) ->where($sort_column, "=", $child->$sort_column) - ->where($where) + ->merge_where($where) ->order_by(array("id" => "ASC")) - ->get() as $row) { + ->execute() as $row) { $position++; if ($row->id == $child->id) { break; @@ -485,7 +487,7 @@ class Item_Model extends ORM_MPTT { foreach ($db->select("id") ->from("items") ->where("parent_id", "=", $this->id) - ->where($where) + ->merge_where($where) ->order_by($order_by) ->get() as $row) { $position++; diff --git a/modules/kohana23_compat/libraries/MY_Database_Builder.php b/modules/kohana23_compat/libraries/MY_Database_Builder.php new file mode 100644 index 00000000..974f9c6d --- /dev/null +++ b/modules/kohana23_compat/libraries/MY_Database_Builder.php @@ -0,0 +1,31 @@ +where($tuple[0], $tuple[1], $tuple[2]); + } + return $this; + } +} \ No newline at end of file -- cgit v1.2.3 From cc4d7c672c863185bab5d654222e205d6517e98a Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 21 Dec 2009 15:40:28 -0800 Subject: Update database tests for K24. Use a mock database that we load through the framework so that we're properly testing the Database_Builder, it's a lot cleaner than what we had before. --- modules/gallery/tests/Database_Test.php | 105 +++++++++++++-------- .../libraries/MY_Database_Builder.php | 4 + 2 files changed, 72 insertions(+), 37 deletions(-) (limited to 'modules/kohana23_compat') diff --git a/modules/gallery/tests/Database_Test.php b/modules/gallery/tests/Database_Test.php index 4259961e..6aa186e5 100644 --- a/modules/gallery/tests/Database_Test.php +++ b/modules/gallery/tests/Database_Test.php @@ -18,17 +18,27 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class Database_Test extends Unit_Test_Case { + function setup() { + $config = Kohana_Config::instance(); + $config->set("database.mock.connection.type", "mock"); + $config->set("database.mock.cache", false); + $config->set("database.mock.table_prefix", "g_"); + } + function simple_where_test() { - $sql = Database_Builder_For_Test::instance() + $sql = db::build("mock") + ->select("some_column") + ->from("some_table") ->where("a", "=", 1) ->where("b", "=", 2) ->compile(); $sql = str_replace("\n", " ", $sql); - $this->assert_same("SELECT * WHERE `a` = 1 AND `b` = 2", $sql); + $this->assert_same("SELECT [some_column] FROM [some_table] WHERE [a] = [1] AND [b] = [2]", $sql); } function compound_where_test() { - $sql = Database_Builder_For_Test::instance() + $sql = db::build("mock") + ->select() ->where("outer1", "=", 1) ->and_open() ->where("inner1", "=", 1) @@ -38,12 +48,13 @@ class Database_Test extends Unit_Test_Case { ->compile(); $sql = str_replace("\n", " ", $sql); $this->assert_same( - "SELECT * WHERE `outer1` = 1 AND (`inner1` = 1 OR `inner2` = 2) AND `outer2` = 2", + "SELECT [*] WHERE [outer1] = [1] AND ([inner1] = [1] OR [inner2] = [2]) AND [outer2] = [2]", $sql); } function group_first_test() { - $sql = Database_Builder_For_Test::instance() + $sql = db::build("mock") + ->select() ->and_open() ->where("inner1", "=", 1) ->or_where("inner2", "=", 2) @@ -53,12 +64,13 @@ class Database_Test extends Unit_Test_Case { ->compile(); $sql = str_replace("\n", " ", $sql); $this->assert_same( - "SELECT * WHERE (`inner1` = 1 OR `inner2` = 2) AND `outer1` = 1 AND `outer2` = 2", + "SELECT [*] WHERE ([inner1] = [1] OR [inner2] = [2]) AND [outer1] = [1] AND [outer2] = [2]", $sql); } function where_array_test() { - $sql = Database_Builder_For_Test::instance() + $sql = db::build("mock") + ->select() ->where("outer1", "=", 1) ->and_open() ->where("inner1", "=", 1) @@ -68,32 +80,33 @@ class Database_Test extends Unit_Test_Case { ->compile(); $sql = str_replace("\n", " ", $sql); $this->assert_same( - "SELECT * WHERE `outer1` = 1 AND (`inner1` = 1 OR `inner2` = 2 OR `inner3` = 3)", + "SELECT [*] WHERE [outer1] = [1] AND ([inner1] = [1] OR [inner2] = [2] OR [inner3] = [3])", $sql); } function notlike_test() { - $sql = Database_Builder_For_Test::instance() + $sql = db::build("mock") + ->select() ->where("outer1", "=", 1) ->or_open() - ->where("inner1", "NOT LIKE", 1) + ->where("inner1", "NOT LIKE", "%1%") ->close() ->compile(); $sql = str_replace("\n", " ", $sql); $this->assert_same( - "SELECT * WHERE `outer1` = 1 OR ( `inner1` NOT LIKE '%1%')", + "SELECT [*] WHERE [outer1] = [1] OR ([inner1] NOT LIKE [%1%])", $sql); } function prefix_replacement_test() { - $db = Database::instance(); - $converted = $db->add_table_prefixes("CREATE TABLE IF NOT EXISTS {test_tables} ( + $db = Database::instance("mock"); + $converted = $db->add_table_prefixes("CREATE TABLE IF NOT EXISTS {test} ( `id` int(9) NOT NULL auto_increment, `name` varchar(32) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY(`name`)) ENGINE=InnoDB DEFAULT CHARSET=utf8"); - $expected = "CREATE TABLE IF NOT EXISTS g3test_test_tables ( + $expected = "CREATE TABLE IF NOT EXISTS g_test ( `id` int(9) NOT NULL auto_increment, `name` varchar(32) NOT NULL, PRIMARY KEY (`id`), @@ -101,16 +114,16 @@ class Database_Test extends Unit_Test_Case { ENGINE=InnoDB DEFAULT CHARSET=utf8"; $this->assert_same($expected, $converted); - $sql = "UPDATE {test_tables} SET `name` = '{test string}' " . + $sql = "UPDATE {test} SET `name` = '{test string}' " . "WHERE `item_id` IN " . - " (SELECT `id` FROM {items} " . + " (SELECT `id` FROM {test} " . " WHERE `left_ptr` >= 1 " . " AND `right_ptr` <= 6)"; $sql = $db->add_table_prefixes($sql); - $expected = "UPDATE g3test_test_tables SET `name` = '{test string}' " . + $expected = "UPDATE g_test SET `name` = '{test string}' " . "WHERE `item_id` IN " . - " (SELECT `id` FROM g3test_items " . + " (SELECT `id` FROM g_test " . " WHERE `left_ptr` >= 1 " . " AND `right_ptr` <= 6)"; @@ -118,34 +131,52 @@ class Database_Test extends Unit_Test_Case { } function prefix_no_replacement_test() { - $update = Database_Builder_For_Test::instance() + $sql = db::build("mock") ->from("test_tables") ->where("1", "=", "1") ->set(array("name" => "Test Name")) - ->update(); + ->update() + ->compile(); + $sql = str_replace("\n", " ", $sql); + $this->assert_same("UPDATE [test_tables] SET [name] = [Test Name] WHERE [1] = [1]", $sql); + } +} - $expected = "UPDATE `g3test_test_tables` SET `name` = 'Test Name' WHERE 1 = 1"; +class Database_Mock extends Database { + public function connect() { + } - $this->assert_same($expected, $update); + public function disconnect() { } -} -class Database_Builder_For_Test extends Database_Builder { - static function instance() { - $db = new Database_Builder_For_Test(); - $db->_table_names["{items}"] = "g3test_items"; - $db->config["table_prefix"] = "g3test_"; - return $db; + public function set_charset($charset) { } - public function query($sql = '') { - if (!empty($sql)) { - $sql = $this->add_table_prefixes($sql); - } - return $sql; + public function query_execute($sql) { } - public function compile() { - return parent::compile(); + public function escape($val) { } -} + + public function list_constraints($table) { + } + + public function list_fields($table) { + } + + public function list_tables() { + return array("test"); + } + + public function quote_column($val) { + return "[$val]"; + } + + public function quote_table($val) { + return "[$val]"; + } + + public function quote($val) { + return "[$val]"; + } +} \ No newline at end of file diff --git a/modules/kohana23_compat/libraries/MY_Database_Builder.php b/modules/kohana23_compat/libraries/MY_Database_Builder.php index 974f9c6d..54a10860 100644 --- a/modules/kohana23_compat/libraries/MY_Database_Builder.php +++ b/modules/kohana23_compat/libraries/MY_Database_Builder.php @@ -28,4 +28,8 @@ class Database_Builder extends Database_Builder_Core { } return $this; } + + public function compile() { + return parent::compile(); + } } \ No newline at end of file -- cgit v1.2.3 From 9c5df1d31bd214fab051b71d092c751a1da20ecc Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 21 Dec 2009 19:59:44 -0800 Subject: Fix preambles, and fix the File_Structure_Test to be more lenient because of preamble variation in K24. --- application/Bootstrap.php | 22 +++++++++---- modules/gallery/config/log_file.php | 20 ++++++++++- modules/gallery/tests/File_Structure_Test.php | 16 ++++++--- modules/kohana23_compat/config/pagination.php | 42 +++++++++++++----------- modules/kohana23_compat/libraries/Pagination.php | 20 ++++++++++- 5 files changed, 87 insertions(+), 33 deletions(-) (limited to 'modules/kohana23_compat') diff --git a/application/Bootstrap.php b/application/Bootstrap.php index f36fac14..95f7b8d2 100644 --- a/application/Bootstrap.php +++ b/application/Bootstrap.php @@ -1,13 +1,21 @@ -\n"; + $expected_2 = " 'pagination', - 'style' => 'classic', - 'uri_segment' => 3, - 'query_string' => '', - 'items_per_page' => 20, - 'auto_hide' => FALSE, +$config["default"] = array( + "directory" => "pagination", + "style" => "classic", + "uri_segment" => 3, + "query_string" => "", + "items_per_page" => 20, + "auto_hide" => FALSE ); diff --git a/modules/kohana23_compat/libraries/Pagination.php b/modules/kohana23_compat/libraries/Pagination.php index 8ff8bf94..012bc484 100644 --- a/modules/kohana23_compat/libraries/Pagination.php +++ b/modules/kohana23_compat/libraries/Pagination.php @@ -1,4 +1,22 @@ - Date: Tue, 22 Dec 2009 13:50:52 -0800 Subject: Add merge_or_where() to MY_Datatabase_Builder and use that instead of or_where() for compatibility and convenience. Caught by failing unit tests. --- modules/gallery/helpers/item.php | 4 ++-- modules/kohana23_compat/libraries/MY_Database_Builder.php | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'modules/kohana23_compat') diff --git a/modules/gallery/helpers/item.php b/modules/gallery/helpers/item.php index b7be23cd..f6181f8a 100644 --- a/modules/gallery/helpers/item.php +++ b/modules/gallery/helpers/item.php @@ -155,12 +155,12 @@ class item_Core { $view_restrictions = array(); if (!identity::active_user()->admin) { foreach (identity::group_ids_for_active_user() as $id) { - $view_restrictions["items.view_$id"] = access::ALLOW; + $view_restrictions[] = array("items.view_$id", "=", access::ALLOW); } } if (count($view_restrictions)) { - $model->and_open()->or_where($view_restrictions)->close(); + $model->and_open()->merge_or_where($view_restrictions)->close(); } return $model; diff --git a/modules/kohana23_compat/libraries/MY_Database_Builder.php b/modules/kohana23_compat/libraries/MY_Database_Builder.php index 54a10860..c82b6ac4 100644 --- a/modules/kohana23_compat/libraries/MY_Database_Builder.php +++ b/modules/kohana23_compat/libraries/MY_Database_Builder.php @@ -29,6 +29,17 @@ class Database_Builder extends Database_Builder_Core { return $this; } + /** + * Merge in a series of where clause tuples and call or_where() on each one. + * @chainable + */ + public function merge_or_where($tuples) { + foreach ($tuples as $tuple) { + $this->or_where($tuple[0], $tuple[1], $tuple[2]); + } + return $this; + } + public function compile() { return parent::compile(); } -- cgit v1.2.3 From d711c5b9300a17e7269415573a15e8a63f0d149c Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Tue, 22 Dec 2009 15:36:36 -0800 Subject: Convert tabs to spaces. Enough to get the file structure test to pass, but not really the Gallery coding convention -- this is a compatibility class though. --- modules/kohana23_compat/libraries/Pagination.php | 438 +++++++++++------------ 1 file changed, 219 insertions(+), 219 deletions(-) (limited to 'modules/kohana23_compat') diff --git a/modules/kohana23_compat/libraries/Pagination.php b/modules/kohana23_compat/libraries/Pagination.php index 012bc484..2b06f359 100644 --- a/modules/kohana23_compat/libraries/Pagination.php +++ b/modules/kohana23_compat/libraries/Pagination.php @@ -29,224 +29,224 @@ */ class Pagination_Core { - // Config values - protected $base_url = ''; - protected $directory = 'pagination'; - protected $style = 'classic'; - protected $uri_segment = 3; - protected $query_string = ''; - protected $items_per_page = 20; - protected $total_items = 0; - protected $auto_hide = FALSE; - - // Autogenerated values - protected $url; - protected $current_page; - protected $total_pages; - protected $current_first_item; - protected $current_last_item; - protected $first_page; - protected $last_page; - protected $previous_page; - protected $next_page; - protected $sql_offset; - protected $sql_limit; - - /** - * Constructs and returns a new Pagination object. - * - * @param array configuration settings - * @return object - */ - public function factory($config = array()) - { - return new Pagination($config); - } - - /** - * Constructs a new Pagination object. - * - * @param array configuration settings - * @return void - */ - public function __construct($config = array()) - { - // No custom group name given - if ( ! isset($config['group'])) - { - $config['group'] = 'default'; - } - - // Pagination setup - $this->initialize($config); - } - - /** - * Sets config values. - * - * @throws Kohana_Exception - * @param array configuration settings - * @return void - */ - public function initialize($config = array()) - { - // Load config group - if (isset($config['group'])) - { - // Load and validate config group - if ( ! is_array($group_config = Kohana::config('pagination.'.$config['group']))) - throw new Kohana_Exception('pagination.undefined_group: ' . $config['group']); - - // All pagination config groups inherit default config group - if ($config['group'] !== 'default') - { - // Load and validate default config group - if ( ! is_array($default_config = Kohana::config('pagination.default'))) - throw new Kohana_Exception('pagination.undefined_group: default'); - - // Merge config group with default config group - $group_config += $default_config; - } - - // Merge custom config items with config group - $config += $group_config; - } - - // Assign config values to the object - foreach ($config as $key => $value) - { - if (property_exists($this, $key)) - { - $this->$key = $value; - } - } - - // Clean view directory - $this->directory = trim($this->directory, '/').'/'; - - // Build generic URL with page in query string - if ($this->query_string !== '') - { - // Extract current page - $this->current_page = isset($_GET[$this->query_string]) ? (int) $_GET[$this->query_string] : 1; - - // Insert {page} placeholder - $_GET[$this->query_string] = '{page}'; - - // Create full URL - $base_url = ($this->base_url === '') ? Router::$current_uri : $this->base_url; - $this->url = url::site($base_url).'?'.str_replace('%7Bpage%7D', '{page}', http_build_query($_GET)); - - // Reset page number - $_GET[$this->query_string] = $this->current_page; - } - - // Build generic URL with page as URI segment - else - { - // Use current URI if no base_url set - $this->url = ($this->base_url === '') ? Router::$segments : explode('/', trim($this->base_url, '/')); - - // Convert uri 'label' to corresponding integer if needed - if (is_string($this->uri_segment)) - { - if (($key = array_search($this->uri_segment, $this->url)) === FALSE) - { - // If uri 'label' is not found, auto add it to base_url - $this->url[] = $this->uri_segment; - $this->uri_segment = count($this->url) + 1; - } - else - { - $this->uri_segment = $key + 2; - } - } - - // Insert {page} placeholder - $this->url[$this->uri_segment - 1] = '{page}'; - - // Create full URL - $this->url = url::site(implode('/', $this->url)).Router::$query_string; - - // Extract current page - $this->current_page = URI::instance()->segment($this->uri_segment); - } - - // Core pagination values - $this->total_items = (int) max(0, $this->total_items); - $this->items_per_page = (int) max(1, $this->items_per_page); - $this->total_pages = (int) ceil($this->total_items / $this->items_per_page); - $this->current_page = (int) min(max(1, $this->current_page), max(1, $this->total_pages)); - $this->current_first_item = (int) min((($this->current_page - 1) * $this->items_per_page) + 1, $this->total_items); - $this->current_last_item = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items); - - // If there is no first/last/previous/next page, relative to the - // current page, value is set to FALSE. Valid page number otherwise. - $this->first_page = ($this->current_page === 1) ? FALSE : 1; - $this->last_page = ($this->current_page >= $this->total_pages) ? FALSE : $this->total_pages; - $this->previous_page = ($this->current_page > 1) ? $this->current_page - 1 : FALSE; - $this->next_page = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE; - - // SQL values - $this->sql_offset = (int) ($this->current_page - 1) * $this->items_per_page; - $this->sql_limit = sprintf(' LIMIT %d OFFSET %d ', $this->items_per_page, $this->sql_offset); - } - - /** - * Generates the HTML for the chosen pagination style. - * - * @param string pagination style - * @return string pagination html - */ - public function render($style = NULL) - { - // Hide single page pagination - if ($this->auto_hide === TRUE AND $this->total_pages <= 1) - return ''; - - if ($style === NULL) - { - // Use default style - $style = $this->style; - } - - // Return rendered pagination view - return View::factory($this->directory.$style, get_object_vars($this))->render(); - } - - /** - * Magically converts Pagination object to string. - * - * @return string pagination html - */ - public function __toString() - { - return $this->render(); - } - - /** - * Magically gets a pagination variable. - * - * @param string variable key - * @return mixed variable value if the key is found - * @return void if the key is not found - */ - public function __get($key) - { - if (isset($this->$key)) - return $this->$key; - } - - /** - * Adds a secondary interface for accessing properties, e.g. $pagination->total_pages(). - * Note that $pagination->total_pages is the recommended way to access properties. - * - * @param string function name - * @return string - */ - public function __call($func, $args = NULL) - { - return $this->__get($func); - } + // Config values + protected $base_url = ''; + protected $directory = 'pagination'; + protected $style = 'classic'; + protected $uri_segment = 3; + protected $query_string = ''; + protected $items_per_page = 20; + protected $total_items = 0; + protected $auto_hide = FALSE; + + // Autogenerated values + protected $url; + protected $current_page; + protected $total_pages; + protected $current_first_item; + protected $current_last_item; + protected $first_page; + protected $last_page; + protected $previous_page; + protected $next_page; + protected $sql_offset; + protected $sql_limit; + + /** + * Constructs and returns a new Pagination object. + * + * @param array configuration settings + * @return object + */ + public function factory($config = array()) + { + return new Pagination($config); + } + + /** + * Constructs a new Pagination object. + * + * @param array configuration settings + * @return void + */ + public function __construct($config = array()) + { + // No custom group name given + if ( ! isset($config['group'])) + { + $config['group'] = 'default'; + } + + // Pagination setup + $this->initialize($config); + } + + /** + * Sets config values. + * + * @throws Kohana_Exception + * @param array configuration settings + * @return void + */ + public function initialize($config = array()) + { + // Load config group + if (isset($config['group'])) + { + // Load and validate config group + if ( ! is_array($group_config = Kohana::config('pagination.'.$config['group']))) + throw new Kohana_Exception('pagination.undefined_group: ' . $config['group']); + + // All pagination config groups inherit default config group + if ($config['group'] !== 'default') + { + // Load and validate default config group + if ( ! is_array($default_config = Kohana::config('pagination.default'))) + throw new Kohana_Exception('pagination.undefined_group: default'); + + // Merge config group with default config group + $group_config += $default_config; + } + + // Merge custom config items with config group + $config += $group_config; + } + + // Assign config values to the object + foreach ($config as $key => $value) + { + if (property_exists($this, $key)) + { + $this->$key = $value; + } + } + + // Clean view directory + $this->directory = trim($this->directory, '/').'/'; + + // Build generic URL with page in query string + if ($this->query_string !== '') + { + // Extract current page + $this->current_page = isset($_GET[$this->query_string]) ? (int) $_GET[$this->query_string] : 1; + + // Insert {page} placeholder + $_GET[$this->query_string] = '{page}'; + + // Create full URL + $base_url = ($this->base_url === '') ? Router::$current_uri : $this->base_url; + $this->url = url::site($base_url).'?'.str_replace('%7Bpage%7D', '{page}', http_build_query($_GET)); + + // Reset page number + $_GET[$this->query_string] = $this->current_page; + } + + // Build generic URL with page as URI segment + else + { + // Use current URI if no base_url set + $this->url = ($this->base_url === '') ? Router::$segments : explode('/', trim($this->base_url, '/')); + + // Convert uri 'label' to corresponding integer if needed + if (is_string($this->uri_segment)) + { + if (($key = array_search($this->uri_segment, $this->url)) === FALSE) + { + // If uri 'label' is not found, auto add it to base_url + $this->url[] = $this->uri_segment; + $this->uri_segment = count($this->url) + 1; + } + else + { + $this->uri_segment = $key + 2; + } + } + + // Insert {page} placeholder + $this->url[$this->uri_segment - 1] = '{page}'; + + // Create full URL + $this->url = url::site(implode('/', $this->url)).Router::$query_string; + + // Extract current page + $this->current_page = URI::instance()->segment($this->uri_segment); + } + + // Core pagination values + $this->total_items = (int) max(0, $this->total_items); + $this->items_per_page = (int) max(1, $this->items_per_page); + $this->total_pages = (int) ceil($this->total_items / $this->items_per_page); + $this->current_page = (int) min(max(1, $this->current_page), max(1, $this->total_pages)); + $this->current_first_item = (int) min((($this->current_page - 1) * $this->items_per_page) + 1, $this->total_items); + $this->current_last_item = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items); + + // If there is no first/last/previous/next page, relative to the + // current page, value is set to FALSE. Valid page number otherwise. + $this->first_page = ($this->current_page === 1) ? FALSE : 1; + $this->last_page = ($this->current_page >= $this->total_pages) ? FALSE : $this->total_pages; + $this->previous_page = ($this->current_page > 1) ? $this->current_page - 1 : FALSE; + $this->next_page = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE; + + // SQL values + $this->sql_offset = (int) ($this->current_page - 1) * $this->items_per_page; + $this->sql_limit = sprintf(' LIMIT %d OFFSET %d ', $this->items_per_page, $this->sql_offset); + } + + /** + * Generates the HTML for the chosen pagination style. + * + * @param string pagination style + * @return string pagination html + */ + public function render($style = NULL) + { + // Hide single page pagination + if ($this->auto_hide === TRUE AND $this->total_pages <= 1) + return ''; + + if ($style === NULL) + { + // Use default style + $style = $this->style; + } + + // Return rendered pagination view + return View::factory($this->directory.$style, get_object_vars($this))->render(); + } + + /** + * Magically converts Pagination object to string. + * + * @return string pagination html + */ + public function __toString() + { + return $this->render(); + } + + /** + * Magically gets a pagination variable. + * + * @param string variable key + * @return mixed variable value if the key is found + * @return void if the key is not found + */ + public function __get($key) + { + if (isset($this->$key)) + return $this->$key; + } + + /** + * Adds a secondary interface for accessing properties, e.g. $pagination->total_pages(). + * Note that $pagination->total_pages is the recommended way to access properties. + * + * @param string function name + * @return string + */ + public function __call($func, $args = NULL) + { + return $this->__get($func); + } } // End Pagination Class \ No newline at end of file -- cgit v1.2.3