Skip to content

Instantly share code, notes, and snippets.

@jeremyboggs
Created September 30, 2012 14:15
Show Gist options
  • Save jeremyboggs/3806844 to your computer and use it in GitHub Desktop.
Save jeremyboggs/3806844 to your computer and use it in GitHub Desktop.
Get Omeka files with particular MIME types. Defaults are HTML, XML, PDF, RTF, Word, and plain text.
<?php
/**
* Helper to only retrieve files with particular MIME types. Defaults include:
*
* - HTML (text/html)
* - XML (application/xml)
* - PDF (application/pdf)
* - RTF (text/rtf)
* - MS Word (application/msword)
* - Plain text (text/plain)
*
* @param int The Item ID.
* @param array An array of MIME types to add to the defaults.
* @return Files
*/
function get_files_by_mime_type($itemId, $includeMimeTypes = array()) {
// List of default MIME types we do want to return.
$defaultMimeTypes = array(
'application/xml',
'application/msword',
'application/pdf',
'text/plain',
'text/rtf',
'text/html'
);
// Merge default MIME types with $includeMimeTypes
$mimeTypes = array_merge($defaultMimeTypes, $includeMimeTypes);
$db = get_db();
$table = $db->getTable('File');
$select = $table->getSelect();
// Select files that do not have derivative images and are associated with
// our Item.
$select->where('f.item_id = ?');
// Set our WHERE statement to be blank.
$where = '';
foreach ($mimeTypes as $type) {
// If where is still blank.
if ($where === '') {
$where .= $db->quoteInto("f.mime_browser LIKE ? ", "%$type%");
}
// If where is not blank, separate each with OR.
else {
$where .= $db->quoteInto("OR f.mime_browser LIKE ? ", "%$type%");
}
}
$select->where($where);
// Fetch the files with our select.
return $table->fetchObjects($select, array($itemId));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment