1 | <?php |
||
2 | |||
3 | /* |
||
4 | * @copyright 2016 Mautic, Inc. All rights reserved |
||
5 | * @author Mautic, Inc |
||
6 | * |
||
7 | * @link https://mautic.org |
||
8 | * |
||
9 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||
10 | */ |
||
11 | |||
12 | namespace MauticPlugin\MauticSocialBundle\Model; |
||
13 | |||
14 | use Mautic\CoreBundle\Model\FormModel; |
||
15 | use MauticPlugin\MauticSocialBundle\Entity\Monitoring; |
||
16 | use MauticPlugin\MauticSocialBundle\Event as Events; |
||
17 | use MauticPlugin\MauticSocialBundle\Form\Type\MonitoringType; |
||
18 | use MauticPlugin\MauticSocialBundle\Form\Type\TwitterHashtagType; |
||
19 | use MauticPlugin\MauticSocialBundle\Form\Type\TwitterMentionType; |
||
20 | use MauticPlugin\MauticSocialBundle\SocialEvents; |
||
21 | use Symfony\Component\EventDispatcher\Event; |
||
22 | use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; |
||
23 | |||
24 | /** |
||
25 | * Class MonitoringModel |
||
26 | * {@inheritdoc} |
||
27 | */ |
||
28 | class MonitoringModel extends FormModel |
||
29 | { |
||
30 | private $networkTypes = [ |
||
31 | 'twitter_handle' => [ |
||
32 | 'label' => 'mautic.social.monitoring.type.list.twitter.handle', |
||
33 | 'form' => TwitterMentionType::class, |
||
34 | ], |
||
35 | 'twitter_hashtag' => [ |
||
36 | 'label' => 'mautic.social.monitoring.type.list.twitter.hashtag', |
||
37 | 'form' => TwitterHashtagType::class, |
||
38 | ], |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | * |
||
44 | * @param $entity |
||
45 | * @param $formFactory |
||
46 | * @param null $action |
||
47 | * @param array $options |
||
48 | * |
||
49 | * @return mixed |
||
50 | * |
||
51 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
52 | */ |
||
53 | public function createForm($entity, $formFactory, $action = null, $params = []) |
||
54 | { |
||
55 | if (!$entity instanceof Monitoring) { |
||
56 | throw new MethodNotAllowedHttpException(['Monitoring']); |
||
57 | } |
||
58 | |||
59 | if (!empty($action)) { |
||
60 | $params['action'] = $action; |
||
61 | } |
||
62 | |||
63 | return $formFactory->create(MonitoringType::class, $entity, $params); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Get a specific entity or generate a new one if id is empty. |
||
68 | * |
||
69 | * @param $id |
||
70 | * |
||
71 | * @return Monitoring|null |
||
72 | */ |
||
73 | public function getEntity($id = null) |
||
74 | { |
||
75 | return $id ? parent::getEntity($id) : new Monitoring(); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | * |
||
81 | * @param $action |
||
82 | * @param $event |
||
83 | * @param $entity |
||
84 | * @param $isNew |
||
85 | * |
||
86 | * @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException |
||
87 | */ |
||
88 | protected function dispatchEvent($action, &$entity, $isNew = false, Event $event = null) |
||
89 | { |
||
90 | if (!$entity instanceof Monitoring) { |
||
91 | throw new MethodNotAllowedHttpException(['Monitoring']); |
||
92 | } |
||
93 | |||
94 | switch ($action) { |
||
95 | case 'pre_save': |
||
96 | $name = SocialEvents::MONITOR_PRE_SAVE; |
||
97 | break; |
||
98 | case 'post_save': |
||
99 | $name = SocialEvents::MONITOR_POST_SAVE; |
||
100 | break; |
||
101 | case 'pre_delete': |
||
102 | $name = SocialEvents::MONITOR_PRE_DELETE; |
||
103 | break; |
||
104 | case 'post_delete': |
||
105 | $name = SocialEvents::MONITOR_POST_DELETE; |
||
106 | break; |
||
107 | default: |
||
108 | return null; |
||
109 | } |
||
110 | |||
111 | if ($this->dispatcher->hasListeners($name)) { |
||
112 | if (empty($event)) { |
||
113 | $event = new Events\SocialEvent($entity, $isNew); |
||
114 | } |
||
115 | |||
116 | $this->dispatcher->dispatch($name, $event); |
||
117 | |||
118 | return $event; |
||
119 | } else { |
||
120 | return null; |
||
121 | } |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | * |
||
127 | * @var \MauticPlugin\MauticSocialBundle\Entity\Monitoring |
||
128 | */ |
||
129 | public function saveEntity($monitoringEntity, $unlock = true) |
||
130 | { |
||
131 | // we're editing an existing record |
||
132 | if (!$monitoringEntity->isNew()) { |
||
133 | //increase the revision |
||
134 | $revision = $monitoringEntity->getRevision(); |
||
135 | ++$revision; |
||
136 | $monitoringEntity->setRevision($revision); |
||
137 | } // is new |
||
138 | else { |
||
139 | $now = new \DateTime(); |
||
140 | $monitoringEntity->setDateAdded($now); |
||
141 | } |
||
142 | |||
143 | parent::saveEntity($monitoringEntity, $unlock); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | public function getRepository() |
||
150 | { |
||
151 | return $this->em->getRepository('MauticSocialBundle:Monitoring'); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @return string |
||
156 | */ |
||
157 | public function getPermissionBase() |
||
158 | { |
||
159 | return 'mauticSocial:monitoring'; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @return array |
||
164 | */ |
||
165 | public function getNetworkTypes() |
||
166 | { |
||
167 | $types = []; |
||
168 | foreach ($this->networkTypes as $type => $data) { |
||
169 | $types[$type] = $data['label']; |
||
170 | } |
||
171 | |||
172 | return $types; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @param string $type |
||
177 | * |
||
178 | * @return |null |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
179 | */ |
||
180 | public function getFormByType($type) |
||
181 | { |
||
182 | return array_key_exists($type, $this->networkTypes) ? $this->networkTypes[$type]['form'] : null; |
||
183 | } |
||
184 | } |
||
185 |