slideDataFactory = $slideDataFactory; $this->dataObjectHelper = $dataObjectHelper; $this->_storeManager = $storeManager; $this->directoryList = $directoryList; $this->filterProvider = $filterProvider; parent::__construct($context, $registry, $resource, $resourceCollection, $data); } /** * Retrieve slide model with slide data * @return SlideInterface */ public function getDataModel() { $slideData = $this->getData(); $slideDataObject = $this->slideDataFactory->create(); $this->dataObjectHelper->populateWithArray( $slideDataObject, $slideData, SlideInterface::class ); return $slideDataObject; } public function getIdentities() { return [$this->_eventPrefix . '_' . $this->getId()]; } public function beforeSave() { if ($this->hasDataChanges()) { $this->setUpdateTime(date('Y-m-d H:i:s')); } parent::beforeSave(); } public function setUpdateTime($updateTime) { return $this->setData(self::UPDATE_TIME, $updateTime); } /** * Returns image url * * @param string $attributeCode * @return bool|string * @throws LocalizedException */ public function getImageUrl($attributeCode = 'image') { $url = false; $image = $this->getData($attributeCode); if ($image) { if (is_string($image)) { $store = $this->_storeManager->getStore(); $baseUrl = $store->getBaseUrl(); $baseUrl = str_replace(parse_url($baseUrl, PHP_URL_PATH), '/', $baseUrl); $url = $baseUrl . ltrim($image, '/'); } else { throw new LocalizedException( __('Something went wrong while getting the image url.') ); } } return $url; } public function hasImage($fieldId = 'image') { return ((bool)$this->getData($fieldId) !== false && $this->existsImage($fieldId)); } public function existsImage($fieldId = 'image') { $fullPath = $this->getAbsoluteImagePath($fieldId); return $fullPath && file_exists($fullPath) && is_file($fullPath); } public function getAbsoluteImagePath($fieldId = 'image') { return $this->getPrefixedPath( $this->getData($fieldId), $this->directoryList->getPath("media") ); } private function getPrefixedPath($path, $prefix = '') { $fullPath = ''; if (!empty($path)) { $path = preg_replace('#^/pub#i', '', $path); $path = preg_replace('#^/media#i', '', $path); if (preg_match('#/$#', $prefix)) { $path = ltrim($path, '/'); } $fullPath = $prefix . $path; } return $fullPath; } public function getContent($store_id = null) { $content = $this->getData('content'); if (empty($content)) { $content = ''; } $content = trim($content); /** @var Template $filter */ $filter = $this->filterProvider->getBlockFilter(); if (!empty($store_id)) { $filter = $filter->setStoreId($store_id); } $content = $filter->filter($content); return $content; } public function getContent2($store_id = null) { $content = $this->getData('content2'); if (empty($content)) { $content = ''; } $content = trim($content); /** @var Template $filter */ $filter = $this->filterProvider->getBlockFilter(); if (!empty($store_id)) { $filter = $filter->setStoreId($store_id); } $content = $filter->filter($content); return $content; } /** * Model construct that should be used for object initialization * * @return void */ protected function _construct() { $this->_init(ResourceModel\Slide::class); } }