-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriceconditionfields.php
More file actions
164 lines (146 loc) · 5.08 KB
/
Copy pathpriceconditionfields.php
File metadata and controls
164 lines (146 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
require_once 'priceconditionfields.civix.php';
use CRM_Priceconditionfields_ExtensionUtil as E;
/**
* Implements hook_civicrm_config().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/
*/
function priceconditionfields_civicrm_config(&$config): void {
_priceconditionfields_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_install().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
*/
function priceconditionfields_civicrm_install(): void {
_priceconditionfields_civix_civicrm_install();
}
/**
* Implements hook_civicrm_enable().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
*/
function priceconditionfields_civicrm_enable(): void {
_priceconditionfields_civix_civicrm_enable();
}
/**
* Implements hook_civicrm_buildForm().
*/
function priceconditionfields_civicrm_buildForm($fname, &$form) {
if (in_array($fname, ['CRM_Price_Form_Field'])) {
if ($form->_action & CRM_Core_Action::UPDATE) {
$domainID = CRM_Core_Config::domainID();
$settings = Civi::settings($domainID);
// Add participant type element
$form->add('select', 'participant_type_condition', E::ts('Participant Type'), [
'' => E::ts('- Select -'),
'all_participants' => E::ts('All Participants'),
'primary_participant' => E::ts('Primary Participant Only'),
'additional_participant' => E::ts('Additional Participant Only'),
], FALSE, ['class' => 'crm-select2']);
$form->setDefaults(['participant_type_condition' => $settings->get('participant_type_condition_fid_' . $form->getVar('_fid'))]);
}
}
}
/**
* Implementation of hook_civicrm_postProcess()
*/
function priceconditionfields_civicrm_postProcess($class, &$form) {
if (in_array($class, ['CRM_Price_Form_Field'])) {
$domainID = CRM_Core_Config::domainID();
$settings = Civi::settings($domainID);
if ($form->getVar('_fid')) {
$id = $form->getVar('_fid');
}
if ($id) {
$participant_type_condition = $form->_submitValues['participant_type_condition'] ?? NULL;
$domainID = CRM_Core_Config::domainID();
$settings = Civi::settings($domainID);
$settings->set('participant_type_condition_fid_' . $id, $participant_type_condition);
}
}
}
/**
* Implements hook_civicrm_buildAmount().
*
* Filter price fields based on participant type during event registration
*/
function priceconditionfields_civicrm_buildAmount($pageType, &$form, &$amount) {
// Determine if this is an event registration form and what participant type we're dealing with
if ($pageType === 'event') {
_priceconditionfields_filter_price_fields($form, $amount);
}
}
/**
* Filter price fields based on current participant type
*/
function _priceconditionfields_filter_price_fields(&$form, &$amount) {
// Determine participant type for current registration
$participantType = _priceconditionfields_get_current_participant_type($form);
if (!$participantType) {
return;
}
// Iterate through price sets and filter fields
if (!empty($amount)) {
foreach ($amount as $fieldId => $priceFields) {
$conditionType = _priceconditionfields_get_price_field_type($fieldId);
//echo '<pre>$conditionType- ';print_r($conditionType);echo '</pre>';
// echo '<pre>$participantType -';print_r($participantType);echo '</pre>';
// Check if this field should be visible for current participant type
if (!_priceconditionfields_should_show_field($conditionType, $participantType)) {
unset($amount[$fieldId]);
}
}
}
}
/**
* Get participant type condition for a price field
*/
function _priceconditionfields_get_price_field_type($priceFieldId) {
$domainID = CRM_Core_Config::domainID();
$settings = Civi::settings($domainID);
$participantType = $settings->get('participant_type_condition_fid_' . $priceFieldId);
if ($participantType) {
if ($participantType === 'all_participants') {
return NULL;
}
return $participantType;
}
return NULL;
}
/**
* Determine if a price field should be shown based on participant type condition and current participant type
*/
function _priceconditionfields_should_show_field($conditionType, $currentParticipantType) {
// If no condition, show for all
if ($conditionType === 'all_participants' || empty($conditionType)) {
return TRUE;
}
// Match condition with current participant type
switch ($conditionType) {
case 'primary_participant':
return $currentParticipantType === 'primary';
case 'additional_participant':
return $currentParticipantType === 'additional';
default:
return TRUE;
}
}
/**
* Detect current participant type during event registration
*
* @return string|null - 'primary', 'additional', or NULL
*/
function _priceconditionfields_get_current_participant_type(&$form) {
// Check form context to determine participant type
$formName = get_class($form);
if ($formName == 'CRM_Event_Form_Registration_Register') {
return 'primary';
}
elseif ($formName == 'CRM_Event_Form_Registration_AdditionalParticipant') {
return 'additional';
}
return NULL;
}