Clean up calls to CRM_Utils_Request::retrieve
[civicrm-core.git] / CRM / Contact / Form / Domain.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
c037736a 19 * This class is to build the form for adding Group.
6a488035
TO
20 */
21class CRM_Contact_Form_Domain extends CRM_Core_Form {
22
23 /**
100fef9d 24 * The group id, used when editing a group
6a488035
TO
25 *
26 * @var int
27 */
28 protected $_id;
29
30 /**
fe482240 31 * The contact_id of domain.
6a488035
TO
32 *
33 * @var int
34 */
35 protected $_contactId;
36
37 /**
100fef9d 38 * Default from email address option value id.
6a488035
TO
39 *
40 * @var int
41 */
42 protected $_fromEmailId = NULL;
43
0bf513d1 44 /**
fe482240 45 * Default location type fields.
0bf513d1 46 *
47 * @var array
48 */
be2fb01f 49 protected $_locationDefaults = [];
0bf513d1 50
6a488035 51 /**
100fef9d 52 * How many locationBlocks should we display?
6a488035
TO
53 *
54 * @var int
55 * @const
56 */
7da04cde 57 const LOCATION_BLOCKS = 1;
6a488035 58
a104f8f5
TM
59 /**
60 * Explicitly declare the entity api name.
61 */
62 public function getDefaultEntity() {
63 return 'Domain';
64 }
65
66 /**
67 * Explicitly declare the form context.
68 */
69 public function getDefaultContext() {
70 return 'create';
71 }
72
00be9182 73 public function preProcess() {
6a488035
TO
74 CRM_Utils_System::setTitle(ts('Organization Address and Contact Info'));
75 $breadCrumbPath = CRM_Utils_System::url('civicrm/admin', 'reset=1');
76 CRM_Utils_System::appendBreadCrumb(ts('Administer CiviCRM'), $breadCrumbPath);
77 $session = CRM_Core_Session::singleton();
78 $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
79
80 $this->_id = CRM_Core_Config::domainID();
81 $this->_action = CRM_Utils_Request::retrieve('action', 'String',
82 $this, FALSE, 'view'
83 );
84 //location blocks.
85 $location = new CRM_Contact_Form_Location();
86 $location->preProcess($this);
87 }
88
86538308 89 /**
fe482240 90 * This virtual function is used to set the default values of.
86538308
EM
91 * various form elements
92 *
a6c01b45
CW
93 * @return array
94 * reference to the array of default values
d424ffde 95 *
86538308 96 */
00be9182 97 public function setDefaultValues() {
be2fb01f
CW
98 $defaults = [];
99 $params = [];
6a488035
TO
100
101 if (isset($this->_id)) {
102 $params['id'] = $this->_id;
103 CRM_Core_BAO_Domain::retrieve($params, $domainDefaults);
104 $this->_contactId = $domainDefaults['contact_id'];
6a488035
TO
105
106 unset($params['id']);
be2fb01f 107 $locParams = ['contact_id' => $domainDefaults['contact_id']];
0bf513d1 108 $this->_locationDefaults = $defaults = CRM_Core_BAO_Location::getValues($locParams);
6a488035
TO
109
110 $config = CRM_Core_Config::singleton();
111 if (!isset($defaults['address'][1]['country_id'])) {
112 $defaults['address'][1]['country_id'] = $config->defaultContactCountry;
113 }
8ef12e64 114
6a488035
TO
115 if (!isset($defaults['address'][1]['state_province_id'])) {
116 $defaults['address'][1]['state_province_id'] = $config->defaultContactStateProvince;
117 }
118
6a488035
TO
119 }
120 $defaults = array_merge($defaults, $domainDefaults);
121 return $defaults;
122 }
123
124 /**
fe482240 125 * Build the form object.
6a488035
TO
126 */
127 public function buildQuickForm() {
be2fb01f
CW
128 $this->addField('name', ['label' => ts('Organization Name')], TRUE);
129 $this->addField('description', ['label' => ts('Description'), 'size' => 30]);
6a488035
TO
130
131 //build location blocks.
132 CRM_Contact_Form_Location::buildQuickForm($this);
133
be2fb01f
CW
134 $this->addButtons([
135 [
353ffa53
TO
136 'type' => 'next',
137 'name' => ts('Save'),
138 'subName' => 'view',
139 'isDefault' => TRUE,
be2fb01f
CW
140 ],
141 [
353ffa53
TO
142 'type' => 'cancel',
143 'name' => ts('Cancel'),
be2fb01f
CW
144 ],
145 ]);
6a488035
TO
146
147 if ($this->_action & CRM_Core_Action::VIEW) {
148 $this->freeze();
149 }
150 $this->assign('emailDomain', TRUE);
151 }
152
153 /**
fe482240 154 * Add local and global form rules.
6a488035 155 */
00be9182 156 public function addRules() {
be2fb01f 157 $this->addFormRule(['CRM_Contact_Form_Domain', 'formRule']);
6a488035
TO
158 }
159
160 /**
fe482240 161 * Global validation rules for the form.
6a488035 162 *
77c5b619
TO
163 * @param array $fields
164 * Posted values of the form.
6a488035 165 *
a6c01b45
CW
166 * @return array
167 * list of errors to be posted back to the form
6a488035 168 */
00be9182 169 public static function formRule($fields) {
6a488035 170 // check for state/country mapping
ac79e2f5 171 $errors = CRM_Contact_Form_Edit_Address::formRule($fields, CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullObject);
233e6285 172 // $errors === TRUE means no errors from above formRule excution,
b44e3f84 173 // so declaring $errors to array for further processing
233e6285 174 if ($errors === TRUE) {
be2fb01f 175 $errors = [];
233e6285 176 }
6a488035 177
6a488035
TO
178 if ($fields['name'] == 'Default Domain Name') {
179 $errors['name'] = ts('Please enter the name of the organization or entity which owns this CiviCRM site.');
180 }
181
182 return empty($errors) ? TRUE : $errors;
183 }
184
185 /**
fe482240 186 * Process the form when submitted.
6a488035
TO
187 */
188 public function postProcess() {
6a488035
TO
189 $params = $this->exportValues();
190 $params['entity_id'] = $this->_id;
191 $params['entity_table'] = CRM_Core_BAO_Domain::getTableName();
192 $domain = CRM_Core_BAO_Domain::edit($params, $this->_id);
193
194 $defaultLocationType = CRM_Core_BAO_LocationType::getDefault();
195
0bf513d1 196 if (isset($this->_locationDefaults['address'][1]['location_type_id'])) {
197 $params['address'][1]['location_type_id'] = $this->_locationDefaults['address'][1]['location_type_id'];
198 }
199 else {
200 $params['address'][1]['location_type_id'] = $defaultLocationType->id;
201 }
202
203 if (isset($this->_locationDefaults['phone'][1]['location_type_id'])) {
204 $params['phone'][1]['location_type_id'] = $this->_locationDefaults['phone'][1]['location_type_id'];
205 }
206 else {
207 $params['phone'][1]['location_type_id'] = $defaultLocationType->id;
208 }
209
210 if (isset($this->_locationDefaults['email'][1]['location_type_id'])) {
211 $params['email'][1]['location_type_id'] = $this->_locationDefaults['email'][1]['location_type_id'];
212 }
213 else {
214 $params['email'][1]['location_type_id'] = $defaultLocationType->id;
215 }
216
be2fb01f
CW
217 $params += ['contact_id' => $this->_contactId];
218 $contactParams = [
353ffa53 219 'sort_name' => $domain->name,
6a488035 220 'display_name' => $domain->name,
353ffa53 221 'legal_name' => $domain->name,
6a488035 222 'organization_name' => $domain->name,
8ef12e64 223 'contact_id' => $this->_contactId,
61aa170d 224 'contact_type' => 'Organization',
be2fb01f 225 ];
6a488035 226
61aa170d 227 if ($this->_contactId) {
228 $contactParams['contact_sub_type'] = CRM_Contact_BAO_Contact::getContactSubType($this->_contactId);
229 }
230
231 CRM_Contact_BAO_Contact::add($contactParams);
232 CRM_Core_BAO_Location::create($params, TRUE);
6a488035
TO
233
234 CRM_Core_BAO_Domain::edit($params, $this->_id);
235
be2fb01f 236 CRM_Core_Session::setStatus(ts("Domain information for '%1' has been saved.", [1 => $domain->name]), ts('Saved'), 'success');
6a488035
TO
237 $session = CRM_Core_Session::singleton();
238 $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
239 }
96025800 240
6a488035 241}