CategoryWidget.php 5.12 KiB
<?php
namespace NicolasBejean\CategoryWidget\Model;
use \Magento\Framework\Exception\LocalizedException;
use \NicolasBejean\CategoryWidget\Api\Data\CategoryWidgetInterface;
use \Magento\Framework\DataObject\IdentityInterface;
use \Magento\Framework\Model\AbstractModel;
/**
* Class CategoryWidget
*
* @category PHP
* @package NicolasBejean\CategoryWidget\Model
* @author Nicolas Béjean <nicolas@bejean.eu>
* @license https://github.com/nicolasbejean/category-widget/blob/master/licence.txt BSD Licence
* @link https://www.bejean.eu
*/
class CategoryWidget extends AbstractModel implements CategoryWidgetInterface, IdentityInterface
{
/**
* Category Widget Cache tag
*
* @var string
*/
const CACHE_TAG = 'categorywidget';
/**
* Category Widget statuses
*/
const STATUS_ENABLED = 1;
const STATUS_DISABLED = 0;
/**
* Prefix of model events names
*
* @var string
*/
protected $eventPrefix = 'categorywidget';
/**
* @return void
*/
protected function _construct()
{
$this->_init(ResourceModel\CategoryWidget::class);
}
/**
* Prevent category widgetrecursion
*
* @return AbstractModel
* @throws LocalizedException
*/
public function beforeSave()
{
if ($this->hasDataChanges()) {
$this->setUpdateTime(null);
}
$needle = 'entity_id="' . $this->getId() . '"';
if (false == strstr($this->getIdentifier(), $needle)) {
return parent::beforeSave();
}
throw new LocalizedException(
__('Make sure that category widget path does not reference the category widget itself.')
);
}
/**
* Get identities
*
* @return array
*/
public function getIdentities()
{
return [self::CACHE_TAG . '_' . $this->getId(), self::CACHE_TAG . '_' . $this->getIdentifier()];
}
/**
* Retrieve category widgetid
*
* @return int
*/
public function getId()
{
return $this->getData(self::ENTITY_ID);
}
/**
* Retrieve category widget title
*
* @return string
*/
public function getTitle()
{
return $this->getData(self::TITLE);
}
/**
* Retrieve category widget identifier
*
* @return string
*/
public function getIdentifier()
{
return (string)$this->getData(self::IDENTIFIER);
}
/**
* Retrieve category widget content
*
* @return string
*/
public function getContent()
{
return $this->getData(self::CONTENT);
}
/**
* Retrieve category widget creation time
*
* @return string
*/
public function getCreationTime()
{
return $this->getData(self::CREATION_TIME);
}
/**
* Retrieve category widget update time
*
* @return string
*/
public function getUpdateTime()
{
return $this->getData(self::UPDATE_TIME);
}
/**
* Is active
*
* @return bool
*/
public function isActive()
{
return (bool)$this->getData(self::IS_ACTIVE);
}
/**
* Set ID
*
* @param int $id
* @return CategoryWidgetInterface
*/
public function setId($id)
{
return $this->setData(self::ENTITY_ID, $id);
}
/**
* Set Title
*
* @param string $title
* @return CategoryWidgetInterface
*/
public function setTitle($title)
{
return $this->setData(self::TITLE, $title);
}
/**
* Set path
*
* @param string $identifier
* @return CategoryWidgetInterface
*/
public function setIdentifier($identifier)
{
return $this->setData(self::IDENTIFIER, $identifier);
}
/**
* Set Content
*
* @param string $content
* @return CategoryWidgetInterface
*/
public function setContent($content)
{
return $this->setData(self::CONTENT, $content);
}
/**
* Set creation time
*
* @param string $creationTime
* @return CategoryWidgetInterface
*/
public function setCreationTime($creationTime)
{
return $this->setData(self::CREATION_TIME, $creationTime);
}
/**
* Set update time
*
* @param string $updateTime
* @return CategoryWidgetInterface
*/
public function setUpdateTime($updateTime)
{
return $this->setData(self::UPDATE_TIME, $updateTime);
}
/**
* Set is active
*
* @param bool|int $isActive
* @return CategoryWidgetInterface
*/
public function setIsActive($isActive)
{
return $this->setData(self::IS_ACTIVE, $isActive);
}
/**
* Receive slider store ids
*
* @return int[]
*/
public function getStores()
{
return $this->hasData('stores') ? $this->getData('stores') : $this->getData('store_id');
}
/**
* Prepare category widget statuses.
*
* @return array
*/
public function getAvailableStatuses()
{
return [self::STATUS_ENABLED => __('Enabled'), self::STATUS_DISABLED => __('Disabled')];
}
}