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