3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
35 * This class is to build the form for adding Group.
37 class CRM_Contact_Form_Domain
extends CRM_Core_Form
{
40 * The group id, used when editing a group
47 * The contact_id of domain.
51 protected $_contactId;
54 * Default from email address option value id.
58 protected $_fromEmailId = NULL;
61 * Default location type fields.
65 protected $_locationDefaults = array();
68 * How many locationBlocks should we display?
73 const LOCATION_BLOCKS
= 1;
76 * Explicitly declare the entity api name.
78 public function getDefaultEntity() {
83 * Explicitly declare the form context.
85 public function getDefaultContext() {
89 public function preProcess() {
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'));
96 $this->_id
= CRM_Core_Config
::domainID();
97 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String',
101 $location = new CRM_Contact_Form_Location();
102 $location->preProcess($this);
106 * This virtual function is used to set the default values of.
107 * various form elements
110 * reference to the array of default values
113 public function setDefaultValues() {
117 if (isset($this->_id
)) {
118 $params['id'] = $this->_id
;
119 CRM_Core_BAO_Domain
::retrieve($params, $domainDefaults);
120 $this->_contactId
= $domainDefaults['contact_id'];
122 unset($params['id']);
123 $locParams = array('contact_id' => $domainDefaults['contact_id']);
124 $this->_locationDefaults
= $defaults = CRM_Core_BAO_Location
::getValues($locParams);
126 $config = CRM_Core_Config
::singleton();
127 if (!isset($defaults['address'][1]['country_id'])) {
128 $defaults['address'][1]['country_id'] = $config->defaultContactCountry
;
131 if (!isset($defaults['address'][1]['state_province_id'])) {
132 $defaults['address'][1]['state_province_id'] = $config->defaultContactStateProvince
;
136 $defaults = array_merge($defaults, $domainDefaults);
141 * Build the form object.
143 public function buildQuickForm() {
144 $this->addField('name', array('label' => ts('Organization Name')), TRUE);
145 $this->addField('description', array('label' => ts('Description'), 'size' => 30));
147 //build location blocks.
148 CRM_Contact_Form_Location
::buildQuickForm($this);
150 $this->addButtons(array(
153 'name' => ts('Save'),
159 'name' => ts('Cancel'),
163 if ($this->_action
& CRM_Core_Action
::VIEW
) {
166 $this->assign('emailDomain', TRUE);
170 * Add local and global form rules.
172 public function addRules() {
173 $this->addFormRule(array('CRM_Contact_Form_Domain', 'formRule'));
177 * Global validation rules for the form.
179 * @param array $fields
180 * Posted values of the form.
183 * list of errors to be posted back to the form
185 public static function formRule($fields) {
186 // check for state/country mapping
187 $errors = CRM_Contact_Form_Edit_Address
::formRule($fields, CRM_Core_DAO
::$_nullArray, CRM_Core_DAO
::$_nullObject);
188 // $errors === TRUE means no errors from above formRule excution,
189 // so declaring $errors to array for further processing
190 if ($errors === TRUE) {
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.');
198 return empty($errors) ?
TRUE : $errors;
202 * Process the form when submitted.
204 public function postProcess() {
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
);
210 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
212 if (isset($this->_locationDefaults
['address'][1]['location_type_id'])) {
213 $params['address'][1]['location_type_id'] = $this->_locationDefaults
['address'][1]['location_type_id'];
216 $params['address'][1]['location_type_id'] = $defaultLocationType->id
;
219 if (isset($this->_locationDefaults
['phone'][1]['location_type_id'])) {
220 $params['phone'][1]['location_type_id'] = $this->_locationDefaults
['phone'][1]['location_type_id'];
223 $params['phone'][1]['location_type_id'] = $defaultLocationType->id
;
226 if (isset($this->_locationDefaults
['email'][1]['location_type_id'])) {
227 $params['email'][1]['location_type_id'] = $this->_locationDefaults
['email'][1]['location_type_id'];
230 $params['email'][1]['location_type_id'] = $defaultLocationType->id
;
233 $params +
= array('contact_id' => $this->_contactId
);
234 $contactParams = array(
235 'sort_name' => $domain->name
,
236 'display_name' => $domain->name
,
237 'legal_name' => $domain->name
,
238 'organization_name' => $domain->name
,
239 'contact_id' => $this->_contactId
,
240 'contact_type' => 'Organization',
243 if ($this->_contactId
) {
244 $contactParams['contact_sub_type'] = CRM_Contact_BAO_Contact
::getContactSubType($this->_contactId
);
247 CRM_Contact_BAO_Contact
::add($contactParams);
248 CRM_Core_BAO_Location
::create($params, TRUE);
250 CRM_Core_BAO_Domain
::edit($params, $this->_id
);
252 CRM_Core_Session
::setStatus(ts("Domain information for '%1' has been saved.", array(1 => $domain->name
)), ts('Saved'), 'success');
253 $session = CRM_Core_Session
::singleton();
254 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin', 'reset=1'));