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

Friendship   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 18
eloc 63
c 0
b 0
f 0
dl 0
loc 145
rs 10

7 Methods

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