Passed
Push — master ( 4761c2...696f77 )
by
unknown
05:50 queued 19s
created

class/FriendshipHandler.php (2 issues)

Labels
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Suico;
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         suico
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          Bruno Barthez, Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
23
 */
24
25
use Criteria;
26
use CriteriaCompo;
27
use CriteriaElement;
28
use XoopsDatabase;
29
use XoopsFormButton;
30
use XoopsFormHidden;
31
use XoopsFormLabel;
32
use XoopsFormRadio;
33
use XoopsFormRadioYN;
34
use XoopsObject;
35
use XoopsPersistableObjectHandler;
36
use XoopsThemeForm;
37
38
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
39
/**
40
 * Includes of form objects and uploader
41
 */
42
require_once XOOPS_ROOT_PATH . '/class/uploader.php';
43
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
44
require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
45
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
46
47
/**
48
 * suico_friendshiphandler class.
49
 * This class provides simple mechanism for Friendship object
50
 */
51
class FriendshipHandler extends XoopsPersistableObjectHandler
52
{
53
    public $helper;
54
    public $isAdmin;
55
56
    /**
57
     * Constructor
58
     * @param \XoopsDatabase|null             $xoopsDatabase
59
     * @param \XoopsModules\Suico\Helper|null $helper
60
     */
61
    public function __construct(
62
        ?XoopsDatabase $xoopsDatabase = null,
63
        $helper = null
64
    ) {
65
        /** @var \XoopsModules\Suico\Helper $this ->helper */
66
        if (null === $helper) {
67
            $this->helper = Helper::getInstance();
0 ignored issues
show
The property helper does not seem to exist on XoopsModules\Suico\Helper.
Loading history...
68
        } else {
69
            $this->helper = $helper;
70
        }
71
        $isAdmin = $this->helper->isUserAdmin();
72
        parent::__construct($xoopsDatabase, 'suico_friendships', Friendship::class, 'friendship_id', 'friendship_id');
73
    }
74
75
    /**
76
     * create a new Groups
77
     *
78
     * @param bool $isNew flag the new objects as "new"?
79
     * @return \XoopsObject Groups
80
     */
81
    public function create(
82
        $isNew = true
83
    ) {
84
        $obj = parent::create($isNew);
85
        if ($isNew) {
86
            $obj->setNew();
87
        } else {
88
            $obj->unsetNew();
89
        }
90
        $obj->helper = $this->helper;
91
        return $obj;
92
    }
93
94
    /**
95
     * retrieve a Friendship
96
     *
97
     * @param int  $id of the Friendship
98
     * @param null $fields
99
     * @return mixed reference to the {@link Friendship} object, FALSE if failed
100
     */
101
    public function get2(
102
        $id = null,
103
        $fields = null
104
    ) {
105
        $sql = 'SELECT * FROM ' . $this->db->prefix('suico_friendships') . ' WHERE friendship_id=' . $id;
106
        if (!$result = $this->db->query($sql)) {
107
            return false;
108
        }
109
        $numrows = $this->db->getRowsNum($result);
110
        if (1 === $numrows) {
111
            $suico_friendship = new Friendship();
112
            $suico_friendship->assignVars($this->db->fetchArray($result));
113
            return $suico_friendship;
114
        }
115
        return false;
116
    }
117
118
    /**
119
     * insert a new Friendship in the database
120
     *
121
     * @param \XoopsObject $xoopsObject       reference to the {@link Friendship}
122
     *                                        object
123
     * @param bool         $force
124
     * @return bool FALSE if failed, TRUE if already present and unchanged or successful
125
     */
126
    public function insert2(
127
        XoopsObject $xoopsObject,
128
        $force = false
129
    ) {
130
        global $xoopsConfig;
131
        if (!$xoopsObject instanceof Friendship) {
132
            return false;
133
        }
134
        if (!$xoopsObject->isDirty()) {
135
            return true;
136
        }
137
        if (!$xoopsObject->cleanVars()) {
138
            return false;
139
        }
140
        $friendship_id = $friend1_uid = $friend2_uid = $level = $hot = $trust = $cool = $fan = '';
141
        foreach ($xoopsObject->cleanVars as $k => $v) {
142
            ${$k} = $v;
143
        }
144
        //        $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)';
145
        if ($xoopsObject->isNew()) {
146
            // ajout/modification d'un Friendship
147
            $xoopsObject = new Friendship();
148
            $format      = 'INSERT INTO %s (friendship_id, friend1_uid, friend2_uid, LEVEL, hot, trust, cool, fan)';
149
            $format      .= 'VALUES (%u, %u, %u, %u, %u, %u, %u, %u)';
150
            $sql         = \sprintf(
151
                $format,
152
                $this->db->prefix('suico_friendships'),
153
                $friendship_id,
154
                $friend1_uid,
155
                $friend2_uid,
156
                $level,
157
                $hot,
158
                $trust,
159
                $cool,
160
                $fan
161
            );
162
            $force       = true;
163
        } else {
164
            $format = 'UPDATE %s SET ';
165
            $format .= 'friendship_id=%u, friend1_uid=%u, friend2_uid=%u, level=%u, hot=%u, trust=%u, cool=%u, fan=%u';
166
            $format .= ' WHERE friendship_id = %u';
167
            $sql    = \sprintf(
168
                $format,
169
                $this->db->prefix('suico_friendships'),
170
                $friendship_id,
171
                $friend1_uid,
172
                $friend2_uid,
173
                $level,
174
                $hot,
175
                $trust,
176
                $cool,
177
                $fan,
178
                $friendship_id
179
            );
180
        }
181
        if ($force) {
182
            $result = $this->db->queryF($sql);
183
        } else {
184
            $result = $this->db->query($sql);
185
        }
186
        if (!$result) {
187
            return false;
188
        }
189
        if (empty($friendship_id)) {
190
            $friendship_id = $this->db->getInsertId();
191
        }
192
        $xoopsObject->assignVar('friendship_id', $friendship_id);
193
        return true;
194
    }
195
196
    /**
197
     * delete a Friendship from the database
198
     *
199
     * @param \XoopsObject $xoopsObject reference to the Friendship to delete
200
     * @param bool         $force
201
     * @return bool FALSE if failed.
202
     */
203
    public function delete(
204
        XoopsObject $xoopsObject,
205
        $force = false
206
    ) {
207
        if (!$xoopsObject instanceof Friendship) {
208
            return false;
209
        }
210
        $sql = \sprintf(
211
            'DELETE FROM %s WHERE friendship_id = %u',
212
            $this->db->prefix('suico_friendships'),
213
            $xoopsObject->getVar('friendship_id')
214
        );
215
        if ($force) {
216
            $result = $this->db->queryF($sql);
217
        } else {
218
            $result = $this->db->query($sql);
219
        }
220
        if (!$result) {
221
            return false;
222
        }
223
        return true;
224
    }
225
226
    /**
227
     * retrieve suico_friendships from the database
228
     *
229
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} conditions to be met
230
     * @param bool                                 $id_as_key       use the UID as key for the array?
231
     * @param bool                                 $as_object
232
     * @return array array of {@link Friendship} objects
233
     */
234
    public function &getObjects(
235
        ?CriteriaElement $criteriaElement = null,
236
        $id_as_key = false,
237
        $as_object = true
238
    ) {
239
        $ret   = [];
240
        $limit = $start = 0;
241
        $sql   = 'SELECT * FROM ' . $this->db->prefix('suico_friendships');
242
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
243
            $sql .= ' ' . $criteriaElement->renderWhere();
0 ignored issues
show
The method renderWhere() does not exist on CriteriaElement. Did you maybe mean render()? ( Ignorable by Annotation )

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

243
            $sql .= ' ' . $criteriaElement->/** @scrutinizer ignore-call */ renderWhere();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
244
            if ('' !== $criteriaElement->getSort()) {
245
                $sql .= ' ORDER BY ' . $criteriaElement->getSort() . ' ' . $criteriaElement->getOrder();
246
            }
247
            $limit = $criteriaElement->getLimit();
248
            $start = $criteriaElement->getStart();
249
        }
250
        $result = $this->db->query($sql, $limit, $start);
251
        if (!$result) {
252
            return $ret;
253
        }
254
        while (false !== ($myrow = $this->db->fetchArray($result))) {
255
            $suico_friendship = new Friendship();
256
            $suico_friendship->assignVars($myrow);
257
            if (!$id_as_key) {
258
                $ret[] = &$suico_friendship;
259
            } else {
260
                $ret[$myrow['friendship_id']] = &$suico_friendship;
261
            }
262
            unset($suico_friendship);
263
        }
264
        return $ret;
265
    }
