Skip to content

Instantly share code, notes, and snippets.

@pepebe
Last active March 1, 2017 16:08
Show Gist options
  • Save pepebe/f37b5585c2aa3db2ccaf806b2f590956 to your computer and use it in GitHub Desktop.
Save pepebe/f37b5585c2aa3db2ccaf806b2f590956 to your computer and use it in GitHub Desktop.
This snippet will edit/add a number of settings to modx_system_settings.
<?php
/*
* This snippet will edit/add a number of settings to modx_system_settings.
* The $settings array MUST contain all relevant information.
* Use the example below to create your own list of relevant settings.
*
* Updating settings
* -----------------
* All settings allready present in the database are updated by this script.
* Keep in mind to send the correct type of data. So don't add "hello" to a key with xtype "combo-bolean".
*
* Adding new settings
* -------------------
* If a setting is not present, the snippet will add it to the database.
* Keep in mind, that if you don't specify the correct properties, modx will set standard properties for you
*
* Available properties:
* - value - mandatory - default: empty
* - namespace - optional - default: "core"
* - area - optional - default: "default"
* - xtype - optional - default: "textfield"
*
* Available xtypes:
* - combo-bolean
* - modx-combo-bolean
* - numberfield
* - text-password
* - textarea
* - textfield
*
* */
$debug = false;
$msg['snippet'] = 'pepebe_updateSystemSettings';
$msg['debug'] = $debug;
$settings = array(
/* personal settings */
'pepebe.bootstrap.path' => array(
'value' => 'assets/libs/bootstrap/3.3.7/'
,'namespace' => 'pepebe'
,'area' => 'libs'
,'xtype' => 'textfield'
)
,'pepebe.isotope.path' => array(
'value' => 'assets/libs/isotope/3.0.2/'
,'namespace' => 'pepebe'
,'area' => 'libs'
,'xtype' => 'textfield'
)
,'pepebe.jquery.path' => array(
'value' => 'assets/libs/jquery/1.12.4/'
,'namespace' => 'pepebe'
,'area' => 'libs'
,'xtype' => 'textfield'
)
,'pepebe.lightbox2.path' => array(
'value' => 'assets/libs/lightbox2/2.9.0/'
,'namespace' => 'pepebe'
,'area' => 'libs'
,'xtype' => 'textfield'
)
/* modx default settings */
,'friendly_alias_translit' => array(
'value' => 'german'
)
,'manager_time_format' => array(
'value' => 'H:i'
)
,'manager_date_format' => array(
'value' => 'd.m.Y'
)
,'date_timezone' => array(
'value' => 'Europe/Berlin'
)
,'locale' => array(
'value' => 'de_DE.UTF-8'
)
,'cultureKey' => array(
'value' => 'de'
)
,'friendly_alias_restrict_chars_pattern' => array(
'value' => '[^a-zA-Z0-9 _-]'
)
,'fe_editor_lang' => array(
'value' => 'de'
)
,'manager_lang_attribute' => array(
'value' => 'de'
)
,'manager_language' => array(
'value' => 'de'
)
/* modx extras settings */
,'redactor.lang' => array(
'value' => 'de'
,'namespace' => 'redactor'
,'area' => 'Internationalisation'
,'xtype' => 'textfield'
)
,'collections.mgr_date_format' => array(
'value' => 'd.m.Y'
,'namespace' => 'collections'
,'area' => 'manager'
,'xtype' => 'textfield'
)
,'collections.mgr_time_format' => array(
'value' => 'H:i'
,'namespace' => 'collections'
,'area' => 'manager'
,'xtype' => 'textfield'
)
,'collections.mgr_datetime_format' => array(
'value' => 'd.m.Y H:i'
,'namespace' => 'collections'
,'area' => 'manager'
,'xtype' => 'textfield'
)
);
$msg['elements'] = count($settings);
if (!$modx->hasPermission('settings')) return false;
if (!function_exists('pepebe_updateSystemSettings')) {
/**
* Sets the system settings
* @param string|array $key
* @return array|null|string
*/
function pepebe_updateSystemSettings($settings,$debug)
{
global $modx;
if (!empty($settings))
{
if (is_array($settings))
{
foreach ($settings as $key => $setting)
{
$changed++;
$updateObject = $modx->getObject('modSystemSetting', $key);
$msg[$key]['options'] = $setting;
if(!empty($updateObject))
{
/* update setting */
$updated++;
$msg[$key]['action']['type'] = 'update';
$msg[$key]['old_value'] = $updateObject->get('value');
$msg[$key]['new_value'] = $setting['value'];
if(!$debug)
{
$msg[$key]['action']['result'] = 'setting changed';
$updateObject->set('value', $setting['value']);
$updateObject->save();
}
else
{
$msg[$key]['action']['result'] = 'setting not changed (debug mode)';
}
}
else
{
/* create setting */
$created++;
$msg[$key]['action']['type'] = 'create';
$msg[$key]['old_value'] = "";
$msg[$key]['new_value'] = $setting['value'];
if(!$debug)
{
$msg[$key]['action']['result'] = 'setting added to system settings';
$createObject = $modx->newObject('modSystemSetting');
$createObject->set('key',$key);
$createObject->set('value',$setting['value']);
$createObject->set('namespace',$setting['namespace']);
$createObject->set('area',$setting['area']);
$createObject->set('xtype',$setting['xtype']);
$createObject->save();
}
else
{
$msg[$key]['action']['result'] = 'setting not added (debug mode)';
}
}
}
$msg['status']['changed'] = $changed;
$msg['status']['updated'] = $updated;
$msg['status']['created'] = $created;
return $msg;
}
else
{
$msg['status'] = 'Error: settings must contain an array';
return $msg;
}
}
else
{
$msg['status'] = 'Error: settings cannot be empty';
return $msg;
}
}
}
$msg['settings'] = pepebe_updateSystemSettings($settings,$debug);
/* reload the config or you'll just get the old version from the config cache */
$modx->reloadConfig();
/* get new values */
foreach($settings as $key => $value) {
$msg['settings'][$key]['new_value'] = $modx->getOption($key);
}
return "<br><pre>".print_r($msg,true)."</pre>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment