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