266
267
    /**
268
     * count suico_friendships matching a condition
269
     *
270
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement} to match
271
     * @return int count of suico_friendships
272
     */
273
    public function getCount(
274
        ?CriteriaElement $criteriaElement = null
275
    ) {
276
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_friendships');
277
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
278
            $sql .= ' ' . $criteriaElement->renderWhere();
279
        }
280
        $result = $this->db->query($sql);
281
        if (!$result) {
282
            return 0;
283
        }
284
        [$count] = $this->db->fetchRow($result);
285
        return (int)$count;
286
    }
287
288
    /**
289
     * delete suico_friendships matching a set of conditions
290
     *
291
     * @param \CriteriaElement|\CriteriaCompo|null $criteriaElement {@link \CriteriaElement}
292
     * @param bool                                 $force
293
     * @param bool                                 $asObject
294
     * @return bool FALSE if deletion failed
295
     */
296
    public function deleteAll(
297
        ?CriteriaElement $criteriaElement = null,
298
        $force = true,
299
        $asObject = false
300
    ) {
301
        $sql = 'DELETE FROM ' . $this->db->prefix('suico_friendships');
302
        if (isset($criteriaElement) && $criteriaElement instanceof CriteriaElement) {
303
            $sql .= ' ' . $criteriaElement->renderWhere();
304
        }
305
        if (!$result = $this->db->query($sql)) {
306
            return false;
307
        }
308
        return true;
309
    }
