User::createUser()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Smallworld;
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
 * SmallWorld
17
 *
18
 * @package      \XoopsModules\Smallworld
19
 * @license      GNU GPL (https://www.gnu.org/licenses/gpl-2.0.html/)
20
 * @copyright    The XOOPS Project (https://xoops.org)
21
 * @copyright    2011 Culex
22
 * @author       Michael Albertsen (http://culex.dk) <[email protected]>
23
 * @link         https://github.com/XoopsModules25x/smallworld
24
 * @since        1.0
25
 */
26
27
//include_once $GLOBALS['xoops']->path('include/common.php');
28
29
use XoopsModules\Smallworld;
30
use XoopsModules\Smallworld\Constants;
31
32
/**
33
 * Class User
34
 *
35
 */
36
class User
37
{
38
    /**
39
     * Check if user has profile
40
     *
41
     * Returns profile type:
42
     *  0= no XOOPS or SW profile,
43
     *  1= XOOPS user but no SW profile,
44
     *  2= has both
45
     *
46
     * @deprecated
47
     * @param int $userId  XOOPS user id
48
     * @return int
49
     */
50
    public function checkIfProfile($userId)
51
    {
52
        $depMsg = get_class() . __FUNCTION__ . " is deprecated, use SwUserHandler::" . __FUNCTION__ . " instead";
53 View Code Duplication
        if (isset($GLOBALS['xoopsLogger'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
            $GLOBALS['xoopsLogger']->addDeprecated($depMsg);
55
        } else {
56
            trigger_error($depMsg, E_USER_WARNING);
57
        }
58
        $userId = (int)$userId;
59
        $type   = Constants::PROFILE_NONE; // init profile type
60
        if (Constants::DEFAULT_UID < $userId) {
61
            // XOOPS user id - now check to see if it's a real user
62
            $xUser = new \XoopsUser($userId);
63
            if ($xUser instanceof \XoopsUser) {
0 ignored issues
show
Bug introduced by
The class XoopsUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
64
                // valid XOOPS user, see if there's a SW profile for them
65
                $swUserHandler = \XoopsModules\Smallworld\Helper::getInstance()->getHandler('SwUser');
66
                $userCount     = $swUserHandler->getCount(new \Criteria('userid', $userId));
67
                //$sql       = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . " WHERE userid = '" . $userId . "'";
68
                //$result    = $GLOBALS['xoopsDB']->queryF($sql);
69
                //$userCount = $GLOBALS['xoopsDB']->getRowsNum($result);
70
                // If \XoopsUser but no smallworld profile set to XOOPS only, otherwise they have a SW profile too
71
                $type = (0 == $userCount) ? Constants::PROFILE_XOOPS_ONLY : Constants::PROFILE_HAS_BOTH;
72
            }
73
        }
74
        return $type;
75
    }
76
77
    /**
78
     * Create user
79
     *
80
     * @param int $userId
81
     * @return mixed
82
     */
83
    public function createUser($userId)
84
    {
85
        $xUser  = new \XoopsUser((int)$userId);
86
        $retVal = false;
87
        if ($xUser instanceof \XoopsUser) { // make sure this is a real XOOPS user
0 ignored issues
show
Bug introduced by
The class XoopsUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
88
            $swUserHandler = \XoopsModules\Smallworld\Helper::getInstance()->getHandler('SwUser');
89
            $swUser = $swUserHandler->create();
90
            $swUser->setVar('userid', (int)$userId);
91
            $retVal = $swUserHandler->insert($swUser);
92
93
            //$sql    = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . ' (userid) VALUES (' . (int)$userId . ')';
94
            //$retVal = $GLOBALS['xoopsDB']->queryF($sql);
95
        }
96
        return $retVal;
97
    }
98
99
    /**
100
     * Check is user is smallworld user
101
     *
102
     * @todo - finish this method - it currently just assigns vars to template
103
     *
104
     * @return void
105
     */
106
    public function chkUser()
107
    {
108
        $greeting = '<br>';
109
        $greeting .= _SMALLWORLD_NOTYETUSER_GREETING . ' ' . $GLOBALS['xoopsUser']->uname() . '.<br><br>';
110
        $greeting .= _SMALLWORLD_NOTYETUSER_BOXTEXT;
111
112
        $GLOBALS['xoopsTpl']->assign('notyetusercontent', $greeting);
113
        $GLOBALS['xoopsTpl']->assign('check', 0);
114
    }
115
116
    /**
117
     * Check is user is friend
118
     *
119
     * @param int $user
120
     * @param int $userId
121
     * @return array
122
     */
123
    public function friendcheck($user, $userId)
124
    {
125
        $respons = [0 => '']; // init response
126
        if ($user == $userId) {
127
            $respons[0] = 2;
128
            return $respons;
129
        }
130
        $sql    = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_friends') . " WHERE me = '" . (int)$user . "' AND you = '" . (int)$userId . "' LIMIT 1";
131
        $result = $GLOBALS['xoopsDB']->query($sql);
132
        $i      = $GLOBALS['xoopsDB']->getRowsNum($result);
133
        if (0 == $i) {
134
            $sql    = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_friends') . " WHERE you = '" . (int)$user . "' AND me = '" . (int)$userId . "' LIMIT 1";
135
            $result = $GLOBALS['xoopsDB']->query($sql);
136
            $i      = $GLOBALS['xoopsDB']->getRowsNum($result);
137
        }
138
        while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
139
            if (0 == $i || '' == $i) {
140
                $respons[0] = 0;
141
            } elseif (1 == $i) {
142
                if (1 == $row['status']) {
143
                    $respons[0] = 1;
144
                } elseif (2 == $row['status']) {
145
                    $respons[0] = 2;
146
                }
147
            }
148
        }
149
150
        return $respons;
151
    }
152
153
    /**
154
     * Get name from userid
155
     *
156
     * @deprecated - moved to SwUserHandler::getName() method
157
     * @param int $userId
158
     * @return string
159
     */
160
    public function getName($userId)
161
    {
162
        $depMsg = get_class() . __FUNCTION__ . " is deprecated, use SwUserHandler::" . __FUNCTION__ . " instead";
163 View Code Duplication
        if (isset($GLOBALS['xoopsLogger'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
            $GLOBALS['xoopsLogger']->addDeprecated($depMsg);
165
        } else {
166
            trigger_error($depMsg, E_USER_WARNING);
167
        }
168
        $name = '';
169
        $swUserHandler = \XoopsModules\Smallworld\Helper::getInstance()->getHandler('SwUser');
170
        $criteria = new \Criteria('userid', (int)$userId);
171
        $criteria->setLimit(1);
172
        $swUserArray = $swUserHandler->getAll($criteria, ['username'], false);
173
        if (0 < count($swUserArray)) {
174
            $swUser = array_pop($swUserArray);
175
            $name = $swUser['username'];
176
        }
177
        /*
178
        $sql    = 'SELECT username FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . " WHERE userid = '" . (int)$userId . "' LIMIT 1";
179
        $result = $GLOBALS['xoopsDB']->queryF($sql);
180
        while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
181
            $name = $row['username'];
182
        }
183
        */
184
        return $name;
185
    }
186
187
    /**
188
     * Check if user is follower
189
     *
190
     * @param int $userId
191
     * @param int $friendId
192
     * @return array
193
     */
194
    public function following_or($userId, $friendId)
195
    {
196
        $respons = [0 => 0]; // init the array
197
        if ($userId != $friendId) {
198
            $sql    = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_followers') . " WHERE me = '" . (int)$userId . "' AND you = '" . (int)$friendId . "'";
199
            $result = $GLOBALS['xoopsDB']->query($sql);
200
            $i      = $GLOBALS['xoopsDB']->getRowsNum($result);
201
            while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
202
                if (1 == $i) {
203
                    $respons[0] = (Constants::FRIEND_STATUS_PENDING == $row['status']) ? Constants::FRIEND_STATUS_PENDING : Constants::FRIEND_STATUS_APPROVED;
204
                }
205
            }
206
        }
207
208
        return $respons;
209
    }
210
211
    /**
212
     * Get requests
213
     *
214
     * @param int $userId
215
     * @return array
216
     */
217
    public function getRequests($userId)
218
    {
219
        $msg      = [];
220
        $sql      = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_friends') . " WHERE you = '" . (int)$userId . "' AND status = '1'";
221
        $result   = $GLOBALS['xoopsDB']->queryF($sql);
222
        //$i        = $GLOBALS['xoopsDB']->getRowsNum($result);
223
        //$swDB     = new \XoopsModules\Smallworld\SwDatabase();
224
        //$wall     = new \XoopsModules\Smallworld\WallUpdates();
225
        /**
226
         * @var \XoopsModules\Smallworld\Helper $helper
227
         * @var \XoopsModules\Smallworld\SwUserHandler $swUserHandler
228
         */
229
        $helper        = \XoopsModules\Smallworld\Helper::getInstance();
230
        $swUserHandler = $helper->getHandler('SwUser');
231
        $start         = 0;
232
        while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result)) && $start <= count($row)) {
233
            $msg[$start]['friendname']  = $swUserHandler->getName($row['me']);
234
            $msg[$start]['img']         = $swUserHandler->gravatar($row['me']);
235
            $msg[$start]['friendimage'] = "<img src='" . XOOPS_UPLOAD_URL . '/' . $msg[$start]['img'] . "' height='40px'>";
236
            $msg[$start]['frienddate']  = date('d-m-Y', $row['date']);
237
            $msg[$start]['accept']      = '<a class="smallworldrequestlink" id = "smallworldfriendrequest_' . $msg[$start]['friendname'] . '" href = "javascript:Smallworld_AcceptDenyFriend(1,' . $row['me'] . ',' . $row['you'] . ',' . $start . ');">' . _SMALLWORLD_ACCEPT . '</a>';
238
            $msg[$start]['deny']        = '<a class="smallworldrequestlink" id = "smallworldfriendrequest_' . $msg[$start]['friendname'] . '" href = "javascript:Smallworld_AcceptDenyFriend(-1,' . $row['me'] . ',' . $row['you'] . ',' . $start . ');">' . _SMALLWORLD_DENY . '</a>';
239
            $msg[$start]['later']       = '<a class="smallworldrequestlink" id = "smallworldfriendrequest_' . $msg[$start]['friendname'] . '" href = "javascript:Smallworld_AcceptDenyFriend(0,' . $row['me'] . ',' . $row['you'] . ',' . $start . ');">' . _SMALLWORLD_LATER . '</a>';
240
            $msg[$start]['cnt']         = $start;
241
            ++$start;
242
        }
243
244
        return $msg;
245
    }
246
247
    /**
248
     * Get partner
249
     *
250
     * @deprecated - replaced with SwUserHandler::spouseExists() method
251
     * @param string $name
252
     * @return int
253
     */
254
    public function spousexist($name)
255
    {
256
        $depMsg = get_class() . __FUNCTION__ . " is deprecated, use SwUserHandler::" . __FUNCTION__ . " instead";
257 View Code Duplication
        if (isset($GLOBALS['xoopsLogger'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
258
            $GLOBALS['xoopsLogger']->addDeprecated($depMsg);
259
        } else {
260
            trigger_error($depMsg, E_USER_WARNING);
261
        }
262
        $swUserHandler = \XoopsModules\Smallworld\Helper::getInstance()->getHandler('SwUser');
263
        $exists = $swUserHandler->getCount(new \Criteria('username', $name));
264
        return $exists ? 1 : 0;
265
        /*
266
        $sql    = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . " WHERE username = '" . $name . "'";
267
        $result = $GLOBALS['xoopsDB']->queryF($sql);
268
        $i      = $GLOBALS['xoopsDB']->getRowsNum($result);
269
270
        return $i;
271
        */
272
    }
273
274
    /**
275
     * Get all users
276
     *
277
     * @deprecated - functionality moved to SwUserHandler::allUsers()
278
     * @return array
279
     */
280
    public function allUsers()
281
    {
282
        $depMsg = get_class() . __FUNCTION__ . " is deprecated, use SwUserHandler::" . __FUNCTION__ . " instead";
283 View Code Duplication
        if (isset($GLOBALS['xoopsLogger'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
284
            $GLOBALS['xoopsLogger']->addDeprecated($depMsg);
285
        } else {
286
            trigger_error($depMsg, E_USER_WARNING);
287
        }
288
        $retVal = [];
289
        $sql    = 'SELECT userid FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . ' ORDER BY userid';
290
        $result = $GLOBALS['xoopsDB']->queryF($sql);
291
        $i      = $GLOBALS['xoopsDB']->getRowsNum($result);
292 View Code Duplication
        if (0 !== $i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
293
            $data = [];
294
            while (false !== ($r = $GLOBALS['xoopsDB']->fetchArray($result))) {
295
                $data[] = $r;
296
            }
297
            $retVal = smallworld_array_flatten($data, 0);
298
        }
299
300
        return $retVal;
301
        //redirect_header(XOOPS_URL . "/modules/smallworld/register.php");
302
    }
303
}
304