silverstripe-cms/code/ThumbnailStripField.php
Sean Harvey 0e727e458d MINOR Tidied up ThumbnailStripField->getimages() formatting
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@65446 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-11-07 03:05:43 +00:00

119 lines
3.3 KiB
PHP
Executable File

<?php
/**
* Provides a strip of thumbnails showing all of the images in the system.
* It will be tied to a 'parent field' that will provide it with a filter by which to reduce the number
* of thumbnails displayed.
* @package cms
* @subpackage assets
*/
class ThumbnailStripField extends FormField {
protected $parentField;
protected $updateMethod;
function __construct($name, $parentField, $updateMethod = "getimages") {
$this->parentField = $parentField;
$this->updateMethod = $updateMethod;
parent::__construct($name);
}
function ParentField() {
return $this->form->FormName() . '_' . $this->parentField;
}
function FieldHolder() {
Requirements::javascript(CMS_DIR . '/javascript/ThumbnailStripField.js');
return $this->renderWith('ThumbnailStripField');
}
function Images() {
//return DataObject::get("Image", "Paretn);
}
function UpdateMethod() {
return $this->updateMethod;
}
/**
* Populate the Thumbnail strip field, by looking for a folder,
* and the descendants of this folder.
*/
function getimages() {
$result = '';
$folder = DataObject::get_by_id('Folder', (int) $_GET['folderID']);
if(!$folder) return _t('ThumbnailStripField.NOTAFOLDER', 'This is not a folder');
$folderList = $folder->getDescendantIDList("Folder");
array_unshift($folderList, $folder->ID);
$images = DataObject::get('Image', 'ParentID IN (' . implode(', ', $folderList) . ')', 'Title');
if($images) {
$result .= '<ul>';
foreach($images as $image) {
$thumbnail = $image->getFormattedImage('StripThumbnail');
// Constrain the output image to a 600x600 square. This is passed to the destwidth/destheight in the class, which are then used to
// set width & height properties on the <img> tag inserted into the CMS. Resampling is done after save
$width = $image->Width;
$height = $image->Height;
if($width > 600) {
$height *= (600/$width);
$width = 600;
}
if($height > 600) {
$width *= (600/$height);
$height = 600;
}
$result .=
'<li>' .
'<a href=" ' . $image->Filename . '?r=' . rand(1,100000) . '">' .
'<img class="destwidth=' . round($width) . ',destheight=' . round($height) . '" src="'. $thumbnail->URL . '?r=' . rand(1,100000) . '" alt="' . $image->Title . '" title="' . $image->Title . '" />' .
'</a>' .
'</li>';
}
$result .= '</ul>';
} else {
$result = '<h2>' . _t('ThumbnailStripField.NOIMAGESFOUND', 'No images found in') . ' ' . $folder->Title . '</h2>';
}
return $result;
}
function getflash() {
$folder = DataObject::get_by_id("Folder", (int) $_GET['folderID']);
if(!$folder) return _t('ThumbnailStripField.NOTAFOLDER', 'This is not a folder');
$folderList = $folder->getDescendantIDList('Folder');
array_unshift($folderList, $folder->ID);
$width = Image::$strip_thumbnail_width - 10;
$height = Image::$strip_thumbnail_height - 10;
$flashObjects = DataObject::get("File", "ParentID IN (" . implode(', ', $folderList) . ") AND Filename LIKE '%.swf'");
$result = '';
if($flashObjects) {
$result .= '<ul>';
foreach($flashObjects as $flashObject) {
$result .= <<<HTML
<li>
<a href="$flashObject->URL">
<img src="cms/images/flash_small.jpg" alt="spacer" />
<br />
$flashObject->Name
</a>
</li>
HTML;
}
$result .= '</ul>';
}
return $result;
}
}
?>