Merge remote-tracking branch 'upstream/4.3' into 4.3-4.4-2013-11-11-10-44-51
[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 // check for state/country mapping
209 $errors = CRM_Contact_Form_Edit_Address::formRule($fields, CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullObject);
210 // $errors === TRUE means no errors from above formRule excution,
211 // so declaring $errors to array for futher processing
212 if ($errors === TRUE) {
213 $errors = array();
214 }
215
216 //fix for CRM-3552,
217 //as we use "fromName"<emailaddresss> format for domain email.
218 if (strpos($fields['email_name'], '"') !== FALSE) {
219 $errors['email_name'] = ts('Double quotes are not allow in from name.');
220 }
221
222 // Check for default from email address and organization (domain) name. Force them to change it.
223 if ($fields['email_address'] == 'info@EXAMPLE.ORG') {
224 $errors['email_address'] = ts('Please enter a valid default FROM email address for system-generated emails.');
225 }
226 if ($fields['name'] == 'Default Domain Name') {
227 $errors['name'] = ts('Please enter the name of the organization or entity which owns this CiviCRM site.');
228 }
229
230 return empty($errors) ? TRUE : $errors;
231 }
232
233 /**
234 * Process the form when submitted
235 *
236 * @return void
237 * @access public
238 */
239 public function postProcess() {
240 $params = array();
241 $params = $this->exportValues();
242 $params['entity_id'] = $this->_id;
243 $params['entity_table'] = CRM_Core_BAO_Domain::getTableName();
244 $domain = CRM_Core_BAO_Domain::edit($params, $this->_id);
245
246 $defaultLocationType = CRM_Core_BAO_LocationType::getDefault();
247
248 $location = array();
249 $params['address'][1]['location_type_id'] = $defaultLocationType->id;
250 $params['phone'][1]['location_type_id'] = $defaultLocationType->id;
251 $params['email'][1]['location_type_id'] = $defaultLocationType->id;
252 $params += array('contact_id' => $this->_contactId);
253 $contactParams = array (
254 'sort_name' => $domain->name,
255 'display_name' => $domain->name,
256 'legal_name' => $domain->name,
257 'organization_name' => $domain->name,
258 'contact_id' => $this->_contactId,
259 );
260 CRM_Contact_BAO_Contact::add($contactParams);
261 $location = CRM_Core_BAO_Location::create($params, TRUE);
262
263
264 CRM_Core_BAO_Domain::edit($params, $this->_id);
265
266 //set domain from email address, CRM-3552
267 $emailName = '"' . $params['email_name'] . '" <' . $params['email_address'] . '>';
268
269 $emailParams = array(
270 'label' => $emailName,
271 'description' => $params['description'],
272 'is_active' => 1,
273 'is_default' => 1,
274 );
275
276 $groupParams = array('name' => 'from_email_address');
277
278 //get the option value wt.
279 if ($this->_fromEmailId) {
280 $action = $this->_action;
281 $emailParams['weight'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $this->_fromEmailId, 'weight');
282 }
283 else {
284 //add from email address.
285 $action = CRM_Core_Action::ADD;
286 $grpId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'from_email_address', 'id', 'name');
287 $fieldValues = array('option_group_id' => $grpId);
288 $emailParams['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', $fieldValues);
289 }
290
291
292 //reset default within domain.
293 $emailParams['reset_default_for'] = array('domain_id' => CRM_Core_Config::domainID());
294
295 CRM_Core_OptionValue::addOptionValue($emailParams, $groupParams, $action, $this->_fromEmailId);
296
297 CRM_Core_Session::setStatus(ts("Domain information for '%1' has been saved.", array(1 => $domain->name)), ts('Saved'), 'success');
298 $session = CRM_Core_Session::singleton();
299 $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
300 }
301 }
302