[#42] refactored profile management
This commit is contained in:
parent
62399657e7
commit
fd47b91b65
2 changed files with 58 additions and 49 deletions
|
@ -21,12 +21,6 @@ use CRM_Twingle_ExtensionUtil as E;
|
||||||
*/
|
*/
|
||||||
class CRM_Twingle_Profile {
|
class CRM_Twingle_Profile {
|
||||||
|
|
||||||
/**
|
|
||||||
* @var CRM_Twingle_Profile[] $_profiles
|
|
||||||
* Caches the profile objects.
|
|
||||||
*/
|
|
||||||
protected static $_profiles = NULL;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string $name
|
* @var string $name
|
||||||
* The name of the profile.
|
* The name of the profile.
|
||||||
|
@ -56,6 +50,20 @@ class CRM_Twingle_Profile {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logs (production) access to this profile
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function logAccess() {
|
||||||
|
CRM_Core_DAO::executeQuery("
|
||||||
|
UPDATE civicrm_twingle_profile
|
||||||
|
SET
|
||||||
|
last_access = NOW(),
|
||||||
|
access_counter = access_counter + 1
|
||||||
|
WHERE name = %1", [1 => [$this->name, 'String']]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks whether the profile's selector matches the given project ID.
|
* Checks whether the profile's selector matches the given project ID.
|
||||||
*
|
*
|
||||||
|
@ -79,7 +87,7 @@ class CRM_Twingle_Profile {
|
||||||
* The profile's configured custom field mapping
|
* The profile's configured custom field mapping
|
||||||
*/
|
*/
|
||||||
public function getCustomFieldMapping() {
|
public function getCustomFieldMapping() {
|
||||||
$custom_field_mapping = array();
|
$custom_field_mapping = [];
|
||||||
if (!empty($custom_field_definition = $this->getAttribute('custom_field_mapping'))) {
|
if (!empty($custom_field_definition = $this->getAttribute('custom_field_mapping'))) {
|
||||||
foreach (preg_split('/\r\n|\r|\n/', $custom_field_definition, -1, PREG_SPLIT_NO_EMPTY) as $custom_field_map) {
|
foreach (preg_split('/\r\n|\r|\n/', $custom_field_definition, -1, PREG_SPLIT_NO_EMPTY) as $custom_field_map) {
|
||||||
list($twingle_field_name, $custom_field_name) = explode("=", $custom_field_map);
|
list($twingle_field_name, $custom_field_name) = explode("=", $custom_field_map);
|
||||||
|
@ -144,7 +152,7 @@ class CRM_Twingle_Profile {
|
||||||
*/
|
*/
|
||||||
public function setAttribute($attribute_name, $value) {
|
public function setAttribute($attribute_name, $value) {
|
||||||
if (!in_array($attribute_name, self::allowedAttributes())) {
|
if (!in_array($attribute_name, self::allowedAttributes())) {
|
||||||
throw new Exception(E::ts('Unknown attribute %1.', array(1 => $attribute_name)));
|
throw new Exception(E::ts('Unknown attribute %1.', [1 => $attribute_name]));
|
||||||
}
|
}
|
||||||
// TODO: Check if value is acceptable.
|
// TODO: Check if value is acceptable.
|
||||||
$this->data[$attribute_name] = $value;
|
$this->data[$attribute_name] = $value;
|
||||||
|
@ -182,17 +190,36 @@ class CRM_Twingle_Profile {
|
||||||
* Persists the profile within the CiviCRM settings.
|
* Persists the profile within the CiviCRM settings.
|
||||||
*/
|
*/
|
||||||
public function saveProfile() {
|
public function saveProfile() {
|
||||||
self::$_profiles[$this->getName()] = $this;
|
// make sure it's valid
|
||||||
$this->verifyProfile();
|
$this->verifyProfile();
|
||||||
self::storeProfiles();
|
|
||||||
|
// check if the profile exists
|
||||||
|
$profile_id = CRM_Core_DAO::singleValueQuery(
|
||||||
|
"SELECT id FROM civicrm_twingle_profile WHERE name = %1", [1 => [$this->name, 'String']]);
|
||||||
|
if ($profile_id) {
|
||||||
|
// existing profile -> just update the config
|
||||||
|
CRM_Core_DAO::executeQuery(
|
||||||
|
"UPDATE civicrm_twingle_profile SET config = %2 WHERE name = %1",
|
||||||
|
[
|
||||||
|
1 => [$this->name, 'String'],
|
||||||
|
2 => [json_encode($this->data), 'String']
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
// new profile -> add new entry to the DB
|
||||||
|
CRM_Core_DAO::executeQuery(
|
||||||
|
"INSERT IGNORE INTO civicrm_twingle_profile(name,config,last_access,access_counter) VALUES (%1, %2, null, 0)",
|
||||||
|
[
|
||||||
|
1 => [$this->name, 'String'],
|
||||||
|
2 => [json_encode($this->data), 'String']
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes the profile from the CiviCRM settings.
|
* Deletes the profile from the database
|
||||||
*/
|
*/
|
||||||
public function deleteProfile() {
|
public function deleteProfile() {
|
||||||
unset(self::$_profiles[$this->getName()]);
|
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_twingle_profile WHERE name = %1", [1 => [$this->name, 'String']]);
|
||||||
self::storeProfiles();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -202,7 +229,7 @@ class CRM_Twingle_Profile {
|
||||||
*/
|
*/
|
||||||
public static function allowedAttributes() {
|
public static function allowedAttributes() {
|
||||||
return array_merge(
|
return array_merge(
|
||||||
array(
|
[
|
||||||
'selector',
|
'selector',
|
||||||
'xcm_profile',
|
'xcm_profile',
|
||||||
'location_type_id',
|
'location_type_id',
|
||||||
|
@ -228,7 +255,7 @@ class CRM_Twingle_Profile {
|
||||||
'membership_postprocess_call',
|
'membership_postprocess_call',
|
||||||
'newsletter_double_opt_in',
|
'newsletter_double_opt_in',
|
||||||
'required_address_components',
|
'required_address_components',
|
||||||
),
|
],
|
||||||
// Add payment methods.
|
// Add payment methods.
|
||||||
array_keys(static::paymentInstruments()),
|
array_keys(static::paymentInstruments()),
|
||||||
|
|
||||||
|
@ -245,7 +272,7 @@ class CRM_Twingle_Profile {
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public static function paymentInstruments() {
|
public static function paymentInstruments() {
|
||||||
return array(
|
return [
|
||||||
'pi_banktransfer' => E::ts('Bank transfer'),
|
'pi_banktransfer' => E::ts('Bank transfer'),
|
||||||
'pi_debit_manual' => E::ts('Debit manual'),
|
'pi_debit_manual' => E::ts('Debit manual'),
|
||||||
'pi_debit_automatic' => E::ts('Debit automatic'),
|
'pi_debit_automatic' => E::ts('Debit automatic'),
|
||||||
|
@ -257,7 +284,7 @@ class CRM_Twingle_Profile {
|
||||||
'pi_paydirekt' => E::ts('paydirekt'),
|
'pi_paydirekt' => E::ts('paydirekt'),
|
||||||
'pi_applepay' => E::ts('Apple Pay'),
|
'pi_applepay' => E::ts('Apple Pay'),
|
||||||
'pi_googlepay' => E::ts('Google Pay'),
|
'pi_googlepay' => E::ts('Google Pay'),
|
||||||
);
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -269,7 +296,7 @@ class CRM_Twingle_Profile {
|
||||||
* @return CRM_Twingle_Profile
|
* @return CRM_Twingle_Profile
|
||||||
*/
|
*/
|
||||||
public static function createDefaultProfile($name = 'default') {
|
public static function createDefaultProfile($name = 'default') {
|
||||||
return new CRM_Twingle_Profile($name, array(
|
return new CRM_Twingle_Profile($name, [
|
||||||
'selector' => '',
|
'selector' => '',
|
||||||
'xcm_profile' => '',
|
'xcm_profile' => '',
|
||||||
'location_type_id' => CRM_Twingle_Submission::LOCATION_TYPE_ID_WORK,
|
'location_type_id' => CRM_Twingle_Submission::LOCATION_TYPE_ID_WORK,
|
||||||
|
@ -307,7 +334,7 @@ class CRM_Twingle_Profile {
|
||||||
'city',
|
'city',
|
||||||
'country',
|
'country',
|
||||||
],
|
],
|
||||||
)
|
]
|
||||||
// Add contribution status for all payment methods.
|
// Add contribution status for all payment methods.
|
||||||
+ array_fill_keys(array_map(function($attribute) {
|
+ array_fill_keys(array_map(function($attribute) {
|
||||||
return $attribute . '_status';
|
return $attribute . '_status';
|
||||||
|
@ -344,9 +371,10 @@ class CRM_Twingle_Profile {
|
||||||
* @return CRM_Twingle_Profile | NULL
|
* @return CRM_Twingle_Profile | NULL
|
||||||
*/
|
*/
|
||||||
public static function getProfile($name) {
|
public static function getProfile($name) {
|
||||||
$profiles = self::getProfiles();
|
$profile_data = CRM_Core_DAO::singleValueQuery("SELECT config FROM civicrm_twingle_profile WHERE name = %1", [
|
||||||
if (isset($profiles[$name])) {
|
1 => [$name, 'String']]);
|
||||||
return $profiles[$name];
|
if ($profile_data) {
|
||||||
|
return new CRM_Twingle_Profile($name, json_decode($profile_data, 1));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -360,33 +388,11 @@ class CRM_Twingle_Profile {
|
||||||
* @return CRM_Twingle_Profile[]
|
* @return CRM_Twingle_Profile[]
|
||||||
*/
|
*/
|
||||||
public static function getProfiles() {
|
public static function getProfiles() {
|
||||||
if (self::$_profiles === NULL) {
|
$profiles = [];
|
||||||
self::$_profiles = array();
|
$profile_data = CRM_Core_DAO::executeQuery("SELECT name, config FROM civicrm_twingle_profile");
|
||||||
if ($profiles_data = Civi::settings()->get('twingle_profiles')) {
|
while ($profile_data->fetch()) {
|
||||||
foreach ($profiles_data as $profile_name => $profile_data) {
|
$profiles[$profile_data->name] = new CRM_Twingle_Profile($profile_data->name, json_decode($profile_data->config, 1));
|
||||||
self::$_profiles[$profile_name] = new CRM_Twingle_Profile($profile_name, $profile_data);
|
}
|
||||||
}
|
return $profiles;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Include the default profile if it was not overridden within the settings.
|
|
||||||
if (!isset(self::$_profiles['default'])) {
|
|
||||||
self::$_profiles['default'] = self::createDefaultProfile();
|
|
||||||
self::storeProfiles();
|
|
||||||
}
|
|
||||||
|
|
||||||
return self::$_profiles;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Persists the list of profiles into the CiviCRM settings.
|
|
||||||
*/
|
|
||||||
public static function storeProfiles() {
|
|
||||||
$profile_data = array();
|
|
||||||
foreach (self::$_profiles as $profile_name => $profile) {
|
|
||||||
$profile_data[$profile_name] = $profile->data;
|
|
||||||
}
|
|
||||||
Civi::settings()->set('twingle_profiles', $profile_data);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,9 @@ class CRM_Twingle_Upgrader extends CRM_Twingle_Upgrader_Base {
|
||||||
public function install() {
|
public function install() {
|
||||||
// create a DB table for the twingle profiles
|
// create a DB table for the twingle profiles
|
||||||
$this->executeSqlFile('sql/civicrm_twingle_profile.sql');
|
$this->executeSqlFile('sql/civicrm_twingle_profile.sql');
|
||||||
|
|
||||||
|
// add a default profile
|
||||||
|
CRM_Twingle_Profile::createDefaultProfile()->saveProfile();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue