Merge pull request #22484 from civicrm/5.46
[civicrm-core.git] / CRM / Admin / Form / MailSettings.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class handles mail account settings.
20 *
21 */
22 class CRM_Admin_Form_MailSettings extends CRM_Admin_Form {
23
24 protected $_testButtonName;
25
26 /**
27 * @var bool
28 */
29 public $submitOnce = TRUE;
30
31 /**
32 * Build the form object.
33 */
34 public function buildQuickForm() {
35 parent::buildQuickForm();
36 $this->setPageTitle(ts('Mail Account'));
37
38 if ($this->_action & CRM_Core_Action::DELETE) {
39 return;
40 }
41
42 $this->_testButtonName = $this->getButtonName('upload', 'test');
43 $buttons = $this->getElement('buttons')->getElements();
44 $buttons[] = $this->createElement(
45 'xbutton',
46 $this->_testButtonName,
47 CRM_Core_Page::crmIcon('fa-chain') . ' ' . ts('Save & Test'),
48 ['type' => 'submit', 'class' => 'crm-button']
49 );
50 $this->getElement('buttons')->setElements($buttons);
51
52 $this->applyFilter('__ALL__', 'trim');
53
54 //get the attributes.
55 $attributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_MailSettings');
56
57 //build setting form
58 $this->add('text', 'name', ts('Name'), $attributes['name'], TRUE);
59
60 $this->add('text', 'domain', ts('Email Domain'), $attributes['domain'], TRUE);
61 $this->addRule('domain', ts('Email domain must use a valid internet domain format (e.g. \'example.org\').'), 'domain');
62
63 $this->add('text', 'localpart', ts('Localpart'), $attributes['localpart']);
64
65 $this->add('text', 'return_path', ts('Return-Path'), $attributes['return_path']);
66 $this->addRule('return_path', ts('Return-Path must use a valid email address format.'), 'email');
67
68 $this->add('select', 'protocol',
69 ts('Protocol'),
70 ['' => ts('- select -')] + CRM_Core_PseudoConstant::get('CRM_Core_DAO_MailSettings', 'protocol'),
71 TRUE
72 );
73
74 $this->add('text', 'server', ts('Server'), $attributes['server']);
75
76 $this->add('text', 'username', ts('Username'), ['autocomplete' => 'off']);
77
78 $this->add('password', 'password', ts('Password'), ['autocomplete' => 'off']);
79
80 $this->add('text', 'source', ts('Source'), $attributes['source']);
81
82 $this->add('checkbox', 'is_ssl', ts('Use SSL?'));
83
84 $usedfor = [
85 1 => ts('Bounce Processing'),
86 0 => ts('Email-to-Activity Processing'),
87 ];
88 $this->add('select', 'is_default', ts('Used For?'), $usedfor);
89 $this->addField('activity_status', ['placeholder' => FALSE]);
90
91 $this->add('checkbox', 'is_non_case_email_skipped', ts('Skip emails which do not have a Case ID or Case hash'));
92 $this->add('checkbox', 'is_contact_creation_disabled_if_no_match', ts('Do not create new contacts when filing emails'));
93 }
94
95 /**
96 * Add local and global form rules.
97 */
98 public function addRules() {
99 $this->addFormRule(['CRM_Admin_Form_MailSettings', 'formRule'], $this);
100 }
101
102 public function getDefaultEntity() {
103 return 'MailSettings';
104 }
105
106 /**
107 * Add local and global form rules.
108 */
109 public function setDefaultValues() {
110 $defaults = parent::setDefaultValues();
111
112 // Set activity status to "Completed" by default.
113 if ($this->_action != CRM_Core_Action::DELETE &&
114 (!$this->_id || !CRM_Core_DAO::getFieldValue('CRM_Core_BAO_MailSettings', $this->_id, 'activity_status'))
115 ) {
116 $defaults['activity_status'] = 'Completed';
117 }
118
119 return $defaults;
120 }
121
122 /**
123 * Global validation rules for the form.
124 *
125 * @param array $fields
126 * Posted values of the form.
127 * @param array $files
128 * Not used here.
129 * @param CRM_Core_Form $form
130 * This form.
131 *
132 * @return array
133 * list of errors to be posted back to the form
134 */
135 public static function formRule($fields, $files, $form) {
136 $errors = [];
137 if ($form->_action != CRM_Core_Action::DELETE) {
138 // Check for default from email address and organization (domain) name. Force them to change it.
139 if ($fields['domain'] == 'EXAMPLE.ORG') {
140 $errors['domain'] = ts('Please enter a valid domain for this mailbox account (the part after @).');
141 }
142 }
143
144 return empty($errors) ? TRUE : $errors;
145 }
146
147 /**
148 * Process the form submission.
149 */
150 public function postProcess() {
151 if ($this->_action & CRM_Core_Action::DELETE) {
152 CRM_Core_BAO_MailSettings::deleteMailSettings($this->_id);
153 CRM_Core_Session::setStatus("", ts('Mail Setting Deleted.'), "success");
154 return;
155 }
156
157 //get the submitted form values.
158 $formValues = $this->controller->exportValues($this->_name);
159
160 //form fields.
161 $fields = [
162 'name',
163 'domain',
164 'localpart',
165 'server',
166 'return_path',
167 'protocol',
168 'port',
169 'username',
170 'password',
171 'source',
172 'is_ssl',
173 'is_default',
174 'activity_status',
175 'is_non_case_email_skipped',
176 'is_contact_creation_disabled_if_no_match',
177 ];
178
179 $params = [];
180 foreach ($fields as $f) {
181 if (in_array($f, [
182 'is_default',
183 'is_ssl',
184 'is_non_case_email_skipped',
185 'is_contact_creation_disabled_if_no_match',
186 ])) {
187 $params[$f] = CRM_Utils_Array::value($f, $formValues, FALSE);
188 }
189 else {
190 $params[$f] = $formValues[$f] ?? NULL;
191 }
192 }
193
194 $params['domain_id'] = CRM_Core_Config::domainID();
195
196 // assign id only in update mode
197 $status = ts('Your New Email Settings have been saved.');
198 if ($this->_action & CRM_Core_Action::UPDATE) {
199 $params['id'] = $this->_id;
200 $status = ts('Your Email Settings have been updated.');
201 }
202
203 $mailSettings = CRM_Core_BAO_MailSettings::create($params);
204
205 if ($mailSettings->id) {
206 CRM_Core_Session::setStatus($status, ts("Saved"), "success");
207 }
208 else {
209 CRM_Core_Session::setStatus("", ts('Changes Not Saved.'), "info");
210 }
211
212 if ($this->controller->getButtonName() == $this->_testButtonName) {
213 $test = civicrm_api4('MailSettings', 'testConnection', [
214 'where' => [['id', '=', $mailSettings->id]],
215 ])->single();
216 CRM_Core_Session::setStatus($test['details'], $test['title'],
217 $test['error'] ? 'error' : 'success');
218 }
219 }
220
221 }