FIX: minor fixtures

This commit is contained in:
Tony Air 2021-02-26 11:14:20 +07:00
parent 878feb96a3
commit b6c20dbf8c
3 changed files with 65 additions and 35 deletions

View File

@ -27,16 +27,16 @@ class ElementImageWidget extends DataExtension
]; ];
private static $available_widths = [ private static $available_widths = [
'300' => 'Small (300px)', '300' => 'Small (300px)',
'400' => 'Medium (400px)', '400' => 'Medium (400px)',
'600' => 'Big (600px)', '600' => 'Big (600px)',
]; ];
private static $db = [ private static $db = [
'Resize' => 'Boolean(1)', 'Resize' => 'Boolean(1)',
'ManualWidth' => 'Boolean(0)', 'ManualWidth' => 'Boolean(0)',
'ImageHeight' => 'Float', 'ImageHeight' => 'Float',
'ImageWidth' => 'Float', 'ImageWidth' => 'Float',
'Content' => 'HTMLText', 'Content' => 'HTMLText',
]; ];
@ -48,44 +48,42 @@ class ElementImageWidget extends DataExtension
{ {
parent::updateCMSFields($fields); parent::updateCMSFields($fields);
$fields->insertBefore( $fields->removeByName(['ImageLinkID', 'Resize']);
'Image',
LinkField::create('ImageLinkID', 'Link') $fields->push(LinkField::create('ImageLinkID', 'Link'));
);
$this->owner->ImageHeight = $this->getHeight(); $this->owner->ImageHeight = $this->getHeight();
$heights = Config::inst()->get(__CLASS__, 'available_heights'); $heights = Config::inst()->get(__CLASS__, 'available_heights');
$widths = Config::inst()->get(__CLASS__, 'available_widths'); $widths = Config::inst()->get(__CLASS__, 'available_widths');
$fields->replaceField('Resize', CheckboxField::create( $fields->push(CheckboxField::create(
'Resize', 'Resize',
'Would you like to scale image?' 'Would you like to scale image?'
)); ));
if (count($heights)) { if (count($heights)) {
$fields->removeByName(['ManualWidth','ImageWidth',]); $fields->removeByName(['ManualWidth','ImageWidth', 'ImageHeight']);
$fields->replaceField( $fields->push(
'ImageHeight', CompositeField::create(
CompositeField::create( DropdownField::create(
DropdownField::create( 'ImageHeight',
'ImageHeight', 'Image Height',
'Image Height', $heights,
$heights, $this->getHeight()
$this->getHeight() )
) ->setEmptyString('(auto)')
->setEmptyString('(auto)') ->displayIf('Resize')->isChecked()->end(),
->displayIf('Resize')->isChecked()->end(), CheckboxField::create('ManualWidth', 'Set Width Manually')
CheckboxField::create('ManualWidth', 'Set Width Manually') ->displayIf('Resize')->isChecked()->end(),
->displayIf('Resize')->isChecked()->end(), DropdownField::create(
DropdownField::create( 'ImageWidth',
'ImageWidth', 'Image Width',
'Image Width', $widths
$widths )
) ->setEmptyString('(auto)')
->setEmptyString('(auto)') ->displayIf('ManualWidth')->isChecked()->end()
->displayIf('ManualWidth')->isChecked()->end() )
)
); );
} else { } else {
$fields->dataFieldByName('ImageHeight') $fields->dataFieldByName('ImageHeight')
@ -117,10 +115,10 @@ class ElementImageWidget extends DataExtension
public function getWidth() public function getWidth()
{ {
$obj = $this->owner; $obj = $this->owner;
return $obj->getField('ManualWidth') && $obj->getField('ImageWidth') return $obj->getField('ManualWidth') && $obj->getField('ImageWidth')
? $obj->getField('ImageWidth') ? $obj->getField('ImageWidth')
: $obj->getColumnWidthRecursive(); : $obj->getColumnWidthRecursive();
} }
public function getHeight() public function getHeight()
@ -140,4 +138,15 @@ class ElementImageWidget extends DataExtension
return 0; return 0;
} }
public function onBeforeWrite()
{
$title = $this->owner->getField('Title');
$img = $this->owner->Image();
if (!$title && $img) {
$this->owner->setField('Title', $img->getTitle());
}
parent::onBeforeWrite();
}
} }

View File

@ -44,6 +44,27 @@ class BannerWidget extends Widget
public function getSibling() public function getSibling()
{ {
return false; return false;
}
private $_random;
public function Random()
{
if (!$this->_random) {
$this->_random = self::get()->filter('Enabled', true)->sort('RAND()')->first();
}
return $this->_random;
}
public function onBeforeWrite()
{
$title = $this->getField('Title');
$img = $this->Image();
if (!$title && $img) {
$this->setField('Title', $img->getTitle());
}
parent::onBeforeWrite();
} }
} }

View File

@ -2,7 +2,7 @@
<div class="image-element__image<% if $Resize %><% if $Height %> height{$Height}<% end_if %><% if $Width %> width{$Width}<% end_if %><% end_if %>"> <div class="image-element__image<% if $Resize %><% if $Height %> height{$Height}<% end_if %><% if $Width %> width{$Width}<% end_if %><% end_if %>">
<img <img
src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
data-lazy-src="$ImageResized.URL" class="img-responsive" alt="$Title.ATT" data-lazy-src="$ImageResized.URL" class="img-responsive" alt="$Title"
/> />
</div> </div>
<% end_if %> <% end_if %>