Merge pull request #11672 from omarabuhussein/CRM-21769-improve-unsupported-locale...
[civicrm-core.git] / CRM / Contact / Form / Edit / CommunicationPreferences.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2018
32 */
33
34 /**
35 * Form helper class for an Communication Preferences object.
36 */
37 class CRM_Contact_Form_Edit_CommunicationPreferences {
38
39 /**
40 * Greetings.
41 *
42 * @var array
43 */
44 static $greetings = array();
45
46 /**
47 * Build the form object elements for Communication Preferences object.
48 *
49 * @param CRM_Core_Form $form
50 * Reference to the form object.
51 */
52 public static function buildQuickForm(&$form) {
53 // since the pcm - preferred communication method is logically
54 // grouped hence we'll use groups of HTML_QuickForm
55
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 }
67 $form->addGroup($privacy, 'privacy', ts('Privacy'), '&nbsp;<br/>');
68
69 // preferred communication method
70 $comm = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method', array('loclize' => TRUE));
71 foreach ($comm as $value => $title) {
72 $commPreff[] = $form->createElement('advcheckbox', $value, NULL, $title);
73 }
74 $form->addField('preferred_communication_method', array('entity' => 'contact', 'type' => 'CheckBoxGroup'));
75 $form->addField('preferred_language', array('entity' => 'contact'));
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
87 $form->addField('preferred_mail_format', array('entity' => 'contact', 'label' => ts('Email Format')));
88
89 $form->addField('is_opt_out', array('entity' => 'contact', 'label' => ts('NO BULK EMAILS (User Opt Out)')));
90
91 $form->addField('communication_style_id', array('entity' => 'contact', 'type' => 'RadioGroup'));
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(
106 '' => ts('- select -'),
107 ) + $greetingTokens
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 /**
118 * Global form rule.
119 *
120 * @param array $fields
121 * The input form values.
122 * @param array $files
123 * The uploaded files if any.
124 * @param CRM_Contact_Form_Edit_CommunicationPreferences $self
125 *
126 * @return bool|array
127 * true if no errors, else array of errors
128 */
129 public static function formRule($fields, $files, $self) {
130 //CRM-4575
131
132 $greetings = self::getGreetingFields($self->_contactType);
133 foreach ($greetings as $greeting => $details) {
134 $customizedValue = CRM_Core_PseudoConstant::getKey('CRM_Contact_BAO_Contact', $details['field'], 'Customized');
135 if (CRM_Utils_Array::value($details['field'], $fields) == $customizedValue && empty($fields[$details['customField']])) {
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 }
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 }
145 return empty($errors) ? TRUE : $errors;
146 }
147
148 /**
149 * Set default values for the form.
150 *
151 * @param CRM_Core_Form $form
152 * @param array $defaults
153 */
154 public static function setDefaultValues(&$form, &$defaults) {
155
156 if (!empty($defaults['preferred_language'])) {
157 $languages = CRM_Contact_BAO_Contact::buildOptions('preferred_language');
158 $defaults['preferred_language'] = CRM_Utils_Array::key($defaults['preferred_language'], $languages);
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
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
171 // CRM-17778 -- set preferred_mail_format to default if unset
172 if (empty($defaults['preferred_mail_format'])) {
173 $defaults['preferred_mail_format'] = 'Both';
174 }
175 else {
176 $defaults['preferred_mail_format'] = array_search($defaults['preferred_mail_format'], CRM_Core_SelectValues::pmf());
177 }
178
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'])) {
183 if ($defaultGreetingTypeId = CRM_Contact_BAO_Contact_Utils::defaultGreeting($form->_contactType, $greeting)
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 /**
199 * Set array of greeting fields.
200 *
201 * @param string $contactType
202 */
203 public static function getGreetingFields($contactType) {
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 }
239
240 }