dbb1c5a02ad66b86a6a44ea6751f3dea68e5a1bf
[civicrm-core.git] / CRM / Contact / Form / Domain.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 * Default location type fields
64 *
65 * @var array
66 */
67 protected $_locationDefaults = array();
68
69 /**
70 * How many locationBlocks should we display?
71 *
72 * @var int
73 * @const
74 */
75 const LOCATION_BLOCKS = 1;
76
77 public 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'));
83
84 $this->_id = CRM_Core_Config::domainID();
85 $this->_action = CRM_Utils_Request::retrieve('action', 'String',
86 $this, FALSE, 'view'
87 );
88 //location blocks.
89 $location = new CRM_Contact_Form_Location();
90 $location->preProcess($this);
91 }
92
93 /**
94 * This virtual function is used to set the default values of
95 * various form elements
96 *
97 * @return array
98 * reference to the array of default values
99 *
100 * @access public
101 */
102 public function setDefaultValues() {
103 $defaults = array();
104 $params = array();
105
106 if (isset($this->_id)) {
107 $params['id'] = $this->_id;
108 CRM_Core_BAO_Domain::retrieve($params, $domainDefaults);
109 $this->_contactId = $domainDefaults['contact_id'];
110 //get the default domain from email address. fix CRM-3552
111 $optionValues = array();
112 $grpParams['name'] = 'from_email_address';
113 CRM_Core_OptionValue::getValues($grpParams, $optionValues);
114 foreach ($optionValues as $Id => $value) {
115 if ($value['is_default'] && $value['is_active']) {
116 $this->_fromEmailId = $Id;
117 $list = explode('"', $value['label']);
118 $domainDefaults['email_name'] = CRM_Utils_Array::value(1, $list);
119 $domainDefaults['email_address'] = CRM_Utils_Mail::pluckEmailFromHeader($value['label']);
120 break;
121 }
122 }
123
124 unset($params['id']);
125 $locParams = array('contact_id' => $domainDefaults['contact_id']);
126 $this->_locationDefaults = $defaults = CRM_Core_BAO_Location::getValues($locParams);
127
128 $config = CRM_Core_Config::singleton();
129 if (!isset($defaults['address'][1]['country_id'])) {
130 $defaults['address'][1]['country_id'] = $config->defaultContactCountry;
131 }
132
133 if (!isset($defaults['address'][1]['state_province_id'])) {
134 $defaults['address'][1]['state_province_id'] = $config->defaultContactStateProvince;
135 }
136
137 }
138 $defaults = array_merge($defaults, $domainDefaults);
139 return $defaults;
140 }
141
142 /**
143 * Build the form object
144 *
145 * @return void
146 */
147 public function buildQuickForm() {
148
149 $this->add('text', 'name', ts('Organization Name'), array('size' => 25), TRUE);
150 $this->add('text', 'description', ts('Description'), array('size' => 25));
151
152 $this->add('text', 'email_name', ts('FROM Name'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Email', 'email'), TRUE);
153
154 $this->add('text', 'email_address', ts('FROM Email Address'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Email', 'email'), TRUE);
155 $this->addRule('email_address', ts('Domain Email Address must use a valid email address format (e.g. \'info@example.org\').'), 'email');
156
157 //build location blocks.
158 CRM_Contact_Form_Location::buildQuickForm($this);
159
160 $this->addButtons(array(
161 array(
162 'type' => 'next',
163 'name' => ts('Save'),
164 'subName' => 'view',
165 'isDefault' => TRUE,
166 ),
167 array(
168 'type' => 'cancel',
169 'name' => ts('Cancel'),
170 ),
171 ));
172
173 if ($this->_action & CRM_Core_Action::VIEW) {
174 $this->freeze();
175 }
176 $this->assign('emailDomain', TRUE);
177 }
178
179 /**
180 * Add local and global form rules
181 *
182 *
183 * @return void
184 */
185 public function addRules() {
186 $this->addFormRule(array('CRM_Contact_Form_Domain', 'formRule'));
187 }
188
189 /**
190 * Global validation rules for the form
191 *
192 * @param array $fields
193 * Posted values of the form.
194 *
195 * @return array
196 * list of errors to be posted back to the form
197 */
198 public static function formRule($fields) {
199 // check for state/country mapping
200 $errors = CRM_Contact_Form_Edit_Address::formRule($fields, CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullObject);
201 // $errors === TRUE means no errors from above formRule excution,
202 // so declaring $errors to array for futher processing
203 if ($errors === TRUE) {
204 $errors = array();
205 }
206
207 //fix for CRM-3552,
208 //as we use "fromName"<emailaddresss> format for domain email.
209 if (strpos($fields['email_name'], '"') !== FALSE) {
210 $errors['email_name'] = ts('Double quotes are not allow in from name.');
211 }
212
213 // Check for default from email address and organization (domain) name. Force them to change it.
214 if ($fields['email_address'] == 'info@EXAMPLE.ORG') {
215 $errors['email_address'] = ts('Please enter a valid default FROM email address for system-generated emails.');
216 }
217 if ($fields['name'] == 'Default Domain Name') {
218 $errors['name'] = ts('Please enter the name of the organization or entity which owns this CiviCRM site.');
219 }
220
221 return empty($errors) ? TRUE : $errors;
222 }
223
224 /**
225 * Process the form when submitted
226 *
227 * @return void
228 */
229 public function postProcess() {
230 $params = $this->exportValues();
231 $params['entity_id'] = $this->_id;
232 $params['entity_table'] = CRM_Core_BAO_Domain::getTableName();
233 $domain = CRM_Core_BAO_Domain::edit($params, $this->_id);
234
235 $defaultLocationType = CRM_Core_BAO_LocationType::getDefault();
236
237 if (isset($this->_locationDefaults['address'][1]['location_type_id'])) {
238 $params['address'][1]['location_type_id'] = $this->_locationDefaults['address'][1]['location_type_id'];
239 }
240 else {
241 $params['address'][1]['location_type_id'] = $defaultLocationType->id;
242 }
243
244 if (isset($this->_locationDefaults['phone'][1]['location_type_id'])) {
245 $params['phone'][1]['location_type_id'] = $this->_locationDefaults['phone'][1]['location_type_id'];
246 }
247 else {
248 $params['phone'][1]['location_type_id'] = $defaultLocationType->id;
249 }
250
251 if (isset($this->_locationDefaults['email'][1]['location_type_id'])) {
252 $params['email'][1]['location_type_id'] = $this->_locationDefaults['email'][1]['location_type_id'];
253 }
254 else {
255 $params['email'][1]['location_type_id'] = $defaultLocationType->id;
256 }
257
258 $params += array('contact_id' => $this->_contactId);
259 $contactParams = array(
260 'sort_name' => $domain->name,
261 'display_name' => $domain->name,
262 'legal_name' => $domain->name,
263 'organization_name' => $domain->name,
264 'contact_id' => $this->_contactId,
265 'contact_type' => 'Organization',
266 );
267
268 if ($this->_contactId) {
269 $contactParams['contact_sub_type'] = CRM_Contact_BAO_Contact::getContactSubType($this->_contactId);
270 }
271
272 CRM_Contact_BAO_Contact::add($contactParams);
273 CRM_Core_BAO_Location::create($params, TRUE);
274
275 CRM_Core_BAO_Domain::edit($params, $this->_id);
276
277 //set domain from email address, CRM-3552
278 $emailName = '"' . $params['email_name'] . '" <' . $params['email_address'] . '>';
279
280 $emailParams = array(
281 'label' => $emailName,
282 'description' => $params['description'],
283 'is_active' => 1,
284 'is_default' => 1,
285 );
286
287 $groupParams = array('name' => 'from_email_address');
288
289 //get the option value wt.
290 if ($this->_fromEmailId) {
291 $action = $this->_action;
292 $emailParams['weight'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $this->_fromEmailId, 'weight');
293 }
294 else {
295 //add from email address.
296 $action = CRM_Core_Action::ADD;
297 $grpId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'from_email_address', 'id', 'name');
298 $fieldValues = array('option_group_id' => $grpId);
299 $emailParams['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', $fieldValues);
300 }
301
302
303 //reset default within domain.
304 $emailParams['reset_default_for'] = array('domain_id' => CRM_Core_Config::domainID());
305
306 CRM_Core_OptionValue::addOptionValue($emailParams, $groupParams, $action, $this->_fromEmailId);
307
308 CRM_Core_Session::setStatus(ts("Domain information for '%1' has been saved.", array(1 => $domain->name)), ts('Saved'), 'success');
309 $session = CRM_Core_Session::singleton();
310 $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin', 'reset=1'));
311 }
312 }