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