1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Handles application settings |
5
|
|
|
* |
6
|
|
|
* @author Sam Stenvall <[email protected]> |
7
|
|
|
* @copyright Copyright © Sam Stenvall 2013- |
8
|
|
|
* @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0 |
9
|
|
|
*/ |
10
|
|
|
class SettingController extends AdminOnlyController |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Displays a form with all settings and allows the user to save or reset |
15
|
|
|
* them. |
16
|
|
|
*/ |
17
|
|
|
public function actionAdmin() |
18
|
|
|
{ |
19
|
|
|
$settings = Setting::model()->findAll(); |
20
|
|
|
$definitions = Setting::model()->getDefinitions(); |
21
|
|
|
|
22
|
|
|
// Sort the settings according to their defined order |
23
|
|
|
usort($settings, function($a, $b) use($definitions) |
24
|
|
|
{ |
25
|
|
|
return $definitions[$a->name]['order'] > |
26
|
|
|
$definitions[$b->name]['order']; |
27
|
|
|
}); |
28
|
|
|
|
29
|
|
|
if (isset($_POST['Setting'])) |
30
|
|
|
{ |
31
|
|
|
$allValid = true; |
32
|
|
|
|
33
|
|
|
// Validate all settings |
34
|
|
|
foreach ($settings as $setting) |
35
|
|
|
{ |
36
|
|
|
$value = $_POST['Setting'][$setting->name]; |
37
|
|
|
|
38
|
|
|
// Update the application language and reset any user |
39
|
|
|
// language preferences if the default is changed |
40
|
|
|
if ($setting->name === 'language' && $setting->value !== $value) |
41
|
|
|
{ |
42
|
|
|
User::model()->updateAll(array( |
43
|
|
|
'language'=>null)); |
44
|
|
|
|
45
|
|
|
Yii::app()->languageManager->setCurrent($value); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$setting->{$setting->name} = $value; |
49
|
|
|
$setting->value = $value; |
50
|
|
|
|
51
|
|
|
if (!$setting->validate(array($setting->name))) |
52
|
|
|
$allValid = false; |
53
|
|
|
|
54
|
|
|
// Flush the API call cache whenever its option is disabled |
55
|
|
|
if ($setting->name == 'cacheApiCalls' && !$value) |
56
|
|
|
Yii::app()->apiCallCache->flush(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// Only if all are valid we save them and refresh the page |
60
|
|
|
if ($allValid) |
61
|
|
|
{ |
62
|
|
|
foreach ($settings as $setting) |
63
|
|
|
$setting->save(false); |
64
|
|
|
|
65
|
|
|
$this->log('"%s" updated the application settings', Yii::app()->user->name); |
66
|
|
|
Yii::app()->user->setFlash('success', Yii::t('Settings', 'Settings updated successfully')); |
67
|
|
|
$this->refresh(); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->render('admin', array( |
72
|
|
|
'settings'=>$settings, |
73
|
|
|
'definitions'=>$definitions, |
74
|
|
|
)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Resets all settings to their default values and redirects to the admin |
79
|
|
|
* page |
80
|
|
|
*/ |
81
|
|
|
public function actionReset() |
82
|
|
|
{ |
83
|
|
|
foreach (Setting::model()->getDefinitions() as $name=> $definition) |
84
|
|
|
$this->saveSetting($name, $definition['default']); |
85
|
|
|
|
86
|
|
|
Yii::app()->user->setFlash('success', Yii::t('Settings', 'Settings have been reset to defaults')); |
87
|
|
|
|
88
|
|
|
$this->redirect(array('setting/admin')); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Saves the specified setting |
93
|
|
|
* @param string $name the setting name |
94
|
|
|
* @param string $value the setting value |
95
|
|
|
*/ |
96
|
|
|
private function saveSetting($name, $value) |
97
|
|
|
{ |
98
|
|
|
$setting = $this->loadModel($name); |
99
|
|
|
$setting->value = $value; |
100
|
|
|
$setting->save(false); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
} |