From b98119be9411ad73b989fdf29284ad59b8340e4e Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 10 Dec 2018 15:43:07 +0100 Subject: [dozmod] Add editor for predefined run scripts Assigning scripts to certain locations only is currently not possible. Whether this is required is to be discussed, since predefined scripts selectable by bwLehrpool-Suite users would almost cover this use case. Refs #3145 --- modules-available/dozmod/pages/runscripts.inc.php | 133 ++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 modules-available/dozmod/pages/runscripts.inc.php (limited to 'modules-available/dozmod/pages/runscripts.inc.php') diff --git a/modules-available/dozmod/pages/runscripts.inc.php b/modules-available/dozmod/pages/runscripts.inc.php new file mode 100644 index 00000000..c6566c13 --- /dev/null +++ b/modules-available/dozmod/pages/runscripts.inc.php @@ -0,0 +1,133 @@ + $scriptname, + 'content' => Request::post('content', '', 'string'), + 'visibility' => Request::post('visibility', 1, 'int'), + 'extension' => preg_replace('/[^a-z0-9_\-~\!\$\=]/i', '', Request::post('extension', '', 'string')), + 'passcreds' => Request::post('passcreds', 0, 'int') !== 0, + 'isglobal' => Request::post('isglobal', 0, 'int') !== 0, + ]; + if ($id === 0) { + // New entry + $ret = Database::exec('INSERT INTO sat.presetrunscript + (scriptname, content, extension, visibility, passcreds, isglobal) VALUES + (:scriptname, :content, :extension, :visibility, :passcreds, :isglobal)', $data); + $id = Database::lastInsertId(); + } else { + // Edit entry + $data['id'] = $id; + Database::exec('UPDATE sat.presetrunscript SET + scriptname = :scriptname, content = :content, extension = :extension, visibility = :visibility, + passcreds = :passcreds, isglobal = :isglobal + WHERE runscriptid = :id', $data); + } + $oslist = Request::post('osid', false, 'array'); + if (is_array($oslist)) { + $oslist = array_filter($oslist, 'is_numeric'); + $query = Database::prepare('INSERT INTO sat.presetrunscript_x_operatingsystem + (runscriptid, osid) VALUES (:id, :osid)'); + foreach ($oslist as $osid) { + $query->execute(['id' => $id, 'osid' => $osid]); + } + $query->closeCursor(); + Database::exec('DELETE FROM sat.presetrunscript_x_operatingsystem + WHERE runscriptid = :id AND osid NOT IN (:oslist)', ['id' => $id, 'oslist' => $oslist]); + } + Message::addSuccess('runscript-saved'); + } + + public static function doRender() + { + $show = Request::get('show', 'list', 'string'); + if ($show === 'list') { + $res = Database::simpleQuery('SELECT runscriptid, scriptname, extension, visibility, passcreds, isglobal + FROM sat.presetrunscript + ORDER BY scriptname ASC'); + $rows = []; + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + if ($row['visibility'] == 0) { + $row['visibility'] = 'eye-close'; + } elseif ($row['visibility'] == 1) { + $row['visibility'] = 'eye-open'; + } else { + $row['visibility'] = 'arrow-down'; + } + $rows[] = $row; + } + Render::addTemplate('runscripts-list', ['list' => $rows, 'hasEditPermission' => User::hasPermission('runscripts.save')]); + } elseif ($show === 'edit') { + // Edit + $id = Request::get('runscriptid', false, 'int'); + if ($id === false) { + Message::addError('main.parameter-missing', 'runscriptid'); + Util::redirect('?do=dozmod§ion=runscripts'); + } + if ($id === 0) { + $row = [ + 'runscriptid' => 0, + 'visibility_1_checked' => 'checked', + 'isglobal_1_checked' => 'checked', + ]; + } else { + $row = Database::queryFirst('SELECT runscriptid, scriptname, content, extension, visibility, passcreds, isglobal + FROM sat.presetrunscript + WHERE runscriptid = :runscriptid', ['runscriptid' => $id]); + $row['visibility_' . $row['visibility'] . '_selected'] = 'selected'; + $row['passcreds_checked'] = $row['passcreds'] ? 'checked' : ''; + $row['isglobal_' . $row['isglobal'] . '_checked'] = 'checked'; + if ($row === false) { + Message::addError('runscript-invalid-id', $id); + Util::redirect('?do=dozmod§ion=runscripts'); + } + } + // Get OS + $row['oslist'] = []; + $res = Database::simpleQuery('SELECT o.osid, o.displayname, pxo.osid AS isvalid FROM sat.operatingsystem o + LEFT JOIN sat.presetrunscript_x_operatingsystem pxo ON (o.osid = pxo.osid AND pxo.runscriptid = :runscriptid) + ORDER BY o.displayname ASC', ['runscriptid' => $id]); + while ($osrow = $res->fetch(PDO::FETCH_ASSOC)) { + $row['oslist'][] = [ + 'osid' => $osrow['osid'], + 'displayname' => $osrow['displayname'], + 'checked' => $osrow['isvalid'] ? 'checked' : '', + ]; + } + // Output + Render::addTemplate('runscripts-edit', $row); + } + } + + public static function doAjax() + { + + } + +} -- cgit v1.2.3-55-g7522