3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2017
35 * Form helper class for an Communication Preferences object.
37 class CRM_Contact_Form_Edit_CommunicationPreferences
{
44 static $greetings = array();
47 * Build the form object elements for Communication Preferences object.
49 * @param CRM_Core_Form $form
50 * Reference to the form object.
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
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();
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']);
64 foreach ($privacyOptions as $name => $label) {
65 $privacy[] = $form->createElement('advcheckbox', $name, NULL, $label);
67 $form->addGroup($privacy, 'privacy', ts('Privacy'), ' <br/>');
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);
74 $form->addField('preferred_communication_method', array('entity' => 'contact', 'type' => 'CheckBoxGroup'));
75 $form->addField('preferred_language', array('entity' => 'contact'));
77 if (!empty($privacyOptions)) {
78 $commPreference['privacy'] = $privacyOptions;
81 $commPreference['preferred_communication_method'] = $comm;
84 //using for display purpose.
85 $form->assign('commPreference', $commPreference);
87 $form->addField('preferred_mail_format', array('entity' => 'contact', 'label' => ts('Email Format')));
89 $form->addField('is_opt_out', array('entity' => 'contact', 'label' => ts('NO BULK EMAILS (User Opt Out)')));
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
);
95 foreach ($greetings as $greeting => $fields) {
97 'contact_type' => $form->_contactType
,
98 'greeting_type' => $greeting,
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'],
106 '' => ts('- select -'),
110 $form->addElement('text', $fields['customField'], $fields['customLabel'],
111 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact', $fields['customField']), $fields['js']
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
127 * true if no errors, else array of errors
129 public static function formRule($fields, $files, $self) {
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'])
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.');
145 return empty($errors) ?
TRUE : $errors;
149 * Set default values for the form.
151 * @param CRM_Core_Form $form
152 * @param array $defaults
154 public static function setDefaultValues(&$form, &$defaults) {
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);
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
;
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'));
171 // CRM-17778 -- set preferred_mail_format to default if unset
172 if (empty($defaults['preferred_mail_format'])) {
173 $defaults['preferred_mail_format'] = 'Both';
176 $defaults['preferred_mail_format'] = array_search($defaults['preferred_mail_format'], CRM_Core_SelectValues
::pmf());
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)
185 $defaults[$greeting . '_id'] = $defaultGreetingTypeId;
191 foreach (CRM_Contact_BAO_Contact
::$_greetingTypes as $greeting) {
192 $name = "{$greeting}_display";
193 $form->assign($name, CRM_Utils_Array
::value($name, $defaults));
199 * Set array of greeting fields.
201 * @param string $contactType
203 public static function getGreetingFields($contactType) {
204 if (empty(self
::$greetings[$contactType])) {
205 self
::$greetings[$contactType] = array();
208 'onfocus' => "if (!this.value) { this.value='Dear ';} else return false",
209 'onblur' => "if ( this.value == 'Dear') { this.value='';} else return false",
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'),
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'),
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'),
237 return self
::$greetings[$contactType];