Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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 | * This class is to build the form for adding Group | |
38 | */ | |
39 | class CRM_Contact_Form_Domain extends CRM_Core_Form { | |
40 | ||
41 | /** | |
42 | * the group id, used when editing a group | |
43 | * | |
44 | * @var int | |
45 | */ | |
46 | protected $_id; | |
47 | ||
48 | /** | |
49 | * the contact_id of domain | |
50 | * | |
51 | * @var int | |
52 | */ | |
53 | protected $_contactId; | |
54 | ||
55 | /** | |
56 | * default from email address option value id. | |
57 | * | |
58 | * @var int | |
59 | */ | |
60 | protected $_fromEmailId = NULL; | |
61 | ||
0bf513d1 | 62 | /** |
63 | * default location type fields | |
64 | * | |
65 | * @var array | |
66 | */ | |
67 | protected $_locationDefaults = array(); | |
68 | ||
6a488035 TO |
69 | /** |
70 | * how many locationBlocks should we display? | |
71 | * | |
72 | * @var int | |
73 | * @const | |
74 | */ | |
8ef12e64 | 75 | CONST LOCATION_BLOCKS = 1; |
6a488035 TO |
76 | |
77 | function preProcess() { | |
78 | CRM_Utils_System::setTitle(ts('Organization Address and Contact Info')); | |
79 | $breadCrumbPath = CRM_Utils_System::url('civicrm/admin', 'reset=1'); | |
80 | CRM_Utils_System::appendBreadCrumb(ts('Administer CiviCRM'), $breadCrumbPath); | |
81 | $session = CRM_Core_Session::singleton(); | |
82 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1')); | |
83 | ||
84 | $this->_id = CRM_Core_Config::domainID(); | |
85 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', | |
86 | $this, FALSE, 'view' | |
87 | ); | |
88 | //location blocks. | |
89 | $location = new CRM_Contact_Form_Location(); | |
90 | $location->preProcess($this); | |
91 | } | |
92 | ||
93 | /* | |
94 | * This function sets the default values for the form. | |
95 | * the default values are retrieved from the database | |
96 | * | |
97 | * @access public | |
355ba699 | 98 | * @return void |
6a488035 | 99 | */ |
86538308 EM |
100 | /** |
101 | * This virtual function is used to set the default values of | |
102 | * various form elements | |
103 | * | |
104 | * access public | |
105 | * | |
106 | * @return array reference to the array of default values | |
107 | * | |
108 | */ | |
109 | /** | |
110 | * @return array | |
111 | */ | |
6a488035 TO |
112 | function setDefaultValues() { |
113 | $defaults = array(); | |
114 | $params = array(); | |
6a488035 TO |
115 | |
116 | if (isset($this->_id)) { | |
117 | $params['id'] = $this->_id; | |
118 | CRM_Core_BAO_Domain::retrieve($params, $domainDefaults); | |
119 | $this->_contactId = $domainDefaults['contact_id']; | |
120 | //get the default domain from email address. fix CRM-3552 | |
121 | $optionValues = array(); | |
122 | $grpParams['name'] = 'from_email_address'; | |
123 | CRM_Core_OptionValue::getValues($grpParams, $optionValues); | |
124 | foreach ($optionValues as $Id => $value) { | |
125 | if ($value['is_default'] && $value['is_active']) { | |
126 | $this->_fromEmailId = $Id; | |
127 | $list = explode('"', $value['label']); | |
128 | $domainDefaults['email_name'] = CRM_Utils_Array::value(1, $list); | |
129 | $domainDefaults['email_address'] = CRM_Utils_Mail::pluckEmailFromHeader($value['label']); | |
130 | break; | |
131 | } | |
132 | } | |
133 | ||
134 | unset($params['id']); | |
135 | $locParams = array('contact_id' => $domainDefaults['contact_id']); | |
0bf513d1 | 136 | $this->_locationDefaults = $defaults = CRM_Core_BAO_Location::getValues($locParams); |
6a488035 TO |
137 | |
138 | $config = CRM_Core_Config::singleton(); | |
139 | if (!isset($defaults['address'][1]['country_id'])) { | |
140 | $defaults['address'][1]['country_id'] = $config->defaultContactCountry; | |
141 | } | |
8ef12e64 | 142 | |
6a488035 TO |
143 | if (!isset($defaults['address'][1]['state_province_id'])) { |
144 | $defaults['address'][1]['state_province_id'] = $config->defaultContactStateProvince; | |
145 | } | |
146 | ||
147 | if (!empty($defaults['address'])) { | |
148 | foreach ($defaults['address'] as $key => $value) { | |
149 | CRM_Contact_Form_Edit_Address::fixStateSelect($this, | |
150 | "address[$key][country_id]", | |
151 | "address[$key][state_province_id]", | |
152 | "address[$key][county_id]", | |
153 | CRM_Utils_Array::value('country_id', $value, | |
154 | $config->defaultContactCountry | |
155 | ), | |
156 | CRM_Utils_Array::value('state_province_id', $value, | |
157 | $config->defaultContactStateProvince | |
158 | ) | |
159 | ); | |
160 | } | |
161 | } | |
162 | } | |
163 | $defaults = array_merge($defaults, $domainDefaults); | |
164 | return $defaults; | |
165 | } | |
166 | ||
167 | /** | |
168 | * Function to actually build the form | |
169 | * | |
355ba699 | 170 | * @return void |
6a488035 TO |
171 | * @access public |
172 | */ | |
173 | public function buildQuickForm() { | |
174 | ||
175 | $this->add('text', 'name', ts('Organization Name'), array('size' => 25), TRUE); | |
176 | $this->add('text', 'description', ts('Description'), array('size' => 25)); | |
177 | ||
178 | $this->add('text', 'email_name', ts('FROM Name'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Email', 'email'), TRUE); | |
179 | ||
180 | $this->add('text', 'email_address', ts('FROM Email Address'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Email', 'email'), TRUE); | |
181 | $this->addRule('email_address', ts('Domain Email Address must use a valid email address format (e.g. \'info@example.org\').'), 'email'); | |
182 | ||
183 | //build location blocks. | |
184 | CRM_Contact_Form_Location::buildQuickForm($this); | |
185 | ||
186 | $this->addButtons(array( | |
187 | array( | |
188 | 'type' => 'next', | |
189 | 'name' => ts('Save'), | |
190 | 'subName' => 'view', | |
191 | 'isDefault' => TRUE, | |
192 | ), | |
193 | array( | |
194 | 'type' => 'cancel', | |
195 | 'name' => ts('Cancel'), | |
196 | ), | |
197 | )); | |
198 | ||
199 | if ($this->_action & CRM_Core_Action::VIEW) { | |
200 | $this->freeze(); | |
201 | } | |
202 | $this->assign('emailDomain', TRUE); | |
203 | } | |
204 | ||
205 | /** | |
206 | * Add local and global form rules | |
207 | * | |
208 | * @access protected | |
209 | * | |
210 | * @return void | |
211 | */ | |
212 | function addRules() { | |
213 | $this->addFormRule(array('CRM_Contact_Form_Domain', 'formRule')); | |
214 | } | |
215 | ||
216 | /** | |
217 | * global validation rules for the form | |
218 | * | |
219 | * @param array $fields posted values of the form | |
220 | * | |
221 | * @return array list of errors to be posted back to the form | |
222 | * @static | |
223 | * @access public | |
224 | */ | |
225 | static function formRule($fields) { | |
6a488035 | 226 | // check for state/country mapping |
ac79e2f5 | 227 | $errors = CRM_Contact_Form_Edit_Address::formRule($fields, CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullObject); |
233e6285 PJ |
228 | // $errors === TRUE means no errors from above formRule excution, |
229 | // so declaring $errors to array for futher processing | |
230 | if ($errors === TRUE) { | |
231 | $errors = array(); | |
232 | } | |
6a488035 TO |
233 | |
234 | //fix for CRM-3552, | |
235 | //as we use "fromName"<emailaddresss> format for domain email. | |
236 | if (strpos($fields['email_name'], '"') !== FALSE) { | |
237 | $errors['email_name'] = ts('Double quotes are not allow in from name.'); | |
238 | } | |
239 | ||
240 | // Check for default from email address and organization (domain) name. Force them to change it. | |
241 | if ($fields['email_address'] == 'info@EXAMPLE.ORG') { | |
242 | $errors['email_address'] = ts('Please enter a valid default FROM email address for system-generated emails.'); | |
243 | } | |
244 | if ($fields['name'] == 'Default Domain Name') { | |
245 | $errors['name'] = ts('Please enter the name of the organization or entity which owns this CiviCRM site.'); | |
246 | } | |
247 | ||
248 | return empty($errors) ? TRUE : $errors; | |
249 | } | |
250 | ||
251 | /** | |
252 | * Process the form when submitted | |
253 | * | |
254 | * @return void | |
255 | * @access public | |
256 | */ | |
257 | public function postProcess() { | |
6a488035 TO |
258 | $params = $this->exportValues(); |
259 | $params['entity_id'] = $this->_id; | |
260 | $params['entity_table'] = CRM_Core_BAO_Domain::getTableName(); | |
261 | $domain = CRM_Core_BAO_Domain::edit($params, $this->_id); | |
262 | ||
263 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); | |
264 | ||
0bf513d1 | 265 | if (isset($this->_locationDefaults['address'][1]['location_type_id'])) { |
266 | $params['address'][1]['location_type_id'] = $this->_locationDefaults['address'][1]['location_type_id']; | |
267 | } | |
268 | else { | |
269 | $params['address'][1]['location_type_id'] = $defaultLocationType->id; | |
270 | } | |
271 | ||
272 | if (isset($this->_locationDefaults['phone'][1]['location_type_id'])) { | |
273 | $params['phone'][1]['location_type_id'] = $this->_locationDefaults['phone'][1]['location_type_id']; | |
274 | } | |
275 | else { | |
276 | $params['phone'][1]['location_type_id'] = $defaultLocationType->id; | |
277 | } | |
278 | ||
279 | if (isset($this->_locationDefaults['email'][1]['location_type_id'])) { | |
280 | $params['email'][1]['location_type_id'] = $this->_locationDefaults['email'][1]['location_type_id']; | |
281 | } | |
282 | else { | |
283 | $params['email'][1]['location_type_id'] = $defaultLocationType->id; | |
284 | } | |
285 | ||
6a488035 TO |
286 | $params += array('contact_id' => $this->_contactId); |
287 | $contactParams = array ( | |
288 | 'sort_name' => $domain->name, | |
289 | 'display_name' => $domain->name, | |
290 | 'legal_name' => $domain->name, | |
291 | 'organization_name' => $domain->name, | |
8ef12e64 | 292 | 'contact_id' => $this->_contactId, |
61aa170d | 293 | 'contact_type' => 'Organization', |
6a488035 | 294 | ); |
6a488035 | 295 | |
61aa170d | 296 | if ($this->_contactId) { |
297 | $contactParams['contact_sub_type'] = CRM_Contact_BAO_Contact::getContactSubType($this->_contactId); | |
298 | } | |
299 | ||
300 | CRM_Contact_BAO_Contact::add($contactParams); | |
301 | CRM_Core_BAO_Location::create($params, TRUE); | |
6a488035 TO |
302 | |
303 | CRM_Core_BAO_Domain::edit($params, $this->_id); | |
304 | ||
305 | //set domain from email address, CRM-3552 | |
306 | $emailName = '"' . $params['email_name'] . '" <' . $params['email_address'] . '>'; | |
307 | ||
308 | $emailParams = array( | |
309 | 'label' => $emailName, | |
310 | 'description' => $params['description'], | |
311 | 'is_active' => 1, | |
312 | 'is_default' => 1, | |
313 | ); | |
314 | ||
315 | $groupParams = array('name' => 'from_email_address'); | |
316 | ||
317 | //get the option value wt. | |
318 | if ($this->_fromEmailId) { | |
319 | $action = $this->_action; | |
320 | $emailParams['weight'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $this->_fromEmailId, 'weight'); | |
321 | } | |
322 | else { | |
323 | //add from email address. | |
324 | $action = CRM_Core_Action::ADD; | |
325 | $grpId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'from_email_address', 'id', 'name'); | |
326 | $fieldValues = array('option_group_id' => $grpId); | |
327 | $emailParams['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', $fieldValues); | |
328 | } | |
329 | ||
330 | ||
331 | //reset default within domain. | |
332 | $emailParams['reset_default_for'] = array('domain_id' => CRM_Core_Config::domainID()); | |
333 | ||
334 | CRM_Core_OptionValue::addOptionValue($emailParams, $groupParams, $action, $this->_fromEmailId); | |
335 | ||
336 | CRM_Core_Session::setStatus(ts("Domain information for '%1' has been saved.", array(1 => $domain->name)), ts('Saved'), 'success'); | |
337 | $session = CRM_Core_Session::singleton(); | |
338 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1')); | |
339 | } | |
340 | } | |
341 |