Skip to content

Commit

Permalink
Added convenience function for saving/removing configuration settings…
Browse files Browse the repository at this point in the history
…. Applied throughout code.
  • Loading branch information
mark committed Sep 21, 2009
1 parent 647af64 commit b446252
Show file tree
Hide file tree
Showing 16 changed files with 74 additions and 102 deletions.
4 changes: 1 addition & 3 deletions applications/conversations/controllers/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ public function Index() {
$ApplicationInfo = array();
include(CombinePaths(array(PATH_APPLICATIONS . DS . 'conversations' . DS . 'settings' . DS . 'about.php')));
$Version = ArrayValue('Version', ArrayValue('Conversations', $ApplicationInfo, array()), 'Undefined');
$Config->Load(PATH_CONF . DS . 'config.php', 'Save');
$Config->Set('Conversations.Version', $Version);
$Config->Save();
SaveToConfig('Conversations.Version', $Version);
}

return $this->Form->ErrorCount() > 0 ? FALSE : TRUE;
Expand Down
26 changes: 12 additions & 14 deletions applications/garden/controllers/gardensetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ public function Index($CurrentStep = 1) {

if ($CurrentStep == $TotalSteps) {
// Save a variable so that the application knows it has been installed.
$Config->Load(PATH_CONF . DS . 'config.php', 'Save');
$Config->Set('Garden.Installed', TRUE);
$Config->Save();
SaveToConfig('Garden.Installed', TRUE);

/*
$Database = Gdn::Database();
Expand All @@ -102,11 +100,10 @@ public function Index($CurrentStep = 1) {
*/
public function Configure($RedirectUrl = '') {
$Config = Gdn::Factory(Gdn::AliasConfig);
$ConfigFile = PATH_CONF . DS . 'config.php';

// Create a model to save configuration settings
$Validation = new Gdn_Validation();
$ConfigurationModel = new Gdn_ConfigurationModel('Configuration', $ConfigFile, $Validation);
$ConfigurationModel = new Gdn_ConfigurationModel($Validation);
$ConfigurationModel->SetField(array('Garden.Locale', 'Garden.Title', 'Garden.RewriteUrls', 'Garden.WebRoot', 'Garden.Cookie.Salt', 'Garden.Cookie.Domain', 'Database.Name', 'Database.Host', 'Database.User', 'Database.Password'));

// Set the models on the forms.
Expand Down Expand Up @@ -197,15 +194,16 @@ public function Configure($RedirectUrl = '') {
// Assign some extra settings to the configuration file if everything succeeded.
$ApplicationInfo = array();
include(CombinePaths(array(PATH_APPLICATIONS . DS . 'garden' . DS . 'settings' . DS . 'about.php')));
$Config->Load($ConfigFile, 'Save');
$Config->Set('Garden.Version', ArrayValue('Version', ArrayValue('Garden', $ApplicationInfo, array()), 'Undefined'));
$Config->Set('Garden.WebRoot', Gdn_Url::WebRoot());
$Config->Set('Garden.RewriteUrls', (function_exists('apache_get_modules') && in_array('mod_rewrite', apache_get_modules())) ? TRUE : FALSE);
$Config->Set('Garden.Domain', $Domain);
$Config->Set('Garden.CanProcessImages', function_exists('gd_info'));
$Config->Set('Garden.Messages.Cache', 'arr:["Garden\/Settings\/Index"]'); // Make sure that the "welcome" message is cached for viewing
$Config->Set('EnabledPlugins.HTMLPurifier', 'HtmlPurifier'); // Make sure html purifier is enabled so html has a default way of being safely parsed
$Config->Save();
$Save = array(
'Garden.Version' => ArrayValue('Version', ArrayValue('Garden', $ApplicationInfo, array()), 'Undefined'),
'Garden.WebRoot' => Gdn_Url::WebRoot(),
'Garden.RewriteUrls' => (function_exists('apache_get_modules') && in_array('mod_rewrite', apache_get_modules())) ? TRUE : FALSE,
'Garden.Domain' => $Domain,
'Garden.CanProcessImages' => function_exists('gd_info'),
'Garden.Messages.Cache' => 'arr:["Garden\/Settings\/Index"]', // Make sure that the "welcome" message is cached for viewing
'EnabledPlugins.HTMLPurifier' => 'HtmlPurifier' // Make sure html purifier is enabled so html has a default way of being safely parsed
);
SaveToConfig($Save);
}
}
return $this->Form->ErrorCount() == 0 ? TRUE : FALSE;
Expand Down
5 changes: 1 addition & 4 deletions applications/garden/controllers/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,7 @@ public function Index($Step = 0) {
// If there were no errors...
if ($this->Form->ErrorCount() == 0) {
// Save the sourceprefix
$Config = Gdn::Factory(Gdn::AliasConfig);
$Config->Load(PATH_CONF . DS . 'config.php', 'Save');
$Config->Set('Garden.Import.SourcePrefix', $SourcePrefix);
$Config->Save();
SaveToConfig('Garden.Import.SourcePrefix', $SourcePrefix);

// Proceed with the next step
$this->Message = Gdn::Translate('<strong>1/19</strong> Checking source & destination tables.');
Expand Down
14 changes: 3 additions & 11 deletions applications/garden/controllers/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function Edit($RouteIndex = FALSE) {
}

$Validation = new Gdn_Validation();
$ConfigurationModel = new Gdn_ConfigurationModel('Configuration', PATH_CONF . DS . 'config.php', $Validation);
$ConfigurationModel = new Gdn_ConfigurationModel($Validation);
$ConfigurationModel->SetField(array('Route', 'Target'));

// Set the model on the form.
Expand All @@ -69,11 +69,7 @@ public function Edit($RouteIndex = FALSE) {
$FormPostValues['Route'] = $this->Route;

if ($ConfigurationModel->Validate($FormPostValues)) {
$Config = Gdn::Factory(Gdn::AliasConfig);
$Path = PATH_CONF . DS . 'config.php';
$Config->Load($Path, 'Save');
$Config->Set('Routes'.'.'.ArrayValue('Route', $FormPostValues), ArrayValue('Target', $FormPostValues));
$Config->Save($Path);
SaveToConfig('Routes'.'.'.ArrayValue('Route', $FormPostValues), ArrayValue('Target', $FormPostValues));
$this->StatusMessage = Translate("The route was saved successfully.");
if ($this->_DeliveryType == DELIVERY_TYPE_ALL)
$this->RedirectUrl = Url('garden/routes');
Expand Down Expand Up @@ -102,11 +98,7 @@ public function Delete($RouteIndex = FALSE, $TransientKey = FALSE) {
&& !in_array($Key, $this->ReservedRoutes)
&& $Key !== FALSE
) {
$Config = Gdn::Factory(Gdn::AliasConfig);
$Path = PATH_CONF . DS . 'config.php';
$Config->Load($Path, 'Save');
$Config->Remove('Routes'.'.'.$Key);
$Config->Save($Path);
RemoveFromConfig('Routes'.'.'.$Key);
}

if ($this->_DeliveryType === DELIVERY_TYPE_ALL)
Expand Down
4 changes: 2 additions & 2 deletions applications/garden/controllers/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function Configure() {
}

$Validation = new Gdn_Validation();
$ConfigurationModel = new Gdn_ConfigurationModel('Configuration', PATH_CONF . DS . 'config.php', $Validation);
$ConfigurationModel = new Gdn_ConfigurationModel($Validation);
$ConfigurationModel->SetField(array(
'Garden.Locale',
'Garden.Title',
Expand Down Expand Up @@ -345,7 +345,7 @@ public function Registration($RedirectUrl = '') {

// Create a model to save configuration settings
$Validation = new Gdn_Validation();
$ConfigurationModel = new Gdn_ConfigurationModel('Configuration', PATH_CONF . DS . 'config.php', $Validation);
$ConfigurationModel = new Gdn_ConfigurationModel($Validation);
$ConfigurationModel->SetField(array('Garden.Registration.Method', 'Garden.Registration.DefaultRoles', 'Garden.Registration.CaptchaPrivateKey', 'Garden.Registration.CaptchaPublicKey', 'Garden.Registration.InviteExpiration'));

// Set the model on the forms.
Expand Down
11 changes: 5 additions & 6 deletions applications/garden/controllers/utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,16 @@ public function UpdateResponse() {
}
}

// Load the config file so we can save some info in it
$Config = Gdn::Factory(Gdn::AliasConfig);
$Config->Load(PATH_CONF . DS . 'config.php', 'Save');
// Save some info to the configuration file
$Save = array();

// If the response wasn't empty, save it in the config
if ($Response != '')
$Config->Set('Garden.RequiredUpdates', $Response);
$Save['Garden.RequiredUpdates'] = $Response;

// Record the current update check time in the config.
$Config->Set('Garden.UpdateCheckDate', time());
$Config->Save();
$Save['Garden.UpdateCheckDate'] = time();
SaveToConfig($Save);
}
}

Expand Down
5 changes: 1 addition & 4 deletions applications/garden/models/class.messagemodel.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ public function Save($FormPostValues, $Settings = FALSE) {

public function SetMessageCache() {
// Retrieve an array of all controllers that have enabled messages associated
$Config = Gdn::Factory(Gdn::AliasConfig);
$Config->Load(PATH_CONF . DS . 'config.php', 'Save');
$Config->Set('Garden.Messages.Cache', $this->GetEnabledLocations());
$Config->Save();
SaveToConfig('Garden.Messages.Cache', $this->GetEnabledLocations());
}
}
4 changes: 1 addition & 3 deletions applications/skeleton/controllers/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ public function Index() {
$ApplicationInfo = array();
include(CombinePaths(array(PATH_APPLICATIONS . DS . 'skeleton' . DS . 'settings' . DS . 'about.php')));
$Version = ArrayValue('Version', ArrayValue('Skeleton', $ApplicationInfo, array()), 'Undefined');
$Config->Load(PATH_CONF . DS . 'config.php', 'Save');
$Config->Set('Skeleton.Version', $Version);
$Config->Save();
SaveToConfig('Skeleton.Version', $Version);
}

return $this->Form->ErrorCount() > 0 ? FALSE : TRUE;
Expand Down
4 changes: 2 additions & 2 deletions applications/vanilla/controllers/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function Index() {
}

$Validation = new Gdn_Validation();
$ConfigurationModel = new Gdn_ConfigurationModel('Configuration', PATH_CONF . DS . 'config.php', $Validation);
$ConfigurationModel = new Gdn_ConfigurationModel($Validation);
$ConfigurationModel->SetField(array(
'Vanilla.Discussions.PerPage',
'Vanilla.Comments.AutoRefresh',
Expand Down Expand Up @@ -65,7 +65,7 @@ public function Spam() {
$this->AddSideMenu('vanilla/settings/spam');

$Validation = new Gdn_Validation();
$ConfigurationModel = new Gdn_ConfigurationModel('Configuration', PATH_CONF . DS . 'config.php', $Validation);
$ConfigurationModel = new Gdn_ConfigurationModel($Validation);
$ConfigurationModel->SetField(array(
'Vanilla.Discussion.SpamCount',
'Vanilla.Discussion.SpamTime',
Expand Down
9 changes: 5 additions & 4 deletions applications/vanilla/controllers/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ public function Index() {
$ApplicationInfo = array();
include(CombinePaths(array(PATH_APPLICATIONS . DS . 'vanilla' . DS . 'settings' . DS . 'about.php')));
$Version = ArrayValue('Version', ArrayValue('Vanilla', $ApplicationInfo, array()), 'Undefined');
$Config->Load(PATH_CONF . DS . 'config.php', 'Save');
$Config->Set('Vanilla.Version', $Version);
$Config->Set('Routes.DefaultController', 'discussions');
$Config->Save();
$Save = array(
'Vanilla.Version' => $Version,
'Routes.DefaultController' => 'discussions'
);
SaveToConfig($Save);
}

return $this->Form->ErrorCount() > 0 ? FALSE : TRUE;
Expand Down
5 changes: 1 addition & 4 deletions applications/vanilla/models/class.categorymodel.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ public function Delete($Category, $ReplacementCategoryID) {

// If there is only one category, make sure that Categories are not used
$CountCategories = $this->Get()->NumRows();
$Config = Gdn::Factory(Gdn::AliasConfig);
$Config->Load(PATH_CONF . DS . 'config.php', 'Save');
$Config->Set('Vanilla.Categories.Use', $CountCategories > 1, TRUE, 'ForSave');
$Config->Save();
SaveToConfig('Vanilla.Categories.Use', $CountCategories > 1);
}
// Make sure to reorganize the categories after deletes
$this->Organize();
Expand Down
10 changes: 2 additions & 8 deletions library/core/class.applicationmanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,7 @@ public function EnableApplication($ApplicationName, $Validation) {
if ($ApplicationFolder == '') {
throw new Exception(Gdn::Translate('The application folder was not properly defined.'));
} else {
$Config = Gdn::Factory(Gdn::AliasConfig);
$Config->Load(PATH_CONF . DS . 'config.php', 'Save');
$Config->Set('EnabledApplications'.'.'.$ApplicationName, $ApplicationFolder);
$Config->Save();
SaveToConfig('EnabledApplications'.'.'.$ApplicationName, $ApplicationFolder);
}

// Redefine the locale manager's settings $Locale->Set($CurrentLocale, $EnabledApps, $EnabledPlugins, TRUE);
Expand Down Expand Up @@ -191,10 +188,7 @@ public function DisableApplication($ApplicationName) {
}

// 2. Disable it
$Config = Gdn::Factory(Gdn::AliasConfig);
$Config->Load(PATH_CONF . DS . 'config.php', 'Save');
$Config->Remove('EnabledApplications'.'.'.$ApplicationName);
$Config->Save();
RemoveFromConfig('EnabledApplications'.'.'.$ApplicationName);

// Redefine the locale manager's settings $Locale->Set($CurrentLocale, $EnabledApps, $EnabledPlugins, TRUE);
$PluginManager = Gdn::Factory('PluginManager');
Expand Down
30 changes: 5 additions & 25 deletions library/core/class.configurationmodel.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,6 @@ class Gdn_ConfigurationModel {
*/
public $Data;

/**
* The full path to the configuration file where these settings should be
* saved.
*
* @var string
*/
protected $_ConfigurationFile;

/**
* A collection of Field => Values that will NOT be validated and WILL be
* saved as long as validation succeeds. You can add to this collection with
Expand All @@ -70,15 +62,12 @@ class Gdn_ConfigurationModel {
* Class constructor. Defines the related database table name.
*
* @param string $ConfigurationArrayName The name of the configuration array that is being manipulated.
* @param string $ConfigurationFile
* @param string $Validation
* @todo $ConfigurationFile and $Validation need descriptions and correct variable types.
* @param object $Validation
*/
public function __construct($ConfigurationArrayName, $ConfigurationFile, &$Validation) {
$this->Name = $ConfigurationArrayName;
public function __construct(&$Validation) {
$this->Name = 'Configuration';
$this->Data = array();
$this->Validation = &$Validation;
$this->_ConfigurationFile = $ConfigurationFile;
}

/**
Expand Down Expand Up @@ -145,27 +134,18 @@ private function NormalizeArray($Array) {
* from the form in the $_POST or $_GET collection.
*/
public function Save($FormPostValues) {
if (isset($this->_ConfigurationFile) === FALSE)
trigger_error(ErrorMessage('You must define the file where the configuration settings will be saved.', 'ConfigurationModel', 'Save'), E_USER_ERROR);

// Fudge your way through the schema application. This will allow me to
// force the validation object to expect the fieldnames contained in
// $this->Data.
$this->Validation->ApplySchema($this->Data);
// Validate the form posted values
if ($this->Validation->Validate($FormPostValues)) {
$Config = Gdn::Factory(Gdn::AliasConfig);
$Config->Load($this->_ConfigurationFile, 'Save', $this->Name);
// Merge the validation fields and the forced settings into a single array
$Settings = $this->Validation->ValidationFields();
if (is_array($this->_ForceSettings))
$Settings = MergeArrays($Settings, $this->_ForceSettings);

foreach ($Settings as $Setting => $Value) {
$Config->Set($Setting, $Value, TRUE);
}
// And save them to the conf file
return $Config->Save();

return SaveToConfig($Settings);
} else {
return FALSE;
}
Expand Down
10 changes: 2 additions & 8 deletions library/core/class.pluginmanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,7 @@ public function EnablePlugin($PluginName, $Validation, $Setup = FALSE) {
// 4. If everything succeeded, add the plugin to the
// $EnabledPlugins array in conf/plugins.php
// $EnabledPlugins['PluginClassName'] = 'Plugin Folder Name';
$Config = Gdn::Factory(Gdn::AliasConfig);
$Config->Load(PATH_CONF . DS . 'config.php', 'Save');
$Config->Set('EnabledPlugins'.'.'.$PluginName, $PluginFolder);
$Config->Save();
SaveToConfig('EnabledPlugins'.'.'.$PluginName, $PluginFolder);

$ApplicationManager = new Gdn_ApplicationManager();
$Locale = Gdn::Locale();
Expand All @@ -373,10 +370,7 @@ public function DisablePlugin($PluginName) {
}

// 2. Disable it
$Config = Gdn::Factory(Gdn::AliasConfig);
$Config->Load(PATH_CONF . DS . 'config.php', 'Save');
$Config->Remove('EnabledPlugins'.'.'.$PluginName);
$Config->Save();
RemoveFromConfig('EnabledPlugins'.'.'.$PluginName);

unset($this->EnabledPlugins[$PluginName]);

Expand Down
5 changes: 1 addition & 4 deletions library/core/class.thememanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ public function EnableTheme($ThemeName) {
if ($ThemeFolder == '') {
throw new Exception(Gdn::Translate('The theme folder was not properly defined.'));
} else {
$Config = Gdn::Factory(Gdn::AliasConfig);
$Config->Load(PATH_CONF . DS . 'config.php', 'Save');
$Config->Set('Garden.Theme', $ThemeFolder);
$Config->Save();
SaveToConfig('Garden.Theme', $ThemeFolder);
}

return TRUE;
Expand Down
30 changes: 30 additions & 0 deletions library/core/functions.general.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,36 @@ function Translate($Code, $Default = '') {
// }
//}

if (!function_exists('SaveToConfig')) {
function SaveToConfig($Name, $Value = '') {
$Config = Gdn::Factory(Gdn::AliasConfig);
$Path = PATH_CONF . DS . 'config.php';
$Config->Load($Path, 'Save');
if (!is_array($Name))
$Name = array($Name => $Value);

foreach ($Name as $k => $v) {
$Config->Set($k, $v);
}
return $Config->Save($Path);
}
}

if (!function_exists('RemoveFromConfig')) {
function RemoveFromConfig($Name) {
$Config = Gdn::Factory(Gdn::AliasConfig);
$Path = PATH_CONF . DS . 'config.php';
$Config->Load($Path, 'Save');
if (!is_array($Name))
$Name = array($Name);

foreach ($Name as $k) {
$Config->Remove($k);
}
return $Config->Save($Path);
}
}

if (!function_exists('MergeArrays')) {
/**
* Merge two associative arrays into a single array.
Expand Down

0 comments on commit b446252

Please sign in to comment.