1: <?php
2:
3: namespace Budabot\Core;
4:
5: 6: 7:
8: class SubcommandManager {
9:
10:
11: public $db;
12:
13:
14: public $chatBot;
15:
16:
17: public $util;
18:
19:
20: public $logger;
21:
22: public $subcommands = array();
23:
24: 25: 26: 27:
28: public function register($module, $channel, $filename, $command, $admin, $parent_command, $description = 'none', $help = '', $defaultStatus = null) {
29: $command = strtolower($command);
30: $module = strtoupper($module);
31:
32: if (!$this->chatBot->processCommandArgs($channel, $admin)) {
33: $this->logger->log('ERROR', "Invalid args for $module:subcommand($command)");
34: return;
35: }
36:
37: list($name, $method) = explode(".", $filename);
38: if (!Registry::instanceExists($name)) {
39: $this->logger->log('ERROR', "Error registering method $filename for subcommand $command. Could not find instance '$name'.");
40: return;
41: }
42:
43: if ($defaultStatus === null) {
44: if ($this->chatBot->vars['default_module_status'] == 1) {
45: $status = 1;
46: } else {
47: $status = 0;
48: }
49: } else {
50: $status = $defaultStatus;
51: }
52:
53: for ($i = 0; $i < count($channel); $i++) {
54: $this->logger->log('DEBUG', "Adding Subcommand to list:($command) File:($filename) Admin:($admin) Channel:({$channel[$i]})");
55:
56: if ($this->chatBot->existing_subcmds[$channel[$i]][$command] == true) {
57: $sql = "UPDATE cmdcfg_<myname> SET `module` = ?, `verify` = ?, `file` = ?, `description` = ?, `dependson` = ?, `help` = ? WHERE `cmd` = ? AND `type` = ?";
58: $this->db->exec($sql, $module, '1', $filename, $description, $parent_command, $help, $command, $channel[$i]);
59: } else {
60: $sql = "INSERT INTO cmdcfg_<myname> (`module`, `type`, `file`, `cmd`, `admin`, `description`, `verify`, `cmdevent`, `dependson`, `status`, `help`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
61: $this->db->exec($sql, $module, $channel[$i], $filename, $command, $admin[$i], $description, '1', 'subcmd', $parent_command, $status, $help);
62: }
63: }
64: }
65:
66: 67: 68: 69:
70: public function loadSubcommands() {
71: $this->logger->log('DEBUG', "Loading enabled subcommands");
72:
73: $this->subcommands = array();
74:
75: $data = $this->db->query("SELECT * FROM cmdcfg_<myname> WHERE `status` = '1' AND `cmdevent` = 'subcmd'");
76: forEach ($data as $row) {
77: $this->subcommands[$row->dependson] []= $row;
78: }
79: }
80: }
81: