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