CategoryHandler   A
last analyzed

Complexity

Total Complexity 34

Size/Duplication

Total Lines 296
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 127
c 0
b 0
f 0
dl 0
loc 296
rs 9.68
wmc 34

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getInsertId() 0 3 1
A getCountCategories() 0 5 1
A getAllCategories() 0 5 1
A getCollection() 0 19 3
A getNextWeight() 0 17 3
A getAllCatsOnline() 0 21 5
A getCategoriesCriteria() 0 7 1
A __construct() 0 3 1
A get() 0 3 1
A create() 0 3 1
A getFormCatsCb() 0 32 4
C getCategoriesForFilter() 0 72 12
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\Forms;
28
use XoopsModules\Wgevents\Forms\FormInline;
29
30
/**
31
 * Class Object Handler Category
32
 */
33
class CategoryHandler extends \XoopsPersistableObjectHandler
34
{
35
    /**
36
     * Constructor
37
     *
38
     * @param \XoopsDatabase $db
39
     */
40
    public function __construct(\XoopsDatabase $db)
41
    {
42
        parent::__construct($db, 'wgevents_category', Category::class, 'id', 'name');
43
    }
44
45
    /**
46
     * @param bool $isNew
47
     *
48
     * @return object
49
     */
50
    public function create($isNew = true)
51
    {
52
        return parent::create($isNew);
53
    }
54
55
    /**
56
     * retrieve a field
57
     *
58
     * @param int $id field id
59
     * @param $fields
60
     * @return \XoopsObject|null reference to the {@link Get} object
61
     */
62
    public function get($id = null, $fields = null)
63
    {
64
        return parent::get($id, $fields);
65
    }
66
67
    /**
68
     * get inserted id
69
     *
70
     * @return int reference to the {@link Get} object
71
     */
72
    public function getInsertId()
73
    {
74
        return $this->db->getInsertId();
75
    }
76
77
    /**
78
     * Get Count Category in the database
79
     * @param int    $start
80
     * @param int    $limit
81
     * @param string $sort
82
     * @param string $order
83
     * @return int
84
     */
85
    public function getCountCategories($start = 0, $limit = 0, $sort = 'weight ASC, id', $order = 'ASC')
86
    {
87
        $crCountCategories = new \CriteriaCompo();
88
        $crCountCategories = $this->getCategoriesCriteria($crCountCategories, $start, $limit, $sort, $order);
89
        return $this->getCount($crCountCategories);
0 ignored issues
show
Bug introduced by
$crCountCategories 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

89
        return $this->getCount(/** @scrutinizer ignore-type */ $crCountCategories);
Loading history...
90
    }
91
92
    /**
93
     * Get All Category in the database
94
     * @param int    $start
95
     * @param int    $limit
96
     * @param string $sort
97
     * @param string $order
98
     * @return array
99
     */
100
    public function getAllCategories($start = 0, $limit = 0, $sort = 'weight ASC, id', $order = 'ASC')
101
    {
102
        $crAllCategories = new \CriteriaCompo();
103
        $crAllCategories = $this->getCategoriesCriteria($crAllCategories, $start, $limit, $sort, $order);
104
        return $this->getAll($crAllCategories);
0 ignored issues
show
Bug introduced by
$crAllCategories 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

104
        return $this->getAll(/** @scrutinizer ignore-type */ $crAllCategories);
Loading history...
105
    }
106
107
    /**
108
     * Get All online categories in the database
109
     * @param int    $type
110
     * @param string $sort
111
     * @param string $order
112
     * @return array
113
     */
114
    public function getAllCatsOnline($type = 0, $sort = 'weight ASC, id', $order = 'ASC')
115
    {
116
        $cats = [];
117
        $crCategory = new \CriteriaCompo();
118
        $crCategory->add(new \Criteria('status', Constants::STATUS_ONLINE));
119
        if (Constants::CATEGORY_TYPE_MAIN == $type) {
120
            $crCategory->add(new \Criteria('type', Constants::CATEGORY_TYPE_SUB, '<'));
121
        }
122
        if (Constants::CATEGORY_TYPE_SUB == $type) {
123
            $crCategory->add(new \Criteria('type', Constants::CATEGORY_TYPE_MAIN, '<>'));
124
        }
125
        $crCategory->setSort($sort);
126
        $crCategory->setOrder($order);
127
        $categoriesCount = $this->getCount($crCategory);
128
        if ($categoriesCount > 0) {
129
            $categoriesAll = $this->getAll($crCategory);
130
            foreach (\array_keys($categoriesAll) as $i) {
131
                $cats[$i] = $categoriesAll[$i]->getVar('name');
132
            }
133
        }
134
        return $cats;
135
    }
136
137
    /**
138
     * Get Criteria Category
139
     * @param        $crCategory
140
     * @param int $start
141
     * @param int $limit
142
     * @param string $sort
143
     * @param string $order
144
     * @return int
145
     */
146
    private function getCategoriesCriteria($crCategory, int $start, int $limit, string $sort, string $order)
147
    {
148
        $crCategory->setStart($start);
149
        $crCategory->setLimit($limit);
150
        $crCategory->setSort($sort);
151
        $crCategory->setOrder($order);
152
        return $crCategory;
153
    }
154
155
    /**
156
     * @public function to get next value for sorting
157
     *
158
     * @return int
159
     */
160
    public function getNextWeight()
161
    {
162
        $nextValue = 0;
163
164
        $crCategory = new \CriteriaCompo();
165
        $crCategory->setSort('weight');
166
        $crCategory->setOrder('DESC');
167
        $crCategory->setLimit(1);
168
        $categoriesCount = $this->getCount($crCategory);
169
        if ($categoriesCount > 0) {
170
            $categoriesAll = $this->getAll($crCategory);
171
            foreach (\array_keys($categoriesAll) as $i) {
172
                $nextValue = $categoriesAll[$i]->getVar('weight');
173
            }
174
        }
175
176
        return $nextValue + 1;
177
178
    }
179
180
    /**
181
     * @public function to get a collection of all existing categories
182
     *
183
     * @return array
184
     */
185
    public function getCollection()
186
    {
187
        $categories = [];
188
        $categoriesCount = $this->getCount();
189
        if ($categoriesCount > 0) {
190
            $categoriesAll = $this->getAll();
191
            foreach (\array_keys($categoriesAll) as $i) {
192
                $categories[$i] = [
193
                    'id' => $i,
194
                    'name' => $categoriesAll[$i]->getVar('name'),
195
                    'color' => $categoriesAll[$i]->getVar('color'),
196
                    'bordercolor' => $categoriesAll[$i]->getVar('bordercolor'),
197
                    'bgcolor' => $categoriesAll[$i]->getVar('bgcolor'),
198
                    'othercss' => $categoriesAll[$i]->getVar('othercss')
199
                ];
200
            }
201
        }
202
203
        return $categories;
204
205
    }
206
207
    /**
208
     * @public function getFormCatsCb: form with checkboxes of cats with events
209
     * @param array $filterCats
210
     * @param string $op
211
     * @param string $filter
212
     * @return FormInline
213
     */
214
    public function getFormCatsCb($filterCats = [], $op = 'list', $filter = '')
215
    {
216
        // Get Theme Form
217
        \xoops_load('XoopsFormLoader');
218
        $form = new Forms\FormInline('', 'formCatsCb', $_SERVER['REQUEST_URI'], 'post', true);
219
        $form->setExtra('enctype="multipart/form-data"');
220
        $cbAll = 1;
221
        // Form Select categories
222
        $catsOnline = $this->getAllCatsOnline();
223
        if (0 === \count($filterCats)) {
224
            foreach (\array_keys($catsOnline) as $i) {
225
                $filterCats[] = $i;
226
            }
227
        } elseif (\count($filterCats) < \count($catsOnline)) {
228
            $cbAll = 0;
229
        }
230
        $catAllSelect = new Forms\FormCheckboxInline(\_MA_WGEVENTS_CATEGORY_FILTER, 'all_cats', $cbAll);
231
        $catAllSelect->addOption(1, _ALL);
232
        $catAllSelect->setExtra(" onclick='toggleAllCats()' ");
233
        $form->addElement($catAllSelect);
234
        $catSelect = new Forms\FormCheckboxInline('', 'filter_cats', $filterCats);
235
        $catSelect->addOptionArray($catsOnline);
236
        $form->addElement($catSelect);
237
        $btnFilter = new \XoopsFormButton('', 'submit', \_MA_WGEVENTS_APPLY_FILTER, 'submit');
238
        $btnFilter->setClass('btn btn-success');
239
        $form->addElement($btnFilter);
240
241
        // To Save
242
        $form->addElement(new \XoopsFormHidden('op', $op));
243
        $form->addElement(new \XoopsFormHidden('start', '0'));
244
        $form->addElement(new \XoopsFormHidden('filter', $filter));
245
        return $form;
246
    }
247
248
    /**
249
     * @public function getCategoriesForFilter: get all cats wtih number of events for displying as filter
250
     * @param string $eventDisplayCats
251
     * @param array  $filterCats
252
     * @param string $op
253
     * @param bool   $useGroups
254
     * @param string $filter
255
     * @return array
256
     */
257
    public function getCategoriesForFilter ($eventDisplayCats, $filterCats, $op, $useGroups, $filter = '') {
258
259
        $helper = Helper::getInstance();
260
        $eventHandler = $helper->getHandler('Event');
261
262
        $uidCurrent  = 0;
263
        $userIsAdmin = false;
264
        if (\is_object($GLOBALS['xoopsUser'])) {
265
            $uidCurrent  = $GLOBALS['xoopsUser']->uid();
266
            $userIsAdmin = $GLOBALS['xoopsUser']->isAdmin();
267
        }
268
269
        $crCategory = new \CriteriaCompo();
270
        $crCategory->add(new \Criteria('status',Constants::STATUS_ONLINE));
271
        $crCategory->setSort('weight');
272
        $crCategory->setOrder('ASC');
273
        $categoriesCount = $this->getCount($crCategory);
274
        $GLOBALS['xoopsTpl']->assign('categoriesCount', $categoriesCount);
275
        if ($categoriesCount > 0) {
276
            if ('form' === $eventDisplayCats) {
277
                $formCatsCb = $this->getFormCatsCb($filterCats, $op, $filter);
278
                $GLOBALS['xoopsTpl']->assign('formCatsCb', $formCatsCb->render());
279
            } else {
280
                //$crCategory->setStart($start);
281
                //$crCategory->setLimit($limit);
282
                $categoriesAll = $this->getAll($crCategory);
283
                $categories = [];
284
                if ('button' === $eventDisplayCats) {
285
                    $categories[0] = ['id' => 0, 'logo' => 'blank.gif', 'name' => 'alle', 'eventsCount' => 0];
286
                }
287
                // Get All Event
288
                foreach (\array_keys($categoriesAll) as $i) {
289
                    $categories[$i] = $categoriesAll[$i]->getValuesCategories();
290
                    $crEvent = new \CriteriaCompo();
291
                    $crEvent->add(new \Criteria('catid',$i));
292
                    // current user
293
                    // - must have perm to see event or
294
                    // - must be event owner
295
                    // - is admin
296
                    if ($useGroups && !$userIsAdmin) {
297
                        $crEventGroup = new \CriteriaCompo();
298
                        $crEventGroup->add(new \Criteria('groups', '%00000%', 'LIKE')); //all users
299
                        if ($uidCurrent > 0) {
300
                            // Get groups
301
                            $memberHandler = \xoops_getHandler('member');
302
                            $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

302
                            /** @scrutinizer ignore-call */ 
303
                            $xoopsGroups = $memberHandler->getGroupsByUser($uidCurrent);
Loading history...
303
                            foreach ($xoopsGroups as $group) {
304
                                $crEventGroup->add(new \Criteria('groups', '%' . substr('00000' . $group, -5) . '%', 'LIKE'), 'OR');
305
                            }
306
                        }
307
                        $crEventGroup->add(new \Criteria('submitter', $uidCurrent), 'OR');
308
                        $crEvent->add($crEventGroup);
309
                        unset($crEventGroup);
310
                    }
311
                    $eventsCount = $eventHandler->getCount($crEvent);
312
                    $nbEventsText = \_MA_WGEVENTS_CATEGORY_NOEVENTS;
313
                    if ($eventsCount > 0) {
314
                        if ($eventsCount > 1) {
315
                            $nbEventsText = \sprintf(\_MA_WGEVENTS_CATEGORY_EVENTS, $eventsCount);
316
                        } else {
317
                            $nbEventsText = \_MA_WGEVENTS_CATEGORY_EVENT;
318
                        }
319
                    }
320
                    $categories[$i]['nbeventsText'] = $nbEventsText;
321
                    $categories[$i]['eventsCount'] = $eventsCount;
322
                }
323
324
                return $categories;
325
            }
326
        }
327
328
        return [];
329
    }
330
}
331