Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
CategoryWidget.php 5.12 KiB
Newer Older
Nicolas's avatar
Nicolas committed
<?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);
    }

    /**
Nicolas's avatar
Nicolas committed
     * Retrieve category widget title
Nicolas's avatar
Nicolas committed
     *
     * @return string
     */
Nicolas's avatar
Nicolas committed
    public function getTitle()
Nicolas's avatar
Nicolas committed
    {
Nicolas's avatar
Nicolas committed
        return $this->getData(self::TITLE);
Nicolas's avatar
Nicolas committed
     * Retrieve category widget identifier
Nicolas's avatar
Nicolas committed
     *
     * @return string
     */
Nicolas's avatar
Nicolas committed
    public function getIdentifier()
Nicolas's avatar
Nicolas committed
    {
Nicolas's avatar
Nicolas committed
        return (string)$this->getData(self::IDENTIFIER);
Nicolas's avatar
Nicolas committed
    }

    /**
     * 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);
    }

    /**
Nicolas's avatar
Nicolas committed
     * Set Title
Nicolas's avatar
Nicolas committed
     *
Nicolas's avatar
Nicolas committed
     * @param string $title
Nicolas's avatar
Nicolas committed
     * @return CategoryWidgetInterface
     */
Nicolas's avatar
Nicolas committed
    public function setTitle($title)
Nicolas's avatar
Nicolas committed
    {
Nicolas's avatar
Nicolas committed
        return $this->setData(self::TITLE, $title);
Nicolas's avatar
Nicolas committed
     * Set path
Nicolas's avatar
Nicolas committed
     *
Nicolas's avatar
Nicolas committed
     * @param string $identifier
Nicolas's avatar
Nicolas committed
     * @return CategoryWidgetInterface
     */
Nicolas's avatar
Nicolas committed
    public function setIdentifier($identifier)
Nicolas's avatar
Nicolas committed
    {
Nicolas's avatar
Nicolas committed
        return $this->setData(self::IDENTIFIER, $identifier);
Nicolas's avatar
Nicolas committed
    }

    /**
     * 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')];
    }
}