-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathField.php
More file actions
438 lines (417 loc) · 15.2 KB
/
Field.php
File metadata and controls
438 lines (417 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
<?php
/**
* Class representing the profile field object
*
* @copyright The ImpressCMS <Project http://www.impresscms.org/>
* @license LICENSE.txt
* @license GNU General Public License (GPL) http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* @package profile
* @since 1.2
* @author Jan Pedersen
* @author The SmartFactory <http://www.smartfactory.ca>
* @author Sina Asghari (aka stranger) <pesian_stranger@users.sourceforge.net>
* @version $Id$
*/
defined('ICMS_ROOT_PATH') or die('ICMS root path not defined');
class mod_profile_Field extends icms_ipf_Object {
/**
* Constructor
*
* @param mod_profile_FieldHandler $handler object handler
*/
public function __construct(&$handler) {
parent::__construct($handler);
$this->quickInitVar('fieldid', XOBJ_DTYPE_INT, true);
$this->quickInitVar('catid', XOBJ_DTYPE_INT, false);
$this->quickInitVar('field_type', XOBJ_DTYPE_TXTBOX, false);
$this->quickInitVar('field_valuetype', XOBJ_DTYPE_TXTBOX, false);
$this->quickInitVar('field_name', XOBJ_DTYPE_TXTBOX, true);
$this->quickInitVar('field_title', XOBJ_DTYPE_TXTBOX, true);
$this->quickInitVar('url', XOBJ_DTYPE_IMAGE, true);
$this->quickInitVar('field_description', XOBJ_DTYPE_TXTAREA, false);
$this->quickInitVar('field_required', XOBJ_DTYPE_INT, false);
$this->quickInitVar('field_maxlength', XOBJ_DTYPE_TXTBOX, false, false, false, 0);
$this->quickInitVar('field_weight', XOBJ_DTYPE_INT, false, false, false, 0);
$this->quickInitVar('field_default', XOBJ_DTYPE_TXTAREA, false);
$this->quickInitVar('field_notnull', XOBJ_DTYPE_INT, false);
$this->quickInitVar('field_edit', XOBJ_DTYPE_INT, false);
$this->quickInitVar('field_show', XOBJ_DTYPE_INT, false);
$this->quickInitVar('field_options', XOBJ_DTYPE_TXTBOX, false);
$this->quickInitVar('exportable', XOBJ_DTYPE_INT, false);
$this->quickInitVar('step_id', XOBJ_DTYPE_INT, false);
$this->quickInitVar('system', XOBJ_DTYPE_INT, false, false, false, 0);
$this->hideFieldFromForm(array('field_valuetype', 'system'));
$this->setControl('url', array('name' => 'image', 'nourl' => true));
$this->setControl('field_required', 'yesno');
$this->setControl('field_notnull', 'yesno');
$this->setControl('field_edit', 'yesno');
$this->setControl('field_show', 'yesno');
$this->setControl('exportable', 'yesno');
$this->setControl('system', 'yesno');
$this->setControl('catid', array('itemHandler' => 'category', 'method' => 'getList', 'module' => 'profile'));
$this->setControl('field_type', array ('itemHandler' => 'field', 'method' => 'getFieldTypeArray', 'module' => 'profile'));
$this->setControl('step_id', array('itemHandler' => 'regstep', 'method' => 'getListForFields', 'module' => 'profile'));
}
/**
* Returns a {@link icms_form_Element} for editing the value of this field
*
* @param icms_member_user_Object $user {@link icms_member_user_Object} object to edit the value of
* @param mod_profile_Profile $profile {@link mod_profile_Profile} object to edit the value of
*
* @return icms_form_Element
*/
public function getEditElement($user, $profile) {
global $icmsConfigAuth;
$value = in_array($this->getVar('field_name'), $this->getUserVars()) ? $user->getVar($this->getVar('field_name'), 'e') : $profile->getVar($this->getVar('field_name'), 'e');
if ($value === null) $value = $this->getVar('field_default');
$caption = $this->getVar('field_title');
$caption = defined($caption) ? constant($caption) : $caption;
$name = $this->getVar('field_name', 'e');
$options = unserialize($this->getVar('field_options', 'n'));
if ($this->getVar('field_type') != "image" && is_array($options)) {
asort($options);
foreach(array_keys($options) as $key){
$optval = defined($options[$key]) ? constant($options[$key]) : $options[$key];
$optkey = defined($key) ? constant($key) : $key;
unset($options[$key]);
$options[$optkey] = $optval;
}
}
switch ($this->getVar('field_type')) {
case "autotext":
$element = new icms_form_elements_Label($caption, $value);
break;
case "textarea":
$element = new icms_form_elements_Textarea($caption, $name, $value, 4, 30);
break;
case "dhtml":
$element = new icms_form_elements_Dhtmltextarea($caption, $name, $value, 10, 30);
break;
case "select":
$element = new icms_form_elements_Select($caption, $name, $value);
$element->addOptionArray($options);
break;
case "select_multi":
$element = new icms_form_elements_Select($caption, $name, $value, 5, true);
$element->addOptionArray($options);
break;
case "radio":
$element = new icms_form_elements_Radio($caption, $name, $value);
$element->addOptionArray($options);
break;
case "checkbox":
$element = new icms_form_elements_Checkbox($caption, $name, $value);
$element->addOptionArray($options);
break;
case "yesno":
$element = new icms_form_elements_Radioyn($caption, $name, $value);
break;
case "group":
$element = new icms_form_elements_select_Group($caption, $name, true, $value);
break;
case "group_multi":
$element = new icms_form_elements_select_Group($caption, $name, true, $value, 5, true);
break;
case "language":
$element = new icms_form_elements_select_Lang($caption, $name, $value);
break;
case "date":
$element = new icms_form_elements_Date($caption, $name, 15, $value);
break;
case "longdate":
$element = new icms_form_elements_Date($caption, $name, 15, str_replace("-", "/", $value));
break;
case "datetime":
$element = new icms_form_elements_Datetime($caption, $name, 15, $value);
break;
case "timezone":
$element = new icms_form_elements_select_Timezone($caption, $name, $value);
$element->setExtra("style='width: 280px;'");
break;
case "rank":
$element = new icms_form_elements_Select($caption, $name, $value);
$ranks = icms_getModuleHandler("userrank", "system")->getList(icms_buildCriteria(array("rank_special" => 1)));
$element->addOption(0, "--------------");
$element->addOptionArray($ranks);
break;
case "theme":
$element = new icms_form_elements_Select($caption, $name, $value);
$element->addOption("0", _MD_PROFILE_SITEDEFAULT);
$handle = opendir(ICMS_THEME_PATH.'/');
$dirlist = array();
while (false !== ($file = readdir($handle))) {
if (is_dir(ICMS_THEME_PATH.'/'.$file) && !preg_match("/^[.]{1,2}$/",$file) && strtolower($file) != 'cvs') {
if (file_exists(ICMS_THEME_PATH.'/'.$file.'/theme.html') && in_array($file, $GLOBALS['icmsConfig']['theme_set_allowed'])) {
$dirlist[$file]=$file;
}
}
}
closedir($handle);
if (!empty($dirlist)) {
asort($dirlist);
$element->addOptionArray($dirlist);
}
break;
case "image":
$element = new icms_form_elements_File($caption, $name, $options['maxsize']*1024);
if ($value != "") {
$this->assignVar('field_description', "");
$element->setDescription($this->getOutputValue($user, $profile));
}
break;
case "openid":
if ($icmsConfigAuth['auth_openid'] != 1) {$element = NULL; break;}
$element = new icms_form_elements_Text($caption, $name, 35, $this->getVar('field_maxlength'), $value);
break;
case "textbox":
default:
$element = new icms_form_elements_Text($caption, $name, 35, $this->getVar('field_maxlength'), $value);
break;
}
if ($this->getVar('field_description') != '') $element->setDescription($this->getVar('field_description'));
return $element;
}
/**
* Returns a value for output of this field
*
* @param icms_member_user_Object $user object to get the value of
* @param mod_profile_Profile $profile object to get the value of
* @global array $icmsConfigAuth
* @return mixed
**/
public function getOutputValue(&$user, $profile) {
global $icmsConfigAuth;
$value = in_array($this->getVar('field_name'), $this->getUserVars()) ? $user->getVar($this->getVar('field_name')) : $profile->getVar($this->getVar('field_name'));
switch ($this->getVar('field_type')) {
case "textarea":
case "dhtml":
if($this->getVar("field_name") == "user_sig") {
$value = $user->getVar("user_sig", "N");
}
return icms_core_DataFilter::undoHtmlSpecialChars(str_replace('&', '&', $value), 1);
break;
case "select":
case "radio":
$options = unserialize($this->getVar('field_options', 'n'));
return isset($options[$value]) ? htmlspecialchars($options[$value]) : "";
break;
case "select_multi":
case "checkbox":
$options = unserialize($this->getVar('field_options', 'n'));
$ret = array();
if (count($options) > 0) {
foreach (array_keys($options) as $key) {
if (in_array($key, $value)) {
$ret[$key] = htmlspecialchars($options[$key]);
}
}
}
return $ret;
break;
case "group":
//change to retrieve groups and return name of group
return $value;
break;
case "group_multi":
//change to retrieve groups and return array of group names
return "";
break;
case "longdate":
//return YYYY/MM/DD format - not optimal as it is not using local date format, but how do we do that
//when we cannot convert it to a UNIX timestamp?
return str_replace("-", "/", $value);
case "date":
if ($value != 0) return formatTimestamp($value, 's');
return "";
break;
case "datetime":
if ($value > 0) return formatTimestamp($value, 'm');
return "";
break;
case "autotext":
$value = $user->getVar($this->getVar('field_name'), 'n'); //autotext can have HTML in it
$value = str_replace("{X_UID}", $user->getVar("uid"), $value );
$value = str_replace("{X_URL}", ICMS_URL, $value );
$value = str_replace("{X_UNAME}", $user->getVar("uname"), $value );
return $value;
break;
case "rank":
$userrank = $user->rank();
return '<img src="' . $userrank['image'] . '" alt="' . $userrank['title'] . '" /> '.$userrank['title'];
break;
case "yesno":
return $value ? _YES : _NO;
break;
case "timezone":
$timezones = icms_form_elements_select_Timezone::getTimeZoneList();
return $timezones[str_replace('.0', '', $value)];
break;
case "image":
if ($value == "") return '';
return "<img src='".ICMS_UPLOAD_URL."/".basename(dirname(dirname(__FILE__)))."/".$value."' alt='image' />";
break;
case "url":
if ($value == "") return '';
return icms_core_DataFilter::makeClickable(formatURL($value));
case "location":
if ($value == "") return '';
return $value.' <a href="http://maps.google.com/?q='.$value.'" target="_blank" ><img src="'.ICMS_URL.'/modules/'.basename(dirname(dirname(__FILE__))).'/images/mapsgoogle.gif" alt="" /></a>';
case "email":
if ($value == "") return '';
if ($user->getVar('user_viewemail') || (is_object(icms::$user) && (icms::$user->isAdmin() || icms::$user->getVar('uid') == $user->getVar('uid')))) return '<a href="mailto:'.$value.'">'.$value.'</a>';
return '';
case "openid":
if ($value == "") return '';
if ($icmsConfigAuth['auth_openid'] == 1 && ($user->getVar('user_viewoid') || (is_object(icms::$user) && (icms::$user->isAdmin() || icms::$user->getVar('uid') == $user->getVar('uid'))))) return $value;
return '';
case "textbox":
case "theme":
case "language":
default:
return $value;
break;
}
}
/**
* Returns a value ready to be saved in the database
*
* @param mixed $value Value to format
* @param mixed $oldvalue old value
*
* @return mixed
*/
public function getValueForSave($value, $oldvalue) {
$value = icms_core_DataFilter::checkVarArray($value);
switch ($this->getVar('field_type')) {
default:
case "textbox":
case "textarea":
case "dhtml":
case "yesno":
case "timezone":
case "theme":
case "language":
case "select":
case "radio":
case "select_multi":
case "checkbox":
case "group":
case "group_multi":
case "longdate":
return $value;
case "date":
if ($value != "") {
return strtotime($value);
}
else {
return $value;
}
break;
case "datetime":
if ($value != "") {
return strtotime($value['date']) + $value['time'];
}
else {
return $value;
}
break;
case "image":
if (!isset($_FILES[$_POST['xoops_upload_file'][0]])) {
return $oldvalue;
}
$options = unserialize($this->getVar('field_options', 'n'));
$dirname = ICMS_UPLOAD_PATH.'/'.basename(dirname(__FILE__, 2));
if (!is_dir($dirname)) mkdir($dirname);
$uploader = new icms_file_MediaUploadHandler($dirname, array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/x-png', 'image/png'), $options['maxsize']*1024, $options['maxwidth'], $options['maxheight']);
if ($uploader->fetchMedia($_POST['xoops_upload_file'][0])) {
$uploader->setPrefix('image');
if ($uploader->upload()) {
@unlink($dirname.'/'.$oldvalue);
return $uploader->getSavedFileName();
} else {
echo $uploader->getErrors();
return $oldvalue;
}
} else {
echo $uploader->getErrors();
return $oldvalue;
}
break;
}
}
/**
* Get names of user variables
*
* @return array
*/
private function getUserVars(): array
{
$profile_handler = icms_getModuleHandler('profile', basename(dirname(dirname(__FILE__))), 'profile');
return $profile_handler->getUserVars();
}
/**
* get show icon for table display
*
* @return str html code for image
*/
public function getShow() {
if ($this->getVar('field_show')) {
$rtn = '<img src="' . ICMS_IMAGES_SET_URL . '/actions/button_ok.png" alt="1" />';
} else {
$rtn = '<img src="' . ICMS_IMAGES_SET_URL . '/actions/button_cancel.png" alt="0" />';
}
return $rtn;
}
/**
* get category title for table display
*
* @return str category title
*/
public function getCatid() {
$icmsPersistableRegistry = icms_ipf_registry_Handler::getInstance();
$category = $icmsPersistableRegistry->getSingleObject('category', $this->getVar('catid'), basename(dirname(dirname(__FILE__))));
return $category->getVar('cat_title');
}
/**
* return the field name
*
* @return string field name
*/
public function getFieldName(): string
{
return $this->getVar('field_name');
}
/**
* build image html tag
*
* @return str image
*/
public function getImage() {
$imgUrl = $this->getVar('url');
if (empty($imgUrl)) return '<img src="'.$this->handler->_moduleUrl.'images/blank18.png" alt="" />';
return '<img src="'.$this->handler->getImageUrl().$imgUrl.'" alt="" />';
}
/**
* generate delete button
*
* @staticvar icms_ipf_Controller $controller
* @return string linked icon to delete the object
*/
public function getDeleteButtonForDisplay()
{
static $controller = null;
if ($this->getVar('system') == 1) return;
if ($controller === null) $controller = new icms_ipf_Controller($this->handler);
return $controller->getDeleteItemLink($this, false, true, false);
}
/**
* generate textbox control to edit weight on acp
*
* @return string textbox control
*/
public function getField_weightControl(): string
{
$control = new icms_form_elements_Text('', 'field_weight[]', 5, 4, $this->getVar('field_weight'));
return $control->render();
}
}