Passed
Push — master ( 670039...f5f943 )
by Goffy
03:56
created

EventHandler::getFormPageNavCounter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 22
nc 2
nop 2
dl 0
loc 29
rs 9.568
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
4
namespace XoopsModules\Wgevents;
5
6
/*
7
 You may not change or alter any portion of this comment or credits
8
 of supporting developers from this source code or any supporting source code
9
 which is considered copyrighted (c) material of the original comment or credit authors.
10
11
 This program is distributed in the hope that it will be useful,
12
 but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
*/
15
16
/**
17
 * wgEvents module for xoops
18
 *
19
 * @copyright    2021 XOOPS Project (https://xoops.org)
20
 * @license      GPL 2.0 or later
21
 * @package      wgevents
22
 * @since        1.0.0
23
 * @min_xoops    2.5.11 Beta1
24
 * @author       Goffy - Wedega - Email:[email protected] - Website:https://xoops.wedega.com
25
 */
26
27
use XoopsModules\Wgevents;
28
use XoopsModules\Wgevents\MailHandler;
29
30
31
/**
32
 * Class Object Handler Event
33
 */
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 null fields
0 ignored issues
show
Bug introduced by
The type XoopsModules\Wgevents\fields was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
     * @param null
72
     * @return int reference to the {@link Get} object
73
     */
74
    public function getInsertId()
75
    {
76
        return $this->db->getInsertId();
77
    }
78
79
    /**
80
     * Get Count Event in the database
81
     * @param int    $start
82
     * @param int    $limit
83
     * @param string $sort
84
     * @param string $order
85
     * @return int
86
     */
87
    public function getCountEvents($start = 0, $limit = 0, $sort = 'id', $order = 'DESC')
