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