Skip to content

Instantly share code, notes, and snippets.

@Advanc8d
Forked from qwertik17/client_config
Created October 31, 2021 13:22
Show Gist options
  • Save Advanc8d/a5cf669142c3ef907abb4987f09534e6 to your computer and use it in GitHub Desktop.
Save Advanc8d/a5cf669142c3ef907abb4987f09534e6 to your computer and use it in GitHub Desktop.
Add groups & fields for MODx ClientConfig
<?php
//СОЗДАНИЕ ИЛИ ОБНОВЛЕНИЕ ГРУПП
$groups = array(
array('id' => 1,'sortorder' => 1,'label' => 'Общие'),
array('id' => 2,'sortorder' => 2,'label' => 'Почта'),
array('id' => 3,'sortorder' => 3,'label' => 'Политика и куки'),
array('id' => 4,'sortorder' => 4,'label' => 'Социальные сети'),
);
foreach ($groups as $group)
{
$check_isset_group = $modx->getObject('cgGroup', array($group['id']));
if (!$check_isset_group)
{
$new_group = $modx->newObject('cgGroup');
foreach ($group as $key=>$value)
{
$new_group->set($key,$value);
}
if ($new_group->save())
{
echo "<p>Группа {$group['label']} успешно <strong>создана</strong></p>";
} else {
echo "<p>Группу {$group['label']} <strong>создать не удалось</strong></p>";
};
} else {
foreach ($group as $key=>$value)
{
$check_isset_group->set($key,$value);
}
if ($check_isset_group->save())
{
echo "<p>Группа {$group['label']} успешно <strong>обновлена</strong></p>";
} else {
echo "<p>Группу {$group['label']} <strong>обновить не удалось</strong></p>";
};
}
}
echo '<hr>';
//СОЗДАНИЕ ИЛИ ОБНОВЛЕНИЕ ПОЛЕЙ
/*
xtype - textfield / textarea / xcheckbox
group - 1- Основные / 2 - Почта / 3 - Политика
*/
$client_config_fields = array(
array(
'key' => 'emailsender',
'label' => 'E-mail администратора',
'description' => 'Указывать только один адрес! Используется как почта отправителя.',
'xtype' => 'textfield',
'sortorder' => 10,
'value' => 'info@'.$modx->getOption('http_host'),
'source' => 0,
'group' => 2
),
array(
'key' => 'main_address',
'label' => 'Основной адрес',
'description' => 'Используется в шапке',
'xtype' => 'textfield',
'sortorder' => 10,
'value' => '',
'source' => 0,
'group' => 1
),
array(
'key' => 'main_phone',
'label' => 'Основной телефон',
'description' => 'Используется в шапке',
'xtype' => 'textfield',
'sortorder' => 10,
'value' => '8 (8452) 39-84-41',
'source' => 0,
'group' => 1
),
array(
'key' => 'insta',
'label' => 'Инстаграмм',
'description' => '',
'xtype' => 'textfield',
'sortorder' => 10,
'value' => '#1234',
'source' => 0,
'group' => 4
),
);
foreach ($client_config_fields as $field)
{
$check_isset_field = $modx->getObject('cgSetting', array('key'=>$field['key']));
if (!$check_isset_field) {
$new_field = $modx->newObject('cgSetting');
foreach ($field as $key=>$value)
{
$new_field->set($key,$value);
}
if ($new_field->save())
{
echo "<p>Поле {$field['key']} успешно <strong>создано</strong></p>";
} else {
echo "<p>Поле {$field['key']} <strong>создать не удалось</strong></p>";
};
} else {
foreach ($field as $key=>$value)
{
$check_isset_field->set($key,$value);
}
if ($check_isset_field->save())
{
echo "<p>Поле {$field['key']} успешно <strong>обновлено</strong></p>";
} else {
echo "<p>Поле {$field['key']} <strong>обновить не удалось</strong></p>";
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment