Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-01-05-23-28-33
[civicrm-core.git] / CRM / Contact / Form / Edit / CommunicationPreferences.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * form helper class for an Communication Preferences object
38 */
39 class CRM_Contact_Form_Edit_CommunicationPreferences {
40
41 /**
42 * Greetings
43 * @var array
44 * @static
45 */
46 static $greetings = array();
47
48 /**
49 * Build the form object elements for Communication Preferences object
50 *
51 * @param CRM_Core_Form $form reference to the form object
52 *
53 * @return void
54 * @static
55 */
56 public static function buildQuickForm(&$form) {
57 // since the pcm - preferred comminication method is logically
58 // grouped hence we'll use groups of HTML_QuickForm
59
60
61 // checkboxes for DO NOT phone, email, mail
62 // we take labels from SelectValues
63 $privacy = $commPreff = $commPreference = array();
64 $privacyOptions = CRM_Core_SelectValues::privacy();
65
66 // we add is_opt_out as a separate checkbox below for display and help purposes so remove it here
67 unset($privacyOptions['is_opt_out']);
68
69 foreach ($privacyOptions as $name => $label) {
70 $privacy[] = $form->createElement('advcheckbox', $name, NULL, $label);
71 }
72 $form->addGroup($privacy, 'privacy', ts('Privacy'), '&nbsp;');
73
74 // preferred communication method
75 $comm = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method', array('loclize' => TRUE));
76 foreach ($comm as $value => $title) {
77 $commPreff[] = $form->createElement('advcheckbox', $value, NULL, $title);
78 }
79 $form->addGroup($commPreff, 'preferred_communication_method', ts('Preferred Method(s)'));
80
81 $form->addSelect('preferred_language');
82
83 if (!empty($privacyOptions)) {
84 $commPreference['privacy'] = $privacyOptions;
85 }
86 if (!empty($comm)) {
87 $commPreference['preferred_communication_method'] = $comm;
88 }
89
90 //using for display purpose.
91 $form->assign('commPreference', $commPreference);
92
93 $form->add('select', 'preferred_mail_format', ts('Email Format'), CRM_Core_SelectValues::pmf());
94 $form->add('checkbox', 'is_opt_out', ts('NO BULK EMAILS (User Opt Out)'));
95
96 $communicationStyleOptions = array();
97 $communicationStyle = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'communication_style_id', array('localize' => TRUE));
98 foreach ($communicationStyle as $key => $var) {
99 $communicationStyleOptions[$key] = $form->createElement('radio', NULL,
100 ts('Communication Style'), $var, $key,
101 array('id' => "civicrm_communication_style_{$var}_{$key}")
102 );
103 }
104 if (!empty($communicationStyleOptions)) {
105 $form->addGroup($communicationStyleOptions, 'communication_style_id', ts('Communication Style'));
106 }
107
108 //check contact type and build filter clause accordingly for greeting types, CRM-4575
109 $greetings = self::getGreetingFields($form->_contactType);
110
111 foreach ($greetings as $greeting => $fields) {
112 $filter = array(
113 'contact_type' => $form->_contactType,
114 'greeting_type' => $greeting,
115 );
116
117 //add addressee in Contact form
118 $greetingTokens = CRM_Core_PseudoConstant::greeting($filter);
119 if (!empty($greetingTokens)) {
120 $form->addElement('select', $fields['field'], $fields['label'],
121 array(
122 '' => ts('- select -')) + $greetingTokens
123 );
124 //custom addressee
125 $form->addElement('text', $fields['customField'], $fields['customLabel'],
126 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', $fields['customField']), $fields['js']
127 );
128 }
129 }
130 }
131
132 /**
133 * Global form rule
134 *
135 * @param array $fields the input form values
136 * @param array $files the uploaded files if any
137 * @param $self
138 *
139 * @return true if no errors, else array of errors
140 * @static
141 */
142 public static function formRule($fields, $files, $self) {
143 //CRM-4575
144
145 $greetings = self::getGreetingFields($self->_contactType);
146 foreach ($greetings as $greeting => $details) {
147 $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name');
148 if (CRM_Utils_Array::value($details['field'], $fields) == $customizedValue && empty($fields[$details['customField']])) {
149 $errors[$details['customField']] = ts('Custom %1 is a required field if %1 is of type Customized.',
150 array(1 => $details['label'])
151 );
152 }
153 }
154 return empty($errors) ? TRUE : $errors;
155 }
156
157 /**
158 * Set default values for the form. Note that in edit/view mode
159 * the default values are retrieved from the database
160 *
161 *
162 * @param CRM_Core_Form $form
163 * @param $defaults
164 *
165 * @return void
166 */
167 public static function setDefaultValues(&$form, &$defaults) {
168
169 if (!empty($defaults['preferred_language'])) {
170 $languages = CRM_Contact_BAO_Contact::buildOptions('preferred_language');
171 $defaults['preferred_language'] = CRM_Utils_Array::key($defaults['preferred_language'], $languages);
172 }
173
174 // CRM-7119: set preferred_language to default if unset
175 if (empty($defaults['preferred_language'])) {
176 $config = CRM_Core_Config::singleton();
177 $defaults['preferred_language'] = $config->lcMessages;
178 }
179
180 if (empty($defaults['communication_style_id'])) {
181 $defaults['communication_style_id'] = array_pop(CRM_Core_OptionGroup::values('communication_style', TRUE, NULL, NULL, 'AND is_default = 1'));
182 }
183
184 //set default from greeting types CRM-4575, CRM-9739
185 if ($form->_action & CRM_Core_Action::ADD) {
186 foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
187 if (empty($defaults[$greeting . '_id'])) {
188 if ($defaultGreetingTypeId =
189 CRM_Contact_BAO_Contact_Utils::defaultGreeting($form->_contactType, $greeting)
190 ) {
191 $defaults[$greeting . '_id'] = $defaultGreetingTypeId;
192 }
193 }
194 }
195 }
196 else {
197 foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
198 $name = "{$greeting}_display";
199 $form->assign($name, CRM_Utils_Array::value($name, $defaults));
200 }
201 }
202 }
203
204 /**
205 * set array of greeting fields
206 *
207 * @param $contactType
208 *
209 * @return void
210 */
211 public static function getGreetingFields($contactType) {
212 if (empty(self::$greetings[$contactType])) {
213 self::$greetings[$contactType] = array();
214
215 $js = array(
216 'onfocus' => "if (!this.value) { this.value='Dear ';} else return false",
217 'onblur' => "if ( this.value == 'Dear') { this.value='';} else return false",
218 );
219
220 self::$greetings[$contactType] = array(
221 'addressee' => array(
222 'field' => 'addressee_id',
223 'customField' => 'addressee_custom',
224 'label' => ts('Addressee'),
225 'customLabel' => ts('Custom Addressee'),
226 'js' => NULL,
227 ),
228 'email_greeting' => array(
229 'field' => 'email_greeting_id',
230 'customField' => 'email_greeting_custom',
231 'label' => ts('Email Greeting'),
232 'customLabel' => ts('Custom Email Greeting'),
233 'js' => $js,
234 ),
235 'postal_greeting' => array(
236 'field' => 'postal_greeting_id',
237 'customField' => 'postal_greeting_custom',
238 'label' => ts('Postal Greeting'),
239 'customLabel' => ts('Custom Postal Greeting'),
240 'js' => $js,
241 ),
242 );
243 }
244
245 return self::$greetings[$contactType];
246 }
247 }