3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * This class is to build the form for adding Group
39 class CRM_Contact_Form_Domain
extends CRM_Core_Form
{
42 * The group id, used when editing a group
49 * The contact_id of domain
53 protected $_contactId;
56 * Default from email address option value id.
60 protected $_fromEmailId = NULL;
63 * Default location type fields
67 protected $_locationDefaults = array();
70 * How many locationBlocks should we display?
75 CONST LOCATION_BLOCKS
= 1;
77 function preProcess() {
78 CRM_Utils_System
::setTitle(ts('Organization Address and Contact Info'));
79 $breadCrumbPath = CRM_Utils_System
::url('civicrm/admin', 'reset=1');
80 CRM_Utils_System
::appendBreadCrumb(ts('Administer CiviCRM'), $breadCrumbPath);
81 $session = CRM_Core_Session
::singleton();
82 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin', 'reset=1'));
84 $this->_id
= CRM_Core_Config
::domainID();
85 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String',
89 $location = new CRM_Contact_Form_Location();
90 $location->preProcess($this);
94 * Set default values for the form.
95 * the default values are retrieved from the database
101 * This virtual function is used to set the default values of
102 * various form elements
106 * @return array reference to the array of default values
112 function setDefaultValues() {
116 if (isset($this->_id
)) {
117 $params['id'] = $this->_id
;
118 CRM_Core_BAO_Domain
::retrieve($params, $domainDefaults);
119 $this->_contactId
= $domainDefaults['contact_id'];
120 //get the default domain from email address. fix CRM-3552
121 $optionValues = array();
122 $grpParams['name'] = 'from_email_address';
123 CRM_Core_OptionValue
::getValues($grpParams, $optionValues);
124 foreach ($optionValues as $Id => $value) {
125 if ($value['is_default'] && $value['is_active']) {
126 $this->_fromEmailId
= $Id;
127 $list = explode('"', $value['label']);
128 $domainDefaults['email_name'] = CRM_Utils_Array
::value(1, $list);
129 $domainDefaults['email_address'] = CRM_Utils_Mail
::pluckEmailFromHeader($value['label']);
134 unset($params['id']);
135 $locParams = array('contact_id' => $domainDefaults['contact_id']);
136 $this->_locationDefaults
= $defaults = CRM_Core_BAO_Location
::getValues($locParams);
138 $config = CRM_Core_Config
::singleton();
139 if (!isset($defaults['address'][1]['country_id'])) {
140 $defaults['address'][1]['country_id'] = $config->defaultContactCountry
;
143 if (!isset($defaults['address'][1]['state_province_id'])) {
144 $defaults['address'][1]['state_province_id'] = $config->defaultContactStateProvince
;
148 $defaults = array_merge($defaults, $domainDefaults);
153 * Build the form object
158 public function buildQuickForm() {
160 $this->add('text', 'name', ts('Organization Name'), array('size' => 25), TRUE);
161 $this->add('text', 'description', ts('Description'), array('size' => 25));
163 $this->add('text', 'email_name', ts('FROM Name'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Email', 'email'), TRUE);
165 $this->add('text', 'email_address', ts('FROM Email Address'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Email', 'email'), TRUE);
166 $this->addRule('email_address', ts('Domain Email Address must use a valid email address format (e.g. \'info@example.org\').'), 'email');
168 //build location blocks.
169 CRM_Contact_Form_Location
::buildQuickForm($this);
171 $this->addButtons(array(
174 'name' => ts('Save'),
180 'name' => ts('Cancel'),
184 if ($this->_action
& CRM_Core_Action
::VIEW
) {
187 $this->assign('emailDomain', TRUE);
191 * Add local and global form rules
197 function addRules() {
198 $this->addFormRule(array('CRM_Contact_Form_Domain', 'formRule'));
202 * Global validation rules for the form
204 * @param array $fields posted values of the form
206 * @return array list of errors to be posted back to the form
210 static function formRule($fields) {
211 // check for state/country mapping
212 $errors = CRM_Contact_Form_Edit_Address
::formRule($fields, CRM_Core_DAO
::$_nullArray, CRM_Core_DAO
::$_nullObject);
213 // $errors === TRUE means no errors from above formRule excution,
214 // so declaring $errors to array for futher processing
215 if ($errors === TRUE) {
220 //as we use "fromName"<emailaddresss> format for domain email.
221 if (strpos($fields['email_name'], '"') !== FALSE) {
222 $errors['email_name'] = ts('Double quotes are not allow in from name.');
225 // Check for default from email address and organization (domain) name. Force them to change it.
226 if ($fields['email_address'] == 'info@EXAMPLE.ORG') {
227 $errors['email_address'] = ts('Please enter a valid default FROM email address for system-generated emails.');
229 if ($fields['name'] == 'Default Domain Name') {
230 $errors['name'] = ts('Please enter the name of the organization or entity which owns this CiviCRM site.');
233 return empty($errors) ?
TRUE : $errors;
237 * Process the form when submitted
242 public function postProcess() {
243 $params = $this->exportValues();
244 $params['entity_id'] = $this->_id
;
245 $params['entity_table'] = CRM_Core_BAO_Domain
::getTableName();
246 $domain = CRM_Core_BAO_Domain
::edit($params, $this->_id
);
248 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
250 if (isset($this->_locationDefaults
['address'][1]['location_type_id'])) {
251 $params['address'][1]['location_type_id'] = $this->_locationDefaults
['address'][1]['location_type_id'];
254 $params['address'][1]['location_type_id'] = $defaultLocationType->id
;
257 if (isset($this->_locationDefaults
['phone'][1]['location_type_id'])) {
258 $params['phone'][1]['location_type_id'] = $this->_locationDefaults
['phone'][1]['location_type_id'];
261 $params['phone'][1]['location_type_id'] = $defaultLocationType->id
;
264 if (isset($this->_locationDefaults
['email'][1]['location_type_id'])) {
265 $params['email'][1]['location_type_id'] = $this->_locationDefaults
['email'][1]['location_type_id'];
268 $params['email'][1]['location_type_id'] = $defaultLocationType->id
;
271 $params +
= array('contact_id' => $this->_contactId
);
272 $contactParams = array (
273 'sort_name' => $domain->name
,
274 'display_name' => $domain->name
,
275 'legal_name' => $domain->name
,
276 'organization_name' => $domain->name
,
277 'contact_id' => $this->_contactId
,
278 'contact_type' => 'Organization',
281 if ($this->_contactId
) {
282 $contactParams['contact_sub_type'] = CRM_Contact_BAO_Contact
::getContactSubType($this->_contactId
);
285 CRM_Contact_BAO_Contact
::add($contactParams);
286 CRM_Core_BAO_Location
::create($params, TRUE);
288 CRM_Core_BAO_Domain
::edit($params, $this->_id
);
290 //set domain from email address, CRM-3552
291 $emailName = '"' . $params['email_name'] . '" <' . $params['email_address'] . '>';
293 $emailParams = array(
294 'label' => $emailName,
295 'description' => $params['description'],
300 $groupParams = array('name' => 'from_email_address');
302 //get the option value wt.
303 if ($this->_fromEmailId
) {
304 $action = $this->_action
;
305 $emailParams['weight'] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $this->_fromEmailId
, 'weight');
308 //add from email address.
309 $action = CRM_Core_Action
::ADD
;
310 $grpId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'from_email_address', 'id', 'name');
311 $fieldValues = array('option_group_id' => $grpId);
312 $emailParams['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_OptionValue', $fieldValues);
316 //reset default within domain.
317 $emailParams['reset_default_for'] = array('domain_id' => CRM_Core_Config
::domainID());
319 CRM_Core_OptionValue
::addOptionValue($emailParams, $groupParams, $action, $this->_fromEmailId
);
321 CRM_Core_Session
::setStatus(ts("Domain information for '%1' has been saved.", array(1 => $domain->name
)), ts('Saved'), 'success');
322 $session = CRM_Core_Session
::singleton();
323 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin', 'reset=1'));