Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
c037736a | 19 | * Form helper class for an Communication Preferences object. |
6a488035 TO |
20 | */ |
21 | class CRM_Contact_Form_Edit_CommunicationPreferences { | |
22 | ||
23 | /** | |
c037736a | 24 | * Greetings. |
25 | * | |
6a488035 | 26 | * @var array |
6a488035 | 27 | */ |
69078420 | 28 | public static $greetings = []; |
6a488035 TO |
29 | |
30 | /** | |
fe482240 | 31 | * Build the form object elements for Communication Preferences object. |
6a488035 | 32 | * |
77c5b619 TO |
33 | * @param CRM_Core_Form $form |
34 | * Reference to the form object. | |
6a488035 | 35 | */ |
00be9182 | 36 | public static function buildQuickForm(&$form) { |
b44e3f84 | 37 | // since the pcm - preferred communication method is logically |
6a488035 TO |
38 | // grouped hence we'll use groups of HTML_QuickForm |
39 | ||
6a488035 TO |
40 | // checkboxes for DO NOT phone, email, mail |
41 | // we take labels from SelectValues | |
be2fb01f | 42 | $privacy = $commPreff = $commPreference = []; |
6a488035 TO |
43 | $privacyOptions = CRM_Core_SelectValues::privacy(); |
44 | ||
45 | // we add is_opt_out as a separate checkbox below for display and help purposes so remove it here | |
46 | unset($privacyOptions['is_opt_out']); | |
47 | ||
48 | foreach ($privacyOptions as $name => $label) { | |
49 | $privacy[] = $form->createElement('advcheckbox', $name, NULL, $label); | |
50 | } | |
dd4706ef | 51 | $form->addGroup($privacy, 'privacy', ts('Privacy'), ' <br/>'); |
6a488035 TO |
52 | |
53 | // preferred communication method | |
406f3cd7 | 54 | $comm = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method', ['localize' => TRUE]); |
6a488035 TO |
55 | foreach ($comm as $value => $title) { |
56 | $commPreff[] = $form->createElement('advcheckbox', $value, NULL, $title); | |
57 | } | |
be2fb01f CW |
58 | $form->addField('preferred_communication_method', ['entity' => 'contact', 'type' => 'CheckBoxGroup']); |
59 | $form->addField('preferred_language', ['entity' => 'contact']); | |
6a488035 TO |
60 | |
61 | if (!empty($privacyOptions)) { | |
62 | $commPreference['privacy'] = $privacyOptions; | |
63 | } | |
64 | if (!empty($comm)) { | |
65 | $commPreference['preferred_communication_method'] = $comm; | |
66 | } | |
67 | ||
68 | //using for display purpose. | |
69 | $form->assign('commPreference', $commPreference); | |
70 | ||
be2fb01f | 71 | $form->addField('preferred_mail_format', ['entity' => 'contact', 'label' => ts('Email Format')]); |
6a488035 | 72 | |
be2fb01f | 73 | $form->addField('is_opt_out', ['entity' => 'contact', 'label' => ts('NO BULK EMAILS (User Opt Out)')]); |
aa62b355 | 74 | |
be2fb01f | 75 | $form->addField('communication_style_id', ['entity' => 'contact', 'type' => 'RadioGroup']); |
6a488035 TO |
76 | //check contact type and build filter clause accordingly for greeting types, CRM-4575 |
77 | $greetings = self::getGreetingFields($form->_contactType); | |
78 | ||
79 | foreach ($greetings as $greeting => $fields) { | |
be2fb01f | 80 | $filter = [ |
6a488035 TO |
81 | 'contact_type' => $form->_contactType, |
82 | 'greeting_type' => $greeting, | |
be2fb01f | 83 | ]; |
6a488035 TO |
84 | |
85 | //add addressee in Contact form | |
86 | $greetingTokens = CRM_Core_PseudoConstant::greeting($filter); | |
87 | if (!empty($greetingTokens)) { | |
88 | $form->addElement('select', $fields['field'], $fields['label'], | |
be2fb01f | 89 | [ |
389bcebf | 90 | '' => ts('- select -'), |
be2fb01f | 91 | ] + $greetingTokens |
6a488035 TO |
92 | ); |
93 | //custom addressee | |
94 | $form->addElement('text', $fields['customField'], $fields['customLabel'], | |
95 | CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', $fields['customField']), $fields['js'] | |
96 | ); | |
97 | } | |
98 | } | |
99 | } | |
100 | ||
101 | /** | |
fe482240 | 102 | * Global form rule. |
6a488035 | 103 | * |
77c5b619 TO |
104 | * @param array $fields |
105 | * The input form values. | |
106 | * @param array $files | |
107 | * The uploaded files if any. | |
c037736a | 108 | * @param CRM_Contact_Form_Edit_CommunicationPreferences $self |
77b97be7 | 109 | * |
72b3a70c CW |
110 | * @return bool|array |
111 | * true if no errors, else array of errors | |
6a488035 | 112 | */ |
00be9182 | 113 | public static function formRule($fields, $files, $self) { |
6a488035 TO |
114 | //CRM-4575 |
115 | ||
116 | $greetings = self::getGreetingFields($self->_contactType); | |
117 | foreach ($greetings as $greeting => $details) { | |
f4a9693e | 118 | $customizedValue = CRM_Core_PseudoConstant::getKey('CRM_Contact_BAO_Contact', $details['field'], 'Customized'); |
8cc574cf | 119 | if (CRM_Utils_Array::value($details['field'], $fields) == $customizedValue && empty($fields[$details['customField']])) { |
6a488035 | 120 | $errors[$details['customField']] = ts('Custom %1 is a required field if %1 is of type Customized.', |
be2fb01f | 121 | [1 => $details['label']] |
6a488035 TO |
122 | ); |
123 | } | |
124 | } | |
765eaf0c | 125 | |
126 | if (array_key_exists('preferred_mail_format', $fields) && empty($fields['preferred_mail_format'])) { | |
127 | $errors['preferred_mail_format'] = ts('Please select an email format preferred by this contact.'); | |
128 | } | |
6a488035 TO |
129 | return empty($errors) ? TRUE : $errors; |
130 | } | |
131 | ||
132 | /** | |
c037736a | 133 | * Set default values for the form. |
6a488035 | 134 | * |
c490a46a | 135 | * @param CRM_Core_Form $form |
c037736a | 136 | * @param array $defaults |
6a488035 | 137 | */ |
00be9182 | 138 | public static function setDefaultValues(&$form, &$defaults) { |
6a488035 TO |
139 | |
140 | if (!empty($defaults['preferred_language'])) { | |
41a03191 | 141 | $languages = CRM_Contact_BAO_Contact::buildOptions('preferred_language'); |
142 | $defaults['preferred_language'] = CRM_Utils_Array::key($defaults['preferred_language'], $languages); | |
6a488035 TO |
143 | } |
144 | ||
145 | // CRM-7119: set preferred_language to default if unset | |
146 | if (empty($defaults['preferred_language'])) { | |
147 | $config = CRM_Core_Config::singleton(); | |
148 | $defaults['preferred_language'] = $config->lcMessages; | |
149 | } | |
150 | ||
aa62b355 OB |
151 | if (empty($defaults['communication_style_id'])) { |
152 | $defaults['communication_style_id'] = array_pop(CRM_Core_OptionGroup::values('communication_style', TRUE, NULL, NULL, 'AND is_default = 1')); | |
153 | } | |
154 | ||
cfb54913 | 155 | // CRM-17778 -- set preferred_mail_format to default if unset |
156 | if (empty($defaults['preferred_mail_format'])) { | |
2515d7ff | 157 | $defaults['preferred_mail_format'] = 'Both'; |
cfb54913 | 158 | } |
33997bc3 | 159 | else { |
160 | $defaults['preferred_mail_format'] = array_search($defaults['preferred_mail_format'], CRM_Core_SelectValues::pmf()); | |
161 | } | |
cfb54913 | 162 | |
6a488035 TO |
163 | //set default from greeting types CRM-4575, CRM-9739 |
164 | if ($form->_action & CRM_Core_Action::ADD) { | |
165 | foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) { | |
166 | if (empty($defaults[$greeting . '_id'])) { | |
389bcebf | 167 | if ($defaultGreetingTypeId = CRM_Contact_BAO_Contact_Utils::defaultGreeting($form->_contactType, $greeting) |
6a488035 TO |
168 | ) { |
169 | $defaults[$greeting . '_id'] = $defaultGreetingTypeId; | |
170 | } | |
171 | } | |
172 | } | |
173 | } | |
174 | else { | |
175 | foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) { | |
176 | $name = "{$greeting}_display"; | |
177 | $form->assign($name, CRM_Utils_Array::value($name, $defaults)); | |
178 | } | |
179 | } | |
180 | } | |
181 | ||
182 | /** | |
c037736a | 183 | * Set array of greeting fields. |
77b97be7 | 184 | * |
c037736a | 185 | * @param string $contactType |
6a488035 | 186 | */ |
00be9182 | 187 | public static function getGreetingFields($contactType) { |
6a488035 | 188 | if (empty(self::$greetings[$contactType])) { |
be2fb01f | 189 | self::$greetings[$contactType] = []; |
6a488035 | 190 | |
be2fb01f | 191 | $js = [ |
6a488035 TO |
192 | 'onfocus' => "if (!this.value) { this.value='Dear ';} else return false", |
193 | 'onblur' => "if ( this.value == 'Dear') { this.value='';} else return false", | |
be2fb01f | 194 | ]; |
6a488035 | 195 | |
be2fb01f CW |
196 | self::$greetings[$contactType] = [ |
197 | 'addressee' => [ | |
6a488035 TO |
198 | 'field' => 'addressee_id', |
199 | 'customField' => 'addressee_custom', | |
200 | 'label' => ts('Addressee'), | |
201 | 'customLabel' => ts('Custom Addressee'), | |
202 | 'js' => NULL, | |
be2fb01f CW |
203 | ], |
204 | 'email_greeting' => [ | |
6a488035 TO |
205 | 'field' => 'email_greeting_id', |
206 | 'customField' => 'email_greeting_custom', | |
207 | 'label' => ts('Email Greeting'), | |
208 | 'customLabel' => ts('Custom Email Greeting'), | |
209 | 'js' => $js, | |
be2fb01f CW |
210 | ], |
211 | 'postal_greeting' => [ | |
6a488035 TO |
212 | 'field' => 'postal_greeting_id', |
213 | 'customField' => 'postal_greeting_custom', | |
214 | 'label' => ts('Postal Greeting'), | |
215 | 'customLabel' => ts('Custom Postal Greeting'), | |
216 | 'js' => $js, | |
be2fb01f CW |
217 | ], |
218 | ]; | |
6a488035 TO |
219 | } |
220 | ||
221 | return self::$greetings[$contactType]; | |
222 | } | |
96025800 | 223 | |
6a488035 | 224 | } |