310
311
    /**
312
     * @param      $countFriends
313
     * @param null $criteria
314
     * @param int  $shuffle
315
     * @return array
316
     */
317
    public function getFriends(
318
        $countFriends,
319
        $criteria = null,
320
        $shuffle = 1
321
    ) {
322
        $ret   = [];
323
        $limit = $start = 0;
324
        $sql   = 'SELECT uname, user_avatar, friend2_uid FROM ' . $this->db->prefix(
325
                'suico_friendships'
326
            ) . ', ' . $this->db->prefix(
327
                'users'
328
            );
329
        if (isset($criteria) && $criteria instanceof CriteriaElement) {
330
            $sql .= ' ' . $criteria->renderWhere();
331
            //attention here this is kind of a hack
332
            $sql .= ' AND uid = friend2_uid ';
333
            if ('' !== $criteria->getSort()) {
334
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
335
            }
336
            $limit  = $criteria->getLimit();
337
            $start  = $criteria->getStart();
338
            $result = $this->db->query($sql, $limit, $start);
339
            $vetor  = [];
340
            $i      = 0;
341
            while (false !== ($myrow = $this->db->fetchArray($result))) {
342
                $vetor[$i]['uid']         = $myrow['friend2_uid'];
343
                $vetor[$i]['uname']       = $myrow['uname'];
344
                $vetor[$i]['user_avatar'] = $myrow['user_avatar'];
345
                $i++;
346
            }
347
            if (1 === $shuffle) {
348
                \shuffle($vetor);
349
                $vetor = \array_slice($vetor, 0, $countFriends);
350
            }
351
            return $vetor;
352
        }
353
    }
354
355
    /**
356
     * @param      $countFriends
357
     * @param null $criteria
358
     * @param int  $shuffle
359
     * @return array
360
     */
361
    public function getFans(
362
        $countFriends,
363
        $criteria = null,
364
        $shuffle = 1
365
    ) {
366
        $ret   = [];
367
        $limit = $start = 0;
368
        $sql   = 'SELECT uname, user_avatar, friend1_uid FROM ' . $this->db->prefix(
369
                'suico_friendships'
370
            ) . ', ' . $this->db->prefix(
371
                'users'
372
            );
373
        if (isset($criteria) && $criteria instanceof CriteriaElement) {
374
            $sql .= ' ' . $criteria->renderWhere();
375
            //attention here this is kind of a hack
376
            $sql .= ' AND uid = friend1_uid ';
377
            if ('' !== $criteria->getSort()) {
378
                $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder();
379
            }
380
            $limit  = $criteria->getLimit();
381
            $start  = $criteria->getStart();
382
            $result = $this->db->query($sql, $limit, $start);
383
            $vetor  = [];
384
            $i      = 0;
385
            while (false !== ($myrow = $this->db->fetchArray($result))) {
386
                $vetor[$i]['uid']         = $myrow['friend1_uid'];
387
                $vetor[$i]['uname']       = $myrow['uname'];
388
                $vetor[$i]['user_avatar'] = $myrow['user_avatar'];
389
                $i++;
390
            }
391
            if (1 === $shuffle) {
392
                \shuffle($vetor);
393
                $vetor = \array_slice($vetor, 0, $countFriends);
394
            }
395
            return $vetor;
396
        }
397
    }
398
399
    /**
400
     * @param $friend
401
     */
402
    public function renderFormSubmit($friend)
