How to mimic Items/Collections browsing admin pages

Hello.

For a plugin I’m working on (see Advice required for a new Hide Files plugins), I’d like to add a browsable list of all hidden files.

As theoretically there could be a lot of hidden files, I’d like for the list to have pagination; besides, I’d like to make columns sortable, exactly like items and collections browse.

What I’ve done so far:

  • created the controller that’s retrieving the files list with the following functions
    public function listAction()
    {
        $this->view->files = $this->_getFiles();
    }
    
	protected function _getFiles()
    {
		$table = get_db()->getTable('File');
		$select = $table->getSelect()
				->where('files.public = 0')
				->order('files.id DESC');
		return $table->fetchObjects($select);
    }

How would I make the sorting work, now? When clicking on the arrows, the page gets reloaded with the correct url (sort_field=filename, f.i.), but what’s the best way to take that into account when retrieving the files list?

Second, and probably most important: how could I implement the pagination?

Thanks.

Pagination and filtering/sorting for controllers generally run through the same method: findBy on the table. If you look at the Omeka_Controller_AbstractActionController’s browseAction you’ll see that it gets the passed parameters to the page and passes them to findBy.

Thanks for the advice, John.

By using findBy I’ve managed to get both pagination and sorting working, but I’ve got a problem with an extra parameter I’m passing: when installing the plugin, I add a “public” column to the “files” table, and set it to true (1) by default. When I pass an additional parameter to the findBy function ($params['public'] = 0), I would expect the findBy to consider that too, but it doesn’t; it seems that the “public” column, although physically present in the table, is not seen as one of the available fields (I can see it in the db table, but not if I print a list of all columns).

Am I missing some step, here? Or is there another way to approach this issue?

Thanks.

The filters that are respected are defined by the applySearchFilters for the table model for the record you’re searching. Files don’t normally have “privateness” on their own, so there’s no filter defined for “public” for files.

The browse sql hook lets you add filters that are respected when you call findBy, so you can use that hook to make findBy for files respect the “public” key and do the proper filtering.

Thanks, John. That did the trick.
Here’s the code, in case somebody else could profit of it:

public function hookFilesBrowseSql($args) 
{
	// Filters out all public files and sort results by added date
	$select = $args['select'];
	$select->where("files.public = ?", 0);
	$select->order("files.added DESC");
}
1 Like

This topic was automatically closed 250 days after the last reply. New replies are no longer allowed.