1: <?php
2:
3: namespace Budabot\User\Modules;
4:
5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
20: class QuoteController {
21:
22: 23: 24: 25:
26: public $moduleName;
27:
28:
29: public $db;
30:
31:
32: public $settingManager;
33:
34:
35: public $accessManager;
36:
37:
38: public $text;
39:
40:
41: public $util;
42:
43: 44: 45: 46:
47: public function setup() {
48: $this->db->loadSQLFile($this->moduleName, "quote");
49: }
50:
51: 52: 53: 54:
55: public function quoteAddCommand($message, $channel, $sender, $sendto, $args) {
56: $quoteMSG = trim($args[1]);
57: $row = $this->db->queryRow("SELECT * FROM `quote` WHERE `msg` LIKE ?", $quoteMSG);
58: if ($row !== null) {
59: $msg = "This quote has already been added as quote <highlight>$row->id<end>.";
60: } else {
61: if (strlen($quoteMSG) > 1000) {
62: $msg = "This quote is too big.";
63: } else {
64: $poster = $sender;
65:
66:
67: $id = $this->getMaxId() + 1;
68:
69: $this->db->exec("INSERT INTO `quote` (`id`, `poster`, `dt`, `msg`) VALUES (?, ?, ?, ?)", $id, $poster, time(), $quoteMSG);
70: $msg = "Quote <highlight>$id<end> has been added.";
71: }
72: }
73: $sendto->reply($msg);
74: }
75:
76: 77: 78: 79:
80: public function quoteRemoveCommand($message, $channel, $sender, $sendto, $args) {
81: $id = $args[2];
82: $row = $this->db->queryRow("SELECT * FROM `quote` WHERE `id` = ?", $id);
83:
84: if ($row !== null) {
85: $poster = $row->poster;
86:
87:
88: if (($poster == $sender) || $this->accessManager->checkAccess($sender, 'moderator')) {
89: $this->db->exec("DELETE FROM `quote` WHERE `id` = ?", $id);
90: $msg = "This quote has been deleted.";
91: } else {
92: $msg = "Only a moderator or $poster can delete this quote.";
93: }
94:
95:
96:
97:
98: $maxId = $this->getMaxId();
99: $currentId = $id + 1;
100:
101: while ($currentId <= $maxId ) {
102: $this->db->exec("UPDATE `quote` SET `id` = `id` - 1 WHERE `id` = ?", $currentId);
103: $currentId++;
104: }
105: } else {
106: $msg = "Could not find this quote. Already deleted?";
107: }
108: $sendto->reply($msg);
109: }
110:
111: 112: 113: 114:
115: public function quoteSearchCommand($message, $channel, $sender, $sendto, $args) {
116: $search = $args[1];
117: $searchParam = '%' . $search . '%';
118:
119:
120: $list = "";
121: $data = $this->db->query("SELECT * FROM `quote` WHERE `poster` LIKE ?", $searchParam);
122: forEach ($data as $row) {
123: $list .= $this->text->makeChatcmd($row->id, "/tell <myname> quote $row->id") . ", ";
124: }
125: if ($list) {
126: $msg .= "<tab>Quotes posted by <highlight>$search<end>: ";
127: $msg .= substr($list, 0, strlen($list) - 2);
128: }
129:
130:
131: $list = "";
132: $data = $this->db->query("SELECT * FROM `quote` WHERE `msg` LIKE ?", $searchParam);
133: forEach ($data as $row) {
134: $list .= $this->text->makeChatcmd($row->id, "/tell <myname> quote $row->id") . ", ";
135: }
136: if ($list) {
137: if ($msg) {
138: $msg .="\n\n";
139: }
140: $msg .= "<tab>Quotes that contain '<highlight>$search<end>': ";
141: $msg .= substr($list, 0, strlen($list) - 2);
142: }
143:
144: if ($msg) {
145: $msg = $this->text->makeBlob("Results for: '$search'", $msg);
146: } else {
147: $msg = "Could not find any matches for this search.";
148: }
149: $sendto->reply($msg);
150: }
151:
152: 153: 154: 155:
156: public function quoteShowCommand($message, $channel, $sender, $sendto, $args) {
157: $id = $args[1];
158:
159: $result = $this->getQuoteInfo($id);
160:
161: if ($result == null) {
162: $msg = "No quote found with ID <highlight>$id<end>.";
163: } else {
164: $msg = $result;
165: }
166: $sendto->reply($msg);
167: }
168:
169: 170: 171: 172:
173: public function quoteShowRandomCommand($message, $channel, $sender, $sendto, $args) {
174:
175: $result = $this->getQuoteInfo(null);
176:
177: if ($result == null) {
178: $msg = "There are no quotes to show.";
179: } else {
180: $msg = $result;
181: }
182: $sendto->reply($msg);
183: }
184:
185: public function getMaxId() {
186: $row = $this->db->queryRow("SELECT COALESCE(MAX(id), 0) AS max_id FROM `quote`");
187: return $row->max_id;
188: }
189:
190: public function getQuoteInfo($id = null) {
191: $count = $this->getMaxId();
192:
193: if ($count == 0) {
194: return null;
195: }
196:
197: if ($id == null) {
198: $id = rand(1, $count);
199: }
200:
201: $row = $this->db->queryRow("SELECT * FROM `quote` WHERE `id` = ?", $id);
202: if ($row === null) {
203: return null;
204: }
205:
206: $poster = $row->poster;
207: $quoteMSG = $row->msg;
208:
209: $msg = "ID: <highlight>$id<end> of $count\n";
210: $msg .= "Poster: <highlight>$poster<end>\n";
211: $msg .= "Date: <highlight>" . $this->util->date($row->dt) . "<end>\n";
212: $msg .= "Quote: <highlight>$quoteMSG<end>\n\n";
213:
214: $msg .= "<header2>Quotes posted by <highlight>$poster<end>\n";
215: $data = $this->db->query("SELECT * FROM `quote` WHERE `poster` = ?", $poster);
216: $list = "";
217: forEach ($data as $row) {
218: $list .= $this->text->makeChatcmd($row->id, "/tell <myname> quote $row->id") . ", ";
219: }
220: $msg .= substr($list, 0, strlen($list) - 2);
221:
222: return $this->text->makeBlob("Quote", $msg).': "'.$quoteMSG.'"';
223: }
224: }
225: