Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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-2013 | |
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 elements for Communication Preferences object | |
50 | * | |
51 | * @param CRM_Core_Form $form reference to the form object | |
52 | * | |
53 | * @return void | |
54 | * @access public | |
55 | * @static | |
56 | */ | |
57 | static function buildQuickForm(&$form) { | |
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'), ' '); | |
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 | ||
82 | $form->add('select', 'preferred_language', | |
83 | ts('Preferred Language'), | |
84 | array( | |
85 | '' => ts('- select -')) + | |
c0c9cd82 | 86 | CRM_Contact_BAO_Contact::buildOptions('preferred_language') |
6a488035 TO |
87 | ); |
88 | ||
89 | if (!empty($privacyOptions)) { | |
90 | $commPreference['privacy'] = $privacyOptions; | |
91 | } | |
92 | if (!empty($comm)) { | |
93 | $commPreference['preferred_communication_method'] = $comm; | |
94 | } | |
95 | ||
96 | //using for display purpose. | |
97 | $form->assign('commPreference', $commPreference); | |
98 | ||
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)')); | |
101 | ||
102 | //check contact type and build filter clause accordingly for greeting types, CRM-4575 | |
103 | $greetings = self::getGreetingFields($form->_contactType); | |
104 | ||
105 | foreach ($greetings as $greeting => $fields) { | |
106 | $filter = array( | |
107 | 'contact_type' => $form->_contactType, | |
108 | 'greeting_type' => $greeting, | |
109 | ); | |
110 | ||
111 | //add addressee in Contact form | |
112 | $greetingTokens = CRM_Core_PseudoConstant::greeting($filter); | |
113 | if (!empty($greetingTokens)) { | |
114 | $form->addElement('select', $fields['field'], $fields['label'], | |
115 | array( | |
116 | '' => ts('- select -')) + $greetingTokens | |
117 | ); | |
118 | //custom addressee | |
119 | $form->addElement('text', $fields['customField'], $fields['customLabel'], | |
120 | CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', $fields['customField']), $fields['js'] | |
121 | ); | |
122 | } | |
123 | } | |
124 | } | |
125 | ||
126 | /** | |
127 | * global form rule | |
128 | * | |
129 | * @param array $fields the input form values | |
130 | * @param array $files the uploaded files if any | |
131 | * @param array $options additional user data | |
132 | * | |
133 | * @return true if no errors, else array of errors | |
134 | * @access public | |
135 | * @static | |
136 | */ | |
137 | static function formRule($fields, $files, $self) { | |
138 | //CRM-4575 | |
139 | ||
140 | $greetings = self::getGreetingFields($self->_contactType); | |
141 | foreach ($greetings as $greeting => $details) { | |
142 | $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name'); | |
143 | if (CRM_Utils_Array::value($details['field'], $fields) == $customizedValue | |
144 | && !CRM_Utils_Array::value($details['customField'], $fields) | |
145 | ) { | |
146 | $errors[$details['customField']] = ts('Custom %1 is a required field if %1 is of type Customized.', | |
147 | array(1 => $details['label']) | |
148 | ); | |
149 | } | |
150 | } | |
151 | return empty($errors) ? TRUE : $errors; | |
152 | } | |
153 | ||
154 | /** | |
155 | * This function sets the default values for the form. Note that in edit/view mode | |
156 | * the default values are retrieved from the database | |
157 | * | |
158 | * @access public | |
159 | * | |
160 | * @return None | |
161 | */ | |
162 | static function setDefaultValues(&$form, &$defaults) { | |
163 | ||
164 | if (!empty($defaults['preferred_language'])) { | |
41a03191 | 165 | $languages = CRM_Contact_BAO_Contact::buildOptions('preferred_language'); |
166 | $defaults['preferred_language'] = CRM_Utils_Array::key($defaults['preferred_language'], $languages); | |
6a488035 TO |
167 | } |
168 | ||
169 | // CRM-7119: set preferred_language to default if unset | |
170 | if (empty($defaults['preferred_language'])) { | |
171 | $config = CRM_Core_Config::singleton(); | |
172 | $defaults['preferred_language'] = $config->lcMessages; | |
173 | } | |
174 | ||
175 | //set default from greeting types CRM-4575, CRM-9739 | |
176 | if ($form->_action & CRM_Core_Action::ADD) { | |
177 | foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) { | |
178 | if (empty($defaults[$greeting . '_id'])) { | |
179 | if ($defaultGreetingTypeId = | |
180 | CRM_Contact_BAO_Contact_Utils::defaultGreeting($form->_contactType, $greeting) | |
181 | ) { | |
182 | $defaults[$greeting . '_id'] = $defaultGreetingTypeId; | |
183 | } | |
184 | } | |
185 | } | |
186 | } | |
187 | else { | |
188 | foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) { | |
189 | $name = "{$greeting}_display"; | |
190 | $form->assign($name, CRM_Utils_Array::value($name, $defaults)); | |
191 | } | |
192 | } | |
193 | } | |
194 | ||
195 | /** | |
196 | * set array of greeting fields | |
197 | * | |
198 | * @return None | |
199 | * @access public | |
200 | */ | |
201 | static function getGreetingFields($contactType) { | |
202 | if (empty(self::$greetings[$contactType])) { | |
203 | self::$greetings[$contactType] = array(); | |
204 | ||
205 | $js = array( | |
206 | 'onfocus' => "if (!this.value) { this.value='Dear ';} else return false", | |
207 | 'onblur' => "if ( this.value == 'Dear') { this.value='';} else return false", | |
208 | ); | |
209 | ||
210 | self::$greetings[$contactType] = array( | |
211 | 'addressee' => array( | |
212 | 'field' => 'addressee_id', | |
213 | 'customField' => 'addressee_custom', | |
214 | 'label' => ts('Addressee'), | |
215 | 'customLabel' => ts('Custom Addressee'), | |
216 | 'js' => NULL, | |
217 | ), | |
218 | 'email_greeting' => array( | |
219 | 'field' => 'email_greeting_id', | |
220 | 'customField' => 'email_greeting_custom', | |
221 | 'label' => ts('Email Greeting'), | |
222 | 'customLabel' => ts('Custom Email Greeting'), | |
223 | 'js' => $js, | |
224 | ), | |
225 | 'postal_greeting' => array( | |
226 | 'field' => 'postal_greeting_id', | |
227 | 'customField' => 'postal_greeting_custom', | |
228 | 'label' => ts('Postal Greeting'), | |
229 | 'customLabel' => ts('Custom Postal Greeting'), | |
230 | 'js' => $js, | |
231 | ), | |
232 | ); | |
233 | } | |
234 | ||
235 | return self::$greetings[$contactType]; | |
236 | } | |
237 | } | |
238 |