Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
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 | /** | |
eceb18cc | 43 | * Build the form object. |
6a488035 | 44 | * |
355ba699 | 45 | * @return void |
6a488035 TO |
46 | */ |
47 | public function buildQuickForm() { | |
48 | parent::buildQuickForm(); | |
e2046b33 | 49 | $this->setPageTitle(ts('Mail Account')); |
6a488035 TO |
50 | |
51 | if ($this->_action & CRM_Core_Action::DELETE) { | |
52 | return; | |
53 | } | |
54 | ||
55 | $this->applyFilter('__ALL__', 'trim'); | |
56 | ||
57 | //get the attributes. | |
58 | $attributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_MailSettings'); | |
59 | ||
60 | //build setting form | |
61 | $this->add('text', 'name', ts('Name'), $attributes['name'], TRUE); | |
62 | ||
63 | $this->add('text', 'domain', ts('Email Domain'), $attributes['domain'], TRUE); | |
64 | $this->addRule('domain', ts('Email domain must use a valid internet domain format (e.g. \'example.org\').'), 'domain'); | |
65 | ||
66 | $this->add('text', 'localpart', ts('Localpart'), $attributes['localpart']); | |
67 | ||
68 | $this->add('text', 'return_path', ts('Return-Path'), $attributes['return_path']); | |
69 | $this->addRule('return_path', ts('Return-Path must use a valid email address format.'), 'email'); | |
70 | ||
71 | $this->add('select', 'protocol', | |
72 | ts('Protocol'), | |
cbf48754 | 73 | array('' => ts('- select -')) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_MailSettings', 'protocol'), |
6a488035 TO |
74 | TRUE |
75 | ); | |
76 | ||
77 | $this->add('text', 'server', ts('Server'), $attributes['server']); | |
78 | ||
79 | $this->add('text', 'username', ts('Username'), array('autocomplete' => 'off')); | |
80 | ||
81 | $this->add('password', 'password', ts('Password'), array('autocomplete' => 'off')); | |
82 | ||
83 | $this->add('text', 'source', ts('Source'), $attributes['source']); | |
84 | ||
85 | $this->add('checkbox', 'is_ssl', ts('Use SSL?')); | |
86 | ||
02fc859b | 87 | $usedfor = array( |
353ffa53 | 88 | 1 => ts('Bounce Processing'), |
6a488035 TO |
89 | 0 => ts('Email-to-Activity Processing'), |
90 | ); | |
91 | $this->add('select', 'is_default', ts('Used For?'), $usedfor); | |
92 | } | |
93 | ||
94 | /** | |
eceb18cc | 95 | * Add local and global form rules. |
6a488035 | 96 | * |
6a488035 TO |
97 | * |
98 | * @return void | |
99 | */ | |
00be9182 | 100 | public function addRules() { |
6a488035 TO |
101 | $this->addFormRule(array('CRM_Admin_Form_MailSettings', 'formRule')); |
102 | } | |
103 | ||
104 | /** | |
eceb18cc | 105 | * Global validation rules for the form. |
6a488035 | 106 | * |
5173bd95 TO |
107 | * @param array $fields |
108 | * Posted values of the form. | |
6a488035 | 109 | * |
a6c01b45 CW |
110 | * @return array |
111 | * list of errors to be posted back to the form | |
6a488035 | 112 | */ |
00be9182 | 113 | public static function formRule($fields) { |
6a488035 TO |
114 | $errors = array(); |
115 | // Check for default from email address and organization (domain) name. Force them to change it. | |
116 | if ($fields['domain'] == 'EXAMPLE.ORG') { | |
117 | $errors['domain'] = ts('Please enter a valid domain for this mailbox account (the part after @).'); | |
118 | } | |
119 | ||
120 | return empty($errors) ? TRUE : $errors; | |
121 | } | |
122 | ||
123 | /** | |
eceb18cc | 124 | * Process the form submission. |
6a488035 | 125 | * |
6a488035 | 126 | * |
355ba699 | 127 | * @return void |
6a488035 | 128 | */ |
00be9182 | 129 | public function postProcess() { |
6a488035 TO |
130 | if ($this->_action & CRM_Core_Action::DELETE) { |
131 | CRM_Core_BAO_MailSettings::deleteMailSettings($this->_id); | |
132 | CRM_Core_Session::setStatus("", ts('Mail Setting Deleted.'), "success"); | |
133 | return; | |
134 | } | |
135 | ||
136 | //get the submitted form values. | |
137 | $formValues = $this->controller->exportValues($this->_name); | |
138 | ||
139 | //form fields. | |
140 | $fields = array( | |
141 | 'name', | |
142 | 'domain', | |
143 | 'localpart', | |
144 | 'server', | |
145 | 'return_path', | |
146 | 'protocol', | |
147 | 'port', | |
148 | 'username', | |
149 | 'password', | |
150 | 'source', | |
151 | 'is_ssl', | |
152 | 'is_default', | |
153 | ); | |
154 | ||
155 | $params = array(); | |
156 | foreach ($fields as $f) { | |
157 | if (in_array($f, array( | |
353ffa53 | 158 | 'is_default', |
389bcebf | 159 | 'is_ssl', |
353ffa53 | 160 | ))) { |
6a488035 TO |
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 | } | |
e2046b33 | 186 | |
6a488035 | 187 | } |