Files
2025-08-02 16:30:27 +02:00

183 lines
4.4 KiB
PHP

<?php
namespace KupShop\OrderDiscountBundle\Actions;
use KupShop\OrderDiscountBundle\Actions\Frontend\HandlerInterface;
use KupShop\OrderDiscountBundle\Entity\OrderDiscount;
use KupShop\OrderingBundle\Entity\Purchase\PurchaseState;
abstract class AbstractAction implements ActionInterface
{
protected static $type;
protected static $position = 100;
protected $adminTemplate;
protected $translationSection = 'OrderDiscountsAction';
protected $icon_class = 'glyphicon glyphicon-edit';
protected $messages = [];
protected $divideDiscountPrice = false;
/**
* @param array $data
*
* @return bool
*/
public function getDivideDiscountPrice($data = [])
{
if (!empty($data['divideDiscountPrice'])) {
$this->divideDiscountPrice = ($data['divideDiscountPrice'] == 'Y');
} else {
$this->divideDiscountPrice = ((\Settings::getDefault()['divide_discount_price'] ?? 'N') == 'Y');
}
return $this->divideDiscountPrice;
}
/**
* @return string
*/
public function getAdminTemplate()
{
return $this->adminTemplate;
}
/**
* @return string
*
* @throws \Exception
*/
public static function getType()
{
if (!static::$type) {
throw new \Exception('Missing type');
}
return static::$type;
}
public static function getPosition()
{
return static::$position;
}
public function getDelayedExecution(): ?int
{
return null;
}
/**
* @return string
*
* @throws \Exception
*/
public function getName()
{
return translate(static::$type, $this->translationSection);
}
/**
* @return string
*/
public function getDescription()
{
return translate(static::$type.'Description', $this->translationSection);
}
/**
* @return string
*/
public function getIconClass()
{
return $this->icon_class;
}
public function getFrontendHandler(): ?HandlerInterface
{
return null;
}
public function applyMultiple(PurchaseState &$purchaseState, OrderDiscount $orderDiscount, array $data, array $multiplier)
{
foreach ($multiplier as $_) {
$this->applyResult($purchaseState, $orderDiscount, array_merge($data, ['trigger_data' => $_]));
}
}
protected function getVars($vars)
{
return $vars;
}
/**
* @param string $name
* @param array $data
*
* @return string
*
* @throws \SmartyException
*/
public function printActionSettings($name = 'data', $data = [], ?int $actionId = null)
{
if (!$this->getAdminTemplate()) {
throw new \Exception('Missing template');
}
$smarty = createSmarty(true, true);
$templateVars = [
'id_action' => $actionId,
'name' => $name,
'data' => $data,
'object' => $this,
];
$smarty->assign($this->getVars($templateVars));
return $smarty->fetch($this->getAdminTemplate());
}
/**
* @return bool
*/
public function checkValidity(&$data)
{
return true;
}
public function handleData($data)
{
$data['messages'] = array_filter($data['messages'] ?? []);
$data = array_filter($data);
return $data;
}
public function getUserMessages($id = ''): array
{
$userMessages = [];
$this->messages = array_filter($this->messages);
foreach ($this->messages as $type => $message) {
$id = $this->getType().$id.'_'.$type;
$userMessages[$id] = ['message' => $message, 'severity' => $type];
unset($this->messages[$type]);
}
return $userMessages;
}
public function createItemNote($data = [], &$name = ''): array
{
$note = ['item_type' => 'discount', 'id_discount' => $data['id'] ?? null, 'discount_type' => self::getType(),
'divide_discount_price' => $this->getDivideDiscountPrice($data), ];
if ($trigger_data = ($data['trigger_data'] ?? null)) {
$note = array_merge($note, $trigger_data);
$coupon = ($trigger_data['coupon'] ?? $trigger_data['generated_coupon']['code'] ?? null);
if ($coupon) {
$name .= " ({$coupon})";
}
}
return $note;
}
}