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

Friendrequest::getAllFriendrequests()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 25
c 0
b 0
f 0
dl 0
loc 39
rs 8.0555
cc 9
nc 12
nop 6
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
 * Module: Yogurt
18
 *
19
 * @category        Module
20
 * @package         yogurt
21
 * @author          Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
22
 * @copyright       {@link https://xoops.org/ XOOPS Project}
23
 * @license         GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
24
 */
25
26
// Friendrequest.php,v 1
27
//  ---------------------------------------------------------------- //
28
// Author: Bruno Barthez                                               //
29
// ----------------------------------------------------------------- //
30
31
use Xmf\Module\Helper\Permission;
32
use XoopsDatabaseFactory;
33
use XoopsObject;
34
35
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
36
37
/**
38
 * Friendrequest class.
39
 * $this class is responsible for providing data access mechanisms to the data source
40
 * of XOOPS user class objects.
41
 */
42
class Friendrequest extends XoopsObject
43
{
44
    public $db;
45
    public $helper;
46
    public $permHelper;
47
48
    // constructor
49
50
    /**
51
     * Friendrequest constructor.
52
     * @param null $id
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $id is correct as it would always require null to be passed?
Loading history...
53
     */
54
    public function __construct($id = null)
55
    {
56
        /** @var Helper $helper */
57
        $this->helper     = Helper::getInstance();
58
        $this->permHelper = new Permission();
59
        $this->db         = XoopsDatabaseFactory::getDatabaseConnection();
60
        $this->initVar('friendreq_id', \XOBJ_DTYPE_INT, null, false, 10);
61
        $this->initVar('friendrequester_uid', \XOBJ_DTYPE_INT, null, false, 10);
62
        $this->initVar('friendrequestto_uid', \XOBJ_DTYPE_INT, null, false, 10);
63
        $this->initVar('date_created', \XOBJ_DTYPE_INT, 0, false);
64
        if (!empty($id)) {
65
            if (\is_array($id)) {
66
                $this->assignVars($id);
67
            } else {
68
                $this->load((int)$id);
69
            }
70
        } else {
71
            $this->setNew();
72
        }
73
    }
74
75
    /**
76
     * @param $id
77
     */
78
    public function load($id)
79
    {
80
        $sql   = 'SELECT * FROM ' . $this->db->prefix('yogurt_friendrequests') . ' WHERE friendreq_id=' . $id;
81
        $myrow = $this->db->fetchArray($this->db->query($sql));
82
        $this->assignVars($myrow);
83
        if (!$myrow) {
84
            $this->setNew();
85
        }
86
    }
87
88
    /**
89
     * @param array  $criteria
90
     * @param bool   $asobject
91
     * @param string $sort
92
     * @param string $order
93
     * @param int    $limit
94
     * @param int    $start
95
     * @return array
96
     */
97
    public function getAllFriendrequests(
98
        $criteria = [],
99
        $asobject = false,
100
        $sort = 'friendreq_id',
101
        $order = 'ASC',
102
        $limit = 0,
103
        $start = 0
104
    ) {
105
        $db         = XoopsDatabaseFactory::getDatabaseConnection();
106
        $ret        = [];
107
        $whereQuery = '';
108
        if (\is_array($criteria) && \count($criteria) > 0) {
109
            $whereQuery = ' WHERE';
110
            foreach ($criteria as $c) {
111
                $whereQuery .= " ${c} AND";
112
            }
113
            $whereQuery = mb_substr($whereQuery, 0, -4);
114
        } elseif (!\is_array($criteria) && $criteria) {
115
            $whereQuery = ' WHERE ' . $criteria;
116
        }
117
        if (!$asobject) {
118
            $sql    = 'SELECT friendreq_id FROM ' . $db->prefix(
119
                    'yogurt_friendrequests'
120
                ) . "${whereQuery} ORDER BY ${sort} ${order}";
121
            $result = $db->query($sql, $limit, $start);
122
            while (false !== ($myrow = $db->fetchArray($result))) {
123
                $ret[] = $myrow['yogurt_friendrequest_id'];
124
            }
125
        } else {
126
            $sql    = 'SELECT * FROM ' . $db->prefix(
127
                    'yogurt_friendrequests'
128
                ) . "${whereQuery} ORDER BY ${sort} ${order}";
129
            $result = $db->query($sql, $limit, $start);
130
            while (false !== ($myrow = $db->fetchArray($result))) {
131
                $ret[] = new self($myrow);
132
            }
133
        }
134
135
        return $ret;
136
    }
137
138
    /**
139
     * Get form
140
     *
141
     * @return \XoopsModules\Yogurt\Form\FriendrequestForm
142
     */
143
    public function getForm()
144
    {
145
        return new Form\FriendrequestForm($this);
146
    }
147
148
    /**
149
     * @return array|null
150
     */
151
    public function getGroupsRead()
152
    {
153
        //$permHelper = new \Xmf\Module\Helper\Permission();
154
        return $this->permHelper->getGroupsForItem(
155
            'sbcolumns_read',
156
            $this->getVar('friendreq_id')
157
        );
158
    }
159
160
    /**
161
     * @return array|null
162
     */
163
    public function getGroupsSubmit()
164
    {
165
        //$permHelper = new \Xmf\Module\Helper\Permission();
166
        return $this->permHelper->getGroupsForItem(
167
            'sbcolumns_submit',
168
            $this->getVar('friendreq_id')
169
        );
170
    }
171
172
    /**
173
     * @return array|null
174
     */
175
    public function getGroupsModeration()
176
    {
177
        //$permHelper = new \Xmf\Module\Helper\Permission();
178
        return $this->permHelper->getGroupsForItem(
179
            'sbcolumns_moderation',
180
            $this->getVar('friendreq_id')
181
        );
182
    }
183
}
184