|
Apache/2.4.41 (Ubuntu) Linux vmi616275.contaboserver.net 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) server ip : 62.171.164.128 | your ip : 127.0.0.1 safemode OFF > / home / dev2.destoffenstraat.com / app / code / Makarovsoft / Email / Model / |
Filename | /home/dev2.destoffenstraat.com/app/code/Makarovsoft/Email/Model/Message.php |
Size | 11.47 kb |
Permission | rwxrwxrwx |
Owner | root : root |
Create time | 17-Aug-2025 10:26 |
Last modified | 06-Apr-2021 18:06 |
Last accessed | 22-Aug-2025 17:18 |
Actions | edit | rename | delete | download (gzip) |
View | text | code | image |
<?php
namespace Makarovsoft\Email\Model;
use Magento\Customer\Model\Customer;
use Magento\Email\Model\Template\Filter;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Filter\FilterManager;
use Magento\Framework\Model\Context;
use Magento\Framework\Registry;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\Data\Collection\Db;
use Magento\Sales\Model\Order;
use Makarovsoft\Email\Model\Message\Url;
use Makarovsoft\Email\Model\Message\Source\IsActive;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Framework\Data\Collection\AbstractDb;
use Makarovsoft\Email\Model\MessageFactory as MessageFactory;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory as OrderCollectionFactory;
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory;
use Makarovsoft\Email\Helper\Provider;
/**
* @method string getName()
* @method Message setUpdatedAt(\string $date)
* @method Message setCreatedAt(\string $date)
* @method Message setUrlKey(\string $urlKey)
* @method array getStores()
* @method int getStoreId()
* @method bool hasStores()
* @method Message setStoreId(\int $storeId)
* @method Message setAvatar(\string $avatar)
* @method string getAvatar()
* @method Message setResume(\string $resumee)
* @method string getResume()
* @method ResourceModel\Message _getResource()
* @method ResourceModel\Message getResource()
* @method string getUrlKey()
* @method int getIsActive()
* @method Message setIsActive(\bool $active)
* @method string getBiography()
* @method string getDob()
* @method string getMetaTitle()
* @method string getMetaDescription()
* @method string getMetaKeywords()
* @method Message setProductsData(array $products)
* @method Message setIsChangedProductList(\bool $changed)
* @method array|null getProductsData()
* @method Message setAffectedProductIds(array $productIds)
* @method int getPosition()
* @method array getCategoriesIds()
* @method Message setCategoriesIds(array $categoryIds)
* @method Message setIsChangedCategoryList(\bool $changed)
* @method Message setAffectedCategoryIds(array $categoryIds)
*/
class Message extends AbstractModel
{
/**
* status enabled
*
* @var int
*/
const STATUS_IN_QUEUE = 1;
/**
* status disabled
*
* @var int
*/
const STATUS_ERROR = 2;
const STATUS_SENT = 3;
/**
* @var Url
*/
protected $urlModel;
/**
* cache tag
*
* @var string
*/
const CACHE_TAG = 'makarovsoft_email_message';
/**
* cache tag
*
* @var string
*/
protected $_cacheTag = 'makarovsoft_email_message';
/**
* Prefix of model events names
*
* @var string
*/
protected $_eventPrefix = 'makarovsoft_email_message';
/**
* filter model
*
* @var \Magento\Framework\Filter\FilterManager
*/
protected $filter;
/**
* @var IsActive
*/
protected $statusList;
/**
* @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
*/
protected $productCollectionFactory;
/**
* @var \Magento\Catalog\Model\ResourceModel\Product\Collection
*/
protected $productCollection;
/**
* @var \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory
*/
protected $categoryCollectionFactory;
/**
* @var \Magento\Catalog\Model\ResourceModel\Category\Collection
*/
protected $categoryCollection;
/**
* @var \Makarovsoft\Email\Model\Filter
*/
protected $variablesFilter;
/**
* @var MessageCollectionFactory
*/
protected $messageFactory;
/**
* @var \Magento\Sales\Model\ResourceModel\Order\Collection
*/
protected $orderFactory;
/**
* @var CustomerCollection
*/
protected $customerFactory;
/**
* @var Provider
*/
protected $provider;
/**
* @param ProductCollectionFactory $productCollectionFactory
* @param CategoryCollectionFactory $categoryCollectionFactory
* @param FilterManager $filter
* @param Url $urlModel
* @param IsActive $statusList
* @param Context $context
* @param Registry $registry
* @param AbstractResource|null $resource
* @param AbstractDb|null $resourceCollection
* @param array $data
*/
public function __construct(
ProductCollectionFactory $productCollectionFactory,
CategoryCollectionFactory $categoryCollectionFactory,
\Makarovsoft\Email\Model\Filter $variablesFilter,
MessageFactory $factory,
CustomerCollectionFactory $customerCollectionFactory,
OrderCollectionFactory $orderCollectionFactory,
Provider $provider,
FilterManager $filter,
Url $urlModel,
IsActive $statusList,
Context $context,
Registry $registry,
AbstractResource $resource = null,
AbstractDb $resourceCollection = null,
array $data = []
)
{
$this->productCollectionFactory = $productCollectionFactory;
$this->categoryCollectionFactory = $categoryCollectionFactory;
$this->filter = $filter;
$this->urlModel = $urlModel;
$this->statusList = $statusList;
$this->variablesFilter = $variablesFilter;
$this->messageFactory = $factory;
$this->orderFactory = $orderCollectionFactory;
$this->customerFactory = $customerCollectionFactory;
$this->provider = $provider;
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
}
/**
* Initialize resource model
*
* @return void
*/
protected function _construct()
{
$this->_init('Makarovsoft\Email\Model\ResourceModel\Message');
}
public function beforeSave() {
$to = null;
/* @var $customer Customer */
if ($customer = $this->getData('customer')) {
if ($customer->getEmail()) {
$to = $customer->getEmail();
}
if (is_null($to)) {
throw new \RuntimeException(__('Customer %1 does not have defined email', $customer->getEmail()));
}
}
/* @var $order Order */
if ($order = $this->getData('order')) {
$to = $order->getCustomerEmail();
if (is_null($to)) {
throw new \RuntimeException(__('Order %1 does not have defined customer email', $order->getIncrementId()));
}
}
if (!is_null($to)) {
$this->setData('to', $to);
}
$this->setBody($this->parse($this->getBody()));
$this->setSubject($this->parse($this->getSubject()));
if ($this->getData('instant')) {
$this->setData('is_active', Message::STATUS_SENT);
$this->provider->setMessage($this)->send();
}
return parent::beforeSave();
}
public function afterSave() {
}
public function parse($string)
{
if (!is_null($this->getData('customer'))) {
/* @var $customer Customer */
$this->variablesFilter->setVariables(['customer' => $this->getData('customer')]);
}
if (!is_null($this->getData('order'))) {
/* @var $order Order */
$this->variablesFilter->setVariables(['order' => $this->getData('order')]);
}
return $this->variablesFilter->filter($string);
}
/**
* Check if message url key exists
* return message id if message exists
*
* @param string $urlKey
* @param int $storeId
* @return int
*/
public function checkUrlKey($urlKey, $storeId)
{
return $this->_getResource()->checkUrlKey($urlKey, $storeId);
}
/**
* Get identities
*
* @return array
*/
public function getIdentities()
{
return [self::CACHE_TAG . '_' . $this->getId()];
}
/**
* Prepare message's statuses.
*
* @return array
*/
public function getAvailableStatuses()
{
return $this->statusList->getOptions();
}
/**
* get default message values
* @return array
*/
public function getDefaultValues()
{
return [
'is_active' => self::STATUS_IN_QUEUE
];
}
/**
* sanitize the url key
*
* @param $string
* @return string
*/
public function formatUrlKey($string)
{
return $this->filter->translitUrl($string);
}
/**
* @return mixed
*/
public function getMessageUrl()
{
return $this->urlModel->getMessageUrl($this);
}
/**
* @return bool
*/
public function isActive()
{
return (bool)$this->getIsActive();
}
/**
* @return array|mixed
*/
public function getProductsPosition()
{
if (!$this->getId()) {
return array();
}
$array = $this->getData('products_position');
if (is_null($array)) {
$array = $this->getResource()->getProductsPosition($this);
$this->setData('products_position', $array);
}
return $array;
}
/**
* @param string $attributes
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
*/
public function getSelectedProductsCollection($attributes = '*')
{
if (is_null($this->productCollection)) {
$collection = $this->productCollectionFactory->create();
$collection->addAttributeToSelect($attributes);
$collection->joinField(
'position',
'makarovsoft_email_message_product',
'position',
'product_id=entity_id',
'{{table}}.message_id='.$this->getId(),
'inner'
);
$this->productCollection = $collection;
}
return $this->productCollection;
}
/**
* @return array
*/
public function getCategoryIds()
{
if (!$this->hasData('category_ids')) {
$ids = $this->_getResource()->getCategoryIds($this);
$this->setData('category_ids', $ids);
}
return (array) $this->_getData('category_ids');
}
/**
* @param string $attributes
* @return \Magento\Catalog\Model\ResourceModel\Category\Collection
*/
public function getSelectedCategoriesCollection($attributes = '*')
{
if (is_null($this->categoryCollection)) {
$collection = $this->categoryCollectionFactory->create();
$collection->addAttributeToSelect($attributes);
$collection->joinField(
'position',
'makarovsoft_email_message_category',
'position',
'category_id=entity_id',
'{{table}}.message_id='.$this->getId(),
'inner'
);
$this->categoryCollection = $collection;
}
return $this->categoryCollection;
}
}
namespace Makarovsoft\Email\Model;
use Magento\Customer\Model\Customer;
use Magento\Email\Model\Template\Filter;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Filter\FilterManager;
use Magento\Framework\Model\Context;
use Magento\Framework\Registry;
use Magento\Framework\Model\ResourceModel\AbstractResource;
use Magento\Framework\Data\Collection\Db;
use Magento\Sales\Model\Order;
use Makarovsoft\Email\Model\Message\Url;
use Makarovsoft\Email\Model\Message\Source\IsActive;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
use Magento\Framework\Data\Collection\AbstractDb;
use Makarovsoft\Email\Model\MessageFactory as MessageFactory;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory as OrderCollectionFactory;
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory;
use Makarovsoft\Email\Helper\Provider;
/**
* @method string getName()
* @method Message setUpdatedAt(\string $date)
* @method Message setCreatedAt(\string $date)
* @method Message setUrlKey(\string $urlKey)
* @method array getStores()
* @method int getStoreId()
* @method bool hasStores()
* @method Message setStoreId(\int $storeId)
* @method Message setAvatar(\string $avatar)
* @method string getAvatar()
* @method Message setResume(\string $resumee)
* @method string getResume()
* @method ResourceModel\Message _getResource()
* @method ResourceModel\Message getResource()
* @method string getUrlKey()
* @method int getIsActive()
* @method Message setIsActive(\bool $active)
* @method string getBiography()
* @method string getDob()
* @method string getMetaTitle()
* @method string getMetaDescription()
* @method string getMetaKeywords()
* @method Message setProductsData(array $products)
* @method Message setIsChangedProductList(\bool $changed)
* @method array|null getProductsData()
* @method Message setAffectedProductIds(array $productIds)
* @method int getPosition()
* @method array getCategoriesIds()
* @method Message setCategoriesIds(array $categoryIds)
* @method Message setIsChangedCategoryList(\bool $changed)
* @method Message setAffectedCategoryIds(array $categoryIds)
*/
class Message extends AbstractModel
{
/**
* status enabled
*
* @var int
*/
const STATUS_IN_QUEUE = 1;
/**
* status disabled
*
* @var int
*/
const STATUS_ERROR = 2;
const STATUS_SENT = 3;
/**
* @var Url
*/
protected $urlModel;
/**
* cache tag
*
* @var string
*/
const CACHE_TAG = 'makarovsoft_email_message';
/**
* cache tag
*
* @var string
*/
protected $_cacheTag = 'makarovsoft_email_message';
/**
* Prefix of model events names
*
* @var string
*/
protected $_eventPrefix = 'makarovsoft_email_message';
/**
* filter model
*
* @var \Magento\Framework\Filter\FilterManager
*/
protected $filter;
/**
* @var IsActive
*/
protected $statusList;
/**
* @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
*/
protected $productCollectionFactory;
/**
* @var \Magento\Catalog\Model\ResourceModel\Product\Collection
*/
protected $productCollection;
/**
* @var \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory
*/
protected $categoryCollectionFactory;
/**
* @var \Magento\Catalog\Model\ResourceModel\Category\Collection
*/
protected $categoryCollection;
/**
* @var \Makarovsoft\Email\Model\Filter
*/
protected $variablesFilter;
/**
* @var MessageCollectionFactory
*/
protected $messageFactory;
/**
* @var \Magento\Sales\Model\ResourceModel\Order\Collection
*/
protected $orderFactory;
/**
* @var CustomerCollection
*/
protected $customerFactory;
/**
* @var Provider
*/
protected $provider;
/**
* @param ProductCollectionFactory $productCollectionFactory
* @param CategoryCollectionFactory $categoryCollectionFactory
* @param FilterManager $filter
* @param Url $urlModel
* @param IsActive $statusList
* @param Context $context
* @param Registry $registry
* @param AbstractResource|null $resource
* @param AbstractDb|null $resourceCollection
* @param array $data
*/
public function __construct(
ProductCollectionFactory $productCollectionFactory,
CategoryCollectionFactory $categoryCollectionFactory,
\Makarovsoft\Email\Model\Filter $variablesFilter,
MessageFactory $factory,
CustomerCollectionFactory $customerCollectionFactory,
OrderCollectionFactory $orderCollectionFactory,
Provider $provider,
FilterManager $filter,
Url $urlModel,
IsActive $statusList,
Context $context,
Registry $registry,
AbstractResource $resource = null,
AbstractDb $resourceCollection = null,
array $data = []
)
{
$this->productCollectionFactory = $productCollectionFactory;
$this->categoryCollectionFactory = $categoryCollectionFactory;
$this->filter = $filter;
$this->urlModel = $urlModel;
$this->statusList = $statusList;
$this->variablesFilter = $variablesFilter;
$this->messageFactory = $factory;
$this->orderFactory = $orderCollectionFactory;
$this->customerFactory = $customerCollectionFactory;
$this->provider = $provider;
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
}
/**
* Initialize resource model
*
* @return void
*/
protected function _construct()
{
$this->_init('Makarovsoft\Email\Model\ResourceModel\Message');
}
public function beforeSave() {
$to = null;
/* @var $customer Customer */
if ($customer = $this->getData('customer')) {
if ($customer->getEmail()) {
$to = $customer->getEmail();
}
if (is_null($to)) {
throw new \RuntimeException(__('Customer %1 does not have defined email', $customer->getEmail()));
}
}
/* @var $order Order */
if ($order = $this->getData('order')) {
$to = $order->getCustomerEmail();
if (is_null($to)) {
throw new \RuntimeException(__('Order %1 does not have defined customer email', $order->getIncrementId()));
}
}
if (!is_null($to)) {
$this->setData('to', $to);
}
$this->setBody($this->parse($this->getBody()));
$this->setSubject($this->parse($this->getSubject()));
if ($this->getData('instant')) {
$this->setData('is_active', Message::STATUS_SENT);
$this->provider->setMessage($this)->send();
}
return parent::beforeSave();
}
public function afterSave() {
}
public function parse($string)
{
if (!is_null($this->getData('customer'))) {
/* @var $customer Customer */
$this->variablesFilter->setVariables(['customer' => $this->getData('customer')]);
}
if (!is_null($this->getData('order'))) {
/* @var $order Order */
$this->variablesFilter->setVariables(['order' => $this->getData('order')]);
}
return $this->variablesFilter->filter($string);
}
/**
* Check if message url key exists
* return message id if message exists
*
* @param string $urlKey
* @param int $storeId
* @return int
*/
public function checkUrlKey($urlKey, $storeId)
{
return $this->_getResource()->checkUrlKey($urlKey, $storeId);
}
/**
* Get identities
*
* @return array
*/
public function getIdentities()
{
return [self::CACHE_TAG . '_' . $this->getId()];
}
/**
* Prepare message's statuses.
*
* @return array
*/
public function getAvailableStatuses()
{
return $this->statusList->getOptions();
}
/**
* get default message values
* @return array
*/
public function getDefaultValues()
{
return [
'is_active' => self::STATUS_IN_QUEUE
];
}
/**
* sanitize the url key
*
* @param $string
* @return string
*/
public function formatUrlKey($string)
{
return $this->filter->translitUrl($string);
}
/**
* @return mixed
*/
public function getMessageUrl()
{
return $this->urlModel->getMessageUrl($this);
}
/**
* @return bool
*/
public function isActive()
{
return (bool)$this->getIsActive();
}
/**
* @return array|mixed
*/
public function getProductsPosition()
{
if (!$this->getId()) {
return array();
}
$array = $this->getData('products_position');
if (is_null($array)) {
$array = $this->getResource()->getProductsPosition($this);
$this->setData('products_position', $array);
}
return $array;
}
/**
* @param string $attributes
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection
*/
public function getSelectedProductsCollection($attributes = '*')
{
if (is_null($this->productCollection)) {
$collection = $this->productCollectionFactory->create();
$collection->addAttributeToSelect($attributes);
$collection->joinField(
'position',
'makarovsoft_email_message_product',
'position',
'product_id=entity_id',
'{{table}}.message_id='.$this->getId(),
'inner'
);
$this->productCollection = $collection;
}
return $this->productCollection;
}
/**
* @return array
*/
public function getCategoryIds()
{
if (!$this->hasData('category_ids')) {
$ids = $this->_getResource()->getCategoryIds($this);
$this->setData('category_ids', $ids);
}
return (array) $this->_getData('category_ids');
}
/**
* @param string $attributes
* @return \Magento\Catalog\Model\ResourceModel\Category\Collection
*/
public function getSelectedCategoriesCollection($attributes = '*')
{
if (is_null($this->categoryCollection)) {
$collection = $this->categoryCollectionFactory->create();
$collection->addAttributeToSelect($attributes);
$collection->joinField(
'position',
'makarovsoft_email_message_category',
'position',
'category_id=entity_id',
'{{table}}.message_id='.$this->getId(),
'inner'
);
$this->categoryCollection = $collection;
}
return $this->categoryCollection;
}
}