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: 21:
22: class NewsController {
23:
24: public $db;
25:
26:
27: public $chatBot;
28:
29:
30: public $settingManager;
31:
32:
33: public $text;
34:
35:
36: public $util;
37:
38: public $moduleName;
39:
40: 41: 42:
43: public function setup() {
44: $this->db->loadSQLFile($this->moduleName, 'news');
45:
46: $this->settingManager->add($this->moduleName, "num_news_shown", "Maximum number of news items shown", "edit", "number", "10", "5;10;15;20");
47: }
48:
49: public function getNews() {
50: $sql = "SELECT * FROM `news` WHERE deleted = 0 ORDER BY `sticky` DESC, `time` DESC LIMIT ?";
51: $data = $this->db->query($sql, intval($this->settingManager->get('num_news_shown')));
52: $msg = '';
53: if (count($data) != 0) {
54: $blob = '';
55: $sticky = "";
56: forEach ($data as $row) {
57: if ($sticky != '') {
58: if ($sticky != $row->sticky) {
59: $blob .= "____________________________\n\n";
60: } else {
61: $blob .= "\n";
62: }
63: }
64:
65: $blob .= "<highlight>{$row->news}<end>\n";
66: $blob .= "By {$row->name} " . $this->util->date($row->time) . " ";
67: $blob .= $this->text->makeChatcmd("Remove", "/tell <myname> news rem $row->id") . " ";
68: if ($row->sticky == 1) {
69: $blob .= $this->text->makeChatcmd("Unsticky", "/tell <myname> news unsticky $row->id")."\n";
70: } else if ($row->sticky == 0) {
71: $blob .= $this->text->makeChatcmd("Sticky", "/tell <myname> news sticky $row->id")."\n";
72: }
73: $sticky = $row->sticky;
74: }
75: $msg = $this->text->makeBlob("News [Last updated at " . $this->util->date($data[0]->time) . "]", $blob);
76: }
77: return $msg;
78: }
79:
80: 81: 82: 83:
84: public function logonEvent($eventObj) {
85: $sender = $eventObj->sender;
86:
87: if ($this->chatBot->isReady() && isset($this->chatBot->guildmembers[$sender])) {
88: if ($this->hasRecentNews()) {
89: $this->chatBot->sendTell($this->getNews(), $sender);
90: }
91: }
92: }
93:
94: 95: 96: 97:
98: public function privateChannelJoinEvent($eventObj) {
99: $sender = $eventObj->sender;
100:
101: if ($this->hasRecentNews()) {
102: $this->chatBot->sendTell($this->getNews(), $sender);
103: }
104: }
105:
106: public function hasRecentNews() {
107: $thirtyDays = time() - (86400 * 30);
108: $row = $this->db->queryRow("SELECT * FROM `news` WHERE deleted = 0 AND time > ? LIMIT 1", $thirtyDays);
109: return $row !== null;
110: }
111:
112: 113: 114: 115: 116: 117:
118: public function newsCommand($message, $channel, $sender, $sendto) {
119: $msg = $this->getNews();
120: if ($msg == '') {
121: $msg = "No News recorded yet.";
122: }
123:
124: $sendto->reply($msg);
125: }
126:
127: 128: 129: 130: 131: 132:
133: public function newsAddCommand($message, $channel, $sender, $sendto, $arr) {
134: $news = $arr[1];
135: $this->db->exec("INSERT INTO `news` (`time`, `name`, `news`, `sticky`, `deleted`) VALUES (?, ?, ?, 0, 0)", time(), $sender, $news);
136: $msg = "News has been added successfully.";
137:
138: $sendto->reply($msg);
139: }
140:
141: 142: 143: 144: 145: 146:
147: public function newsRemCommand($message, $channel, $sender, $sendto, $arr) {
148: $id = $arr[1];
149:
150: $row = $this->getNewsItem($id);
151: if ($row === null) {
152: $msg = "No news entry found with the ID <highlight>{$id}<end>.";
153: } else {
154: $this->db->exec("UPDATE `news` SET deleted = 1 WHERE `id` = ?", $id);
155: $msg = "News entry <highlight>{$id}<end> was deleted successfully.";
156: }
157:
158: $sendto->reply($msg);
159: }
160:
161: 162: 163: 164: 165: 166:
167: public function stickyCommand($message, $channel, $sender, $sendto, $arr) {
168: $id = $arr[1];
169:
170: $row = $this->getNewsItem($id);
171:
172: if ($row->sticky == 1) {
173: $msg = "News ID $id is already stickied.";
174: } else {
175: $this->db->exec("UPDATE `news` SET `sticky` = 1 WHERE `id` = ?", $id);
176: $msg = "News ID $id successfully stickied.";
177: }
178: $sendto->reply($msg);
179: }
180:
181: 182: 183: 184: 185: 186:
187: public function unstickyCommand($message, $channel, $sender, $sendto, $arr) {
188: $id = $arr[1];
189:
190: $row = $this->getNewsItem($id);
191:
192: if ($row->sticky == 0) {
193: $msg = "News ID $id is not stickied.";
194: } else if ($row->sticky == 1) {
195: $this->db->exec("UPDATE `news` SET `sticky` = 0 WHERE `id` = ?", $id);
196: $msg = "News ID $id successfully unstickied.";
197: }
198: $sendto->reply($msg);
199: }
200:
201: public function getNewsItem($id) {
202: return $this->db->queryRow("SELECT * FROM `news` WHERE `deleted` = 0 AND `id` = ?", $id);
203: }
204: }
205: