Merge pull request #15764 from civicrm/5.20
[civicrm-core.git] / CRM / Contact / Form / Domain.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
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
f299f7db 31 * @copyright CiviCRM LLC (c) 2004-2020
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 */
be2fb01f 65 protected $_locationDefaults = [];
0bf513d1 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() {
be2fb01f
CW
114 $defaults = [];
115 $params = [];
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'];
6a488035
TO
121
122 unset($params['id']);
be2fb01f 123 $locParams = ['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
6a488035
TO
135 }
136 $defaults = array_merge($defaults, $domainDefaults);
137 return $defaults;
138 }
139
140 /**
fe482240 141 * Build the form object.
6a488035
TO
142 */
143 public function buildQuickForm() {
be2fb01f
CW
144 $this->addField('name', ['label' => ts('Organization Name')], TRUE);
145 $this->addField('description', ['label' => ts('Description'), 'size' => 30]);
6a488035
TO
146
147 //build location blocks.
148 CRM_Contact_Form_Location::buildQuickForm($this);
149
be2fb01f
CW
150 $this->addButtons([
151 [
353ffa53
TO
152 'type' => 'next',
153 'name' => ts('Save'),
154 'subName' => 'view',
155 'isDefault' => TRUE,
be2fb01f
CW
156 ],
157 [
353ffa53
TO
158 'type' => 'cancel',
159 'name' => ts('Cancel'),
be2fb01f
CW
160 ],
161 ]);
6a488035
TO
162
163 if ($this->_action & CRM_Core_Action::VIEW) {
164 $this->freeze();
165 }
166 $this->assign('emailDomain', TRUE);
167 }
168
169 /**
fe482240 170 * Add local and global form rules.
6a488035 171 */
00be9182 172 public function addRules() {
be2fb01f 173 $this->addFormRule(['CRM_Contact_Form_Domain', 'formRule']);
6a488035
TO
174 }
175
176 /**
fe482240 177 * Global validation rules for the form.
6a488035 178 *
77c5b619
TO
179 * @param array $fields
180 * Posted values of the form.
6a488035 181 *
a6c01b45
CW
182 * @return array
183 * list of errors to be posted back to the form
6a488035 184 */
00be9182 185 public static function formRule($fields) {
6a488035 186 // check for state/country mapping
ac79e2f5 187 $errors = CRM_Contact_Form_Edit_Address::formRule($fields, CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullObject);
233e6285 188 // $errors === TRUE means no errors from above formRule excution,
b44e3f84 189 // so declaring $errors to array for further processing
233e6285 190 if ($errors === TRUE) {
be2fb01f 191 $errors = [];
233e6285 192 }
6a488035 193
6a488035
TO
194 if ($fields['name'] == 'Default Domain Name') {
195 $errors['name'] = ts('Please enter the name of the organization or entity which owns this CiviCRM site.');
196 }
197
198 return empty($errors) ? TRUE : $errors;
199 }
200
201 /**
fe482240 202 * Process the form when submitted.
6a488035
TO
203 */
204 public function postProcess() {
6a488035
TO
205 $params = $this->exportValues();
206 $params['entity_id'] = $this->_id;
207 $params['entity_table'] = CRM_Core_BAO_Domain::getTableName();
208 $domain = CRM_Core_BAO_Domain::edit($params, $this->_id);
209
210 $defaultLocationType = CRM_Core_BAO_LocationType::getDefault();
211
0bf513d1 212 if (isset($this->_locationDefaults['address'][1]['location_type_id'])) {
213 $params['address'][1]['location_type_id'] = $this->_locationDefaults['address'][1]['location_type_id'];
214 }
215 else {
216 $params['address'][1]['location_type_id'] = $defaultLocationType->id;
217 }
218
219 if (isset($this->_locationDefaults['phone'][1]['location_type_id'])) {
220 $params['phone'][1]['location_type_id'] = $this->_locationDefaults['phone'][1]['location_type_id'];
221 }
222 else {
223 $params['phone'][1]['location_type_id'] = $defaultLocationType->id;
224 }
225
226 if (isset($this->_locationDefaults['email'][1]['location_type_id'])) {
227 $params['email'][1]['location_type_id'] = $this->_locationDefaults['email'][1]['location_type_id'];
228 }
229 else {
230 $params['email'][1]['location_type_id'] = $defaultLocationType->id;
231 }
232
be2fb01f
CW
233 $params += ['contact_id' => $this->_contactId];
234 $contactParams = [
353ffa53 235 'sort_name' => $domain->name,
6a488035 236 'display_name' => $domain->name,
353ffa53 237 'legal_name' => $domain->name,
6a488035 238 'organization_name' => $domain->name,
8ef12e64 239 'contact_id' => $this->_contactId,
61aa170d 240 'contact_type' => 'Organization',
be2fb01f 241 ];
6a488035 242
61aa170d 243 if ($this->_contactId) {
244 $contactParams['contact_sub_type'] = CRM_Contact_BAO_Contact::getContactSubType($this->_contactId);
245 }
246
247 CRM_Contact_BAO_Contact::add($contactParams);
248 CRM_Core_BAO_Location::create($params, TRUE);
6a488035
TO
249
250 CRM_Core_BAO_Domain::edit($params, $this->_id);
251
be2fb01f 252 CRM_Core_Session::setStatus(ts("Domain information for '%1' has been saved.", [1 => $domain->name]), ts('Saved'), 'success');
6a488035
TO
253 $session = CRM_Core_Session::singleton();
254 $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
255 }
96025800 256
6a488035 257}