de.propeace.mailinglistsync/Civi/Mailinglistsync/MailingListSettings.php
2025-03-28 19:07:48 +01:00

135 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Civi\Mailinglistsync;
use CRM_Mailinglistsync_ExtensionUtil as E;
class MailingListSettings {
public const SETTINGS = [
E::SHORT_NAME . '_enable' => [
'data_type' => 'boolean',
],
E::SHORT_NAME . '_logging' => [
'data_type' => 'boolean',
],
E::SHORT_NAME . '_domain' => [
'data_type' => 'string',
],
E::SHORT_NAME . '_mlmmj_host' => [
'data_type' => 'string',
],
E::SHORT_NAME . '_mlmmj_token' => [
'data_type' => 'string',
],
E::SHORT_NAME . '_mlmmj_port' => [
'data_type' => 'integer',
'default_value' => 443,
],
E::SHORT_NAME . '_dovecot_host' => [
'data_type' => 'string',
],
E::SHORT_NAME . '_dovecot_token' => [
'data_type' => 'string',
],
E::SHORT_NAME . '_dovecot_port' => [
'data_type' => 'integer',
'default_value' => 443,
],
E::SHORT_NAME . '_participant_status' => [
'data_type' => 'array',
],
E::SHORT_NAME . '_ad_contact_tags' => [
'data_type' => 'array',
],
];
/**
* Stores a setting in Civi::settings
*
* @param array $settings
* Expects an array with key => value for the setting
*/
public static function set(array $settings): void {
// Remove possibly illegal data from settings
$settings = array_intersect_key($settings, self::SETTINGS);
// Cast settings to the correct datatype
foreach ($settings as $key => $value) {
if ($value !== '') {
settype($settings[$key], self::SETTINGS[$key]['data_type']);
} else {
$settings[$key] = NULL;
}
}
\Civi::settings()->add($settings);
}
/**
* Returns a specific value of a setting if the key is passed as parameter.
* Else all settings will be returned as associative array.
*
* @param null $key
* The name of the setting or NULL
*
* @return array|mixed|null
*/
public static function get($key = NULL): mixed {
if (!is_null($key)) {
return \Civi::settings()->get(E::SHORT_NAME . '_' . $key);
}
else {
$settings = [];
foreach (array_keys(self::SETTINGS) as $key) {
$settings[$key] = \Civi::settings()->get($key) ?? self::SETTINGS[$key]['default_value'] ?? NULL;
}
return $settings;
}
}
/**
* Validates the settings.
*
* @param array $values
* @param array $errors
*
* @return void
*/
public static function validate(array $values, array &$errors): void {
// Validate domain
if (empty($values[E::SHORT_NAME . '_domain'])) {
$errors[E::SHORT_NAME . '_domain'] = E::ts('Domain is required');
}
// Validate url if synchronization is enabled
$url = $values[E::SHORT_NAME . '_mlmmj_host'];
if (!empty($values[E::SHORT_NAME . '__mlmmj_enable']) && !filter_var($url, FILTER_VALIDATE_URL)) {
$errors[E::SHORT_NAME . '_mlmmj_host'] = E::ts('Invalid URL');
}
// Validate port if synchronization is enabled and port is set
$port = $values[E::SHORT_NAME . '_mlmmj_port'] ?? NULL;
if (!empty($values[E::SHORT_NAME . '_mlmmj_enable']) && !empty($port)) {
if (is_numeric($port)) {
$errors[E::SHORT_NAME . '_mlmmj_port'] = E::ts('Port must be a number');
}
if ($port < 1 || $port > 65535) {
$errors[E::SHORT_NAME . '_mlmmj_port'] = E::ts('Port must be between 1 and 65535');
}
}
// Require host and token if mlmmj is enabled
if (!empty($values[E::SHORT_NAME . '_mlmmj_enable'])) {
if (empty($values[E::SHORT_NAME . '_mlmmj_host'])) {
$errors[E::SHORT_NAME . '_mlmmj_host'] = E::ts('Host is required');
}
if (empty($values[E::SHORT_NAME . '_mlmmj_token'])) {
$errors[E::SHORT_NAME . '_mlmmj_token'] = E::ts('Token is required');
}
}
}
}