![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/cartforge.co/app/code/Webkul/PrivateShop/Model/ |
<?php /** * Webkul Software * * @category Webkul * @package Webkul_PrivateShop * @author Webkul Software Private Limited * @copyright Webkul Software Private Limited (https://webkul.com) * @license https://store.webkul.com/license.html */ namespace Webkul\PrivateShop\Model; use Webkul\PrivateShop\Api\Data\PrivateGroupInterface; use Magento\Customer\Api\CustomerRepositoryInterface; use Magento\Framework\Exception\MailException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory; class PrivateGroup extends \Magento\Framework\Model\AbstractModel implements PrivateGroupInterface { public const XML_PATH_EMAIL_TEMPLATE = 'private_shop/email/notification_template'; public const EMAIL_SENDER_NAME = 'trans_email/ident_support/name'; public const EMAIL_SENDER = 'trans_email/ident_support/email'; /** * @var string Event Prefix */ protected $_eventPrefix = 'webkul_privateshop_privategroup'; /** * @var \Webkul\PrivateShop\Model\EmailData */ protected $emailData = null; /** * @var \Magento\Framework\App\Config\ScopeConfigInterface */ protected $scopeConfig; /** * @var \Magento\Framework\Mail\Template\TransportBuilder */ protected $_transportBuilder; /** * @var \Magento\Store\Model\StoreManagerInterface */ protected $_storeManager; /** * @var CustomerRepositoryInterface */ protected $customerRepository; /** * @var CollectionFactory */ protected $customerCollectionFactory; /** * @var \Magento\Framework\Translate\Inline\StateInterface */ protected $inlineTranslation; /** * @var \Psr\Log\LoggerInterface */ protected $logger; /** * * @param \Magento\Framework\Model\Context $context * @param \Magento\Framework\Registry $registry * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig * @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder * @param \Magento\Store\Model\StoreManagerInterface $storeManager * @param CustomerRepositoryInterface $customerRepository * @param CollectionFactory $customerCollectionFactory * @param \Psr\Log\LoggerInterface $logger * @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation */ public function __construct( \Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Webkul\PrivateShop\Model\Mail\TransportBuilder $transportBuilder, \Magento\Store\Model\StoreManagerInterface $storeManager, CustomerRepositoryInterface $customerRepository, CollectionFactory $customerCollectionFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation ) { parent::__construct($context, $registry); $this->scopeConfig = $scopeConfig; $this->_transportBuilder = $transportBuilder; $this->_storeManager = $storeManager; $this->customerRepository = $customerRepository; $this->customerCollectionFactory = $customerCollectionFactory; $this->inlineTranslation = $inlineTranslation; $this->logger = $logger; } /** * Constructor Method * * @return void */ protected function _construct() { $this->_init(\Webkul\PrivateShop\Model\ResourceModel\PrivateGroup::class); } /** * Get privategroup_id * * @return string */ public function getId() { return $this->getData(self::ENTITY_ID); } /** * Set privategroup_id * * @param string $privategroupId * @return \Webkul\PrivateShop\Api\Data\PrivateGroupInterface */ public function setId($privategroupId) { return $this->setData(self::ENTITY_ID, $privategroupId); } /** * Get status * * @return string|null */ public function getStatus() { return $this->getData(self::STATUS); } /** * Set status * * @param string $status * @return \Webkul\PrivateShop\Api\Data\PrivateGroupInterface */ public function setStatus($status) { return $this->setData(self::STATUS, $status); } /** * Get group_name * * @return string */ public function getGroupName() { return $this->getData(self::GROUP_NAME); } /** * Set group_name * * @param string $groupName * @return \Webkul\PrivateShop\Api\Data\PrivateGroupInterface */ public function setGroupName($groupName) { return $this->setData(self::GROUP_NAME, $groupName); } /** * SetEmailData * * @param \Webkul\PrivateShop\Model\EmailData $emailData * @return void */ public function setEmailData($emailData) { $this->emailData = $emailData; } /** * Collection fo customer assigned to group * * @param int $groupId * @return \Magento\Customer\Model\ResourceModel\Customer\Collection */ private function getCustomerCollection() { $filterArray = []; $filterArray[] = ['like' => '%'.$this->getId().'%']; return $this->customerCollectionFactory->create() ->addAttributeToFilter('customer_private_group', $filterArray); } /** * Send Email to all customer belongs to group. * * @return void */ public function sendEmail() { $customerCollection = $this->getCustomerCollection(); if ($customerCollection->getSize()) { $template = $this->getTemplateId(self::XML_PATH_EMAIL_TEMPLATE); foreach ($customerCollection as $customer) { $transportBuilder = $this->_transportBuilder; $transportBuilder->setTemplateIdentifier($template) ->setTemplateOptions( [ 'area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $this->_storeManager->getStore()->getId(), ] ) ->setTemplateVars(['customer' => $customer, 'data' => $this->emailData]) ->setFrom( [ 'email' => $this->getConfigValue( self::EMAIL_SENDER, $this->getStore()->getStoreId() ), 'name' => $this->getConfigValue( self::EMAIL_SENDER_NAME, $this->getStore()->getStoreId() ) ] ) ->addTo($customer->getEmail(), $customer->getFirstname()); try { $transport = $transportBuilder->getTransport(); $transport->sendMessage(); $this->inlineTranslation->resume(); } catch (MailException $e) { throw new \Magento\Framework\Exception\LocalizedException($e->getMessage()); } catch (MailException $e) { throw new \Magento\Framework\Exception\LocalizedException($e->getMessage()); } } } } /** * Return store configuration value. * * @param string $path * @param int $storeId * * @return mixed */ protected function getConfigValue($path, $storeId) { return $this->scopeConfig->getValue( $path, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $storeId ); } /** * Return store. * * @return Store */ public function getStore() { return $this->_storeManager->getStore(); } /** * Return template id. * * @param getTemplateId $xmlPath * @return mixed */ public function getTemplateId($xmlPath) { return $this->getConfigValue($xmlPath, $this->getStore()->getStoreId()); } }