403
    {
404
        global $xoopsUser;
405
        /**
406
         * criteria fetch friendship to be edited
407
         */
408
        $criteria_friend1    = new Criteria(
409
            'friend1_uid', $xoopsUser->getVar(
410
            'uid'
411
        )
412
        );
413
        $field_friend_fan    = $field_friend_friendly = $field_friend_funny = $field_friend_cool = '';
414
        $criteria_friend2    = new Criteria('friend2_uid', $friend->getVar('uid'));
415
        $criteria_friendship = new CriteriaCompo($criteria_friend1);
416
        $criteria_friendship->add($criteria_friend2);
417
        $friendships = $this->getObjects($criteria_friendship);
418
        $friendship  = $friendships[0];
419
        $form        = new XoopsThemeForm(
420
            \_MD_SUICO_EDIT_FRIENDSHIP, 'form_editfriendship', 'editfriendship.php', 'post', true
421
        );
422
        //$field_friend_avatar      = new XoopsFormLabel(_MD_SUICO_PHOTO, "<img src=../../uploads/".$friend->getVar('user_avatar').">");
423
        if ('avatars/blank.gif' === $friend->getVar(
424
                'user_avatar'
425
            )) {
426
            $field_friend_avatar = new XoopsFormLabel(\_MD_SUICO_PHOTO, '<img src=assets/images/noavatar.gif>');
427
        } else {
428
            $field_friend_avatar = new XoopsFormLabel(
429
                \_MD_SUICO_PHOTO, '<img src=../../uploads/' . $friend->getVar(
430
                                    'user_avatar'
431
                                ) . '>'
432
            );
433
        }
434
        $field_friend_name = new XoopsFormLabel(\_MD_SUICO_FRIENDNAME, $friend->getVar('uname'));
435
        if (1 === $this->helper->getConfig('allow_friendshiplevel')) {
436
            $field_friend_level = new XoopsFormRadio(\_MD_SUICO_LEVEL, 'level', $friendship->getVar('level'), '<br>');
437
            $field_friend_level->addOption('1', \_MD_SUICO_UNKNOWN_ACCEPTED);
438
            $field_friend_level->addOption('3', \_MD_SUICO_AQUAITANCE);
439
            $field_friend_level->addOption('5', \_MD_SUICO_FRIEND);
440
            $field_friend_level->addOption('7', \_MD_SUICO_BESTFRIEND);
441
        }
442
        if (1 === $this->helper->getConfig('allow_fanssevaluation')) {
443
            $field_friend_fan      = new XoopsFormRadioYN(
444
                \_MD_SUICO_FAN, 'fan', $friendship->getVar(
445
                'fan'
446
            ), '<img src="assets/images/fans.gif" alt="' . \_YES . '" title="' . \_YES . '">', '<img src="assets/images/fansbw.gif" alt="' . \_NO . '" title="' . \_NO . '">'
447
            );
448
            $field_friend_friendly = new XoopsFormRadio(\_MD_SUICO_FRIENDLY, 'hot', $friendship->getVar('hot'));
449
            $field_friend_friendly->addOption(
450
                '1',
451
                '<img src="assets/images/friendlya.gif" alt="' . \_MD_SUICO_FRIENDLYNO . '" title="' . \_MD_SUICO_FRIENDLYNO . '">'
452
            );
453
            $field_friend_friendly->addOption(
454
                '2',
455
                '<img src="assets/images/friendlyb.gif" alt="' . \_MD_SUICO_FRIENDLYYES . '" title="' . \_MD_SUICO_FRIENDLYYES . '">'
456
            );
457
            $field_friend_friendly->addOption(
458
                '3',
459
                '<img src="assets/images/friendlyc.gif" alt="' . \_MD_SUICO_FRIENDLYALOT . '" title="' . \_MD_SUICO_FRIENDLYALOT . '">'
460
            );
461
            $field_friend_funny = new XoopsFormRadio(\_MD_SUICO_FUNNY, 'trust', $friendship->getVar('trust'));
462
            $field_friend_funny->addOption(
463
                '1',
464
                '<img src="assets/images/funnya.gif" alt="' . \_MD_SUICO_FUNNYNO . '" title="' . \_MD_SUICO_FUNNYNO . '">'
465
            );
466
            $field_friend_funny->addOption(
467
                '2',
468
                '<img src="assets/images/funnyb.gif" alt="' . \_MD_SUICO_FUNNYYES . '" title="' . \_MD_SUICO_FUNNYYES . '">'
469
            );
470
            $field_friend_funny->addOption(
471
                '3',
472
                '<img src="assets/images/funnyc.gif" alt="' . \_MD_SUICO_FUNNYALOT . '" title="' . \_MD_SUICO_FUNNYALOT . '">'
473
            );
474
            $field_friend_cool = new XoopsFormRadio(\_MD_SUICO_COOL, 'cool', $friendship->getVar('cool'));
475
            $field_friend_cool->addOption(
476
                '1',
477
                '<img src="assets/images/coola.gif" alt="' . \_MD_SUICO_COOLNO . '" title="' . \_MD_SUICO_COOLNO . '">'
478
            );
479
            $field_friend_cool->addOption(
480
                '2',
481
                '<img src="assets/images/coolb.gif" alt="' . \_MD_SUICO_COOLYES . '" title="' . \_MD_SUICO_COOLYES . '">'
482
            );
483
            $field_friend_cool->addOption(
484
                '3',
485
                '<img src="assets/images/coolc.gif" alt="' . \_MD_SUICO_COOLALOT . '" title="' . \_MD_SUICO_COOLALOT . '">'
486
            );
487
        }
488
        $form->setExtra('enctype="multipart/form-data"');
489
        $buttonSend                 = new XoopsFormButton('', 'submit_button', \_MD_SUICO_UPDATEFRIEND, 'submit');
490
        $field_friend_friendid      = new XoopsFormHidden('friend_uid', $friend->getVar('uid'));
491
        $field_friend_marker        = new XoopsFormHidden('marker', '1');
492
        $field_friend_friendshio_id = new XoopsFormHidden('friendship_id', $friendship->getVar('friendship_id'));
493
        $form->addElement($field_friend_friendid);
494
        $form->addElement($field_friend_friendshio_id);
495
        $form->addElement($field_friend_marker);
496
        $form->addElement($field_friend_avatar);
497
        $form->addElement($field_friend_name);
498
        $form->addElement($field_friend_level);
499
        $form->addElement($field_friend_fan);
500
        $form->addElement($field_friend_friendly);
501
        $form->addElement($field_friend_funny);
502
        $form->addElement($field_friend_cool);
503
        $form->addElement($buttonSend);
504
        $form->display();
505
    }
