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 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
<?php
/**
***********************************************************************************************
* @copyright 2004-2016 The Admidio Team
* @see http://www.admidio.org/
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2.0 only
***********************************************************************************************
*/
/**
* @class ProfileFields
* @brief Reads the user fields structure out of database and give access to it
*
* When an object is created than the actual profile fields structure will
* be read. In addition to this structure you can read the user values for
* all fields if you call @c readUserData . If you read field values than
* you will get the formated output. It's also possible to set user data and
* save this data to the database
*/
class ProfileFields
{
public $mProfileFields = array(); ///< Array with all user fields objects
public $mUserData = array(); ///< Array with all user data objects
protected $mUserId; ///< UserId of the current user of this object
protected $mDb; ///< An object of the class Database for communication with the database
protected $noValueCheck; ///< if true, than no value will be checked if method setValue is called
public $columnsValueChanged; ///< flag if a value of one field had changed
/**
* constructor that will initialize variables and read the profile field structure
* @param \Database $database Database object (should be @b $gDb)
* @param int $organizationId The id of the organization for which the profile field structure should be read
*/
public function __construct(&$database, $organizationId)
{
$this->setDatabase($database);
$this->readProfileFields($organizationId);
$this->mUserId = 0;
$this->noValueCheck = false;
$this->columnsValueChanged = false;
}
/**
* Called on serialization of this object. The database object could not
* be serialized and should be ignored.
* @return string[] Returns all class variables that should be serialized.
*/
public function __sleep()
{
return array_diff(array_keys(get_object_vars($this)), array('mDb'));
}
/**
* user data of all profile fields will be initialized
* the fields array will not be renewed
*/
public function clearUserData()
{
$this->mUserData = array();
$this->mUserId = 0;
$this->columnsValueChanged = false;
}
/**
* returns for a fieldname intern (usf_name_intern) the value of the column from table adm_user_fields
* @param string $fieldNameIntern Expects the @b usf_name_intern of table @b adm_user_fields
* @param string $column The column name of @b adm_user_field for which you want the value
* @param string $format Optional the format (is necessary for timestamps)
* @return mixed
*/
public function getProperty($fieldNameIntern, $column, $format = '')
{
if(array_key_exists($fieldNameIntern, $this->mProfileFields))
{
return $this->mProfileFields[$fieldNameIntern]->getValue($column, $format);
}
// if id-field not exists then return zero
if(strpos($column, '_id') > 0)
{
return 0;
}
return '';
}
/**
* returns for field id (usf_id) the value of the column from table adm_user_fields
* @param int $fieldId Expects the @b usf_id of table @b adm_user_fields
* @param string $column The column name of @b adm_user_field for which you want the value
* @param string $format Optional the format (is necessary for timestamps)
* @return string
*/
public function getPropertyById($fieldId, $column, $format = '')
{
foreach($this->mProfileFields as $field)
{
if((int) $field->getValue('usf_id') === $fieldId)
{
return $field->getValue($column, $format);
}
}
return '';
}
/**
* Returns the value of the field in html format with consideration of all layout parameters
* @param string $fieldNameIntern Internal profile field name of the field that should be html formated
* @param string|int $value The value that should be formated must be commited so that layout is also possible for values that aren't stored in database
* @param string|int $value2 An optional parameter that is necessary for some special fields like email to commit the user id
* @return string Returns an html formated string that considered the profile field settings
*/
public function getHtmlValue($fieldNameIntern, $value, $value2 = '')
{
global $gPreferences, $g_root_path, $gL10n;
// if value is empty or null, then do nothing
if($value != '' && array_key_exists($fieldNameIntern, $this->mProfileFields))
{
// create html for each field type
$htmlValue = $value;
switch ($this->mProfileFields[$fieldNameIntern]->getValue('usf_type'))
{
case 'CHECKBOX':
if($value == 1)
{
$htmlValue = '<img src="'.THEME_PATH.'/icons/checkbox_checked.gif" alt="on" />';
}
else
{
$htmlValue = '<img src="'.THEME_PATH.'/icons/checkbox.gif" alt="off" />';
}
break;
case 'DATE':
if($value !== '')
{
// date must be formated
$date = DateTime::createFromFormat('Y-m-d', $value);
if(is_object($date))
{
$htmlValue = $date->format($gPreferences['system_date']);
}
}
break;
case 'EMAIL':
// the value in db is only the position, now search for the text
if($value !== '')
{
if($gPreferences['enable_mail_module'] != 1)
{
$emailLink = 'mailto:'.$value;
}
else
{
// set value2 to user id because we need a second parameter in the link to mail module
if($value2 === '')
{
$value2 = $this->mUserId;
}
$emailLink = $g_root_path.'/adm_program/modules/messages/messages_write.php?usr_id='. $value2;
}
if(strlen($value) > 30)
{
$htmlValue = '<a href="'.$emailLink.'" title="'.$value.'">'.substr($value, 0, 30).'...</a>';
}
else
{
$htmlValue = '<a href="'.$emailLink.'" style="overflow: visible; display: inline;" title="'.$value.'">'.$value.'</a>';
}
}
break;
case 'DROPDOWN':
case 'RADIO_BUTTON':
$arrListValuesWithKeys = array(); // array with list values and keys that represents the internal value
// first replace windows new line with unix new line and then create an array
$valueFormated = str_replace("\r\n", "\n", $this->mProfileFields[$fieldNameIntern]->getValue('usf_value_list', 'database'));
$arrListValues = explode("\n", $valueFormated);
foreach($arrListValues as $key => &$listValue)
{
// if value is imagefile or imageurl then show image
if($this->mProfileFields[$fieldNameIntern]->getValue('usf_type') === 'RADIO_BUTTON'
&& (strpos(admStrToLower($listValue), '.png') > 0 || strpos(admStrToLower($listValue), '.jpg') > 0))
{
// if there is imagefile and text separated by | then explode them
if(strpos($listValue, '|') > 0)
{
$listValueImage = substr($listValue, 0, strpos($listValue, '|'));
$listValueText = substr($listValue, strpos($listValue, '|') + 1);
}
else
{
$listValueImage = $listValue;
$listValueText = $this->getValue('usf_name');
}
// if text is a translation-id then translate it
if(strpos($listValueText, '_') === 3)
{
$listValueText = $gL10n->get(admStrToUpper($listValueText));
}
try
{
// create html for optionbox entry
if(strpos(admStrToLower($listValueImage), 'http') === 0 && strValidCharacters($listValueImage, 'url'))
{
$listValue = '<img class="admidio-icon-info" src="'.$listValueImage.'" title="'.$listValueText.'" alt="'.$listValueText.'" />';
}
elseif(admStrIsValidFileName($listValueImage, true))
{
$listValue = '<img class="admidio-icon-info" src="'.THEME_PATH.'/icons/'.$listValueImage.'" title="'.$listValueText.'" alt="'.$listValueText.'" />';
}
}
catch(AdmException $e)
{
$e->showText();
// => EXIT
}
}
// if text is a translation-id then translate it
if(strpos($listValue, '_') === 3)
{
$listValue = $gL10n->get(admStrToUpper($listValue));
}
// save values in new array that starts with key = 1
$arrListValuesWithKeys[++$key] = $listValue;
}
unset($listValue);
$htmlValue = $arrListValuesWithKeys[$value];
break;
case 'PHONE':
if($value !== '')
{
$htmlValue = '<a href="tel:'. str_replace(array('-', '/', ' ', '(', ')'), '', $value).'">'. $value. '</a>';
}
break;
case 'URL':
if($value !== '')
{
if(strlen($value) > 35)
{
$htmlValue = '<a href="'. $value.'" target="_blank" title="'. $value.'">'. substr($value, strpos($value, '//') + 2, 35). '...</a>';
}
else
{
$htmlValue = '<a href="'. $value.'" target="_blank" title="'. $value.'">'. substr($value, strpos($value, '//') + 2). '</a>';
}
}
break;
case 'TEXT_BIG':
$htmlValue = nl2br($value);
break;
}
// if field has url then create a link
if($this->mProfileFields[$fieldNameIntern]->getValue('usf_url') !== '')
{
if($fieldNameIntern === 'FACEBOOK' && is_numeric($value))
{
// facebook has two different profile urls (id and facebook name),
// we could only store one way in database (facebook name) and the other (id) is defined here :)
$htmlValue = '<a href="https://www.facebook.com/profile.php?id='.$value.'" target="_blank">'.$htmlValue.'</a>';
}
else
{
$htmlValue = '<a href="'.$this->mProfileFields[$fieldNameIntern]->getValue('usf_url').'" target="_blank">'.$htmlValue.'</a>';
}
// replace a variable in url with user value
if(strpos($this->mProfileFields[$fieldNameIntern]->getValue('usf_url'), '#user_content#') !== false)
{
$htmlValue = preg_replace('/#user_content#/', $value, $htmlValue);
}
}
$value = $htmlValue;
}
else
{
// special case for type CHECKBOX and no value is there, then show unchecked checkbox
if(array_key_exists($fieldNameIntern, $this->mProfileFields)
&& $this->mProfileFields[$fieldNameIntern]->getValue('usf_type') === 'CHECKBOX')
{
$value = '<img src="'.THEME_PATH.'/icons/checkbox.gif" alt="off" />';
// if field has url then create a link
if($this->mProfileFields[$fieldNameIntern]->getValue('usf_url') !== '')
{
$value = '<a href="'.$this->mProfileFields[$fieldNameIntern]->getValue('usf_url').'" target="_blank">'.$value.'</a>';
}
}
}
return $value;
}
/**
* Returns the user value for this column @n
* format = 'd.m.Y' : a date or timestamp field accepts the format of the PHP date() function @n
* format = 'html' : returns the value in html-format if this is necessary for that field type @n
* format = 'database' : returns the value that is stored in database with no format applied
* @param string $fieldNameIntern Expects the @b usf_name_intern of table @b adm_user_fields
* @param string $format Returns the field value in a special format @b text, @b html, @b database
* or datetime (detailed description in method description)
* @return string|int|bool Returns the value for the column.
*/
public function getValue($fieldNameIntern, $format = '')
{
global $gL10n, $gPreferences;
$value = '';
// exists a profile field with that name ?
// then check if user has a data object for this field and then read value of this object
if(array_key_exists($fieldNameIntern, $this->mProfileFields)
&& array_key_exists($this->mProfileFields[$fieldNameIntern]->getValue('usf_id'), $this->mUserData))
{
$value = $this->mUserData[$this->mProfileFields[$fieldNameIntern]->getValue('usf_id')]->getValue('usd_value', $format);
if($format !== 'database')
{
if($fieldNameIntern === 'COUNTRY')
{
if ($value !== '')
{
// read the language name of the country
$value = $gL10n->getCountryByCode($value);
}
}
else
{
switch ($this->mProfileFields[$fieldNameIntern]->getValue('usf_type'))
{
case 'DATE':
if ($value !== '')
{
// if no format or html is set then show date format from Admidio settings
if($format === '' || $format === 'html')
{
$dateFormat = $gPreferences['system_date'];
}
else
{
$dateFormat = $format;
}
// if date field then the current date format must be used
$date = DateTime::createFromFormat('Y-m-d', $value);
if($date === false)
{
return $value;
}
$value = $date->format($dateFormat);
}
break;
case 'DROPDOWN':
case 'RADIO_BUTTON':
// the value in db is only the position, now search for the text
if($value > 0 && $format !== 'html')
{
$arrListValues = $this->mProfileFields[$fieldNameIntern]->getValue('usf_value_list');
$value = $arrListValues[$value];
}
break;
}
}
}
}
// get html output for that field type and value
if($format === 'html')
{
$value = $this->getHtmlValue($fieldNameIntern, $value);
}
return $value;
}
/**
* If this method is called than all further calls of method @b setValue will not check the values.
* The values will be stored in database without any inspections !
*/
public function noValueCheck()
{
$this->noValueCheck = true;
}
/**
* Reads the profile fields structure out of database table @b adm_user_fields
* and adds an object for each field structure to the @b mProfileFields array.
* @param int $organizationId The id of the organization for which the profile fields
* structure should be read.
*/
public function readProfileFields($organizationId)
{
// first initialize existing data
$this->mProfileFields = array();
$this->clearUserData();
// read all user fields and belonging category data of organization
$sql = 'SELECT *
FROM '.TBL_USER_FIELDS.'
INNER JOIN '.TBL_CATEGORIES.'
ON cat_id = usf_cat_id
WHERE cat_org_id IS NULL
OR cat_org_id = '.$organizationId.'
ORDER BY cat_sequence ASC, usf_sequence ASC';
$userFieldsStatement = $this->mDb->query($sql);
while($row = $userFieldsStatement->fetch())
{
if(!array_key_exists($row['usf_name_intern'], $this->mProfileFields))
{
$this->mProfileFields[$row['usf_name_intern']] = new TableUserField($this->mDb);
}
$this->mProfileFields[$row['usf_name_intern']]->setArray($row);
}
}
/**
* Reads the user data of all profile fields out of database table @b adm_user_data
* and adds an object for each field data to the @b mUserData array.
* If profile fields structure wasn't read, this will be done before.
* @param int $userId The id of the user for which the user data should be read.
* @param int $organizationId The id of the organization for which the profile fields
* structure should be read if necessary.
*/
public function readUserData($userId, $organizationId)
{
if(count($this->mProfileFields) === 0)
{
$this->readProfileFields($organizationId);
}
if($userId > 0)
{
// remember the user
$this->mUserId = $userId;
// read all user data of user
$sql = 'SELECT *
FROM '.TBL_USER_DATA.'
INNER JOIN '.TBL_USER_FIELDS.'
ON usf_id = usd_usf_id
WHERE usd_usr_id = '.$userId;
$userDataStatement = $this->mDb->query($sql);
while($row = $userDataStatement->fetch())
{
if(!array_key_exists($row['usd_usf_id'], $this->mUserData))
{
$this->mUserData[$row['usd_usf_id']] = new TableAccess($this->mDb, TBL_USER_DATA, 'usd');
}
$this->mUserData[$row['usd_usf_id']]->setArray($row);
}
}
}
/**
* save data of every user field
* @param int $userId id is necessary if new user, that id was not known before
*/
public function saveUserData($userId)
{
$this->mDb->startTransaction();
foreach($this->mUserData as $value)
{
// if new user than set user id
if($this->mUserId === 0)
{
$value->setValue('usd_usr_id', $userId);
}
// if value exists and new value is empty then delete entry
if($value->getValue('usd_id') > 0 && $value->getValue('usd_value') === '')
{
$value->delete();
}
else
{
$value->save();
}
}
$this->columnsValueChanged = false;
$this->mUserId = $userId;
$this->mDb->endTransaction();
}
/**
* Set the database object for communication with the database of this class.
* @param \Database $database An object of the class Database. This should be the global $gDb object.
*/
public function setDatabase(&$database)
{
$this->mDb =& $database;
}
/**
* set value for column usd_value of field
* @param string $fieldNameIntern
* @param mixed $fieldValue
* @return bool
*/
public function setValue($fieldNameIntern, $fieldValue)
{
global $gPreferences;
$returnCode = false;
if($fieldValue !== '')
{
switch ($this->mProfileFields[$fieldNameIntern]->getValue('usf_type'))
{
case 'CHECKBOX':
// Checkbox darf nur 0 oder 1 haben
if(!$this->noValueCheck && $fieldValue !== '0' && $fieldValue !== '1')
{
return false;
}
break;
case 'DATE':
// Datum muss gueltig sein und formatiert werden
$date = DateTime::createFromFormat($gPreferences['system_date'], $fieldValue);
if($date === false)
{
if(!$this->noValueCheck)
{
return false;
}
}
else
{
$fieldValue = $date->format('Y-m-d');
}
break;
case 'EMAIL':
// Email darf nur gueltige Zeichen enthalten und muss einem festen Schema entsprechen
$fieldValue = admStrToLower($fieldValue);
if (!$this->noValueCheck && !strValidCharacters($fieldValue, 'email'))
{
return false;
}
break;
case 'NUMBER':
// A number must be numeric
if(!is_numeric($fieldValue) && !$this->noValueCheck)
{
return false;
}
// numbers don't have leading zero
$fieldValue = ltrim($fieldValue, '0');
break;
case 'DECIMAL':
// A number must be numeric
if(!$this->noValueCheck && !is_numeric(strtr($fieldValue, ',.', '00')))
{
return false;
}
// numbers don't have leading zero
$fieldValue = ltrim($fieldValue, '0');
break;
case 'PHONE':
// check phone number for valid characters
if (!$this->noValueCheck && !strValidCharacters($fieldValue, 'phone'))
{
return false;
}
break;
case 'URL':
// Set http hat the beginning if no protocol was defined
if(strpos(admStrToLower($fieldValue), 'http://') === false
&& strpos(admStrToLower($fieldValue), 'https://') === false)
{
$fieldValue = 'http://'. $fieldValue;
}
// now check url for valid characters
if (!$this->noValueCheck && !strValidCharacters($fieldValue, 'url'))
{
return false;
}
break;
}
}
// first check if user has a data object for this field and then set value of this user field
if(array_key_exists($this->mProfileFields[$fieldNameIntern]->getValue('usf_id'), $this->mUserData))
{
$returnCode = $this->mUserData[$this->mProfileFields[$fieldNameIntern]->getValue('usf_id')]->setValue('usd_value', $fieldValue);
}
elseif(array_key_exists($fieldNameIntern, $this->mProfileFields) && $fieldValue !== '')
{
$userData = &$this->mUserData[$this->mProfileFields[$fieldNameIntern]->getValue('usf_id')];
$userData = new TableAccess($this->mDb, TBL_USER_DATA, 'usd');
$userData->setValue('usd_usf_id', $this->mProfileFields[$fieldNameIntern]->getValue('usf_id'));
$userData->setValue('usd_usr_id', $this->mUserId);
$returnCode = $userData->setValue('usd_value', $fieldValue);
}
if($returnCode && $this->mUserData[$this->mProfileFields[$fieldNameIntern]->getValue('usf_id')]->hasColumnsValueChanged())
{
$this->columnsValueChanged = true;
}
return $returnCode;
}
}