1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
You may not change or alter any portion of this comment or credits |
4
|
|
|
of supporting developers from this source code or any supporting source code |
5
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
6
|
|
|
|
7
|
|
|
This program is distributed in the hope that it will be useful, |
8
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
9
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
use Xoops\Core\Database\Connection; |
13
|
|
|
use Xoops\Core\Kernel\Dtype; |
14
|
|
|
use Xoops\Core\Kernel\XoopsObject; |
|
|
|
|
15
|
|
|
use Xoops\Core\Kernel\XoopsPersistableObjectHandler; |
|
|
|
|
16
|
|
|
use Xoops\Core\Kernel\CriteriaElement; |
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Extended User Profile |
20
|
|
|
* |
21
|
|
|
* @copyright 2000-2016 XOOPS Project (http://xoops.org) |
22
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
23
|
|
|
* @package profile |
24
|
|
|
* @since 2.3.0 |
25
|
|
|
* @author Jan Pedersen |
26
|
|
|
* @author Taiwen Jiang <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class ProfileProfile extends XoopsObject |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var null|ProfileProfileHandler |
32
|
|
|
*/ |
33
|
|
|
public $handler; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param array $fields |
37
|
|
|
*/ |
38
|
|
|
public function __construct($fields) |
39
|
|
|
{ |
40
|
|
|
$this->initVar('profile_id', Dtype::TYPE_INTEGER, null, true); |
41
|
|
|
$this->init($fields); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Initiate variables |
46
|
|
|
* |
47
|
|
|
* @param array $fields field information array of {@link ProfileField} objects |
48
|
|
|
*/ |
49
|
|
|
public function init($fields) |
50
|
|
|
{ |
51
|
|
|
if (is_array($fields) && count($fields) > 0) { |
52
|
|
|
/* @var ProfileField $field */ |
53
|
|
|
foreach ($fields as $key => $field) { |
54
|
|
|
$this->initVar($key, $field->getVar('field_valuetype'), $field->getVar('field_default', 'n'), $field->getVar('field_required'), $field->getVar('field_maxlength')); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
class ProfileProfileHandler extends XoopsPersistableObjectHandler |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
/** |
63
|
|
|
* @var bool|ProfileFieldHandler |
64
|
|
|
*/ |
65
|
|
|
private $_fHandler; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Array of {@link ProfileField} objects |
69
|
|
|
* |
70
|
|
|
* @var array |
71
|
|
|
*/ |
72
|
|
|
private $_fields = array(); |
73
|
|
|
|
74
|
|
|
public function __construct(Connection $db) |
75
|
|
|
{ |
76
|
|
|
parent::__construct($db, 'profile_profile', 'ProfileProfile', 'profile_id'); |
77
|
|
|
$xoops = Xoops::getInstance(); |
|
|
|
|
78
|
|
|
$this->_fHandler = \Xoops::getModuleHelper('profile')->getHandler('field'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* create a new {@link ProfileProfile} |
83
|
|
|
* |
84
|
|
|
* @param bool $isNew Flag the new objects as "new"? |
85
|
|
|
* |
86
|
|
|
* @return ProfileProfile {@link ProfileProfile} |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
public function create($isNew = true) |
89
|
|
|
{ |
90
|
|
|
/* @var $obj ProfileProfile */ |
91
|
|
|
$obj = new $this->className($this->loadFields()); |
92
|
|
|
$obj->handler = $this; |
93
|
|
|
if ($isNew === true) { |
94
|
|
|
$obj->setNew(); |
95
|
|
|
} |
96
|
|
|
return $obj; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get a {@link ProfileProfile} |
101
|
|
|
* |
102
|
|
|
* @param $uid |
103
|
|
|
* @param bool $createOnFailure create a new {@link ProfileProfile} if none is fetched |
104
|
|
|
* |
105
|
|
|
* @return null|ProfileProfile|XoopsObject |
106
|
|
|
*/ |
107
|
|
|
public function getProfile($uid, $createOnFailure = true) |
108
|
|
|
{ |
109
|
|
|
$obj = parent::get($uid); |
|
|
|
|
110
|
|
|
if (!is_object($obj) && $createOnFailure) { |
111
|
|
|
$obj = $this->create(); |
112
|
|
|
} |
113
|
|
|
return $obj; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Create new {@link ProfileField} object |
118
|
|
|
* |
119
|
|
|
* @param bool $isNew |
120
|
|
|
* |
121
|
|
|
* @return ProfileField |
122
|
|
|
*/ |
123
|
|
|
public function createField($isNew = true) |
124
|
|
|
{ |
125
|
|
|
$return = $this->_fHandler->create($isNew); |
126
|
|
|
return $return; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Load field information |
131
|
|
|
* |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
public function loadFields() |
135
|
|
|
{ |
136
|
|
|
if (count($this->_fields) == 0) { |
137
|
|
|
$this->_fields = $this->_fHandler->loadFields(); |
138
|
|
|
} |
139
|
|
|
return $this->_fields; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Fetch fields |
144
|
|
|
* |
145
|
|
|
* @param CriteriaElement $criteria {@link CriteriaElement} object |
146
|
|
|
* @param bool $id_as_key return array with field IDs as key? |
147
|
|
|
* @param bool $as_object return array of objects? |
148
|
|
|
* |
149
|
|
|
* @return array |
150
|
|
|
**/ |
151
|
|
|
public function getFields(CriteriaElement $criteria, $id_as_key = true, $as_object = true) |
152
|
|
|
{ |
153
|
|
|
return $this->_fHandler->getObjects($criteria, $id_as_key, $as_object); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Insert a field in the database |
158
|
|
|
* |
159
|
|
|
* @param ProfileField $field |
160
|
|
|
* @param bool $force |
161
|
|
|
* |
162
|
|
|
* @return bool |
163
|
|
|
*/ |
164
|
|
|
public function insertField(ProfileField $field, $force = false) |
165
|
|
|
{ |
166
|
|
|
return $this->_fHandler->insert($field, $force); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Delete a field from the database |
171
|
|
|
* |
172
|
|
|
* @param ProfileField $field |
173
|
|
|
* @param bool $force |
174
|
|
|
* |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
|
|
public function deleteField(ProfileField $field, $force = false) |
178
|
|
|
{ |
179
|
|
|
return $this->_fHandler->delete($field, $force); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Save a new field in the database |
184
|
|
|
* |
185
|
|
|
* @param array $vars array of variables, taken from $module->loadInfo('profile')['field'] |
186
|
|
|
* @param int $weight |
187
|
|
|
* |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
|
|
public function saveField($vars, $weight = 0) |
191
|
|
|
{ |
192
|
|
|
$field = $this->createField(); |
193
|
|
|
$field->setVar('field_name', $vars['name']); |
194
|
|
|
$field->setVar('field_valuetype', $vars['valuetype']); |
195
|
|
|
$field->setVar('field_type', $vars['type']); |
196
|
|
|
$field->setVar('field_weight', $weight); |
197
|
|
|
if (isset($vars['title'])) { |
198
|
|
|
$field->setVar('field_title', $vars['title']); |
199
|
|
|
} |
200
|
|
|
if (isset($vars['description'])) { |
201
|
|
|
$field->setVar('field_description', $vars['description']); |
202
|
|
|
} |
203
|
|
|
if (isset($vars['required'])) { |
204
|
|
|
$field->setVar('field_required', $vars['required']); //0 = no, 1 = yes |
205
|
|
|
} |
206
|
|
|
if (isset($vars['maxlength'])) { |
207
|
|
|
$field->setVar('field_maxlength', $vars['maxlength']); |
208
|
|
|
} |
209
|
|
|
if (isset($vars['default'])) { |
210
|
|
|
$field->setVar('field_default', $vars['default']); |
211
|
|
|
} |
212
|
|
|
if (isset($vars['notnull'])) { |
213
|
|
|
$field->setVar('field_notnull', $vars['notnull']); |
214
|
|
|
} |
215
|
|
|
if (isset($vars['show'])) { |
216
|
|
|
$field->setVar('field_show', $vars['show']); |
217
|
|
|
} |
218
|
|
|
if (isset($vars['edit'])) { |
219
|
|
|
$field->setVar('field_edit', $vars['edit']); |
220
|
|
|
} |
221
|
|
|
if (isset($vars['config'])) { |
222
|
|
|
$field->setVar('field_config', $vars['config']); |
223
|
|
|
} |
224
|
|
|
if (isset($vars['options'])) { |
225
|
|
|
$field->setVar('field_options', $vars['options']); |
226
|
|
|
} else { |
227
|
|
|
$field->setVar('field_options', array()); |
228
|
|
|
} |
229
|
|
|
if ($this->insertField($field)) { |
230
|
|
|
$msg = ' Field <strong>' . $vars['name'] . '</strong> added to the database'; |
231
|
|
|
} else { |
232
|
|
|
$msg = ' <span class="red">ERROR: Could not insert field <strong>' . $vars['name'] . '</strong> into the database. ' . implode(' ', $field->getErrors()) . $this->db2->errorInfo() . '</span>'; |
233
|
|
|
} |
234
|
|
|
unset($field); |
235
|
|
|
return $msg; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* insert a new object in the database |
240
|
|
|
* |
241
|
|
|
* @param XoopsObject|ProfileProfile $obj reference to the object |
242
|
|
|
* @param bool $force whether to force the query execution despite security settings |
243
|
|
|
* |
244
|
|
|
* @return bool FALSE if failed, TRUE if already present and unchanged or successful |
245
|
|
|
*/ |
246
|
|
|
public function insert(XoopsObject $obj, $force = false) |
247
|
|
|
{ |
248
|
|
|
$uservars = $this->getUserVars(); |
249
|
|
|
foreach ($uservars as $var) { |
250
|
|
|
unset($obj->vars[$var]); |
251
|
|
|
} |
252
|
|
|
if (count($obj->vars) == 0) { |
253
|
|
|
return true; |
254
|
|
|
} |
255
|
|
|
return parent::insert($obj, $force); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Get array of standard variable names (user table) |
260
|
|
|
* |
261
|
|
|
* @return array |
262
|
|
|
*/ |
263
|
|
|
public function getUserVars() |
264
|
|
|
{ |
265
|
|
|
return $this->_fHandler->getUserVars(); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Search profiles and users |
270
|
|
|
* |
271
|
|
|
* @param CriteriaElement $criteria CriteriaElement |
272
|
|
|
* @param array $searchvars Fields to be fetched |
273
|
|
|
* @param array $groups for Usergroups is selected (only admin!) |
274
|
|
|
* |
275
|
|
|
* @return array |
276
|
|
|
*/ |
277
|
|
|
public function search(CriteriaElement $criteria, $searchvars = array(), $groups = null) |
278
|
|
|
{ |
279
|
|
|
$xoops = Xoops::getInstance(); |
280
|
|
|
$uservars = $this->getUserVars(); |
281
|
|
|
|
282
|
|
|
$searchvars_user = array_intersect($searchvars, $uservars); |
283
|
|
|
$searchvars_profile = array_diff($searchvars, $uservars); |
284
|
|
|
$sv = array('u.uid, u.uname, u.email, u.user_viewemail'); |
285
|
|
|
if (!empty($searchvars_user)) { |
286
|
|
|
$sv[0] .= ",u." . implode(", u.", $searchvars_user); |
287
|
|
|
} |
288
|
|
|
if (!empty($searchvars_profile)) { |
289
|
|
|
$sv[] = "p." . implode(", p.", $searchvars_profile); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
$qb = $xoops->db()->createXoopsQueryBuilder(); |
293
|
|
|
|
294
|
|
|
$qb->select((empty($searchvars) ? "u.*, p.*" : implode(", ", $sv))) |
295
|
|
|
->fromPrefix('system_user', 'u') |
296
|
|
|
->leftJoin('u', $this->table, 'p', 'u.uid=p.profile_id'); |
297
|
|
|
|
298
|
|
|
if (!empty($groups)) { |
299
|
|
|
$qb->leftJoinPrefix('u', 'system_usergroup', 'g', 'u.uid=g.uid'); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
$criteria->renderQb($qb); |
303
|
|
|
|
304
|
|
|
if (!empty($groups)) { |
305
|
|
|
$qb->andWhere('g.groupid IN (:grps)')->setParameter('grps', $groups); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
$result = $qb->execute(); |
309
|
|
|
|
310
|
|
|
$user_handler = $xoops->getHandlerUser(); |
311
|
|
|
$uservars = $this->getUserVars(); |
312
|
|
|
|
313
|
|
|
$users = array(); |
314
|
|
|
$profiles = array(); |
315
|
|
|
while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) { |
316
|
|
|
$profile = $this->create(false); |
317
|
|
|
$user = $user_handler->create(false); |
|
|
|
|
318
|
|
|
|
319
|
|
|
foreach ($myrow as $name => $value) { |
320
|
|
|
if (in_array($name, $uservars)) { |
321
|
|
|
$user->assignVar($name, $value); |
322
|
|
|
} else { |
323
|
|
|
$profile->assignVar($name, $value); |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
$profiles[$myrow['uid']] = $profile; |
327
|
|
|
$users[$myrow['uid']] = $user; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
$qb->select('COUNT(*)')->setMaxResults(null)->setFirstResult(null); |
331
|
|
|
$result = $qb->execute(); |
332
|
|
|
$count = $result->fetchColumn(); |
333
|
|
|
|
334
|
|
|
return array($users, $profiles, (int)($count)); |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: