|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Cloudinary\Cloudinary\Model\Config\Backend; |
| 4 | + |
| 5 | +use Magento\Framework\App\Cache\TypeListInterface; |
| 6 | +use Magento\Framework\App\Config\ReinitableConfigInterface; |
| 7 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 8 | +use Magento\Framework\Data\Collection\AbstractDb; |
| 9 | +use Magento\Framework\Message\ManagerInterface; |
| 10 | +use Magento\Framework\Model\Context; |
| 11 | +use Magento\Framework\Model\ResourceModel\AbstractResource; |
| 12 | +use Magento\Framework\Registry; |
| 13 | + |
| 14 | +class VideoSettingsFreeParams extends \Magento\Framework\App\Config\Value |
| 15 | +{ |
| 16 | + const BAD_JSON_ERROR_MESSAGE = "Json error on 'Video free parameters' please correct."; |
| 17 | + |
| 18 | + const JSON_ERRORS = [ |
| 19 | + JSON_ERROR_NONE => 'No error', |
| 20 | + JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', |
| 21 | + JSON_ERROR_STATE_MISMATCH => 'State mismatch (invalid or malformed JSON)', |
| 22 | + JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded', |
| 23 | + JSON_ERROR_SYNTAX => 'Syntax error', |
| 24 | + JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded' |
| 25 | + ]; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var ManagerInterface |
| 29 | + */ |
| 30 | + private $messageManager; |
| 31 | + |
| 32 | + /** |
| 33 | + * Application config |
| 34 | + * |
| 35 | + * @var ScopeConfigInterface |
| 36 | + */ |
| 37 | + protected $appConfig; |
| 38 | + |
| 39 | + public function __construct( |
| 40 | + Context $context, |
| 41 | + Registry $registry, |
| 42 | + ScopeConfigInterface $config, |
| 43 | + TypeListInterface $cacheTypeList, |
| 44 | + AbstractResource $resource = null, |
| 45 | + AbstractDb $resourceCollection = null, |
| 46 | + ManagerInterface $messageManager, |
| 47 | + ReinitableConfigInterface $appConfig, |
| 48 | + array $data = [] |
| 49 | + ) { |
| 50 | + $this->messageManager = $messageManager; |
| 51 | + $this->appConfig = $appConfig; |
| 52 | + |
| 53 | + parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + protected function jsonDecode($jsonString) |
| 58 | + { |
| 59 | + $json = preg_replace('/\r|\n/','',trim($jsonString)); |
| 60 | + $json = str_replace( |
| 61 | + array('"', "'"), |
| 62 | + array('\"', '"'), |
| 63 | + $json |
| 64 | + ); |
| 65 | + |
| 66 | + $start = strpos($json, 'logoImageUrl'); |
| 67 | + // Find the end of the URL (first comma after the start of logoImageUrl) |
| 68 | + $end = strpos($json, ',', $start); |
| 69 | + $logoUrl = null; |
| 70 | + if ($start) { |
| 71 | + |
| 72 | + $logoUrl = substr($json, $start, $end - $start); |
| 73 | + $json = substr($json, 0, $start) . substr($json, $end + 1); |
| 74 | + } |
| 75 | + $linkStart = strpos($json, 'logoOnclickUrl'); |
| 76 | + $linkEnd = strpos($json, ',',$linkStart); |
| 77 | + $linkUrl = null; |
| 78 | + if ($linkStart) { |
| 79 | + $linkUrl = substr($json, $linkStart, $linkEnd - $linkStart); |
| 80 | + $json = substr($json, 0, $linkStart) . substr($json, $linkEnd +1); |
| 81 | + } |
| 82 | + |
| 83 | + // Extract the logoImageUrl part |
| 84 | + |
| 85 | + $json = preg_replace('/(\w+):/', '"$1":', $json); |
| 86 | + $arr = json_decode($json, true); |
| 87 | + |
| 88 | + if (is_array($arr)) { |
| 89 | + if ($logoUrl) { |
| 90 | + $url = str_replace("logoImageUrl:", "", $logoUrl); |
| 91 | + $url = str_replace('"', "", $url); |
| 92 | + $arr['player']['logoImageUrl'] = $url; |
| 93 | + } |
| 94 | + if ($linkUrl) { |
| 95 | + $url = str_replace("logoOnclickUrl:", "", $linkUrl); |
| 96 | + $url = str_replace('"', "", $url); |
| 97 | + $arr['player']['logoOnclickUrl'] = $url; |
| 98 | + } |
| 99 | + $json = json_encode($arr); |
| 100 | + } |
| 101 | + // $json = str_replace("'", '"', $json); |
| 102 | + return (json_decode($json)) ? $json : $jsonString; |
| 103 | + } |
| 104 | + |
| 105 | + function isJson($string) { |
| 106 | + json_decode($string); |
| 107 | + return json_last_error() === JSON_ERROR_NONE; |
| 108 | + } |
| 109 | + |
| 110 | + public function beforeSave() |
| 111 | + { |
| 112 | + $rawValue = $this->getValue(); |
| 113 | + |
| 114 | + parent::beforeSave(); |
| 115 | + |
| 116 | + $this->cacheTypeList->cleanType(\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER); |
| 117 | + $this->appConfig->reinit(); |
| 118 | + |
| 119 | + if ($rawValue) { |
| 120 | + |
| 121 | + $data = $this->jsonDecode($rawValue); |
| 122 | + if ($data === null || $data === false) { |
| 123 | + $this->setValue('{}'); |
| 124 | + try { |
| 125 | + if (json_last_error() !== JSON_ERROR_NONE) { |
| 126 | + $this->messageManager->addError(self::BAD_JSON_ERROR_MESSAGE . ' (' . self::JSON_ERRORS[json_last_error()] . ')'); |
| 127 | + } |
| 128 | + } catch (\Exception $e) { |
| 129 | + $this->messageManager->addError(self::BAD_JSON_ERROR_MESSAGE); |
| 130 | + } |
| 131 | + } else { |
| 132 | + if ($this->isJson($data)) { |
| 133 | + |
| 134 | + return $this->setValue($data); |
| 135 | + } |
| 136 | + $this->setValue('{}'); |
| 137 | + } |
| 138 | + } else { |
| 139 | + $this->setValue('{}'); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments