Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * This class handles mail account settings. | |
20 | * | |
21 | */ | |
22 | class CRM_Admin_Form_MailSettings extends CRM_Admin_Form { | |
23 | ||
e3f6a1e9 TO |
24 | protected $_testButtonName; |
25 | ||
88aae6d4 A |
26 | /** |
27 | * @var bool | |
28 | */ | |
29 | public $submitOnce = TRUE; | |
30 | ||
6a488035 | 31 | /** |
eceb18cc | 32 | * Build the form object. |
6a488035 TO |
33 | */ |
34 | public function buildQuickForm() { | |
35 | parent::buildQuickForm(); | |
e2046b33 | 36 | $this->setPageTitle(ts('Mail Account')); |
6a488035 TO |
37 | |
38 | if ($this->_action & CRM_Core_Action::DELETE) { | |
39 | return; | |
40 | } | |
41 | ||
a6bd60f3 | 42 | $this->_testButtonName = $this->getButtonName('upload', 'test'); |
e3f6a1e9 TO |
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 | ||
6a488035 TO |
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'), | |
be2fb01f | 70 | ['' => ts('- select -')] + CRM_Core_PseudoConstant::get('CRM_Core_DAO_MailSettings', 'protocol'), |
6a488035 TO |
71 | TRUE |
72 | ); | |
73 | ||
74 | $this->add('text', 'server', ts('Server'), $attributes['server']); | |
75 | ||
be2fb01f | 76 | $this->add('text', 'username', ts('Username'), ['autocomplete' => 'off']); |
6a488035 | 77 | |
be2fb01f | 78 | $this->add('password', 'password', ts('Password'), ['autocomplete' => 'off']); |
6a488035 TO |
79 | |
80 | $this->add('text', 'source', ts('Source'), $attributes['source']); | |
81 | ||
82 | $this->add('checkbox', 'is_ssl', ts('Use SSL?')); | |
83 | ||
be2fb01f | 84 | $usedfor = [ |
353ffa53 | 85 | 1 => ts('Bounce Processing'), |
6a488035 | 86 | 0 => ts('Email-to-Activity Processing'), |
be2fb01f | 87 | ]; |
6a488035 | 88 | $this->add('select', 'is_default', ts('Used For?'), $usedfor); |
be2fb01f | 89 | $this->addField('activity_status', ['placeholder' => FALSE]); |
993a642c | 90 | |
f945f0c4 | 91 | $this->add('checkbox', 'is_non_case_email_skipped', ts('Skip emails which do not have a Case ID or Case hash')); |
993a642c | 92 | $this->add('checkbox', 'is_contact_creation_disabled_if_no_match', ts('Do not create new contacts when filing emails')); |
6a488035 TO |
93 | } |
94 | ||
95 | /** | |
eceb18cc | 96 | * Add local and global form rules. |
6a488035 | 97 | */ |
00be9182 | 98 | public function addRules() { |
54438400 | 99 | $this->addFormRule(['CRM_Admin_Form_MailSettings', 'formRule'], $this); |
6a488035 TO |
100 | } |
101 | ||
040073c9 CW |
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 | ||
6a488035 | 122 | /** |
eceb18cc | 123 | * Global validation rules for the form. |
6a488035 | 124 | * |
5173bd95 TO |
125 | * @param array $fields |
126 | * Posted values of the form. | |
54438400 | 127 | * @param array $files |
128 | * Not used here. | |
129 | * @param CRM_Core_Form $form | |
130 | * This form. | |
6a488035 | 131 | * |
a6c01b45 CW |
132 | * @return array |
133 | * list of errors to be posted back to the form | |
6a488035 | 134 | */ |
54438400 | 135 | public static function formRule($fields, $files, $form) { |
be2fb01f | 136 | $errors = []; |
54438400 | 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 | } | |
6a488035 TO |
142 | } |
143 | ||
144 | return empty($errors) ? TRUE : $errors; | |
145 | } | |
146 | ||
147 | /** | |
eceb18cc | 148 | * Process the form submission. |
6a488035 | 149 | */ |
00be9182 | 150 | public function postProcess() { |
6a488035 TO |
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. | |
be2fb01f | 161 | $fields = [ |
6a488035 TO |
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', | |
040073c9 | 174 | 'activity_status', |
f945f0c4 | 175 | 'is_non_case_email_skipped', |
993a642c | 176 | 'is_contact_creation_disabled_if_no_match', |
be2fb01f | 177 | ]; |
6a488035 | 178 | |
be2fb01f | 179 | $params = []; |
6a488035 | 180 | foreach ($fields as $f) { |
be2fb01f | 181 | if (in_array($f, [ |
353ffa53 | 182 | 'is_default', |
389bcebf | 183 | 'is_ssl', |
f945f0c4 | 184 | 'is_non_case_email_skipped', |
993a642c | 185 | 'is_contact_creation_disabled_if_no_match', |
be2fb01f | 186 | ])) { |
6a488035 TO |
187 | $params[$f] = CRM_Utils_Array::value($f, $formValues, FALSE); |
188 | } | |
189 | else { | |
9c1bc317 | 190 | $params[$f] = $formValues[$f] ?? NULL; |
6a488035 TO |
191 | } |
192 | } | |
193 | ||
194 | $params['domain_id'] = CRM_Core_Config::domainID(); | |
195 | ||
196 | // assign id only in update mode | |
f945f0c4 | 197 | $status = ts('Your New Email Settings have been saved.'); |
6a488035 TO |
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 | } | |
e3f6a1e9 TO |
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 | } | |
6a488035 | 219 | } |
e2046b33 | 220 | |
6a488035 | 221 | } |