Skip to content

Commit

Permalink
added form for sending new message
Browse files Browse the repository at this point in the history
  • Loading branch information
konecnyjakub committed Jun 9, 2018
1 parent 5a2ee7a commit b08e771
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 12 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"php": ">=7.1.0",
"nette/application": "^2.4",
"nette/di": "^2.4.10",
"nette/forms": "^2.4",
"latte/latte": "^2.4",
"nette/utils": "^2.4",
"nexendrie/translation": "^1.1.1",
Expand Down
16 changes: 4 additions & 12 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ interface IGroupChatControlFactory {

With this code, we have created group chat. It isn't much of code, is it? Let's examine it closely now.

We create a new class which extends the abstract component from this package. In your own chats you usually need to define just constructor, the base class handles all remaining logic. The constructor of the base class requires database adapter (it will be described later), names and values for fields which identify texts and people for this chat (if both are identified by same field and value, you pass one pair and then nulls) and translator. There is currently only text to translate: "People in this room:". You have to use this parameter only if you do not want to use the default one.
We create a new class which extends the abstract component from this package. In your own chats you usually need to define just constructor, the base class handles all remaining logic. The constructor of the base class requires database adapter (it will be described later), names and values for fields which identify texts and people for this chat (if both are identified by same field and value, you pass one pair and then nulls) and translator. You have to use this parameter only if you do not want to use the default one.

The factory for component is pretty straightforward: an interface with method create.

Expand Down Expand Up @@ -161,25 +161,17 @@ use Nette\Application\UI\Form;
class ChatPresenter extends Presenter {
/** @var \App\Chat\IGroupChatControlFactory @inject */
public $groupChatFactory;
/** @var \HeroesofAbenez\Chat\NewChatMessageFormFactory @inject */
public $newMessageFormFactory;

protected function createComponentGroupChat() {
return $this->groupChatFactory->create();
}

protected function createComponentNewChatMessageForm(): Form {
$form = new Form();
$form->addText("message")
->setRequired("Enter message.");
$form->addSubmit("send", "Send");
/** @var \App\Chat\GroupChatControl $chat */
$chat = $this->groupChatFactory->create();
$form->addComponent($chat, "chat");
$form->onSuccess[] = function(Form $form, array $values) {
/** @var \App\Chat\GroupChatControl $chat */
$chat = $form->getComponent("chat");
$chat->newMessage($values["message"]);
};
return $form;
return $this->newMessageFormFactory->create($chat);
}
}
?>
Expand Down
5 changes: 5 additions & 0 deletions src/DI/ChatExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use HeroesofAbenez\Chat\IChatCommand;
use Nette\Utils\Validators;
use HeroesofAbenez\Chat\ChatControl;
use HeroesofAbenez\Chat\NewChatMessageFormFactory;
use HeroesofAbenez\Chat\IChatMessageProcessor;
use HeroesofAbenez\Chat\IDatabaseAdapter;
use Nette\DI\MissingServiceException;
Expand All @@ -25,6 +26,8 @@ final class ChatExtension extends \Nette\DI\CompilerExtension {
/** @internal */
public const SERVICE_DATABASE_ADAPTER = "databaseAdapter";
/** @internal */
public const SERVICE_NEW_MESSAGE_FORM = "newMessageForm";
/** @internal */
public const TAG_CHAT = "chat.chat";

protected $defaults = [
Expand Down Expand Up @@ -140,6 +143,8 @@ public function loadConfiguration(): void {
$databaseAdapter = $this->getDatabaseAdapter();
$builder->addDefinition($this->prefix(static::SERVICE_DATABASE_ADAPTER))
->setType($databaseAdapter);
$builder->addDefinition($this->prefix(static::SERVICE_NEW_MESSAGE_FORM))
->setType(NewChatMessageFormFactory::class);
}

protected function registerMessageProcessors(): void {
Expand Down
44 changes: 44 additions & 0 deletions src/NewChatMessageFormFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);

namespace HeroesofAbenez\Chat;

use Nette\Application\UI\Form;
use Nette\Localization\ITranslator;
use Nexendrie\Translation\Translator;
use Nexendrie\Translation\Loaders\NeonLoader;

/**
* NewChatMessageFormFactory
*
* @author Jakub Konečný
*/
final class NewChatMessageFormFactory {
/** @var ITranslator */
protected $translator;

public function __construct(?ITranslator $translator = null) {
if(is_null($translator)) {
$loader = new NeonLoader();
$loader->folders = [__DIR__ . "/lang"];
$translator = new Translator($loader);
}
$this->translator = $translator;
}

public function create(ChatControl $chatControl): Form {
$form = new Form();
$form->setTranslator($this->translator);
$form->addText("message")
->setRequired("chat.newMessageForm.messageField.empty");
$form->addSubmit("send", "chat.newMessageForm.submitButton.label");
$form->addComponent($chatControl, "chat");
$form->onSuccess[] = function(Form $form, array $values) {
/** @var ChatControl $chat */
$chat = $form->getComponent("chat");
$chat->newMessage($values["message"]);
};
return $form;
}
}
?>
5 changes: 5 additions & 0 deletions src/lang/chat.cs.neon
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
peopleInRoom: "Lidé v této místnosti:"
newMessageForm:
messageField:
empty: "Zadej zprávu."
submitButton:
label: "Odeslat"
5 changes: 5 additions & 0 deletions src/lang/chat.en.neon
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
peopleInRoom: "People in this room:"
newMessageForm:
messageField:
empty: "Enter message."
submitButton:
label: "Send"

0 comments on commit b08e771

Please sign in to comment.