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