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