Refactoring for better performance and maintainability

This commit is contained in:
Jens Schuppe 2019-08-30 10:26:39 +02:00
parent 007dd179ba
commit 17589ef616
5 changed files with 248 additions and 148 deletions

View file

@ -44,6 +44,68 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
*/ */
protected static $_paymentInstruments = NULL; protected static $_paymentInstruments = NULL;
/**
* @var array
*
* A static cache of retrieved groups found within static::getGroups().
*/
protected static $_groups = NULL;
/**
* @var array
*
* A static cache of retrieved newsletter groups found within
* static::getNewsletterGroups().
*/
protected static $_newsletterGroups = NULL;
/**
* @var array
*
* A static cache of retrieved campaigns found within static::getCampaigns().
*/
protected static $_campaigns = NULL;
/**
* @var array
*
* A static cache of retrieved financial types found within
* static::getFinancialTypes().
*/
protected static $_financialTypes = NULL;
/**
* @var array
*
* A static cache of retrieved genders found within
* static::getGenderOptions().
*/
protected static $_genderOptions = NULL;
/**
* @var array
*
* A static cache of retrieved location types found within
* static::getLocationTypes().
*/
protected static $_locationTypes = NULL;
/**
* @var array
*
* A static cache of retrieved membership types found within
* static::getMembershipTypes().
*/
protected static $_membershipTypes = NULL;
/**
* @var array
*
* A static cache of retrieved CiviSEPA creditors found within
* static::getSepaCreditors().
*/
protected static $_sepaCreditors = NULL;
/** /**
* Builds the form structure. * Builds the form structure.
*/ */
@ -117,7 +179,7 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
'select', 'select',
'location_type_id', 'location_type_id',
E::ts('Location type'), E::ts('Location type'),
$this->getLocationTypes(), static::getLocationTypes(),
TRUE TRUE
); );
@ -125,7 +187,7 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
'select', 'select',
'location_type_id_organisation', 'location_type_id_organisation',
E::ts('Location type for organisations'), E::ts('Location type for organisations'),
$this->getLocationTypes(), static::getLocationTypes(),
TRUE TRUE
); );
@ -133,14 +195,14 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
'select', // field type 'select', // field type
'financial_type_id', // field name 'financial_type_id', // field name
E::ts('Financial type'), // field label E::ts('Financial type'), // field label
$this->getFinancialTypes(), // list of options static::getFinancialTypes(), // list of options
TRUE // is required TRUE // is required
); );
$this->add( $this->add(
'select', // field type 'select', // field type
'financial_type_id_recur', // field name 'financial_type_id_recur', // field name
E::ts('Financial type (recurring)'), // field label E::ts('Financial type (recurring)'), // field label
$this->getFinancialTypes(), // list of options static::getFinancialTypes(), // list of options
TRUE // is required TRUE // is required
); );
@ -148,21 +210,21 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
'select', 'select',
'gender_male', 'gender_male',
E::ts('Gender option for submitted value "male"'), E::ts('Gender option for submitted value "male"'),
$this->getGenderOptions(), static::getGenderOptions(),
TRUE TRUE
); );
$this->add( $this->add(
'select', 'select',
'gender_female', 'gender_female',
E::ts('Gender option for submitted value "female"'), E::ts('Gender option for submitted value "female"'),
$this->getGenderOptions(), static::getGenderOptions(),
TRUE TRUE
); );
$this->add( $this->add(
'select', 'select',
'gender_other', 'gender_other',
E::ts('Gender option for submitted value "other"'), E::ts('Gender option for submitted value "other"'),
$this->getGenderOptions(), static::getGenderOptions(),
TRUE TRUE
); );
@ -173,7 +235,7 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
'select', // field type 'select', // field type
$pi_name, // field name $pi_name, // field name
E::ts('Record %1 as', array(1 => $pi_label)), // field label E::ts('Record %1 as', array(1 => $pi_label)), // field label
$this->getPaymentInstruments(), // list of options static::getPaymentInstruments(), // list of options
TRUE // is required TRUE // is required
); );
} }
@ -183,7 +245,7 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
'select', 'select',
'sepa_creditor_id', 'sepa_creditor_id',
E::ts('CiviSEPA creditor'), E::ts('CiviSEPA creditor'),
$this->getSepaCreditors(), static::getSepaCreditors(),
TRUE TRUE
); );
} }
@ -192,7 +254,7 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
'select', // field type 'select', // field type
'newsletter_groups', // field name 'newsletter_groups', // field name
E::ts('Sign up for newsletter groups'), // field label E::ts('Sign up for newsletter groups'), // field label
$this->getNewsletterGroups(), // list of options static::getNewsletterGroups(), // list of options
FALSE, // is not required FALSE, // is not required
array('class' => 'crm-select2 huge', 'multiple' => 'multiple') array('class' => 'crm-select2 huge', 'multiple' => 'multiple')
); );
@ -201,7 +263,7 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
'select', // field type 'select', // field type
'postinfo_groups', // field name 'postinfo_groups', // field name
E::ts('Sign up for postal mail groups'), // field label E::ts('Sign up for postal mail groups'), // field label
$this->getPostinfoGroups(), // list of options static::getPostinfoGroups(), // list of options
FALSE, // is not required FALSE, // is not required
array('class' => 'crm-select2 huge', 'multiple' => 'multiple') array('class' => 'crm-select2 huge', 'multiple' => 'multiple')
); );
@ -210,7 +272,7 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
'select', // field type 'select', // field type
'donation_receipt_groups', // field name 'donation_receipt_groups', // field name
E::ts('Sign up for Donation receipt groups'), // field label E::ts('Sign up for Donation receipt groups'), // field label
$this->getDonationReceiptGroups(), // list of options static::getDonationReceiptGroups(), // list of options
FALSE, // is not required FALSE, // is not required
array('class' => 'crm-select2 huge', 'multiple' => 'multiple') array('class' => 'crm-select2 huge', 'multiple' => 'multiple')
); );
@ -219,7 +281,7 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
'select', // field type 'select', // field type
'campaign', // field name 'campaign', // field name
E::ts('Assign donation to campaign'), // field label E::ts('Assign donation to campaign'), // field label
array('' => E::ts('- none -')) + $this->getCampaigns(), // list of options array('' => E::ts('- none -')) + static::getCampaigns(), // list of options
FALSE, // is not required FALSE, // is not required
array('class' => 'crm-select2 huge') array('class' => 'crm-select2 huge')
); );
@ -228,7 +290,7 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
'select', // field type 'select', // field type
'membership_type_id', // field name 'membership_type_id', // field name
E::ts('Create membership of type'), // field label E::ts('Create membership of type'), // field label
array('' => E::ts('- none -')) + $this->getMembershipTypes(), // list of options array('' => E::ts('- none -')) + static::getMembershipTypes(), // list of options
FALSE, // is not required FALSE, // is not required
array('class' => 'crm-select2 huge') array('class' => 'crm-select2 huge')
); );
@ -393,16 +455,18 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
* *
* @throws \CiviCRM_API3_Exception * @throws \CiviCRM_API3_Exception
*/ */
public function getLocationTypes() { public static function getLocationTypes() {
$location_types = array(); if (!isset(static::$_locationTypes)) {
$query = civicrm_api3('LocationType', 'get', array( static::$_locationTypes = array();
'is_active' => 1, $query = civicrm_api3('LocationType', 'get', array(
)); 'option.limit' => 0,
foreach ($query['values'] as $type) { 'is_active' => 1,
$location_types[$type['id']] = $type['name']; ));
foreach ($query['values'] as $type) {
static::$_locationTypes[$type['id']] = $type['name'];
}
} }
return static::$_locationTypes;
return $location_types;
} }
/** /**
@ -413,17 +477,19 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
* *
* @throws \CiviCRM_API3_Exception * @throws \CiviCRM_API3_Exception
*/ */
public function getFinancialTypes() { public static function getFinancialTypes() {
$financial_types = array(); if (!isset(static::$_financialTypes)) {
$query = civicrm_api3('FinancialType', 'get', array( static::$_financialTypes = array();
'is_active' => 1, $query = civicrm_api3('FinancialType', 'get', array(
'option.limit' => 0, 'option.limit' => 0,
'return' => 'id,name' 'is_active' => 1,
)); 'return' => 'id,name'
foreach ($query['values'] as $type) { ));
$financial_types[$type['id']] = $type['name']; foreach ($query['values'] as $type) {
static::$_financialTypes[$type['id']] = $type['name'];
}
} }
return $financial_types; return static::$_financialTypes;
} }
/** /**
@ -434,17 +500,19 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
* *
* @throws \CiviCRM_API3_Exception * @throws \CiviCRM_API3_Exception
*/ */
public function getMembershipTypes() { public static function getMembershipTypes() {
$membership_types = array(); if (!isset(static::$_membershipTypes)) {
$query = civicrm_api3('MembershipType', 'get', array( static::$_membershipTypes = array();
'is_active' => 1, $query = civicrm_api3('MembershipType', 'get', array(
'option.limit' => 0, 'option.limit' => 0,
'return' => 'id,name' 'is_active' => 1,
)); 'return' => 'id,name'
foreach ($query['values'] as $type) { ));
$membership_types[$type['id']] = $type['name']; foreach ($query['values'] as $type) {
static::$_membershipTypes[$type['id']] = $type['name'];
}
} }
return $membership_types; return static::$_membershipTypes;
} }
/** /**
@ -455,21 +523,23 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
* *
* @throws \CiviCRM_API3_Exception * @throws \CiviCRM_API3_Exception
*/ */
public function getGenderOptions() { public static function getGenderOptions() {
$genders = array(); if (!isset(static::$_genderOptions)) {
$query = civicrm_api3('OptionValue', 'get', array( static::$_genderOptions = array();
'option_group_id' => 'gender', $query = civicrm_api3('OptionValue', 'get', array(
'is_active' => 1, 'option.limit' => 0,
'option.limit' => 0, 'option_group_id' => 'gender',
'return' => array( 'is_active' => 1,
'value', 'return' => array(
'label', 'value',
), 'label',
)); ),
foreach ($query['values'] as $gender) { ));
$genders[$gender['value']] = $gender['label']; foreach ($query['values'] as $gender) {
static::$_genderOptions[$gender['value']] = $gender['label'];
}
} }
return $genders; return static::$_genderOptions;
} }
/** /**
@ -479,33 +549,37 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
* *
* @throws \CiviCRM_API3_Exception * @throws \CiviCRM_API3_Exception
*/ */
public function getSepaCreditors() { public static function getSepaCreditors() {
$creditors = array(); if (!isset(static::$_sepaCreditors)) {
static::$_sepaCreditors = array();
if (CRM_Twingle_Submission::civiSepaEnabled()) { if (CRM_Twingle_Submission::civiSepaEnabled()) {
$result = civicrm_api3('SepaCreditor', 'get', array( $result = civicrm_api3('SepaCreditor', 'get', array(
'option.limit' => 0, 'option.limit' => 0,
)); ));
foreach ($result['values'] as $sepa_creditor) { foreach ($result['values'] as $sepa_creditor) {
$creditors[$sepa_creditor['id']] = $sepa_creditor['name']; static::$_sepaCreditors[$sepa_creditor['id']] = $sepa_creditor['name'];
}
} }
} }
return static::$_sepaCreditors;
return $creditors;
} }
/** /**
* Retrieves payment instruments present within the system as options for * Retrieves payment instruments present within the system as options for
* select form elements. * select form elements.
*
* @return array
*
* @throws \CiviCRM_API3_Exception
*/ */
public function getPaymentInstruments() { public static function getPaymentInstruments() {
if (!isset(self::$_paymentInstruments)) { if (!isset(self::$_paymentInstruments)) {
self::$_paymentInstruments = array(); self::$_paymentInstruments = array();
$query = civicrm_api3('OptionValue', 'get', array( $query = civicrm_api3('OptionValue', 'get', array(
'option.limit' => 0,
'option_group_id' => 'payment_instrument', 'option_group_id' => 'payment_instrument',
'is_active' => 1, 'is_active' => 1,
'option.limit' => 0, 'return' => 'value,label'
'return' => 'value,label'
)); ));
foreach ($query['values'] as $payment_instrument) { foreach ($query['values'] as $payment_instrument) {
// Do not include CiviSEPA payment instruments, but add a SEPA option if // Do not include CiviSEPA payment instruments, but add a SEPA option if
@ -542,79 +616,106 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
/** /**
* Retrieves active groups used as mailing lists within the system as options * Retrieves active groups used as mailing lists within the system as options
* for select form elements. * for select form elements.
*
* @return array
*
* @throws \CiviCRM_API3_Exception
*/ */
public function getNewsletterGroups() { public static function getNewsletterGroups() {
$groups = array(); if (!isset(static::$_newsletterGroups)) {
$group_types = civicrm_api3('OptionValue', 'get', array( static::$_newsletterGroups = array();
'option_group_id' => 'group_type', $group_types = civicrm_api3('OptionValue', 'get', array(
'name' => CRM_Twingle_Submission::GROUP_TYPE_NEWSLETTER, 'option.limit' => 0,
)); 'option_group_id' => 'group_type',
if ($group_types['count'] > 0) { 'name' => CRM_Twingle_Submission::GROUP_TYPE_NEWSLETTER,
$group_type = reset($group_types['values']);
$query = civicrm_api3('Group', 'get', array(
'is_active' => 1,
'group_type' => array('LIKE' => '%' . CRM_Utils_Array::implodePadded($group_type['value']) . '%'),
'option.limit' => 0,
'return' => 'id,name'
)); ));
foreach ($query['values'] as $group) { if ($group_types['count'] > 0) {
$groups[$group['id']] = $group['name']; $group_type = reset($group_types['values']);
$query = civicrm_api3('Group', 'get', array(
'is_active' => 1,
'group_type' => array('LIKE' => '%' . CRM_Utils_Array::implodePadded($group_type['value']) . '%'),
'option.limit' => 0,
'return' => 'id,name'
));
foreach ($query['values'] as $group) {
static::$_newsletterGroups[$group['id']] = $group['name'];
}
}
else {
static::$_newsletterGroups[''] = E::ts('No mailing lists available');
} }
} }
else { return static::$_newsletterGroups;
$groups[''] = E::ts('No mailing lists available');
}
return $groups;
} }
/** /**
* Retrieves active groups as options for select form elements. * Retrieves active groups as options for select form elements.
*
* @return array
*
* @throws \CiviCRM_API3_Exception
*/ */
public function getGroups() { public static function getGroups() {
$groups = array(); if (!isset(static::$_groups)) {
$query = civicrm_api3('Group', 'get', array( static::$_groups = array();
'is_active' => 1, $query = civicrm_api3('Group', 'get', array(
'option.limit' => 0, 'option.limit' => 0,
'return' => 'id,name' 'is_active' => 1,
)); 'return' => 'id,name'
foreach ($query['values'] as $group) { ));
$groups[$group['id']] = $group['name']; foreach ($query['values'] as $group) {
static::$_groups[$group['id']] = $group['name'];
}
} }
return $groups; return static::$_groups;
} }
/** /**
* Retrieves active groups used as postal mailing lists within the system as * Retrieves active groups used as postal mailing lists within the system as
* options for select form elements. * options for select form elements.
*
* @return array
*
* @throws \CiviCRM_API3_Exception
*/ */
public function getPostinfoGroups() { public static function getPostinfoGroups() {
return $this->getGroups(); return static::getGroups();
} }
/** /**
* Retrieves active groups used as donation receipt requester lists within the * Retrieves active groups used as donation receipt requester lists within the
* system as options for select form elements. * system as options for select form elements.
*
* @return array
*
* @throws \CiviCRM_API3_Exception
*/ */
public function getDonationReceiptGroups() { public static function getDonationReceiptGroups() {
return $this->getGroups(); return static::getGroups();
} }
/** /**
* Retrieves campaigns as options for select elements. * Retrieves campaigns as options for select elements.
*
* @return array
*
* @throws \CiviCRM_API3_Exception
*/ */
public function getCampaigns() { public static function getCampaigns() {
$campaigns = array(); if (!isset(static::$_campaigns)) {
$query = civicrm_api3('Campaign', 'get', array( static::$_campaigns = array();
'option.limit' => 0, $query = civicrm_api3('Campaign', 'get', array(
'return' => array( 'option.limit' => 0,
'id', 'return' => array(
'title', 'id',
) 'title',
)); )
foreach ($query['values'] as $campaign) { ));
$campaigns[$campaign['id']] = $campaign['title']; foreach ($query['values'] as $campaign) {
static::$_campaigns[$campaign['id']] = $campaign['title'];
}
} }
return $campaigns; return static::$_campaigns;
} }
} }

View file

@ -115,15 +115,16 @@ class CRM_Twingle_Profile {
* Retrieves an attribute of the profile. * Retrieves an attribute of the profile.
* *
* @param string $attribute_name * @param string $attribute_name
* @param mixed $default
* *
* @return mixed | NULL * @return mixed | NULL
*/ */
public function getAttribute($attribute_name) { public function getAttribute($attribute_name, $default = NULL) {
if (isset($this->data[$attribute_name])) { if (isset($this->data[$attribute_name])) {
return $this->data[$attribute_name]; return $this->data[$attribute_name];
} }
else { else {
return NULL; return $default;
} }
} }
@ -180,34 +181,27 @@ class CRM_Twingle_Profile {
* @return array * @return array
*/ */
public static function allowedAttributes() { public static function allowedAttributes() {
return array( return array_merge(
'selector', array(
'location_type_id', 'selector',
'location_type_id_organisation', 'location_type_id',
'financial_type_id', 'location_type_id_organisation',
'financial_type_id_recur', 'financial_type_id',
'pi_banktransfer', 'financial_type_id_recur',
'pi_debit_manual', 'sepa_creditor_id',
'pi_debit_automatic', 'gender_male',
'pi_creditcard', 'gender_female',
'pi_mobilephone_germany', 'gender_other',
'pi_paypal', 'newsletter_groups',
'pi_sofortueberweisung', 'postinfo_groups',
'pi_amazonpay', 'donation_receipt_groups',
'pi_paydirekt', 'campaign',
'pi_applepay', 'contribution_source',
'pi_googlepay', 'custom_field_mapping',
'sepa_creditor_id', 'membership_type_id',
'gender_male', ),
'gender_female', // Add payment methods.
'gender_other', array_keys(static::paymentInstruments())
'newsletter_groups',
'postinfo_groups',
'donation_receipt_groups',
'campaign',
'contribution_source',
'custom_field_mapping',
'membership_type_id',
); );
} }

View file

@ -27,6 +27,11 @@ class CRM_Twingle_Submission {
*/ */
const GROUP_TYPE_NEWSLETTER = 'Mailing List'; const GROUP_TYPE_NEWSLETTER = 'Mailing List';
/**
* The option value for the contribution type for completed contributions.
*/
const CONTRIBUTION_STATUS_COMPLETED = 'Completed';
/** /**
* The default ID of the "Employer of" relationship type. * The default ID of the "Employer of" relationship type.
*/ */

View file

@ -105,7 +105,7 @@ function civicrm_api3_twingle_donation_endrecurring($params) {
$contribution = civicrm_api3('ContributionRecur', 'create', array( $contribution = civicrm_api3('ContributionRecur', 'create', array(
'id' => $contribution['id'], 'id' => $contribution['id'],
'end_date' => $params['ended_at'], 'end_date' => $params['ended_at'],
'contribution_status_id' => 'Completed', 'contribution_status_id' => CRM_Twingle_Submission::CONTRIBUTION_STATUS_COMPLETED,
)); ));
} }

View file

@ -632,7 +632,7 @@ function civicrm_api3_twingle_donation_Submit($params) {
// Create contribution. // Create contribution.
$contribution_data += array( $contribution_data += array(
'contribution_status_id' => 'Completed', 'contribution_status_id' => CRM_Twingle_Submission::CONTRIBUTION_STATUS_COMPLETED,
'receive_date' => $params['confirmed_at'], 'receive_date' => $params['confirmed_at'],
); );
$contribution = civicrm_api3('Contribution', 'create', $contribution_data); $contribution = civicrm_api3('Contribution', 'create', $contribution_data);