Organizing Files

Improvements?

Let us know by posting here.

Grouping Files with File Sets

File Sets in Concrete CMS allow for efficient file organization, enabling a file to belong to multiple sets. The file manager interface facilitates file set filtering, and they are easily manageable via code. To add a file to a file set:

$file = \Concrete\Core\File\File::getByID(1);
$set = \Concrete\Core\File\Set\Set::getByName('My File Set');
$set->addFileToSet($file);

To remove a file from a set:

$set->removeFileFromSet($file);

Creating a File Set Programmatically

For automatic file set creation, like in custom packages, you can use:

$set = \Concrete\Core\File\Set\Set::createAndGetSet(
    'My File Set', 
    \Concrete\Core\File\Set\Set::TYPE_PUBLIC
);

FileList Object in Concrete CMS

The Concrete\Core\File\FileList object in Concrete CMS helps developers query files using various criteria, considering permissions and underlying complex table structures.

$list = new \Concrete\Core\File\FileList();
$files = $list->getResults(); // Array of \Concrete\Core\File\File objects

Basic Filtering Examples

Filter by File Type using constants from \Concrete\Core\File\Type\Type:

  • \Concrete\Core\File\Type\Type::T_IMAGE = 1 (and other type constants)
$list->filterByType(\Concrete\Core\File\Type\Type::T_IMAGE);

Filter by Extension:

$list->filterByExtension('png');

Filter By Keywords:

$list->filterByKeywords('foobar');

Filter By File Set:

$set = \Concrete\Core\File\Set\Set::getByName('My File Set');
$list->filterBySet($set);

Include Files in No Sets:

$list->filterByNoSet();

Filter By File Size (in KB):

$list->filterBySize(1024, 2048); // 1MB to 2MB

Filter by Attribute

$list->filterByAttribute('width', 200, '>='); // Width 200 or greater

Sorting

Sort by filename ascending:

$list->sortByFilenameAscending();

Sort by File Set Display Order:

$list->sortByFileSetDisplayOrder();

Permissions

Ignore permissions:

$list->ignorePermissions();

Custom Queries

Utilize the Doctrine DBAL QueryBuilder through the FileList object:

$query = $list->getQueryObject();

Pagination

Get paginated results:

$pagination = $list->getPagination();
// Use methods like getTotalResults(), getTotalPages(), hasNexPage(), hasPreviousPage()

Rendering Pagination

Render pagination HTML (Bootstrap 3, Basic Pagination, etc.):

print $pagination->renderDefaultView();