Total Complexity | 60 |
Total Lines | 507 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like EventHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EventHandler, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
34 | class EventHandler extends \XoopsPersistableObjectHandler |
||
35 | { |
||
36 | /** |
||
37 | * Constructor |
||
38 | * |
||
39 | * @param \XoopsDatabase $db |
||
40 | */ |
||
41 | public function __construct(\XoopsDatabase $db) |
||
42 | { |
||
43 | parent::__construct($db, 'wgevents_event', Event::class, 'id', 'name'); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param bool $isNew |
||
48 | * |
||
49 | * @return object |
||
50 | */ |
||
51 | public function create($isNew = true) |
||
52 | { |
||
53 | return parent::create($isNew); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * retrieve a field |
||
58 | * |
||
59 | * @param int $id field id |
||
60 | * @param $fields |
||
61 | * @return \XoopsObject|null reference to the {@link Get} object |
||
62 | */ |
||
63 | public function get($id = null, $fields = null) |
||
64 | { |
||
65 | return parent::get($id, $fields); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * get inserted id |
||
70 | * |
||
71 | * @return int reference to the {@link Get} object |
||
72 | */ |
||
73 | public function getInsertId() |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Get Count Event in the database |
||
80 | * @param int $start |
||
81 | * @param int $limit |
||
82 | * @param string $sort |
||
83 | * @param string $order |
||
84 | * @return int |
||
85 | */ |
||
86 | public function getCountEvents($start = 0, $limit = 0, $sort = 'id', $order = 'DESC') |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Get All Event in the database |
||
95 | * @param int $start |
||
96 | * @param int $limit |
||
97 | * @param string $sort |
||
98 | * @param string $order |
||
99 | * @return array |
||
100 | */ |
||
101 | public function getAllEvents($start = 0, $limit = 0, $sort = 'id', $order = 'DESC') |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Get Criteria Event |
||
110 | * @param $crEvent |
||
111 | * @param int $start |
||
112 | * @param int $limit |
||
113 | * @param string $sort |
||
114 | * @param string $order |
||
115 | * @return int |
||
116 | */ |
||
117 | private function getEventsCriteria($crEvent, int $start, int $limit, string $sort, string $order) |
||
118 | { |
||
119 | $crEvent->setStart($start); |
||
120 | $crEvent->setLimit($limit); |
||
121 | $crEvent->setSort($sort); |
||
122 | $crEvent->setOrder($order); |
||
123 | return $crEvent; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @public function getForm |
||
128 | * @param bool $action |
||
129 | * @return \XoopsThemeForm |
||
130 | */ |
||
131 | public function getFormEventSelect($action = false) |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * TODO: not in use currently |
||
156 | * @public function getForm |
||
157 | * @param array $params |
||
158 | * @param string $action |
||
159 | * @return Forms\FormInline |
||
160 | */ |
||
161 | public function getFormPageNavCounter($params = [], $action = '') |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @public function to get events for given params |
||
194 | * |
||
195 | * @param int $start |
||
196 | * @param int $limit |
||
197 | * @param int $dateFrom // filter date created from (timestamp) |
||
198 | * @param int $dateTo // filter date created to (timestamp) |
||
199 | * @param string $sortBy |
||
200 | * @param string $orderBy |
||
201 | * @param string $op |
||
202 | * @param int $evId |
||
203 | * @param string $filter |
||
204 | * @param array $filterCats |
||
205 | * @param int $dateCreated |
||
206 | * @return array |
||
207 | */ |
||
208 | public function getEvents($start = 0, $limit = 0, $dateFrom = 0, $dateTo = 0, $sortBy = 'datefrom', $orderBy = 'ASC', $op = 'list', $evId = 0, $filter = '', $filterCats = [], $dateCreated = 0) |
||
209 | { |
||
210 | $helper = Helper::getInstance(); |
||
211 | |||
212 | /* |
||
213 | echo '<br>start:'.$start; |
||
214 | echo '<br>limit:'.$limit; |
||
215 | echo '<br>datefrom:'.\formatTimestamp($dateFrom, 'm').'('.$dateFrom.')'; |
||
216 | echo '<br>dateto:'.\formatTimestamp($dateTo, 'm').'('.$dateTo.')'; |
||
217 | echo '<br>sortBy:'.$sortBy; |
||
218 | echo '<br>orderBy:'.$orderBy; |
||
219 | echo '<br>op:'.$op; |
||
220 | echo '<br>evId:'.$evId; |
||
221 | echo '<br>filter:'.$filter; |
||
222 | foreach ($filterCats as $filterCat) { |
||
223 | echo '<br>filterCat:'.$filterCat; |
||
224 | } |
||
225 | */ |
||
226 | |||
227 | $showItem = ($evId > 0); |
||
228 | $uidCurrent = 0; |
||
229 | $userIsAdmin = false; |
||
230 | if (\is_object($GLOBALS['xoopsUser'])) { |
||
231 | $uidCurrent = $GLOBALS['xoopsUser']->uid(); |
||
232 | $userIsAdmin = $GLOBALS['xoopsUser']->isAdmin(); |
||
233 | } |
||
234 | $useGroups = (bool)$helper->getConfig('use_groups'); |
||
235 | |||
236 | //apply criteria for events |
||
237 | $crEvent = new \CriteriaCompo(); |
||
238 | if ($showItem) { |
||
239 | $crEvent->add(new \Criteria('id', $evId)); |
||
240 | } elseif ('me' === $filter && $uidCurrent > 0) { |
||
241 | $crEvent->add(new \Criteria('submitter', $uidCurrent)); |
||
242 | } |
||
243 | //get only events which are online or from me |
||
244 | $crEventOnline = new \CriteriaCompo(); |
||
245 | $crEventOnline->add(new \Criteria('status', Constants::STATUS_OFFLINE, '>')); |
||
246 | $crEventOnline->add(new \Criteria('submitter', $uidCurrent), 'OR'); |
||
247 | $crEvent->add($crEventOnline); |
||
248 | |||
249 | if ($dateCreated > 0) { |
||
250 | $crEvent->add(new \Criteria('datecreated', $dateCreated, '>=')); |
||
251 | } |
||
252 | // current user |
||
253 | // - must have perm to see event or |
||
254 | // - must be event owner |
||
255 | // - is admin |
||
256 | if ($useGroups && !$userIsAdmin) { |
||
257 | $crEventGroup = new \CriteriaCompo(); |
||
258 | $crEventGroup->add(new \Criteria('groups', '%00000%', 'LIKE')); //all users |
||
259 | if ($uidCurrent > 0) { |
||
260 | // Get groups |
||
261 | $memberHandler = \xoops_getHandler('member'); |
||
262 | $xoopsGroups = $memberHandler->getGroupsByUser($uidCurrent); |
||
263 | foreach ($xoopsGroups as $group) { |
||
264 | $crEventGroup->add(new \Criteria('groups', '%' . substr('00000' . $group, -5) . '%', 'LIKE'), 'OR'); |
||
265 | } |
||
266 | } |
||
267 | $crEventGroup->add(new \Criteria('submitter', $uidCurrent), 'OR'); |
||
268 | $crEvent->add($crEventGroup); |
||
269 | unset($crEventGroup); |
||
270 | } |
||
271 | if (!$showItem) { |
||
272 | if ('past' === $op) { |
||
273 | // list events before now |
||
274 | $crEvent->add(new \Criteria('datefrom', $dateFrom, '<')); |
||
275 | $sortBy = 'datefrom'; |
||
276 | $orderBy = 'DESC'; |
||
277 | } else { |
||
278 | // calendar view: |
||
279 | // - event start is between dateFrom and dateTo |
||
280 | // - event end is between dateFrom and dateTo |
||
281 | // ==> dateFrom and dateTo needed |
||
282 | |||
283 | // index/event/block view: |
||
284 | // - event start or event end is greater than dateFrom |
||
285 | // ==> dateFrom needed, dateTo must be 0 |
||
286 | $crEventFromTo = new \CriteriaCompo(); |
||
287 | $crEventStart = new \CriteriaCompo(); |
||
288 | $crEventStart->add(new \Criteria('datefrom', $dateFrom, '>=')); |
||
289 | if ($dateTo > 0) { |
||
290 | $crEventStart->add(new \Criteria('datefrom', $dateTo, '<=')); |
||
291 | } |
||
292 | $crEventFromTo->add($crEventStart); |
||
293 | $crEventEnd = new \CriteriaCompo(); |
||
294 | $crEventEnd->add(new \Criteria('dateto', $dateFrom, '>=')); |
||
295 | if ($dateTo > 0) { |
||
296 | $crEventEnd->add(new \Criteria('dateto', $dateTo, '<=')); |
||
297 | } |
||
298 | $crEventFromTo->add($crEventEnd, 'OR'); |
||
299 | $crEvent->add($crEventFromTo); |
||
300 | |||
301 | unset($crEventStart, $crEventEnd, $crEventFromTo); |
||
302 | $sortBy = 'datefrom'; |
||
303 | $orderBy = 'ASC'; |
||
304 | } |
||
305 | if (\count($filterCats) > 0) { |
||
306 | $crEventCats = new \CriteriaCompo(); |
||
307 | $crEventCats->add(new \Criteria('catid', '(' . \implode(',', $filterCats) . ')', 'IN')); |
||
308 | foreach ($filterCats as $filterCat) { |
||
309 | $crEventCats->add(new \Criteria('subcats', '%"' . $filterCat . '"%', 'LIKE'), 'OR'); |
||
310 | } |
||
311 | $crEvent->add($crEventCats); |
||
312 | } |
||
313 | } |
||
314 | $crEvent->setSort($sortBy); |
||
315 | $crEvent->setOrder($orderBy); |
||
316 | $eventsCount = $this->getCount($crEvent); |
||
317 | if ($eventsCount > 0) { |
||
318 | if ($limit > 0 && !$showItem) { |
||
319 | $crEvent->setStart($start); |
||
320 | $crEvent->setLimit($limit); |
||
321 | } |
||
322 | // Get All Event |
||
323 | $eventsAll = $this->getAll($crEvent); |
||
324 | |||
325 | return ['count' => $eventsCount, 'eventsAll' => $eventsAll]; |
||
326 | } |
||
327 | |||
328 | return ['count' => 0, 'eventsAll' => []]; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * compare two versions of events |
||
333 | * @param $versionOld |
||
334 | * @param $versionNew |
||
335 | * @return string |
||
336 | */ |
||
337 | public function getEventsCompare($versionOld, $versionNew) |
||
338 | { |
||
339 | $changedValues = []; |
||
340 | // find changes in important fields of table events |
||
341 | $fields = []; |
||
342 | $fields[] = ['name' => 'name', 'caption' => \_MA_WGEVENTS_EVENT_NAME, 'type' => 'text']; |
||
343 | $fields[] = ['name' => 'desc', 'caption' => \_MA_WGEVENTS_EVENT_DESC, 'type' => 'text']; |
||
344 | $fields[] = ['name' => 'datefrom', 'caption' => \_MA_WGEVENTS_EVENT_DATEFROM, 'type' => 'datetime']; |
||
345 | $fields[] = ['name' => 'dateto', 'caption' => \_MA_WGEVENTS_EVENT_DATETO, 'type' => 'datetime']; |
||
346 | $fields[] = ['name' => 'location', 'caption' => \_MA_WGEVENTS_EVENT_LOCATION, 'type' => 'text']; |
||
347 | $fields[] = ['name' => 'fee', 'caption' => \_MA_WGEVENTS_EVENT_FEE, 'type' => 'fee']; |
||
348 | |||
349 | foreach ($fields as $field) { |
||
350 | $valueOld = (string)$versionOld->getVar($field['name']); |
||
351 | $valueNew = (string)$versionNew->getVar($field['name']); |
||
352 | if ($valueOld !== $valueNew) { |
||
353 | if ('' === $valueNew) { |
||
354 | $valueNew = _MA_WGEVENTS_MAIL_REG_MODIFICATION_DELETED; |
||
355 | } |
||
356 | switch ($field['type']) { |
||
357 | case 'datetime': |
||
358 | $changedValues[] = [ |
||
359 | 'caption' => $field['caption'], |
||
360 | 'valueOld' => \formatTimestamp($valueOld, 'm'), |
||
361 | 'valueNew' => \formatTimestamp($valueNew, 'm') |
||
362 | ]; |
||
363 | break; |
||
364 | case 'fee': |
||
365 | $evFee = \json_decode($valueOld, true); |
||
366 | $evFeeText = ''; |
||
367 | foreach($evFee as $fee) { |
||
368 | $evFeeText .= Utility::FloatToString((float)$fee[0]) . ' ' . $fee[1] . '<br>'; |
||
369 | } |
||
370 | $valueOld = $evFeeText; |
||
371 | $evFee = \json_decode($valueNew, true); |
||
372 | $evFeeText = ''; |
||
373 | foreach($evFee as $fee) { |
||
374 | $evFeeText .= Utility::FloatToString((float)$fee[0]) . ' ' . $fee[1] . '<br>'; |
||
375 | } |
||
376 | $valueNew = $evFeeText; |
||
377 | $changedValues[] = [ |
||
378 | 'caption' => $field['caption'], |
||
379 | 'valueOld' => $valueOld, |
||
380 | 'valueNew' => $valueNew |
||
381 | ]; |
||
382 | break; |
||
383 | case'default': |
||
384 | default: |
||
385 | $changedValues[] = [ |
||
386 | 'caption' => $field['caption'], |
||
387 | 'valueOld' => $valueOld, |
||
388 | 'valueNew' => $valueNew |
||
389 | ]; |
||
390 | break; |
||
391 | } |
||
392 | } |
||
393 | } |
||
394 | if (\count($changedValues) > 0) { |
||
395 | $mailHandler = new MailHandler(); |
||
396 | return $mailHandler->array2table ($changedValues); |
||
397 | } |
||
398 | |||
399 | return ''; |
||
400 | } |
||
401 | /** |
||
402 | * get email recipients for noticiations |
||
403 | * @param $registerNotify |
||
404 | * @return array|false|string[] |
||
405 | */ |
||
406 | public function getRecipientsNotify($registerNotify) |
||
422 | } |
||
423 | /** |
||
424 | * get clean date from/to for displaying |
||
425 | * @param int $datefrom |
||
426 | * @param int $dateto |
||
427 | * @param bool $allday |
||
428 | * @return string |
||
429 | */ |
||
430 | public function getDateFromToText($datefrom, $dateto, $allday) |
||
431 | { |
||
432 | $text = ''; |
||
433 | $today = date('d.m.Y', time()) === date('d.m.Y', $datefrom); |
||
434 | $multiday = (int)date('j', $dateto) !== (int)date('j', $datefrom); |
||
435 | /* |
||
436 | $days = 1; |
||
437 | if ($multiday) { |
||
438 | $days = (1 + date_create(date('d.m.Y', $dateto))->diff(date_create(date('d.m.Y', $datefrom)))->format("%a")); |
||
439 | } |
||
440 | */ |
||
441 | $lng_until = ' ' . \_MA_WGEVENTS_EVENT_DATEUNTIL . ' '; |
||
442 | $lng_today = \_MA_WGEVENTS_EVENT_TODAY; |
||
443 | $lng_allday = \_MA_WGEVENTS_EVENT_ALLDAY; |
||
444 | |||
445 | if ($today) { |
||
446 | // get all types of today |
||
447 | if ($allday && !$multiday) { |
||
448 | // today, allday, no multiday |
||
449 | $text = $lng_today . ' ' . $lng_allday; |
||
450 | } elseif (!$allday && !$multiday) { |
||
451 | // today, no allday, no multiday |
||
452 | $text = $lng_today . ' ' . date('H:i', $datefrom) . $lng_until . date('H:i', $dateto); |
||
453 | } else { |
||
454 | // today, no allday, multiday |
||
455 | $text = $lng_today . ' ' . date('H:i', $datefrom) . $lng_until . \formatTimestamp($dateto, 'm'); |
||
456 | } |
||
457 | } elseif ($allday && $multiday) { |
||
458 | // not today, allday, multiday |
||
459 | $text = \formatTimestamp($datefrom, 's') . $lng_allday . $lng_until . \formatTimestamp($dateto, 'm') . $lng_allday; |
||
460 | } elseif (!$allday && !$multiday) { |
||
461 | // not today, no allday, no multiday |
||
462 | $text = \formatTimestamp($datefrom, 's') . ' ' . date('H:i', $datefrom) . $lng_until . date('H:i', $dateto); |
||
463 | } else { |
||
464 | // not today, no allday, multiday |
||
465 | $text = \formatTimestamp($datefrom, 'm') . $lng_until . \formatTimestamp($dateto, 'm'); |
||
466 | //TODO: same time for each day / different times for different days |
||
467 | } |
||
468 | /* |
||
469 | echo '<br>today:'.$today; |
||
470 | echo '<br>datefrom:'.\formatTimestamp($datefrom, 'm'); |
||
471 | echo '<br>dateto:'.\formatTimestamp($dateto, 'm'); |
||
472 | echo '<br>multiday:'.$multiday; |
||
473 | echo '<br>return:'.$text; |
||
474 | */ |
||
475 | return $text; |
||
476 | } |
||
477 | |||
478 | |||
479 | /** |
||
480 | * @public function getFormFilterExport: form for selecting cats and number of lines for export of events |
||
481 | * @param bool $eventDisplayCats |
||
482 | * @param array $filterCats |
||
483 | * @return \XoopsThemeForm |
||
484 | */ |
||
485 | public function getFormFilterExport($limit, $dateFrom, $dateTo, $eventDisplayCats = false, $filterCats = []) |
||
541 | } |
||
542 | } |
||
543 |