/
ildar8919
/
Test
Обзор
Документация
Войти
/
ildar8919
/
Test
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
local/php_interface/lib/orm/EmployeeManager.php
225 строк
8 KB
Ildar
Новые доработки по ТЗ
25 июн 2026, 10:16
25 июн 2026, 10:16
fe08a4f
Код
Авторство
О чём код?
<?php namespace Orm; use Bitrix\Main\Loader; use Bitrix\Main\Application; class EmployeeManager { private $errors = []; public function create(array $data): ?int { try { $connection = Application::getConnection(); $lastName = $connection->getSqlHelper()->forSql($data['LAST_NAME'] ?? ''); $name = $connection->getSqlHelper()->forSql($data['NAME'] ?? ''); $secondName = $connection->getSqlHelper()->forSql($data['SECOND_NAME'] ?? ''); $vacationDays = (int)($data['VACATION_DAYS'] ?? 0); $gender = $connection->getSqlHelper()->forSql($data['GENDER'] ?? ''); $dealId = !empty($data['DEAL_ID']) ? (int)$data['DEAL_ID'] : 'NULL'; $birthDate = 'NULL'; if (!empty($data['BIRTH_DATE']) && is_object($data['BIRTH_DATE'])) { $birthDate = "'" . $data['BIRTH_DATE']->format('Y-m-d') . "'"; } global $USER; $createdBy = is_object($USER) ? (int)$USER->GetID() : 1; $sql = " INSERT INTO b_employees ( LAST_NAME, NAME, SECOND_NAME, VACATION_DAYS, GENDER, DEAL_ID, CREATED_BY, BIRTH_DATE, DATE_CREATE ) VALUES ( '$lastName', '$name', '$secondName', $vacationDays, '$gender', $dealId, $createdBy, $birthDate, NOW() ) "; $connection->queryExecute($sql); $id = $connection->getInsertedId(); return $id; } catch (\Exception $e) { $this->errors[] = $e->getMessage(); return null; } } public function update(int $id, array $data): bool { try { $connection = Application::getConnection(); $lastName = $connection->getSqlHelper()->forSql($data['LAST_NAME'] ?? ''); $name = $connection->getSqlHelper()->forSql($data['NAME'] ?? ''); $secondName = $connection->getSqlHelper()->forSql($data['SECOND_NAME'] ?? ''); $vacationDays = (int)($data['VACATION_DAYS'] ?? 0); $gender = $connection->getSqlHelper()->forSql($data['GENDER'] ?? ''); $dealId = !empty($data['DEAL_ID']) ? (int)$data['DEAL_ID'] : 'NULL'; $birthDate = 'NULL'; if (!empty($data['BIRTH_DATE']) && is_object($data['BIRTH_DATE'])) { $birthDate = "'" . $data['BIRTH_DATE']->format('Y-m-d') . "'"; } global $USER; $modifiedBy = is_object($USER) ? (int)$USER->GetID() : 1; $sql = " UPDATE b_employees SET LAST_NAME = '$lastName', NAME = '$name', SECOND_NAME = '$secondName', VACATION_DAYS = $vacationDays, GENDER = '$gender', DEAL_ID = $dealId, BIRTH_DATE = $birthDate, MODIFIED_BY = $modifiedBy, DATE_MODIFY = NOW() WHERE ID = " . (int)$id . " "; $connection->queryExecute($sql); return true; } catch (\Exception $e) { $this->errors[] = $e->getMessage(); return false; } } public function delete(int $id): bool { try { $connection = Application::getConnection(); $sql = "DELETE FROM b_employees WHERE ID = " . (int)$id; $connection->queryExecute($sql); return true; } catch (\Exception $e) { $this->errors[] = $e->getMessage(); return false; } } public function getById(int $id): ?array { try { $connection = Application::getConnection(); $sql = "SELECT * FROM b_employees WHERE ID = " . (int)$id; $result = $connection->query($sql); return $result->fetch(); } catch (\Exception $e) { $this->errors[] = $e->getMessage(); return null; } } public function getList(array $filter = [], array $order = ['ID' => 'ASC'], int $limit = 20, int $offset = 0): array { try { $connection = Application::getConnection(); $where = "1=1"; if (!empty($filter['%LAST_NAME'])) { $where .= " AND LAST_NAME LIKE '%" . $connection->getSqlHelper()->forSql($filter['%LAST_NAME']) . "%'"; } if (!empty($filter['%NAME'])) { $where .= " AND NAME LIKE '%" . $connection->getSqlHelper()->forSql($filter['%NAME']) . "%'"; } if (!empty($filter['GENDER'])) { $where .= " AND GENDER = '" . $connection->getSqlHelper()->forSql($filter['GENDER']) . "'"; } $orderStr = "ID ASC"; if (!empty($order)) { $orderField = key($order); $orderDir = $order[$orderField] ?? 'ASC'; $orderStr = $orderField . " " . $orderDir; } $sql = " SELECT * FROM b_employees WHERE $where ORDER BY $orderStr LIMIT " . (int)$limit . " OFFSET " . (int)$offset . " "; $result = $connection->query($sql); $items = []; while ($row = $result->fetch()) { $items[] = $row; } return $items; } catch (\Exception $e) { $this->errors[] = $e->getMessage(); return []; } } public function getCount(array $filter = []): int { try { $connection = Application::getConnection(); $where = "1=1"; if (!empty($filter['%LAST_NAME'])) { $where .= " AND LAST_NAME LIKE '%" . $connection->getSqlHelper()->forSql($filter['%LAST_NAME']) . "%'"; } if (!empty($filter['%NAME'])) { $where .= " AND NAME LIKE '%" . $connection->getSqlHelper()->forSql($filter['%NAME']) . "%'"; } if (!empty($filter['GENDER'])) { $where .= " AND GENDER = '" . $connection->getSqlHelper()->forSql($filter['GENDER']) . "'"; } $sql = "SELECT COUNT(*) as CNT FROM b_employees WHERE $where"; $result = $connection->query($sql); $row = $result->fetch(); return (int)($row['CNT'] ?? 0); } catch (\Exception $e) { $this->errors[] = $e->getMessage(); return 0; } } public function getDeals(): array { $deals = []; if (!Loader::includeModule('crm')) { return $deals; } try { $dbDeals = \CCrmDeal::GetList( ['TITLE' => 'ASC'], ['CHECK_PERMISSIONS' => 'N'], ['ID', 'TITLE'], ['nTopCount' => 100] ); while ($deal = $dbDeals->Fetch()) { $deals[$deal['ID']] = $deal['TITLE']; } } catch (\Exception $e) { // Игнорируем ошибки CRM } return $deals; } public function getErrors(): array { return $this->errors; } }