3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 * how many locationBlocks should we display?
68 CONST LOCATION_BLOCKS
= 1;
70 function preProcess() {
71 CRM_Utils_System
::setTitle(ts('Organization Address and Contact Info'));
72 $breadCrumbPath = CRM_Utils_System
::url('civicrm/admin', 'reset=1');
73 CRM_Utils_System
::appendBreadCrumb(ts('Administer CiviCRM'), $breadCrumbPath);
74 $session = CRM_Core_Session
::singleton();
75 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin', 'reset=1'));
77 $this->_id
= CRM_Core_Config
::domainID();
78 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String',
82 $location = new CRM_Contact_Form_Location();
83 $location->preProcess($this);
87 * This function sets the default values for the form.
88 * the default values are retrieved from the database
93 function setDefaultValues() {
98 if (isset($this->_id
)) {
99 $params['id'] = $this->_id
;
100 CRM_Core_BAO_Domain
::retrieve($params, $domainDefaults);
101 $this->_contactId
= $domainDefaults['contact_id'];
102 //get the default domain from email address. fix CRM-3552
103 $optionValues = array();
104 $grpParams['name'] = 'from_email_address';
105 CRM_Core_OptionValue
::getValues($grpParams, $optionValues);
106 foreach ($optionValues as $Id => $value) {
107 if ($value['is_default'] && $value['is_active']) {
108 $this->_fromEmailId
= $Id;
109 $list = explode('"', $value['label']);
110 $domainDefaults['email_name'] = CRM_Utils_Array
::value(1, $list);
111 $domainDefaults['email_address'] = CRM_Utils_Mail
::pluckEmailFromHeader($value['label']);
116 unset($params['id']);
117 $locParams = array('contact_id' => $domainDefaults['contact_id']);
118 $defaults = CRM_Core_BAO_Location
::getValues($locParams);
120 $config = CRM_Core_Config
::singleton();
121 if (!isset($defaults['address'][1]['country_id'])) {
122 $defaults['address'][1]['country_id'] = $config->defaultContactCountry
;
125 if (!isset($defaults['address'][1]['state_province_id'])) {
126 $defaults['address'][1]['state_province_id'] = $config->defaultContactStateProvince
;
129 if (!empty($defaults['address'])) {
130 foreach ($defaults['address'] as $key => $value) {
131 CRM_Contact_Form_Edit_Address
::fixStateSelect($this,
132 "address[$key][country_id]",
133 "address[$key][state_province_id]",
134 "address[$key][county_id]",
135 CRM_Utils_Array
::value('country_id', $value,
136 $config->defaultContactCountry
138 CRM_Utils_Array
::value('state_province_id', $value,
139 $config->defaultContactStateProvince
145 $defaults = array_merge($defaults, $domainDefaults);
150 * Function to actually build the form
155 public function buildQuickForm() {
157 $this->add('text', 'name', ts('Organization Name'), array('size' => 25), TRUE);
158 $this->add('text', 'description', ts('Description'), array('size' => 25));
160 $this->add('text', 'email_name', ts('FROM Name'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Email', 'email'), TRUE);
162 $this->add('text', 'email_address', ts('FROM Email Address'), CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Email', 'email'), TRUE);
163 $this->addRule('email_address', ts('Domain Email Address must use a valid email address format (e.g. \'info@example.org\').'), 'email');
165 //build location blocks.
166 CRM_Contact_Form_Location
::buildQuickForm($this);
168 $this->addButtons(array(
171 'name' => ts('Save'),
177 'name' => ts('Cancel'),
181 if ($this->_action
& CRM_Core_Action
::VIEW
) {
184 $this->assign('emailDomain', TRUE);
188 * Add local and global form rules
194 function addRules() {
195 $this->addFormRule(array('CRM_Contact_Form_Domain', 'formRule'));
199 * global validation rules for the form
201 * @param array $fields posted values of the form
203 * @return array list of errors to be posted back to the form
207 static function formRule($fields) {
209 // check for state/country mapping
210 CRM_Contact_Form_Edit_Address
::formRule($fields, $errors);
213 //as we use "fromName"<emailaddresss> format for domain email.
214 if (strpos($fields['email_name'], '"') !== FALSE) {
215 $errors['email_name'] = ts('Double quotes are not allow in from name.');
218 // Check for default from email address and organization (domain) name. Force them to change it.
219 if ($fields['email_address'] == 'info@EXAMPLE.ORG') {
220 $errors['email_address'] = ts('Please enter a valid default FROM email address for system-generated emails.');
222 if ($fields['name'] == 'Default Domain Name') {
223 $errors['name'] = ts('Please enter the name of the organization or entity which owns this CiviCRM site.');
226 return empty($errors) ?
TRUE : $errors;
230 * Process the form when submitted
235 public function postProcess() {
237 $params = $this->exportValues();
238 $params['entity_id'] = $this->_id
;
239 $params['entity_table'] = CRM_Core_BAO_Domain
::getTableName();
240 $domain = CRM_Core_BAO_Domain
::edit($params, $this->_id
);
242 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
245 $params['address'][1]['location_type_id'] = $defaultLocationType->id
;
246 $params['phone'][1]['location_type_id'] = $defaultLocationType->id
;
247 $params['email'][1]['location_type_id'] = $defaultLocationType->id
;
248 $params +
= array('contact_id' => $this->_contactId
);
249 $contactParams = array (
250 'sort_name' => $domain->name
,
251 'display_name' => $domain->name
,
252 'legal_name' => $domain->name
,
253 'organization_name' => $domain->name
,
254 'contact_id' => $this->_contactId
,
256 CRM_Contact_BAO_Contact
::add($contactParams);
257 $location = CRM_Core_BAO_Location
::create($params, TRUE);
260 CRM_Core_BAO_Domain
::edit($params, $this->_id
);
262 //set domain from email address, CRM-3552
263 $emailName = '"' . $params['email_name'] . '" <' . $params['email_address'] . '>';
265 $emailParams = array(
266 'label' => $emailName,
267 'description' => $params['description'],
272 $groupParams = array('name' => 'from_email_address');
274 //get the option value wt.
275 if ($this->_fromEmailId
) {
276 $action = $this->_action
;
277 $emailParams['weight'] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue', $this->_fromEmailId
, 'weight');
280 //add from email address.
281 $action = CRM_Core_Action
::ADD
;
282 $grpId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionGroup', 'from_email_address', 'id', 'name');
283 $fieldValues = array('option_group_id' => $grpId);
284 $emailParams['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_OptionValue', $fieldValues);
288 //reset default within domain.
289 $emailParams['reset_default_for'] = array('domain_id' => CRM_Core_Config
::domainID());
291 CRM_Core_OptionValue
::addOptionValue($emailParams, $groupParams, $action, $this->_fromEmailId
);
293 CRM_Core_Session
::setStatus(ts("Domain information for '%1' has been saved.", array(1 => $domain->name
)), ts('Saved'), 'success');
294 $session = CRM_Core_Session
::singleton();
295 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin', 'reset=1'));