Diogo Alves
Diogo Alves

Reputation: 378

Zend Paginator with Gdata Youtube

How Zend_Paginator can work according to the exchange of the variable query?

In line 8 performs a single fetch and does not change even by changing the query variable.

How to do paging function in accordance with the start-index from gdata feed?

The code: http://pastebin.com/rmxSP1Us

            $yt = new Zend_Gdata_YouTube();

        $limit = 12;
        $offset = ($page - 1) * $limit + 1;

        $query = "http://gdata.youtube.com/feeds/api/users/aculinario/favorites?start-index=$offset";

        $paginator = Zend_Paginator::factory($yt->getVideoFeed($query));
        $paginator->setCurrentPageNumber($page);
        $paginator->setItemCountPerPage($limit);
        $paginator->setPageRange(6);            
        $this->view->paginator = $paginator;

        echo $query // query changes but paginator no, every time Zend_Paginator factory should check the returned array of getVideoFeed, but not this checking

Sry, my poor english, i'm Trying

Upvotes: 0

Views: 907

Answers (1)

tuvac
tuvac

Reputation: 21

I got something similar working using a quick & dirty paginator adapter.

It's worth noting there's probably nicer, more generic ways to achieve this. But this will get you going if you're in a hurry.

<?php
class Lib_Paginator_Adapter_YoutubeUser implements Zend_Paginator_Adapter_Interface
{
    protected $_username;
    protected $_results;

    public function __construct($username)
    {
        $this->_username = $username;
    }

    public function getItems($offset, $itemCountPerPage)
    {
        $url = sprintf(
                '%s/%s/%s',
                Zend_Gdata_YouTube::USER_URI,
                $this->_username,
                Zend_Gdata_YouTube::UPLOADS_URI_SUFFIX
        );  
        try 
        {          
            $query = new Zend_Gdata_Query($url);
            $query->setMaxResults($itemCountPerPage)
                ->setStartIndex($offset);
            $youtube  = new Zend_Gdata_YouTube();
            $this->_results = $youtube->getUserUploads(null, $query);
            return $this->_results;
        }
        catch (Exception $ex)
        {
            echo $ex->getMessage();
            exit;
        }
    }

    public function count()
    {
        try 
        { 
            $youtube  = new Zend_Gdata_YouTube();
            return $youtube->getUserUploads($this->_username)->getTotalResults()->getText();
        }
        catch (Exception $ex)
        {
            echo $ex->getMessage();
            exit;
        }
    }

}

Then in your controller

    $page = $this->getRequest()->getParam("page");
    $limit = 10;
    $username = 'aculinario';
    $paginator = new Zend_Paginator(new Lib_Paginator_Adapter_YoutubeUser($username));
    $paginator->setItemCountPerPage($limit);
    $paginator->setPageRange(10);
    $paginator->setCurrentPageNumber($page);
    $this->view->youtubeFeed = $paginator;

Upvotes: 2

Related Questions