class DBPostRepository implements PostRepositoryInterface
{
    protected $sortableFields = array(
        'title', 
        'final',
        'publication_id', 
        'category_id', 
        'user_id', 
        'updated_at');

    protected $currentSort = 'updated_at';

    protected $currentSortOrder = 'desc';

    protected $perPage = 25;


    public function __construct(Post $posts)
    {
        $this->posts = $posts;
    }

    public function setSorting($field, $direction = 'desc')
    {
        if(in_array($field, $this->sortableFields) {
            $this->currentSort = $field;
        }
        $this->currentSortOrder = ($direction == 'asc') ? 'asc' : 'desc';

        return $this;
    }

    protected function newQuery()
    {
        return $this->posts->orderBy($this->currentSort, $this->currentSortOrder);
    }

    public function findByFields($publicationId = null, $publishDate = null)
    {
        $query = $this->newQuery();

        if($publicationId) {
            $query->wherePublicationId($publicationId);
        }
        if($publishDate) {
            $query->wherePublishDate($publishDate)
        }
        return $this->paginate($this->perPage);
    }
    
}


public function index()
{
    $posts = $this->posts
        ->orderBy(Input::get('sort'), Input::get('order'))
        ->findByFields(Input::get('publication_id'), Input::get('publish_date'));

    return View::make('posts.index', array(
        'category'        => null,
        'subcategory'     => null,
        'select'          => Input::get('publish_date', null),
        'posts'           => $posts,
        'pagination'      => $posts->appends(Input::only('publication_id', 'sort', 'order'))
    ));
}