]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Features/HasEvent.php
Disable events from mass changes; closes #1733
[galette.git] / galette / lib / Galette / Features / HasEvent.php
1 <?php
2
3 /**
4 * Has events trait
5 *
6 * PHP version 5
7 *
8 * Copyright © 2024 The Galette Team
9 *
10 * This file is part of Galette (http://galette.tuxfamily.org).
11 *
12 * Galette is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * Galette is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * @category Features
26 * @package Galette
27 *
28 * @author Johan Cwiklinski <johan@x-tnd.be>
29 * @copyright 2024 The Galette Team
30 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
31 * @link https://galette.eu
32 */
33
34 namespace Galette\Features;
35
36 /**
37 * Has events trait
38 *
39 * @category Features
40 * @name HasEvents
41 * @package Galette
42 * @author Johan Cwiklinski <johan@x-tnd.be>
43 * @copyright 2024 The Galette Team
44 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
45 * @link https://galette.eu
46 */
47
48 trait HasEvent
49 {
50 private bool $has_add_event = false;
51 private bool $has_edit_event = false;
52 private bool $has_delete_event = false;
53 protected bool $events_active = true;
54
55 /**
56 * Get prefix for events
57 *
58 * @return string
59 */
60 abstract protected function getEventsPrefix(): string;
61
62 /**
63 * Activate events
64 *
65 * @return self
66 */
67 public function activateEvents(): self
68 {
69 $this->events_active = true;
70 return $this;
71 }
72
73 /**
74 * Disable events
75 *
76 * @return self
77 */
78 public function disableEvents(): self
79 {
80 $this->events_active = false;
81 return $this;
82 }
83
84 /**
85 * Are events enabled
86 *
87 * @return bool
88 */
89 public function areEventsEnabled(): bool
90 {
91 return $this->events_active;
92 }
93
94 /**
95 * Activate add event
96 *
97 * @return self
98 */
99 public function withAddEvent(): self
100 {
101 $this->has_add_event = true;
102 return $this;
103 }
104
105 /**
106 * Disable add event
107 *
108 * @return self
109 */
110 public function withoutAddEvent(): self
111 {
112 $this->has_add_event = false;
113 return $this;
114 }
115
116 /**
117 * Get add event name
118 *
119 * @return ?string
120 */
121 public function getAddEventName(): ?string
122 {
123 if (!$this->hasAddEvent()) {
124 return null;
125 }
126 return sprintf(
127 '%1$s.add',
128 $this->getEventsPrefix()
129 );
130 }
131
132 /**
133 * Has add event
134 *
135 * @return bool
136 */
137 public function hasAddEvent(): bool
138 {
139 return $this->areEventsEnabled() && $this->has_add_event;
140 }
141
142 /**
143 * Activate edit event
144 *
145 * @return self
146 */
147 public function withEditEvent(): self
148 {
149 $this->has_edit_event = true;
150 return $this;
151 }
152
153 /**
154 * Disable edit event
155 *
156 * @return self
157 */
158 public function withoutEditEvent(): self
159 {
160 $this->has_edit_event = false;
161 return $this;
162 }
163
164 /**
165 * Get edit event name
166 *
167 * @return ?string
168 */
169 public function getEditEventName(): ?string
170 {
171 if (!$this->hasEditEvent()) {
172 return null;
173 }
174 return sprintf(
175 '%1$s.edit',
176 $this->getEventsPrefix()
177 );
178 }
179
180 /**
181 * Has edit event
182 *
183 * @return bool
184 */
185 public function hasEditEvent(): bool
186 {
187 return $this->areEventsEnabled() && $this->has_edit_event;
188 }
189
190 /**
191 * Activate add event
192 *
193 * @return self
194 */
195 public function withDeleteEvent(): self
196 {
197 $this->has_delete_event = true;
198 return $this;
199 }
200
201 /**
202 * Disable delete event
203 *
204 * @return self
205 */
206 public function withoutDeleteEvent(): self
207 {
208 $this->has_delete_event = false;
209 return $this;
210 }
211
212 /**
213 * Get edit event name
214 *
215 * @return ?string
216 */
217 public function getDeleteEventName(): ?string
218 {
219 if (!$this->hasDeleteEvent()) {
220 return null;
221 }
222 return sprintf(
223 '%1$s.delete',
224 $this->getEventsPrefix()
225 );
226 }
227
228 /**
229 * Has delete event
230 *
231 * @return bool
232 */
233 public function hasDeleteEvent(): bool
234 {
235 return $this->areEventsEnabled() && $this->has_delete_event;
236 }
237 }