module_title = _t('contacts', 'Contacts'); } /** * Список шаблонов уведомлений * @return array */ public function sendmailTemplates(): array { return [ 'contacts_admin' => [ 'title' => _t('contacts', 'Contact Form: New Message Notification'), 'description' => _t('contacts', 'The notification sent to the administrator ([mail]) after sending the message via contact form', [ 'mail' => bff::mailAdmin() ]), 'vars' => [ '{name}' => _t('contacts', 'Name'), '{email}' => _t('', 'Email'), '{message}' => _t('contacts', 'Message') ], 'priority' => 100, 'enotify' => -1, ], ]; } /** * Формирование URL * @param string $key ключ * @param array $params доп. параметры * @param bool $dynamic динамическая ссылка * @return string */ public function url(string $key, array $params = [], $dynamic = false): string { return $this->router->url('contacts-' . $key, $params, ['dynamic' => $dynamic, 'module' => 'contacts']); } /** * Типы контактов * @param bool $options true - в виде HTML::options, false - в виде массива * @param string|null $lang * @return array|string */ public function getContactTypes(bool $options = false, ?string $lang = null) { $types = Options::getOptions('contacts-types', $lang ?? $this->locale->current()); foreach ($types as $k => &$v) { $v = ['id' => $k, 'title' => $v]; } unset($v); if ($options) { return HTML::selectOptions($types, 0, false, 'id', 'title'); } return $types; } /** * Сохраняем сообщение из формы контактов * @param int $type тип контакта {@see getContactTypes} * @param \bff\auth\User|null $user авторизованный пользователь или null * @param array $data [message, name, email, user_ip, useragent] * @return Contact|null */ public function sendContactMessage(array $data, $type, $user) { $message = $data['message'] ?? ''; if (empty($message)) { return null; } # корректируем тип контакта $typesList = $this->getContactTypes(); if (! array_key_exists($type, $typesList)) { $type = key($typesList); } $data['user_id'] = ($user ? $user->id() : 0); $data['ctype'] = $type; $data['message'] = $message; $contact = $this->model->contact()->fill($data); if (! $contact->save()) { return null; } $this->updateCounter($type, 1); $name = $data['name'] ?? ''; $email = $data['email'] ?? ''; # Отправляем уведомление администратору bff::sendMailTemplate( [ 'name' => $name, 'email' => $email, 'message' => nl2br($message), ], 'contacts_admin', bff::mailAdmin() ); NewContact::dispatch($contact->id, $data['user_id']); return $contact; } /** * Обновление счетчика новых сообщений, отправленных через форму * @param int $typeID ID типа сообщения * @param int $increment * @return void */ protected function updateCounter(int $typeID, int $increment) { config::saveCount('contacts_new', $increment, true); config::saveCount('contacts_new_' . $typeID, $increment, true); } /** * Пересчет всех счетчиков * @return void */ protected function countersRefresh() { $newTotal = $this->model->contactsListing(['viewed' => 0], true); config::save('contacts_new', $newTotal, true); $types = $this->getContactTypes(); foreach ($types as $k => $v) { $newType = 0; if ($newTotal > 0) { $newType = $this->model->contactsListing(['viewed' => 0, 'ctype' => $k], true); } config::save('contacts_new_' . $k, $newType, true); } } /** * Метод обрабатывающий ситуацию с удалением пользователя * @param int $userID ID пользователя * @param array $options доп. параметры удаления * @return void */ public function onUserDeleted($userID, array $options = []) { $this->model->deleteUserContacts($userID); $this->countersRefresh(); } }