Passed
Pull Request — master (#81)
by Michael
03:26
created

Friendrequest::getAllFriendrequests()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 49
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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