PageRenderTime 27ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-widget/Model/Widget.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 417 lines | 233 code | 38 blank | 146 comment | 35 complexity | e4ac2adcc008438c282e6d63eaeed536 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Widget\Model;
  7. /**
  8. * Widget model for different purposes
  9. */
  10. class Widget
  11. {
  12. /**
  13. * @var \Magento\Widget\Model\Config\Data
  14. */
  15. protected $dataStorage;
  16. /**
  17. * @var \Magento\Framework\App\Cache\Type\Config
  18. */
  19. protected $configCacheType;
  20. /**
  21. * @var \Magento\Framework\View\Asset\Repository
  22. */
  23. protected $assetRepo;
  24. /**
  25. * @var \Magento\Framework\View\Asset\Source
  26. */
  27. protected $assetSource;
  28. /**
  29. * @var \Magento\Framework\View\FileSystem
  30. */
  31. protected $viewFileSystem;
  32. /**
  33. * @var \Magento\Framework\Escaper
  34. */
  35. protected $escaper;
  36. /**
  37. * @var array
  38. */
  39. protected $widgetsArray = [];
  40. /**
  41. * @var \Magento\Widget\Helper\Conditions
  42. */
  43. protected $conditionsHelper;
  44. /**
  45. * @param \Magento\Framework\Escaper $escaper
  46. * @param \Magento\Widget\Model\Config\Data $dataStorage
  47. * @param \Magento\Framework\View\Asset\Repository $assetRepo
  48. * @param \Magento\Framework\View\Asset\Source $assetSource
  49. * @param \Magento\Framework\View\FileSystem $viewFileSystem
  50. * @param \Magento\Widget\Helper\Conditions $conditionsHelper
  51. */
  52. public function __construct(
  53. \Magento\Framework\Escaper $escaper,
  54. \Magento\Widget\Model\Config\Data $dataStorage,
  55. \Magento\Framework\View\Asset\Repository $assetRepo,
  56. \Magento\Framework\View\Asset\Source $assetSource,
  57. \Magento\Framework\View\FileSystem $viewFileSystem,
  58. \Magento\Widget\Helper\Conditions $conditionsHelper
  59. ) {
  60. $this->escaper = $escaper;
  61. $this->dataStorage = $dataStorage;
  62. $this->assetRepo = $assetRepo;
  63. $this->assetSource = $assetSource;
  64. $this->viewFileSystem = $viewFileSystem;
  65. $this->conditionsHelper = $conditionsHelper;
  66. }
  67. /**
  68. * Return widget config based on its class type
  69. *
  70. * @param string $type Widget type
  71. * @return null|array
  72. * @api
  73. */
  74. public function getWidgetByClassType($type)
  75. {
  76. $widgets = $this->getWidgets();
  77. /** @var array $widget */
  78. foreach ($widgets as $widget) {
  79. if (isset($widget['@'])) {
  80. if (isset($widget['@']['type'])) {
  81. if ($type === $widget['@']['type']) {
  82. return $widget;
  83. }
  84. }
  85. }
  86. }
  87. return null;
  88. }
  89. /**
  90. * Return widget XML configuration as \Magento\Framework\DataObject and makes some data preparations
  91. *
  92. * @param string $type Widget type
  93. * @return null|\Magento\Framework\Simplexml\Element
  94. */
  95. public function getConfigAsXml($type)
  96. {
  97. return $this->getXmlElementByType($type);
  98. }
  99. /**
  100. * Return widget XML configuration as \Magento\Framework\DataObject and makes some data preparations
  101. *
  102. * @param string $type Widget type
  103. * @return \Magento\Framework\DataObject
  104. */
  105. public function getConfigAsObject($type)
  106. {
  107. $widget = $this->getWidgetByClassType($type);
  108. $object = new \Magento\Framework\DataObject();
  109. if ($widget === null) {
  110. return $object;
  111. }
  112. $widget = $this->getAsCanonicalArray($widget);
  113. // Save all nodes to object data
  114. $object->setType($type);
  115. $object->setData($widget);
  116. // Correct widget parameters and convert its data to objects
  117. $newParams = $this->prepareWidgetParameters($object);
  118. $object->setData('parameters', $newParams);
  119. return $object;
  120. }
  121. /**
  122. * Prepare widget parameters
  123. *
  124. * @param \Magento\Framework\DataObject $object
  125. * @return array
  126. */
  127. protected function prepareWidgetParameters(\Magento\Framework\DataObject $object)
  128. {
  129. $params = $object->getData('parameters');
  130. $newParams = [];
  131. if (is_array($params)) {
  132. $sortOrder = 0;
  133. foreach ($params as $key => $data) {
  134. if (is_array($data)) {
  135. $data = $this->prepareDropDownValues($data, $key, $sortOrder);
  136. $data = $this->prepareHelperBlock($data);
  137. $newParams[$key] = new \Magento\Framework\DataObject($data);
  138. $sortOrder++;
  139. }
  140. }
  141. }
  142. uasort($newParams, [$this, 'sortParameters']);
  143. return $newParams;
  144. }
  145. /**
  146. * Prepare drop-down values
  147. *
  148. * @param array $data
  149. * @param string $key
  150. * @param int $sortOrder
  151. * @return array
  152. */
  153. protected function prepareDropDownValues(array $data, $key, $sortOrder)
  154. {
  155. $data['key'] = $key;
  156. $data['sort_order'] = isset($data['sort_order']) ? (int)$data['sort_order'] : $sortOrder;
  157. $values = [];
  158. if (isset($data['values']) && is_array($data['values'])) {
  159. foreach ($data['values'] as $value) {
  160. if (isset($value['label']) && isset($value['value'])) {
  161. $values[] = $value;
  162. }
  163. }
  164. }
  165. $data['values'] = $values;
  166. return $data;
  167. }
  168. /**
  169. * Prepare helper block
  170. *
  171. * @param array $data
  172. * @return array
  173. */
  174. protected function prepareHelperBlock(array $data)
  175. {
  176. if (isset($data['helper_block'])) {
  177. $helper = new \Magento\Framework\DataObject();
  178. if (isset($data['helper_block']['data']) && is_array($data['helper_block']['data'])) {
  179. $helper->addData($data['helper_block']['data']);
  180. }
  181. if (isset($data['helper_block']['type'])) {
  182. $helper->setType($data['helper_block']['type']);
  183. }
  184. $data['helper_block'] = $helper;
  185. }
  186. return $data;
  187. }
  188. /**
  189. * Return filtered list of widgets
  190. *
  191. * @param array $filters Key-value array of filters for widget node properties
  192. * @return array
  193. * @api
  194. */
  195. public function getWidgets($filters = [])
  196. {
  197. $widgets = $this->dataStorage->get();
  198. $result = $widgets;
  199. // filter widgets by params
  200. if (is_array($filters) && count($filters) > 0) {
  201. foreach ($widgets as $code => $widget) {
  202. try {
  203. foreach ($filters as $field => $value) {
  204. if (!isset($widget[$field]) || (string)$widget[$field] != $value) {
  205. throw new \Exception();
  206. }
  207. }
  208. } catch (\Exception $e) {
  209. unset($result[$code]);
  210. continue;
  211. }
  212. }
  213. }
  214. return $result;
  215. }
  216. /**
  217. * Return list of widgets as array
  218. *
  219. * @param array $filters Key-value array of filters for widget node properties
  220. * @return array
  221. * @api
  222. */
  223. public function getWidgetsArray($filters = [])
  224. {
  225. if (empty($this->widgetsArray)) {
  226. $result = [];
  227. foreach ($this->getWidgets($filters) as $code => $widget) {
  228. $result[$widget['name']] = [
  229. 'name' => __((string)$widget['name']),
  230. 'code' => $code,
  231. 'type' => $widget['@']['type'],
  232. 'description' => __((string)$widget['description']),
  233. ];
  234. }
  235. usort($result, [$this, "sortWidgets"]);
  236. $this->widgetsArray = $result;
  237. }
  238. return $this->widgetsArray;
  239. }
  240. /**
  241. * Return widget presentation code in WYSIWYG editor
  242. *
  243. * @param string $type Widget Type
  244. * @param array $params Pre-configured Widget Params
  245. * @param bool $asIs Return result as widget directive(true) or as placeholder image(false)
  246. * @return string Widget directive ready to parse
  247. * @api
  248. */
  249. public function getWidgetDeclaration($type, $params = [], $asIs = true)
  250. {
  251. $directive = '{{widget type="' . $type . '"';
  252. foreach ($params as $name => $value) {
  253. // Retrieve default option value if pre-configured
  254. if ($name == 'conditions') {
  255. $name = 'conditions_encoded';
  256. $value = $this->conditionsHelper->encode($value);
  257. } elseif (is_array($value)) {
  258. $value = implode(',', $value);
  259. } elseif (trim($value) == '') {
  260. $widget = $this->getConfigAsObject($type);
  261. $parameters = $widget->getParameters();
  262. if (isset($parameters[$name]) && is_object($parameters[$name])) {
  263. $value = $parameters[$name]->getValue();
  264. }
  265. }
  266. if ($value) {
  267. $directive .= sprintf(' %s="%s"', $name, $value);
  268. }
  269. }
  270. $directive .= '}}';
  271. if ($asIs) {
  272. return $directive;
  273. }
  274. $html = sprintf(
  275. '<img id="%s" src="%s" title="%s">',
  276. $this->idEncode($directive),
  277. $this->getPlaceholderImageUrl($type),
  278. $this->escaper->escapeUrl($directive)
  279. );
  280. return $html;
  281. }
  282. /**
  283. * Get image URL of WYSIWYG placeholder image
  284. *
  285. * @param string $type
  286. * @return string
  287. */
  288. public function getPlaceholderImageUrl($type)
  289. {
  290. $placeholder = false;
  291. $widget = $this->getWidgetByClassType($type);
  292. if (is_array($widget) && isset($widget['placeholder_image'])) {
  293. $placeholder = (string)$widget['placeholder_image'];
  294. }
  295. if ($placeholder) {
  296. $asset = $this->assetRepo->createAsset($placeholder);
  297. $placeholder = $this->assetSource->getFile($asset);
  298. if ($placeholder) {
  299. return $asset->getUrl();
  300. }
  301. }
  302. return $this->assetRepo->getUrl('Magento_Widget::placeholder.gif');
  303. }
  304. /**
  305. * Get a list of URLs of WYSIWYG placeholder images
  306. *
  307. * Returns array(<type> => <url>)
  308. *
  309. * @return array
  310. */
  311. public function getPlaceholderImageUrls()
  312. {
  313. $result = [];
  314. $widgets = $this->getWidgets();
  315. /** @var array $widget */
  316. foreach ($widgets as $widget) {
  317. if (isset($widget['@'])) {
  318. if (isset($widget['@']['type'])) {
  319. $type = $widget['@']['type'];
  320. $result[$type] = $this->getPlaceholderImageUrl($type);
  321. }
  322. }
  323. }
  324. return $result;
  325. }
  326. /**
  327. * Remove attributes from widget array and emulate work of \Magento\Framework\Simplexml\Element::asCanonicalArray
  328. *
  329. * @param array $inputArray
  330. * @return array
  331. */
  332. protected function getAsCanonicalArray($inputArray)
  333. {
  334. if (array_key_exists('@', $inputArray)) {
  335. unset($inputArray['@']);
  336. }
  337. foreach ($inputArray as $key => $value) {
  338. if (!is_array($value)) {
  339. continue;
  340. }
  341. $inputArray[$key] = $this->getAsCanonicalArray($value);
  342. }
  343. return $inputArray;
  344. }
  345. /**
  346. * Encode string to valid HTML id element, based on base64 encoding
  347. *
  348. * @param string $string
  349. * @return string
  350. */
  351. protected function idEncode($string)
  352. {
  353. return strtr(base64_encode($string), '+/=', ':_-');
  354. }
  355. /**
  356. * User-defined widgets sorting by Name
  357. *
  358. * @param array $firstElement
  359. * @param array $secondElement
  360. * @return bool
  361. */
  362. protected function sortWidgets($firstElement, $secondElement)
  363. {
  364. return strcmp($firstElement["name"], $secondElement["name"]);
  365. }
  366. /**
  367. * Widget parameters sort callback
  368. *
  369. * @param \Magento\Framework\DataObject $firstElement
  370. * @param \Magento\Framework\DataObject $secondElement
  371. * @return int
  372. */
  373. protected function sortParameters($firstElement, $secondElement)
  374. {
  375. $aOrder = (int)$firstElement->getData('sort_order');
  376. $bOrder = (int)$secondElement->getData('sort_order');
  377. return $aOrder < $bOrder ? -1 : ($aOrder > $bOrder ? 1 : 0);
  378. }
  379. }