diff --git a/kodorvan/constructor/system/localizations/english.php b/kodorvan/constructor/system/localizations/english.php index cc96c97..7b3a1cb 100644 --- a/kodorvan/constructor/system/localizations/english.php +++ b/kodorvan/constructor/system/localizations/english.php @@ -9,6 +9,7 @@ return [ // Main menu 'menu_title' => 'Main menu', + 'menu_update' => 'Last update', // Account 'account_title' => 'Account', diff --git a/kodorvan/constructor/system/models/account.php b/kodorvan/constructor/system/models/account.php index 1eaf6f7..09211c4 100644 --- a/kodorvan/constructor/system/models/account.php +++ b/kodorvan/constructor/system/models/account.php @@ -8,7 +8,9 @@ namespace kodorvan\constructor\models; use kodorvan\constructor\models\core, kodorvan\constructor\models\authorizations, kodorvan\constructor\models\settings, - kodorvan\constructor\models\projects; + kodorvan\constructor\models\project, + kodorvan\constructor\models\project\enumerations\type as project_type, + kodorvan\constructor\models\project\enumerations\status as project_status; // The library for languages support use mirzaev\languages\language; @@ -229,11 +231,11 @@ final class account extends core implements record_interface $settings->write(account: $record->identifier); // Writing the record into the database - $record = $this->database->read( + /* $record = $this->database->read( filter: fn(record $_record) => $_record->identifier === $record->identifier, update: fn(record &$_record) => $_record = $record, amount: 1 - )[0] ?? null; + )[0] ?? null; */ // Exit (success) return $record; @@ -370,14 +372,26 @@ final class account extends core implements record_interface * * Search for the account projects * + * @param project_type|null $type Type of the project + * @param project_status|null $status Status of the project * @param int $amount Maximum amount * * @return array The account projects */ - public function projects(int $amount = 20): array - { + public function projects( + ?project_type $type = null, + ?project_status $status = null, + int $amount = 20 + ): array { // Search for the account projects - $projects = new project()->database->read(filter: fn(record $record) => $record->active === 1 && $record->account === $this->identifier, amount: $amount); + $projects = new project()->database->read( + filter: fn(record $record) => + $record->active === 1 + && $record->account === $this->identifier + && ($type === null || $record->type === $type->name) + && ($status === null || $record->status === $status->name), + amount: $amount + ); // Exit (success/fail) return $projects; diff --git a/kodorvan/constructor/system/models/authorizations.php b/kodorvan/constructor/system/models/authorizations.php index 51113f6..ef7858e 100644 --- a/kodorvan/constructor/system/models/authorizations.php +++ b/kodorvan/constructor/system/models/authorizations.php @@ -92,7 +92,7 @@ final class authorizations extends core implements record_interface * @param bool $system_settings * @param bool $active Is the record active? * - * @return int|false The record identifier, if created + * @return int|false The record, if created */ public function write( int $account, @@ -100,7 +100,7 @@ final class authorizations extends core implements record_interface bool $settings = true, bool $system_settings = false, bool $active = true, - ): int|false + ): record|false { $record = $this->database->record( $this->database->count() + 1, @@ -117,7 +117,7 @@ final class authorizations extends core implements record_interface $created = $this->database->write($record); // Exit (success) - return $created ? $record->identifier : false; + return $created ? $record : false; } /** diff --git a/kodorvan/constructor/system/models/project.php b/kodorvan/constructor/system/models/project.php index f449f57..f3080df 100644 --- a/kodorvan/constructor/system/models/project.php +++ b/kodorvan/constructor/system/models/project.php @@ -5,7 +5,9 @@ declare(strict_types=1); namespace kodorvan\constructor\models; // Files of the project -use kodorvan\constructor\models\core; +use kodorvan\constructor\models\core, + kodorvan\constructor\models\project\enumerations\status as project_status, + kodorvan\constructor\models\project\enumerations\status as project_type; // Baza database use mirzaev\baza\database, @@ -66,6 +68,8 @@ final class project extends core implements record_interface ->columns( new column('identifier', type::long_long_unsigned), new column('account', type::long_long_unsigned), + new column('status', type::string, ['length' => 16]), + new column('type', type::string, ['length' => 32]), new column('name', type::string, ['length' => 64]), /* new column('', type::), */ new column('active', type::char), @@ -82,19 +86,32 @@ final class project extends core implements record_interface * Write * * @param int $account The account identifier - * @param string $name Name of the project + * @param project_status $status Status of the project + * @param project_type $status Type of the project + * @param string|null $name Name of the project * @param int $active Is the record active? * - * @return int|false The record identifier, if created + * @return record|false The record, if created */ public function write( int $account, - string $name = '', + project_status $status = project_status::creating, + project_type $type = project_type::special, + ?string $name = null, bool $active = true, - ): int|false { + ): record|false { + if (empty($name)) { + // Not received the project name + + // Generating the project name + $name = 'Project №' . count(new account()->read(filter: fn(record $record) => $record->active === 1 && $record->account === $account)?->projects() ?? []); + } + $record = $this->database->record( $this->database->count() + 1, $account, + $status->name, + $type->name, $name, (int) $active, svoboda::timestamp(), @@ -105,7 +122,7 @@ final class project extends core implements record_interface $created = $this->database->write($record); // Exit (success) - return $created ? $record->identifier : false; + return $created ? $record : false; } /** @@ -115,8 +132,20 @@ final class project extends core implements record_interface */ public function serialize(): self { + if ($this->serialized) { + // The record implementor is serialized + + // Exit (fail) + throw new exception_runtime('The record implementor is already serialized'); + } + // Serializing the record parameters $this->record->active = (int) $this->record->active; + $this->record->status = $this->record->status->name; + $this->record->type = $this->record->type->name; + + // Writing the status of serializing + $this->serialized = true; // Exit (success) return $this; @@ -129,11 +158,52 @@ final class project extends core implements record_interface */ public function deserialize(): self { + if (!$this->serialized) { + // The record implementor is deserialized + + // Exit (fail) + throw new exception_runtime('The record implementor is already deserialized'); + } + // Deserializing the record parameters $this->record->active = (bool) $this->record->active; + $this->record->status = project_status::{$this->record->status}; + $this->record->type = project_status::{$this->record->type}; + + // Writing the status of serializing + $this->serialized = false; + + // Exit (success) + return $this; + } + + /** + * Parameters + * + * Search for all the project properties + * + * @return self The instance from which the method was called (fluent interface) + */ + public function parameters(): self + { + // Deserializing the record parameters + $this->record->active = (bool) $this->record->active; + $this->record->status = project_status::{$this->record->status}; + + if (!$this->serialized) { + // Not serialized + + + } else { + // Serialized + + // Exit (fail) + throw new exception('The project implementator is serialized'); + } + /* if ($this->record->type === '') */ + // Exit (success) return $this; } } - diff --git a/kodorvan/constructor/system/models/project/enumerations/status.php b/kodorvan/constructor/system/models/project/enumerations/status.php new file mode 100644 index 0000000..860b599 --- /dev/null +++ b/kodorvan/constructor/system/models/project/enumerations/status.php @@ -0,0 +1,27 @@ + + */ +enum status +{ + case creating; + case calculated; + case requested; + case developing; + case developed; + case launched; +} diff --git a/kodorvan/constructor/system/models/settings.php b/kodorvan/constructor/system/models/settings.php index d6620b3..98a8bc5 100644 --- a/kodorvan/constructor/system/models/settings.php +++ b/kodorvan/constructor/system/models/settings.php @@ -83,12 +83,12 @@ final class settings extends core implements record_interface * @param int $account The account identifier * @param int $active Is the record active? * - * @return int|false The record identifier, if created + * @return int|false The record, if created */ public function write( int $account, bool $active = true, - ): int|false { + ): record|false { $record = $this->database->record( $this->database->count() + 1, $account, @@ -101,7 +101,7 @@ final class settings extends core implements record_interface $created = $this->database->write($record); // Exit (success) - return $created ? $record->identifier : false; + return $created ? $record : false; } /** @@ -132,4 +132,3 @@ final class settings extends core implements record_interface return $this; } } - diff --git a/kodorvan/constructor/system/models/telegram/commands.php b/kodorvan/constructor/system/models/telegram/commands.php index 8bdb728..c286fb9 100644 --- a/kodorvan/constructor/system/models/telegram/commands.php +++ b/kodorvan/constructor/system/models/telegram/commands.php @@ -78,8 +78,8 @@ final class commands extends core $experiment = '⚠️ ' . $localization['menu_experiment']; // Initializing the message last update text - exec(command: 'git log --oneline $(git describe --tags --abbrev=0 @^)..@ -1 --format="%at" | xargs -I{} date -d @{} "+%Y.%m.%d %H:%M"', output: $git); - $update = empty($git) ? '' : "\n*" . $localization['menu_update'] . ':* ' . $git; + exec(command: 'git log --oneline $(git describe --tags --abbrev=0 @^ --always)..@ -1 --format="%at" | xargs -I{} date -d @{} "+%Y.%m.%d %H:%M"', output: $git); + $update = empty($git[0]) ? '' : "\n\n🔏 *" . $localization['menu_update'] . ':* ' . unmarkdown($git[0]); // Sending the message $context->sendMessage( @@ -96,7 +96,7 @@ final class commands extends core [ [ 'text' => '📂 ' . $localization['menu_button_project_new'], - 'callback_data' => 'calculator' + 'callback_data' => 'project_create' ], [ 'text' => '🗂 ' . $localization['menu_button_projects'] . ': ' . $projects, diff --git a/kodorvan/constructor/system/models/telegram/processes/project/create.php b/kodorvan/constructor/system/models/telegram/processes/project/create.php new file mode 100644 index 0000000..fe17b58 --- /dev/null +++ b/kodorvan/constructor/system/models/telegram/processes/project/create.php @@ -0,0 +1,894 @@ + + */ +final class create extends core +{ + /** + * Start + * + * Starting the account localization creating process + * + * @param context $context Request data from Telegram + * + * @return void + */ + public static function start(context $context): void + { + // Initializing the account + $account = $context->get('account'); + + if ($account instanceof account) { + // Initialized the account + + // Initializing language + $language = $context->get('language'); + + if ($language instanceof language) { + // Initialized language + + // Initializing localization + $localization = $context->get('localization'); + + if ($localization) { + // Initialized localization + + // Searching for the project + $project = $account->projects(status: project_status::creation, amount: 1)[0] ?? null; + + if ($project instanceof project) { + // Found the project + + // Sending the project creation menu + static::menu($context); + } else { + // Not found the project + + // Initializing buffer of languages + $languages = language::cases(); + + // Deleting the actual language from buffer of languages + unset($languages[array_search($language, $languages, strict: true)]); + + // Sorting buffer of languages by the actual language + $languages = [$language, ...$languages]; + + // Initializing the account model + $model_account = new account; + + // Initializing the account localizations + $existed = $model_account->localization->database->read( + filter: fn(record $localization) => $localization->account === $account->identifier, + amount: ACCOUNT_LOCALIZATION_CREATE_ACCOUNT_LOCALIZATIONS_AMOUNT + ); + + if (count($existed) !== count(language::cases())) { + // Not all languages in the registry have localizations created (expected) + + // Declaring the buffer of languages to exclude + $exclude = []; + + // Initializing languages to exclude + foreach ($existed as $record) $exclude[] = $record->language; + + if (!empty($exclude)) { + // Initialized languages to exclude + + // Deleting excluded languages + $languages = array_filter($languages, fn(language $language) => array_search($language->name, $exclude, strict: true) === false); + } + + // Initializing the account localization create buffer + $process = [ + 'language' => array_values($languages)[0], + 'name' => null, + ]; + + // Writing to the telegram user buffer + $context->setUserDataItem(static::PROCESS, $process) + ->then(function () use ($context, $account, $localization) { + // Writed to the telegram user buffer + + // Sending the message + $context->sendMessage('📂 *' . $localization['account_localization_create_started'] . '*') + ->then(function (message $message) use ($context, $account, $localization) { + // Sended the message + + // Sending the account localization create menu + static::menu($context); + }); + }); + } else { + // All languages in the registry have localizations created (expected) + + // Sending the message + $context->sendMessage('⚠️ *' . $localization['account_localization_create_every_language_created'] . '*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + }); + } + } + } else { + // Not initialized localization + + // Sending the message + $context->sendMessage('⚠️ *Failed to initialize localization*') + ->then(function (message $message) use ($context) { + // Ending the conversation process + $context->endConversation(); + }); + } + } else { + // Not initialized language + + // Sending the message + $context->sendMessage('⚠️ *Failed to initialize language*') + ->then(function (message $message) use ($context) { + // Ending the conversation process + $context->endConversation(); + }); + } + } else { + // Not initialized the account + + // Sending the message + $context->sendMessage('⚠️ *Failed to initialize the account*') + ->then(function (message $message) use ($context) { + // Ending the conversation process + $context->endConversation(); + }); + } + } + + /** + * Cancel + * + * Ending the account localization create process + * without creating the localization record in the database + * + * @param context $context Request data from Telegram + * + * @return void + */ + public static function cancel(context $context): void + { + // Initializing the account + $account = $context->get('account'); + + if ($account instanceof account) { + // Initialized the account + + // Initializing localization + $localization = $context->get('localization'); + + if ($localization) { + // Initialized localization + + // Reading from the telegram user buffer + $context->getUserDataItem(static::PROCESS) + ->then(function (?array $process) use ($context, $localization) { + // Readed from the telegram user buffer + + if ($process) { + // Found started account localization create process + + // Deleting in the telegram user buffer + $context->deleteUserDataItem(static::PROCESS) + ->then(function () use ($context, $localization) { + // Deleted in the telegram user buffer + + // Sending the message + $context->sendMessage('🗑 *' . $localization['account_localization_create_canceled'] . '*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + + // Sending the account localizations menu + telegram_account::localizations($context); + }); + }); + } else { + // Not found started account localization create process + + // Sending the message + $context->sendMessage('⚠️ *' . $localization['account_localization_create_not_started'] . '*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + + // Sending the account localizations menu + telegram_account::localizations($context); + }); + } + }); + } else { + // Not initialized localization + + // Sending the message + $context->sendMessage('⚠️ *Failed to initialize localization*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + }); + } + } else { + // Not initialized the account + + // Sending the message + $context->sendMessage('⚠️ *Failed to initialize the account*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + }); + } + } + + /** + * End + * + * Ending the account localization create process + * and creating the localization record in the database + * + * @param context $context Request data from Telegram + * + * @return void + */ + public static function end(context $context): void + { + // Initializing the account + $account = $context->get('account'); + + if ($account instanceof account) { + // Initialized the account + + // Initializing language + $language = $context->get('language'); + + if ($language instanceof language) { + // Initialized language + + // Initializing localization + $localization = $context->get('localization'); + + if ($localization) { + // Initialized localization + + // Reading from the telegram user buffer + $context->getUserDataItem(static::PROCESS) + ->then(function (?array $process) use ($context, $account, $language, $localization) { + // Readed from the telegram user buffer + + if ($process) { + // Found started account localization create process + + // Initializing the account model + $model_account = new account; + + // Creating the account localization + $created_localization = $model_account->localization->create( + account: $account->identifier, + language: $process['language'], + name: $process['name'] + ); + + if ($created_localization) { + // Created the account localization + + // Sending the message + $context->sendMessage('✏️ *' . $localization['account_localization_create_created'] . '*') + ->then(function (message $message) use ($context, $account, $language, $localization) { + // Sended the message + + // Deleting from the telegram user buffer + $context->deleteUserDataItem(static::PROCESS) + ->then(function () use ($context, $localization) { + // Deleted from the telegram user buffer + + // Sending the message + $context->sendMessage('✅ *' . $localization['account_localization_create_completed'] . '*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + + // Sending the account localizations menu + telegram_account::localizations($context); + }); + }); + }); + } else { + // Not created the account localization + + // Sending the message + $context->sendMessage('⚠️ *' . $localization['account_localization_create_not_created'] . '*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + }); + } + } else { + // Not found started account localization create process + + // Sending the message + $context->sendMessage('⚠️ *' . $localization['account_localization_create_not_started'] . '*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + + // Sending the account localizations menu + telegram_account::localizations($context); + }); + } + }); + } else { + // Not initialized localization + + // Sending the message + $context->sendMessage('⚠️ *Failed to initialize localization*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + }); + } + } else { + // Not initialized language + + // Sending the message + $context->sendMessage('⚠️ *Failed to initialize language*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + }); + } + } else { + // Not initialized the account + + // Sending the message + $context->sendMessage('⚠️ *Failed to initialize the account*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + }); + } + } + + /** + * Menu + * + * Sends the account localization create menu with parameters: language, name + * When all parameters was initialized then sends the complete button + * + * @param context $context Request data from Telegram + * + * @return void + */ + protected static function menu(context $context): void + { + // Initializing the account + $account = $context->get('account'); + + if ($account instanceof account) { + // Initialized the account + + // Initializing language + $language = $context->get('language'); + + if ($language) { + // Initialized language + + // Initializing localization + $localization = $context->get('localization'); + + if ($localization) { + // Initialized localization + + // Reading from the telegram user buffer + $context->getUserDataItem(static::PROCESS) + ->then(function (?array $process) use ($context, $account, $language, $localization) { + // Readed from the telegram user buffer + + if ($process) { + // Found started account localization create process + + // Initializing the buffer of generated keyboard with languages + $keyboard = [ + [ + [ + 'text' => empty($process['language']) ? '🟢 ' . $localization['account_localization_create_button_language'] : '🟢 ' . $localization['account_localization_create_button_language'] . ': ' . $process['language']->flag() . ' ' . $process['language']->label($language), + 'callback_data' => 'account_localization_create_language' + ] + ], + [ + [ + 'text' => empty($process['name']) ? '🔴 ' . $localization['account_localization_create_button_name'] : '🟢 ' . $localization['account_localization_create_button_name'] . ': ' . $process['name'], + 'callback_data' => 'account_localization_create_name' + ] + ], + ]; + + // Initializing the index of last row + $last = count($keyboard); + + // Initializing the last row + $keyboard[$last] ??= []; + + // Initializing the button for canceling the generation process + $keyboard[$last][] = [ + 'text' => '❎ ' . $localization['account_localization_create_button_cancel'], + 'callback_data' => 'account_localization_create_cancel' + ]; + + if ( + !empty($process['language']) && + !empty($process['name']) + ) { + // Initialized all requeired parameters + + // Initializing the button for completing the generation process + $keyboard[$last][] = [ + 'text' => '✅ ' . $localization['account_localization_create_button_confirm'], + 'callback_data' => 'account_localization_create_end' + ]; + } + + // Ending the conversation process + $context->endConversation() + ->then(function () use ($context, $localization, $keyboard) { + // Deinitialized the conversation process + + // Sending the message + $context->sendMessage( + '📀 *' . $localization['account_localization_create_generation'] . '*', + [ + 'reply_markup' => [ + 'inline_keyboard' => $keyboard, + 'disable_notification' => true, + 'remove_keyboard' => true + ], + ] + ); + }); + } else { + // Not found started account localization create process + + // Sending the message + $context->sendMessage('⚠️ *' . $localization['account_localization_create_not_started'] . '*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + + // Sending the account localizations menu + telegram_account::localizations($context); + }); + } + }); + } else { + // Not initialized localization + + // Sending the message + $context->sendMessage('⚠️ *Failed to initialize localization*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + }); + } + } else { + // Not initialized language + + // Sending the message + $context->sendMessage('⚠️ *Failed to initialize language*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + }); + } + } else { + // Not initialized the account + + // Sending the message + $context->sendMessage('⚠️ *Failed to initialize the account*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + }); + } + } + + /** + * Language + * + * Write language into the account localization create buffer + * + * @param context $context Request data from Telegram + * @param language $new The language + * + * @return void + */ + public static function language(context $context, language $new): void + { + // Initializing the account + $account = $context->get('account'); + + if ($account instanceof account) { + // Initialized the account + + // Initializing language + $language = $context->get('language'); + + if ($language instanceof language) { + // Initialized language + + // Initializing localization + $localization = $context->get('localization'); + + if ($localization) { + // Initialized localization + + // Reading from the telegram user buffer + $context->getUserDataItem(static::PROCESS) + ->then(function (?array $process) use ($context, $account, $language, $localization, $new) { + // Readed from the telegram user buffer + + if ($process) { + // Found started account localization create process + + try { + // Initializing the old language + $old = $process['language']; + + // Writing into the account localization create process buffer + $process['language'] = $new; + + // Writing to the telegram user buffer + $context->setUserDataItem(static::PROCESS, $process) + ->then(function () use ($context, $account, $language, $localization, $new, $old) { + // Writed to the telegram user buffer + + // Sending the message + $context->sendMessage('✅ *' . $localization['account_localization_create_language_update_success'] . '* ' . ($old->flag() ? $old->flag() . ' ' : '') . $old->label($language) . ' → *' . ($new->flag() ? $new->flag() . ' ' : '') . $new->label($language) . '*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Sending the account localization create menu + static::menu($context); + }); + }); + } catch (error $error) { + // Failed to send the message about language update + + // Sending the message + $context->sendMessage('❎ *' . $localization['account_localization_create_language_update_fail']) + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + }); + } + } else { + // Not found started account localization create process + + // Sending the message + $context->sendMessage('⚠️ *' . $localization['account_localization_create_not_started'] . '*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + + // Sending the account localizations menu + telegram_account::localizations($context); + }); + } + }); + } else { + // Not initialized localization + + // Sending the message + $context->sendMessage('⚠️ *Failed to initialize localization*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + }); + } + } else { + // Not initialized language + + // Sending the message + $context->sendMessage('⚠️ *Failed to initialize language*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + }); + } + } else { + // Not initialized the account + + // Sending the message + $context->sendMessage('⚠️ *Failed to initialize the account*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + }); + } + } + + /** + * Name + * + * Write name into the account localization create buffer + * + * @param context $context Request data from Telegram + * + * @return void + */ + public static function name(context $context): void + { + // Initializing the account + $account = $context->get('account'); + + if ($account instanceof account) { + // Initialized the account + + // Initializing localization + $localization = $context->get('localization'); + + if ($localization) { + // Initialized localization + + // Reading from the telegram user buffer + $context->getUserDataItem(static::PROCESS) + ->then(function (?array $process) use ($context, $account, $localization) { + // Readed from the telegram user buffer + + if ($process) { + // Found started account localization create process + + // Initializing the new name + $new = $context->getMessage()->getText(); + + if (!empty($new)) { + // Initialized the new name + + if (mb_strlen($new) >= 2) { + // Passed minimum length check + + if (mb_strlen($new) <= 128) { + // Passed maximum length check + + // Search for restricted characters + preg_match_all('/[\W\d]/u', $new, $matches); + + // Declaring the buffer of found restricted characters + $characters = []; + + foreach ($matches[0] as $match) { + // Iterating over found restricted characters + + if (match ($match) { + ' ', '-' => false, + default => true + }) { + // Found a restricted character + + // Writing into the buffer of found restricted characters + $characters[] = $match; + } + } + + if (empty($characters)) { + // Not found restricted characters + + try { + // Initializing the old name + $old = empty($process['name']) ? '_' . $localization['empty'] . '_' : $process['name']; + + // Writing into the account localization create process buffer + $process['name'] = $new; + + // Writing to the telegram user buffer + $context->setUserDataItem(static::PROCESS, $process) + ->then(function () use ($context, $account, $localization, $new, $old) { + // Writed to the telegram user buffer + + // Escaping characters for markdown + $escaped = str_replace('-', '\\-', $new); + + // Sending the message + $context->sendMessage('✅ *' . $localization['account_localization_create_name_update_success'] . "* $old → *$escaped*") + ->then(function (message $message) use ($context) { + // Sended the message + + // Sending the account localization create menu + static::menu($context); + }); + }); + } catch (error $error) { + // Failed to send the message about name update + + // Sending the message + $context->sendMessage('❎ *' . $localization['account_localization_create_name_update_fail']) + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + }); + } + } else { + // Found restricted characters + + // Initializing title of the message + $title = '⚠️ *' . $localization['account_localization_create_name_request_restricted_characters_title'] . '*'; + + // Initializing description of the message + $description = '*' . $localization['account_localization_create_name_request_restricted_characters_description'] . '* \\' . implode(', \\', $characters); + + // Sending the message + $context->sendMessage( + <<then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + + // Requesting to enter name again + button_account_localization_create::name($context); + }); + } + } else { + // Not passed maximum length check + + // Sending the message + $context->sendMessage('⚠️ *' . $localization['account_localization_create_name_request_too_long'] . '*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + + // Requesting to enter name again + button_account_localization_create::name($context); + }); + } + } else { + // Not passed minimum length check + + // Sending the message + $context->sendMessage('⚠️ *' . $localization['account_localization_create_name_request_too_short'] . '*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + + // Requesting to enter name again + button_account_localization_create::name($context); + }); + } + } else { + // Failed to initialize the new name + + // Sending the message + $context->sendMessage('📄 *' . $localization['account_localization_create_name_request_not_acceptable'] . '*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + + // Requesting to enter name again + button_account_localization_create::name($context); + }); + } + } else { + // Not found started account localization create process + + // Sending the message + $context->sendMessage('⚠️ *' . $localization['account_localization_create_not_started'] . '*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + + // Sending the account localizations menu + telegram_account::localizations($context); + }); + } + }); + } else { + // Not initialized localization + + // Sending the message + $context->sendMessage('⚠️ *Failed to initialize localization*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + }); + } + } else { + // Not initialized the account + + // Sending the message + $context->sendMessage('⚠️ *Failed to initialize the account*') + ->then(function (message $message) use ($context) { + // Sended the message + + // Ending the conversation process + $context->endConversation(); + }); + } + } +} diff --git a/kodorvan/constructor/system/public/telegram.php b/kodorvan/constructor/system/public/telegram.php index 5089eac..807f520 100644 --- a/kodorvan/constructor/system/public/telegram.php +++ b/kodorvan/constructor/system/public/telegram.php @@ -92,5 +92,7 @@ foreach (language::cases() as $language) { $robot->onCbQueryData(["settings_language_$language->name"], fn(context $context) => settings::language($context, $language)); }; +$robot->onCbQueryData('project_create', ['process_project_create', 'name']); + // Starting chat-robot $robot->run();