summaryrefslogtreecommitdiffstats
path: root/modules-available/sysconfig/addconfig.inc.php
blob: 27af31e889528a74dbe36c09dd2a5df89d34a02a (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
<?php

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

	/**
	 * Holds the instance for the currently executing step
	 * @var AddConfig_Base
	 */
	private static $instance = null;
	
	/**
	 * Config being edited (if any)
	 * @var ?ConfigTgz
	 */
	protected $edit = null;

	public static function setStep(string $step)
	{
		if (empty($step) || !class_exists($step) || get_parent_class($step) !== 'AddConfig_Base') {
			Message::addError('invalid-action', $step);
			Util::redirect('?do=SysConfig');
		}
		self::$instance = new $step();
		if (($editId = Request::any('edit', 0, 'int')) !== 0) {
			self::$instance->edit = ConfigTgz::get($editId);
			if (self::$instance->edit === null)
				ErrorHandler::traceError('Invalid config id for editing');
			Util::addRedirectParam('edit', self::$instance->edit->id());
		}
	}

	/**
	 * 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 === null) {
			ErrorHandler::traceError('No step instance yet');
		}
		self::$instance->preprocessInternal();
	}
	
	public static function render()
	{
		if (self::$instance === null)
			ErrorHandler::traceError('No step instance yet');
		if (self::$instance->edit !== null)
			Message::addInfo('replacing-config', self::$instance->edit->title());
		self::$instance->renderInternal();
	}
	
	public static function ajax()
	{
		if (self::$instance === null) {
			ErrorHandler::traceError('No step instance yet');
		}
		self::$instance->ajaxInternal();
	}

}

/**
 * Start dialog for adding config. Ask for title,
 * show selection of modules.
 */
class AddConfig_Start extends AddConfig_Base
{

	protected function renderInternal()
	{
		$mods = ConfigModule::getList();
		$res = Database::simpleQuery("SELECT moduleid, title, moduletype, filepath FROM configtgz_module"
			. " ORDER BY title ASC"); // Move to ConfigModule
		if ($this->edit === null) {
			$active = array();
		} else {
			$active = $this->edit->getModuleIds();
		}
		$id = 0;
		$modGroups = array();
		foreach ($mods as &$mod) {
			$mod['groupid'] = 'g' . ++$id;
			$modGroups[$mod['group']] =& $mod;
		}
		unset($mod);
		foreach ($res as $row) {
			if (!isset($mods[$row['moduletype']])) {
				$mods[$row['moduletype']] = array(
					'unique' => false,
					'group' => 'Undefined moduletype in addconfig.inc.php',
					'groupid' => 'g' . ++$id,
				);
				$modGroups[$mods[$row['moduletype']]['group']] =& $mods[$row['moduletype']];
			}
			unset($group);
			$group =& $modGroups[$mods[$row['moduletype']]['group']];
			if (!isset($group['modules'])) {
				$group['modules'] = array();
			}
			if (empty($row['filepath']) || !file_exists($row['filepath'])) $row['missing'] = true;
			$row['active'] = in_array($row['moduleid'], $active);
			$group['modules'][] = $row;
		}
		if ($this->edit !== null) {
			$title = $this->edit->title();
		} else {
			$title = Request::any('title', '', 'string');
		}
		$dummy = 0;
		$sort = [];
		foreach ($modGroups as &$mod) {
			$sort[] = $mod['sortOrder'];
			if (!empty($mod['modules']) && $mod['unique']) {
				array_unshift($mod['modules'], array(
					'moduleid' => 'x' . (++$dummy),
					'title' => Dictionary::translate('lang_noModuleFromThisGroup'),
				));
			}
		}
		array_multisort($sort, SORT_ASC | SORT_NUMERIC, $modGroups);
		unset($mod);
		Render::addDialog(Dictionary::translate("lang_configurationCompilation"), false, 'cfg-start', array(
			'step' => 'AddConfig_Finish',
			'groups' => array_values($modGroups),
			'title' => $title,
			'edit' => $this->edit === null ? null : $this->edit->id(),
		));
	}

}

/**
 * Success dialog if adding config worked.
 */
class AddConfig_Finish extends AddConfig_Base
{
	/**
	 * @var ?ConfigTgz
	 */
	private $config = null;
	
	protected function preprocessInternal()
	{
		$modules = Request::post('module');
		$title = Request::post('title', Request::REQUIRED, 'string');
		if (!is_array($modules)) {
			Message::addError('missing-file');
			Util::redirect('?do=SysConfig&action=addconfig');
		}
		if ($this->edit === null) {
			$this->config = ConfigTgz::insert($title, $modules);
		} else {
			$this->edit->update($title, $modules);
			$this->config = $this->edit;
		}
		if ($this->config->generate(true, 150) === false) {
			Message::addError('unsuccessful-action');
			Util::redirect('?do=SysConfig&action=addconfig');
		}
	}

	protected function renderInternal()
	{
		Render::addDialog(Dictionary::translate('lang_configurationCompilation'), false, 'cfg-finish', array(
			'configid' => $this->config->id()
		));
	}
	
}