Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate Twingle Shop #69

Merged
merged 14 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion CRM/Twingle/Form/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ public function buildQuickForm(): void {

// Assign template variables.
$this->assign('op', $this->_op);
$this->assign('twingle_use_shop', (int) Civi::settings()->get('twingle_use_shop'));
$this->assign('profile_name', $profile_name);
$this->assign('is_default', $is_default);

Expand Down Expand Up @@ -354,6 +355,10 @@ public function buildQuickForm(): void {
static::getPrefixOptions()
);

// Add script and css for Twingle Shop integration
Civi::resources()->addScriptUrl(E::url('js/twingle_shop.js'));
Civi::resources()->addStyleFile(E::LONG_NAME, 'css/twingle_shop.css');

$payment_instruments = CRM_Twingle_Profile::paymentInstruments();
$this->assign('payment_instruments', $payment_instruments);
foreach ($payment_instruments as $pi_name => $pi_label) {
Expand Down Expand Up @@ -523,6 +528,42 @@ public function buildQuickForm(): void {
['class' => 'crm-select2 huge', 'multiple' => 'multiple']
);

if (Civi::settings()->get('twingle_use_shop')) {
$this->add(
'checkbox', // field type
'enable_shop_integration', // field name
E::ts('Enable Shop Integration'), // field label
FALSE,
[]
);

$this->add(
'select', // field type
'shop_financial_type', // field name
E::ts('Default Financial Type'), // field label
static::getFinancialTypes(), // list of options
TRUE,
['class' => 'crm-select2 huge']
);

$this->add(
'select', // field type
'shop_donation_financial_type', // field name
E::ts('Financial Type for top up donations'), // field label
static::getFinancialTypes(), // list of options
TRUE,
['class' => 'crm-select2 huge']
);

$this->add(
'checkbox', // field type
'shop_map_products', // field name
E::ts('Map Products as Price Fields'), // field label
FALSE, // is not required
[]
);
}

$this->addButtons([
[
'type' => 'submit',
Expand Down Expand Up @@ -990,5 +1031,4 @@ public static function getCampaigns() {
}
return static::$_campaigns;
}

}
55 changes: 38 additions & 17 deletions CRM/Twingle/Form/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ class CRM_Twingle_Form_Settings extends CRM_Core_Form {
* List of all settings options.
*/
public static $SETTINGS_LIST = [
'twingle_prefix',
'twingle_use_sepa',
'twingle_dont_use_reference',
'twingle_protect_recurring',
'twingle_protect_recurring_activity_type',
'twingle_protect_recurring_activity_subject',
'twingle_protect_recurring_activity_status',
'twingle_protect_recurring_activity_assignee',
'twingle_prefix',
'twingle_use_sepa',
'twingle_dont_use_reference',
'twingle_protect_recurring',
'twingle_protect_recurring_activity_type',
'twingle_protect_recurring_activity_subject',
'twingle_protect_recurring_activity_status',
'twingle_protect_recurring_activity_assignee',
'twingle_use_shop',
'twingle_access_key',
];

/**
Expand Down Expand Up @@ -105,13 +107,25 @@ public function buildQuickForm(): void {
]
);

$this->addButtons([
[
'type' => 'submit',
'name' => E::ts('Save'),
'isDefault' => TRUE,
],
]);
$this->add(
'checkbox',
'twingle_use_shop',
E::ts("Use Twingle Shop Integration")
);

$this->add(
'text',
'twingle_access_key',
E::ts("Twingle Access Key")
);

$this->addButtons(array(
array (
'type' => 'submit',
'name' => E::ts('Save'),
'isDefault' => TRUE,
)
));

// set defaults
foreach (self::$SETTINGS_LIST as $setting) {
Expand All @@ -124,8 +138,7 @@ public function buildQuickForm(): void {
}

/**
* Custom form validation, because the activity creation fields
* are only mandatory if activity creation is active
* Custom form validation, as some fields are mandatory only when others are active.
* @return bool
*/
public function validate() {
Expand All @@ -146,6 +159,14 @@ public function validate() {
}
}

// Twingle Access Key is required if Shop Integration is enabled
if (
CRM_Utils_Array::value('twingle_use_shop', $this->_submitValues) &&
!CRM_Utils_Array::value('twingle_access_key', $this->_submitValues, FALSE)
) {
$this->_errors['twingle_access_key'] = E::ts("An Access Key is required to enable Twingle Shop Integration");
}

return (0 == count($this->_errors));
}

Expand Down
1 change: 1 addition & 0 deletions CRM/Twingle/Page/Profiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function run():void {
}
$this->assign('profiles', $profiles);
$this->assign('profile_stats', CRM_Twingle_Profile::getProfileStats());
$this->assign('twingle_use_shop', (int) Civi::settings()->get('twingle_use_shop'));

// Add custom css
Civi::resources()->addStyleFile(E::LONG_NAME, 'css/twingle.css');
Expand Down
31 changes: 29 additions & 2 deletions CRM/Twingle/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ class CRM_Twingle_Profile {
*/
protected $data;

/**
* @var array $check_box_fields
* List of check box fields
*/
public $check_box_fields = [
'newsletter_double_opt_in',
'enable_shop_integration',
'shop_map_products',
];

/**
* CRM_Twingle_Profile constructor.
*
Expand Down Expand Up @@ -197,6 +207,16 @@ function($project_id) {
);
}

/**
* Determine if Twingle Shop integration is enabled in general and
* specifically for this profile.
* @return bool
*/
public function isShopEnabled(): bool {
return Civi::settings()->get('twingle_use_shop') &&
$this->data['enable_shop_integration'];
}

/**
* Retrieves an attribute of the profile.
*
Expand Down Expand Up @@ -532,6 +552,10 @@ public static function allowedAttributes(bool $asMetadata = FALSE) {
'required_address_components' => ['required' => FALSE],
'map_as_contribution_notes' => ['required' => FALSE],
'map_as_contact_notes' => ['required' => FALSE],
'enable_shop_integration' => ['required' => FALSE],
'shop_financial_type' => ['required' => FALSE],
'shop_donation_financial_type' => ['required' => FALSE],
'shop_map_products' => ['required' => FALSE],
],
// Add payment methods.
array_combine(
Expand Down Expand Up @@ -650,6 +674,10 @@ public static function createDefaultProfile($name = 'default') {
],
'map_as_contribution_notes' => [],
'map_as_contact_notes' => [],
'enable_shop_integration' => FALSE,
'shop_financial_type' => 1,
'shop_donation_financial_type' => 1,
'shop_map_products' => FALSE,
]
// Add contribution status for all payment methods.
// phpcs:ignore Drupal.Formatting.SpaceUnaryOperator.PlusMinus
Expand All @@ -666,7 +694,7 @@ public static function createDefaultProfile($name = 'default') {
* @param string $project_id
*
* @return CRM_Twingle_Profile
* @throws \CRM_Twingle_Exceptions_ProfileException
* @throws \Civi\Twingle\Exceptions\ProfileException
* @throws \Civi\Core\Exception\DBQueryException
*/
public static function getProfileForProject($project_id) {
Expand Down Expand Up @@ -779,5 +807,4 @@ public static function getProfileStats() {
}
return $stats;
}

}
Loading
Loading