Passed
Pull Request — master (#81)
by Michael
02:58
created

class/GroupsHandler.php (2 issues)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Yogurt;
6
7
/*
8
 You may not change or alter any portion of this comment or credits
9
 of supporting developers from this source code or any supporting source code
10
 which is considered copyrighted (c) material of the original comment or credit authors.
11
 
12
 This program is distributed in the hope that it will be useful,
13
 but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
*/
16
17
/**
18
 * @category        Module
19
 * @package         yogurt
20
 * @copyright       {@link https://xoops.org/ XOOPS Project}
21
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
22
 * @author          Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
23
 */
24
25
use CriteriaElement;
26
use XoopsDatabase;
27
use XoopsFormButton;
28
use XoopsFormFile;
29
use XoopsFormHidden;
30
use XoopsFormLabel;
31
use XoopsFormText;
32
use XoopsFormTextArea;
33
use XoopsMediaUploader;
34
use XoopsObject;
35
use XoopsPersistableObjectHandler;
36
use XoopsThemeForm;
37
38
/**
39
 * Protection against inclusion outside the site
40
 */
41
if (!\defined('XOOPS_ROOT_PATH')) {
42
    die('XOOPS root path not defined');
43
}
44
45
// -------------------------------------------------------------------------
46
// ------------------Groups user handler class -------------------
47
// -------------------------------------------------------------------------
48
49
/**
50
 * yogurt_groupshandler class.
51
 * This class provides simple mecanisme for Groups object
52
 */
53
class GroupsHandler extends XoopsPersistableObjectHandler
54
{
55
    public $helper;
56
57
    public $isAdmin;
58
59
    /**
60
     * Constructor
61
     * @param \XoopsDatabase|null              $xoopsDatabase
62
     * @param \XoopsModules\Yogurt\Helper|null $helper
63
     */
64
65
    public function __construct(
66
        ?XoopsDatabase $xoopsDatabase = null,
67
        $helper = null
68
    ) {
69
        /** @var \XoopsModules\Yogurt\Helper $this ->helper */
70
71
        if (null === $helper) {
72
            $this->helper = Helper::getInstance();
73
        } else {
74
            $this->helper = $helper;
75
        }
76
77
        $isAdmin = $this->helper->isUserAdmin();
78
79
        parent::__construct($xoopsDatabase, 'yogurt_groups', Groups::class, 'group_id', 'group_title');
80
    }
81
82
    /**
83
     * create a new Groups
84
     *
85
     * @param bool $isNew flag the new objects as "new"?
86
     * @return \XoopsObject Groups
87
     */
88
89
    public function create(
90
        $isNew = true
91
    ) {
92
        $obj = parent::create($isNew);
93
94
        if ($isNew) {
95
            $obj->setNew();
96
        } else {
97
            $obj->unsetNew();
98
        }
99
100
        $obj->helper = $this->helper;
101
102
        return $obj;
103
    }
104
105
    /**
106
     * retrieve a Groups
107
     *
108
     * @param int  $id of the Groups
109
     * @param null $fields
110
     * @return mixed reference to the {@link Groups} object, FALSE if failed
111
     */
112
113
    public function get2(
114
        $id = null,
115
        $fields = null
116
    ) {
117
        $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_groups') . ' WHERE group_id=' . $id;
118
119
        if (!$result = $this->db->query($sql)) {
120
            return false;
121
        }
122
123
        $numrows = $this->db->getRowsNum($result);
124
125
        if (1 === $numrows) {
126
            $yogurt_groups = new Groups();
127
128
            $yogurt_groups->assignVars($this->db->fetchArray($result));
129
130
            return $yogurt_groups;
131
        }
132
133
        return false;
134
    }
135
136
    /**
137
     * insert a new Groups in the database
138
     *
139
     * @param \XoopsObject $xoopsObject   reference to the {@link Groups}
140
     *                                    object
141
     * @param bool         $force
142
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
143
     */
144
145
    public function insert2(
146
        XoopsObject $xoopsObject,
147
        $force = false
148
    ) {
149
        global $xoopsConfig;
150
151
        if (!$xoopsObject instanceof Groups) {
152
            return false;
153
        }
154
155
        if (!$xoopsObject->isDirty()) {
156
            return true;
157
        }
158
159
        if (!$xoopsObject->cleanVars()) {
160
            return false;
161
        }
162
163
        foreach ($xoopsObject->cleanVars as $k => $v) {
164
            ${$k} = $v;
165
        }
166
        //        $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)';
167
        if ($xoopsObject->isNew()) {
168
            // ajout/modification d'un Groups
169
170
            $xoopsObject = new Groups();
171
172
            $format = 'INSERT INTO %s (group_id, owner_uid, group_title, group_desc, group_img)';
173
174
            $format .= 'VALUES (%u, %u, %s, %s, %s)';
175
176
            $sql = \sprintf(
177
                $format,
178
                $this->db->prefix('yogurt_groups'),
179
                $group_id,
180
                $owner_uid,
181
                $this->db->quoteString($group_title),
182
                $this->db->quoteString($group_desc),
183
                $this->db->quoteString($group_img)
184
            );
185
186
            $force = true;
187
        } else {
188
            $format = 'UPDATE %s SET ';
189
190
            $format .= 'group_id=%u, owner_uid=%u, group_title=%s, group_desc=%s, group_img=%s';
191
192
            $format .= ' WHERE group_id = %u';
193
194
            $sql = \sprintf(
195
                $format,
196
                $this->db->prefix('yogurt_groups'),
197
                $group_id,
198
                $owner_uid,
199
                $this->db->quoteString($group_title),
200
                $this->db->quoteString($group_desc),
201
                $this->db->quoteString($group_img),
202
                $group_id
203
            );
204
        }
205
206
        if ($force) {
207
            $result = $this->db->queryF($sql);
208
        } else {
209
            $result = $this->db->query($sql);
210
        }
211
212
        if (!$result) {
213
            return false;
214
        }
215
216
        if (empty($group_id)) {
217
            $group_id = $this->db->getInsertId();
218
        }
219
220
        $xoopsObject->assignVar('group_id', $group_id);
221
222
        return true;
223
    }
224
225
    /**
226
     * delete a Groups from the database
227
     *
228
     * @param \XoopsObject $xoopsObject reference to the Groups to delete
229
     * @param bool         $force
230
     * @return bool FALSE if failed.
231
     */
232
233
    public function delete(
234
        XoopsObject $xoopsObject,
235
        $force = false
236
    ) {
237
        if (!$xoopsObject instanceof Groups) {
238
            return false;
239
        }
240
241
        $sql = \sprintf(
242
            'DELETE FROM %s WHERE group_id = %u',
243
            $this->db->prefix('yogurt_groups'),
244
            $xoopsObject->getVar('group_id')
245
        );
246
247
        if ($force) {
248
            $result = $this->db->queryF($sql);
249
        } else {
250
            $result = $this->db->query($sql);
251
        }
252
253
        if (!$result) {
254
            return false;
255
        }
256
257
        return true;
258
    }
259
260
    /**
261
     * retrieve yogurt_groupss from the database
262
     *
263
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met
264
     * @param bool                                 $id_as_key       use the UID as key for the array?
265
     * @param bool                                 $as_object
266
     * @return array array of {@link Groups} objects
267
     */
268
269
    public function &getObjects(
270
        ?CriteriaElement $criteriaElement = null,
271
        $id_as_key = false,
272
        $as_object = true
273
    ) {
274
        $ret = [];
275
276
        $limit = $start = 0;
277
278
        $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_groups');
279
280
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
281
            $sql .= ' ' . $criteriaElement->renderWhere();
282
283
            if ('' !== $criteriaElement->getSort()) {
284
                $sql .= ' ORDER BY ' . $criteriaElement->getSort() . ' ' . $criteriaElement->getOrder();
285
            }
286
287
            $limit = $criteriaElement->getLimit();
288
289
            $start = $criteriaElement->getStart();
290
        }
291
292
        $result = $this->db->query($sql, $limit, $start);
293
294
        if (!$result) {
295
            return $ret;
296
        }
297
298
        while (false !== ($myrow = $this->db->fetchArray($result))) {
299
            $yogurt_groups = new Groups();
300
301
            $yogurt_groups->assignVars($myrow);
302
303
            if (!$id_as_key) {
304
                $ret[] = &$yogurt_groups;
305
            } else {
306
                $ret[$myrow['group_id']] = &$yogurt_groups;
307
            }
308
309
            unset($yogurt_groups);
310
        }
311
312
        return $ret;
313
    }
314
315
    /**
316
     * retrieve yogurt_groupss from the database
317
     *
318
     * @param \CriteriaElement|\CriteriaCompo|null $criteria  {@link \CriteriaElement} conditions to be met
319
     * @param bool                                 $id_as_key use the UID as key for the array?
320
     * @return array array of {@link Groups} objects
321
     */
322
323
    public function getGroups(
324
        $criteria = null,
325
        $id_as_key = false
326
    ) {
327
        $ret = [];
328
329
        $limit = $start = 0;
330
331
        $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_groups');
332
333
        if (isset($criteria) && $criteria instanceof CriteriaElement) {
334
            $sql .= ' ' . $criteria->renderWhere();
335
336
            if ('' !== $criteria->getSort()) {
337
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
338
            }
339
340
            $limit = $criteria->getLimit();
341
342
            $start = $criteria->getStart();
343
        }
344
345
        $result = $this->db->query($sql, $limit, $start);
346
347
        if (!$result) {
348
            return $ret;
349
        }
350
351
        $i = 0;
352
353
        while (false !== ($myrow = $this->db->fetchArray($result))) {
354
            $ret[$i]['id'] = $myrow['group_id'];
355
356
            $ret[$i]['title'] = $myrow['group_title'];
357
358
            $ret[$i]['img'] = $myrow['group_img'];
359
360
            $ret[$i]['desc'] = $myrow['group_desc'];
361
362
            $ret[$i]['uid'] = $myrow['owner_uid'];
363
364
            $groupid = $myrow['group_id'];
365
366
            $query = 'SELECT COUNT(rel_id) AS grouptotalmembers FROM ' . $GLOBALS['xoopsDB']->prefix('yogurt_relgroupuser') . ' WHERE rel_group_id=' . $groupid . '';
367
368
            $queryresult = $GLOBALS['xoopsDB']->query($query);
369
370
            $row = $GLOBALS['xoopsDB']->fetchArray($queryresult);
371
372
            $grouptotalmembers            = $row['grouptotalmembers'];
373
            $ret[$i]['grouptotalmembers'] = $grouptotalmembers . ' ' . \_MD_YOGURT_GROUPMEMBERS;
374
            $i++;
375
        }
376
377
        return $ret;
378
    }
379
380
    /**
381
     * count yogurt_groupss matching a condition
382
     *
383
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match
384
     * @return int count of yogurt_groupss
385
     */
386
387
    public function getCount(
388
        ?CriteriaElement $criteriaElement = null
389
    ) {
390
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('yogurt_groups');
391
392
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
393
            $sql .= ' ' . $criteriaElement->renderWhere();
394
        }
395
396
        $result = $this->db->query($sql);
397
398
        if (!$result) {
399
            return 0;
400
        }
401
402
        [$count] = $this->db->fetchRow($result);
403
404
        return $count;
405
    }
