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

/app/bundles/DashboardBundle/Event/WidgetDetailEvent.php

https://gitlab.com/mautic-master/mautic
PHP | 299 lines | 149 code | 37 blank | 113 comment | 12 complexity | 4d8954a71c5b5aa9f213cf8ea2f5aca6 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Mautic
  4. * @copyright 2014 Mautic Contributors. All rights reserved.
  5. * @author Mautic
  6. * @link http://mautic.org
  7. * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
  8. */
  9. namespace Mautic\DashboardBundle\Event;
  10. use Mautic\CoreBundle\Event\CommonEvent;
  11. use Mautic\DashboardBundle\Entity\Widget;
  12. use Mautic\CoreBundle\Helper\CacheStorageHelper;
  13. use Mautic\CoreBundle\Security\Permissions\CorePermissions;
  14. use Symfony\Component\Translation\TranslatorInterface;
  15. /**
  16. * Class WidgetDetailEvent
  17. *
  18. * @package Mautic\DashboardBundle\Event
  19. */
  20. class WidgetDetailEvent extends CommonEvent
  21. {
  22. protected $widget;
  23. protected $type;
  24. protected $template;
  25. protected $templateData = array();
  26. protected $errorMessage;
  27. protected $uniqueId;
  28. protected $cacheDir;
  29. protected $uniqueCacheDir;
  30. protected $cacheTimeout;
  31. protected $startTime = 0;
  32. protected $loadTime = 0;
  33. protected $translator;
  34. /**
  35. * @var CorePermissions $security
  36. */
  37. protected $security = null;
  38. public function __construct(TranslatorInterface $translator)
  39. {
  40. $this->translator = $translator;
  41. $this->startTime = microtime();
  42. }
  43. /**
  44. * Set the cache dir
  45. *
  46. * @param string $cacheDir
  47. */
  48. public function setCacheDir($cacheDir, $uniqueCacheDir = null)
  49. {
  50. $this->cacheDir = $cacheDir;
  51. $this->uniqueCacheDir = $uniqueCacheDir;
  52. }
  53. /**
  54. * Set the cache timeout
  55. *
  56. * @param string $cacheTimeout
  57. */
  58. public function setCacheTimeout($cacheTimeout)
  59. {
  60. $this->cacheTimeout = (int) $cacheTimeout;
  61. }
  62. /**
  63. * Set the widget type
  64. *
  65. * @param string $type
  66. */
  67. public function setType($type)
  68. {
  69. $this->type = $type;
  70. }
  71. /**
  72. * Get the widget type
  73. *
  74. * @return string $type
  75. */
  76. public function getType()
  77. {
  78. return $this->type;
  79. }
  80. /**
  81. * Set the widget entity
  82. *
  83. * @param Widget $widget
  84. */
  85. public function setWidget(Widget $widget)
  86. {
  87. $this->widget = $widget;
  88. $params = $widget->getParams();
  89. // Set required params if undefined
  90. if (!isset($params['timeUnit'])) {
  91. $params['timeUnit'] = null;
  92. }
  93. if (!isset($params['amount'])) {
  94. $params['amount'] = null;
  95. }
  96. if (!isset($params['dateFormat'])) {
  97. $params['dateFormat'] = null;
  98. }
  99. if (!isset($params['filter'])) {
  100. $params['filter'] = array();
  101. }
  102. $widget->setParams($params);
  103. $this->setType($widget->getType());
  104. $this->setCacheTimeout($widget->getCacheTimeout());
  105. }
  106. /**
  107. * Returns the widget entity
  108. *
  109. * @param Widget $widget
  110. */
  111. public function getWidget()
  112. {
  113. return $this->widget;
  114. }
  115. /**
  116. * Set the widget template
  117. *
  118. * @param string $template
  119. */
  120. public function setTemplate($template)
  121. {
  122. $this->template = $template;
  123. $this->widget->setTemplate($template);
  124. }
  125. /**
  126. * Get the widget template
  127. *
  128. * @return string $template
  129. */
  130. public function getTemplate()
  131. {
  132. return $this->template;
  133. }
  134. /**
  135. * Set the widget template data
  136. *
  137. * @param array $templateData
  138. */
  139. public function setTemplateData(array $templateData, $skipCache = false)
  140. {
  141. $this->templateData = $templateData;
  142. $this->widget->setTemplateData($templateData);
  143. $this->widget->setLoadTime(abs(microtime() - $this->startTime));
  144. // Store the template data to the cache
  145. if (!$skipCache && $this->cacheDir && $this->widget->getCacheTimeout() > 0) {
  146. $cache = new CacheStorageHelper($this->cacheDir, $this->uniqueCacheDir);
  147. $cache->set($this->getUniqueWidgetId(), $templateData);
  148. }
  149. }
  150. /**
  151. * Get the widget template data
  152. *
  153. * @return string $templateData
  154. */
  155. public function getTemplateData()
  156. {
  157. return $this->templateData;
  158. }
  159. /**
  160. * Set en error message
  161. *
  162. * @param array $errorMessage
  163. */
  164. public function setErrorMessage($errorMessage)
  165. {
  166. $this->errorMessage = $errorMessage;
  167. $this->widget->setErrorMessage($errorMessage);
  168. }
  169. /**
  170. * Get an error message
  171. *
  172. * @return string $errorMessage
  173. */
  174. public function getErrorMessage()
  175. {
  176. return $this->errorMessage;
  177. }
  178. /**
  179. * Build a unique ID from type and widget params
  180. *
  181. * @return string
  182. */
  183. public function getUniqueWidgetId()
  184. {
  185. if ($this->uniqueId) {
  186. return $this->uniqueId;
  187. }
  188. $params = $this->getWidget()->getParams();
  189. // Unset dateFrom and dateTo since they constantly change
  190. unset($params['dateFrom'], $params['dateTo']);
  191. $uniqueSettings = array(
  192. 'params' => $params,
  193. 'width' => $this->getWidget()->getWidth(),
  194. 'height' => $this->getWidget()->getHeight(),
  195. 'locale' => $this->translator->getLocale()
  196. );
  197. return $this->uniqueId = $this->getType().'_'.substr(md5(json_encode($uniqueSettings)), 0, 16);
  198. }
  199. /**
  200. * Checks the cache for the widget data.
  201. * If cache exists, it sets the TemplateData.
  202. *
  203. * @return string
  204. */
  205. public function isCached()
  206. {
  207. if (!$this->cacheDir) {
  208. return false;
  209. }
  210. $cache = new CacheStorageHelper($this->cacheDir, $this->uniqueCacheDir);
  211. $data = $cache->get($this->getUniqueWidgetId(), $this->cacheTimeout);
  212. if ($data) {
  213. $this->widget->setCached(true);
  214. $this->setTemplateData($data, true);
  215. return true;
  216. }
  217. return false;
  218. }
  219. /**
  220. * Get the Translator object
  221. *
  222. * @return Translator $translator
  223. */
  224. public function getTranslator()
  225. {
  226. return $this->translator;
  227. }
  228. /**
  229. * Set security object to check the perimissions
  230. *
  231. * @param CorePermissions $security
  232. */
  233. public function setSecurity(CorePermissions $security)
  234. {
  235. $this->security = $security;
  236. }
  237. /**
  238. * Check if the user has at least one permission of defined array of permissions
  239. *
  240. * @param array $permissions
  241. *
  242. * @return boolean
  243. */
  244. public function hasPermissions(array $permissions)
  245. {
  246. if (!$this->security) return true;
  247. $perm = $this->security->isGranted($permissions, "RETURN_ARRAY");
  248. return in_array(true, $perm);
  249. }
  250. /**
  251. * Check if the user has defined permission to see the widgets
  252. *
  253. * @param string $permission
  254. *
  255. * @return boolean
  256. */
  257. public function hasPermission($permission)
  258. {
  259. if (!$this->security) return true;
  260. return $this->security->isGranted($permission);
  261. }
  262. }