Merge pull request #19248 from eileenmcnaughton/dep_utils
[civicrm-core.git] / CRM / Contact / Form / Domain.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class is to build the form for adding Group.
20 */
21 class CRM_Contact_Form_Domain extends CRM_Core_Form {
22
23 /**
24 * The group id, used when editing a group
25 *
26 * @var int
27 */
28 protected $_id;
29
30 /**
31 * The contact_id of domain.
32 *
33 * @var int
34 */
35 protected $_contactId;
36
37 /**
38 * Default from email address option value id.
39 *
40 * @var int
41 */
42 protected $_fromEmailId = NULL;
43
44 /**
45 * Default location type fields.
46 *
47 * @var array
48 */
49 protected $_locationDefaults = [];
50
51 /**
52 * How many locationBlocks should we display?
53 *
54 * @var int
55 * @const
56 */
57 const LOCATION_BLOCKS = 1;
58
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
73 public function preProcess() {
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
89 /**
90 * This virtual function is used to set the default values of.
91 * various form elements
92 *
93 * @return array
94 * reference to the array of default values
95 *
96 */
97 public function setDefaultValues() {
98 $defaults = [];
99 $params = [];
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'];
105
106 unset($params['id']);
107 $locParams = ['contact_id' => $domainDefaults['contact_id']];
108 $this->_locationDefaults = $defaults = CRM_Core_BAO_Location::getValues($locParams);
109
110 $config = CRM_Core_Config::singleton();
111 if (!isset($defaults['address'][1]['country_id'])) {
112 $defaults['address'][1]['country_id'] = $config->defaultContactCountry;
113 }
114
115 if (!isset($defaults['address'][1]['state_province_id'])) {
116 $defaults['address'][1]['state_province_id'] = $config->defaultContactStateProvince;
117 }
118
119 }
120 $defaults = array_merge($defaults, $domainDefaults);
121 return $defaults;
122 }
123
124 /**
125 * Build the form object.
126 */
127 public function buildQuickForm() {
128 $this->addField('name', ['label' => ts('Organization Name')], TRUE);
129 $this->addField('description', ['label' => ts('Description'), 'size' => 30]);
130
131 //build location blocks.
132 CRM_Contact_Form_Location::buildQuickForm($this);
133
134 $this->addButtons([
135 [
136 'type' => 'next',
137 'name' => ts('Save'),
138 'subName' => 'view',
139 'isDefault' => TRUE,
140 ],
141 [
142 'type' => 'cancel',
143 'name' => ts('Cancel'),
144 ],
145 ]);
146
147 if ($this->_action & CRM_Core_Action::VIEW) {
148 $this->freeze();
149 }
150 $this->assign('emailDomain', TRUE);
151 }
152
153 /**
154 * Add local and global form rules.
155 */
156 public function addRules() {
157 $this->addFormRule(['CRM_Contact_Form_Domain', 'formRule']);
158 }
159
160 /**
161 * Global validation rules for the form.
162 *
163 * @param array $fields
164 * Posted values of the form.
165 *
166 * @return array
167 * list of errors to be posted back to the form
168 */
169 public static function formRule($fields) {
170 // check for state/country mapping
171 $errors = CRM_Contact_Form_Edit_Address::formRule($fields, CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullObject);
172 // $errors === TRUE means no errors from above formRule excution,
173 // so declaring $errors to array for further processing
174 if ($errors === TRUE) {
175 $errors = [];
176 }
177
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 /**
186 * Process the form when submitted.
187 */
188 public function postProcess() {
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
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
217 $params += ['contact_id' => $this->_contactId];
218 $contactParams = [
219 'sort_name' => $domain->name,
220 'display_name' => $domain->name,
221 'legal_name' => $domain->name,
222 'organization_name' => $domain->name,
223 'contact_id' => $this->_contactId,
224 'contact_type' => 'Organization',
225 ];
226
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);
233
234 CRM_Core_BAO_Domain::edit($params, $this->_id);
235
236 CRM_Core_Session::setStatus(ts("Domain information for '%1' has been saved.", [1 => $domain->name]), ts('Saved'), 'success');
237 $session = CRM_Core_Session::singleton();
238 $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
239 }
240
241 }