From 9abacc2da789048a83953da5622a6e588bb432a4 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Thu, 10 Aug 2017 15:56:58 +0200 Subject: [runmode] Add isclient property per client, implement overview page --- modules-available/runmode/inc/runmode.inc.php | 23 +++++-- modules-available/runmode/install.inc.php | 14 +++- modules-available/runmode/page.inc.php | 79 +++++++++++++++++++--- .../runmode/templates/machine-selector.html | 1 + .../runmode/templates/module-machine-list.html | 44 ++++++++++++ 5 files changed, 145 insertions(+), 16 deletions(-) create mode 100644 modules-available/runmode/templates/module-machine-list.html (limited to 'modules-available/runmode') 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 @@ +
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 @@ +

+ {{lang_specialMachinesForMode}}: + {{modulename}} +

+ +
+ + + + + + + + + + {{#list}} + + + + + + + {{/list}} +
{{lang_machine}}{{lang_mode}}{{lang_isclient}}{{lang_delete}}
+ +
{{machineuuid}}
+
+ {{#mode_name}} +
{{mode_name}}
+
{{modeid}}
+ {{/mode_name}} + {{^mode_name}} +
{{modeid}}
+ {{/mode_name}} +
+ {{#isclient}} + + {{/isclient}} + + +
+
\ No newline at end of file -- cgit v1.2.3-55-g7522