3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
37 * form helper class for an Communication Preferences object
39 class CRM_Contact_Form_Edit_CommunicationPreferences
{
46 static $greetings = array();
49 * build the form elements for Communication Preferences object
51 * @param CRM_Core_Form $form reference to the form object
57 static function buildQuickForm(&$form) {
58 // since the pcm - preferred comminication method is logically
59 // grouped hence we'll use groups of HTML_QuickForm
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();
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']);
70 foreach ($privacyOptions as $name => $label) {
71 $privacy[] = $form->createElement('advcheckbox', $name, NULL, $label);
73 $form->addGroup($privacy, 'privacy', ts('Privacy'), ' ');
75 // preferred communication method
76 $comm = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'preferred_communication_method', array('loclize' => TRUE));
77 foreach ($comm as $value => $title) {
78 $commPreff[] = $form->createElement('advcheckbox', $value, NULL, $title);
80 $form->addGroup($commPreff, 'preferred_communication_method', ts('Preferred Method(s)'));
82 $form->add('select', 'preferred_language',
83 ts('Preferred Language'),
85 '' => ts('- select -')) +
86 CRM_Contact_BAO_Contact
::buildOptions('preferred_language')
89 if (!empty($privacyOptions)) {
90 $commPreference['privacy'] = $privacyOptions;
93 $commPreference['preferred_communication_method'] = $comm;
96 //using for display purpose.
97 $form->assign('commPreference', $commPreference);
99 $form->add('select', 'preferred_mail_format', ts('Email Format'), CRM_Core_SelectValues
::pmf());
100 $form->add('checkbox', 'is_opt_out', ts('NO BULK EMAILS (User Opt Out)'));
102 $communicationStyleOptions = array();
103 $communicationStyle = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'communication_style_id', array('localize' => TRUE));
104 foreach ($communicationStyle as $key => $var) {
105 $communicationStyleOptions[$key] = $form->createElement('radio', NULL,
106 ts('Communication Style'), $var, $key,
107 array('id' => "civicrm_communication_style_{$var}_{$key}")
110 if (!empty($communicationStyleOptions)) {
111 $form->addGroup($communicationStyleOptions, 'communication_style_id', ts('Communication Style'));
114 //check contact type and build filter clause accordingly for greeting types, CRM-4575
115 $greetings = self
::getGreetingFields($form->_contactType
);
117 foreach ($greetings as $greeting => $fields) {
119 'contact_type' => $form->_contactType
,
120 'greeting_type' => $greeting,
123 //add addressee in Contact form
124 $greetingTokens = CRM_Core_PseudoConstant
::greeting($filter);
125 if (!empty($greetingTokens)) {
126 $form->addElement('select', $fields['field'], $fields['label'],
128 '' => ts('- select -')) +
$greetingTokens
131 $form->addElement('text', $fields['customField'], $fields['customLabel'],
132 CRM_Core_DAO
::getAttribute('CRM_Contact_DAO_Contact', $fields['customField']), $fields['js']
141 * @param array $fields the input form values
142 * @param array $files the uploaded files if any
143 * @param array $options additional user data
145 * @return true if no errors, else array of errors
149 static function formRule($fields, $files, $self) {
152 $greetings = self
::getGreetingFields($self->_contactType
);
153 foreach ($greetings as $greeting => $details) {
154 $customizedValue = CRM_Core_OptionGroup
::getValue($greeting, 'Customized', 'name');
155 if (CRM_Utils_Array
::value($details['field'], $fields) == $customizedValue && empty($fields[$details['customField']])) {
156 $errors[$details['customField']] = ts('Custom %1 is a required field if %1 is of type Customized.',
157 array(1 => $details['label'])
161 return empty($errors) ?
TRUE : $errors;
165 * This function sets the default values for the form. Note that in edit/view mode
166 * the default values are retrieved from the database
172 static function setDefaultValues(&$form, &$defaults) {
174 if (!empty($defaults['preferred_language'])) {
175 $languages = CRM_Contact_BAO_Contact
::buildOptions('preferred_language');
176 $defaults['preferred_language'] = CRM_Utils_Array
::key($defaults['preferred_language'], $languages);
179 // CRM-7119: set preferred_language to default if unset
180 if (empty($defaults['preferred_language'])) {
181 $config = CRM_Core_Config
::singleton();
182 $defaults['preferred_language'] = $config->lcMessages
;
185 if (empty($defaults['communication_style_id'])) {
186 $defaults['communication_style_id'] = array_pop(CRM_Core_OptionGroup
::values('communication_style', TRUE, NULL, NULL, 'AND is_default = 1'));
189 //set default from greeting types CRM-4575, CRM-9739
190 if ($form->_action
& CRM_Core_Action
::ADD
) {
191 foreach (CRM_Contact_BAO_Contact
::$_greetingTypes as $greeting) {
192 if (empty($defaults[$greeting . '_id'])) {
193 if ($defaultGreetingTypeId =
194 CRM_Contact_BAO_Contact_Utils
::defaultGreeting($form->_contactType
, $greeting)
196 $defaults[$greeting . '_id'] = $defaultGreetingTypeId;
202 foreach (CRM_Contact_BAO_Contact
::$_greetingTypes as $greeting) {
203 $name = "{$greeting}_display";
204 $form->assign($name, CRM_Utils_Array
::value($name, $defaults));
210 * set array of greeting fields
215 static function getGreetingFields($contactType) {
216 if (empty(self
::$greetings[$contactType])) {
217 self
::$greetings[$contactType] = array();
220 'onfocus' => "if (!this.value) { this.value='Dear ';} else return false",
221 'onblur' => "if ( this.value == 'Dear') { this.value='';} else return false",
224 self
::$greetings[$contactType] = array(
225 'addressee' => array(
226 'field' => 'addressee_id',
227 'customField' => 'addressee_custom',
228 'label' => ts('Addressee'),
229 'customLabel' => ts('Custom Addressee'),
232 'email_greeting' => array(
233 'field' => 'email_greeting_id',
234 'customField' => 'email_greeting_custom',
235 'label' => ts('Email Greeting'),
236 'customLabel' => ts('Custom Email Greeting'),
239 'postal_greeting' => array(
240 'field' => 'postal_greeting_id',
241 'customField' => 'postal_greeting_custom',
242 'label' => ts('Postal Greeting'),
243 'customLabel' => ts('Custom Postal Greeting'),
249 return self
::$greetings[$contactType];