PageRenderTime 28ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/bundles/DashboardBundle/Model/DashboardModel.php

https://gitlab.com/mautic-master/mautic
PHP | 272 lines | 140 code | 35 blank | 97 comment | 13 complexity | 44bde7fc18c440a315fa80f59a3e53c0 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\Model;
  10. use Mautic\CoreBundle\Helper\CoreParametersHelper;
  11. use Mautic\CoreBundle\Helper\PathsHelper;
  12. use Mautic\CoreBundle\Model\FormModel;
  13. use Symfony\Component\HttpFoundation\Session\Session;
  14. use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
  15. use Mautic\DashboardBundle\Entity\Widget;
  16. use Mautic\DashboardBundle\Event\WidgetDetailEvent;
  17. use Mautic\DashboardBundle\DashboardEvents;
  18. use Mautic\CoreBundle\Entity\CommonRepository;
  19. use Mautic\CoreBundle\Helper\CacheStorageHelper;
  20. /**
  21. * Class DashboardModel
  22. */
  23. class DashboardModel extends FormModel
  24. {
  25. /**
  26. * @var Session
  27. */
  28. protected $session;
  29. /**
  30. * @var CoreParametersHelper
  31. */
  32. protected $coreParametersHelper;
  33. /**
  34. * @var PathsHelper
  35. */
  36. protected $pathsHelper;
  37. /**
  38. * DashboardModel constructor.
  39. *
  40. * @param CoreParametersHelper $coreParametersHelper
  41. * @param PathsHelper $pathsHelper
  42. */
  43. public function __construct(CoreParametersHelper $coreParametersHelper, PathsHelper $pathsHelper)
  44. {
  45. $this->coreParametersHelper = $coreParametersHelper;
  46. $this->pathsHelper = $pathsHelper;
  47. }
  48. /**
  49. * @param Session $session
  50. */
  51. public function setSession(Session $session)
  52. {
  53. $this->session = $session;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. *
  58. * @return string
  59. */
  60. public function getRepository()
  61. {
  62. return $this->em->getRepository('MauticDashboardBundle:Widget');
  63. }
  64. /**
  65. * {@inheritdoc}
  66. *
  67. * @return string
  68. */
  69. public function getPermissionBase()
  70. {
  71. return 'dashboard:widgets';
  72. }
  73. /**
  74. * Get a specific entity or generate a new one if id is empty
  75. *
  76. * @param $id
  77. * @return null|object
  78. */
  79. public function getEntity($id = null)
  80. {
  81. if ($id === null) {
  82. return new Widget();
  83. }
  84. $entity = parent::getEntity($id);
  85. return $entity;
  86. }
  87. /**
  88. * Load widgets for the current user from database
  89. *
  90. * @return array
  91. */
  92. public function getWidgets()
  93. {
  94. $widgets = $this->getEntities(array(
  95. 'orderBy' => 'w.ordering',
  96. 'filter' => array(
  97. 'force' => array(
  98. array(
  99. 'column' => 'w.createdBy',
  100. 'expr' => 'eq',
  101. 'value' => $this->user->getId()
  102. )
  103. )
  104. )
  105. ));
  106. return $widgets;
  107. }
  108. /**
  109. * Fill widgets with their content
  110. *
  111. * @param array $widgets
  112. * @param array $filter
  113. */
  114. public function populateWidgetsContent(&$widgets, $filter = array())
  115. {
  116. if (count($widgets)) {
  117. foreach ($widgets as &$widget) {
  118. if (!($widget instanceof Widget)) {
  119. $widget = $this->populateWidgetEntity($widget);
  120. }
  121. $this->populateWidgetContent($widget, $filter);
  122. }
  123. }
  124. }
  125. /**
  126. * Creates a new Widget object from an array data
  127. *
  128. * @param array $data
  129. *
  130. * @return Widget
  131. */
  132. public function populateWidgetEntity($data)
  133. {
  134. $entity = new Widget;
  135. foreach ($data as $property => $value) {
  136. $method = "set".ucfirst($property);
  137. if (method_exists($entity, $method)) {
  138. $entity->$method($value);
  139. }
  140. unset($data[$property]);
  141. }
  142. return $entity;
  143. }
  144. /**
  145. * Load widget content from the onWidgetDetailGenerate event
  146. *
  147. * @param Widget $widget
  148. * @param array $filter
  149. */
  150. public function populateWidgetContent(Widget &$widget, $filter = array())
  151. {
  152. $cacheDir = $this->coreParametersHelper->getParameter('cached_data_dir', $this->pathsHelper->getSystemPath('cache', true));
  153. if ($widget->getCacheTimeout() == null || $widget->getCacheTimeout() == -1) {
  154. $widget->setCacheTimeout($this->coreParametersHelper->getParameter('cached_data_timeout'));
  155. }
  156. // Merge global filter with widget params
  157. $widgetParams = $widget->getParams();
  158. $resultParams = array_merge($widgetParams, $filter);
  159. // Add the user timezone
  160. if (empty($resultParams['timezone'])) {
  161. $resultParams['timezone'] = $this->user->getTimezone();
  162. }
  163. // Clone the objects in param array to avoid reference issues if some subscriber changes them
  164. foreach ($resultParams as &$param) {
  165. if (is_object($param)) {
  166. $param = clone $param;
  167. }
  168. }
  169. $widget->setParams($resultParams);
  170. $event = new WidgetDetailEvent($this->translator);
  171. $event->setWidget($widget);
  172. $event->setCacheDir($cacheDir, $this->user->getId());
  173. $event->setSecurity($this->security);
  174. $this->dispatcher->dispatch(DashboardEvents::DASHBOARD_ON_MODULE_DETAIL_GENERATE, $event);
  175. }
  176. /**
  177. * Clears the temporary widget cache
  178. */
  179. public function clearDashboardCache()
  180. {
  181. $cacheDir = $this->coreParametersHelper->getParameter('cached_data_dir', $this->pathsHelper->getSystemPath('cache', true));
  182. $cacheStorage = new CacheStorageHelper($cacheDir, $this->user->getId());
  183. $cacheStorage->clear();
  184. }
  185. /**
  186. * {@inheritdoc}
  187. *
  188. * @param Widget $entity
  189. * @param \Symfony\Component\Form\FormFactory $formFactory
  190. * @param string|null $action
  191. * @param array $options
  192. *
  193. * @return \Symfony\Component\Form\Form
  194. * @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
  195. */
  196. public function createForm($entity, $formFactory, $action = null, $options = array())
  197. {
  198. if (!$entity instanceof Widget) {
  199. throw new MethodNotAllowedHttpException(array('Widget'), 'Entity must be of class Widget()');
  200. }
  201. if (!empty($action)) {
  202. $options['action'] = $action;
  203. }
  204. return $formFactory->create('widget', $entity, $options);
  205. }
  206. /**
  207. * Create/edit entity
  208. *
  209. * @param object $entity
  210. * @param bool $unlock
  211. */
  212. public function saveEntity($entity, $unlock = true)
  213. {
  214. // Set widget name from widget type if empty
  215. if (!$entity->getName()) {
  216. $entity->setName($this->translator->trans('mautic.widget.' . $entity->getType()));
  217. }
  218. parent::saveEntity($entity, $unlock);
  219. }
  220. /**
  221. * Generate default date range filter and time unit
  222. *
  223. * @return array
  224. */
  225. public function getDefaultFilter()
  226. {
  227. $lastMonth = new \DateTime();
  228. $lastMonth->sub(new \DateInterval('P30D'));
  229. $today = new \DateTime();
  230. $dateFrom = new \DateTime($this->session->get('mautic.dashboard.date.from', $lastMonth->format('Y-m-d 00:00:00')));
  231. $dateTo = new \DateTime($this->session->get('mautic.dashboard.date.to', $today->format('Y-m-d H:i:s')));
  232. return array(
  233. 'dateFrom' => $dateFrom,
  234. 'dateTo' => $dateTo,
  235. );
  236. }
  237. }