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)) {
static::$_locationTypes = array();
$query = civicrm_api3('LocationType', 'get', array( $query = civicrm_api3('LocationType', 'get', array(
'option.limit' => 0,
'is_active' => 1, 'is_active' => 1,
)); ));
foreach ($query['values'] as $type) { foreach ($query['values'] as $type) {
$location_types[$type['id']] = $type['name']; static::$_locationTypes[$type['id']] = $type['name'];
} }
}
return $location_types; return static::$_locationTypes;
} }
/** /**
@ -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)) {
static::$_financialTypes = array();
$query = civicrm_api3('FinancialType', 'get', array( $query = civicrm_api3('FinancialType', 'get', array(
'is_active' => 1,
'option.limit' => 0, 'option.limit' => 0,
'is_active' => 1,
'return' => 'id,name' 'return' => 'id,name'
)); ));
foreach ($query['values'] as $type) { foreach ($query['values'] as $type) {
$financial_types[$type['id']] = $type['name']; 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)) {
static::$_membershipTypes = array();
$query = civicrm_api3('MembershipType', 'get', array( $query = civicrm_api3('MembershipType', 'get', array(
'is_active' => 1,
'option.limit' => 0, 'option.limit' => 0,
'is_active' => 1,
'return' => 'id,name' 'return' => 'id,name'
)); ));
foreach ($query['values'] as $type) { foreach ($query['values'] as $type) {
$membership_types[$type['id']] = $type['name']; 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)) {
static::$_genderOptions = array();
$query = civicrm_api3('OptionValue', 'get', array( $query = civicrm_api3('OptionValue', 'get', array(
'option.limit' => 0,
'option_group_id' => 'gender', 'option_group_id' => 'gender',
'is_active' => 1, 'is_active' => 1,
'option.limit' => 0,
'return' => array( 'return' => array(
'value', 'value',
'label', 'label',
), ),
)); ));
foreach ($query['values'] as $gender) { foreach ($query['values'] as $gender) {
$genders[$gender['value']] = $gender['label']; static::$_genderOptions[$gender['value']] = $gender['label'];
} }
return $genders; }
return static::$_genderOptions;
} }
/** /**
@ -479,32 +549,36 @@ 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 $creditors; return static::$_sepaCreditors;
} }
/** /**
* 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) {
@ -542,10 +616,16 @@ 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)) {
static::$_newsletterGroups = array();
$group_types = civicrm_api3('OptionValue', 'get', array( $group_types = civicrm_api3('OptionValue', 'get', array(
'option.limit' => 0,
'option_group_id' => 'group_type', 'option_group_id' => 'group_type',
'name' => CRM_Twingle_Submission::GROUP_TYPE_NEWSLETTER, 'name' => CRM_Twingle_Submission::GROUP_TYPE_NEWSLETTER,
)); ));
@ -558,52 +638,72 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
'return' => 'id,name' 'return' => 'id,name'
)); ));
foreach ($query['values'] as $group) { foreach ($query['values'] as $group) {
$groups[$group['id']] = $group['name']; static::$_newsletterGroups[$group['id']] = $group['name'];
} }
} }
else { else {
$groups[''] = E::ts('No mailing lists available'); static::$_newsletterGroups[''] = E::ts('No mailing lists available');
} }
return $groups; }
return static::$_newsletterGroups;
} }
/** /**
* 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)) {
static::$_groups = array();
$query = civicrm_api3('Group', 'get', array( $query = civicrm_api3('Group', 'get', array(
'is_active' => 1,
'option.limit' => 0, 'option.limit' => 0,
'is_active' => 1,
'return' => 'id,name' 'return' => 'id,name'
)); ));
foreach ($query['values'] as $group) { foreach ($query['values'] as $group) {
$groups[$group['id']] = $group['name']; 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)) {
static::$_campaigns = array();
$query = civicrm_api3('Campaign', 'get', array( $query = civicrm_api3('Campaign', 'get', array(
'option.limit' => 0, 'option.limit' => 0,
'return' => array( 'return' => array(
@ -612,9 +712,10 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
) )
)); ));
foreach ($query['values'] as $campaign) { foreach ($query['values'] as $campaign) {
$campaigns[$campaign['id']] = $campaign['title']; 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,23 +181,13 @@ class CRM_Twingle_Profile {
* @return array * @return array
*/ */
public static function allowedAttributes() { public static function allowedAttributes() {
return array( return array_merge(
array(
'selector', 'selector',
'location_type_id', 'location_type_id',
'location_type_id_organisation', 'location_type_id_organisation',
'financial_type_id', 'financial_type_id',
'financial_type_id_recur', 'financial_type_id_recur',
'pi_banktransfer',
'pi_debit_manual',
'pi_debit_automatic',
'pi_creditcard',
'pi_mobilephone_germany',
'pi_paypal',
'pi_sofortueberweisung',
'pi_amazonpay',
'pi_paydirekt',
'pi_applepay',
'pi_googlepay',
'sepa_creditor_id', 'sepa_creditor_id',
'gender_male', 'gender_male',
'gender_female', 'gender_female',
@ -208,6 +199,9 @@ class CRM_Twingle_Profile {
'contribution_source', 'contribution_source',
'custom_field_mapping', 'custom_field_mapping',
'membership_type_id', 'membership_type_id',
),
// Add payment methods.
array_keys(static::paymentInstruments())
); );
} }

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);