init
This commit is contained in:
Executable
+171
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
/**
|
||||
* Magezon
|
||||
*
|
||||
* This source file is subject to the Magezon Software License, which is available at https://www.magezon.com/license
|
||||
* Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
|
||||
* If you wish to customize this module for your needs.
|
||||
* Please refer to https://www.magezon.com for more information.
|
||||
*
|
||||
* @category Magezon
|
||||
* @package Magezon_Core
|
||||
* @copyright Copyright (C) 2019 Magezon (https://www.magezon.com)
|
||||
*/
|
||||
|
||||
namespace Magezon\Core\Helper;
|
||||
|
||||
use Magento\Framework\App\Filesystem\DirectoryList;
|
||||
|
||||
class Api extends \Magento\Framework\App\Helper\AbstractHelper
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\Filesystem
|
||||
*/
|
||||
protected $fileSystem;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\App\Helper\Context $context
|
||||
* @param \Magento\Framework\Filesystem $fileSystem
|
||||
*/
|
||||
public function __construct(
|
||||
\Magento\Framework\App\Helper\Context $context,
|
||||
\Magento\Framework\Filesystem $fileSystem
|
||||
) {
|
||||
parent::__construct($context);
|
||||
$this->fileSystem = $fileSystem;
|
||||
}
|
||||
|
||||
public function imagePreprocessing($fileContent, $saveFolder, $enableFilesDispersion = false)
|
||||
{
|
||||
if (strpos($fileContent, 'base64') === false) {
|
||||
return $fileContent;
|
||||
}
|
||||
|
||||
$mediaDirectory = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA);
|
||||
if (is_array($fileContent)) {
|
||||
$imageData = explode("base64,", $fileContent['base64_encoded_data'])[1];
|
||||
} else {
|
||||
$imageData = explode("base64,", $fileContent)[1];
|
||||
}
|
||||
|
||||
$imageData = base64_decode($imageData);
|
||||
$destinationFolder = $mediaDirectory->getAbsolutePath($saveFolder) . '/';
|
||||
if (!file_exists($destinationFolder)) {
|
||||
mkdir($destinationFolder, 0777, true);
|
||||
}
|
||||
|
||||
if (is_array($fileContent)) {
|
||||
if (!isset($fileContent['name'])) {
|
||||
$name = uniqid();
|
||||
} else {
|
||||
$name = $fileContent['name'];
|
||||
}
|
||||
} else {
|
||||
$name = uniqid();
|
||||
}
|
||||
|
||||
if (strpos($name, '.') === false) {
|
||||
$name .= '.jpg';
|
||||
}
|
||||
|
||||
$actual_name = pathinfo($destinationFolder . $name,PATHINFO_FILENAME);
|
||||
$original_name = $actual_name;
|
||||
$extension = pathinfo($destinationFolder . $name, PATHINFO_EXTENSION);
|
||||
$i = 1;
|
||||
|
||||
if ($enableFilesDispersion) {
|
||||
if ($this->endsWith($destinationFolder, "/")) {
|
||||
$destinationFolder = substr($destinationFolder, 0, -1);
|
||||
}
|
||||
$dispertionPath = $this->getDispretionPath($name) . '/';
|
||||
$destinationFolder .= $dispertionPath;
|
||||
$this->_createDestinationFolder($destinationFolder);
|
||||
}
|
||||
|
||||
while(file_exists($destinationFolder . $actual_name . '.' . $extension))
|
||||
{
|
||||
$actual_name = (string) $original_name . $i;
|
||||
$name = $actual_name . "." . $extension;
|
||||
$i++;
|
||||
}
|
||||
|
||||
$success = file_put_contents($destinationFolder . $name, $imageData);
|
||||
if ($success) {
|
||||
if ($enableFilesDispersion) {
|
||||
return $dispertionPath . $name;
|
||||
}
|
||||
return $name;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get dispertion path
|
||||
*
|
||||
* @param string $fileName
|
||||
* @return string
|
||||
*/
|
||||
public static function getDispretionPath($fileName)
|
||||
{
|
||||
$char = 0;
|
||||
$dispertionPath = '';
|
||||
while ($char < 2 && $char < strlen($fileName)) {
|
||||
if (empty($dispertionPath)) {
|
||||
$dispertionPath = '/' . ('.' == $fileName[$char] ? '_' : $fileName[$char]);
|
||||
} else {
|
||||
$dispertionPath = self::_addDirSeparator(
|
||||
$dispertionPath
|
||||
) . ('.' == $fileName[$char] ? '_' : $fileName[$char]);
|
||||
}
|
||||
$char++;
|
||||
}
|
||||
return $dispertionPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add directory separator
|
||||
*
|
||||
* @param string $dir
|
||||
* @return string
|
||||
*/
|
||||
protected static function _addDirSeparator($dir)
|
||||
{
|
||||
if (substr($dir, -1) != '/') {
|
||||
$dir .= '/';
|
||||
}
|
||||
return $dir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create destination folder
|
||||
*
|
||||
* @param string $destinationFolder
|
||||
* @return \Magento\Framework\File\Uploader
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function _createDestinationFolder($destinationFolder)
|
||||
{
|
||||
if (!$destinationFolder) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
if (substr($destinationFolder, -1) == '/') {
|
||||
$destinationFolder = substr($destinationFolder, 0, -1);
|
||||
}
|
||||
|
||||
if (!(is_dir($destinationFolder)
|
||||
|| mkdir($destinationFolder, 0777, true)
|
||||
)) {
|
||||
throw new \Exception("Unable to create directory '{$destinationFolder}'.");
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function endsWith($haystack, $needle)
|
||||
{
|
||||
$length = strlen($needle);
|
||||
|
||||
return $length === 0 ||
|
||||
(substr($haystack, -$length) === $needle);
|
||||
}
|
||||
}
|
||||
Executable
+456
@@ -0,0 +1,456 @@
|
||||
<?php
|
||||
/**
|
||||
* Magezon
|
||||
*
|
||||
* This source file is subject to the Magezon Software License, which is available at https://www.magezon.com/license
|
||||
* Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
|
||||
* If you wish to customize this module for your needs.
|
||||
* Please refer to https://www.magezon.com for more information.
|
||||
*
|
||||
* @category Magezon
|
||||
* @package Magezon_Core
|
||||
* @copyright Copyright (C) 2019 Magezon (https://www.magezon.com)
|
||||
*/
|
||||
|
||||
namespace Magezon\Core\Helper;
|
||||
|
||||
use \Magento\Framework\App\ObjectManager;
|
||||
|
||||
class Data extends \Magento\Framework\App\Helper\AbstractHelper
|
||||
{
|
||||
/**
|
||||
* Default charset
|
||||
*/
|
||||
const ICONV_CHARSET = 'UTF-8';
|
||||
|
||||
/**
|
||||
* @var \Magento\Store\Model\StoreManagerInterface
|
||||
*/
|
||||
protected $_storeManager;
|
||||
|
||||
/**
|
||||
* @var \Magento\Cms\Model\Template\FilterProvider
|
||||
*/
|
||||
protected $filterProvider;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Registry
|
||||
*/
|
||||
protected $registry;
|
||||
|
||||
/**
|
||||
* @var \Magento\Cms\Model\Page
|
||||
*/
|
||||
protected $cmsPage;
|
||||
|
||||
/**
|
||||
* @var \Magezon\Core\Framework\Serialize\Serializer\Json
|
||||
*/
|
||||
protected $serializer;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\App\Helper\Context $context
|
||||
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
|
||||
* @param \Magento\Cms\Model\Template\FilterProvider $filterProvider
|
||||
* @param \Magento\Framework\Registry $registry
|
||||
* @param \Magento\Cms\Model\Page $cmsPage
|
||||
* @param \Magezon\Core\Framework\Serialize\Serializer\Json $serializer
|
||||
*/
|
||||
public function __construct(
|
||||
\Magento\Framework\App\Helper\Context $context,
|
||||
\Magento\Store\Model\StoreManagerInterface $storeManager,
|
||||
\Magento\Cms\Model\Template\FilterProvider $filterProvider,
|
||||
\Magento\Framework\Registry $registry,
|
||||
\Magento\Cms\Model\Page $cmsPage,
|
||||
\Magezon\Core\Framework\Serialize\Serializer\Json $serializer
|
||||
) {
|
||||
parent::__construct($context);
|
||||
$this->_storeManager = $storeManager;
|
||||
$this->filterProvider = $filterProvider;
|
||||
$this->registry = $registry;
|
||||
$this->cmsPage = $cmsPage;
|
||||
$this->serializer = $serializer;
|
||||
}
|
||||
|
||||
public function filter($str)
|
||||
{
|
||||
if (!$str || !is_string($str)) {
|
||||
return $str;
|
||||
}
|
||||
$str = $this->decodeDirectiveImages($str);
|
||||
$storeId = $this->_storeManager->getStore()->getId();
|
||||
$filter = $this->filterProvider->getBlockFilter()->setStoreId($storeId);
|
||||
$variables = [];
|
||||
if ($this->cmsPage->getId()) {
|
||||
$variables['page'] = $this->cmsPage;
|
||||
}
|
||||
if ($category = $this->getCurrentCategory()) {
|
||||
$variables['category'] = $category;
|
||||
}
|
||||
if ($product = $this->getCurrentProduct()) {
|
||||
$variables['product'] = $product;
|
||||
}
|
||||
$filter->setVariables($variables);
|
||||
$productMetadata = ObjectManager::getInstance()->get(\Magento\Framework\App\ProductMetadataInterface::class);
|
||||
if ($productMetadata->getVersion() >= '2.4.0') {
|
||||
$filter->setStrictMode(false);
|
||||
}
|
||||
return $filter->filter($str);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Magento\Catalog\Model\Category
|
||||
*/
|
||||
public function getCurrentCategory()
|
||||
{
|
||||
return $this->registry->registry('current_category');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Magento\Catalog\Model\Product
|
||||
*/
|
||||
public function getCurrentProduct()
|
||||
{
|
||||
return $this->registry->registry('current_product');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $content
|
||||
* @return string
|
||||
*/
|
||||
public function decodeDirectiveImages($content)
|
||||
{
|
||||
$matches = $search = $replace = [];
|
||||
preg_match_all('/<img[\s\r\n]+.*?>/is', $content, $matches);
|
||||
foreach ($matches[0] as $imgHTML) {
|
||||
$key = 'directive/___directive/';
|
||||
if (strpos($imgHTML, $key) !== false) {
|
||||
$srcKey = 'src="';
|
||||
$start = strpos($imgHTML, $srcKey) + strlen($srcKey);
|
||||
$end = strpos($imgHTML, '"', $start);
|
||||
if ($end > $start) {
|
||||
$imgSrc = substr($imgHTML, $start, $end - $start);
|
||||
$start = strpos($imgSrc, $key) + strlen($key);
|
||||
$imgBase64 = substr($imgSrc, $start);
|
||||
$replaceHTML = str_replace($imgSrc, $this->urlDecoder->decode(urldecode($imgBase64)), $imgHTML);
|
||||
$search[] = $imgHTML;
|
||||
$replace[] = $replaceHTML;
|
||||
}
|
||||
}
|
||||
}
|
||||
return str_replace($search, $replace, $content);
|
||||
}
|
||||
|
||||
public function unserialize($string)
|
||||
{
|
||||
if ($string && $this->isJSON($string)) {
|
||||
return $this->serializer->unserialize($string);
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
public function serialize($array = [])
|
||||
{
|
||||
return $this->serializer->serialize($array);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $string
|
||||
* @return boolean
|
||||
*/
|
||||
public function isJSON($string)
|
||||
{
|
||||
return is_string($string) && is_array(json_decode($string, true)) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMediaUrl()
|
||||
{
|
||||
$mediaUrl = $this->_storeManager->getStore()->getBaseUrl(
|
||||
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
|
||||
);
|
||||
return $mediaUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove base url
|
||||
*/
|
||||
public function convertImageUrl($string)
|
||||
{
|
||||
$mediaUrl = $this->getMediaUrl();
|
||||
return str_replace($mediaUrl, '', $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function startsWith($haystack, $needle)
|
||||
{
|
||||
$length = strlen($needle);
|
||||
return (substr($haystack, 0, $length) === $needle);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function endsWith($haystack, $needle)
|
||||
{
|
||||
$length = strlen($needle);
|
||||
|
||||
return $length === 0 ||
|
||||
(substr($haystack, -$length) === $needle);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $string
|
||||
* @return string
|
||||
*/
|
||||
public function getImageUrl($string)
|
||||
{
|
||||
if ($string && is_string($string) && strpos($string, 'http') === false && (strpos($string, '<div') === false)) {
|
||||
$mediaUrl = $this->getMediaUrl();
|
||||
$string = $mediaUrl . $string;
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert string to numbder
|
||||
*/
|
||||
public function dataPreprocessing($data)
|
||||
{
|
||||
if (is_array($data)) {
|
||||
foreach ($data as &$_row) {
|
||||
$_row = $this->unserialize($_row);
|
||||
if ($_row === '1' || $_row === '0') {
|
||||
$_row = (int) $_row;
|
||||
}
|
||||
$_row = $this->getImageUrl($_row);
|
||||
if (is_array($_row)) {
|
||||
$_row = $this->dataPreprocessing($_row);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass through to mb_substr()
|
||||
*
|
||||
* @param string $string
|
||||
* @param int $offset
|
||||
* @param int $length
|
||||
* @return string
|
||||
*/
|
||||
public function substr($string, $length, $keepWords = true)
|
||||
{
|
||||
$string = $this->cleanString($string);
|
||||
if ($keepWords) {
|
||||
if (preg_match('/^.{1,' . $length . '}\b/s', $string, $match)) {
|
||||
$string = $match[0];
|
||||
}
|
||||
} else {
|
||||
$string = mb_substr($string, 0, $length, self::ICONV_CHARSET);
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean non UTF-8 characters
|
||||
*
|
||||
* @param string $string
|
||||
* @return string
|
||||
*/
|
||||
public function cleanString($string)
|
||||
{
|
||||
return mb_convert_encoding($string, self::ICONV_CHARSET);
|
||||
}
|
||||
/**
|
||||
* Retrieve string length using default charset
|
||||
*
|
||||
* @param string $string
|
||||
* @return int
|
||||
*/
|
||||
public function strlen($string)
|
||||
{
|
||||
return mb_strlen($string, self::ICONV_CHARSET);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter images with placeholders in the content
|
||||
*
|
||||
* @param string $content
|
||||
* @return string
|
||||
*/
|
||||
public function filterCarouselLazyImage($content)
|
||||
{
|
||||
$matches = $search = $replace = [];
|
||||
preg_match_all('/<img[\s\r\n]+.*?>/is', $content, $matches);
|
||||
$placeHolderUrl = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACklEQVR4nGNiAAAABgADNjd8qAAAAABJRU5ErkJggg==';
|
||||
$lazyClasses = 'owl-lazy';
|
||||
|
||||
foreach ($matches[0] as $imgHTML) {
|
||||
if (! preg_match("/src=['\"]data:image/is", $imgHTML) && strpos($imgHTML, 'data-src')===false) {
|
||||
|
||||
// replace the src and add the data-src attribute
|
||||
$replaceHTML = preg_replace('/<img(.*?)src=/is', '<img$1src="' . $placeHolderUrl . '" data-src=', $imgHTML);
|
||||
|
||||
// add the lazy class to the img element
|
||||
if (preg_match('/class=["\']/i', $replaceHTML)) {
|
||||
$replaceHTML = preg_replace('/class=(["\'])(.*?)["\']/is', 'class=$1' . $lazyClasses . ' $2$1', $replaceHTML);
|
||||
} else {
|
||||
$replaceHTML = preg_replace('/<img/is', '<img class="' . $lazyClasses . '"', $replaceHTML);
|
||||
}
|
||||
|
||||
$search[] = $imgHTML;
|
||||
$replace[] = $replaceHTML;
|
||||
}
|
||||
}
|
||||
|
||||
$content = str_replace($search, $replace, $content);
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $number
|
||||
* @return int
|
||||
*/
|
||||
public function getResponsiveClass($number)
|
||||
{
|
||||
if (in_array($number, [1, 2, 3, 4, 6, 12])) {
|
||||
return 12 / $number;
|
||||
}
|
||||
if ($number == 5) {
|
||||
return 15;
|
||||
}
|
||||
return $number;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public function getStyleColor($value, $isImportant = false)
|
||||
{
|
||||
if ($value && (!$this->startsWith($value, '#') && !$this->startsWith($value, 'rgb'))) {
|
||||
if ($value != 'transparent') {
|
||||
$value = '#' . $value;
|
||||
}
|
||||
}
|
||||
if ($value && $isImportant) {
|
||||
$value .= ' !important';
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public function getStyleProperty($value, $isImportant = false, $unit = '')
|
||||
{
|
||||
if (is_numeric($value)) {
|
||||
if ($unit) {
|
||||
$value .= $unit;
|
||||
} else {
|
||||
$value .= 'px';
|
||||
}
|
||||
}
|
||||
if ($value == '-') {
|
||||
$value = '';
|
||||
}
|
||||
if ($value && $isImportant) {
|
||||
$value .= ' !important';
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|array $target
|
||||
* @param array $styles
|
||||
* @param string $suffix
|
||||
* @return string
|
||||
*/
|
||||
public function getStyles($target, $styles, $suffix = '')
|
||||
{
|
||||
$html = '';
|
||||
if (is_array($target)) {
|
||||
foreach ($target as $k => $_selector) {
|
||||
if (!$_selector) {
|
||||
unset($target[$k]);
|
||||
}
|
||||
}
|
||||
$i = 0;
|
||||
$count = count($target);
|
||||
foreach ($target as $_selector) {
|
||||
$html .= $_selector . $suffix;
|
||||
if ($i!=$count-1) {
|
||||
$html .= ',';
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
} else {
|
||||
$html = $target . $suffix;
|
||||
}
|
||||
$stylesHtml = $this->parseStyles($styles);
|
||||
if (!$stylesHtml) {
|
||||
return;
|
||||
}
|
||||
if ($styles) {
|
||||
$html .= '{';
|
||||
$html .= $stylesHtml;
|
||||
$html .= '}';
|
||||
}
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $styles
|
||||
* @return string
|
||||
*/
|
||||
public function parseStyles($styles)
|
||||
{
|
||||
$result = '';
|
||||
foreach ($styles as $k => $v) {
|
||||
if ($v=='') {
|
||||
continue;
|
||||
}
|
||||
$result .= $k . ':' . $v . ';';
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check value is null
|
||||
*
|
||||
* @param $value
|
||||
* @return bool
|
||||
*/
|
||||
public function isNull($value)
|
||||
{
|
||||
if (is_numeric($value)) {
|
||||
return false;
|
||||
}
|
||||
if ($value === '' || $value === null) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $html
|
||||
* @return string
|
||||
*/
|
||||
public function cleanStyle($html)
|
||||
{
|
||||
$regex = '@(?:<style class="mgz-style">)(.*)</style>@msU';
|
||||
preg_match_all($regex, $html, $matches);
|
||||
if ($matches[0]) {
|
||||
$html = str_replace($matches[0], [], $html);
|
||||
}
|
||||
return $html;
|
||||
}
|
||||
}
|
||||
Executable
+83
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
/**
|
||||
* Magezon
|
||||
*
|
||||
* This source file is subject to the Magezon Software License, which is available at https://www.magezon.com/license
|
||||
* Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
|
||||
* If you wish to customize this module for your needs.
|
||||
* Please refer to https://www.magezon.com for more information.
|
||||
*
|
||||
* @category Magezon
|
||||
* @package Magezon_Core
|
||||
* @copyright Copyright (C) 2019 Magezon (https://www.magezon.com)
|
||||
*/
|
||||
|
||||
namespace Magezon\Core\Helper;
|
||||
|
||||
use Magento\Framework\App\Filesystem\DirectoryList;
|
||||
|
||||
class File extends \Magento\Framework\App\Helper\AbstractHelper
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\ObjectManagerInterface
|
||||
*/
|
||||
protected $_objectManager;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\RequestInterface
|
||||
*/
|
||||
protected $_httpRequest;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\App\Helper\Context $context
|
||||
* @param \Magento\Framework\ObjectManagerInterface $objectManager
|
||||
*/
|
||||
public function __construct(
|
||||
\Magento\Framework\App\Helper\Context $context,
|
||||
\Magento\Framework\ObjectManagerInterface $objectManager
|
||||
) {
|
||||
parent::__construct($context);
|
||||
$this->_objectManager = $objectManager;
|
||||
$this->_httpRequest = $context->getRequest();
|
||||
}
|
||||
|
||||
public function uploadImage($type, $data, $mediaFolder, $allowedExtensions = '', $maximumSize = 0)
|
||||
{
|
||||
$mediaDirectory = $this->_objectManager->get('Magento\Framework\Filesystem')->getDirectoryRead(DirectoryList::MEDIA);
|
||||
$imagePath = $mediaDirectory->getAbsolutePath();
|
||||
if (isset($data[$type]['delete'])) {
|
||||
$imagePath .= isset($data[$type]['value']) ? $data[$type]['value'] : $data[$type];
|
||||
if ($imagePath != $mediaDirectory && file_exists($imagePath)) {
|
||||
unlink($imagePath);
|
||||
}
|
||||
$data[$type] = '';
|
||||
}
|
||||
$image = $this->_httpRequest->getFiles($type);
|
||||
if (isset($image['error']) && $image['error'] == 0) {
|
||||
if ($imagePath) {
|
||||
if ($imagePath != $mediaDirectory && file_exists($imagePath) && $imagePath != $mediaDirectory->getAbsolutePath()) {
|
||||
unlink($imagePath);
|
||||
}
|
||||
}
|
||||
if ($maximumSize && $image['size'] > $maximumSize) {
|
||||
throw new \Magento\Framework\Exception\ValidatorException(__('The file is too large. Maximum upload file size: %1MB', $maximumSize/1000000));
|
||||
}
|
||||
$savePath = $mediaDirectory->getAbsolutePath($mediaFolder);
|
||||
$uploader = $this->_objectManager->create(
|
||||
'Magento\Framework\File\Uploader',
|
||||
array('fileId' => $type)
|
||||
);
|
||||
$uploader->setAllowRenameFiles(true);
|
||||
$uploader->setFilesDispersion(false);
|
||||
if ($allowedExtensions) {
|
||||
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
|
||||
}
|
||||
$file = $uploader->save($savePath);
|
||||
$data[$type] = $mediaFolder . '/' . $file['name'];
|
||||
$data[$type] = str_replace('//', '/', $data[$type]);
|
||||
} else if (isset($data[$type]) && is_array($data[$type])) {
|
||||
$data[$type] = $data[$type]['value'];
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
Executable
+127
@@ -0,0 +1,127 @@
|
||||
<?php
|
||||
/**
|
||||
* Magezon
|
||||
*
|
||||
* This source file is subject to the Magezon Software License, which is available at https://www.magezon.com/license
|
||||
* Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
|
||||
* If you wish to customize this module for your needs.
|
||||
* Please refer to https://www.magezon.com for more information.
|
||||
*
|
||||
* @category Magezon
|
||||
* @package Magezon_Core
|
||||
* @copyright Copyright (C) 2019 Magezon (https://www.magezon.com)
|
||||
*/
|
||||
|
||||
namespace Magezon\Core\Helper;
|
||||
|
||||
use Magento\Framework\App\Filesystem\DirectoryList;
|
||||
|
||||
class Image extends \Magento\Framework\App\Helper\AbstractHelper
|
||||
{
|
||||
/**
|
||||
* @var \Magezon\Core\Block\Product\ImageBuilder
|
||||
*/
|
||||
protected $imageBuilder;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Filesystem
|
||||
*/
|
||||
protected $_filesystem;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Image\AdapterFactory
|
||||
*/
|
||||
protected $_imageFactory;
|
||||
|
||||
/**
|
||||
* @var \Magento\Store\Model\StoreManagerInterface
|
||||
*/
|
||||
protected $_storeManager;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\App\Helper\Context $context
|
||||
* @param \Magezon\Core\Block\Product\ImageBuilder $imageBuilder
|
||||
* @param \Magento\Framework\Filesystem $filesystem
|
||||
* @param \Magento\Framework\Image\AdapterFactory $imageFactory
|
||||
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
|
||||
*/
|
||||
public function __construct(
|
||||
\Magento\Framework\App\Helper\Context $context,
|
||||
\Magezon\Core\Block\Product\ImageBuilder $imageBuilder,
|
||||
\Magento\Framework\Filesystem $filesystem,
|
||||
\Magento\Framework\Image\AdapterFactory $imageFactory,
|
||||
\Magento\Store\Model\StoreManagerInterface $storeManager
|
||||
) {
|
||||
parent::__construct($context);
|
||||
$this->imageBuilder = $imageBuilder;
|
||||
$this->_filesystem = $filesystem;
|
||||
$this->_imageFactory = $imageFactory;
|
||||
$this->_storeManager = $storeManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve product image
|
||||
*
|
||||
* @param \Magento\Catalog\Model\Product $product
|
||||
* @param string $imageId
|
||||
* @param array $attributes
|
||||
* @return \Magento\Catalog\Block\Product\Image
|
||||
*/
|
||||
public function resizeImage($product, $imageWidth = '', $imageHeight = '', $attributes = [], $imageId ='category_page_grid')
|
||||
{
|
||||
return $this->imageBuilder->setProduct($product)
|
||||
->setImageWidth($imageWidth)
|
||||
->setImageHeight($imageHeight)
|
||||
->setResizeImageWidth($imageWidth)
|
||||
->setResizeImageHeight($imageHeight)
|
||||
->setImageId($imageId)
|
||||
->setAttributes($attributes)
|
||||
->create();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $src
|
||||
* @param integer $width
|
||||
* @param integer $height
|
||||
* @param integer $quality
|
||||
* @param string $dir
|
||||
* @param string $newName
|
||||
* @param boolean $deleteIfExist
|
||||
* @return string
|
||||
*/
|
||||
public function resize($src, $width = 150, $height = 0, $quality = 80, $dir = 'magezon/resized', $newName = '', $deleteIfExist = false)
|
||||
{
|
||||
$dir = $dir . '/' . $width;
|
||||
$absPath = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath($src);
|
||||
if ($newName) {
|
||||
$imageResized = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath($dir . '/' . $newName);
|
||||
} else {
|
||||
$imageResized = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath($dir . '/' . $src);
|
||||
}
|
||||
$imageResize = $this->_imageFactory->create();
|
||||
$resizedURL = '';
|
||||
if (file_exists($absPath)) {
|
||||
$imageResize->open($absPath);
|
||||
$imageResize->backgroundColor([255, 255, 255]);
|
||||
$imageResize->constrainOnly(TRUE);
|
||||
$imageResize->keepTransparency(TRUE);
|
||||
$imageResize->keepFrame(true);
|
||||
$imageResize->keepAspectRatio(true);
|
||||
$imageResize->quality($quality);
|
||||
if ($height) {
|
||||
$imageResize->resize($width, $height);
|
||||
} else {
|
||||
$imageResize->resize($width);
|
||||
}
|
||||
if (!file_exists($imageResized) || $deleteIfExist) {
|
||||
$imageResize->save($imageResized);
|
||||
}
|
||||
if ($newName) {
|
||||
$resizedURL = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA). $dir . '/' . $newName;
|
||||
} else {
|
||||
$resizedURL = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA). $dir . '/' . $src;
|
||||
}
|
||||
}
|
||||
return $resizedURL;
|
||||
}
|
||||
}
|
||||
Executable
+285
@@ -0,0 +1,285 @@
|
||||
<?php
|
||||
/**
|
||||
* Magezon
|
||||
*
|
||||
* This source file is subject to the Magezon Software License, which is available at https://www.magezon.com/license
|
||||
* Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
|
||||
* If you wish to customize this module for your needs.
|
||||
* Please refer to https://www.magezon.com for more information.
|
||||
*
|
||||
* @category Magezon
|
||||
* @package Magezon_Core
|
||||
* @copyright Copyright (C) 2019 Magezon (https://www.magezon.com)
|
||||
*/
|
||||
|
||||
namespace Magezon\Core\Helper;
|
||||
|
||||
class Style extends \Magento\Framework\App\Helper\AbstractHelper
|
||||
{
|
||||
/**
|
||||
* @var Data
|
||||
*/
|
||||
protected $dataHelper;
|
||||
|
||||
/**
|
||||
* @param \Magezon\Core\Helper\Data $dataHelper
|
||||
*/
|
||||
public function __construct(
|
||||
\Magezon\Core\Helper\Data $dataHelper
|
||||
) {
|
||||
$this->dataHelper = $dataHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|array $target
|
||||
* @param array $styles
|
||||
* @param string $suffix
|
||||
* @return string
|
||||
*/
|
||||
public function getHtml($target, $styles, $suffix = '')
|
||||
{
|
||||
$html = '';
|
||||
if (is_array($target)) {
|
||||
foreach ($target as $k => $_selector) {
|
||||
if (!$_selector) {
|
||||
unset($target[$k]);
|
||||
}
|
||||
}
|
||||
$i = 0;
|
||||
$count = count($target);
|
||||
foreach ($target as $_selector) {
|
||||
$html .= $_selector . $suffix;
|
||||
if ($i!=$count-1) {
|
||||
$html .= ',';
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
} else {
|
||||
$html = $target . $suffix;
|
||||
}
|
||||
$stylesHtml = $this->parseStyles($styles);
|
||||
if (!$stylesHtml) return;
|
||||
if ($styles) {
|
||||
$html .= '{';
|
||||
$html .= $stylesHtml;
|
||||
$html .= '}';
|
||||
}
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $styles
|
||||
* @return string
|
||||
*/
|
||||
public function parseStyles($styles)
|
||||
{
|
||||
$result = '';
|
||||
foreach ($styles as $k => $v) {
|
||||
if ($v=='') continue;
|
||||
$result .= $k . ':' . $v . ';';
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public function getColor($value, $important = false)
|
||||
{
|
||||
if ($value && (!$this->dataHelper->startsWith($value, '#') && !$this->dataHelper->startsWith($value, 'rgb'))) {
|
||||
if ($value != 'transparent') {
|
||||
$value = '#' . $value;
|
||||
}
|
||||
}
|
||||
if ($value && $important) {
|
||||
$value .= ' !important';
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public function getProperty($value, $important = false, $unit = '')
|
||||
{
|
||||
if (!$unit) $unit = 'px';
|
||||
if (is_numeric($value)) {
|
||||
$value .= $unit;
|
||||
} else if ($value == '-') {
|
||||
$value = '';
|
||||
}
|
||||
if ($value && $important) {
|
||||
$value .= ' !important';
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
* @return boolean
|
||||
*/
|
||||
public function isNull($value) {
|
||||
if (is_numeric($value)) return false;
|
||||
if ($value === '' || $value === null) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
public function getStyles($data)
|
||||
{
|
||||
$styles = [];
|
||||
$config = new \Magento\Framework\DataObject($data);
|
||||
|
||||
$paddingTop = $this->getProperty($config['padding_top']);
|
||||
$paddingRight = $this->getProperty($config['padding_right']);
|
||||
$paddingBottom = $this->getProperty($config['padding_bottom']);
|
||||
$paddingLeft = $this->getProperty($config['padding_left']);
|
||||
if (!$this->isNull($paddingTop) && !$this->isNull($paddingRight) && !$this->isNull($paddingBottom) && !$this->isNull($paddingLeft)) {
|
||||
if ($paddingTop == $paddingRight && $paddingTop == $paddingBottom && $paddingTop == $paddingLeft) {
|
||||
$styles['padding'] = $paddingTop;
|
||||
} else {
|
||||
$styles['padding'] = $paddingTop . ' ' . $paddingRight . ' ' . $paddingBottom . ' ' . $paddingLeft;
|
||||
}
|
||||
} else {
|
||||
$styles['padding-top'] = $paddingTop;
|
||||
$styles['padding-right'] = $paddingRight;
|
||||
$styles['padding-bottom'] = $paddingBottom;
|
||||
$styles['padding-left'] = $paddingLeft;
|
||||
}
|
||||
|
||||
$marginTop = $this->getProperty($config['margin_top']);
|
||||
$marginRight = $this->getProperty($config['margin_right']);
|
||||
$marginBottom = $this->getProperty($config['margin_bottom']);
|
||||
$marginLeft = $this->getProperty($config['margin_left']);
|
||||
if (!$this->isNull($marginTop) && !$this->isNull($marginRight) && !$this->isNull($marginBottom) && !$this->isNull($marginLeft)) {
|
||||
if ($marginTop == $marginRight && $marginTop == $marginBottom && $marginTop == $marginLeft) {
|
||||
$styles['margin'] = $marginTop;
|
||||
} else {
|
||||
$styles['margin'] = $marginTop . ' ' . $marginRight . ' ' . $marginBottom . ' ' . $marginLeft;
|
||||
}
|
||||
} else {
|
||||
$styles['margin-top'] = $marginTop;
|
||||
$styles['margin-right'] = $marginRight;
|
||||
$styles['margin-bottom'] = $marginBottom;
|
||||
$styles['margin-left'] = $marginLeft;
|
||||
}
|
||||
|
||||
$borderStyle = $config['border_style'];
|
||||
if ($borderStyle && $config['border_color']) {
|
||||
$borderTopWidth = $this->getProperty($config['border_top_width']);
|
||||
$borderRightWidth = $this->getProperty($config['border_right_width']);
|
||||
$borderBottomWidth = $this->getProperty($config['border_bottom_width']);
|
||||
$borderLeftWidth = $this->getProperty($config['border_left_width']);
|
||||
$borderColor = $this->getColor($config['border_color']);
|
||||
$styles['border-color'] = $this->getColor($config['border_color']);
|
||||
|
||||
if (!$this->isNull($borderTopWidth)) {
|
||||
$styles['border-top-width'] = $borderTopWidth;
|
||||
$styles['border-top-style'] = $borderStyle;
|
||||
}
|
||||
|
||||
if (!$this->isNull($borderRightWidth)) {
|
||||
$styles['border-right-width'] = $borderRightWidth;
|
||||
$styles['border-right-style'] = $borderStyle;
|
||||
}
|
||||
|
||||
if (!$this->isNull($borderBottomWidth)) {
|
||||
$styles['border-bottom-width'] = $borderBottomWidth;
|
||||
$styles['border-bottom-style'] = $borderStyle;
|
||||
}
|
||||
|
||||
if (!$this->isNull($borderLeftWidth)) {
|
||||
$styles['border-left-width'] = $borderLeftWidth;
|
||||
$styles['border-left-style'] = $borderStyle;
|
||||
}
|
||||
|
||||
if (!$this->isNull($borderTopWidth) && !$this->isNull($borderRightWidth) && !$this->isNull($borderBottomWidth) && !$this->isNull($borderLeftWidth)) {
|
||||
unset($styles['border-top-style']);
|
||||
unset($styles['border-right-style']);
|
||||
unset($styles['border-bottom-style']);
|
||||
unset($styles['border-left-style']);
|
||||
$styles['border-style'] = $borderStyle;
|
||||
|
||||
if ($borderTopWidth == $borderRightWidth && $borderTopWidth == $borderBottomWidth && $borderTopWidth == $borderLeftWidth) {
|
||||
$styles['border'] = $borderTopWidth . ' ' . $borderStyle . ' ' . $borderColor;
|
||||
unset($styles['border-top-width']);
|
||||
unset($styles['border-top-style']);
|
||||
unset($styles['border-right-width']);
|
||||
unset($styles['border-right-style']);
|
||||
unset($styles['border-bottom-width']);
|
||||
unset($styles['border-bottom-style']);
|
||||
unset($styles['border-left-width']);
|
||||
unset($styles['border-left-style']);
|
||||
unset($styles['border-color']);
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->isNull($borderTopWidth) && $this->isNull($borderRightWidth) && $this->isNull($borderBottomWidth) && $this->isNull($borderLeftWidth)) {
|
||||
unset($styles['border-style']);
|
||||
unset($styles['border-color']);
|
||||
}
|
||||
}
|
||||
|
||||
$borderTopLeftRadius = $this->getProperty($config['border_top_left_radius']);
|
||||
$borderTopRightRadius = $this->getProperty($config['border_top_right_radius']);
|
||||
$borderBottomRightRadius = $this->getProperty($config['border_bottom_right_radius']);
|
||||
$borderBottomLeftRadius = $this->getProperty($config['border_bottom_left_radius']);
|
||||
if (!$this->isNull($borderTopLeftRadius) || !$this->isNull($borderTopRightRadius) || !$this->isNull($borderBottomRightRadius) || !$this->isNull($borderBottomLeftRadius)) {
|
||||
if ($borderTopLeftRadius == $borderTopRightRadius && $borderTopLeftRadius == $borderBottomRightRadius && $borderTopLeftRadius == $borderBottomLeftRadius) {
|
||||
$styles['border-radius'] = $borderTopLeftRadius;
|
||||
} else {
|
||||
if (!$borderTopLeftRadius) $borderTopLeftRadius = 0;
|
||||
if (!$borderTopRightRadius) $borderTopRightRadius = 0;
|
||||
if (!$borderBottomRightRadius) $borderBottomRightRadius = 0;
|
||||
if (!$borderBottomLeftRadius) $borderBottomLeftRadius = 0;
|
||||
$styles['border-radius'] = $borderTopLeftRadius . ' ' . $borderTopRightRadius . ' ' . $borderBottomRightRadius . ' ' . $borderBottomLeftRadius;
|
||||
}
|
||||
}
|
||||
|
||||
$styles['background-color'] = $this->getColor($config['background_color']);
|
||||
if ($backgroundImage = $config['background_image']) {
|
||||
$styles['background-image'] = 'url(\'' . $this->dataHelper->getImageUrl($backgroundImage) . '\')';
|
||||
|
||||
$backgroundPosition = $config['background_position'];
|
||||
if ($backgroundPosition == 'custom') {
|
||||
if ($config['custom_background_position']) {
|
||||
$styles['background-position'] = $config['custom_background_position'];
|
||||
}
|
||||
} else {
|
||||
$styles['background-position'] = $backgroundPosition;
|
||||
}
|
||||
|
||||
$backgroundSize = $config['background_size'];
|
||||
if ($backgroundSize == 'custom') {
|
||||
if ($config['custom_background_size']) {
|
||||
$styles['background-size'] = $config['custom_background_size'];
|
||||
}
|
||||
} else {
|
||||
$styles['background-size'] = $backgroundSize;
|
||||
}
|
||||
|
||||
$styles['background-repeat'] = $config['background_repeat'];
|
||||
}
|
||||
|
||||
if ($config['boxshadow']) {
|
||||
$boxshadowHorizontal = $this->getProperty($config['boxshadow_horizontal']);
|
||||
$boxshadowVertical = $this->getProperty($config['boxshadow_vertical']);
|
||||
$boxshadowBlur = $this->getProperty($config['boxshadow_blur']);
|
||||
$boxshadowSpread = $this->getProperty($config['boxshadow_spread']);
|
||||
$boxshadowColor = $this->getColor($config['boxshadow_color']);
|
||||
$styles['box-shadow'] = $boxshadowHorizontal . ' ' . $boxshadowVertical . ' ' . $boxshadowBlur . ' ' . $boxshadowSpread . ' ' . $boxshadowColor;
|
||||
if ($config['boxshadow_position'] == 'inset') {
|
||||
$styles['box-shadow'] .= ' inset';
|
||||
}
|
||||
}
|
||||
|
||||
return $styles;
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
/**
|
||||
* Magezon
|
||||
*
|
||||
* This source file is subject to the Magezon Software License, which is available at https://www.magezon.com/license
|
||||
* Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
|
||||
* If you wish to customize this module for your needs.
|
||||
* Please refer to https://www.magezon.com for more information.
|
||||
*
|
||||
* @category Magezon
|
||||
* @package Magezon_Core
|
||||
* @copyright Copyright (C) 2019 Magezon (https://www.magezon.com)
|
||||
*/
|
||||
|
||||
namespace Magezon\Core\Helper\Wysiwyg;
|
||||
|
||||
class Images extends \Magento\Cms\Helper\Wysiwyg\Images
|
||||
{
|
||||
/**
|
||||
* Prepare Image insertion declaration for Wysiwyg or textarea(as_is mode)
|
||||
*
|
||||
* @param string $filename Filename transferred via Ajax
|
||||
* @param bool $renderAsTag Leave image HTML as is or transform it to controller directive
|
||||
* @return string
|
||||
*/
|
||||
public function getImageHtmlDeclaration($filename, $renderAsTag = false)
|
||||
{
|
||||
$fileurl = $this->getCurrentUrl() . $filename;
|
||||
$mediaUrl = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
|
||||
$mediaPath = str_replace($mediaUrl, '', $fileurl);
|
||||
return $mediaPath;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user