Friendship::getAllFriendships()   B
last analyzed

Complexity

Conditions 9
Paths 12

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 23
nc 12
nop 6
dl 0
loc 37
rs 8.0555
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Suico;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * @category        Module
17
 * @copyright       {@link https://xoops.org/ XOOPS Project}
18
 * @license         GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
19
 * @author          Bruno Barthez, Marcello Brandão aka  Suico, Mamba, LioMJ  <https://xoops.org>
20
 */
21
22
use Xmf\Module\Helper\Permission;
23
use XoopsDatabaseFactory;
24
use XoopsObject;
25
26
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
27
/**
28
 * Includes of form objects and uploader
29
 */
30
require_once XOOPS_ROOT_PATH . '/class/uploader.php';
31
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
32
require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
33
require_once XOOPS_ROOT_PATH . '/kernel/object.php';
34
35
/**
36
 * Friendship class.
37
 * $this class is responsible for providing data access mechanisms to the data source
38
 * of XOOPS user class objects.
39
 */
40
class Friendship extends XoopsObject
41
{
42
    public \XoopsDatabase $db;
43
    public Helper         $helper;
44
    public Permission     $permHelper;
45
    public                $friendship_id;
46
    public $friend;
47
    public $level;
48
    public $hot;
49
    public $trust;
50
    public $cool;
51
    public $fan;
52
    public $date_created;
53
    public $date_updated;
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
        $this->db         = XoopsDatabaseFactory::getDatabaseConnection();
68
        $this->initVar('friendship_id', \XOBJ_DTYPE_INT, null, false, 10);
69
        $this->initVar('friend1_uid', \XOBJ_DTYPE_INT, null, false, 10);
70
        $this->initVar('friend2_uid', \XOBJ_DTYPE_INT, null, false, 10);
71
        $this->initVar('level', \XOBJ_DTYPE_INT, null, false, 10);
72
        $this->initVar('hot', \XOBJ_DTYPE_INT, null, false, 10);
73
        $this->initVar('trust', \XOBJ_DTYPE_INT, null, false, 10);
74
        $this->initVar('cool', \XOBJ_DTYPE_INT, null, false, 10);
75
        $this->initVar('fan', \XOBJ_DTYPE_INT, null, false, 10);
76
        $this->initVar('date_created', \XOBJ_DTYPE_INT, 0, false);
77
        $this->initVar('date_updated', \XOBJ_DTYPE_INT, 0, false);
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): void
93
    {
94
        $sql   = 'SELECT * FROM ' . $this->db->prefix('suico_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 * FROM ' . $db->prefix('suico_friendships') . "{$whereQuery} ORDER BY {$sort} {$order}";
133
            $result = $db->query($sql, $limit, $start);
134
            while (false !== ($myrow = $db->fetchArray($result))) {
135
                $ret[] = new self($myrow);
136
            }
137
        } else {
138
            $sql    = 'SELECT friendship_id FROM ' . $db->prefix(
139
                    'suico_friendships'
140
                ) . "{$whereQuery} ORDER BY {$sort} {$order}";
141
            $result = $db->query($sql, $limit, $start);
142
            while (false !== ($myrow = $db->fetchArray($result))) {
143
                $ret[] = $myrow['suico_friendship_id'];
144
            }
145
        }
146
147
        return $ret;
148
    }
149
150
    /**
151
     * Get form
152
     *
153
     * @return \XoopsModules\Suico\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