1 | <?php |
||
2 | |||
3 | /* |
||
4 | * @copyright 2014 Mautic Contributors. All rights reserved |
||
5 | * @author Mautic |
||
6 | * |
||
7 | * @link http://mautic.org |
||
8 | * |
||
9 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||
10 | */ |
||
11 | |||
12 | namespace Mautic\StageBundle\Model; |
||
13 | |||
14 | use Mautic\CoreBundle\Helper\Chart\ChartQuery; |
||
15 | use Mautic\CoreBundle\Helper\Chart\LineChart; |
||
16 | use Mautic\CoreBundle\Helper\UserHelper; |
||
17 | use Mautic\CoreBundle\Model\FormModel as CommonFormModel; |
||
18 | use Mautic\LeadBundle\Model\LeadModel; |
||
19 | use Mautic\StageBundle\Entity\Stage; |
||
20 | use Mautic\StageBundle\Event\StageBuilderEvent; |
||
21 | use Mautic\StageBundle\Event\StageEvent; |
||
22 | use Mautic\StageBundle\Form\Type\StageType; |
||
23 | use Mautic\StageBundle\StageEvents; |
||
24 | use Symfony\Component\EventDispatcher\Event; |
||
25 | use Symfony\Component\HttpFoundation\Session\Session; |
||
26 | use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; |
||
27 | |||
28 | /** |
||
29 | * Class StageModel. |
||
30 | */ |
||
31 | class StageModel extends CommonFormModel |
||
32 | { |
||
33 | /** |
||
34 | * @var Session |
||
35 | */ |
||
36 | protected $session; |
||
37 | |||
38 | /** |
||
39 | * @var LeadModel |
||
40 | */ |
||
41 | protected $leadModel; |
||
42 | |||
43 | /** |
||
44 | * @var UserHelper |
||
45 | */ |
||
46 | protected $userHelper; |
||
47 | |||
48 | public function __construct(LeadModel $leadModel, Session $session, UserHelper $userHelper) |
||
49 | { |
||
50 | $this->session = $session; |
||
51 | $this->leadModel = $leadModel; |
||
52 | $this->userHelper = $userHelper; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | * |
||
58 | * @return \Mautic\StageBundle\Entity\StageRepository |
||
59 | */ |
||
60 | public function getRepository() |
||
61 | { |
||
62 | return $this->em->getRepository('MauticStageBundle:Stage'); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public function getPermissionBase() |
||
69 | { |
||
70 | return 'stage:stages'; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | * |
||
76 | * @throws MethodNotAllowedHttpException |
||
77 | */ |
||
78 | public function createForm($entity, $formFactory, $action = null, $options = []) |
||
79 | { |
||
80 | if (!$entity instanceof Stage) { |
||
81 | throw new MethodNotAllowedHttpException(['Stage']); |
||
82 | } |
||
83 | if (!empty($action)) { |
||
84 | $options['action'] = $action; |
||
85 | } |
||
86 | |||
87 | return $formFactory->create(StageType::class, $entity, $options); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | * |
||
93 | * @return Stage|null |
||
94 | */ |
||
95 | public function getEntity($id = null) |
||
96 | { |
||
97 | if (null === $id) { |
||
98 | return new Stage(); |
||
99 | } |
||
100 | |||
101 | return parent::getEntity($id); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | * |
||
107 | * @throws MethodNotAllowedHttpException |
||
108 | */ |
||
109 | protected function dispatchEvent($action, &$entity, $isNew = false, Event $event = null) |
||
110 | { |
||
111 | if (!$entity instanceof Stage) { |
||
112 | throw new MethodNotAllowedHttpException(['Stage']); |
||
113 | } |
||
114 | |||
115 | switch ($action) { |
||
116 | case 'pre_save': |
||
117 | $name = StageEvents::STAGE_PRE_SAVE; |
||
118 | break; |
||
119 | case 'post_save': |
||
120 | $name = StageEvents::STAGE_POST_SAVE; |
||
121 | break; |
||
122 | case 'pre_delete': |
||
123 | $name = StageEvents::STAGE_PRE_DELETE; |
||
124 | break; |
||
125 | case 'post_delete': |
||
126 | $name = StageEvents::STAGE_POST_DELETE; |
||
127 | break; |
||
128 | default: |
||
129 | return null; |
||
130 | } |
||
131 | |||
132 | if ($this->dispatcher->hasListeners($name)) { |
||
133 | if (empty($event)) { |
||
134 | $event = new StageEvent($entity, $isNew); |
||
135 | $event->setEntityManager($this->em); |
||
136 | } |
||
137 | |||
138 | $this->dispatcher->dispatch($name, $event); |
||
139 | |||
140 | return $event; |
||
141 | } |
||
142 | |||
143 | return null; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Gets array of custom actions from bundles subscribed StageEvents::STAGE_ON_BUILD. |
||
148 | * |
||
149 | * @return mixed |
||
150 | */ |
||
151 | public function getStageActions() |
||
152 | { |
||
153 | static $actions; |
||
154 | |||
155 | if (empty($actions)) { |
||
156 | //build them |
||
157 | $actions = []; |
||
158 | $event = new StageBuilderEvent($this->translator); |
||
159 | $this->dispatcher->dispatch(StageEvents::STAGE_ON_BUILD, $event); |
||
160 | $actions['actions'] = $event->getActions(); |
||
161 | $actions['list'] = $event->getActionList(); |
||
162 | $actions['choices'] = $event->getActionChoices(); |
||
163 | } |
||
164 | |||
165 | return $actions; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Get line chart data of stages. |
||
170 | * |
||
171 | * @param char $unit {@link php.net/manual/en/function.date.php#refsect1-function.date-parameters} |
||
172 | * @param DateTime $dateFrom |
||
173 | * @param DateTime $dateTo |
||
174 | * @param string $dateFormat |
||
175 | * @param array $filter |
||
176 | * @param bool $canViewOthers |
||
177 | * |
||
178 | * @return array |
||
179 | */ |
||
180 | public function getStageLineChartData($unit, \DateTime $dateFrom, \DateTime $dateTo, $dateFormat = null, $filter = [], $canViewOthers = true) |
||
181 | { |
||
182 | $chart = new LineChart($unit, $dateFrom, $dateTo, $dateFormat); |
||
183 | $query = new ChartQuery($this->em->getConnection(), $dateFrom, $dateTo); |
||
184 | $q = $query->prepareTimeDataQuery('lead_stages_change_log', 'date_added', $filter); |
||
185 | |||
186 | if (!$canViewOthers) { |
||
187 | $q->join('t', MAUTIC_TABLE_PREFIX.'leads', 'l', 'l.id = t.lead_id') |
||
188 | ->andWhere('l.owner_id = :userId') |
||
189 | ->setParameter('userId', $this->userHelper->getUser()->getId()); |
||
190 | } |
||
191 | |||
192 | $data = $query->loadAndBuildTimeData($q); |
||
193 | $chart->setDataset($this->translator->trans('mautic.stage.changes'), $data); |
||
194 | |||
195 | return $chart->render(); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @return array |
||
200 | */ |
||
201 | public function getUserStages() |
||
202 | { |
||
203 | $user = (!$this->security->isGranted('stage:stages:viewother')) ? |
||
204 | $this->userHelper->getUser() : false; |
||
205 | |||
206 | return $this->getRepository()->getStages($user); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
207 | } |
||
208 | } |
||
209 |