3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class is to build the form for adding Group.
21 class CRM_Contact_Form_Domain
extends CRM_Core_Form
{
24 * The group id, used when editing a group
31 * The contact_id of domain.
35 protected $_contactId;
38 * Default from email address option value id.
42 protected $_fromEmailId = NULL;
45 * Default location type fields.
49 protected $_locationDefaults = [];
52 * How many locationBlocks should we display?
57 const LOCATION_BLOCKS
= 1;
60 * Explicitly declare the entity api name.
62 public function getDefaultEntity() {
67 * Explicitly declare the form context.
69 public function getDefaultContext() {
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'));
80 $this->_id
= CRM_Core_Config
::domainID();
81 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String',
85 $location = new CRM_Contact_Form_Location();
86 $location->preProcess($this);
90 * This virtual function is used to set the default values of.
91 * various form elements
94 * reference to the array of default values
97 public function setDefaultValues() {
101 if (isset($this->_id
)) {
102 $params['id'] = $this->_id
;
103 CRM_Core_BAO_Domain
::retrieve($params, $domainDefaults);
104 $this->_contactId
= $domainDefaults['contact_id'];
106 unset($params['id']);
107 $locParams = ['contact_id' => $domainDefaults['contact_id']];
108 $this->_locationDefaults
= $defaults = CRM_Core_BAO_Location
::getValues($locParams);
110 $config = CRM_Core_Config
::singleton();
111 if (!isset($defaults['address'][1]['country_id'])) {
112 $defaults['address'][1]['country_id'] = $config->defaultContactCountry
;
115 if (!isset($defaults['address'][1]['state_province_id'])) {
116 $defaults['address'][1]['state_province_id'] = $config->defaultContactStateProvince
;
120 $defaults = array_merge($defaults, $domainDefaults);
125 * Build the form object.
127 public function buildQuickForm() {
128 $this->addField('name', ['label' => ts('Organization Name')], TRUE);
129 $this->addField('description', ['label' => ts('Description'), 'size' => 30]);
131 //build location blocks.
132 CRM_Contact_Form_Location
::buildQuickForm($this);
137 'name' => ts('Save'),
143 'name' => ts('Cancel'),
147 if ($this->_action
& CRM_Core_Action
::VIEW
) {
150 $this->assign('emailDomain', TRUE);
154 * Add local and global form rules.
156 public function addRules() {
157 $this->addFormRule(['CRM_Contact_Form_Domain', 'formRule']);
161 * Global validation rules for the form.
163 * @param array $fields
164 * Posted values of the form.
167 * list of errors to be posted back to the form
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) {
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.');
182 return empty($errors) ?
TRUE : $errors;
186 * Process the form when submitted.
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
);
194 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
196 if (isset($this->_locationDefaults
['address'][1]['location_type_id'])) {
197 $params['address'][1]['location_type_id'] = $this->_locationDefaults
['address'][1]['location_type_id'];
200 $params['address'][1]['location_type_id'] = $defaultLocationType->id
;
203 if (isset($this->_locationDefaults
['phone'][1]['location_type_id'])) {
204 $params['phone'][1]['location_type_id'] = $this->_locationDefaults
['phone'][1]['location_type_id'];
207 $params['phone'][1]['location_type_id'] = $defaultLocationType->id
;
210 if (isset($this->_locationDefaults
['email'][1]['location_type_id'])) {
211 $params['email'][1]['location_type_id'] = $this->_locationDefaults
['email'][1]['location_type_id'];
214 $params['email'][1]['location_type_id'] = $defaultLocationType->id
;
217 $params +
= ['contact_id' => $this->_contactId
];
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',
227 if ($this->_contactId
) {
228 $contactParams['contact_sub_type'] = CRM_Contact_BAO_Contact
::getContactSubType($this->_contactId
);
231 CRM_Contact_BAO_Contact
::add($contactParams);
232 CRM_Core_BAO_Location
::create($params, TRUE);
234 CRM_Core_BAO_Domain
::edit($params, $this->_id
);
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'));