506
507
    /**
508
     * Get the averages of each evaluation hot funny etc...
509
     *
510
     * @param int $user_uid
511
     * @return array with averages
512
     */
513
    public function getMoyennes(
514
        $user_uid
515
    ) {
516
        global $xoopsUser;
517
        $vetor               = [];
518
        $vetor['mediahot']   = 0;
519
        $vetor['mediatrust'] = 0;
520
        $vetor['mediacool']  = 0;
521
        $vetor['sumfan']     = 0;
522
        //Calculating avg(hot)
523
        $sql    = 'SELECT friend2_uid, Avg(hot) AS mediahot FROM ' . $this->db->prefix(
524
                'suico_friendships'
525
            );
526
        $sql    .= ' WHERE  (hot>0) GROUP BY friend2_uid HAVING (friend2_uid=' . $user_uid . ') ';
527
        $result = $this->db->query($sql);
528
        while (false !== ($myrow = $this->db->fetchArray($result))) {
529
            $vetor['mediahot'] = $myrow['mediahot'] * 16;
530
        }
531
        //Calculating avg(trust)
532
        $sql    = 'SELECT friend2_uid, Avg(trust) AS mediatrust FROM ' . $this->db->prefix(
533
                'suico_friendships'
534
            );
535
        $sql    .= ' WHERE  (trust>0) GROUP BY friend2_uid HAVING (friend2_uid=' . $user_uid . ') ';
536
        $result = $this->db->query($sql);
537
        while (false !== ($myrow = $this->db->fetchArray($result))) {
538
            $vetor['mediatrust'] = $myrow['mediatrust'] * 16;
539
        }
540
        //Calculating avg(cool)
541
        $sql    = 'SELECT friend2_uid, Avg(cool) AS mediacool FROM ' . $this->db->prefix(
542
                'suico_friendships'
543
            );
544
        $sql    .= ' WHERE  (cool>0) GROUP BY friend2_uid HAVING (friend2_uid=' . $user_uid . ') ';
545
        $result = $this->db->query($sql);
546
        while (false !== ($myrow = $this->db->fetchArray($result))) {
547
            $vetor['mediacool'] = $myrow['mediacool'] * 16;
548
        }
549
        //Calculating sum(fans)
550
        $sql    = 'SELECT friend2_uid, Sum(fan) AS sumfan FROM ' . $this->db->prefix(
551
                'suico_friendships'
552
            );
553
        $sql    .= ' GROUP BY friend2_uid HAVING (friend2_uid=' . $user_uid . ') ';
554
        $result = $this->db->query($sql);
555
        while (false !== ($myrow = $this->db->fetchArray($result))) {
556
            $vetor['sumfan'] = $myrow['sumfan'];
557
        }
558
        return $vetor;
559
    }
560
}
561