[limb-svn] r6523 - in 3.x/trunk/limb/macro: src src/tags/pager tests/cases/tags
svn at limb-project.com
svn at limb-project.com
Sat Nov 17 14:16:42 MSK 2007
Author: serega
Date: 2007-11-17 14:16:42 +0300 (Sat, 17 Nov 2007)
New Revision: 6523
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6523
Added:
3.x/trunk/limb/macro/src/tags/pager/paginate.tag.php
3.x/trunk/limb/macro/tests/cases/tags/lmbMacroPaginateTagTest.class.php
Modified:
3.x/trunk/limb/macro/src/lmbMacroNode.class.php
Log:
-- {{paginate}} tag added. It allows to paginate any iterator that supports lmbCollectionInterface. {{paginate}} tag allows also to link iterator with pager. In other words it allows to pass total items in the paginated iterator to pager and gets pagination params from pager
Modified: 3.x/trunk/limb/macro/src/lmbMacroNode.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/lmbMacroNode.class.php 2007-11-17 11:14:37 UTC (rev 6522)
+++ 3.x/trunk/limb/macro/src/lmbMacroNode.class.php 2007-11-17 11:16:42 UTC (rev 6523)
@@ -138,7 +138,25 @@
}
}
}
+
+ /**
+ * Sometimes it is useful to find node located in another tree branch, eg:
+ * <code>
+ * {{block}}{{some_tag id='tag1'}}{{/block}}
+ * {{block}}{{some_tag id='tag2'}}{{/block}}
+ * </code>
+ * in this case we can find tag1 tag from tag2 tag using findUpChild.
+ */
+ function findUpChild($id)
+ {
+ if($child = $this->findChild($id))
+ return $child;
+ if($this->parent)
+ return $this->parent->findUpChild($id);
+ }
+
+
function findChildByClass($class)
{
foreach($this->children as $child)
Added: 3.x/trunk/limb/macro/src/tags/pager/paginate.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/pager/paginate.tag.php (rev 0)
+++ 3.x/trunk/limb/macro/src/tags/pager/paginate.tag.php 2007-11-17 11:16:42 UTC (rev 6523)
@@ -0,0 +1,56 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * Applies pager to iterator (so called "pagination")
+ * @tag paginate
+ * @package macro
+ * @version $Id$
+ */
+class lmbMacroPaginateTag extends lmbMacroTag
+{
+ function generate($code)
+ {
+ $iterator = $this->get('iterator');
+
+ if($this->has('pager'))
+ {
+ if(!$pager_tag = $this->parent->findUpChild($this->get('pager')))
+ $this->raise('Can\'t find pager by "pager" attribute in {{paginate}} tag');
+
+ $pager = $pager_tag->getPagerVar();
+
+ if($this->has('limit'))
+ $code->writePhp("{$pager}->setItemsPerPage({$this->get('limit')});\n");
+
+ $code->writePhp("{$pager}->setTotalItems({$iterator}->count());\n");
+ $code->writePhp("{$pager}->prepare();\n");
+ $offset = $code->generateVar();
+ $code->writePhp("{$offset} = {$pager}->getCurrentPageBeginItem();\n");
+ $code->writePhp("if({$offset} > 0) {$offset} = {$offset} - 1;\n");
+ $code->writePhp("{$iterator}->paginate({$offset}, {$pager}->getItemsPerPage());\n");
+ return;
+ }
+ elseif($this->has('offset'))
+ {
+ if(!$this->has('limit'))
+ $this->raise('"limit" attribute for {{paginate}} is required if "offset" is given');
+
+ $code->writePhp("{$iterator}->paginate({$this->get('offset')},{$this->get('limit')});\n");
+ return;
+ }
+ elseif($this->has('limit'))
+ {
+ $code->writePhp("{$iterator}->paginate(0,{$this->get('limit')});\n");
+ return;
+ }
+ }
+}
+
+
Added: 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroPaginateTagTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroPaginateTagTest.class.php (rev 0)
+++ 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroPaginateTagTest.class.php 2007-11-17 11:16:42 UTC (rev 6523)
@@ -0,0 +1,107 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+lmb_require('limb/core/src/lmbArrayIterator.class.php');
+
+class lmbMacroPaginateTagTest extends lmbBaseMacroTest
+{
+ protected $old_get;
+ protected $old_server;
+
+ function setUp()
+ {
+ parent :: setUp();
+
+ $this->old_get = $_GET;
+ $this->old_server = $_SERVER;
+
+ $_SERVER['REQUEST_URI'] = 'test.com';
+ $_GET = array();
+ }
+
+ function tearDown()
+ {
+ $_GET = $this->old_get;
+ $_SERVER = $this->old_server;
+
+ parent :: tearDown();
+ }
+
+ function testPaginateWithoutPager()
+ {
+ $template = '{{paginate iterator="$#test_iterator" offset="2" limit="2"/}}'.
+ '{{list using="$#test_iterator" as="$item"}}{{list:item}}{$item}{{/list:item}}{{/list}}';
+
+ $page = $this->_createMacroTemplate($template, 'tpl.html');
+ $page->set('test_iterator', new lmbArrayIterator(array('Ivan', 'Pavel', 'Mike', 'Bob', 'Todd')));
+
+ $expected = 'MikeBob';
+ $this->assertEqual($page->render(), $expected);
+ }
+
+ function testErrorIfOffsetGivenWithoutLimit()
+ {
+ $template = '{{paginate iterator="$#test_iterator" offset="2"/}}'.
+ '{{list using="$#test_iterator" as="$item"}}{{list:item}}{$item}{{/list:item}}{{/list}}';
+
+ try
+ {
+ $page = $this->_createMacroTemplate($template, 'tpl.html');
+ $page->render();
+ $this->assertTrue(false);
+ }
+ catch(lmbMacroException $e)
+ {
+ $this->assertTrue(true);
+ }
+ }
+
+ function testPaginateWithoutPagerAndOffsetIsZeroIfNotSpecified()
+ {
+ $template = '{{paginate iterator="$#test_iterator" limit="2"/}}'.
+ '{{list using="$#test_iterator" as="$item"}}{{list:item}}{$item}{{/list:item}}{{/list}}';
+
+ $page = $this->_createMacroTemplate($template, 'tpl.html');
+ $page->set('test_iterator', new lmbArrayIterator(array('Ivan', 'Pavel', 'Mike', 'Bob', 'Todd')));
+
+ $expected = 'IvanPavel';
+ $this->assertEqual($page->render(), $expected);
+ }
+
+ function testPaginateWithPager()
+ {
+ $template = '{{paginate iterator="$#test_iterator" pager="test_pager"/}}'.
+ '{{list using="$#test_iterator" as="$item"}}{{list:item}}{$item}{{/list:item}}{{/list}}'.
+ '{{pager id="test_pager" items="2"/}}';
+
+ $_GET['test_pager'] = 2; // offset = 2 since the second page
+
+ $page = $this->_createMacroTemplate($template, 'tpl.html');
+ $page->set('test_iterator', new lmbArrayIterator(array('Ivan', 'Pavel', 'Mike', 'Bob', 'Todd')));
+
+ $expected = 'MikeBob';
+ $this->assertEqual($page->render(), $expected);
+ }
+
+ function testPaginateWithPagerOverwritesPagerItemsPerPageByLimitAttribute()
+ {
+ $template = '{{paginate iterator="$#test_iterator" pager="test_pager" limit="3"/}}'.
+ '{{list using="$#test_iterator" as="$item"}}{{list:item}}{$item}{{/list:item}}{{/list}}'.
+ '{{pager id="test_pager" items="2"/}}';
+
+ $_GET['test_pager'] = 2; // offset = 2 since the second page
+
+ $page = $this->_createMacroTemplate($template, 'tpl.html');
+ $page->set('test_iterator', new lmbArrayIterator(array('Ivan', 'Pavel', 'Mike', 'Bob', 'Todd', 'Serega')));
+
+ $expected = 'BobToddSerega';
+ $this->assertEqual($page->render(), $expected);
+ }
+}
+
More information about the limb-svn
mailing list