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