406
407
    /**
408
     * delete yogurt_groupss matching a set of conditions
409
     *
410
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement}
411
     * @param bool                                 $force
412
     * @param bool                                 $asObject
413
     * @return bool FALSE if deletion failed
414
     */
415
416
    public function deleteAll(
417
        ?CriteriaElement $criteriaElement = null,
418
        $force = true,
419
        $asObject = false
420
    ) {
421
        $sql = 'DELETE FROM ' . $this->db->prefix('yogurt_groups');
422
423
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
424
            $sql .= ' ' . $criteriaElement->renderWhere();
425
        }
426
427
        if (!$result = $this->db->query($sql)) {
428
            return false;
429
        }
430
431
        return true;
432
    }
433
434
    /**
435
     * @param $maxbytes
436
     * @param $xoopsTpl
437
     * @return bool
438
     */
439
440
    public function renderFormSubmit(
441
        $maxbytes,
442
        $xoopsTpl
443
    ) {
444
        $form = new XoopsThemeForm(\_MD_YOGURT_SUBMIT_GROUP, 'form_group', 'submitGroup.php', 'post', true);
445
        $form->setExtra('enctype="multipart/form-data"');
446
447
        $field_url     = new XoopsFormFile(\_MD_YOGURT_GROUP_IMAGE, 'group_img', $maxbytes);
448
        $field_title   = new XoopsFormText(\_MD_YOGURT_GROUP_TITLE, 'group_title', 35, 55);
449
        $field_desc    = new XoopsFormText(\_MD_YOGURT_GROUP_DESC, 'group_desc', 35, 55);
450
        $field_marker  = new XoopsFormHidden('marker', '1');
451
        $buttonSend    = new XoopsFormButton('', 'submit_button', \_MD_YOGURT_UPLOADGROUP, 'submit');
452
        $field_warning = new XoopsFormLabel(\sprintf(\_MD_YOGURT_YOU_CAN_UPLOAD, $maxbytes / 1024));
453
454
        $form->addElement($field_warning);
455
456
        $form->addElement($field_url, true);
457
458
        $form->addElement($field_title);
459
460
        $form->addElement($field_desc);
461
462
        $form->addElement($field_marker);
463
        $form->addElement($buttonSend);
464
        $form->display();
465
466
        return true;
467
    }
