diff options
Diffstat (limited to 'modules-available/sysconfig/addmodule.inc.php')
-rw-r--r-- | modules-available/sysconfig/addmodule.inc.php | 131 |
1 files changed, 95 insertions, 36 deletions
diff --git a/modules-available/sysconfig/addmodule.inc.php b/modules-available/sysconfig/addmodule.inc.php index 1f78de81..4564537e 100644 --- a/modules-available/sysconfig/addmodule.inc.php +++ b/modules-available/sysconfig/addmodule.inc.php @@ -9,33 +9,39 @@ abstract class AddModule_Base /** * Holds the instance for the currently executing step - * @var \AddModule_Base + * + * @var AddModule_Base */ private static $instance = false; /** * Instance of ConfigModule we're editing. False if not editing but creating. - * @var \ConfigModule + * + * @var ?ConfigModule */ - protected $edit = false; + protected $edit = null; /** * * @param string $step name of class representing the current step + * @param int $editId (optional) overwrite for the request parameter 'edit' */ - public static function setStep($step) + public static function setStep(string $step, int $editId = null): void { if (empty($step) || !class_exists($step) || get_parent_class($step) !== 'AddModule_Base') { Message::addError('invalid-action', $step); Util::redirect('?do=SysConfig'); } self::$instance = new $step(); - if (Request::any('edit')) { - self::$instance->edit = ConfigModule::get(Request::any('edit')); - if (self::$instance->edit === false) - Util::traceError('Invalid module id for editing'); - if (!preg_match('/^' . self::$instance->edit->moduleType() . '_/', $step)) - Util::traceError('Module to edit is of different type!'); + if ($editId === null) { + $editId = Request::any('edit', 0, 'int'); + } + if ($editId !== 0) { + self::$instance->edit = ConfigModule::get($editId); + if (self::$instance->edit === null) + ErrorHandler::traceError('Invalid module id for editing'); + if ($step !== 'AddModule_Assign' && !preg_match('/^' . self::$instance->edit->moduleType() . '_/', $step)) + ErrorHandler::traceError('Module to edit is of different type!'); Util::addRedirectParam('edit', self::$instance->edit->id()); } } @@ -61,7 +67,7 @@ abstract class AddModule_Base /** * Called before any HTML rendering happens, so you can - * pepare stuff, validate input, and optionally redirect + * prepare stuff, validate input, and optionally redirect * early if something is wrong, or you received post * data etc. */ @@ -89,7 +95,7 @@ abstract class AddModule_Base public static function preprocess() { if (self::$instance === false) { - Util::traceError('No step instance yet'); + ErrorHandler::traceError('No step instance yet'); } self::$instance->preprocessInternal(); } @@ -97,9 +103,9 @@ abstract class AddModule_Base public static function render() { if (self::$instance === false) { - Util::traceError('No step instance yet'); + ErrorHandler::traceError('No step instance yet'); } - if (self::$instance->edit !== false) { + if (get_class(self::$instance) !== 'AddModule_Assign' && self::$instance->edit !== null) { Message::addInfo('replacing-module', self::$instance->edit->title()); } self::$instance->renderInternal(); @@ -108,7 +114,7 @@ abstract class AddModule_Base public static function ajax() { if (self::$instance === false) { - Util::traceError('No step instance yet'); + ErrorHandler::traceError('No step instance yet'); } self::$instance->ajaxInternal(); } @@ -136,45 +142,98 @@ class AddModule_Start extends AddModule_Base } +/** + * End dialog for adding module. Here the user + * can assign the module to configs. + */ +class AddModule_Assign extends AddModule_Base +{ + + protected function preprocessInternal() + { + $assign = Request::any('assign', false, 'boolean'); + + if ($assign) { + $configIds = Request::any('configs', [], 'array'); + $moduleId = $this->edit->id(); + $moduleType = $this->edit->moduleType(); + + if (ConfigModule::getList()[$moduleType]['unique']) { + $moduleIds = []; + foreach (ConfigModule::getAll($moduleType) ?? [] as $module) { + $moduleIds[] = $module->id(); + } + + Database::exec("DELETE FROM configtgz_x_module WHERE configid IN (:configids) AND moduleid IN (:moduleids)", + array('configids' => $configIds, 'moduleids' => $moduleIds)); + } + + foreach ($configIds as $configId) { + Database::exec("INSERT INTO configtgz_x_module (configid, moduleid) VALUES (:configid, :moduleid)", array( + 'configid' => $configId, + 'moduleid' => $moduleId + )); + ConfigTgz::get($configId)->generate(); + } + + Util::redirect('?do=SysConfig'); + } + } + + protected function renderInternal() + { + $data = ['configs' => SysConfig::getAll()]; + if (count($data['configs']) === 0) + Util::redirect('?do=SysConfig'); + + $moduleType = $this->edit->moduleType(); + if (ConfigModule::getList()[$moduleType]['unique']) { + $modules = Database::queryAll('SELECT configtgz_module.moduleid as moduleid, configtgz_module.title as title, configtgz_x_module.configid as configid' + . ' FROM configtgz_module INNER JOIN configtgz_x_module ON configtgz_module.moduleid = configtgz_x_module.moduleid' + . ' WHERE configtgz_module.moduletype = :moduletype', + array('moduletype' => $moduleType)); + + $modulesByConfigId = []; + foreach ($modules as $module) { + $modulesByConfigId[$module['configid']] = $module; + } + + foreach ($data['configs'] as &$config) { + if (!isset($modulesByConfigId[$config['configid']])) continue; + $config['replaces'] = $modulesByConfigId[$config['configid']]['title']; + } + } + + $data['edit'] = $this->edit->id(); + Render::addDialog(Dictionary::translate('lang_moduleAssign'), false, 'assign', $data); + } + +} + /* * Helper functions to set/get a batch of vars from/to post variables or a module */ -/** - * - * @param \ConfigModule $module - * @param array $array - * @param array $keys - */ -function moduleToArray($module, &$array, $keys) +function moduleToArray(ConfigModule $module, array &$array, array $keys): void { foreach ($keys as $key) { $array[$key] = $module->getData($key); } } -/** - * - * @param \ConfigModule $module - * @param array $array - * @param array $keys - */ -function arrayToModule($module, $array, $keys) +function arrayToModule(ConfigModule $module, array $array, array $keys): void { foreach ($keys as $key) { $module->setData($key, $array[$key]); } } -/** - * - * @param array $array - * @param array $keys - */ -function postToArray(&$array, $keys, $ignoreMissing = false) + +function postToArray(array &$array, array $keys, $ignoreMissing = false): void { foreach ($keys as $key) { $val = Request::post($key, '--not-in-post'); - if ($ignoreMissing && $val === '--not-in-post') continue; + if ($ignoreMissing && $val === '--not-in-post') + continue; $array[$key] = $val; } } |