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