RegistrationHandler   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 279
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 129
dl 0
loc 279
rs 9.76
c 0
b 0
f 0
wmc 33

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 3 1
A get() 0 3 1
A getAllRegistrations() 0 5 1
A getInsertId() 0 3 1
A getRegistrationsCriteria() 0 7 1
A getCountRegistrations() 0 5 1
B getRegistrationDetailsByEvent() 0 60 9
B cleanupRegistrations() 0 54 8
B getRegistrationsCompare() 0 49 9
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Wgevents;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * wgEvents module for xoops
17
 *
18
 * @copyright    2021 XOOPS Project (https://xoops.org)
19
 * @license      GPL 2.0 or later
20
 * @package      wgevents
21
 * @since        1.0.0
22
 * @min_xoops    2.5.11 Beta1
23
 * @author       Goffy - Wedega - Email:[email protected] - Website:https://xoops.wedega.com
24
 */
25
26
use XoopsModules\Wgevents;
27
use XoopsModules\Wgevents\{
28
    Helper,
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsModules\Wgevents\Helper. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
29
    Utility
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsModules\Wgevents\Utility. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
30
};
31
32
/**
33
 * Class Object Handler Registration
34
 */
35
class RegistrationHandler extends \XoopsPersistableObjectHandler
36
{
37
    /**
38
     * Constructor
39
     *
40
     * @param \XoopsDatabase $db
41
     */
42
    public function __construct(\XoopsDatabase $db)
43
    {
44
        parent::__construct($db, 'wgevents_registration', Registration::class, 'id', 'evid');
45
    }
46
47
    /**
48
     * @param bool $isNew
49
     *
50
     * @return object
51
     */
52
    public function create($isNew = true)
53
    {
54
        return parent::create($isNew);
55
    }
56
57
    /**
58
     * retrieve a field
59
     *
60
     * @param int $id field id
61
     * @param $fields
62
     * @return \XoopsObject|null reference to the {@link Get} object
63
     */
64
    public function get($id = null, $fields = null)
65
    {
66
        return parent::get($id, $fields);
67
    }
68
69
    /**
70
     * get inserted id
71
     *
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 Registration 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 getCountRegistrations($start = 0, $limit = 0, $sort = 'id ASC, evid', $order = 'ASC')
88
    {
89
        $crCountRegistrations = new \CriteriaCompo();
90
        $crCountRegistrations = $this->getRegistrationsCriteria($crCountRegistrations, $start, $limit, $sort, $order);
91
        return $this->getCount($crCountRegistrations);
92
    }
93
94
    /**
95
     * Get All Registration 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 getAllRegistrations($start = 0, $limit = 0, $sort = 'id ASC, evid', $order = 'ASC')
103
    {
104
        $crAllRegistrations = new \CriteriaCompo();
105
        $crAllRegistrations = $this->getRegistrationsCriteria($crAllRegistrations, $start, $limit, $sort, $order);
106
        return $this->getAll($crAllRegistrations);
107
    }
108
109
    /**
110
     * Get Criteria Registration
111
     * @param        $crRegistration
112
     * @param int $start
113
     * @param int $limit
114
     * @param string $sort
115
     * @param string $order
116
     * @return \CriteriaCompo
117
     */
118
    private function getRegistrationsCriteria($crRegistration, int $start, int $limit, string $sort, string $order)
119
    {
120
        $crRegistration->setStart($start);
121
        $crRegistration->setLimit($limit);
122
        $crRegistration->setSort($sort);
123
        $crRegistration->setOrder($order);
124
        return $crRegistration;
125
    }
126
127
    /**
128
     * Delete all registrations for given event
129
     * @param int $evId
130
     * @return bool
131
     */
132
    public function cleanupRegistrations(int $evId)
133
    {
134
        if ($evId > 0) {
135
            $helper = Helper::getInstance();
136
            $eventHandler = $helper->getHandler('Event');
137
            $taskHandler = $helper->getHandler('Task');
138
139
            $crRegistration = new \CriteriaCompo();
140
            $crRegistration->add(new \Criteria('evid', $evId));
141
            $registrationsCount = $this->getCount($crRegistration);
142
            if ($registrationsCount > 0) {
143
                // declare types
144
                $typeNotify = Constants::MAIL_REG_NOTIFY_CANCEL;
145
                $typeConfirm = Constants::MAIL_REG_CONFIRM_CANCEL;
146
                // get mail addresses from register_notify
147
                $eventObj = $eventHandler->get($evId);
148
                $registerNotify = (string)$eventObj->getVar('register_notify', 'e');
149
                //get all registrations
150
                $registrationsAll = $this->getAll($crRegistration);
151
                foreach (\array_keys($registrationsAll) as $i) {
152
                    $mailsHandler = new MailHandler();
153
                    $mailParams = $mailsHandler->getMailParam($eventObj, $i);
154
                    unset($mailsHandler);
155
                    if ($this->delete($registrationsAll[$i])) {
156
                        // Event delete notification
157
                        /*
158
                        $tags = [];
159
                        $tags['ITEM_NAME'] = $regEvid;
160
                        $notificationHandler = \xoops_getHandler('notification');
161
                        $notificationHandler->triggerEvent('global', 0, 'global_delete', $tags);
162
                        $notificationHandler->triggerEvent('registrations', $regId, 'registration_delete', $tags);
163
                        */
164
                        // send notifications/confirmation emails
165
                        if ('' !== $registerNotify) {
166
                            // send notifications to emails of register_notify
167
                            $notifyEmails = \preg_split("/\r\n|\n|\r/", $registerNotify);
168
                            foreach ($notifyEmails as $recipient) {
169
                                $taskHandler->createTask($typeNotify, $recipient, json_encode($mailParams));
170
                            }
171
                        }
172
                        $regEmail = $registrationsAll[$i]->getVar('email');
173
                        if ('' !== $regEmail) {
174
                            // send confirmation, if radio is checked
175
                            $taskHandler->createTask($typeConfirm, $regEmail, json_encode($mailParams));
176
                        }
177
                        // execute mail sending by task handler
178
                        $taskHandler->processTasks();
179
                    } else {
180
                        $GLOBALS['xoopsTpl']->assign('error', $registrationsAll[$i]->getHtmlErrors());
181
                    }
182
                }
183
            }
184
        }
185
        return true;
186
    }
187
188
    /**
189
     * get all registrations/answers of given event
190
     * @param int $evId // id of event
191
     * @param array $questionsArr // array with all questions for this event
192
     * @param bool $currentUserOnly // true: filter result for current user - false: return all for given event
193
     * @param string $sortBy // sort registrations by
194
     * @param string $orderBy // order for sorting
195
     * @return bool|array
196
     */
197
    public function getRegistrationDetailsByEvent(int $evId, array $questionsArr, $currentUserOnly = true, $sortBy = 'datecreated', $orderBy = 'ASC')
198
    {
199
        if ($evId > 0) {
200
            $helper  = \XoopsModules\Wgevents\Helper::getInstance();
201
            $eventHandler     = $helper->getHandler('Event');
202
            $answerHandler     = $helper->getHandler('Answer');
203
            $permissionsHandler = $helper->getHandler('Permission');
204
205
            $eventObj = $eventHandler->get($evId);
206
            $evSubmitter = $eventObj->getVar('submitter');
207
            $evStatus = $eventObj->getVar('status');
208
209
            $registrations = [];
210
            $crRegistration = new \CriteriaCompo();
211
            $crRegistration->add(new \Criteria('evid', $evId));
212
            if ($currentUserOnly) {
213
                $uidCurrent = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->uid() : 0;
214
                if ($uidCurrent > 0) {
215
                    $crRegistration->add(new \Criteria('submitter', $uidCurrent));
216
                } else {
217
                    $regIp = $_SERVER['REMOTE_ADDR'];
218
                    $crRegistration->add(new \Criteria('ip', $regIp));
219
                }
220
            }
221
            $crRegistration->setSort($sortBy);
222
            $crRegistration->setOrder($orderBy);
223
            $registrationsCount = $this->getCount($crRegistration);
224
            $GLOBALS['xoopsTpl']->assign('registrationsCount', $registrationsCount);
225
            $registrationsAll = $this->getAll($crRegistration);
226
            if ($registrationsCount > 0) {
227
                // Get All Registration
228
                foreach (\array_keys($registrationsAll) as $regId) {
229
                    $registerValues = $registrationsAll[$regId]->getValuesRegistrations();
230
                    $registrations[$regId] = $registrationsAll[$regId]->getValuesRegistrations();
231
                    $registrations[$regId]['id'] = $regId;
232
                    $registrations[$regId]['evid'] = $evId;
233
                    if ($registerValues['submitter'] > 0) {
234
                        $registrations[$regId]['submitter_text'] = $registerValues['submitter_text'];
235
                    } else {
236
                        $registrations[$regId]['submitter_text'] = $registerValues['ip'];
237
                    }
238
                    $registrations[$regId]['permRegistrationEdit'] = $permissionsHandler->getPermRegistrationsEdit(
239
                                                                            $registerValues['ip'],
240
                                                                            $registerValues['submitter'],
241
                                                                            $evSubmitter,
242
                                                                            $evStatus,
243
                                                                        );
244
                    $permRegistrationApprove = $permissionsHandler->getPermRegistrationsApprove($evSubmitter, $evStatus);
245
                    $registrations[$regId]['permRegistrationApprove'] = $permRegistrationApprove;
246
                    $registrations[$regId]['permRegistrationConfirm'] = ($permRegistrationApprove && (int)$registerValues['status'] < Constants::STATUS_APPROVED);
247
248
                    // get all answers for this event
249
                    $answers = $answerHandler->getAnswersDetailsByRegistration($regId, $questionsArr);
250
                    $registrations[$regId]['answers'] = $answers;
251
                }
252
                return $registrations;
253
254
            }
255
        }
256
        return false;
257
    }
258
259
    /**
260
     * compare two versions of registration
261
     * @param  $versionOld
262
     * @param  $regIdNew
263
     * @return string
264
     */
265
    public function getRegistrationsCompare($versionOld, $regIdNew)
266
    {
267
        $versionNew = $this->get($regIdNew);
268
        $infotext = '';
269
        $fields = [];
270
        $fields[] = ['name' => 'firstname', 'caption' => \_MA_WGEVENTS_REGISTRATION_FIRSTNAME, 'type' => 'text'];
271
        $fields[] = ['name' => 'lastname', 'caption' => \_MA_WGEVENTS_REGISTRATION_LASTNAME, 'type' => 'text'];
272
        $fields[] = ['name' => 'email', 'caption' => \_MA_WGEVENTS_REGISTRATION_EMAIL, 'type' => 'text'];
273
        $fields[] = ['name' => 'paidamount', 'caption' => \_MA_WGEVENTS_REGISTRATION_PAIDAMOUNT, 'type' => 'float'];
274
        foreach ($fields as $field) {
275
            $valueOld = $versionOld->getVar($field['name']);
276
            $valueNew = $versionNew->getVar($field['name']);
277
            if ($valueOld != $valueNew) {
278
                switch ($field['type']) {
279
                    case 'text':
280
                    default:
281
                        $infotext .= \sprintf(\_MA_WGEVENTS_MAIL_REG_MODIFICATION, $field['caption'], $valueOld, $valueNew) . PHP_EOL;
0 ignored issues
show
Bug introduced by
It seems like $valueNew can also be of type array and array; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

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

281
                        $infotext .= \sprintf(\_MA_WGEVENTS_MAIL_REG_MODIFICATION, $field['caption'], $valueOld, /** @scrutinizer ignore-type */ $valueNew) . PHP_EOL;
Loading history...
282
                        break;
283
                    case 'datetime':
284
                        $infotext .= \sprintf(\_MA_WGEVENTS_MAIL_REG_MODIFICATION, $field['caption'], \formatTimestamp($valueOld, 'm'), \formatTimestamp($valueNew, 'm')) . PHP_EOL;
285
                        break;
286
                    case 'float':
287
                        $infotext .= \sprintf(\_MA_WGEVENTS_MAIL_REG_MODIFICATION, $field['caption'], Utility::FloatToString($valueOld), Utility::FloatToString($valueNew)) . PHP_EOL;
288
                        break;
289
                }
290
            }
291
        }
292
        unset($fields, $field);
293
294
        $field = 'listwait';
295
        $valueOld = $versionOld->getVar($field);
296
        $valueNew = $versionNew->getVar($field);
297
        if ($valueOld != $valueNew) {
298
            $infotext .= \sprintf(\_MA_WGEVENTS_MAIL_REG_MODIFICATION, \_MA_WGEVENTS_REGISTRATION_LISTWAIT, Utility::getListWaitText($valueOld), Utility::getListWaitText($valueNew)) . PHP_EOL;
299
        }
300
        $field = 'status';
301
        $valueOld = $versionOld->getVar($field);
302
        $valueNew = $versionNew->getVar($field);
303
        if ($valueOld != $valueNew) {
304
            $infotext .= \sprintf(\_MA_WGEVENTS_MAIL_REG_MODIFICATION, \_MA_WGEVENTS_STATUS, Utility::getStatusText($valueOld), Utility::getStatusText($valueNew)) . PHP_EOL;
305
        }
306
        $field = 'financial';
307
        $valueOld = $versionOld->getVar($field);
308
        $valueNew = $versionNew->getVar($field);
309
        if ($valueOld != $valueNew) {
310
            $infotext .= \sprintf(\_MA_WGEVENTS_MAIL_REG_MODIFICATION, \_MA_WGEVENTS_REGISTRATION_FINANCIAL, Utility::getFinancialText($valueOld), Utility::getFinancialText($valueNew)) . PHP_EOL;
311
        }
312
313
        return $infotext;
314
    }
315
316
}
317