Completed
Pull Request — master (#6575)
by Maxence
37:23 queued 23:28
created
lib/private/Settings/Manager.php 3 patches
Doc Comments   +6 added lines patch added patch discarded remove patch
@@ -273,6 +273,9 @@  discard block
 block discarded – undo
273 273
 		return $this->mapper->has($table, 'section', $sectionId);
274 274
 	}
275 275
 
276
+	/**
277
+	 * @param string $type
278
+	 */
276 279
 	private function setupSettingsEntry($settingsClassName, $type) {
277 280
 		if (!class_exists($settingsClassName)) {
278 281
 			$this->log->debug('Could not find ' . $type . ' section class ' . $settingsClassName);
@@ -302,6 +305,9 @@  discard block
 block discarded – undo
302 305
 		}
303 306
 	}
304 307
 
308
+	/**
309
+	 * @param string $type
310
+	 */
305 311
 	private function getSectionTableForType($type) {
306 312
 		if($type === 'admin') {
307 313
 			return Mapper::TABLE_ADMIN_SECTIONS;
Please login to merge, or discard this patch.
Indentation   +484 added lines, -484 removed lines patch added patch discarded remove patch
@@ -42,488 +42,488 @@
 block discarded – undo
42 42
 use OCP\Settings\ISection;
43 43
 
44 44
 class Manager implements IManager {
45
-	/** @var ILogger */
46
-	private $log;
47
-	/** @var IDBConnection */
48
-	private $dbc;
49
-	/** @var Mapper */
50
-	private $mapper;
51
-	/** @var IL10N */
52
-	private $l;
53
-	/** @var IConfig */
54
-	private $config;
55
-	/** @var EncryptionManager */
56
-	private $encryptionManager;
57
-	/** @var IUserManager */
58
-	private $userManager;
59
-	/** @var ILockingProvider */
60
-	private $lockingProvider;
61
-	/** @var IRequest */
62
-	private $request;
63
-	/** @var IURLGenerator */
64
-	private $url;
65
-	/** @var AccountManager */
66
-	private $accountManager;
67
-	/** @var IGroupManager */
68
-	private $groupManager;
69
-	/** @var IFactory */
70
-	private $l10nFactory;
71
-	/** @var \OC_Defaults */
72
-	private $defaults;
73
-	/** @var IAppManager */
74
-	private $appManager;
75
-
76
-	/**
77
-	 * @param ILogger $log
78
-	 * @param IDBConnection $dbc
79
-	 * @param IL10N $l
80
-	 * @param IConfig $config
81
-	 * @param EncryptionManager $encryptionManager
82
-	 * @param IUserManager $userManager
83
-	 * @param ILockingProvider $lockingProvider
84
-	 * @param IRequest $request
85
-	 * @param Mapper $mapper
86
-	 * @param IURLGenerator $url
87
-	 * @param AccountManager $accountManager
88
-	 * @param IGroupManager $groupManager
89
-	 * @param IFactory $l10nFactory
90
-	 * @param \OC_Defaults $defaults
91
-	 */
92
-	public function __construct(
93
-		ILogger $log,
94
-		IDBConnection $dbc,
95
-		IL10N $l,
96
-		IConfig $config,
97
-		EncryptionManager $encryptionManager,
98
-		IUserManager $userManager,
99
-		ILockingProvider $lockingProvider,
100
-		IRequest $request,
101
-		Mapper $mapper,
102
-		IURLGenerator $url,
103
-		AccountManager $accountManager,
104
-		IGroupManager $groupManager,
105
-		IFactory $l10nFactory,
106
-		\OC_Defaults $defaults,
107
-		IAppManager $appManager
108
-	) {
109
-		$this->log = $log;
110
-		$this->dbc = $dbc;
111
-		$this->mapper = $mapper;
112
-		$this->l = $l;
113
-		$this->config = $config;
114
-		$this->encryptionManager = $encryptionManager;
115
-		$this->userManager = $userManager;
116
-		$this->lockingProvider = $lockingProvider;
117
-		$this->request = $request;
118
-		$this->url = $url;
119
-		$this->accountManager = $accountManager;
120
-		$this->groupManager = $groupManager;
121
-		$this->l10nFactory = $l10nFactory;
122
-		$this->defaults = $defaults;
123
-		$this->appManager = $appManager;
124
-	}
125
-
126
-	/**
127
-	 * @inheritdoc
128
-	 */
129
-	public function setupSettings(array $settings) {
130
-		if (isset($settings[IManager::KEY_ADMIN_SECTION])) {
131
-			$this->setupSectionEntry($settings[IManager::KEY_ADMIN_SECTION], 'admin');
132
-		}
133
-		if (isset($settings[IManager::KEY_ADMIN_SETTINGS])) {
134
-			$this->setupSettingsEntry($settings[IManager::KEY_ADMIN_SETTINGS], 'admin');
135
-		}
136
-
137
-		if (isset($settings[IManager::KEY_PERSONAL_SECTION])) {
138
-			$this->setupSectionEntry($settings[IManager::KEY_PERSONAL_SECTION], 'personal');
139
-		}
140
-		if (isset($settings[IManager::KEY_PERSONAL_SETTINGS])) {
141
-			$this->setupSettingsEntry($settings[IManager::KEY_PERSONAL_SETTINGS], 'personal');
142
-		}
143
-	}
144
-
145
-	/**
146
-	 * attempts to remove an apps section and/or settings entry. A listener is
147
-	 * added centrally making sure that this method is called ones an app was
148
-	 * disabled.
149
-	 *
150
-	 * @param string $appId
151
-	 * @since 9.1.0
152
-	 */
153
-	public function onAppDisabled($appId) {
154
-		$appInfo = \OC_App::getAppInfo($appId); // hello static legacy
155
-
156
-		if (isset($appInfo['settings'][IManager::KEY_ADMIN_SECTION])) {
157
-			$this->mapper->remove(Mapper::TABLE_ADMIN_SECTIONS, trim($appInfo['settings'][IManager::KEY_ADMIN_SECTION], '\\'));
158
-		}
159
-		if (isset($appInfo['settings'][IManager::KEY_ADMIN_SETTINGS])) {
160
-			$this->mapper->remove(Mapper::TABLE_ADMIN_SETTINGS, trim($appInfo['settings'][IManager::KEY_ADMIN_SETTINGS], '\\'));
161
-		}
162
-
163
-		if (isset($appInfo['settings'][IManager::KEY_PERSONAL_SECTION])) {
164
-			$this->mapper->remove(Mapper::TABLE_PERSONAL_SECTIONS, trim($appInfo['settings'][IManager::KEY_PERSONAL_SECTION], '\\'));
165
-		}
166
-		if (isset($appInfo['settings'][IManager::KEY_PERSONAL_SETTINGS])) {
167
-			$this->mapper->remove(Mapper::TABLE_PERSONAL_SETTINGS, trim($appInfo['settings'][IManager::KEY_PERSONAL_SETTINGS], '\\'));
168
-		}
169
-	}
170
-
171
-	public function checkForOrphanedClassNames() {
172
-		$tables = [Mapper::TABLE_ADMIN_SECTIONS, Mapper::TABLE_ADMIN_SETTINGS, Mapper::TABLE_PERSONAL_SECTIONS, Mapper::TABLE_PERSONAL_SETTINGS];
173
-		foreach ($tables as $table) {
174
-			$classes = $this->mapper->getClasses($table);
175
-			foreach ($classes as $className) {
176
-				try {
177
-					\OC::$server->query($className);
178
-				} catch (QueryException $e) {
179
-					$this->mapper->remove($table, $className);
180
-				}
181
-			}
182
-		}
183
-	}
184
-
185
-	/**
186
-	 * @param string $sectionClassName
187
-	 * @param string $type either 'admin' or 'personal'
188
-	 */
189
-	private function setupSectionEntry($sectionClassName, $type) {
190
-		if (!class_exists($sectionClassName)) {
191
-			$this->log->debug('Could not find ' . ucfirst($type) . ' section class ' . $sectionClassName);
192
-			return;
193
-		}
194
-		try {
195
-			$section = $this->query($sectionClassName);
196
-		} catch (QueryException $e) {
197
-			// cancel
198
-			return;
199
-		}
200
-
201
-		if (!$section instanceof ISection) {
202
-			$this->log->error(
203
-				ucfirst($type) .' section instance must implement \OCP\ISection. Invalid class: {class}',
204
-				['class' => $sectionClassName]
205
-			);
206
-			return;
207
-		}
208
-		$table = $this->getSectionTableForType($type);
209
-		if(!$this->hasSection($section->getID(), $table)) {
210
-			$this->addSection($section, $table);
211
-		} else {
212
-			$this->updateSection($section, $table);
213
-		}
214
-	}
215
-
216
-	private function addSection(ISection $section, $table) {
217
-		$this->mapper->add($table, [
218
-			'id' => $section->getID(),
219
-			'class' => get_class($section),
220
-			'priority' => $section->getPriority(),
221
-		]);
222
-	}
223
-
224
-	private function addSettings(ISettings $settings, $table) {
225
-		$this->mapper->add($table, [
226
-			'class' => get_class($settings),
227
-			'section' => $settings->getSection(),
228
-			'priority' => $settings->getPriority(),
229
-		]);
230
-	}
231
-
232
-	private function updateSettings(ISettings $settings, $table) {
233
-		$this->mapper->update(
234
-			$table,
235
-			'section',
236
-			$settings->getSection(),
237
-			[
238
-				'class' => get_class($settings),
239
-				'priority' => $settings->getPriority(),
240
-			]
241
-		);
242
-	}
243
-
244
-	private function updateSection(ISection $section, $table) {
245
-		$this->mapper->update(
246
-			$table,
247
-			'id',
248
-			$section->getID(),
249
-			[
250
-				'class' => get_class($section),
251
-				'priority' => $section->getPriority(),
252
-			]
253
-		);
254
-	}
255
-
256
-	/**
257
-	 * @param string $sectionId
258
-	 * @param string $table
259
-	 *
260
-	 * @return bool
261
-	 */
262
-	private function hasSection($sectionId, $table) {
263
-		return $this->mapper->has($table, 'id', $sectionId);
264
-	}
265
-
266
-	/**
267
-	 * @param string $sectionId
268
-	 * @param $table
269
-	 *
270
-	 * @return bool
271
-	 */
272
-	private function hasSettings($sectionId, $table) {
273
-		return $this->mapper->has($table, 'section', $sectionId);
274
-	}
275
-
276
-	private function setupSettingsEntry($settingsClassName, $type) {
277
-		if (!class_exists($settingsClassName)) {
278
-			$this->log->debug('Could not find ' . $type . ' section class ' . $settingsClassName);
279
-			return;
280
-		}
281
-
282
-		try {
283
-			/** @var ISettings $settings */
284
-			$settings = $this->query($settingsClassName);
285
-		} catch (QueryException $e) {
286
-			// cancel
287
-			return;
288
-		}
289
-
290
-		if (!$settings instanceof ISettings) {
291
-			$this->log->error(
292
-				ucfirst($type) . ' section instance must implement \OCP\Settings\ISettings. Invalid class: {class}',
293
-				['class' => $settingsClassName]
294
-			);
295
-			return;
296
-		}
297
-		$table = $this->getSettingsTableForType($type);
298
-		if (!$this->hasSettings($settings->getSection(), $table)) {
299
-			$this->addSettings($settings, $table);
300
-		} else {
301
-			$this->updateSettings($settings, $table);
302
-		}
303
-	}
304
-
305
-	private function getSectionTableForType($type) {
306
-		if($type === 'admin') {
307
-			return Mapper::TABLE_ADMIN_SECTIONS;
308
-		} else if($type === 'personal') {
309
-			return Mapper::TABLE_PERSONAL_SECTIONS;
310
-		}
311
-		throw new \InvalidArgumentException('"admin" or "personal" expected');
312
-	}
313
-
314
-	private function getSettingsTableForType($type) {
315
-		if($type === 'admin') {
316
-			return Mapper::TABLE_ADMIN_SETTINGS;
317
-		} else if($type === 'personal') {
318
-			return Mapper::TABLE_PERSONAL_SETTINGS;
319
-		}
320
-		throw new \InvalidArgumentException('"admin" or "personal" expected');
321
-	}
322
-
323
-	private function query($className) {
324
-		try {
325
-			return \OC::$server->query($className);
326
-		} catch (QueryException $e) {
327
-			$this->log->logException($e);
328
-			throw $e;
329
-		}
330
-	}
331
-
332
-	/**
333
-	 * @inheritdoc
334
-	 */
335
-	public function getAdminSections() {
336
-		// built-in sections
337
-		$sections = [
338
-			0 => [new Section('server', $this->l->t('Basic settings'), 0, $this->url->imagePath('settings', 'admin.svg'))],
339
-			5 => [new Section('sharing', $this->l->t('Sharing'), 0, $this->url->imagePath('core', 'actions/share.svg'))],
340
-			10 => [new Section('security', $this->l->t('Security'), 0, $this->url->imagePath('core', 'actions/password.svg'))],
341
-			45 => [new Section('encryption', $this->l->t('Encryption'), 0, $this->url->imagePath('core', 'actions/password.svg'))],
342
-			98 => [new Section('additional', $this->l->t('Additional settings'), 0, $this->url->imagePath('core', 'actions/settings-dark.svg'))],
343
-			99 => [new Section('tips-tricks', $this->l->t('Tips & tricks'), 0, $this->url->imagePath('settings', 'help.svg'))],
344
-		];
345
-
346
-		$rows = $this->mapper->getAdminSectionsFromDB();
347
-
348
-		foreach ($rows as $row) {
349
-			if (!isset($sections[$row['priority']])) {
350
-				$sections[$row['priority']] = [];
351
-			}
352
-			try {
353
-				$sections[$row['priority']][] = $this->query($row['class']);
354
-			} catch (QueryException $e) {
355
-				// skip
356
-			}
357
-		}
358
-
359
-		ksort($sections);
360
-
361
-		return $sections;
362
-	}
363
-
364
-	/**
365
-	 * @param string $section
366
-	 * @return ISection[]
367
-	 */
368
-	private function getBuiltInAdminSettings($section) {
369
-		$forms = [];
370
-		try {
371
-			if ($section === 'server') {
372
-				/** @var ISettings $form */
373
-				$form = new Admin\Server($this->dbc, $this->request, $this->config, $this->lockingProvider, $this->l);
374
-				$forms[$form->getPriority()] = [$form];
375
-				$form = new Admin\ServerDevNotice();
376
-				$forms[$form->getPriority()] = [$form];
377
-			}
378
-			if ($section === 'encryption') {
379
-				/** @var ISettings $form */
380
-				$form = new Admin\Encryption($this->encryptionManager, $this->userManager);
381
-				$forms[$form->getPriority()] = [$form];
382
-			}
383
-			if ($section === 'sharing') {
384
-				/** @var ISettings $form */
385
-				$form = new Admin\Sharing($this->config);
386
-				$forms[$form->getPriority()] = [$form];
387
-			}
388
-			if ($section === 'additional') {
389
-				/** @var ISettings $form */
390
-				$form = new Admin\Additional($this->config);
391
-				$forms[$form->getPriority()] = [$form];
392
-			}
393
-			if ($section === 'tips-tricks') {
394
-				/** @var ISettings $form */
395
-				$form = new Admin\TipsTricks($this->config);
396
-				$forms[$form->getPriority()] = [$form];
397
-			}
398
-		} catch (QueryException $e) {
399
-			// skip
400
-		}
401
-		return $forms;
402
-	}
403
-
404
-	/**
405
-	 * @param string $section
406
-	 * @return ISection[]
407
-	 */
408
-	private function getBuiltInPersonalSettings($section) {
409
-		$forms = [];
410
-		try {
411
-			if ($section === 'personal-info') {
412
-				/** @var ISettings $form */
413
-				$form = new Personal\PersonalInfo(
414
-					$this->config,
415
-					$this->userManager,
416
-					$this->groupManager,
417
-					$this->accountManager,
418
-					$this->appManager,
419
-					$this->l10nFactory,
420
-					$this->l
421
-				);
422
-				$forms[$form->getPriority()] = [$form];
423
-			}
424
-			if($section === 'security') {
425
-				/** @var ISettings $form */
426
-				$form = new Personal\Security();
427
-				$forms[$form->getPriority()] = [$form];
428
-			}
429
-			if ($section === 'additional') {
430
-				/** @var ISettings $form */
431
-				$form = new Personal\Additional($this->config);
432
-				$forms[$form->getPriority()] = [$form];
433
-			}
434
-		} catch (QueryException $e) {
435
-			// skip
436
-		}
437
-		return $forms;
438
-	}
439
-
440
-	/**
441
-	 * @inheritdoc
442
-	 */
443
-	public function getAdminSettings($section) {
444
-		$settings = $this->getBuiltInAdminSettings($section);
445
-		$dbRows = $this->mapper->getAdminSettingsFromDB($section);
446
-
447
-		foreach ($dbRows as $row) {
448
-			if (!isset($settings[$row['priority']])) {
449
-				$settings[$row['priority']] = [];
450
-			}
451
-			try {
452
-				$settings[$row['priority']][] = $this->query($row['class']);
453
-			} catch (QueryException $e) {
454
-				// skip
455
-			}
456
-		}
457
-
458
-		ksort($settings);
459
-		return $settings;
460
-	}
461
-
462
-	/**
463
-	 * @inheritdoc
464
-	 */
465
-	public function getPersonalSections() {
466
-		$sections = [
467
-			0 => [new Section('personal-info', $this->l->t('Personal info'), 0, $this->url->imagePath('core', 'actions/info.svg'))],
468
-			5 => [new Section('security', $this->l->t('Security'), 0, $this->url->imagePath('settings', 'password.svg'))],
469
-			15 => [new Section('sync-clients', $this->l->t('Sync clients'), 0, $this->url->imagePath('settings', 'change.svg'))],
470
-		];
471
-
472
-		$legacyForms = \OC_App::getForms('personal');
473
-		if(count($legacyForms) > 0 && $this->hasLegacyPersonalSettingsToRender($legacyForms)) {
474
-			$sections[98] = [new Section('additional', $this->l->t('Additional settings'), 0, $this->url->imagePath('core', 'actions/settings-dark.svg'))];
475
-		}
476
-
477
-		$rows = $this->mapper->getPersonalSectionsFromDB();
478
-
479
-		foreach ($rows as $row) {
480
-			if (!isset($sections[$row['priority']])) {
481
-				$sections[$row['priority']] = [];
482
-			}
483
-			try {
484
-				$sections[$row['priority']][] = $this->query($row['class']);
485
-			} catch (QueryException $e) {
486
-				// skip
487
-			}
488
-		}
489
-
490
-		ksort($sections);
491
-
492
-		return $sections;
493
-	}
494
-
495
-	/**
496
-	 * @param $forms
497
-	 * @return bool
498
-	 */
499
-	private function hasLegacyPersonalSettingsToRender($forms) {
500
-		foreach ($forms as $form) {
501
-			if(trim($form) !== '') {
502
-				return true;
503
-			}
504
-		}
505
-		return false;
506
-	}
507
-
508
-	/**
509
-	 * @inheritdoc
510
-	 */
511
-	public function getPersonalSettings($section) {
512
-		$settings = $this->getBuiltInPersonalSettings($section);
513
-		$dbRows = $this->mapper->getPersonalSettingsFromDB($section);
514
-
515
-		foreach ($dbRows as $row) {
516
-			if (!isset($settings[$row['priority']])) {
517
-				$settings[$row['priority']] = [];
518
-			}
519
-			try {
520
-				$settings[$row['priority']][] = $this->query($row['class']);
521
-			} catch (QueryException $e) {
522
-				// skip
523
-			}
524
-		}
525
-
526
-		ksort($settings);
527
-		return $settings;
528
-	}
45
+    /** @var ILogger */
46
+    private $log;
47
+    /** @var IDBConnection */
48
+    private $dbc;
49
+    /** @var Mapper */
50
+    private $mapper;
51
+    /** @var IL10N */
52
+    private $l;
53
+    /** @var IConfig */
54
+    private $config;
55
+    /** @var EncryptionManager */
56
+    private $encryptionManager;
57
+    /** @var IUserManager */
58
+    private $userManager;
59
+    /** @var ILockingProvider */
60
+    private $lockingProvider;
61
+    /** @var IRequest */
62
+    private $request;
63
+    /** @var IURLGenerator */
64
+    private $url;
65
+    /** @var AccountManager */
66
+    private $accountManager;
67
+    /** @var IGroupManager */
68
+    private $groupManager;
69
+    /** @var IFactory */
70
+    private $l10nFactory;
71
+    /** @var \OC_Defaults */
72
+    private $defaults;
73
+    /** @var IAppManager */
74
+    private $appManager;
75
+
76
+    /**
77
+     * @param ILogger $log
78
+     * @param IDBConnection $dbc
79
+     * @param IL10N $l
80
+     * @param IConfig $config
81
+     * @param EncryptionManager $encryptionManager
82
+     * @param IUserManager $userManager
83
+     * @param ILockingProvider $lockingProvider
84
+     * @param IRequest $request
85
+     * @param Mapper $mapper
86
+     * @param IURLGenerator $url
87
+     * @param AccountManager $accountManager
88
+     * @param IGroupManager $groupManager
89
+     * @param IFactory $l10nFactory
90
+     * @param \OC_Defaults $defaults
91
+     */
92
+    public function __construct(
93
+        ILogger $log,
94
+        IDBConnection $dbc,
95
+        IL10N $l,
96
+        IConfig $config,
97
+        EncryptionManager $encryptionManager,
98
+        IUserManager $userManager,
99
+        ILockingProvider $lockingProvider,
100
+        IRequest $request,
101
+        Mapper $mapper,
102
+        IURLGenerator $url,
103
+        AccountManager $accountManager,
104
+        IGroupManager $groupManager,
105
+        IFactory $l10nFactory,
106
+        \OC_Defaults $defaults,
107
+        IAppManager $appManager
108
+    ) {
109
+        $this->log = $log;
110
+        $this->dbc = $dbc;
111
+        $this->mapper = $mapper;
112
+        $this->l = $l;
113
+        $this->config = $config;
114
+        $this->encryptionManager = $encryptionManager;
115
+        $this->userManager = $userManager;
116
+        $this->lockingProvider = $lockingProvider;
117
+        $this->request = $request;
118
+        $this->url = $url;
119
+        $this->accountManager = $accountManager;
120
+        $this->groupManager = $groupManager;
121
+        $this->l10nFactory = $l10nFactory;
122
+        $this->defaults = $defaults;
123
+        $this->appManager = $appManager;
124
+    }
125
+
126
+    /**
127
+     * @inheritdoc
128
+     */
129
+    public function setupSettings(array $settings) {
130
+        if (isset($settings[IManager::KEY_ADMIN_SECTION])) {
131
+            $this->setupSectionEntry($settings[IManager::KEY_ADMIN_SECTION], 'admin');
132
+        }
133
+        if (isset($settings[IManager::KEY_ADMIN_SETTINGS])) {
134
+            $this->setupSettingsEntry($settings[IManager::KEY_ADMIN_SETTINGS], 'admin');
135
+        }
136
+
137
+        if (isset($settings[IManager::KEY_PERSONAL_SECTION])) {
138
+            $this->setupSectionEntry($settings[IManager::KEY_PERSONAL_SECTION], 'personal');
139
+        }
140
+        if (isset($settings[IManager::KEY_PERSONAL_SETTINGS])) {
141
+            $this->setupSettingsEntry($settings[IManager::KEY_PERSONAL_SETTINGS], 'personal');
142
+        }
143
+    }
144
+
145
+    /**
146
+     * attempts to remove an apps section and/or settings entry. A listener is
147
+     * added centrally making sure that this method is called ones an app was
148
+     * disabled.
149
+     *
150
+     * @param string $appId
151
+     * @since 9.1.0
152
+     */
153
+    public function onAppDisabled($appId) {
154
+        $appInfo = \OC_App::getAppInfo($appId); // hello static legacy
155
+
156
+        if (isset($appInfo['settings'][IManager::KEY_ADMIN_SECTION])) {
157
+            $this->mapper->remove(Mapper::TABLE_ADMIN_SECTIONS, trim($appInfo['settings'][IManager::KEY_ADMIN_SECTION], '\\'));
158
+        }
159
+        if (isset($appInfo['settings'][IManager::KEY_ADMIN_SETTINGS])) {
160
+            $this->mapper->remove(Mapper::TABLE_ADMIN_SETTINGS, trim($appInfo['settings'][IManager::KEY_ADMIN_SETTINGS], '\\'));
161
+        }
162
+
163
+        if (isset($appInfo['settings'][IManager::KEY_PERSONAL_SECTION])) {
164
+            $this->mapper->remove(Mapper::TABLE_PERSONAL_SECTIONS, trim($appInfo['settings'][IManager::KEY_PERSONAL_SECTION], '\\'));
165
+        }
166
+        if (isset($appInfo['settings'][IManager::KEY_PERSONAL_SETTINGS])) {
167
+            $this->mapper->remove(Mapper::TABLE_PERSONAL_SETTINGS, trim($appInfo['settings'][IManager::KEY_PERSONAL_SETTINGS], '\\'));
168
+        }
169
+    }
170
+
171
+    public function checkForOrphanedClassNames() {
172
+        $tables = [Mapper::TABLE_ADMIN_SECTIONS, Mapper::TABLE_ADMIN_SETTINGS, Mapper::TABLE_PERSONAL_SECTIONS, Mapper::TABLE_PERSONAL_SETTINGS];
173
+        foreach ($tables as $table) {
174
+            $classes = $this->mapper->getClasses($table);
175
+            foreach ($classes as $className) {
176
+                try {
177
+                    \OC::$server->query($className);
178
+                } catch (QueryException $e) {
179
+                    $this->mapper->remove($table, $className);
180
+                }
181
+            }
182
+        }
183
+    }
184
+
185
+    /**
186
+     * @param string $sectionClassName
187
+     * @param string $type either 'admin' or 'personal'
188
+     */
189
+    private function setupSectionEntry($sectionClassName, $type) {
190
+        if (!class_exists($sectionClassName)) {
191
+            $this->log->debug('Could not find ' . ucfirst($type) . ' section class ' . $sectionClassName);
192
+            return;
193
+        }
194
+        try {
195
+            $section = $this->query($sectionClassName);
196
+        } catch (QueryException $e) {
197
+            // cancel
198
+            return;
199
+        }
200
+
201
+        if (!$section instanceof ISection) {
202
+            $this->log->error(
203
+                ucfirst($type) .' section instance must implement \OCP\ISection. Invalid class: {class}',
204
+                ['class' => $sectionClassName]
205
+            );
206
+            return;
207
+        }
208
+        $table = $this->getSectionTableForType($type);
209
+        if(!$this->hasSection($section->getID(), $table)) {
210
+            $this->addSection($section, $table);
211
+        } else {
212
+            $this->updateSection($section, $table);
213
+        }
214
+    }
215
+
216
+    private function addSection(ISection $section, $table) {
217
+        $this->mapper->add($table, [
218
+            'id' => $section->getID(),
219
+            'class' => get_class($section),
220
+            'priority' => $section->getPriority(),
221
+        ]);
222
+    }
223
+
224
+    private function addSettings(ISettings $settings, $table) {
225
+        $this->mapper->add($table, [
226
+            'class' => get_class($settings),
227
+            'section' => $settings->getSection(),
228
+            'priority' => $settings->getPriority(),
229
+        ]);
230
+    }
231
+
232
+    private function updateSettings(ISettings $settings, $table) {
233
+        $this->mapper->update(
234
+            $table,
235
+            'section',
236
+            $settings->getSection(),
237
+            [
238
+                'class' => get_class($settings),
239
+                'priority' => $settings->getPriority(),
240
+            ]
241
+        );
242
+    }
243
+
244
+    private function updateSection(ISection $section, $table) {
245
+        $this->mapper->update(
246
+            $table,
247
+            'id',
248
+            $section->getID(),
249
+            [
250
+                'class' => get_class($section),
251
+                'priority' => $section->getPriority(),
252
+            ]
253
+        );
254
+    }
255
+
256
+    /**
257
+     * @param string $sectionId
258
+     * @param string $table
259
+     *
260
+     * @return bool
261
+     */
262
+    private function hasSection($sectionId, $table) {
263
+        return $this->mapper->has($table, 'id', $sectionId);
264
+    }
265
+
266
+    /**
267
+     * @param string $sectionId
268
+     * @param $table
269
+     *
270
+     * @return bool
271
+     */
272
+    private function hasSettings($sectionId, $table) {
273
+        return $this->mapper->has($table, 'section', $sectionId);
274
+    }
275
+
276
+    private function setupSettingsEntry($settingsClassName, $type) {
277
+        if (!class_exists($settingsClassName)) {
278
+            $this->log->debug('Could not find ' . $type . ' section class ' . $settingsClassName);
279
+            return;
280
+        }
281
+
282
+        try {
283
+            /** @var ISettings $settings */
284
+            $settings = $this->query($settingsClassName);
285
+        } catch (QueryException $e) {
286
+            // cancel
287
+            return;
288
+        }
289
+
290
+        if (!$settings instanceof ISettings) {
291
+            $this->log->error(
292
+                ucfirst($type) . ' section instance must implement \OCP\Settings\ISettings. Invalid class: {class}',
293
+                ['class' => $settingsClassName]
294
+            );
295
+            return;
296
+        }
297
+        $table = $this->getSettingsTableForType($type);
298
+        if (!$this->hasSettings($settings->getSection(), $table)) {
299
+            $this->addSettings($settings, $table);
300
+        } else {
301
+            $this->updateSettings($settings, $table);
302
+        }
303
+    }
304
+
305
+    private function getSectionTableForType($type) {
306
+        if($type === 'admin') {
307
+            return Mapper::TABLE_ADMIN_SECTIONS;
308
+        } else if($type === 'personal') {
309
+            return Mapper::TABLE_PERSONAL_SECTIONS;
310
+        }
311
+        throw new \InvalidArgumentException('"admin" or "personal" expected');
312
+    }
313
+
314
+    private function getSettingsTableForType($type) {
315
+        if($type === 'admin') {
316
+            return Mapper::TABLE_ADMIN_SETTINGS;
317
+        } else if($type === 'personal') {
318
+            return Mapper::TABLE_PERSONAL_SETTINGS;
319
+        }
320
+        throw new \InvalidArgumentException('"admin" or "personal" expected');
321
+    }
322
+
323
+    private function query($className) {
324
+        try {
325
+            return \OC::$server->query($className);
326
+        } catch (QueryException $e) {
327
+            $this->log->logException($e);
328
+            throw $e;
329
+        }
330
+    }
331
+
332
+    /**
333
+     * @inheritdoc
334
+     */
335
+    public function getAdminSections() {
336
+        // built-in sections
337
+        $sections = [
338
+            0 => [new Section('server', $this->l->t('Basic settings'), 0, $this->url->imagePath('settings', 'admin.svg'))],
339
+            5 => [new Section('sharing', $this->l->t('Sharing'), 0, $this->url->imagePath('core', 'actions/share.svg'))],
340
+            10 => [new Section('security', $this->l->t('Security'), 0, $this->url->imagePath('core', 'actions/password.svg'))],
341
+            45 => [new Section('encryption', $this->l->t('Encryption'), 0, $this->url->imagePath('core', 'actions/password.svg'))],
342
+            98 => [new Section('additional', $this->l->t('Additional settings'), 0, $this->url->imagePath('core', 'actions/settings-dark.svg'))],
343
+            99 => [new Section('tips-tricks', $this->l->t('Tips & tricks'), 0, $this->url->imagePath('settings', 'help.svg'))],
344
+        ];
345
+
346
+        $rows = $this->mapper->getAdminSectionsFromDB();
347
+
348
+        foreach ($rows as $row) {
349
+            if (!isset($sections[$row['priority']])) {
350
+                $sections[$row['priority']] = [];
351
+            }
352
+            try {
353
+                $sections[$row['priority']][] = $this->query($row['class']);
354
+            } catch (QueryException $e) {
355
+                // skip
356
+            }
357
+        }
358
+
359
+        ksort($sections);
360
+
361
+        return $sections;
362
+    }
363
+
364
+    /**
365
+     * @param string $section
366
+     * @return ISection[]
367
+     */
368
+    private function getBuiltInAdminSettings($section) {
369
+        $forms = [];
370
+        try {
371
+            if ($section === 'server') {
372
+                /** @var ISettings $form */
373
+                $form = new Admin\Server($this->dbc, $this->request, $this->config, $this->lockingProvider, $this->l);
374
+                $forms[$form->getPriority()] = [$form];
375
+                $form = new Admin\ServerDevNotice();
376
+                $forms[$form->getPriority()] = [$form];
377
+            }
378
+            if ($section === 'encryption') {
379
+                /** @var ISettings $form */
380
+                $form = new Admin\Encryption($this->encryptionManager, $this->userManager);
381
+                $forms[$form->getPriority()] = [$form];
382
+            }
383
+            if ($section === 'sharing') {
384
+                /** @var ISettings $form */
385
+                $form = new Admin\Sharing($this->config);
386
+                $forms[$form->getPriority()] = [$form];
387
+            }
388
+            if ($section === 'additional') {
389
+                /** @var ISettings $form */
390
+                $form = new Admin\Additional($this->config);
391
+                $forms[$form->getPriority()] = [$form];
392
+            }
393
+            if ($section === 'tips-tricks') {
394
+                /** @var ISettings $form */
395
+                $form = new Admin\TipsTricks($this->config);
396
+                $forms[$form->getPriority()] = [$form];
397
+            }
398
+        } catch (QueryException $e) {
399
+            // skip
400
+        }
401
+        return $forms;
402
+    }
403
+
404
+    /**
405
+     * @param string $section
406
+     * @return ISection[]
407
+     */
408
+    private function getBuiltInPersonalSettings($section) {
409
+        $forms = [];
410
+        try {
411
+            if ($section === 'personal-info') {
412
+                /** @var ISettings $form */
413
+                $form = new Personal\PersonalInfo(
414
+                    $this->config,
415
+                    $this->userManager,
416
+                    $this->groupManager,
417
+                    $this->accountManager,
418
+                    $this->appManager,
419
+                    $this->l10nFactory,
420
+                    $this->l
421
+                );
422
+                $forms[$form->getPriority()] = [$form];
423
+            }
424
+            if($section === 'security') {
425
+                /** @var ISettings $form */
426
+                $form = new Personal\Security();
427
+                $forms[$form->getPriority()] = [$form];
428
+            }
429
+            if ($section === 'additional') {
430
+                /** @var ISettings $form */
431
+                $form = new Personal\Additional($this->config);
432
+                $forms[$form->getPriority()] = [$form];
433
+            }
434
+        } catch (QueryException $e) {
435
+            // skip
436
+        }
437
+        return $forms;
438
+    }
439
+
440
+    /**
441
+     * @inheritdoc
442
+     */
443
+    public function getAdminSettings($section) {
444
+        $settings = $this->getBuiltInAdminSettings($section);
445
+        $dbRows = $this->mapper->getAdminSettingsFromDB($section);
446
+
447
+        foreach ($dbRows as $row) {
448
+            if (!isset($settings[$row['priority']])) {
449
+                $settings[$row['priority']] = [];
450
+            }
451
+            try {
452
+                $settings[$row['priority']][] = $this->query($row['class']);
453
+            } catch (QueryException $e) {
454
+                // skip
455
+            }
456
+        }
457
+
458
+        ksort($settings);
459
+        return $settings;
460
+    }
461
+
462
+    /**
463
+     * @inheritdoc
464
+     */
465
+    public function getPersonalSections() {
466
+        $sections = [
467
+            0 => [new Section('personal-info', $this->l->t('Personal info'), 0, $this->url->imagePath('core', 'actions/info.svg'))],
468
+            5 => [new Section('security', $this->l->t('Security'), 0, $this->url->imagePath('settings', 'password.svg'))],
469
+            15 => [new Section('sync-clients', $this->l->t('Sync clients'), 0, $this->url->imagePath('settings', 'change.svg'))],
470
+        ];
471
+
472
+        $legacyForms = \OC_App::getForms('personal');
473
+        if(count($legacyForms) > 0 && $this->hasLegacyPersonalSettingsToRender($legacyForms)) {
474
+            $sections[98] = [new Section('additional', $this->l->t('Additional settings'), 0, $this->url->imagePath('core', 'actions/settings-dark.svg'))];
475
+        }
476
+
477
+        $rows = $this->mapper->getPersonalSectionsFromDB();
478
+
479
+        foreach ($rows as $row) {
480
+            if (!isset($sections[$row['priority']])) {
481
+                $sections[$row['priority']] = [];
482
+            }
483
+            try {
484
+                $sections[$row['priority']][] = $this->query($row['class']);
485
+            } catch (QueryException $e) {
486
+                // skip
487
+            }
488
+        }
489
+
490
+        ksort($sections);
491
+
492
+        return $sections;
493
+    }
494
+
495
+    /**
496
+     * @param $forms
497
+     * @return bool
498
+     */
499
+    private function hasLegacyPersonalSettingsToRender($forms) {
500
+        foreach ($forms as $form) {
501
+            if(trim($form) !== '') {
502
+                return true;
503
+            }
504
+        }
505
+        return false;
506
+    }
507
+
508
+    /**
509
+     * @inheritdoc
510
+     */
511
+    public function getPersonalSettings($section) {
512
+        $settings = $this->getBuiltInPersonalSettings($section);
513
+        $dbRows = $this->mapper->getPersonalSettingsFromDB($section);
514
+
515
+        foreach ($dbRows as $row) {
516
+            if (!isset($settings[$row['priority']])) {
517
+                $settings[$row['priority']] = [];
518
+            }
519
+            try {
520
+                $settings[$row['priority']][] = $this->query($row['class']);
521
+            } catch (QueryException $e) {
522
+                // skip
523
+            }
524
+        }
525
+
526
+        ksort($settings);
527
+        return $settings;
528
+    }
529 529
 }
Please login to merge, or discard this patch.
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -188,7 +188,7 @@  discard block
 block discarded – undo
188 188
 	 */
189 189
 	private function setupSectionEntry($sectionClassName, $type) {
190 190
 		if (!class_exists($sectionClassName)) {
191
-			$this->log->debug('Could not find ' . ucfirst($type) . ' section class ' . $sectionClassName);
191
+			$this->log->debug('Could not find '.ucfirst($type).' section class '.$sectionClassName);
192 192
 			return;
193 193
 		}
194 194
 		try {
@@ -200,13 +200,13 @@  discard block
 block discarded – undo
200 200
 
201 201
 		if (!$section instanceof ISection) {
202 202
 			$this->log->error(
203
-				ucfirst($type) .' section instance must implement \OCP\ISection. Invalid class: {class}',
203
+				ucfirst($type).' section instance must implement \OCP\ISection. Invalid class: {class}',
204 204
 				['class' => $sectionClassName]
205 205
 			);
206 206
 			return;
207 207
 		}
208 208
 		$table = $this->getSectionTableForType($type);
209
-		if(!$this->hasSection($section->getID(), $table)) {
209
+		if (!$this->hasSection($section->getID(), $table)) {
210 210
 			$this->addSection($section, $table);
211 211
 		} else {
212 212
 			$this->updateSection($section, $table);
@@ -275,7 +275,7 @@  discard block
 block discarded – undo
275 275
 
276 276
 	private function setupSettingsEntry($settingsClassName, $type) {
277 277
 		if (!class_exists($settingsClassName)) {
278
-			$this->log->debug('Could not find ' . $type . ' section class ' . $settingsClassName);
278
+			$this->log->debug('Could not find '.$type.' section class '.$settingsClassName);
279 279
 			return;
280 280
 		}
281 281
 
@@ -289,7 +289,7 @@  discard block
 block discarded – undo
289 289
 
290 290
 		if (!$settings instanceof ISettings) {
291 291
 			$this->log->error(
292
-				ucfirst($type) . ' section instance must implement \OCP\Settings\ISettings. Invalid class: {class}',
292
+				ucfirst($type).' section instance must implement \OCP\Settings\ISettings. Invalid class: {class}',
293 293
 				['class' => $settingsClassName]
294 294
 			);
295 295
 			return;
@@ -303,18 +303,18 @@  discard block
 block discarded – undo
303 303
 	}
304 304
 
305 305
 	private function getSectionTableForType($type) {
306
-		if($type === 'admin') {
306
+		if ($type === 'admin') {
307 307
 			return Mapper::TABLE_ADMIN_SECTIONS;
308
-		} else if($type === 'personal') {
308
+		} else if ($type === 'personal') {
309 309
 			return Mapper::TABLE_PERSONAL_SECTIONS;
310 310
 		}
311 311
 		throw new \InvalidArgumentException('"admin" or "personal" expected');
312 312
 	}
313 313
 
314 314
 	private function getSettingsTableForType($type) {
315
-		if($type === 'admin') {
315
+		if ($type === 'admin') {
316 316
 			return Mapper::TABLE_ADMIN_SETTINGS;
317
-		} else if($type === 'personal') {
317
+		} else if ($type === 'personal') {
318 318
 			return Mapper::TABLE_PERSONAL_SETTINGS;
319 319
 		}
320 320
 		throw new \InvalidArgumentException('"admin" or "personal" expected');
@@ -421,7 +421,7 @@  discard block
 block discarded – undo
421 421
 				);
422 422
 				$forms[$form->getPriority()] = [$form];
423 423
 			}
424
-			if($section === 'security') {
424
+			if ($section === 'security') {
425 425
 				/** @var ISettings $form */
426 426
 				$form = new Personal\Security();
427 427
 				$forms[$form->getPriority()] = [$form];
@@ -470,7 +470,7 @@  discard block
 block discarded – undo
470 470
 		];
471 471
 
472 472
 		$legacyForms = \OC_App::getForms('personal');
473
-		if(count($legacyForms) > 0 && $this->hasLegacyPersonalSettingsToRender($legacyForms)) {
473
+		if (count($legacyForms) > 0 && $this->hasLegacyPersonalSettingsToRender($legacyForms)) {
474 474
 			$sections[98] = [new Section('additional', $this->l->t('Additional settings'), 0, $this->url->imagePath('core', 'actions/settings-dark.svg'))];
475 475
 		}
476 476
 
@@ -498,7 +498,7 @@  discard block
 block discarded – undo
498 498
 	 */
499 499
 	private function hasLegacyPersonalSettingsToRender($forms) {
500 500
 		foreach ($forms as $form) {
501
-			if(trim($form) !== '') {
501
+			if (trim($form) !== '') {
502 502
 				return true;
503 503
 			}
504 504
 		}
Please login to merge, or discard this patch.
lib/private/Settings/Mapper.php 1 patch
Indentation   +186 added lines, -186 removed lines patch added patch discarded remove patch
@@ -26,191 +26,191 @@
 block discarded – undo
26 26
 use OCP\IDBConnection;
27 27
 
28 28
 class Mapper {
29
-	const TABLE_ADMIN_SETTINGS = 'admin_settings';
30
-	const TABLE_ADMIN_SECTIONS = 'admin_sections';
31
-	const TABLE_PERSONAL_SETTINGS = 'personal_settings';
32
-	const TABLE_PERSONAL_SECTIONS = 'personal_sections';
33
-
34
-	/** @var IDBConnection */
35
-	private $dbc;
36
-
37
-	/**
38
-	 * @param IDBConnection $dbc
39
-	 */
40
-	public function __construct(IDBConnection $dbc) {
41
-		$this->dbc = $dbc;
42
-	}
43
-
44
-	/**
45
-	 * Get the configured admin settings from the database for the provided section
46
-	 *
47
-	 * @param string $section
48
-	 * @return array[] [['class' => string, 'priority' => int], ...]
49
-	 */
50
-	public function getAdminSettingsFromDB($section) {
51
-		return $this->getSettingsFromDB(self::TABLE_ADMIN_SETTINGS, $section);
52
-	}
53
-
54
-	/**
55
-	 * Get the configured personal settings from the database for the provided section
56
-	 *
57
-	 * @param string $section
58
-	 * @return array[] [['class' => string, 'priority' => int], ...]
59
-	 */
60
-	public function getPersonalSettingsFromDB($section) {
61
-		return $this->getSettingsFromDB(self::TABLE_PERSONAL_SETTINGS, $section);
62
-	}
63
-
64
-	/**
65
-	 * Get the configured settings from the database for the provided table and section
66
-	 *
67
-	 * @param $table
68
-	 * @param $section
69
-	 * @return array
70
-	 */
71
-	private function getSettingsFromDB($table, $section) {
72
-		$query = $this->dbc->getQueryBuilder();
73
-		$query->select(['class', 'priority'])
74
-			->from($table)
75
-			->where($query->expr()->eq('section', $this->dbc->getQueryBuilder()->createParameter('section')))
76
-			->setParameter('section', $section);
77
-
78
-		$result = $query->execute();
79
-		return $result->fetchAll();
80
-	}
81
-
82
-	/**
83
-	 * Get the configured admin sections from the database
84
-	 *
85
-	 * @return array[] [['class' => string, 'priority' => int], ...]
86
-	 */
87
-	public function getAdminSectionsFromDB() {
88
-		return $this->getSectionsFromDB('admin');
89
-	}
90
-
91
-	/**
92
-	 * Get the configured admin sections from the database
93
-	 *
94
-	 * @return array[] [['class' => string, 'priority' => int], ...]
95
-	 */
96
-	public function getPersonalSectionsFromDB() {
97
-		return $this->getSectionsFromDB('personal');
98
-	}
99
-
100
-	/**
101
-	 * Get the configured sections from the database by table
102
-	 *
103
-	 * @param string $type either 'personal' or 'admin'
104
-	 * @return array[] [['class' => string, 'priority' => int], ...]
105
-	 */
106
-	public function getSectionsFromDB($type) {
107
-		if($type === 'admin') {
108
-			$sectionsTable = self::TABLE_ADMIN_SECTIONS;
109
-			$settingsTable = self::TABLE_ADMIN_SETTINGS;
110
-		} else if($type === 'personal') {
111
-			$sectionsTable = self::TABLE_PERSONAL_SECTIONS;
112
-			$settingsTable = self::TABLE_PERSONAL_SETTINGS;
113
-		} else {
114
-			throw new \InvalidArgumentException('"admin" or "personal" expected');
115
-		}
116
-		$query = $this->dbc->getQueryBuilder();
117
-		$query->selectDistinct('s.class')
118
-			->addSelect('s.priority')
119
-			->from($sectionsTable, 's')
120
-			->from($settingsTable, 'f')
121
-			->where($query->expr()->eq('s.id', 'f.section'));
122
-		$result = $query->execute();
123
-		return array_map(function ($row) {
124
-			$row['priority'] = (int)$row['priority'];
125
-			return $row;
126
-		}, $result->fetchAll());
127
-	}
128
-
129
-	/**
130
-	 * @param string $table one of the Mapper::TABLE_* constants
131
-	 * @param array $values
132
-	 */
133
-	public function add($table, array $values) {
134
-		$query = $this->dbc->getQueryBuilder();
135
-		$values = array_map(function ($value) use ($query) {
136
-			return $query->createNamedParameter($value);
137
-		}, $values);
138
-		$query->insert($table)->values($values);
139
-		$query->execute();
140
-	}
141
-
142
-	/**
143
-	 * returns the registered classes in the given table
144
-	 *
145
-	 * @param string $table one of the Mapper::TABLE_* constants
146
-	 * @return string[]
147
-	 */
148
-	public function getClasses($table) {
149
-		$q = $this->dbc->getQueryBuilder();
150
-		$resultStatement = $q->select('class')
151
-			->from($table)
152
-			->execute();
153
-		$data = $resultStatement->fetchAll();
154
-		$resultStatement->closeCursor();
155
-
156
-		return array_map(function ($row) {
157
-			return $row['class'];
158
-		}, $data);
159
-	}
160
-
161
-	/**
162
-	 * Check if a class is configured in the database
163
-	 *
164
-	 * @param string $table one of the Mapper::TABLE_* constants
165
-	 * @param $field
166
-	 * @param string $sectionId
167
-	 * @return bool
168
-	 */
169
-	public function has($table, $field, $sectionId) {
170
-		$query = $this->dbc->getQueryBuilder();
171
-		$query->select('id')
172
-			->from($table)
173
-			->where($query->expr()->eq($field, $query->createNamedParameter($sectionId)))
174
-			->setMaxResults(1);
175
-
176
-		$result = $query->execute();
177
-		$row = $result->fetch();
178
-		$result->closeCursor();
179
-
180
-		return (bool)$row;
181
-	}
182
-
183
-	/**
184
-	 * deletes an settings or admin entry from the given table
185
-	 *
186
-	 * @param string $table one of the Mapper::TABLE_* constants
187
-	 * @param string $className
188
-	 */
189
-	public function remove($table, $className) {
190
-		$query = $this->dbc->getQueryBuilder();
191
-		$query->delete($table)
192
-			->where($query->expr()->eq('class', $query->createNamedParameter($className)));
193
-
194
-		$query->execute();
195
-	}
196
-
197
-	/**
198
-	 * @param string $table one of the Mapper::TABLE_* constants
199
-	 * @param string $idCol
200
-	 * @param string $id
201
-	 * @param array $values
202
-	 * @suppress SqlInjectionChecker
203
-	 */
204
-	public function update($table, $idCol, $id, $values) {
205
-		$query = $this->dbc->getQueryBuilder();
206
-		$query->update($table);
207
-		foreach ($values as $key => $value) {
208
-			$query->set($key, $query->createNamedParameter($value));
209
-		}
210
-		$query
211
-			->where($query->expr()->eq($idCol, $query->createParameter($idCol)))
212
-			->setParameter($idCol, $id)
213
-			->execute();
214
-	}
29
+    const TABLE_ADMIN_SETTINGS = 'admin_settings';
30
+    const TABLE_ADMIN_SECTIONS = 'admin_sections';
31
+    const TABLE_PERSONAL_SETTINGS = 'personal_settings';
32
+    const TABLE_PERSONAL_SECTIONS = 'personal_sections';
33
+
34
+    /** @var IDBConnection */
35
+    private $dbc;
36
+
37
+    /**
38
+     * @param IDBConnection $dbc
39
+     */
40
+    public function __construct(IDBConnection $dbc) {
41
+        $this->dbc = $dbc;
42
+    }
43
+
44
+    /**
45
+     * Get the configured admin settings from the database for the provided section
46
+     *
47
+     * @param string $section
48
+     * @return array[] [['class' => string, 'priority' => int], ...]
49
+     */
50
+    public function getAdminSettingsFromDB($section) {
51
+        return $this->getSettingsFromDB(self::TABLE_ADMIN_SETTINGS, $section);
52
+    }
53
+
54
+    /**
55
+     * Get the configured personal settings from the database for the provided section
56
+     *
57
+     * @param string $section
58
+     * @return array[] [['class' => string, 'priority' => int], ...]
59
+     */
60
+    public function getPersonalSettingsFromDB($section) {
61
+        return $this->getSettingsFromDB(self::TABLE_PERSONAL_SETTINGS, $section);
62
+    }
63
+
64
+    /**
65
+     * Get the configured settings from the database for the provided table and section
66
+     *
67
+     * @param $table
68
+     * @param $section
69
+     * @return array
70
+     */
71
+    private function getSettingsFromDB($table, $section) {
72
+        $query = $this->dbc->getQueryBuilder();
73
+        $query->select(['class', 'priority'])
74
+            ->from($table)
75
+            ->where($query->expr()->eq('section', $this->dbc->getQueryBuilder()->createParameter('section')))
76
+            ->setParameter('section', $section);
77
+
78
+        $result = $query->execute();
79
+        return $result->fetchAll();
80
+    }
81
+
82
+    /**
83
+     * Get the configured admin sections from the database
84
+     *
85
+     * @return array[] [['class' => string, 'priority' => int], ...]
86
+     */
87
+    public function getAdminSectionsFromDB() {
88
+        return $this->getSectionsFromDB('admin');
89
+    }
90
+
91
+    /**
92
+     * Get the configured admin sections from the database
93
+     *
94
+     * @return array[] [['class' => string, 'priority' => int], ...]
95
+     */
96
+    public function getPersonalSectionsFromDB() {
97
+        return $this->getSectionsFromDB('personal');
98
+    }
99
+
100
+    /**
101
+     * Get the configured sections from the database by table
102
+     *
103
+     * @param string $type either 'personal' or 'admin'
104
+     * @return array[] [['class' => string, 'priority' => int], ...]
105
+     */
106
+    public function getSectionsFromDB($type) {
107
+        if($type === 'admin') {
108
+            $sectionsTable = self::TABLE_ADMIN_SECTIONS;
109
+            $settingsTable = self::TABLE_ADMIN_SETTINGS;
110
+        } else if($type === 'personal') {
111
+            $sectionsTable = self::TABLE_PERSONAL_SECTIONS;
112
+            $settingsTable = self::TABLE_PERSONAL_SETTINGS;
113
+        } else {
114
+            throw new \InvalidArgumentException('"admin" or "personal" expected');
115
+        }
116
+        $query = $this->dbc->getQueryBuilder();
117
+        $query->selectDistinct('s.class')
118
+            ->addSelect('s.priority')
119
+            ->from($sectionsTable, 's')
120
+            ->from($settingsTable, 'f')
121
+            ->where($query->expr()->eq('s.id', 'f.section'));
122
+        $result = $query->execute();
123
+        return array_map(function ($row) {
124
+            $row['priority'] = (int)$row['priority'];
125
+            return $row;
126
+        }, $result->fetchAll());
127
+    }
128
+
129
+    /**
130
+     * @param string $table one of the Mapper::TABLE_* constants
131
+     * @param array $values
132
+     */
133
+    public function add($table, array $values) {
134
+        $query = $this->dbc->getQueryBuilder();
135
+        $values = array_map(function ($value) use ($query) {
136
+            return $query->createNamedParameter($value);
137
+        }, $values);
138
+        $query->insert($table)->values($values);
139
+        $query->execute();
140
+    }
141
+
142
+    /**
143
+     * returns the registered classes in the given table
144
+     *
145
+     * @param string $table one of the Mapper::TABLE_* constants
146
+     * @return string[]
147
+     */
148
+    public function getClasses($table) {
149
+        $q = $this->dbc->getQueryBuilder();
150
+        $resultStatement = $q->select('class')
151
+            ->from($table)
152
+            ->execute();
153
+        $data = $resultStatement->fetchAll();
154
+        $resultStatement->closeCursor();
155
+
156
+        return array_map(function ($row) {
157
+            return $row['class'];
158
+        }, $data);
159
+    }
160
+
161
+    /**
162
+     * Check if a class is configured in the database
163
+     *
164
+     * @param string $table one of the Mapper::TABLE_* constants
165
+     * @param $field
166
+     * @param string $sectionId
167
+     * @return bool
168
+     */
169
+    public function has($table, $field, $sectionId) {
170
+        $query = $this->dbc->getQueryBuilder();
171
+        $query->select('id')
172
+            ->from($table)
173
+            ->where($query->expr()->eq($field, $query->createNamedParameter($sectionId)))
174
+            ->setMaxResults(1);
175
+
176
+        $result = $query->execute();
177
+        $row = $result->fetch();
178
+        $result->closeCursor();
179
+
180
+        return (bool)$row;
181
+    }
182
+
183
+    /**
184
+     * deletes an settings or admin entry from the given table
185
+     *
186
+     * @param string $table one of the Mapper::TABLE_* constants
187
+     * @param string $className
188
+     */
189
+    public function remove($table, $className) {
190
+        $query = $this->dbc->getQueryBuilder();
191
+        $query->delete($table)
192
+            ->where($query->expr()->eq('class', $query->createNamedParameter($className)));
193
+
194
+        $query->execute();
195
+    }
196
+
197
+    /**
198
+     * @param string $table one of the Mapper::TABLE_* constants
199
+     * @param string $idCol
200
+     * @param string $id
201
+     * @param array $values
202
+     * @suppress SqlInjectionChecker
203
+     */
204
+    public function update($table, $idCol, $id, $values) {
205
+        $query = $this->dbc->getQueryBuilder();
206
+        $query->update($table);
207
+        foreach ($values as $key => $value) {
208
+            $query->set($key, $query->createNamedParameter($value));
209
+        }
210
+        $query
211
+            ->where($query->expr()->eq($idCol, $query->createParameter($idCol)))
212
+            ->setParameter($idCol, $id)
213
+            ->execute();
214
+    }
215 215
 
216 216
 }
Please login to merge, or discard this patch.