summaryrefslogtreecommitdiffstats
path: root/modules-available/sysconfig/addmodule.inc.php
blob: 91fee45d3962a792735f3d29ee09d90770f165b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php

/**
 * Addmodule subpage base - makes sure
 * we have the two required methods preprocess and render
 */
abstract class AddModule_Base
{

	/**
	 * Holds the instance for the currently executing step
	 * @var \AddModule_Base
	 */
	private static $instance = false;
	
	/**
	 * Instance of ConfigModule we're editing. False if not editing but creating.
	 * @var \ConfigModule
	 */
	protected $edit = false;

	/**
	 * 
	 * @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, $editId = false)
	{
		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 ($editId = $editId ? $editId : Request::any('edit')) {
			self::$instance->edit = ConfigModule::get($editId);
			if (self::$instance->edit === false)
				Util::traceError('Invalid module id for editing');
			if ($step !== 'AddModule_Assign' && !preg_match('/^' . self::$instance->edit->moduleType() . '_/', $step))
				Util::traceError('Module to edit is of different type!');
			Util::addRedirectParam('edit', self::$instance->edit->id());
		}
	}

	protected function tmError()
	{
		Message::addError('main.taskmanager-error');
		Util::redirect('?do=SysConfig');
	}

	protected function taskError($status)
	{
		if (isset($status['data']['error'])) {
			$error = $status['data']['error'];
		} elseif (isset($status['statusCode'])) {
			$error = $status['statusCode'];
		} else {
			$error = Dictionary::translate('lang_unknwonTaskManager');
		}
		Message::addError('main.task-error', $error);
		Util::redirect('?do=SysConfig');
	}

	/**
	 * Called before any HTML rendering happens, so you can
	 * prepare stuff, validate input, and optionally redirect
	 * early if something is wrong, or you received post
	 * data etc.
	 */
	protected function preprocessInternal()
	{
		// void
	}

	/**
	 * Do page rendering.
	 */
	protected function renderInternal()
	{
		// void
	}
	
	/**
	 * Handle ajax stuff
	 */
	protected function ajaxInternal()
	{
		// void
	}
	
	public static function preprocess()
	{
		if (self::$instance === false) {
			Util::traceError('No step instance yet');
		}
		self::$instance->preprocessInternal();
	}
	
	public static function render()
	{
		if (self::$instance === false) {
			Util::traceError('No step instance yet');
		}
		if (get_class(self::$instance) !== 'AddModule_Assign' && self::$instance->edit !== false) {
			Message::addInfo('replacing-module', self::$instance->edit->title());
		}
		self::$instance->renderInternal();
	}
	
	public static function ajax()
	{
		if (self::$instance === false) {
			Util::traceError('No step instance yet');
		}
		self::$instance->ajaxInternal();
	}

}

/**
 * Start dialog for adding module. Here the user
 * selects which kind of module they want to add.
 */
class AddModule_Start extends AddModule_Base
{

	protected function renderInternal()
	{
		$title = $order = array();
		$mods = ConfigModule::getList();
		foreach ($mods as $module) {
			$title[] = $module['title'];
			$order[] = $module['sortOrder'];
		}
		array_multisort($order, SORT_ASC, $title, SORT_ASC, $mods);
		Render::addDialog(Dictionary::translate('lang_moduleAdd'), false, 'start', array('modules' => array_values($mods)));
	}

}

/**
 * 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)
{
	foreach ($keys as $key) {
		$array[$key] = $module->getData($key);
	}
}

/**
 * 
 * @param \ConfigModule $module
 * @param array $array
 * @param array $keys
 */
function arrayToModule($module, $array, $keys)
{
	foreach ($keys as $key) {
		$module->setData($key, $array[$key]);
	}
}
/**
 * 
 * @param array $array
 * @param array $keys
 */
function postToArray(&$array, $keys, $ignoreMissing = false)
{
	foreach ($keys as $key) {
		$val = Request::post($key, '--not-in-post');
		if ($ignoreMissing && $val === '--not-in-post') continue;
		$array[$key] = $val;
	}
}