| @copyright 2007-2026 (c) AVE.cms | @link https://ave-cms.ru | @version 3.3 */ namespace App\Frontend\Auth; defined('BASEPATH') || die('Direct access to this location is not allowed.'); use DB; use App\Content\PublicUserTables; class ProfileRepository { public function fields($context = 'profile') { $column = (string) $context === 'registration' ? 'show_registration' : 'show_profile'; $rows = DB::query('SELECT * FROM `' . PublicUserTables::table('user_profile_fields') . '` WHERE is_active=1 AND `' . $column . '`=1 ORDER BY position ASC,id ASC')->getAll(); $out = array(); foreach ($rows ?: array() as $row) { $field = (array) $row; $field['choices'] = $this->choices(isset($field['options']) ? $field['options'] : ''); $out[] = $field; } return $out; } public function values($userId) { $rows = DB::query('SELECT field_id,value FROM `' . PublicUserTables::table('user_profile_values') . '` WHERE user_id=%i', (int) $userId)->getAll(); $out = array(); foreach ($rows ?: array() as $row) { $row = (array) $row; $out[(int) $row['field_id']] = (string) $row['value']; } return $out; } public function save($userId, array $values, $context = 'profile') { foreach ($this->fields($context) as $field) { $fieldId = (int) $field['id']; $value = isset($values[$fieldId]) ? $values[$fieldId] : ''; if ((string) $field['type'] === 'checkbox') { $value = !empty($value) ? '1' : '0'; } elseif (is_array($value)) { $value = implode(',', array_map('trim', $value)); } else { $value = trim((string) $value); } DB::query('INSERT INTO `' . PublicUserTables::table('user_profile_values') . '` (user_id,field_id,value,updated_at) VALUES (%i,%i,%s,%i)' . ' ON DUPLICATE KEY UPDATE value=VALUES(value),updated_at=VALUES(updated_at)', (int) $userId, $fieldId, $value, time()); } } public function validate(array $values, $context = 'profile') { $errors = array(); foreach ($this->fields($context) as $field) { $value = isset($values[(int) $field['id']]) ? $values[(int) $field['id']] : ''; if (!empty($field['is_required']) && trim(is_array($value) ? implode('', $value) : (string) $value) === '') { $errors['extra_' . (int) $field['id']] = 'Заполните поле «' . (string) $field['name'] . '».'; } } return $errors; } protected function choices($value) { $json = json_decode((string) $value, true); if (is_array($json)) { return $json; } return array_values(array_filter(array_map('trim', preg_split('/[\r\n,]+/', (string) $value)))); } }