88
    {
89
        $crCountEvents = new \CriteriaCompo();
90
        $crCountEvents = $this->getEventsCriteria($crCountEvents, $start, $limit, $sort, $order);
91
        return $this->getCount($crCountEvents);
0 ignored issues
show
Bug introduced by
$crCountEvents of type integer is incompatible with the type CriteriaElement|null expected by parameter $criteria of XoopsPersistableObjectHandler::getCount(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
        return $this->getCount(/** @scrutinizer ignore-type */ $crCountEvents);
Loading history...
92
    }
93
94
    /**
95
     * Get All Event in the database
96
     * @param int    $start
97
     * @param int    $limit
98
     * @param string $sort
99
     * @param string $order
100
     * @return array
101
     */
102
    public function getAllEvents($start = 0, $limit = 0, $sort = 'id', $order = 'DESC')
103
    {
104
        $crAllEvents = new \CriteriaCompo();
105
        $crAllEvents = $this->getEventsCriteria($crAllEvents, $start, $limit, $sort, $order);
106
        return $this->getAll($crAllEvents);
0 ignored issues
show
Bug introduced by
$crAllEvents of type integer is incompatible with the type CriteriaElement|null expected by parameter $criteria of XoopsPersistableObjectHandler::getAll(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

106
        return $this->getAll(/** @scrutinizer ignore-type */ $crAllEvents);
Loading history...
107
    }
108
109
    /**
110
     * Get Criteria Event
111
     * @param        $crEvent
112
     * @param int $start
113
     * @param int $limit
114
     * @param string $sort
115
     * @param string $order
116
     * @return int
117
     */
118
    private function getEventsCriteria($crEvent, int $start, int $limit, string $sort, string $order)
119
    {
120
        $crEvent->setStart($start);
121
        $crEvent->setLimit($limit);
122
        $crEvent->setSort($sort);
123
        $crEvent->setOrder($order);
124
        return $crEvent;
125
    }
126
127
    /**
128
     * @public function getForm
129
     * @param bool $action
130
     * @return \XoopsThemeForm
131
     */
132
    public function getFormEventSelect($action = false)
133
    {
134
        $helper = Helper::getInstance();
135
        if (!$action) {
136
            $action = $_SERVER['REQUEST_URI'];
137
        }
138
        // Get Theme Form
139
        \xoops_load('XoopsFormLoader');
140
        $form = new \XoopsThemeForm(\_MA_WGEVENTS_EVENT_SELECT, 'form', $action, 'post', true);
141
        $form->setExtra('enctype="multipart/form-data"');
142
        // Form Table categories
143
        $eventHandler = $helper->getHandler('Event');
144
        $evidSelect = new \XoopsFormSelect(\_MA_WGEVENTS_EVENT_ID, 'evid', 0);
145
        $evidSelect->addOption('');
146
        $evidSelect->addOptionArray($eventHandler->getList());
147
        $evidSelect->setExtra("onchange='submit()'");
148
        $form->addElement($evidSelect);
149
        // To list
150
        $form->addElement(new \XoopsFormHidden('op', 'list'));
151
152
        return $form;
153
    }
154
155
    /**
156
     * @public function getForm
157
     * @param array  $params
158
     * @param string $action
159
     * @return \XoopsThemeForm
160
     */
161
    public function getFormPageNavCounter($params = [], $action = '')
162
    {
163
        if (!$action) {
164
            $action = $_SERVER['REQUEST_URI'];
165
        }
166
        // Get Theme Form
167
        \xoops_load('XoopsFormLoader');
168
        $form = new Forms\FormInline('', 'formPageNavCounter', $action, 'post', true);
169
        $form->setExtra('enctype="multipart/form-data"');
170
        // Form Table categories
171
        $pageNavTray = new Forms\FormElementTray('', '');
172
        $evidSelect = new \XoopsFormSelect(\_MA_WGEVENTS_EVENT_ID, 'limit', $params['limit']);
173
        $evidSelect->addOption(10);
174
        $evidSelect->addOption(20);
175
        $evidSelect->addOption(30);
176
        $evidSelect->addOption(40);
177
        $evidSelect->addOption(50);
178
        $evidSelect->addOption(100);
179
        $evidSelect->addOption(0, _ALL);
180
        $evidSelect->setExtra("onchange='submit()'");
181
        $pageNavTray->addElement($evidSelect);
182
        $form->addElement($pageNavTray);
183
        // To list
184
        $form->addElement(new \XoopsFormHidden('op',         $params['op']));
185
        $form->addElement(new \XoopsFormHidden('start', 0));
186
        $form->addElement(new \XoopsFormHidden('cat_id',     $params['cat_id']));
187
        $form->addElement(new \XoopsFormHidden('filterCats', $params['filterCats']));
188
189
        return $form;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $form returns the type XoopsModules\Wgevents\Forms\FormInline which is incompatible with the documented return type XoopsThemeForm.
Loading history...
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
        } else {
241
            if ('me' == $filter && $uidCurrent > 0) {
242
                $crEvent->add(new \Criteria('submitter', $uidCurrent));
243
            }
244
        }
245
        //get only events which are online or from me
246
        $crEventOnline = new \CriteriaCompo();
247
        $crEventOnline->add(new \Criteria('status', Constants::STATUS_OFFLINE, '>'));
248
        $crEventOnline->add(new \Criteria('submitter', $uidCurrent), 'OR');
249
        $crEvent->add($crEventOnline);
250
251
        if ($dateCreated > 0) {
252
            $crEvent->add(new \Criteria('datecreated', $dateCreated, '>='));
253
        }
254
        if ($useGroups) {
255
            // current user
256
            // - must have perm to see event or
257
            // - must be event owner
258
            // - is admin
259
            if (!$userIsAdmin) {
260
                $crEventGroup = new \CriteriaCompo();
261
                $crEventGroup->add(new \Criteria('groups', '%00000%', 'LIKE')); //all users
262
                if ($uidCurrent > 0) {
263
                    // Get groups
264
                    $memberHandler = \xoops_getHandler('member');
265
                    $xoopsGroups = $memberHandler->getGroupsByUser($uidCurrent);
0 ignored issues
show
Bug introduced by
The method getGroupsByUser() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsMembershipHandler or XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

265
                    /** @scrutinizer ignore-call */ 
266
                    $xoopsGroups = $memberHandler->getGroupsByUser($uidCurrent);
Loading history...
266
                    foreach ($xoopsGroups as $group) {
267
                        $crEventGroup->add(new \Criteria('groups', '%' . substr('00000' . $group, -5) . '%', 'LIKE'), 'OR');
268
                    }
269
                }
270
                $crEventGroup->add(new \Criteria('submitter', $uidCurrent), 'OR');
271
                $crEvent->add($crEventGroup);
272
                unset($crEventGroup);
273
            }
274
        }
275
        if (!$showItem) {
276
            if ('past' == $op) {
277
                // list events before now
278
                $crEvent->add(new \Criteria('datefrom', $dateFrom, '<'));
279
                $sortBy  = 'datefrom';
280
                $orderBy = 'DESC';
281
            } else {
282
                // calendar view:
283
                // - event start is between dateFrom and dateTo
284
                // - event end is between dateFrom and dateTo
285
                // ==> dateFrom and dateTo needed
286
287
                // index/event/block view:
288
                // - event start or event end is greater than dateFrom
289
                // ==> dateFrom needed, dateTo must be 0
290
                $crEventFromTo = new \CriteriaCompo();
291
                $crEventStart = new \CriteriaCompo();
292
                $crEventStart->add(new \Criteria('datefrom', $dateFrom, '>='));
293
                if ($dateTo > 0) {
294
                    $crEventStart->add(new \Criteria('datefrom', $dateTo, '<='));
295
                }
296
                $crEventFromTo->add($crEventStart);
297
                $crEventEnd = new \CriteriaCompo();
298
                $crEventEnd->add(new \Criteria('dateto', $dateFrom, '>='));
299
                if ($dateTo > 0) {
300
                    $crEventEnd->add(new \Criteria('dateto', $dateTo, '<='));
301
                }
302
                $crEventFromTo->add($crEventEnd, 'OR');
303
                $crEvent->add($crEventFromTo);
304
305
                unset($crEventStart, $crEventEnd, $crEventFromTo);
306
                $sortBy  = 'datefrom';
307
                $orderBy = 'ASC';
308
            }
309
            if (\count($filterCats) > 0) {
310
                $crEventCats = new \CriteriaCompo();
311
                $crEventCats->add(new \Criteria('catid', '(' . \implode(',', $filterCats) . ')', 'IN'));
312
                foreach ($filterCats as $filterCat) {
313
                    $crEventCats->add(new \Criteria('subcats', '%"' . $filterCat . '"%', 'LIKE'), 'OR');
314
                }
315
                $crEvent->add($crEventCats);
316
            }
317
        }
318
        $crEvent->setSort($sortBy);
319
        $crEvent->setOrder($orderBy);
320
        $eventsCount = $this->getCount($crEvent);
321
        if ($eventsCount > 0) {
322
            if ($limit > 0) {
323
                $crEvent->setStart($start);
324
                $crEvent->setLimit($limit);
325
            }
326
            // Get All Event
327
            $eventsAll = $this->getAll($crEvent);
328
329
            return ['count' => $eventsCount, 'eventsAll' => $eventsAll];
330
        }
331
332
        return ['count' => 0, 'eventsAll' => []];
333
    }
334
335
    /**
336
     * compare two versions of events
337
     * @param  $versionOld
338
     * @param  $versionNew
339
     * @return string
340
     */
341
    public function getEventsCompare($versionOld, $versionNew)
342
    {
343
        $changedValues = [];
344
        // find changes in important fields of table events
345
        $fields = [];
346
        $fields[] = ['name' => 'name', 'caption' => \_MA_WGEVENTS_EVENT_NAME, 'type' => 'text'];
347
        $fields[] = ['name' => 'desc', 'caption' => \_MA_WGEVENTS_EVENT_DESC, 'type' => 'text'];
348
        $fields[] = ['name' => 'datefrom', 'caption' => \_MA_WGEVENTS_EVENT_DATEFROM, 'type' => 'datetime'];
349
        $fields[] = ['name' => 'dateto', 'caption' => \_MA_WGEVENTS_EVENT_DATETO, 'type' => 'datetime'];
350
        $fields[] = ['name' => 'location', 'caption' => \_MA_WGEVENTS_EVENT_LOCATION, 'type' => 'text'];
351
        $fields[] = ['name' => 'fee', 'caption' => \_MA_WGEVENTS_EVENT_FEE, 'type' => 'text'];
352
353
        foreach ($fields as $field) {
354
            $valueOld = $versionOld->getVar($field['name']);
355
            $valueNew = $versionNew->getVar($field['name']);
356
            if ($valueOld != $valueNew) {
357
                if ('' == $valueNew) {
358
                    $valueNew = _MA_WGEVENTS_MAIL_REG_MODIFICATION_DELETED;
359
                }
360
                switch ($field['type']) {
361
                    case 'datetime':
362
                        $changedValues[] = [
363
                            'caption' => $field['caption'],
364
                            'valueOld' => \formatTimestamp($valueOld, 'm'),
365
                            'valueNew' => \formatTimestamp($valueNew, 'm')
366
                        ];
367
                        break;
368
                    case'default':
369
                    default:
370
                       $changedValues[] = [
371
                            'caption' => $field['caption'],
372
                            'valueOld' => $valueOld,
373
                            'valueNew' => $valueNew
374
                        ];
375
                        break;
376
                }
377
            }
378
        }
379
        if (\count($changedValues) > 0) {
380
            $mailHandler = new MailHandler();
381
            return $mailHandler->array2table ($changedValues);
382
        }
383
384
        return '';
385
    }
386
    /**
387
     * get email recipients for noticiations
388
     * @param  $registerNotify
389
     * @return array|false|string[]
390
     */
391
    public function getRecipientsNotify($registerNotify)
392
    {
393
        $notifyEmails   = preg_split("/\r\n|\n|\r/", $registerNotify);
394
        // no notification to myself
395
        if (\is_object($GLOBALS['xoopsUser'])) {
396
            $email = $GLOBALS['xoopsUser']->email();
397
            if ('' != $email) {
398
                foreach ($notifyEmails as $key => $value) {
399
                    if ($value == $email) {
400
                        unset($notifyEmails[$key]);
401
                    }
402
                }
403
            }
404
       }
405
406
        return $notifyEmails;
407
    }
408
    /**
409
     * get clean date from/to for displaying
410
     * @param  int $datefrom
411
     * @param  int $dateto
412
     * @param  bool $allday
413
     * @return string
414
     */
415
    public function getDateFromToText($datefrom, $dateto, $allday)
416
    {
417
        $text = '';
418
        $today = date('d.m.Y', time()) === date('d.m.Y', $datefrom);
419
        $multiday = (int)date('j', $dateto) > (int)date('j', $datefrom);
420
421
        // currently only used by blocks
422
        //if (\defined(\_MB_WGEVENTS_EVENT_TODAY)) {
423
            $lng_today = \_MA_WGEVENTS_EVENT_TODAY;
424
            $lng_allday = \_MA_WGEVENTS_EVENT_ALLDAY;
425
        //}
426
427
        if ($today) {
428
            // get all types of today
429
            if ($allday && !$multiday) {
430
                // today, allday, no multiday
431
                $text = $lng_today . ' ' . $lng_allday;
432
            } else if ($today && !$allday && !$multiday) {
433
                // today, no allday, no multiday
434
                $text = $lng_today . ' ' . date('H:i', $datefrom) . ' - ' . date('H:i', $dateto);
435
            } else {
436
                // today, no allday, multiday
437
                $text = $lng_today . ' ' . date('H:i', $datefrom) . ' - ' . \formatTimestamp($dateto, 'm');
438
            }
439
        } else {
440
            // not today
441
            if ($allday && $multiday) {
442
                // allday, multiday
443
                $text =  \formatTimestamp($datefrom, 's') . $lng_allday . ' - ' . \formatTimestamp($dateto, 'm') . $lng_allday;
444
            } else if (!$allday && !$multiday) {
445
                // no allday, no multiday
446
                $text = \formatTimestamp($datefrom, 's') . ' ' . date('H:i', $datefrom) . ' - ' . date('H:i', $dateto);
447
            } else {
448
                // no allday, multiday
449
                $text = \formatTimestamp($datefrom, 'm') . ' - ' . \formatTimestamp($dateto, 'm');
450
            }
451
        }
452
        /*
453
        echo '<br>today:'.$today;
454
        echo '<br>datefrom:'.\formatTimestamp($datefrom, 'm');
455
        echo '<br>dateto:'.\formatTimestamp($dateto, 'm');
456
        echo '<br>multiday:'.$multiday;
457
        echo '<br>return:'.$text;
458
        */
459
        return $text;
460
    }
461
}
462