diff options
Diffstat (limited to 'modules-available/runmode')
-rw-r--r-- | modules-available/runmode/inc/runmode.inc.php | 23 | ||||
-rw-r--r-- | modules-available/runmode/install.inc.php | 14 | ||||
-rw-r--r-- | modules-available/runmode/page.inc.php | 79 | ||||
-rw-r--r-- | modules-available/runmode/templates/machine-selector.html | 1 | ||||
-rw-r--r-- | modules-available/runmode/templates/module-machine-list.html | 44 |
5 files changed, 145 insertions, 16 deletions
diff --git a/modules-available/runmode/inc/runmode.inc.php b/modules-available/runmode/inc/runmode.inc.php index 916866f1..0f4994f4 100644 --- a/modules-available/runmode/inc/runmode.inc.php +++ b/modules-available/runmode/inc/runmode.inc.php @@ -9,7 +9,7 @@ class RunMode * Get runmode config for a specific module * * @param string $module name of module - * @return RunModeModuleConfig|false + * @return \RunModeModuleConfig|false config, false if moudles doesn't support run modes */ public static function getModuleConfig($module) { @@ -23,7 +23,15 @@ class RunMode return (self::$moduleConfigs[$module] = new RunModeModuleConfig($file)); } - public static function setRunMode($machineuuid, $moduleId, $modeId, $modeData) + /** + * @param string $machineuuid + * @param string $moduleId + * @param string|null $modeId an ID specific to the module to further specify the run mode, NULL to delete the run mode entry + * @param string|null $modeData optional, additional data for the run mode + * @param bool|null $isClient whether to count the machine as a client (in statistics etc.) NULL for looking at module's general runmode config + * @return bool whether it was set + */ + public static function setRunMode($machineuuid, $moduleId, $modeId, $modeData, $isClient) { // - Check if module provides runmode config at all $config = self::getModuleConfig($moduleId); @@ -37,13 +45,18 @@ class RunMode if (is_null($modeId)) { Database::exec('DELETE FROM runmode WHERE machineuuid = :machineuuid', compact('machineuuid')); } else { - Database::exec('INSERT INTO runmode (machineuuid, module, modeid, modedata)' - . ' VALUES (:uuid, :module, :modeid, :modedata)' - . ' ON DUPLICATE KEY UPDATE module = VALUES(module), modeid = VALUES(modeid), modedata = VALUES(modedata)', array( + if ($isClient === null) { + $isClient = $config->isClient; + } + Database::exec('INSERT INTO runmode (machineuuid, module, modeid, modedata, isclient)' + . ' VALUES (:uuid, :module, :modeid, :modedata, :isclient)' + . ' ON DUPLICATE KEY' + . ' UPDATE module = VALUES(module), modeid = VALUES(modeid), modedata = VALUES(modedata), isclient = VALUES(isclient)', array( 'uuid' => $machineuuid, 'module' => $moduleId, 'modeid' => $modeId, 'modedata' => $modeData, + 'isclient' => ($isClient ? 1 : 0), )); } return true; diff --git a/modules-available/runmode/install.inc.php b/modules-available/runmode/install.inc.php index 962a0cc9..e2b1ed0f 100644 --- a/modules-available/runmode/install.inc.php +++ b/modules-available/runmode/install.inc.php @@ -2,14 +2,15 @@ $res = array(); -$res[] = tableCreate('runmode', ' +$res[] = tableCreate('runmode', " `machineuuid` char(36) CHARACTER SET ascii NOT NULL, `module` varchar(30) CHARACTER SET ascii NOT NULL, `modeid` varchar(60) CHARACTER SET ascii NOT NULL, `modedata` blob DEFAULT NULL, + `isclient` bool DEFAULT '1', PRIMARY KEY (`machineuuid`), KEY `module` (`module`,`modeid`) -'); +"); if (!tableExists('machine')) { // Cannot add constraint yet @@ -27,6 +28,15 @@ if (!tableExists('machine')) { } } +if (!tableHasColumn('runmode', 'isclient')) { + $ret = Database::exec("ALTER TABLE runmode ADD COLUMN isclient bool DEFAULT '1'"); + if ($ret === false) { + finalResponse(UPDATE_FAILED, 'Could not add lastchange field'); + } elseif ($ret > 0) { + $ret[] = UPDATE_DONE; + } +} + // Create response for browser if (in_array(UPDATE_DONE, $res)) { diff --git a/modules-available/runmode/page.inc.php b/modules-available/runmode/page.inc.php index f3e7d024..05f32f81 100644 --- a/modules-available/runmode/page.inc.php +++ b/modules-available/runmode/page.inc.php @@ -27,14 +27,29 @@ class Page_RunMode extends Page $module = Request::post('module', false, 'string'); $modeId = Request::post('modeid', false, 'string'); // TODO Validate + $active = 0; foreach ($machines as $machine) { - Database::exec("INSERT IGNORE INTO runmode (machineuuid, module, modeid) - VALUES (:machine, :module, :modeId)", compact('machine', 'module', 'modeId')); + $ret = RunMode::setRunMode($machine, $module, $modeId, null, null); + if ($ret) { + $active++; + } else { + Message::addError('invalid-module-or-machine', $module, $machine); + } } - Database::exec('DELETE FROM runmode + $deleted = Database::exec('DELETE FROM runmode WHERE module = :module AND modeid = :modeId AND machineuuid NOT IN (:machines)', compact('module', 'modeId', 'machines')); + Message::addError('enabled-removed-save', $active, $deleted); + $redirect = Request::post('redirect', false, 'string'); + if ($redirect !== false) { + Util::redirect($redirect); + } Util::redirect('?do=runmode&module=' . $module . '&modeid=' . $modeId); + } elseif ($action === 'delete-machine') { + $machineuuid = Request::post('machineuuid', false, 'string'); + if ($machineuuid === false) { + + } } } @@ -45,8 +60,7 @@ class Page_RunMode extends Page $this->renderModule($moduleId); return; } - // TODO - Message::addInfo('OMGhai2U'); + $this->renderClientList(false); } private function renderModule($moduleId) @@ -56,14 +70,60 @@ class Page_RunMode extends Page Message::addError('main.no-such-module', $moduleId); Util::redirect('?do=runmode'); } + $module->activate(); + $config = RunMode::getModuleConfig($moduleId); + if ($config === false) { + Message::addError('module-hasnt-runmode', $moduleId); + Util::redirect('?do=runmode'); + } + // Given modeId? $modeId = Request::get('modeid', false, 'string'); if ($modeId !== false) { + // Show edit page for specific module-mode combo $this->renderModuleMode($module, $modeId); return; } - // TODO - Message::addError('main.parameter-missing', 'modeid'); - Util::redirect('?do=runmode'); + // Show list of machines with assigned mode for this module + $this->renderClientList($moduleId); + Render::setTitle(Page::getModule()->getDisplayName() . ' – ' . $module->getDisplayName()); + } + + private function renderClientList($onlyModule) + { + if ($onlyModule === false) { + $where = ''; + } else { + $where = ' AND r.module = :moduleId '; + } + $res = Database::simpleQuery("SELECT m.machineuuid, m.hostname, m.clientip, r.module, r.modeid, r.isclient" + . " FROM runmode r" + . " INNER JOIN machine m ON (m.machineuuid = r.machineuuid $where )" + . " ORDER BY m.hostname ASC, m.clientip ASC", array('moduleId' => $onlyModule)); + $modules = array(); + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + if (!isset($modules[$row['module']])) { + if (!Module::isAvailable($row['module'])) + continue; + $modules[$row['module']] = array('config' => RunMode::getModuleConfig($row['module']), 'list' => array()); + } + if (empty($row['hostname'])) { + $row['hostname'] = $row['clientip']; + } + if ($modules[$row['module']]['config']->getModeName !== false) { + $row['mode_name'] = call_user_func($modules[$row['module']]['config']->getModeName, $row['modeid']); + } + $modules[$row['module']]['list'][] = $row; + } + foreach ($modules as $moduleId => $rows) { + $module = Module::get($moduleId); + if ($module === false) + continue; + Render::addTemplate('module-machine-list', array( + 'list' => $rows['list'], + 'modulename' => $module->getDisplayName(), + 'module' => $moduleId, + )); + } } /** @@ -83,7 +143,8 @@ class Page_RunMode extends Page 'modeid' => $modeId, 'moduleName' => $module->getDisplayName(), 'modeName' => $modeName, - 'machines' => json_encode(RunMode::getForMode($module, $modeId, true)) + 'machines' => json_encode(RunMode::getForMode($module, $modeId, true)), + 'redirect' => Request::get('redirect', '', 'string'), ]); } diff --git a/modules-available/runmode/templates/machine-selector.html b/modules-available/runmode/templates/machine-selector.html index 95b255b5..d3ff7378 100644 --- a/modules-available/runmode/templates/machine-selector.html +++ b/modules-available/runmode/templates/machine-selector.html @@ -14,6 +14,7 @@ <input type="hidden" name="action" value="save-mode"> <input type="hidden" name="module" value="{{module}}" id="i-module"> <input type="hidden" name="modeid" value="{{modeid}}" id="i-modeid"> + <input type="hidden" name="redirect" value="{{redirect}}"> <select id="machine-sel" name="machines[]" multiple> </select> <div class="buttonbar"> diff --git a/modules-available/runmode/templates/module-machine-list.html b/modules-available/runmode/templates/module-machine-list.html new file mode 100644 index 00000000..a749a4a7 --- /dev/null +++ b/modules-available/runmode/templates/module-machine-list.html @@ -0,0 +1,44 @@ +<h2> + {{lang_specialMachinesForMode}}: + <a href="?do={{module}}">{{modulename}}</a> +</h2> + +<form method="post" action="?do=runmode" onclick="return confirm('{{lang_confirmDelete}}')"> + <input type="hidden" name="token" value="{{token}}"> + <input type="hidden" name="action" value="delete-machine"> + <table class="table"> + <tr> + <th>{{lang_machine}}</th> + <th>{{lang_mode}}</th> + <th class="slx-smallcol">{{lang_isclient}}</th> + <th class="slx-smallcol">{{lang_delete}}</th> + </tr> + {{#list}} + <tr> + <td> + <div class="slx-bold" title="{{clientip}}"><a + href="?do=statistics&uuid={{machineuuid}}">{{hostname}}</a></div> + <div class="small">{{machineuuid}}</div> + </td> + <td> + {{#mode_name}} + <div>{{mode_name}}</div> + <div class="small">{{modeid}}</div> + {{/mode_name}} + {{^mode_name}} + <div>{{modeid}}</div> + {{/mode_name}} + </td> + <td class="text-center"> + {{#isclient}} + <span class="glyphicon glyphicon-check text-success"></span> + {{/isclient}} + </td> + <td class="text-center"> + <button class="btn btn-danger btn-sm" name="machineuuid" value="{{machineuuid}}"><span + class="glyphicon glyphicon-remove"></span></button> + </td> + </tr> + {{/list}} + </table> +</form>
\ No newline at end of file |