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

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