468
469
    /**
470
     * @param $group
471
     * @param $maxbytes
472
     * @return bool
473
     */
474
475
    public function renderFormEdit(
476
        $group,
477
        $maxbytes
478
    ) {
479
        $form = new XoopsThemeForm(\_MD_YOGURT_EDIT_GROUP, 'form_editgroup', 'editgroup.php', 'post', true);
480
        $form->setExtra('enctype="multipart/form-data"');
481
482
        $field_groupid = new XoopsFormHidden('group_id', $group->getVar('group_id'));
483
        $field_url     = new XoopsFormFile(\_MD_YOGURT_GROUP_IMAGE, 'img', $maxbytes);
484
        $field_url->setExtra('style="visibility:hidden;"');
485
        $field_title   = new XoopsFormText(\_MD_YOGURT_GROUP_TITLE, 'title', 35, 55, $group->getVar('group_title'));
486
        $field_desc    = new XoopsFormTextArea(\_MD_YOGURT_GROUP_DESC, 'desc', $group->getVar('group_desc'));
487
        $field_marker  = new XoopsFormHidden('marker', '1');
488
        $buttonSend    = new XoopsFormButton('', 'submit_button', \_MD_YOGURT_UPLOADGROUP, 'submit');
489
        $field_warning = new XoopsFormLabel(\sprintf(\_MD_YOGURT_YOU_CAN_UPLOAD, $maxbytes / 1024));
490
491
        $field_oldpicture = new XoopsFormLabel(
492
            \_MD_YOGURT_GROUP_IMAGE, '<img src="' . \XOOPS_UPLOAD_URL . '/' . $group->getVar(
493
                                       'group_img'
494
                                   ) . '">'
495
        );
496
497
        $field_maintainimage = new XoopsFormLabel(
498
            \_MD_YOGURT_MAINTAIN_OLD_IMAGE, "<input type='checkbox' value='1' id='flag_oldimg' name='flag_oldimg' onclick=\"groupImgSwitch(img)\"  checked>"
499
        );
500
501
        $form->addElement($field_oldpicture);
502
503
        $form->addElement($field_maintainimage);
504
505
        $form->addElement($field_warning);
506
507
        $form->addElement($field_url);
508
509
        $form->addElement($field_groupid);
510
511
        $form->addElement($field_title);
512
513
        $form->addElement($field_desc);
514
515
        $form->addElement($field_marker);
516
        $form->addElement($buttonSend);
517
        $form->display();
518
519
        echo "
520
        <!-- Start Form Validation JavaScript //-->
521
<script type='text/javascript'>
522
<!--//
523
function groupImgSwitch(img) {
524
525
var elestyle = xoopsGetElementById(img).style;
526
527
    if (elestyle.visibility == \"hidden\") {
528
        elestyle.visibility = \"visible\";
529
    } else {
530
        elestyle.visibility = \"hidden\";
531
    }
532
533
534
}
535
//--></script>
536
<!-- End Form Validation JavaScript //-->
537
        ";
538
539
        return true;
540
    }
