convert nickname field
[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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
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 43 * @var array
6a488035
TO
44 */
45 static $greetings = array();
46
47 /**
fe482240 48 * Build the form object elements for Communication Preferences object.
6a488035 49 *
77c5b619
TO
50 * @param CRM_Core_Form $form
51 * Reference to the form object.
6a488035
TO
52 *
53 * @return void
6a488035 54 */
00be9182 55 public static function buildQuickForm(&$form) {
6a488035
TO
56 // since the pcm - preferred comminication method is logically
57 // grouped hence we'll use groups of HTML_QuickForm
58
6a488035
TO
59 // checkboxes for DO NOT phone, email, mail
60 // we take labels from SelectValues
61 $privacy = $commPreff = $commPreference = array();
62 $privacyOptions = CRM_Core_SelectValues::privacy();
63
64 // we add is_opt_out as a separate checkbox below for display and help purposes so remove it here
65 unset($privacyOptions['is_opt_out']);
66
67 foreach ($privacyOptions as $name => $label) {
68 $privacy[] = $form->createElement('advcheckbox', $name, NULL, $label);
69 }
70 $form->addGroup($privacy, 'privacy', ts('Privacy'), '&nbsp;');
71
72 // preferred communication method
e7e657f0 73 $comm = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method', array('loclize' => TRUE));
6a488035
TO
74 foreach ($comm as $value => $title) {
75 $commPreff[] = $form->createElement('advcheckbox', $value, NULL, $title);
76 }
77 $form->addGroup($commPreff, 'preferred_communication_method', ts('Preferred Method(s)'));
78
d825b696 79 $form->addSelect('preferred_language');
6a488035
TO
80
81 if (!empty($privacyOptions)) {
82 $commPreference['privacy'] = $privacyOptions;
83 }
84 if (!empty($comm)) {
85 $commPreference['preferred_communication_method'] = $comm;
86 }
87
88 //using for display purpose.
89 $form->assign('commPreference', $commPreference);
90
91 $form->add('select', 'preferred_mail_format', ts('Email Format'), CRM_Core_SelectValues::pmf());
92 $form->add('checkbox', 'is_opt_out', ts('NO BULK EMAILS (User Opt Out)'));
93
aa62b355
OB
94 $communicationStyleOptions = array();
95 $communicationStyle = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'communication_style_id', array('localize' => TRUE));
96 foreach ($communicationStyle as $key => $var) {
97 $communicationStyleOptions[$key] = $form->createElement('radio', NULL,
98 ts('Communication Style'), $var, $key,
99 array('id' => "civicrm_communication_style_{$var}_{$key}")
100 );
101 }
102 if (!empty($communicationStyleOptions)) {
103 $form->addGroup($communicationStyleOptions, 'communication_style_id', ts('Communication Style'));
104 }
105
6a488035
TO
106 //check contact type and build filter clause accordingly for greeting types, CRM-4575
107 $greetings = self::getGreetingFields($form->_contactType);
108
109 foreach ($greetings as $greeting => $fields) {
110 $filter = array(
111 'contact_type' => $form->_contactType,
112 'greeting_type' => $greeting,
113 );
114
115 //add addressee in Contact form
116 $greetingTokens = CRM_Core_PseudoConstant::greeting($filter);
117 if (!empty($greetingTokens)) {
118 $form->addElement('select', $fields['field'], $fields['label'],
119 array(
389bcebf 120 '' => ts('- select -'),
353ffa53 121 ) + $greetingTokens
6a488035
TO
122 );
123 //custom addressee
124 $form->addElement('text', $fields['customField'], $fields['customLabel'],
125 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', $fields['customField']), $fields['js']
126 );
127 }
128 }
129 }
130
131 /**
fe482240 132 * Global form rule.
6a488035 133 *
77c5b619
TO
134 * @param array $fields
135 * The input form values.
136 * @param array $files
137 * The uploaded files if any.
77b97be7
EM
138 * @param $self
139 *
72b3a70c
CW
140 * @return bool|array
141 * true if no errors, else array of errors
6a488035 142 */
00be9182 143 public static function formRule($fields, $files, $self) {
6a488035
TO
144 //CRM-4575
145
146 $greetings = self::getGreetingFields($self->_contactType);
147 foreach ($greetings as $greeting => $details) {
148 $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name');
8cc574cf 149 if (CRM_Utils_Array::value($details['field'], $fields) == $customizedValue && empty($fields[$details['customField']])) {
6a488035
TO
150 $errors[$details['customField']] = ts('Custom %1 is a required field if %1 is of type Customized.',
151 array(1 => $details['label'])
152 );
153 }
154 }
155 return empty($errors) ? TRUE : $errors;
156 }
157
158 /**
c490a46a 159 * Set default values for the form. Note that in edit/view mode
6a488035
TO
160 * the default values are retrieved from the database
161 *
6a488035 162 *
c490a46a 163 * @param CRM_Core_Form $form
77b97be7
EM
164 * @param $defaults
165 *
355ba699 166 * @return void
6a488035 167 */
00be9182 168 public static function setDefaultValues(&$form, &$defaults) {
6a488035
TO
169
170 if (!empty($defaults['preferred_language'])) {
41a03191 171 $languages = CRM_Contact_BAO_Contact::buildOptions('preferred_language');
172 $defaults['preferred_language'] = CRM_Utils_Array::key($defaults['preferred_language'], $languages);
6a488035
TO
173 }
174
175 // CRM-7119: set preferred_language to default if unset
176 if (empty($defaults['preferred_language'])) {
177 $config = CRM_Core_Config::singleton();
178 $defaults['preferred_language'] = $config->lcMessages;
179 }
180
aa62b355
OB
181 if (empty($defaults['communication_style_id'])) {
182 $defaults['communication_style_id'] = array_pop(CRM_Core_OptionGroup::values('communication_style', TRUE, NULL, NULL, 'AND is_default = 1'));
183 }
184
6a488035
TO
185 //set default from greeting types CRM-4575, CRM-9739
186 if ($form->_action & CRM_Core_Action::ADD) {
187 foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
188 if (empty($defaults[$greeting . '_id'])) {
389bcebf 189 if ($defaultGreetingTypeId = CRM_Contact_BAO_Contact_Utils::defaultGreeting($form->_contactType, $greeting)
6a488035
TO
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 /**
fe482240 205 * set array of greeting fields.
6a488035 206 *
77b97be7
EM
207 * @param $contactType
208 *
355ba699 209 * @return void
6a488035 210 */
00be9182 211 public static function getGreetingFields($contactType) {
6a488035
TO
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 }
96025800 247
6a488035 248}