CRM_Admin_Form_Preferences - Fix loading of settings
[civicrm-core.git] / CRM / Admin / Form / MailSettings.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 */
33
34/**
35 * This class handles mail account settings.
36 *
37 */
38class 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);
88 }
89
90 /**
eceb18cc 91 * Add local and global form rules.
6a488035 92 */
00be9182 93 public function addRules() {
6a488035
TO
94 $this->addFormRule(array('CRM_Admin_Form_MailSettings', 'formRule'));
95 }
96
97 /**
eceb18cc 98 * Global validation rules for the form.
6a488035 99 *
5173bd95
TO
100 * @param array $fields
101 * Posted values of the form.
6a488035 102 *
a6c01b45
CW
103 * @return array
104 * list of errors to be posted back to the form
6a488035 105 */
00be9182 106 public static function formRule($fields) {
6a488035
TO
107 $errors = array();
108 // Check for default from email address and organization (domain) name. Force them to change it.
109 if ($fields['domain'] == 'EXAMPLE.ORG') {
110 $errors['domain'] = ts('Please enter a valid domain for this mailbox account (the part after @).');
111 }
112
113 return empty($errors) ? TRUE : $errors;
114 }
115
116 /**
eceb18cc 117 * Process the form submission.
6a488035 118 */
00be9182 119 public function postProcess() {
6a488035
TO
120 if ($this->_action & CRM_Core_Action::DELETE) {
121 CRM_Core_BAO_MailSettings::deleteMailSettings($this->_id);
122 CRM_Core_Session::setStatus("", ts('Mail Setting Deleted.'), "success");
123 return;
124 }
125
126 //get the submitted form values.
127 $formValues = $this->controller->exportValues($this->_name);
128
129 //form fields.
130 $fields = array(
131 'name',
132 'domain',
133 'localpart',
134 'server',
135 'return_path',
136 'protocol',
137 'port',
138 'username',
139 'password',
140 'source',
141 'is_ssl',
142 'is_default',
143 );
144
145 $params = array();
146 foreach ($fields as $f) {
147 if (in_array($f, array(
353ffa53 148 'is_default',
389bcebf 149 'is_ssl',
353ffa53 150 ))) {
6a488035
TO
151 $params[$f] = CRM_Utils_Array::value($f, $formValues, FALSE);
152 }
153 else {
154 $params[$f] = CRM_Utils_Array::value($f, $formValues);
155 }
156 }
157
158 $params['domain_id'] = CRM_Core_Config::domainID();
159
160 // assign id only in update mode
161 $status = ts('Your New Email Settings have been saved.');
162 if ($this->_action & CRM_Core_Action::UPDATE) {
163 $params['id'] = $this->_id;
164 $status = ts('Your Email Settings have been updated.');
165 }
166
167 $mailSettings = CRM_Core_BAO_MailSettings::create($params);
168
169 if ($mailSettings->id) {
170 CRM_Core_Session::setStatus($status, ts("Saved"), "success");
171 }
172 else {
173 CRM_Core_Session::setStatus("", ts('Changes Not Saved.'), "info");
174 }
175 }
e2046b33 176
6a488035 177}