1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | |||
4 | namespace HeroesofAbenez\Chat; |
||
5 | |||
6 | /** |
||
7 | * Basic Chat Control |
||
8 | * |
||
9 | * @author Jakub Konečný |
||
10 | * @property int $messagesPerPage |
||
11 | * @property string $characterProfileLink |
||
12 | * @property-read \Nette\Bridges\ApplicationLatte\Template $template |
||
13 | */ |
||
14 | 1 | abstract class ChatControl extends \Nette\Application\UI\Control { |
|
15 | protected IDatabaseAdapter $database; |
||
16 | /** @var IChatMessageProcessor[] */ |
||
17 | protected array $messageProcessors = []; |
||
18 | protected string $textColumn; |
||
19 | protected string $characterColumn; |
||
20 | protected int $textValue; |
||
21 | /** @var int */ |
||
22 | protected $characterValue; |
||
23 | protected string $characterProfileLink = ""; |
||
24 | protected string $templateFile = __DIR__ . "/chat.latte"; |
||
25 | protected int $messagesPerPage = 25; |
||
26 | |||
27 | /** |
||
28 | * @param mixed $characterValue |
||
29 | */ |
||
30 | public function __construct(IDatabaseAdapter $databaseAdapter, string $textColumn, int $textValue, string $characterColumn = null, $characterValue = null) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
31 | 1 | $this->database = $databaseAdapter; |
|
32 | 1 | $this->textColumn = $textColumn; |
|
33 | 1 | $this->characterColumn = $characterColumn ?? $textColumn; |
|
34 | 1 | $this->textValue = $textValue; |
|
35 | 1 | $this->characterValue = $characterValue ?? $textValue; |
|
36 | 1 | } |
|
37 | |||
38 | public function getMessagesPerPage(): int { |
||
39 | 1 | return $this->messagesPerPage; |
|
40 | } |
||
41 | |||
42 | public function setMessagesPerPage(int $messagesPerPage): void { |
||
43 | 1 | if($messagesPerPage < 0) { |
|
44 | 1 | $messagesPerPage = 0; |
|
45 | } |
||
46 | 1 | $this->messagesPerPage = $messagesPerPage; |
|
47 | 1 | } |
|
48 | |||
49 | public function getCharacterProfileLink(): string { |
||
50 | 1 | return $this->characterProfileLink; |
|
51 | } |
||
52 | |||
53 | public function setCharacterProfileLink(string $characterProfileLink): void { |
||
54 | 1 | $this->characterProfileLink = $characterProfileLink; |
|
55 | 1 | } |
|
56 | |||
57 | public function addMessageProcessor(IChatMessageProcessor $processor): void { |
||
58 | 1 | $this->messageProcessors[] = $processor; |
|
59 | 1 | } |
|
60 | |||
61 | /** |
||
62 | * Gets texts for the current chat |
||
63 | */ |
||
64 | public function getTexts(): ChatMessagesCollection { |
||
65 | 1 | return $this->database->getTexts($this->textColumn, $this->textValue, $this->messagesPerPage); |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * Gets characters in the current chat |
||
70 | */ |
||
71 | public function getCharacters(): ChatCharactersCollection { |
||
72 | 1 | return $this->database->getCharacters($this->characterColumn, $this->characterValue); |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Renders the chat |
||
77 | */ |
||
78 | public function render(): void { |
||
79 | 1 | $this->template->setFile($this->templateFile); |
|
80 | 1 | $this->template->characters = $this->getCharacters(); |
|
81 | 1 | $this->template->texts = $this->getTexts(); |
|
82 | 1 | $this->template->characterProfileLink = $this->characterProfileLink; |
|
83 | 1 | $this->template->render(); |
|
84 | 1 | } |
|
85 | |||
86 | protected function processMessage(string $message): ?string { |
||
87 | foreach($this->messageProcessors as $processor) { |
||
88 | $result = $processor->parse($message); |
||
89 | if(is_string($result)) { |
||
90 | return $result; |
||
91 | } |
||
92 | } |
||
93 | return null; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Submits new message |
||
98 | */ |
||
99 | public function newMessage(string $message): void { |
||
100 | $result = $this->processMessage($message); |
||
101 | if($result !== null) { |
||
102 | $this->presenter->flashMessage($result); |
||
103 | } else { |
||
104 | $this->database->addMessage($message, $this->textColumn, $this->textValue); |
||
105 | } |
||
106 | $this->presenter->redirect("this"); |
||
107 | } |
||
108 | } |
||
109 | ?> |