Merge pull request #16077 from civicrm/5.21
[civicrm-core.git] / CRM / Admin / Form / MailSettings.php
CommitLineData
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 */
22class CRM_Admin_Form_MailSettings extends CRM_Admin_Form {
23
24 /**
eceb18cc 25 * Build the form object.
6a488035
TO
26 */
27 public function buildQuickForm() {
28 parent::buildQuickForm();
e2046b33 29 $this->setPageTitle(ts('Mail Account'));
6a488035
TO
30
31 if ($this->_action & CRM_Core_Action::DELETE) {
32 return;
33 }
34
35 $this->applyFilter('__ALL__', 'trim');
36
37 //get the attributes.
38 $attributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_MailSettings');
39
40 //build setting form
41 $this->add('text', 'name', ts('Name'), $attributes['name'], TRUE);
42
43 $this->add('text', 'domain', ts('Email Domain'), $attributes['domain'], TRUE);
44 $this->addRule('domain', ts('Email domain must use a valid internet domain format (e.g. \'example.org\').'), 'domain');
45
46 $this->add('text', 'localpart', ts('Localpart'), $attributes['localpart']);
47
48 $this->add('text', 'return_path', ts('Return-Path'), $attributes['return_path']);
49 $this->addRule('return_path', ts('Return-Path must use a valid email address format.'), 'email');
50
51 $this->add('select', 'protocol',
52 ts('Protocol'),
be2fb01f 53 ['' => ts('- select -')] + CRM_Core_PseudoConstant::get('CRM_Core_DAO_MailSettings', 'protocol'),
6a488035
TO
54 TRUE
55 );
56
57 $this->add('text', 'server', ts('Server'), $attributes['server']);
58
be2fb01f 59 $this->add('text', 'username', ts('Username'), ['autocomplete' => 'off']);
6a488035 60
be2fb01f 61 $this->add('password', 'password', ts('Password'), ['autocomplete' => 'off']);
6a488035
TO
62
63 $this->add('text', 'source', ts('Source'), $attributes['source']);
64
65 $this->add('checkbox', 'is_ssl', ts('Use SSL?'));
66
be2fb01f 67 $usedfor = [
353ffa53 68 1 => ts('Bounce Processing'),
6a488035 69 0 => ts('Email-to-Activity Processing'),
be2fb01f 70 ];
6a488035 71 $this->add('select', 'is_default', ts('Used For?'), $usedfor);
be2fb01f 72 $this->addField('activity_status', ['placeholder' => FALSE]);
6a488035
TO
73 }
74
75 /**
eceb18cc 76 * Add local and global form rules.
6a488035 77 */
00be9182 78 public function addRules() {
be2fb01f 79 $this->addFormRule(['CRM_Admin_Form_MailSettings', 'formRule']);
6a488035
TO
80 }
81
040073c9
CW
82 public function getDefaultEntity() {
83 return 'MailSettings';
84 }
85
86 /**
87 * Add local and global form rules.
88 */
89 public function setDefaultValues() {
90 $defaults = parent::setDefaultValues();
91
92 // Set activity status to "Completed" by default.
93 if ($this->_action != CRM_Core_Action::DELETE &&
94 (!$this->_id || !CRM_Core_DAO::getFieldValue('CRM_Core_BAO_MailSettings', $this->_id, 'activity_status'))
95 ) {
96 $defaults['activity_status'] = 'Completed';
97 }
98
99 return $defaults;
100 }
101
6a488035 102 /**
eceb18cc 103 * Global validation rules for the form.
6a488035 104 *
5173bd95
TO
105 * @param array $fields
106 * Posted values of the form.
6a488035 107 *
a6c01b45
CW
108 * @return array
109 * list of errors to be posted back to the form
6a488035 110 */
00be9182 111 public static function formRule($fields) {
be2fb01f 112 $errors = [];
6a488035
TO
113 // Check for default from email address and organization (domain) name. Force them to change it.
114 if ($fields['domain'] == 'EXAMPLE.ORG') {
115 $errors['domain'] = ts('Please enter a valid domain for this mailbox account (the part after @).');
116 }
117
118 return empty($errors) ? TRUE : $errors;
119 }
120
121 /**
eceb18cc 122 * Process the form submission.
6a488035 123 */
00be9182 124 public function postProcess() {
6a488035
TO
125 if ($this->_action & CRM_Core_Action::DELETE) {
126 CRM_Core_BAO_MailSettings::deleteMailSettings($this->_id);
127 CRM_Core_Session::setStatus("", ts('Mail Setting Deleted.'), "success");
128 return;
129 }
130
131 //get the submitted form values.
132 $formValues = $this->controller->exportValues($this->_name);
133
134 //form fields.
be2fb01f 135 $fields = [
6a488035
TO
136 'name',
137 'domain',
138 'localpart',
139 'server',
140 'return_path',
141 'protocol',
142 'port',
143 'username',
144 'password',
145 'source',
146 'is_ssl',
147 'is_default',
040073c9 148 'activity_status',
be2fb01f 149 ];
6a488035 150
be2fb01f 151 $params = [];
6a488035 152 foreach ($fields as $f) {
be2fb01f 153 if (in_array($f, [
353ffa53 154 'is_default',
389bcebf 155 'is_ssl',
be2fb01f 156 ])) {
6a488035
TO
157 $params[$f] = CRM_Utils_Array::value($f, $formValues, FALSE);
158 }
159 else {
160 $params[$f] = CRM_Utils_Array::value($f, $formValues);
161 }
162 }
163
164 $params['domain_id'] = CRM_Core_Config::domainID();
165
166 // assign id only in update mode
167 $status = ts('Your New Email Settings have been saved.');
168 if ($this->_action & CRM_Core_Action::UPDATE) {
169 $params['id'] = $this->_id;
170 $status = ts('Your Email Settings have been updated.');
171 }
172
173 $mailSettings = CRM_Core_BAO_MailSettings::create($params);
174
175 if ($mailSettings->id) {
176 CRM_Core_Session::setStatus($status, ts("Saved"), "success");
177 }
178 else {
179 CRM_Core_Session::setStatus("", ts('Changes Not Saved.'), "info");
180 }
181 }
e2046b33 182
6a488035 183}