/
Francuz
/
DailyPort
Обзор
Документация
Войти
/
Francuz
/
DailyPort
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/UserProfile.php
179 строк
7 KB
Francuz
feat: страницы Пользователи и Настройки
15 июн 2026, 20:37
15 июн 2026, 20:37
5065114
Код
Авторство
О чём код?
<?php /** * Класс UserProfile — расширенный профиль пользователя. * * Управляет: * - личными данными (ФИО, фото, описание) * - телефонами (несколько, один основной) * - дополнительными аккаунтами (почты, мессенджеры) * - статусом активности * - правами доступа */ class UserProfile { private Database $db; public function __construct(Database $db) { $this->db = $db; } /** Получить всех пользователей */ public function all(): array { $result = $this->db->query("SELECT * FROM users ORDER BY id ASC"); return $result->fetch_all(MYSQLI_ASSOC); } /** Получить только активных */ public function active(): array { $result = $this->db->query("SELECT * FROM users WHERE is_active = 1 ORDER BY id ASC"); return $result->fetch_all(MYSQLI_ASSOC); } /** Найти по ID */ public function find(int $id): ?array { $id = $this->db->escape((string)$id); $result = $this->db->query("SELECT * FROM users WHERE id = $id"); return $result->fetch_assoc() ?: null; } /** Получить телефоны пользователя */ public function getPhones(int $userId): array { $userId = $this->db->escape((string)$userId); $result = $this->db->query("SELECT * FROM user_phones WHERE user_id = $userId ORDER BY is_primary DESC, sort_order ASC"); return $result->fetch_all(MYSQLI_ASSOC); } /** Получить доп. аккаунты пользователя */ public function getAccounts(int $userId): array { $userId = $this->db->escape((string)$userId); $result = $this->db->query("SELECT * FROM user_accounts WHERE user_id = $userId ORDER BY sort_order ASC"); return $result->fetch_all(MYSQLI_ASSOC); } /** Получить полный профиль (user + phones + accounts) */ public function getFull(int $userId): ?array { $user = $this->find($userId); if (!$user) return null; $user['phones'] = $this->getPhones($userId); $user['accounts'] = $this->getAccounts($userId); return $user; } /** Создать пользователя */ public function create(array $data): int { $name = $this->db->escape($data['name'] ?? ''); $email = $this->db->escape($data['email'] ?? ''); $password = $this->db->escape(md5($data['password'] ?? '')); $firstName = $this->db->escape($data['first_name'] ?? ''); $lastName = $this->db->escape($data['last_name'] ?? ''); $patronymic = $this->db->escape($data['patronymic'] ?? ''); $permission = (int)($data['permission'] ?? 0); $this->db->query("INSERT INTO users (name, email, password, first_name, last_name, patronymic, permission) VALUES ('$name', '$email', '$password', '$firstName', '$lastName', '$patronymic', $permission)"); return (int)$this->db->query("SELECT LAST_INSERT_ID() AS id")->fetch_assoc()['id']; } /** Обновить пользователя */ public function update(int $id, array $data): bool { $id = $this->db->escape((string)$id); $sets = []; foreach (['name', 'email', 'first_name', 'last_name', 'patronymic', 'description'] as $field) { if (array_key_exists($field, $data)) { $val = $this->db->escape($data[$field] ?? ''); $sets[] = "$field = '$val'"; } } if (array_key_exists('password', $data) && $data['password'] !== '') { $pass = $this->db->escape(md5($data['password'])); $sets[] = "password = '$pass'"; } if (array_key_exists('permission', $data)) { $perm = (int)$data['permission']; $sets[] = "permission = $perm"; } if (array_key_exists('is_active', $data)) { $active = $data['is_active'] ? 1 : 0; $sets[] = "is_active = $active"; } if (array_key_exists('photo', $data)) { $photo = $this->db->escape($data['photo'] ?? ''); $sets[] = $photo === '' ? "photo = NULL" : "photo = '$photo'"; } if (empty($sets)) return false; $this->db->query("UPDATE users SET " . implode(', ', $sets) . " WHERE id = $id"); return true; } /** Удалить пользователя */ public function delete(int $id): bool { $id = $this->db->escape((string)$id); $this->db->query("DELETE FROM users WHERE id = $id"); return true; } /** Добавить телефон */ public function addPhone(int $userId, string $phone, bool $isPrimary = false): int { $userId = $this->db->escape((string)$userId); $phone = $this->db->escape($phone); $primary = $isPrimary ? 1 : 0; if ($isPrimary) { $this->db->query("UPDATE user_phones SET is_primary = 0 WHERE user_id = $userId"); } $maxOrder = $this->db->query("SELECT COALESCE(MAX(sort_order), 0) + 1 AS next FROM user_phones WHERE user_id = $userId")->fetch_assoc(); $order = $maxOrder['next']; $this->db->query("INSERT INTO user_phones (user_id, phone, is_primary, sort_order) VALUES ($userId, '$phone', $primary, $order)"); return (int)$this->db->query("SELECT LAST_INSERT_ID() AS id")->fetch_assoc()['id']; } /** Удалить телефон */ public function deletePhone(int $id): bool { $id = $this->db->escape((string)$id); $this->db->query("DELETE FROM user_phones WHERE id = $id"); return true; } /** Добавить аккаунт */ public function addAccount(int $userId, string $type, string $value): int { $userId = $this->db->escape((string)$userId); $type = $this->db->escape($type); $value = $this->db->escape($value); $maxOrder = $this->db->query("SELECT COALESCE(MAX(sort_order), 0) + 1 AS next FROM user_accounts WHERE user_id = $userId")->fetch_assoc(); $order = $maxOrder['next']; $this->db->query("INSERT INTO user_accounts (user_id, type, value, sort_order) VALUES ($userId, '$type', '$value', $order)"); return (int)$this->db->query("SELECT LAST_INSERT_ID() AS id")->fetch_assoc()['id']; } /** Удалить аккаунт */ public function deleteAccount(int $id): bool { $id = $this->db->escape((string)$id); $this->db->query("DELETE FROM user_accounts WHERE id = $id"); return true; } }