Oubli dans la MAJ en 2.3.4 --> à vérifier s'il n'y a pas d'autres oublis

This commit is contained in:
2020-05-03 23:44:13 +02:00
parent 315320df7c
commit 28b21af6c8
8 changed files with 285 additions and 17 deletions

View File

@@ -20,6 +20,8 @@ use Thelia\Core\HttpFoundation\Request;
use Thelia\Core\HttpFoundation\Session\Session;
use Thelia\Core\Security\SecurityContext;
use Thelia\Core\Template\ParserContext;
use Thelia\Coupon\CouponManager;
use Thelia\Coupon\Type\CouponInterface;
use Thelia\Log\Tlog;
use Thelia\Model\Base\BrandQuery;
use Thelia\Model\Cart;
@@ -33,6 +35,7 @@ use Thelia\Model\FolderQuery;
use Thelia\Model\MetaDataQuery;
use Thelia\Model\ModuleConfigQuery;
use Thelia\Model\ModuleQuery;
use Thelia\Model\Order;
use Thelia\Model\OrderQuery;
use Thelia\Model\ProductQuery;
use Thelia\Model\Tools\ModelCriteriaTools;
@@ -40,6 +43,8 @@ use Thelia\TaxEngine\TaxEngine;
use Thelia\Tools\DateTimeFormat;
use TheliaSmarty\Template\AbstractSmartyPlugin;
use TheliaSmarty\Template\SmartyPluginDescriptor;
use Thelia\Core\Event\Image\ImageEvent;
use Thelia\Core\Event\TheliaEvents;
/**
* Implementation of data access to main Thelia objects (users, cart, etc.)
@@ -64,6 +69,9 @@ class DataAccessFunctions extends AbstractSmartyPlugin
/** @var TaxEngine */
protected $taxEngine;
/** @var CouponManager */
protected $couponManager;
private static $dataAccessCache = array();
public function __construct(
@@ -71,13 +79,15 @@ class DataAccessFunctions extends AbstractSmartyPlugin
SecurityContext $securityContext,
TaxEngine $taxEngine,
ParserContext $parserContext,
EventDispatcherInterface $dispatcher
EventDispatcherInterface $dispatcher,
CouponManager $couponManager
) {
$this->securityContext = $securityContext;
$this->parserContext = $parserContext;
$this->requestStack = $requestStack;
$this->dispatcher = $dispatcher;
$this->taxEngine = $taxEngine;
$this->couponManager = $couponManager;
}
/**
@@ -306,6 +316,14 @@ class DataAccessFunctions extends AbstractSmartyPlugin
self::$dataAccessCache['currentCountry'] = $taxCountry;
}
/** @var State $taxState */
if (array_key_exists('currentState', self::$dataAccessCache)) {
$taxState = self::$dataAccessCache['currentState'];
} else {
$taxState = $this->taxEngine->getDeliveryState();
self::$dataAccessCache['currentState'] = $taxState;
}
/** @var Cart $cart */
$cart = $this->getSession()->getSessionCart($this->dispatcher);
@@ -325,17 +343,17 @@ class DataAccessFunctions extends AbstractSmartyPlugin
break;
case "total_price":
case "total_price_with_discount":
$result = $cart->getTotalAmount();
$result = $cart->getTotalAmount(true);
break;
case "total_price_without_discount":
$result = $cart->getTotalAmount(false);
break;
case "total_taxed_price":
case "total_taxed_price_with_discount":
$result = $cart->getTaxedAmount($taxCountry);
$result = $cart->getTaxedAmount($taxCountry, true, $taxState);
break;
case "total_taxed_price_without_discount":
$result = $cart->getTaxedAmount($taxCountry, false);
$result = $cart->getTaxedAmount($taxCountry, false, $taxState);
break;
case "is_virtual":
case "contains_virtual_product":
@@ -353,6 +371,31 @@ class DataAccessFunctions extends AbstractSmartyPlugin
return $result;
}
public function couponDataAccess($params, &$smarty)
{
/** @var Order $order */
$order = $this->getSession()->getOrder();
$attribute = $this->getNormalizedParam($params, array('attribute', 'attrib', 'attr'));
switch ($attribute) {
case 'has_coupons':
return count($this->couponManager->getCouponsKept()) > 0;
case 'coupon_count':
return count($this->couponManager->getCouponsKept());
case 'coupon_list':
$orderCoupons = [];
/** @var CouponInterface $coupon */
foreach($this->couponManager->getCouponsKept() as $coupon) {
$orderCoupons[] = $coupon->getCode();
}
return $orderCoupons;
case 'is_delivery_free':
return $this->couponManager->isCouponRemovingPostage($order);
}
throw new \InvalidArgumentException(sprintf("%s has no '%s' attribute", 'Order', $attribute));
}
/**
* Provides access to an attribute of the current order
*
@@ -362,6 +405,7 @@ class DataAccessFunctions extends AbstractSmartyPlugin
*/
public function orderDataAccess($params, &$smarty)
{
/** @var Order $order */
$order = $this->getSession()->getOrder();
$attribute = $this->getNormalizedParam($params, array('attribute', 'attrib', 'attr'));
switch ($attribute) {
@@ -743,6 +787,134 @@ class DataAccessFunctions extends AbstractSmartyPlugin
return $return;
}
/**
* Provides access to the uploaded store-related images (such as logo or favicon)
*
* @param array $params
* @param string $content
* @param \Smarty_Internal_Template $template
* @param boolean $repeat
* @return string|null
*/
public function storeMediaDataAccess($params, $content, \Smarty_Internal_Template $template, &$repeat)
{
$type = $this->getParam($params, 'type', null);
$allowedTypes = ['favicon', 'logo', 'banner'];
if ($type !== null && in_array($type, $allowedTypes)) {
switch ($type) {
case 'favicon':
$configKey = 'favicon_file';
$defaultImageName = 'favicon.png';
break;
case 'logo':
$configKey = 'logo_file';
$defaultImageName = 'logo.png';
break;
case 'banner':
$configKey = 'banner_file';
$defaultImageName = 'banner.jpg';
break;
}
$uploadDir = ConfigQuery::read('images_library_path');
if ($uploadDir === null) {
$uploadDir = THELIA_LOCAL_DIR . 'media' . DS . 'images';
} else {
$uploadDir = THELIA_ROOT . $uploadDir;
}
$uploadDir .= DS . 'store';
$imageFileName = ConfigQuery::read($configKey);
$skipImageTransform = false;
// If we couldn't find the image path in the config table or if it doesn't exist, we take the default image provided.
if ($imageFileName == null) {
$imageSourcePath = $uploadDir . DS . $defaultImageName;
} else {
$imageSourcePath = $uploadDir . DS . $imageFileName;
if (!file_exists($imageSourcePath)) {
Tlog::getInstance()->error(sprintf('Source image file %s does not exists.', $imageSourcePath));
$imageSourcePath = $uploadDir . DS . $defaultImageName;
}
if ($type == 'favicon') {
$extension = pathinfo($imageSourcePath, PATHINFO_EXTENSION);
if ($extension == 'ico') {
$mime_type = 'image/x-icon';
// If the media is a .ico favicon file, we skip the image transformations,
// as transformations on .ico file are not supported by Thelia.
$skipImageTransform = true;
} else {
$mime_type = 'image/png';
}
$template->assign('MEDIA_MIME_TYPE', $mime_type);
}
}
$event = new ImageEvent();
$event->setSourceFilepath($imageSourcePath)
->setCacheSubdirectory('store');
if (!$skipImageTransform) {
switch ($this->getParam($params, 'resize_mode', null)) {
case 'crop':
$resize_mode = \Thelia\Action\Image::EXACT_RATIO_WITH_CROP;
break;
case 'borders':
$resize_mode = \Thelia\Action\Image::EXACT_RATIO_WITH_BORDERS;
break;
case 'none':
default:
$resize_mode = \Thelia\Action\Image::KEEP_IMAGE_RATIO;
}
// Prepare transformations
$width = $this->getParam($params, 'width', null);
$height = $this->getParam($params, 'height', null);
$rotation = $this->getParam($params, 'rotation', null);
if (!is_null($width)) {
$event->setWidth($width);
}
if (!is_null($height)) {
$event->setHeight($height);
}
$event->setResizeMode($resize_mode);
if (!is_null($rotation)) {
$event->setRotation($rotation);
}
}
$this->dispatcher->dispatch(TheliaEvents::IMAGE_PROCESS, $event);
$template->assign('MEDIA_URL', $event->getFileUrl());
}
if (isset($content)) {
return $content;
}
return null;
}
/**
* @inheritdoc
*/
@@ -765,6 +937,9 @@ class DataAccessFunctions extends AbstractSmartyPlugin
new SmartyPluginDescriptor('function', 'stats', $this, 'statsAccess'),
new SmartyPluginDescriptor('function', 'meta', $this, 'metaAccess'),
new SmartyPluginDescriptor('function', 'module_config', $this, 'moduleConfigDataAccess'),
new SmartyPluginDescriptor('function', 'coupon', $this, 'couponDataAccess'),
new SmartyPluginDescriptor('block', 'local_media', $this, 'storeMediaDataAccess'),
);
}