Completed
Pull Request — master (#5591)
by Joas
16:45
created
lib/private/Settings/Manager.php 2 patches
Indentation   +481 added lines, -481 removed lines patch added patch discarded remove patch
@@ -42,485 +42,485 @@
 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(get_class($section), $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
-			'class',
236
-			get_class($settings),
237
-			[
238
-				'section' => $settings->getSection(),
239
-				'priority' => $settings->getPriority(),
240
-			]
241
-		);
242
-	}
243
-
244
-	private function updateSection(ISection $section, $table) {
245
-		$this->mapper->update(
246
-			$table,
247
-			'class',
248
-			get_class($section),
249
-			[
250
-				'id' => $section->getID(),
251
-				'priority' => $section->getPriority(),
252
-			]
253
-		);
254
-	}
255
-
256
-	/**
257
-	 * @param string $className
258
-	 * @param string $table
259
-	 * @return bool
260
-	 */
261
-	private function hasSection($className, $table) {
262
-		return $this->mapper->has($table, $className);
263
-	}
264
-
265
-	/**
266
-	 * @param string $className
267
-	 * @return bool
268
-	 */
269
-	private function hasSettings($className, $table) {
270
-		return $this->mapper->has($table, $className);
271
-	}
272
-
273
-	private function setupSettingsEntry($settingsClassName, $type) {
274
-		if (!class_exists($settingsClassName)) {
275
-			$this->log->debug('Could not find ' . $type . ' section class ' . $settingsClassName);
276
-			return;
277
-		}
278
-
279
-		try {
280
-			/** @var ISettings $settings */
281
-			$settings = $this->query($settingsClassName);
282
-		} catch (QueryException $e) {
283
-			// cancel
284
-			return;
285
-		}
286
-
287
-		if (!$settings instanceof ISettings) {
288
-			$this->log->error(
289
-				ucfirst($type) . ' section instance must implement \OCP\Settings\ISettings. Invalid class: {class}',
290
-				['class' => $settingsClassName]
291
-			);
292
-			return;
293
-		}
294
-		$table = $this->getSettingsTableForType($type);
295
-		if (!$this->hasSettings(get_class($settings), $table)) {
296
-			$this->addSettings($settings, $table);
297
-		} else {
298
-			$this->updateSettings($settings, $table);
299
-		}
300
-	}
301
-
302
-	private function getSectionTableForType($type) {
303
-		if($type === 'admin') {
304
-			return Mapper::TABLE_ADMIN_SECTIONS;
305
-		} else if($type === 'personal') {
306
-			return Mapper::TABLE_PERSONAL_SECTIONS;
307
-		}
308
-		throw new \InvalidArgumentException('"admin" or "personal" expected');
309
-	}
310
-
311
-	private function getSettingsTableForType($type) {
312
-		if($type === 'admin') {
313
-			return Mapper::TABLE_ADMIN_SETTINGS;
314
-		} else if($type === 'personal') {
315
-			return Mapper::TABLE_PERSONAL_SETTINGS;
316
-		}
317
-		throw new \InvalidArgumentException('"admin" or "personal" expected');
318
-	}
319
-
320
-	private function query($className) {
321
-		try {
322
-			return \OC::$server->query($className);
323
-		} catch (QueryException $e) {
324
-			$this->log->logException($e);
325
-			throw $e;
326
-		}
327
-	}
328
-
329
-	/**
330
-	 * @inheritdoc
331
-	 */
332
-	public function getAdminSections() {
333
-		// built-in sections
334
-		$sections = [
335
-			0 => [new Section('server', $this->l->t('Basic settings'), 0, $this->url->imagePath('settings', 'admin.svg'))],
336
-			5 => [new Section('sharing', $this->l->t('Sharing'), 0, $this->url->imagePath('core', 'actions/share.svg'))],
337
-			10 => [new Section('security', $this->l->t('Security'), 0, $this->url->imagePath('core', 'actions/password.svg'))],
338
-			45 => [new Section('encryption', $this->l->t('Encryption'), 0, $this->url->imagePath('core', 'actions/password.svg'))],
339
-			98 => [new Section('additional', $this->l->t('Additional settings'), 0, $this->url->imagePath('core', 'actions/settings-dark.svg'))],
340
-			99 => [new Section('tips-tricks', $this->l->t('Tips & tricks'), 0, $this->url->imagePath('settings', 'help.svg'))],
341
-		];
342
-
343
-		$rows = $this->mapper->getAdminSectionsFromDB();
344
-
345
-		foreach ($rows as $row) {
346
-			if (!isset($sections[$row['priority']])) {
347
-				$sections[$row['priority']] = [];
348
-			}
349
-			try {
350
-				$sections[$row['priority']][] = $this->query($row['class']);
351
-			} catch (QueryException $e) {
352
-				// skip
353
-			}
354
-		}
355
-
356
-		ksort($sections);
357
-
358
-		return $sections;
359
-	}
360
-
361
-	/**
362
-	 * @param string $section
363
-	 * @return ISection[]
364
-	 */
365
-	private function getBuiltInAdminSettings($section) {
366
-		$forms = [];
367
-		try {
368
-			if ($section === 'server') {
369
-				/** @var ISettings $form */
370
-				$form = new Admin\Server($this->dbc, $this->request, $this->config, $this->lockingProvider, $this->l);
371
-				$forms[$form->getPriority()] = [$form];
372
-				$form = new Admin\ServerDevNotice();
373
-				$forms[$form->getPriority()] = [$form];
374
-			}
375
-			if ($section === 'encryption') {
376
-				/** @var ISettings $form */
377
-				$form = new Admin\Encryption($this->encryptionManager, $this->userManager);
378
-				$forms[$form->getPriority()] = [$form];
379
-			}
380
-			if ($section === 'sharing') {
381
-				/** @var ISettings $form */
382
-				$form = new Admin\Sharing($this->config);
383
-				$forms[$form->getPriority()] = [$form];
384
-			}
385
-			if ($section === 'additional') {
386
-				/** @var ISettings $form */
387
-				$form = new Admin\Additional($this->config);
388
-				$forms[$form->getPriority()] = [$form];
389
-			}
390
-			if ($section === 'tips-tricks') {
391
-				/** @var ISettings $form */
392
-				$form = new Admin\TipsTricks($this->config);
393
-				$forms[$form->getPriority()] = [$form];
394
-			}
395
-		} catch (QueryException $e) {
396
-			// skip
397
-		}
398
-		return $forms;
399
-	}
400
-
401
-	/**
402
-	 * @param string $section
403
-	 * @return ISection[]
404
-	 */
405
-	private function getBuiltInPersonalSettings($section) {
406
-		$forms = [];
407
-		try {
408
-			if ($section === 'personal-info') {
409
-				/** @var ISettings $form */
410
-				$form = new Personal\PersonalInfo(
411
-					$this->config,
412
-					$this->userManager,
413
-					$this->groupManager,
414
-					$this->accountManager,
415
-					$this->appManager,
416
-					$this->l10nFactory,
417
-					$this->l
418
-				);
419
-				$forms[$form->getPriority()] = [$form];
420
-			}
421
-			if($section === 'security') {
422
-				/** @var ISettings $form */
423
-				$form = new Personal\Security();
424
-				$forms[$form->getPriority()] = [$form];
425
-			}
426
-			if ($section === 'additional') {
427
-				/** @var ISettings $form */
428
-				$form = new Personal\Additional($this->config);
429
-				$forms[$form->getPriority()] = [$form];
430
-			}
431
-		} catch (QueryException $e) {
432
-			// skip
433
-		}
434
-		return $forms;
435
-	}
436
-
437
-	/**
438
-	 * @inheritdoc
439
-	 */
440
-	public function getAdminSettings($section) {
441
-		$settings = $this->getBuiltInAdminSettings($section);
442
-		$dbRows = $this->mapper->getAdminSettingsFromDB($section);
443
-
444
-		foreach ($dbRows as $row) {
445
-			if (!isset($settings[$row['priority']])) {
446
-				$settings[$row['priority']] = [];
447
-			}
448
-			try {
449
-				$settings[$row['priority']][] = $this->query($row['class']);
450
-			} catch (QueryException $e) {
451
-				// skip
452
-			}
453
-		}
454
-
455
-		ksort($settings);
456
-		return $settings;
457
-	}
458
-
459
-	/**
460
-	 * @inheritdoc
461
-	 */
462
-	public function getPersonalSections() {
463
-		$sections = [
464
-			0 => [new Section('personal-info', $this->l->t('Personal info'), 0, $this->url->imagePath('core', 'actions/info.svg'))],
465
-			5 => [new Section('security', $this->l->t('Security'), 0, $this->url->imagePath('settings', 'password.svg'))],
466
-			15 => [new Section('sync-clients', $this->l->t('Sync clients'), 0, $this->url->imagePath('settings', 'change.svg'))],
467
-		];
468
-
469
-		$legacyForms = \OC_App::getForms('personal');
470
-		if(count($legacyForms) > 0 && $this->hasLegacyPersonalSettingsToRender($legacyForms)) {
471
-			$sections[98] = [new Section('additional', $this->l->t('Additional settings'), 0, $this->url->imagePath('core', 'actions/settings-dark.svg'))];
472
-		}
473
-
474
-		$rows = $this->mapper->getPersonalSectionsFromDB();
475
-
476
-		foreach ($rows as $row) {
477
-			if (!isset($sections[$row['priority']])) {
478
-				$sections[$row['priority']] = [];
479
-			}
480
-			try {
481
-				$sections[$row['priority']][] = $this->query($row['class']);
482
-			} catch (QueryException $e) {
483
-				// skip
484
-			}
485
-		}
486
-
487
-		ksort($sections);
488
-
489
-		return $sections;
490
-	}
491
-
492
-	/**
493
-	 * @param $forms
494
-	 * @return bool
495
-	 */
496
-	private function hasLegacyPersonalSettingsToRender($forms) {
497
-		foreach ($forms as $form) {
498
-			if(trim($form) !== '') {
499
-				return true;
500
-			}
501
-		}
502
-		return false;
503
-	}
504
-
505
-	/**
506
-	 * @inheritdoc
507
-	 */
508
-	public function getPersonalSettings($section) {
509
-		$settings = $this->getBuiltInPersonalSettings($section);
510
-		$dbRows = $this->mapper->getPersonalSettingsFromDB($section);
511
-
512
-		foreach ($dbRows as $row) {
513
-			if (!isset($settings[$row['priority']])) {
514
-				$settings[$row['priority']] = [];
515
-			}
516
-			try {
517
-				$settings[$row['priority']][] = $this->query($row['class']);
518
-			} catch (QueryException $e) {
519
-				// skip
520
-			}
521
-		}
522
-
523
-		ksort($settings);
524
-		return $settings;
525
-	}
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(get_class($section), $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
+            'class',
236
+            get_class($settings),
237
+            [
238
+                'section' => $settings->getSection(),
239
+                'priority' => $settings->getPriority(),
240
+            ]
241
+        );
242
+    }
243
+
244
+    private function updateSection(ISection $section, $table) {
245
+        $this->mapper->update(
246
+            $table,
247
+            'class',
248
+            get_class($section),
249
+            [
250
+                'id' => $section->getID(),
251
+                'priority' => $section->getPriority(),
252
+            ]
253
+        );
254
+    }
255
+
256
+    /**
257
+     * @param string $className
258
+     * @param string $table
259
+     * @return bool
260
+     */
261
+    private function hasSection($className, $table) {
262
+        return $this->mapper->has($table, $className);
263
+    }
264
+
265
+    /**
266
+     * @param string $className
267
+     * @return bool
268
+     */
269
+    private function hasSettings($className, $table) {
270
+        return $this->mapper->has($table, $className);
271
+    }
272
+
273
+    private function setupSettingsEntry($settingsClassName, $type) {
274
+        if (!class_exists($settingsClassName)) {
275
+            $this->log->debug('Could not find ' . $type . ' section class ' . $settingsClassName);
276
+            return;
277
+        }
278
+
279
+        try {
280
+            /** @var ISettings $settings */
281
+            $settings = $this->query($settingsClassName);
282
+        } catch (QueryException $e) {
283
+            // cancel
284
+            return;
285
+        }
286
+
287
+        if (!$settings instanceof ISettings) {
288
+            $this->log->error(
289
+                ucfirst($type) . ' section instance must implement \OCP\Settings\ISettings. Invalid class: {class}',
290
+                ['class' => $settingsClassName]
291
+            );
292
+            return;
293
+        }
294
+        $table = $this->getSettingsTableForType($type);
295
+        if (!$this->hasSettings(get_class($settings), $table)) {
296
+            $this->addSettings($settings, $table);
297
+        } else {
298
+            $this->updateSettings($settings, $table);
299
+        }
300
+    }
301
+
302
+    private function getSectionTableForType($type) {
303
+        if($type === 'admin') {
304
+            return Mapper::TABLE_ADMIN_SECTIONS;
305
+        } else if($type === 'personal') {
306
+            return Mapper::TABLE_PERSONAL_SECTIONS;
307
+        }
308
+        throw new \InvalidArgumentException('"admin" or "personal" expected');
309
+    }
310
+
311
+    private function getSettingsTableForType($type) {
312
+        if($type === 'admin') {
313
+            return Mapper::TABLE_ADMIN_SETTINGS;
314
+        } else if($type === 'personal') {
315
+            return Mapper::TABLE_PERSONAL_SETTINGS;
316
+        }
317
+        throw new \InvalidArgumentException('"admin" or "personal" expected');
318
+    }
319
+
320
+    private function query($className) {
321
+        try {
322
+            return \OC::$server->query($className);
323
+        } catch (QueryException $e) {
324
+            $this->log->logException($e);
325
+            throw $e;
326
+        }
327
+    }
328
+
329
+    /**
330
+     * @inheritdoc
331
+     */
332
+    public function getAdminSections() {
333
+        // built-in sections
334
+        $sections = [
335
+            0 => [new Section('server', $this->l->t('Basic settings'), 0, $this->url->imagePath('settings', 'admin.svg'))],
336
+            5 => [new Section('sharing', $this->l->t('Sharing'), 0, $this->url->imagePath('core', 'actions/share.svg'))],
337
+            10 => [new Section('security', $this->l->t('Security'), 0, $this->url->imagePath('core', 'actions/password.svg'))],
338
+            45 => [new Section('encryption', $this->l->t('Encryption'), 0, $this->url->imagePath('core', 'actions/password.svg'))],
339
+            98 => [new Section('additional', $this->l->t('Additional settings'), 0, $this->url->imagePath('core', 'actions/settings-dark.svg'))],
340
+            99 => [new Section('tips-tricks', $this->l->t('Tips & tricks'), 0, $this->url->imagePath('settings', 'help.svg'))],
341
+        ];
342
+
343
+        $rows = $this->mapper->getAdminSectionsFromDB();
344
+
345
+        foreach ($rows as $row) {
346
+            if (!isset($sections[$row['priority']])) {
347
+                $sections[$row['priority']] = [];
348
+            }
349
+            try {
350
+                $sections[$row['priority']][] = $this->query($row['class']);
351
+            } catch (QueryException $e) {
352
+                // skip
353
+            }
354
+        }
355
+
356
+        ksort($sections);
357
+
358
+        return $sections;
359
+    }
360
+
361
+    /**
362
+     * @param string $section
363
+     * @return ISection[]
364
+     */
365
+    private function getBuiltInAdminSettings($section) {
366
+        $forms = [];
367
+        try {
368
+            if ($section === 'server') {
369
+                /** @var ISettings $form */
370
+                $form = new Admin\Server($this->dbc, $this->request, $this->config, $this->lockingProvider, $this->l);
371
+                $forms[$form->getPriority()] = [$form];
372
+                $form = new Admin\ServerDevNotice();
373
+                $forms[$form->getPriority()] = [$form];
374
+            }
375
+            if ($section === 'encryption') {
376
+                /** @var ISettings $form */
377
+                $form = new Admin\Encryption($this->encryptionManager, $this->userManager);
378
+                $forms[$form->getPriority()] = [$form];
379
+            }
380
+            if ($section === 'sharing') {
381
+                /** @var ISettings $form */
382
+                $form = new Admin\Sharing($this->config);
383
+                $forms[$form->getPriority()] = [$form];
384
+            }
385
+            if ($section === 'additional') {
386
+                /** @var ISettings $form */
387
+                $form = new Admin\Additional($this->config);
388
+                $forms[$form->getPriority()] = [$form];
389
+            }
390
+            if ($section === 'tips-tricks') {
391
+                /** @var ISettings $form */
392
+                $form = new Admin\TipsTricks($this->config);
393
+                $forms[$form->getPriority()] = [$form];
394
+            }
395
+        } catch (QueryException $e) {
396
+            // skip
397
+        }
398
+        return $forms;
399
+    }
400
+
401
+    /**
402
+     * @param string $section
403
+     * @return ISection[]
404
+     */
405
+    private function getBuiltInPersonalSettings($section) {
406
+        $forms = [];
407
+        try {
408
+            if ($section === 'personal-info') {
409
+                /** @var ISettings $form */
410
+                $form = new Personal\PersonalInfo(
411
+                    $this->config,
412
+                    $this->userManager,
413
+                    $this->groupManager,
414
+                    $this->accountManager,
415
+                    $this->appManager,
416
+                    $this->l10nFactory,
417
+                    $this->l
418
+                );
419
+                $forms[$form->getPriority()] = [$form];
420
+            }
421
+            if($section === 'security') {
422
+                /** @var ISettings $form */
423
+                $form = new Personal\Security();
424
+                $forms[$form->getPriority()] = [$form];
425
+            }
426
+            if ($section === 'additional') {
427
+                /** @var ISettings $form */
428
+                $form = new Personal\Additional($this->config);
429
+                $forms[$form->getPriority()] = [$form];
430
+            }
431
+        } catch (QueryException $e) {
432
+            // skip
433
+        }
434
+        return $forms;
435
+    }
436
+
437
+    /**
438
+     * @inheritdoc
439
+     */
440
+    public function getAdminSettings($section) {
441
+        $settings = $this->getBuiltInAdminSettings($section);
442
+        $dbRows = $this->mapper->getAdminSettingsFromDB($section);
443
+
444
+        foreach ($dbRows as $row) {
445
+            if (!isset($settings[$row['priority']])) {
446
+                $settings[$row['priority']] = [];
447
+            }
448
+            try {
449
+                $settings[$row['priority']][] = $this->query($row['class']);
450
+            } catch (QueryException $e) {
451
+                // skip
452
+            }
453
+        }
454
+
455
+        ksort($settings);
456
+        return $settings;
457
+    }
458
+
459
+    /**
460
+     * @inheritdoc
461
+     */
462
+    public function getPersonalSections() {
463
+        $sections = [
464
+            0 => [new Section('personal-info', $this->l->t('Personal info'), 0, $this->url->imagePath('core', 'actions/info.svg'))],
465
+            5 => [new Section('security', $this->l->t('Security'), 0, $this->url->imagePath('settings', 'password.svg'))],
466
+            15 => [new Section('sync-clients', $this->l->t('Sync clients'), 0, $this->url->imagePath('settings', 'change.svg'))],
467
+        ];
468
+
469
+        $legacyForms = \OC_App::getForms('personal');
470
+        if(count($legacyForms) > 0 && $this->hasLegacyPersonalSettingsToRender($legacyForms)) {
471
+            $sections[98] = [new Section('additional', $this->l->t('Additional settings'), 0, $this->url->imagePath('core', 'actions/settings-dark.svg'))];
472
+        }
473
+
474
+        $rows = $this->mapper->getPersonalSectionsFromDB();
475
+
476
+        foreach ($rows as $row) {
477
+            if (!isset($sections[$row['priority']])) {
478
+                $sections[$row['priority']] = [];
479
+            }
480
+            try {
481
+                $sections[$row['priority']][] = $this->query($row['class']);
482
+            } catch (QueryException $e) {
483
+                // skip
484
+            }
485
+        }
486
+
487
+        ksort($sections);
488
+
489
+        return $sections;
490
+    }
491
+
492
+    /**
493
+     * @param $forms
494
+     * @return bool
495
+     */
496
+    private function hasLegacyPersonalSettingsToRender($forms) {
497
+        foreach ($forms as $form) {
498
+            if(trim($form) !== '') {
499
+                return true;
500
+            }
501
+        }
502
+        return false;
503
+    }
504
+
505
+    /**
506
+     * @inheritdoc
507
+     */
508
+    public function getPersonalSettings($section) {
509
+        $settings = $this->getBuiltInPersonalSettings($section);
510
+        $dbRows = $this->mapper->getPersonalSettingsFromDB($section);
511
+
512
+        foreach ($dbRows as $row) {
513
+            if (!isset($settings[$row['priority']])) {
514
+                $settings[$row['priority']] = [];
515
+            }
516
+            try {
517
+                $settings[$row['priority']][] = $this->query($row['class']);
518
+            } catch (QueryException $e) {
519
+                // skip
520
+            }
521
+        }
522
+
523
+        ksort($settings);
524
+        return $settings;
525
+    }
526 526
 }
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(get_class($section), $table)) {
209
+		if (!$this->hasSection(get_class($section), $table)) {
210 210
 			$this->addSection($section, $table);
211 211
 		} else {
212 212
 			$this->updateSection($section, $table);
@@ -272,7 +272,7 @@  discard block
 block discarded – undo
272 272
 
273 273
 	private function setupSettingsEntry($settingsClassName, $type) {
274 274
 		if (!class_exists($settingsClassName)) {
275
-			$this->log->debug('Could not find ' . $type . ' section class ' . $settingsClassName);
275
+			$this->log->debug('Could not find '.$type.' section class '.$settingsClassName);
276 276
 			return;
277 277
 		}
278 278
 
@@ -286,7 +286,7 @@  discard block
 block discarded – undo
286 286
 
287 287
 		if (!$settings instanceof ISettings) {
288 288
 			$this->log->error(
289
-				ucfirst($type) . ' section instance must implement \OCP\Settings\ISettings. Invalid class: {class}',
289
+				ucfirst($type).' section instance must implement \OCP\Settings\ISettings. Invalid class: {class}',
290 290
 				['class' => $settingsClassName]
291 291
 			);
292 292
 			return;
@@ -300,18 +300,18 @@  discard block
 block discarded – undo
300 300
 	}
301 301
 
302 302
 	private function getSectionTableForType($type) {
303
-		if($type === 'admin') {
303
+		if ($type === 'admin') {
304 304
 			return Mapper::TABLE_ADMIN_SECTIONS;
305
-		} else if($type === 'personal') {
305
+		} else if ($type === 'personal') {
306 306
 			return Mapper::TABLE_PERSONAL_SECTIONS;
307 307
 		}
308 308
 		throw new \InvalidArgumentException('"admin" or "personal" expected');
309 309
 	}
310 310
 
311 311
 	private function getSettingsTableForType($type) {
312
-		if($type === 'admin') {
312
+		if ($type === 'admin') {
313 313
 			return Mapper::TABLE_ADMIN_SETTINGS;
314
-		} else if($type === 'personal') {
314
+		} else if ($type === 'personal') {
315 315
 			return Mapper::TABLE_PERSONAL_SETTINGS;
316 316
 		}
317 317
 		throw new \InvalidArgumentException('"admin" or "personal" expected');
@@ -418,7 +418,7 @@  discard block
 block discarded – undo
418 418
 				);
419 419
 				$forms[$form->getPriority()] = [$form];
420 420
 			}
421
-			if($section === 'security') {
421
+			if ($section === 'security') {
422 422
 				/** @var ISettings $form */
423 423
 				$form = new Personal\Security();
424 424
 				$forms[$form->getPriority()] = [$form];
@@ -467,7 +467,7 @@  discard block
 block discarded – undo
467 467
 		];
468 468
 
469 469
 		$legacyForms = \OC_App::getForms('personal');
470
-		if(count($legacyForms) > 0 && $this->hasLegacyPersonalSettingsToRender($legacyForms)) {
470
+		if (count($legacyForms) > 0 && $this->hasLegacyPersonalSettingsToRender($legacyForms)) {
471 471
 			$sections[98] = [new Section('additional', $this->l->t('Additional settings'), 0, $this->url->imagePath('core', 'actions/settings-dark.svg'))];
472 472
 		}
473 473
 
@@ -495,7 +495,7 @@  discard block
 block discarded – undo
495 495
 	 */
496 496
 	private function hasLegacyPersonalSettingsToRender($forms) {
497 497
 		foreach ($forms as $form) {
498
-			if(trim($form) !== '') {
498
+			if (trim($form) !== '') {
499 499
 				return true;
500 500
 			}
501 501
 		}
Please login to merge, or discard this patch.