Source of file Image.php
Size: 11,607 Bytes - Last Modified: 2021-12-23T10:21:36+00:00
/var/www/docs.ssmods.com/process/src/src/Model/Product/Image.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 | <?php namespace SilverCart\Model\Product; use SilverCart\Admin\Controllers\ProductAdmin; use SilverCart\Admin\Controllers\PaymentMethodAdmin; use SilverCart\Dev\Tools; use SilverCart\Model\Payment\PaymentMethod; use SilverCart\Model\Product\ImageTranslation; use SilverCart\Model\Product\Product; use SilverCart\Model\Widgets\ImageSliderImage; use SilverCart\Model\Widgets\SlidorionProductGroupWidget; use SilverCart\ORM\DataObjectExtension; use SilverStripe\Assets\Image as SilverStripeImage; use SilverStripe\Control\Controller; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\TextField; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\Filters\PartialMatchFilter; use SilverStripe\ORM\HasManyList; /** * DataObject to handle images added to a product or sth. else. * Provides additional (meta-)information about the image. * * @package SilverCart * @subpackage Model_Product * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 29.09.2017 * @copyright 2017 pixeltricks GmbH * @license see license file in modules root directory * * @property string $ProductNumberToReference Product number of the product to reference to * @property int $SortOrder Sort order * * @property string $Title Title (current locale context) * @property string $Content Content (current locale context) * @property string $Description Description (current locale context) * * @method Product Product() Returns the related Product. * @method PaymentMethod PaymentMethod() Returns the related Payment Method. * * @method HasManyList ImageTranslations() Returns a list of translations for this image. */ class Image extends DataObject { use \SilverCart\ORM\ExtensibleDataObject; /** * DB properties * * @var array */ private static $db = [ 'ProductNumberToReference' => 'Varchar(128)', 'SortOrder' => 'Int', ]; /** * Has one relations * * @var array */ private static $has_one = [ 'Product' => Product::class, 'PaymentMethod' => PaymentMethod::class, 'Image' => SilverStripeImage::class, ]; /** * 1:n relationships. * * @var array */ private static $has_many = [ 'ImageTranslations' => ImageTranslation::class ]; /** * Belongs many many relations. * * @var array */ private static $belongs_many_many = [ 'SlidorionProductGroupWidgets' => SlidorionProductGroupWidget::class, ]; /** * Default sort field and direction. * * @var string */ private static $default_sort = 'SortOrder ASC'; /** * Casted properties * * @var array */ private static $casting = [ 'Title' => 'Varchar', 'Content' => 'HTMLText', 'Description' => 'HTMLText', ]; /** * DB table name * * @var string */ private static $table_name = 'SilvercartImage'; /** * Link * * @var string */ protected $link = null; /** * Marker to check whether the CMS fields are called or not * * @var bool */ protected $getCMSFieldsIsCalled = false; /** * Constructor. Overwrites some basic attributes. * * @param array $record Record to fill Object with * @param bool $isSingleton Is this a singleton? * * @return void * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 05.06.2012 */ public function __construct($record = null, $isSingleton = false) { parent::__construct($record, $isSingleton); if ($this->Image()->exists()) { $this->Image()->Title = $this->Title; } } /** * getter for the Title, looks for set translation * * @return string The Title from the translation object or an empty string */ public function getTitle() { $title = $this->getTranslationFieldValue('Title'); if ($this->Product()->ID && empty($title)) { $title = $this->Product()->Title; } return $title; } /** * getter for the content, looks for set translation * * @return string The content from the translation object or an empty string */ public function getContent() { return $this->getTranslationFieldValue('Content'); } /** * getter for the description, looks for set translation * * @return string The description from the translation object or an empty string */ public function getDescription() { return $this->getTranslationFieldValue('Description'); } /** * Returns the image respecting the current translation context. * * @return Image */ public function Image() : SilverStripeImage { $image = $this->getComponent('Image'); if (!$this->getCMSFieldsIsCalled) { $translation = $this->getTranslation(); if (is_object($translation) && $translation->ImageFile()->exists() ) { $image = $translation->ImageFile(); } } return $image; } /** * Returns the translated singular name of the object. If no translation exists * the class name will be returned. * * @return string */ public function singular_name() : string { return Tools::singular_name_for($this); } /** * Returns the translated plural name of the object. If no translation exists * the class name will be returned. * * @return string */ public function plural_name() : string { return Tools::plural_name_for($this); } /** * customizes the backends fields, mainly for ModelAdmin * * @return FieldList */ public function getCMSFields() : FieldList { $this->getCMSFieldsIsCalled = true; $this->beforeUpdateCMSFields(function(FieldList $fields) { $fields->removeByName('ProductID'); $fields->removeByName('PaymentMethodID'); $controller = Controller::curr(); if ($controller instanceof ProductAdmin || $controller instanceof PaymentMethodAdmin ) { $fields->removeByName('Content'); $fields->removeByName('Description'); } $fields->removeByName('SlidorionProductGroupWidgets'); $fields->addFieldToTab('Root.Main', TextField::create('ProductNumberToReference', $this->fieldLabel('ProductNumberToReference'))); }); return DataObjectExtension::getCMSFields($this); } /** * Field labels for display in tables. * * @param boolean $includerelations A boolean value to indicate if the labels returned include relation fields * * @return array */ public function fieldLabels($includerelations = true) : array { return $this->defaultFieldLabels($includerelations, [ 'ImageTranslations' => ImageTranslation::singleton()->plural_name(), 'PaymentMethod' => PaymentMethod::singleton()->singular_name(), 'Product' => Product::singleton()->singular_name(), 'Thumbnail' => _t(Image::class . '.THUMBNAIL', 'Preview'), 'Title' => _t(Image::class . '.TITLE', 'Display name'), 'Content' => _t(Image::class . '.CONTENT', 'Text content'), 'Description' => _t(Image::class . '.DESCRIPTION', 'Description (e.g. for Slidorion textfield)'), 'SortOrder' => _t(Image::class . '.SORTORDER', 'Sort order'), 'Image' => SilverStripeImage::singleton()->singular_name(), 'ProductNumberToReference' => _t(ImageSliderImage::class . '.ProductNumberToReference', 'Productnumber of the product to link to'), 'ProductNumberToReferenceInfo' => _t(ImageSliderImage::class . '.ProductNumberToReferenceInfo', 'Will be used instead the page.'), ]); } /** * Summaryfields for display in tables. * * @return array */ public function summaryFields() : array { $summaryFields = [ 'Image.ImageThumbnail' => $this->fieldLabel('Thumbnail'), 'Title' => $this->fieldLabel('Title') ]; $this->extend('updateSummaryFields', $summaryFields); return $summaryFields; } /** * Searchable fields definition * * @return array */ public function searchableFields() : array { $searchableFields = [ 'ImageTranslations.Title' => [ 'title' => $this->fieldLabel('Title'), 'filter' => PartialMatchFilter::class, ] ]; return $searchableFields; } /** * Returns a HTML snippet for the related Files icon. * * @return string */ public function getFileIcon() : string { return "<img src=\"{$this->Image()->Icon()}\" alt=\"{$this->Image()->FileType}\" title=\"{$this->Image()->Title}\" />"; } /** * Returns the products link * * @return string */ public function getProductLink() : string { $link = ""; if ($this->Product()->exists()) { $link = $this->Product()->Link(); } return $link; } /** * Was the object just accidently written? * object without attribute or file appended * * @return bool $result * * @author Roland Lehmann <rlehmann@pixeltricks.de> * @since 14.07.2012 */ public function isEmptyObject() : bool { $result = false; if (!$this->Image()->exists() && $this->isEmptyMultilingualAttributes() ) { $result = true; } return $result; } /** * Deletes the related SilverStripe Image before deleting $this. * * @return void */ public function onBeforeDelete() : void { parent::onBeforeDelete(); $image = $this->Image(); if ($image instanceof SilverStripeImage && $image->exists() ) { $image->deleteFile(); $image->delete(); } } /** * On before write hook. * * @return void * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 26.03.2013 */ protected function onBeforeWrite() : void { parent::onBeforeWrite(); if ($this->Product()->exists() && empty($this->Title) ) { $this->Title = $this->Product()->Title; } } /** * Returns the link. * * @return string * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 16.06.2014 */ public function Link() : string { if (is_null($this->link)) { $this->link = ""; if (!empty($this->ProductNumberToReference)) { $product = Product::get()->filter('ProductNumberShop', $this->ProductNumberToReference)->first(); if ($product instanceof Product) { $this->link = $product->Link(); } } } return $this->link; } } |