2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2010-12-20 03:19:17 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Assets;
|
|
|
|
|
|
|
|
use SilverStripe\Core\Convert;
|
2016-09-07 05:35:47 +02:00
|
|
|
use SilverStripe\Forms\HTMLReadonlyField;
|
|
|
|
use SilverStripe\Forms\LiteralField;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\Parsers\ShortcodeParser;
|
|
|
|
use SilverStripe\View\Parsers\ShortcodeHandler;
|
2016-09-07 05:35:47 +02:00
|
|
|
use SilverStripe\Forms\Tab;
|
|
|
|
use SilverStripe\Forms\HeaderField;
|
|
|
|
use SilverStripe\Forms\TabSet;
|
|
|
|
use SilverStripe\Forms\TextField;
|
|
|
|
use SilverStripe\Forms\DatetimeField;
|
|
|
|
use SilverStripe\Forms\ReadonlyField;
|
|
|
|
use SilverStripe\Forms\HiddenField;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2010-12-20 03:19:17 +01:00
|
|
|
* Represents an Image
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-03-08 12:20:51 +01:00
|
|
|
class Image extends File implements ShortcodeHandler {
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $table_name = 'Image';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $singular_name = "Image";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $plural_name = "Images";
|
|
|
|
|
2016-03-02 06:18:10 +01:00
|
|
|
public function __construct($record = null, $isSingleton = false, $model = null, $queryParams = array()) {
|
|
|
|
parent::__construct($record, $isSingleton, $model, $queryParams);
|
2015-09-15 04:52:02 +02:00
|
|
|
$this->File->setAllowedCategories('image/supported');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-02-23 22:43:37 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getCMSFields() {
|
2016-09-07 05:35:47 +02:00
|
|
|
$path = '/' . dirname($this->getFilename());
|
|
|
|
|
2016-09-19 03:02:26 +02:00
|
|
|
$previewLink = Convert::raw2att($this->PreviewLink());
|
2016-09-07 05:35:47 +02:00
|
|
|
$image = "<img src=\"{$previewLink}\" class=\"editor__thumbnail\" />";
|
|
|
|
|
|
|
|
$link = $this->Link();
|
2016-10-04 06:09:11 +02:00
|
|
|
|
|
|
|
$statusTitle = $this->getStatusTitle();
|
|
|
|
$statusFlag = "<span class=\"editor__status-flag\">{$statusTitle}</span>";
|
2016-09-07 05:35:47 +02:00
|
|
|
|
|
|
|
$content = Tab::create('Main',
|
|
|
|
HeaderField::create('TitleHeader', $this->Title, 1)
|
|
|
|
->addExtraClass('editor__heading'),
|
|
|
|
LiteralField::create("ImageFull", $image)
|
|
|
|
->addExtraClass('editor__file-preview'),
|
|
|
|
TabSet::create('Editor',
|
|
|
|
Tab::create('Details',
|
|
|
|
TextField::create("Title", $this->fieldLabel('Title')),
|
|
|
|
TextField::create("Name", $this->fieldLabel('Filename')),
|
|
|
|
ReadonlyField::create(
|
|
|
|
"Path",
|
|
|
|
_t('AssetTableField.PATH', 'Path'),
|
|
|
|
(($path !== '/.') ? $path : '') . '/'
|
|
|
|
),
|
|
|
|
HTMLReadonlyField::create(
|
|
|
|
'ClickableURL',
|
|
|
|
_t('AssetTableField.URL','URL'),
|
|
|
|
sprintf('<i class="%s"></i><a href="%s" target="_blank">%s</a>',
|
2016-09-14 05:09:45 +02:00
|
|
|
'font-icon-link btn--icon-large form-control-static__icon', $link, $link)
|
2016-09-07 05:35:47 +02:00
|
|
|
)
|
|
|
|
),
|
|
|
|
Tab::create('Usage',
|
2016-09-20 02:56:45 +02:00
|
|
|
DatetimeField::create(
|
|
|
|
"Created",
|
|
|
|
_t('AssetTableField.CREATED', 'First uploaded')
|
|
|
|
)->setReadonly(true),
|
2016-09-07 05:35:47 +02:00
|
|
|
DatetimeField::create(
|
|
|
|
"LastEdited",
|
|
|
|
_t('AssetTableField.LASTEDIT', 'Last changed')
|
|
|
|
)->setReadonly(true)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
HiddenField::create('ID', $this->ID)
|
2013-04-15 12:17:00 +02:00
|
|
|
);
|
2016-09-07 05:35:47 +02:00
|
|
|
|
|
|
|
if ($dimensions = $this->getDimensions()) {
|
|
|
|
$content->insertAfter(
|
|
|
|
'TitleHeader',
|
|
|
|
LiteralField::create(
|
|
|
|
"DisplaySize",
|
2016-10-04 06:09:11 +02:00
|
|
|
sprintf('<div class="editor__specs">%spx, %s %s</div>',
|
|
|
|
$dimensions, $this->getSize(), $statusFlag)
|
2016-09-07 05:35:47 +02:00
|
|
|
)
|
|
|
|
);
|
2016-10-04 06:09:11 +02:00
|
|
|
} else {
|
|
|
|
$content->insertAfter(
|
|
|
|
'TitleHeader',
|
|
|
|
LiteralField::create('StatusFlag', $statusFlag)
|
|
|
|
);
|
2016-09-07 05:35:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$fields = FieldList::create(TabSet::create('Root', $content));
|
|
|
|
|
|
|
|
$this->extend('updateCMSFields', $fields);
|
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
return $fields;
|
2013-04-15 12:17:00 +02:00
|
|
|
}
|
2013-09-27 19:06:56 +02:00
|
|
|
|
2015-09-15 04:52:02 +02:00
|
|
|
public function getIsImage() {
|
|
|
|
return true;
|
2015-09-09 06:18:39 +02:00
|
|
|
}
|
2015-10-15 00:09:04 +02:00
|
|
|
|
|
|
|
/**
|
2016-03-08 12:20:51 +01:00
|
|
|
* Replace"[image id=n]" shortcode with an image reference.
|
|
|
|
* Permission checks will be enforced by the file routing itself.
|
2015-10-15 00:09:04 +02:00
|
|
|
*
|
2016-03-08 12:20:51 +01:00
|
|
|
* @param array $args Arguments passed to the parser
|
|
|
|
* @param string $content Raw shortcode
|
|
|
|
* @param ShortcodeParser $parser Parser
|
|
|
|
* @param string $shortcode Name of shortcode used to register this handler
|
|
|
|
* @param array $extra Extra arguments
|
|
|
|
* @return string Result of the handled shortcode
|
2015-10-15 00:09:04 +02:00
|
|
|
*/
|
2016-03-08 12:20:51 +01:00
|
|
|
public static function handle_shortcode($args, $content, $parser, $shortcode, $extra = array()) {
|
|
|
|
// Find appropriate record, with fallback for error handlers
|
|
|
|
$record = static::find_shortcode_record($args, $errorCode);
|
|
|
|
if($errorCode) {
|
|
|
|
$record = static::find_error_record($errorCode);
|
|
|
|
}
|
|
|
|
if (!$record) {
|
|
|
|
return null; // There were no suitable matches at all.
|
|
|
|
}
|
2015-10-15 00:09:04 +02:00
|
|
|
|
2016-03-08 12:20:51 +01:00
|
|
|
// Check if a resize is required
|
|
|
|
$src = $record->Link();
|
|
|
|
if($record instanceof Image) {
|
|
|
|
$width = isset($args['width']) ? $args['width'] : null;
|
|
|
|
$height = isset($args['height']) ? $args['height'] : null;
|
|
|
|
$hasCustomDimensions = ($width && $height);
|
|
|
|
if ($hasCustomDimensions && (($width != $record->getWidth()) || ($height != $record->getHeight()))) {
|
|
|
|
$resized = $record->ResizedImage($width, $height);
|
|
|
|
// Make sure that the resized image actually returns an image
|
|
|
|
if($resized) {
|
|
|
|
$src = $resized->getURL();
|
|
|
|
}
|
2015-10-15 00:09:04 +02:00
|
|
|
}
|
2016-03-08 12:20:51 +01:00
|
|
|
}
|
2015-10-15 00:09:04 +02:00
|
|
|
|
2016-03-08 12:20:51 +01:00
|
|
|
// Build the HTML tag
|
|
|
|
$attrs = array_merge(
|
|
|
|
// Set overrideable defaults
|
|
|
|
['src' => '', 'alt' => $record->Title],
|
|
|
|
// Use all other shortcode arguments
|
|
|
|
$args,
|
|
|
|
// But enforce some values
|
|
|
|
['id' => '', 'src' => $src]
|
|
|
|
);
|
2015-10-15 00:09:04 +02:00
|
|
|
|
2016-03-08 12:20:51 +01:00
|
|
|
// Clean out any empty attributes
|
|
|
|
$attrs = array_filter($attrs, function($v) {return (bool)$v;});
|
2015-10-15 00:09:04 +02:00
|
|
|
|
2016-03-08 12:20:51 +01:00
|
|
|
// Condense to HTML attribute string
|
|
|
|
$attrsStr = join(' ', array_map(function($name) use ($attrs) {
|
|
|
|
return Convert::raw2att($name) . '="' . Convert::raw2att($attrs[$name]) . '"';
|
|
|
|
}, array_keys($attrs)));
|
2015-10-15 00:09:04 +02:00
|
|
|
|
2016-03-08 12:20:51 +01:00
|
|
|
return '<img ' . $attrsStr . ' />';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Regenerates "[image id=n]" shortcode with new src attribute prior to being edited within the CMS.
|
|
|
|
*
|
|
|
|
* @param array $args Arguments passed to the parser
|
|
|
|
* @param string $content Raw shortcode
|
|
|
|
* @param ShortcodeParser $parser Parser
|
|
|
|
* @param string $shortcode Name of shortcode used to register this handler
|
|
|
|
* @param array $extra Extra arguments
|
|
|
|
* @return string Result of the handled shortcode
|
|
|
|
*/
|
|
|
|
public static function regenerate_shortcode($args, $content, $parser, $shortcode, $extra = array()) {
|
|
|
|
// Check if there is a suitable record
|
|
|
|
$record = static::find_shortcode_record($args);
|
|
|
|
if($record) {
|
|
|
|
$args['src'] = $record->getURL();
|
2015-10-15 00:09:04 +02:00
|
|
|
}
|
2016-03-08 12:20:51 +01:00
|
|
|
|
|
|
|
// Rebuild shortcode
|
|
|
|
$parts = array();
|
|
|
|
foreach($args as $name => $value) {
|
|
|
|
$htmlValue = Convert::raw2att($value ?: $name);
|
|
|
|
$parts[] = sprintf('%s="%s"', $name, $htmlValue);
|
|
|
|
}
|
|
|
|
return sprintf("[%s %s]", $shortcode, implode(' ', $parts));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to regenerate all shortcode links.
|
|
|
|
*
|
|
|
|
* @param string $value HTML value
|
|
|
|
* @return string value with links resampled
|
|
|
|
*/
|
|
|
|
public static function regenerate_html_links($value) {
|
|
|
|
// Create a shortcode generator which only regenerates links
|
|
|
|
$regenerator = ShortcodeParser::get('regenerator');
|
|
|
|
return $regenerator->parse($value);
|
2015-10-15 00:09:04 +02:00
|
|
|
}
|
2016-04-28 01:41:40 +02:00
|
|
|
|
|
|
|
public function PreviewLink($action = null) {
|
|
|
|
// Since AbsoluteLink can whitelist protected assets,
|
|
|
|
// do permission check first
|
|
|
|
if(!$this->canView()) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-19 03:02:26 +02:00
|
|
|
|
|
|
|
// Size to width / height
|
|
|
|
$width = (int)$this->config()->get('asset_preview_width');
|
|
|
|
$height = (int)$this->config()->get('asset_preview_height');
|
|
|
|
$resized = $this->FitMax($width, $height);
|
|
|
|
if ($resized && $resized->exists()) {
|
|
|
|
$link = $resized->getAbsoluteURL();
|
|
|
|
} else {
|
|
|
|
$link = $this->getIcon();
|
|
|
|
}
|
2016-04-28 01:41:40 +02:00
|
|
|
$this->extend('updatePreviewLink', $link, $action);
|
|
|
|
return $link;
|
|
|
|
}
|
2011-08-26 17:57:05 +02:00
|
|
|
}
|