Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use SilverStripe\View\TemplateIteratorProvider;
- class SSViewer_SiteIteratorSupport implements TemplateIteratorProvider
- {
- /**
- * @var int
- */
- protected $iteratorPos;
- /**
- * @var int
- */
- protected $iteratorTotalItems;
- public static function get_template_iterator_variables()
- {
- return array(
- 'First',
- 'Last',
- 'FirstLast',
- 'Pos',
- 'ListClass'
- );
- }
- /**
- * @param int $pos position in iterator
- * @param int $totalItems total number of items
- */
- public function iteratorProperties($pos, $totalItems)
- {
- $this->iteratorPos = $pos;
- $this->iteratorTotalItems = $totalItems;
- }
- /**
- * @see SilverStripe\View\SSViewer_BasicIteratorSupport::First
- * @return boolean
- */
- public function First()
- {
- return $this->iteratorPos == 0;
- }
- /**
- * @see SilverStripe\View\SSViewer_BasicIteratorSupport::Last
- * @return boolean
- */
- public function Last()
- {
- return $this->iteratorPos == $this->iteratorTotalItems - 1;
- }
- /**
- * @see SilverStripe\View\SSViewer_BasicIteratorSupport::FirstLast
- * @return string|null
- */
- public function FirstLast()
- {
- if ($this->First() && $this->Last()) {
- return 'first last';
- }
- if ($this->First()) {
- return 'first';
- }
- if ($this->Last()) {
- return 'last';
- }
- return null;
- }
- /**
- * @see SilverStripe\View\SSViewer_BasicIteratorSupport::Pos
- * @param int $startIndex
- * @return int
- */
- public function Pos($startIndex = 1)
- {
- return $this->iteratorPos + $startIndex;
- }
- /**
- * @param int $length
- * @return string
- */
- public function ListClass($length = 2)
- {
- $classes = '';
- if ($firstLast = $this->FirstLast()) {
- $classes .= $firstLast;
- }
- $pos = $this->Pos(0);
- if ($length - 1 === ($pos % $length)) {
- $classes .= ' end';
- }
- if (0 === $pos % $length) {
- $classes .= ' start';
- }
- $classes .= $pos % 2 ? ' even' : ' odd';
- return trim($classes, ' ');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement