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

Friendship::getAllyogurt_friendships()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
c 0
b 0
f 0
dl 0
loc 37
rs 8.0555
cc 9
nc 12
nop 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A Friendship::load() 0 10 2
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
 * Includes of form objects and uploader
32
 */
33
require_once XOOPS_ROOT_PATH . '/class/uploader.php';
34
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
35
require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
36
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
37
38
/**
39
 * Friendship class.
40
 * $this class is responsible for providing data access mechanisms to the data source
41
 * of XOOPS user class objects.
42
 */
43
class Friendship extends XoopsObject
44
{
45
    public $db;
46
47
    public $helper;
48
49
    public $permHelper;
50
51
    // constructor
52
53
    /**
54
     * Friendship constructor.
55
     * @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...
56
     */
57
58
    public function __construct($id = null)
59
    {
60
        parent::__construct();
61
62
        /** @var Helper $helper */
63
64
        $this->helper = Helper::getInstance();
65
66
        $this->permHelper = new Permission();
67
68
        $this->db = XoopsDatabaseFactory::getDatabaseConnection();
69
70
        $this->initVar('friendship_id', \XOBJ_DTYPE_INT, null, false, 10);
71
72
        $this->initVar('friend1_uid', \XOBJ_DTYPE_INT, null, false, 10);
73
74
        $this->initVar('friend2_uid', \XOBJ_DTYPE_INT, null, false, 10);
75
76
        $this->initVar('level', \XOBJ_DTYPE_INT, null, false, 10);
77
78
        $this->initVar('hot', \XOBJ_DTYPE_INT, null, false, 10);
79
80
        $this->initVar('trust', \XOBJ_DTYPE_INT, null, false, 10);
81
82
        $this->initVar('cool', \XOBJ_DTYPE_INT, null, false, 10);
83
84
        $this->initVar('fan', \XOBJ_DTYPE_INT, null, false, 10);
85
86
        $this->initVar('date_created', \XOBJ_DTYPE_INT, 0, false);
87
88
        $this->initVar('date_updated', \XOBJ_DTYPE_INT, 0, false);
89
90
        if (!empty($id)) {
91
            if (\is_array($id)) {
92
                $this->assignVars($id);
93
            } else {
94
                $this->load((int)$id);
95
            }
96
        } else {
97
            $this->setNew();
98
        }
99
    }
100
101
    /**
102
     * @param $id
103
     */
104
105
    public function load($id)
106
    {
107
        $sql = 'SELECT * FROM ' . $this->db->prefix('yogurt_friendships') . ' WHERE friendship_id=' . $id;
108
109
        $myrow = $this->db->fetchArray($this->db->query($sql));
110
111
        $this->assignVars($myrow);
112
113
        if (!$myrow) {
114
            $this->setNew();
115
        }
116
    }
117
118
    /**
119
     * @param array  $criteria
120
     * @param bool   $asobject
121
     * @param string $sort
122
     * @param string $order
123
     * @param int    $limit
124
     * @param int    $start
125
     * @return array
126
     */
127
128
    public function getAllFriendships(
129
        $criteria = [],
130
        $asobject = false,
131
        $sort = 'friendship_id',
132
        $order = 'ASC',
133
        $limit = 0,
134
        $start = 0
135
    ) {
136
        $db = XoopsDatabaseFactory::getDatabaseConnection();
137
138
        $ret = [];
139
140
        $whereQuery = '';
141
142
        if (\is_array($criteria) && \count($criteria) > 0) {
143
            $whereQuery = ' WHERE';
144
145
            foreach ($criteria as $c) {
146
                $whereQuery .= " ${c} AND";
147
            }
148
149
            $whereQuery = mb_substr($whereQuery, 0, -4);
150
        } elseif (!\is_array($criteria) && $criteria) {
151
            $whereQuery = ' WHERE ' . $criteria;
152
        }
153
154
        if (!$asobject) {
155
            $sql = 'SELECT friendship_id FROM ' . $db->prefix(
156
                    'yogurt_friendships'
157
                ) . "${whereQuery} ORDER BY ${sort} ${order}";
158
159
            $result = $db->query($sql, $limit, $start);
160
161
            while (false !== ($myrow = $db->fetchArray($result))) {
162
                $ret[] = $myrow['yogurt_friendship_id'];
163
            }
164
        } else {
165
            $sql = 'SELECT * FROM ' . $db->prefix('yogurt_friendships') . "${whereQuery} ORDER BY ${sort} ${order}";
166
167
            $result = $db->query($sql, $limit, $start);
168
169
            while (false !== ($myrow = $db->fetchArray($result))) {
170
                $ret[] = new self($myrow);
171
            }
172
        }
173
174
        return $ret;
175
    }
176
177
    /**
178
     * Get form
179
     *
180
     * @return \XoopsModules\Yogurt\Form\FriendshipForm
181
     */
182
183
    public function getForm()
184
    {
185
        return new Form\FriendshipForm($this);
186
    }
187
188
    /**
189
     * @return array|null
190
     */
191
192
    public function getGroupsRead()
193
    {
194
        //$permHelper = new \Xmf\Module\Helper\Permission();
195
196
        return $this->permHelper->getGroupsForItem(
197
            'sbcolumns_read',
198
            $this->getVar('friendship_id')
199
        );
200
    }
201
202
    /**
203
     * @return array|null
204
     */
205
206
    public function getGroupsSubmit()
207
    {
208
        //$permHelper = new \Xmf\Module\Helper\Permission();
209
210
        return $this->permHelper->getGroupsForItem(
211
            'sbcolumns_submit',
212
            $this->getVar('friendship_id')
213
        );
214
    }
215
216
    /**
217
     * @return array|null
218
     */
219
220
    public function getGroupsModeration()
221
    {
222
        //$permHelper = new \Xmf\Module\Helper\Permission();
223
224
        return $this->permHelper->getGroupsForItem(
225
            'sbcolumns_moderation',
226
            $this->getVar('friendship_id')
227
        );
228
    }
229
}
230