CRM-13577 - cleanup vedaconsulting patch and PR - 1821
[civicrm-core.git] / CRM / Contact / Form / Domain.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36 /**
37 * This class is to build the form for adding Group
38 */
39 class CRM_Contact_Form_Domain extends CRM_Core_Form {
40
41 /**
42 * the group id, used when editing a group
43 *
44 * @var int
45 */
46 protected $_id;
47
48 /**
49 * the contact_id of domain
50 *
51 * @var int
52 */
53 protected $_contactId;
54
55 /**
56 * default from email address option value id.
57 *
58 * @var int
59 */
60 protected $_fromEmailId = NULL;
61
62 /**
63 * how many locationBlocks should we display?
64 *
65 * @var int
66 * @const
67 */
68 CONST LOCATION_BLOCKS = 1;
69
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'));
76
77 $this->_id = CRM_Core_Config::domainID();
78 $this->_action = CRM_Utils_Request::retrieve('action', 'String',
79 $this, FALSE, 'view'
80 );
81 //location blocks.
82 $location = new CRM_Contact_Form_Location();
83 $location->preProcess($this);
84 }
85
86 /*
87 * This function sets the default values for the form.
88 * the default values are retrieved from the database
89 *
90 * @access public
91 * @return None
92 */
93 function setDefaultValues() {
94 $defaults = array();
95 $params = array();
96 $locParams = array();
97
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']);
112 break;
113 }
114 }
115
116 unset($params['id']);
117 $locParams = array('contact_id' => $domainDefaults['contact_id']);
118 $defaults = CRM_Core_BAO_Location::getValues($locParams);
119
120 $config = CRM_Core_Config::singleton();
121 if (!isset($defaults['address'][1]['country_id'])) {
122 $defaults['address'][1]['country_id'] = $config->defaultContactCountry;
123 }
124
125 if (!isset($defaults['address'][1]['state_province_id'])) {
126 $defaults['address'][1]['state_province_id'] = $config->defaultContactStateProvince;
127 }
128
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
137 ),
138 CRM_Utils_Array::value('state_province_id', $value,
139 $config->defaultContactStateProvince
140 )
141 );
142 }
143 }
144 }
145 $defaults = array_merge($defaults, $domainDefaults);
146 return $defaults;
147 }
148
149 /**
150 * Function to actually build the form
151 *
152 * @return None
153 * @access public
154 */
155 public function buildQuickForm() {
156
157 $this->add('text', 'name', ts('Organization Name'), array('size' => 25), TRUE);
158 $this->add('text', 'description', ts('Description'), array('size' => 25));
159
160 $this->add('text', 'email_name', ts('FROM Name'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Email', 'email'), TRUE);
161
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');
164
165 //build location blocks.
166 CRM_Contact_Form_Location::buildQuickForm($this);
167
168 $this->addButtons(array(
169 array(
170 'type' => 'next',
171 'name' => ts('Save'),
172 'subName' => 'view',
173 'isDefault' => TRUE,
174 ),
175 array(
176 'type' => 'cancel',
177 'name' => ts('Cancel'),
178 ),
179 ));
180
181 if ($this->_action & CRM_Core_Action::VIEW) {
182 $this->freeze();
183 }
184 $this->assign('emailDomain', TRUE);
185 }
186
187 /**
188 * Add local and global form rules
189 *
190 * @access protected
191 *
192 * @return void
193 */
194 function addRules() {
195 $this->addFormRule(array('CRM_Contact_Form_Domain', 'formRule'));
196 }
197
198 /**
199 * global validation rules for the form
200 *
201 * @param array $fields posted values of the form
202 *
203 * @return array list of errors to be posted back to the form
204 * @static
205 * @access public
206 */
207 static function formRule($fields) {
208 $errors = array();
209 // check for state/country mapping
210 CRM_Contact_Form_Edit_Address::formRule($fields, $errors);
211
212 //fix for CRM-3552,
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.');
216 }
217
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.');
221 }
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.');
224 }
225
226 return empty($errors) ? TRUE : $errors;
227 }
228
229 /**
230 * Process the form when submitted
231 *
232 * @return void
233 * @access public
234 */
235 public function postProcess() {
236 $params = array();
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);
241
242 $defaultLocationType = CRM_Core_BAO_LocationType::getDefault();
243
244 $location = array();
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,
255 );
256 CRM_Contact_BAO_Contact::add($contactParams);
257 $location = CRM_Core_BAO_Location::create($params, TRUE);
258
259
260 CRM_Core_BAO_Domain::edit($params, $this->_id);
261
262 //set domain from email address, CRM-3552
263 $emailName = '"' . $params['email_name'] . '" <' . $params['email_address'] . '>';
264
265 $emailParams = array(
266 'label' => $emailName,
267 'description' => $params['description'],
268 'is_active' => 1,
269 'is_default' => 1,
270 );
271
272 $groupParams = array('name' => 'from_email_address');
273
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');
278 }
279 else {
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);
285 }
286
287
288 //reset default within domain.
289 $emailParams['reset_default_for'] = array('domain_id' => CRM_Core_Config::domainID());
290
291 CRM_Core_OptionValue::addOptionValue($emailParams, $groupParams, $action, $this->_fromEmailId);
292
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'));
296 }
297 }
298