1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace XoopsModules\Yogurt; |
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
|
|
|
/** |
27
|
|
|
* @package kernel |
28
|
|
|
* @copyright (c) 2000-2016 XOOPS Project (www.xoops.org) |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
use XoopsModules\Yogurt; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Class ProfileHandler |
35
|
|
|
* @package XoopsModules\Yogurt |
36
|
|
|
*/ |
37
|
|
|
class ProfileHandler extends \XoopsPersistableObjectHandler |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* holds reference to {@link profileFieldHandler} object |
41
|
|
|
*/ |
42
|
|
|
|
43
|
|
|
public $_fHandler; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Array of {@link XoopsYogurt\Field} objects |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
|
50
|
|
|
public $_fields = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param \XoopsDatabase $db |
54
|
|
|
*/ |
55
|
|
|
|
56
|
|
|
public function __construct(\XoopsDatabase $db) |
57
|
|
|
{ |
58
|
|
|
parent::__construct($db, 'yogurt_profile', Profile::class, 'profile_id'); |
59
|
|
|
|
60
|
|
|
$this->_fHandler = \XoopsModules\Yogurt\Helper::getInstance()->getHandler('Field'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* create a new {@link ProfileProfile} |
65
|
|
|
* |
66
|
|
|
* @param bool $isNew Flag the new objects as "new"? |
67
|
|
|
* |
68
|
|
|
* @return object {@link ProfileProfile} |
69
|
|
|
*/ |
70
|
|
|
|
71
|
|
|
public function create($isNew = true) |
72
|
|
|
{ |
73
|
|
|
$obj = new $this->className($this->loadFields()); |
74
|
|
|
|
75
|
|
|
$obj->handler = $this; |
76
|
|
|
|
77
|
|
|
if ($isNew) { |
78
|
|
|
$obj->setNew(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $obj; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get a ProfileProfile object for a user id. |
86
|
|
|
* |
87
|
|
|
* We will create an empty profile if none exists. This behavior allows user objects |
88
|
|
|
* created outside of profile to be edited correctly in the profile module. |
89
|
|
|
* |
90
|
|
|
* @param int|null $uid |
91
|
|
|
* @param string[]|null $fields array of field names to fetch, null for all |
92
|
|
|
* |
93
|
|
|
* @return object {@link ProfileProfile} |
94
|
|
|
* |
95
|
|
|
* @internal This was get($uid, $createOnFailure = true). No callers found using the extra parameter. |
96
|
|
|
* @internal Modified to match parent signature. |
97
|
|
|
*/ |
98
|
|
|
|
99
|
|
|
public function get($uid = null, $fields = null) |
100
|
|
|
{ |
101
|
|
|
$obj = parent::get($uid, $fields); |
102
|
|
|
|
103
|
|
|
if (!\is_object($obj)) { |
104
|
|
|
$obj = $this->create(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $obj; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Create new {@link Yogurt\Field} object |
112
|
|
|
* |
113
|
|
|
* @param bool $isNew |
114
|
|
|
* |
115
|
|
|
* @return Yogurt\Field |
116
|
|
|
*/ |
117
|
|
|
|
118
|
|
|
public function createField($isNew = true) |
119
|
|
|
{ |
120
|
|
|
$return = $this->_fHandler->create($isNew); |
121
|
|
|
|
122
|
|
|
return $return; |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Load field information |
127
|
|
|
* |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
|
|
|
131
|
|
|
public function loadFields() |
132
|
|
|
{ |
133
|
|
|
if (0 == \count($this->_fields)) { |
134
|
|
|
$this->_fields = $this->_fHandler->loadFields(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $this->_fields; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Fetch fields |
142
|
|
|
* |
143
|
|
|
* @param \CriteriaElement $criteria {@link CriteriaElement} object |
144
|
|
|
* @param bool $id_as_key return array with field IDs as key? |
145
|
|
|
* @param bool $as_object return array of objects? |
146
|
|
|
* |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
|
150
|
|
|
public function getFields(\CriteriaElement $criteria, $id_as_key = true, $as_object = true) |
151
|
|
|
{ |
152
|
|
|
return $this->_fHandler->getObjects($criteria, $id_as_key, $as_object); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Insert a field in the database |
157
|
|
|
* |
158
|
|
|
* @param \XoopsModules\Yogurt\Field $field |
159
|
|
|
* @param bool $force |
160
|
|
|
* |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
|
|
|
164
|
|
|
public function insertField(Yogurt\Field $field, $force = false) |
165
|
|
|
{ |
166
|
|
|
return $this->_fHandler->insert($field, $force); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Delete a field from the database |
171
|
|
|
* |
172
|
|
|
* @param \XoopsModules\Yogurt\Field $field |
173
|
|
|
* @param bool $force |
174
|
|
|
* |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
|
|
|
178
|
|
|
public function deleteField(Yogurt\Field $field, $force = false) |
179
|
|
|
{ |
180
|
|
|
return $this->_fHandler->delete($field, $force); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Save a new field in the database |
185
|
|
|
* |
186
|
|
|
* @param array $vars array of variables, taken from $module->loadInfo('profile')['field'] |
187
|
|
|
* @param int $weight |
188
|
|
|
* |
189
|
|
|
* @return string |
190
|
|
|
* @internal param int $type valuetype of the field |
191
|
|
|
* @internal param int $moduleid ID of the module, this field belongs to |
192
|
|
|
* @internal param int $categoryid ID of the category to add it to |
193
|
|
|
*/ |
194
|
|
|
|
195
|
|
|
public function saveField($vars, $weight = 0) |
196
|
|
|
{ |
197
|
|
|
$field = $this->createField(); |
198
|
|
|
|
199
|
|
|
$field->setVar('field_name', $vars['name']); |
200
|
|
|
|
201
|
|
|
$field->setVar('field_valuetype', $vars['valuetype']); |
202
|
|
|
|
203
|
|
|
$field->setVar('field_type', $vars['type']); |
204
|
|
|
|
205
|
|
|
$field->setVar('field_weight', $weight); |
206
|
|
|
|
207
|
|
|
if (isset($vars['title'])) { |
208
|
|
|
$field->setVar('field_title', $vars['title']); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
if (isset($vars['description'])) { |
212
|
|
|
$field->setVar('field_description', $vars['description']); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
if (isset($vars['required'])) { |
216
|
|
|
$field->setVar('field_required', $vars['required']); //0 = no, 1 = yes |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if (isset($vars['maxlength'])) { |
220
|
|
|
$field->setVar('field_maxlength', $vars['maxlength']); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
if (isset($vars['default'])) { |
224
|
|
|
$field->setVar('field_default', $vars['default']); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
if (isset($vars['notnull'])) { |
228
|
|
|
$field->setVar('field_notnull', $vars['notnull']); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
if (isset($vars['show'])) { |
232
|
|
|
$field->setVar('field_show', $vars['show']); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
if (isset($vars['edit'])) { |
236
|
|
|
$field->setVar('field_edit', $vars['edit']); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
if (isset($vars['config'])) { |
240
|
|
|
$field->setVar('field_config', $vars['config']); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
if (isset($vars['options'])) { |
244
|
|
|
$field->setVar('field_options', $vars['options']); |
245
|
|
|
} else { |
246
|
|
|
$field->setVar('field_options', []); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
if ($this->insertField($field)) { |
250
|
|
|
$msg = ' Field <strong>' . $vars['name'] . '</strong> added to the database'; |
251
|
|
|
} else { |
252
|
|
|
$msg = ' <span class="red">ERROR: Could not insert field <strong>' . $vars['name'] . '</strong> into the database. ' . \implode(' ', $field->getErrors()) . $this->db->error() . '</span>'; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
unset($field); |
256
|
|
|
|
257
|
|
|
return $msg; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* insert a new object in the database |
262
|
|
|
* |
263
|
|
|
* @param \XoopsObject $obj reference to the object |
264
|
|
|
* @param bool $force whether to force the query execution despite security settings |
265
|
|
|
* |
266
|
|
|
* @return bool FALSE if failed, TRUE if already present and unchanged or successful |
267
|
|
|
*/ |
268
|
|
|
|
269
|
|
|
public function insert(\XoopsObject $obj, $force = false) |
270
|
|
|
{ |
271
|
|
|
if (!($obj instanceof $this->className)) { |
272
|
|
|
return false; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
$uservars = $this->getUserVars(); |
276
|
|
|
|
277
|
|
|
foreach ($uservars as $var) { |
278
|
|
|
unset($obj->vars[$var]); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
if (0 == \count($obj->vars)) { |
282
|
|
|
return true; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
return parent::insert($obj, $force); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Get array of standard variable names (user table) |
290
|
|
|
* |
291
|
|
|
* @return array |
292
|
|
|
*/ |
293
|
|
|
|
294
|
|
|
public function getUserVars() |
295
|
|
|
{ |
296
|
|
|
return $this->_fHandler->getUserVars(); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Search profiles and users |
301
|
|
|
* |
302
|
|
|
* @param \CriteriaElement $criteria CriteriaElement |
303
|
|
|
* @param array $searchvars Fields to be fetched |
304
|
|
|
* @param array $groups for Usergroups is selected (only admin!) |
305
|
|
|
* |
306
|
|
|
* @return array |
307
|
|
|
*/ |
308
|
|
|
|
309
|
|
|
public function search(\CriteriaElement $criteria, $searchvars = [], $groups = null) |
310
|
|
|
{ |
311
|
|
|
$uservars = $this->getUserVars(); |
312
|
|
|
|
313
|
|
|
$searchvars_user = \array_intersect($searchvars, $uservars); |
314
|
|
|
|
315
|
|
|
$searchvars_profile = \array_diff($searchvars, $uservars); |
316
|
|
|
|
317
|
|
|
$sv = ['u.uid, u.uname, u.email, u.user_viewemail']; |
318
|
|
|
|
319
|
|
|
if (!empty($searchvars_user)) { |
320
|
|
|
$sv[0] .= ',u.' . \implode(', u.', $searchvars_user); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
if (!empty($searchvars_profile)) { |
324
|
|
|
$sv[] = 'p.' . \implode(', p.', $searchvars_profile); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
$sql_select = 'SELECT ' . (empty($searchvars) ? 'u.*, p.*' : \implode(', ', $sv)); |
328
|
|
|
|
329
|
|
|
$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'); |
330
|
|
|
|
331
|
|
|
$sql_clause = ' WHERE 1=1'; |
332
|
|
|
|
333
|
|
|
$sql_order = ''; |
334
|
|
|
|
335
|
|
|
$limit = $start = 0; |
336
|
|
|
|
337
|
|
|
if (isset($criteria) && $criteria instanceof \CriteriaElement) { |
338
|
|
|
$sql_clause .= ' AND ' . $criteria->render(); |
339
|
|
|
|
340
|
|
|
if ('' !== $criteria->getSort()) { |
341
|
|
|
$sql_order = ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
$limit = $criteria->getLimit(); |
345
|
|
|
|
346
|
|
|
$start = $criteria->getStart(); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
if (!empty($groups)) { |
350
|
|
|
$sql_clause .= ' AND g.groupid IN (' . \implode(', ', $groups) . ')'; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
$sql_users = $sql_select . $sql_from . $sql_clause . $sql_order; |
354
|
|
|
|
355
|
|
|
$result = $this->db->query($sql_users, $limit, $start); |
356
|
|
|
|
357
|
|
|
if (!$result) { |
358
|
|
|
return [[], [], 0]; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
$user_handler = \xoops_getHandler('user'); |
362
|
|
|
|
363
|
|
|
$uservars = $this->getUserVars(); |
364
|
|
|
|
365
|
|
|
$users = []; |
366
|
|
|
|
367
|
|
|
$profiles = []; |
368
|
|
|
|
369
|
|
|
while (false !== ($myrow = $this->db->fetchArray($result))) { |
370
|
|
|
$profile = $this->create(false); |
371
|
|
|
|
372
|
|
|
$user = $user_handler->create(false); |
|
|
|
|
373
|
|
|
|
374
|
|
|
foreach ($myrow as $name => $value) { |
375
|
|
|
if (\in_array($name, $uservars)) { |
376
|
|
|
$user->assignVar($name, $value); |
377
|
|
|
} else { |
378
|
|
|
$profile->assignVar($name, $value); |
379
|
|
|
} |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
$profiles[$myrow['uid']] = $profile; |
383
|
|
|
|
384
|
|
|
$users[$myrow['uid']] = $user; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
$count = \count($users); |
388
|
|
|
|
389
|
|
|
if ((!empty($limit) && $count >= $limit) || !empty($start)) { |
390
|
|
|
$sql_count = 'SELECT COUNT(*)' . $sql_from . $sql_clause; |
391
|
|
|
|
392
|
|
|
$result = $this->db->query($sql_count); |
393
|
|
|
|
394
|
|
|
[$count] = $this->db->fetchRow($result); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
return [$users, $profiles, (int)$count]; |
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
|