541
542
    /**
543
     * @param string $group_title
544
     * @param string $group_desc
545
     * @param string $group_img
546
     * @param string $path_upload
547
     * @param int    $maxfilebytes
548
     * @param int    $maxfilewidth
549
     * @param int    $maxfileheight
550
     * @param int    $change_img
551
     * @param string $group
552
     * @return bool
553
     */
554
555
    public function receiveGroup(
556
        $group_title,
557
        $group_desc,
558
        $group_img,
559
        $path_upload,
560
        $maxfilebytes,
561
        $maxfilewidth,
562
        $maxfileheight,
563
        $change_img = 1,
564
        $group = ''
565
        //        $pictwidth,
566
        //        $pictheight,
567
        //        $thumbwidth,
568
        //        $thumbheight
569
    )
570
    {
571
        global $xoopsUser, $xoopsDB, $_POST, $_FILES;
572
573
        //search logged user id
574
575
        $uid = $xoopsUser->getVar('uid');
576
577
        if ('' === $group || Groups::class !== \get_class($group)) {
0 ignored issues
show
$group of type string is incompatible with the type object expected by parameter $object of get_class(). ( Ignorable by Annotation )

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

577
        if ('' === $group || Groups::class !== \get_class(/** @scrutinizer ignore-type */ $group)) {
Loading history...
578
            $group = $this->create();
579
        } else {
580
            $group->unsetNew();
581
        }
582
583
        $helper = Helper::getInstance();
584
585
        $pictwidth = $helper->getConfig('resized_width');
586
587
        $pictheight = $helper->getConfig('resized_height');
588
589
        $thumbwidth = $helper->getConfig('thumb_width');
590
591
        $thumbheight = $helper->getConfig('thumb_height');
592
593
        if (1 === $change_img) {
594
            // mimetypes and settings put this in admin part later
595
596
            $allowed_mimetypes = Helper::getInstance()->getConfig(
597
                'mimetypes'
598
            );
599
600
            $maxfilesize = $maxfilebytes;
601
602
            $uploadDir = \XOOPS_UPLOAD_PATH . '/yogurt/groups/';
603
604
            // create the object to upload
605
606
            $uploader = new XoopsMediaUploader(
607
                $uploadDir, $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight
608
            );
609
610
            // fetch the media
611
612
            if ($uploader->fetchMedia($_POST['xoops_upload_file'][0])) {
613
                //lets create a name for it
614
615
                $uploader->setPrefix('group_' . $uid . '_');
616
617
                //now let s upload the file
618
619
                if (!$uploader->upload()) {
620
                    // if there are errors lets return them
621
622
                    echo '<div style="color:#FF0000; background-color:#FFEAF4; border-color:#FF0000; border-width:thick; border-style:solid; text-align:center"><p>' . $uploader->getErrors() . '</p></div>';
623
624
                    return false;
625
                }
626
627
                // now let s create a new object picture and set its variables
628
629
                $savedFilename = $uploader->getSavedFileName();
630
631
                $group->setVar('group_img', $savedFilename);
632
633
                $imageMimetype = $uploader->getMediaType();
634
635
                $group->setVar('group_img', $savedFilename);
636
637
                $maxWidth_grouplogo = Helper::getInstance()->getConfig('groupslogo_width');
638
639
                $maxHeight_grouplogo = Helper::getInstance()->getConfig('groupslogo_height');
640
641
                $resizer = new Common\Resizer();
642
643
                $resizer->sourceFile = $uploadDir . $savedFilename;
644
645
                $resizer->endFile = $uploadDir . $savedFilename;
646
647
                $resizer->imageMimetype = $imageMimetype;
648
649
                $resizer->maxWidth = $maxWidth_grouplogo;
650
651
                $resizer->maxHeight = $maxHeight_grouplogo;
652
653
                $result = $resizer->resizeImage();
654
655
                $maxWidth_grouplogo = Helper::getInstance()->getConfig('thumb_width');
656
657
                $maxHeight_grouplogo = Helper::getInstance()->getConfig('thumb_height');
658
659
                $resizer->endFile = $uploadDir . '/thumb_' . $savedFilename;
660
661
                $resizer->imageMimetype = $imageMimetype;
662
663
                $resizer->maxWidth = $maxWidth_grouplogo;
664
665
                $resizer->maxHeight = $maxHeight_grouplogo;
666
667
                $result = $resizer->resizeImage();
668
669
                $maxWidth_grouplogo = Helper::getInstance()->getConfig('resized_width');
670
671
                $maxHeight_grouplogo = Helper::getInstance()->getConfig('resized_height');
672
673
                $resizer->endFile = $uploadDir . '/resized_' . $savedFilename;
674
675
                $resizer->imageMimetype = $imageMimetype;
676
677
                $resizer->maxWidth = $maxWidth_grouplogo;
678
679
                $resizer->maxHeight = $maxHeight_grouplogo;
680
681
                $result = $resizer->resizeImage();
682
            } else {
683
                echo '<div style="color:#FF0000; background-color:#FFEAF4; border-color:#FF0000; border-width:thick; border-style:solid; text-align:center"><p>' . $uploader->getErrors() . '</p></div>';
684
685
                return false;
686
            }
687
        }
688
689
        $group->setVar('group_title', $group_title);
690
691
        $group->setVar('group_desc', $group_desc);
692
693
        $group->setVar('owner_uid', $uid);
694
695
        $this->insert($group);
0 ignored issues
show
It seems like $group can also be of type string; however, parameter $object of XoopsPersistableObjectHandler::insert() does only seem to accept XoopsObject, 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

695
        $this->insert(/** @scrutinizer ignore-type */ $group);
Loading history...
696
697
        return true;
698
    }
699
}
700