1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace XoopsModules\Suico; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Extended User Profile |
9
|
|
|
* |
10
|
|
|
* You may not change or alter any portion of this comment or credits |
11
|
|
|
* of supporting developers from this source code or any supporting source code |
12
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
16
|
|
|
* |
17
|
|
|
* @copyright (c) 2000-2016 XOOPS Project (www.xoops.org) |
18
|
|
|
* @license GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html) |
19
|
|
|
* @package profile |
20
|
|
|
* @since 2.3.0 |
21
|
|
|
* @author Jan Pedersen |
22
|
|
|
* @author Taiwen Jiang <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @package kernel |
27
|
|
|
* @copyright (c) 2000-2016 XOOPS Project (www.xoops.org) |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
use XoopsModules\Suico; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Class ProfileHandler |
34
|
|
|
* @package XoopsModules\Suico |
35
|
|
|
*/ |
36
|
|
|
class ProfileHandler extends \XoopsPersistableObjectHandler |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* holds reference to {@link ProfileFieldHandler} object |
40
|
|
|
*/ |
41
|
|
|
public $fieldHandler; |
42
|
|
|
/** |
43
|
|
|
* Array of {@link Suico\Field} objects |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
public $_fields = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param \XoopsDatabase $db |
50
|
|
|
*/ |
51
|
|
|
public function __construct(\XoopsDatabase $db) |
52
|
|
|
{ |
53
|
|
|
parent::__construct($db, 'suico_profile', Profile::class, 'profile_id'); |
54
|
|
|
$this->fieldHandler = Helper::getInstance()->getHandler('Field'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* create a new {@link ProfileProfile} |
59
|
|
|
* |
60
|
|
|
* @param bool $isNew Flag the new objects as "new"? |
61
|
|
|
* |
62
|
|
|
* @return object {@link ProfileProfile} |
63
|
|
|
*/ |
64
|
|
|
public function create($isNew = true) |
65
|
|
|
{ |
66
|
|
|
$obj = new $this->className($this->loadFields()); |
67
|
|
|
$obj->handler = $this; |
68
|
|
|
if ($isNew) { |
69
|
|
|
$obj->setNew(); |
70
|
|
|
} |
71
|
|
|
return $obj; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get a ProfileProfile object for a user id. |
76
|
|
|
* |
77
|
|
|
* We will create an empty profile if none exists. This behavior allows user objects |
78
|
|
|
* created outside of profile to be edited correctly in the profile module. |
79
|
|
|
* |
80
|
|
|
* @param int|null $uid |
81
|
|
|
* @param string[]|null $fields array of field names to fetch, null for all |
82
|
|
|
* |
83
|
|
|
* @return object {@link ProfileProfile} |
84
|
|
|
* |
85
|
|
|
* @internal This was get($uid, $createOnFailure = true). No callers found using the extra parameter. |
86
|
|
|
* @internal Modified to match parent signature. |
87
|
|
|
*/ |
88
|
|
|
public function get($uid = null, $fields = null) |
89
|
|
|
{ |
90
|
|
|
$obj = parent::get($uid, $fields); |
91
|
|
|
if (!\is_object($obj)) { |
92
|
|
|
$obj = $this->create(); |
93
|
|
|
} |
94
|
|
|
return $obj; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Create new {@link Suico\Field} object |
99
|
|
|
* |
100
|
|
|
* @param bool $isNew |
101
|
|
|
* |
102
|
|
|
* @return \XoopsModules\Suico\Field|\XoopsObject |
103
|
|
|
*/ |
104
|
|
|
public function createField($isNew = true) |
105
|
|
|
{ |
106
|
|
|
$return = $this->fieldHandler->create($isNew); |
107
|
|
|
return $return; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Load field information |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
public function loadFields() |
116
|
|
|
{ |
117
|
|
|
if (0 == \count($this->_fields)) { |
118
|
|
|
$this->_fields = $this->fieldHandler->loadFields(); |
119
|
|
|
} |
120
|
|
|
return $this->_fields; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Fetch fields |
125
|
|
|
* |
126
|
|
|
* @param \CriteriaElement $criteria {@link CriteriaElement} object |
127
|
|
|
* @param bool $id_as_key return array with field IDs as key? |
128
|
|
|
* @param bool $as_object return array of objects? |
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
public function getFields(\CriteriaElement $criteria, $id_as_key = true, $as_object = true) |
133
|
|
|
{ |
134
|
|
|
return $this->fieldHandler->getObjects($criteria, $id_as_key, $as_object); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Insert a field in the database |
139
|
|
|
* |
140
|
|
|
* @param \XoopsModules\Suico\Field $field |
141
|
|
|
* @param bool $force |
142
|
|
|
* |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
|
|
public function insertField(Suico\Field $field, $force = false) |
146
|
|
|
{ |
147
|
|
|
return $this->fieldHandler->insert($field, $force); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Delete a field from the database |
152
|
|
|
* |
153
|
|
|
* @param \XoopsModules\Suico\Field $field |
154
|
|
|
* @param bool $force |
155
|
|
|
* |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
|
|
public function deleteField(Suico\Field $field, $force = false) |
159
|
|
|
{ |
160
|
|
|
return $this->fieldHandler->delete($field, $force); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Save a new field in the database |
165
|
|
|
* |
166
|
|
|
* @param array $vars array of variables, taken from $module->loadInfo('profile')['field'] |
167
|
|
|
* @param int $weight |
168
|
|
|
* |
169
|
|
|
* @return string |
170
|
|
|
* @internal param int $type valuetype of the field |
171
|
|
|
* @internal param int $moduleid ID of the module, this field belongs to |
172
|
|
|
* @internal param int $categoryid ID of the category to add it to |
173
|
|
|
*/ |
174
|
|
|
public function saveField($vars, $weight = 0) |
175
|
|
|
{ |
176
|
|
|
$field = $this->createField(); |
177
|
|
|
$field->setVar('field_name', $vars['name']); |
178
|
|
|
$field->setVar('field_valuetype', $vars['valuetype']); |
179
|
|
|
$field->setVar('field_type', $vars['type']); |
180
|
|
|
$field->setVar('field_weight', $weight); |
181
|
|
|
if (isset($vars['title'])) { |
182
|
|
|
$field->setVar('field_title', $vars['title']); |
183
|
|
|
} |
184
|
|
|
if (isset($vars['description'])) { |
185
|
|
|
$field->setVar('field_description', $vars['description']); |
186
|
|
|
} |
187
|
|
|
if (isset($vars['required'])) { |
188
|
|
|
$field->setVar('field_required', $vars['required']); //0 = no, 1 = yes |
189
|
|
|
} |
190
|
|
|
if (isset($vars['maxlength'])) { |
191
|
|
|
$field->setVar('field_maxlength', $vars['maxlength']); |
192
|
|
|
} |
193
|
|
|
if (isset($vars['default'])) { |
194
|
|
|
$field->setVar('field_default', $vars['default']); |
195
|
|
|
} |
196
|
|
|
if (isset($vars['notnull'])) { |
197
|
|
|
$field->setVar('field_notnull', $vars['notnull']); |
198
|
|
|
} |
199
|
|
|
if (isset($vars['show'])) { |
200
|
|
|
$field->setVar('field_show', $vars['show']); |
201
|
|
|
} |
202
|
|
|
if (isset($vars['edit'])) { |
203
|
|
|
$field->setVar('field_edit', $vars['edit']); |
204
|
|
|
} |
205
|
|
|
if (isset($vars['config'])) { |
206
|
|
|
$field->setVar('field_config', $vars['config']); |
207
|
|
|
} |
208
|
|
|
if (isset($vars['options'])) { |
209
|
|
|
$field->setVar('field_options', $vars['options']); |
210
|
|
|
} else { |
211
|
|
|
$field->setVar('field_options', []); |
212
|
|
|
} |
213
|
|
|
if ($this->insertField($field)) { |
214
|
|
|
$msg = ' Field <strong>' . $vars['name'] . '</strong> added to the database'; |
215
|
|
|
} else { |
216
|
|
|
$msg = ' <span class="red">ERROR: Could not insert field <strong>' . $vars['name'] . '</strong> into the database. ' . \implode(' ', $field->getErrors()) . $this->db->error() . '</span>'; |
217
|
|
|
} |
218
|
|
|
unset($field); |
219
|
|
|
return $msg; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* insert a new object in the database |
224
|
|
|
* |
225
|
|
|
* @param \XoopsObject $obj reference to the object |
226
|
|
|
* @param bool $force whether to force the query execution despite security settings |
227
|
|
|
* |
228
|
|
|
* @return bool FALSE if failed, TRUE if already present and unchanged or successful |
229
|
|
|
*/ |
230
|
|
|
public function insert(\XoopsObject $obj, $force = false) |
231
|
|
|
{ |
232
|
|
|
if (!($obj instanceof $this->className)) { |
233
|
|
|
return false; |
234
|
|
|
} |
235
|
|
|
$uservars = $this->getUserVars(); |
236
|
|
|
foreach ($uservars as $var) { |
237
|
|
|
unset($obj->vars[$var]); |
238
|
|
|
} |
239
|
|
|
if (0 == \count($obj->vars)) { |
240
|
|
|
return true; |
241
|
|
|
} |
242
|
|
|
return parent::insert($obj, $force); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Get array of standard variable names (user table) |
247
|
|
|
* |
248
|
|
|
* @return array |
249
|
|
|
*/ |
250
|
|
|
public function getUserVars() |
251
|
|
|
{ |
252
|
|
|
return $this->fieldHandler->getUserVars(); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Search profiles and users |
257
|
|
|
* |
258
|
|
|
* @param \CriteriaElement $criteria CriteriaElement |
259
|
|
|
* @param array $searchvars Fields to be fetched |
260
|
|
|
* @param array|null $groups for Usergroups is selected (only admin!) |
261
|
|
|
* |
262
|
|
|
* @return array |
263
|
|
|
*/ |
264
|
|
|
public function search(\CriteriaElement $criteria, $searchvars = [], $groups = null) |
265
|
|
|
{ |
266
|
|
|
$uservars = $this->getUserVars(); |
267
|
|
|
$searchvars_user = \array_intersect($searchvars, $uservars); |
268
|
|
|
$searchvars_profile = \array_diff($searchvars, $uservars); |
269
|
|
|
$sv = ['u.uid, u.uname, u.email, u.user_viewemail']; |
270
|
|
|
if (!empty($searchvars_user)) { |
271
|
|
|
$sv[0] .= ',u.' . \implode(', u.', $searchvars_user); |
272
|
|
|
} |
273
|
|
|
if (!empty($searchvars_profile)) { |
274
|
|
|
$sv[] = 'p.' . \implode(', p.', $searchvars_profile); |
275
|
|
|
} |
276
|
|
|
$sql_select = 'SELECT ' . (empty($searchvars) ? 'u.*, p.*' : \implode(', ', $sv)); |
277
|
|
|
$sql_from = ' FROM ' . $this->db->prefix('users') . ' AS u LEFT JOIN ' . $this->table . ' AS p ON u.uid=p.profile_id' . (empty($groups) ? '' : ' LEFT JOIN ' . $this->db->prefix('groups_users_link') . ' AS g ON u.uid=g.uid'); |
278
|
|
|
$sql_clause = ' WHERE 1=1'; |
279
|
|
|
$sql_order = ''; |
280
|
|
|
$limit = $start = 0; |
281
|
|
|
if (isset($criteria) && $criteria instanceof \CriteriaElement) { |
282
|
|
|
$sql_clause .= ' AND ' . $criteria->render(); |
283
|
|
|
if ('' !== $criteria->getSort()) { |
284
|
|
|
$sql_order = ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
285
|
|
|
} |
286
|
|
|
$limit = $criteria->getLimit(); |
287
|
|
|
$start = $criteria->getStart(); |
288
|
|
|
} |
289
|
|
|
if (!empty($groups)) { |
290
|
|
|
$sql_clause .= ' AND g.groupid IN (' . \implode(', ', $groups) . ')'; |
291
|
|
|
} |
292
|
|
|
$sql_users = $sql_select . $sql_from . $sql_clause . $sql_order; |
293
|
|
|
$result = $this->db->query($sql_users, $limit, $start); |
294
|
|
|
if (!$result) { |
295
|
|
|
return [[], [], 0]; |
296
|
|
|
} |
297
|
|
|
$userHandler = \xoops_getHandler('user'); |
298
|
|
|
$uservars = $this->getUserVars(); |
299
|
|
|
$users = []; |
300
|
|
|
$profiles = []; |
301
|
|
|
while (false !== ($myrow = $this->db->fetchArray($result))) { |
302
|
|
|
$profile = $this->create(false); |
303
|
|
|
$user = $userHandler->create(false); |
|
|
|
|
304
|
|
|
foreach ($myrow as $name => $value) { |
305
|
|
|
if (\in_array($name, $uservars)) { |
306
|
|
|
$user->assignVar($name, $value); |
307
|
|
|
} else { |
308
|
|
|
$profile->assignVar($name, $value); |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
$profiles[$myrow['uid']] = $profile; |
312
|
|
|
$users[$myrow['uid']] = $user; |
313
|
|
|
} |
314
|
|
|
$count = \count($users); |
315
|
|
|
if ((!empty($limit) && $count >= $limit) || !empty($start)) { |
316
|
|
|
$sql_count = 'SELECT COUNT(*)' . $sql_from . $sql_clause; |
317
|
|
|
$result = $this->db->query($sql_count); |
318
|
|
|
[$count] = $this->db->fetchRow($result); |
319
|
|
|
} |
320
|
|
|
return [$users, $profiles, (int)$count]; |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.