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