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