Merge pull request #22496 from civicrm/5.46
[civicrm-core.git] / CRM / Core / BAO / MailSettings.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Core_BAO_MailSettings extends CRM_Core_DAO_MailSettings {
18
19 /**
20 * Get a list of setup-actions.
21 *
22 * @return array
23 * List of available actions. See description in the hook-docs.
24 * @see CRM_Utils_Hook::mailSetupActions()
25 */
26 public static function getSetupActions() {
27 $setupActions = [];
28 $setupActions['standard'] = [
29 'title' => ts('Standard Mail Account'),
30 'callback' => ['CRM_Core_BAO_MailSettings', 'setupStandardAccount'],
31 ];
32
33 CRM_Utils_Hook::mailSetupActions($setupActions);
34 return $setupActions;
35 }
36
37 public static function setupStandardAccount($setupAction) {
38 return [
39 'url' => CRM_Utils_System::url('civicrm/admin/mailSettings', 'action=add&reset=1', TRUE, NULL, FALSE),
40 ];
41 }
42
43 /**
44 * Return the DAO object containing to the default row of
45 * civicrm_mail_settings and cache it for further calls
46 *
47 * @param bool $reset
48 *
49 * @return CRM_Core_BAO_MailSettings
50 * DAO with the default mail settings set
51 */
52 public static function defaultDAO($reset = FALSE) {
53 static $mailSettings = [];
54 $domainID = CRM_Core_Config::domainID();
55 if (empty($mailSettings[$domainID]) || $reset) {
56 $dao = new self();
57 $dao->is_default = 1;
58 $dao->domain_id = $domainID;
59 $dao->find(TRUE);
60 $mailSettings[$domainID] = $dao;
61 }
62 return $mailSettings[$domainID];
63 }
64
65 /**
66 * Return the domain from the default set of settings.
67 *
68 * @return string
69 * default domain
70 */
71 public static function defaultDomain() {
72 return self::defaultDAO()->domain;
73 }
74
75 /**
76 * Return the localpart from the default set of settings.
77 *
78 * @return string
79 * default localpart
80 */
81 public static function defaultLocalpart() {
82 return self::defaultDAO()->localpart;
83 }
84
85 /**
86 * Return the return path from the default set of settings.
87 *
88 * @return string
89 * default return path
90 */
91 public static function defaultReturnPath() {
92 return self::defaultDAO()->return_path;
93 }
94
95 /**
96 * Return the "include message ID" flag from the default set of settings.
97 *
98 * @return bool
99 * default include message ID
100 */
101 public static function includeMessageId() {
102 return Civi::settings()->get('include_message_id');
103 }
104
105 /**
106 * Retrieve DB object based on input parameters.
107 *
108 * It also stores all the retrieved values in the default array.
109 *
110 * @param array $params
111 * (reference ) an assoc array of name/value pairs.
112 * @param array $defaults
113 * (reference ) an assoc array to hold the flattened values.
114 *
115 * @return CRM_Core_BAO_MailSettings
116 */
117 public static function retrieve(&$params, &$defaults) {
118 $mailSettings = new CRM_Core_DAO_MailSettings();
119 $mailSettings->copyValues($params);
120
121 $result = NULL;
122 if ($mailSettings->find(TRUE)) {
123 CRM_Core_DAO::storeValues($mailSettings, $defaults);
124 $result = $mailSettings;
125 }
126
127 return $result;
128 }
129
130 /**
131 * Add new mail Settings.
132 *
133 * @param array $params
134 * Reference array contains the values submitted by the form.
135 *
136 *
137 * @return object
138 */
139 public static function add(&$params) {
140 $result = NULL;
141 if (empty($params)) {
142 return $result;
143 }
144
145 if (empty($params['id'])) {
146 $params['is_ssl'] = CRM_Utils_Array::value('is_ssl', $params, FALSE);
147 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
148 }
149
150 //handle is_default.
151 if (!empty($params['is_default'])) {
152 $query = 'UPDATE civicrm_mail_settings SET is_default = 0 WHERE domain_id = %1';
153 $queryParams = [1 => [CRM_Core_Config::domainID(), 'Integer']];
154 CRM_Core_DAO::executeQuery($query, $queryParams);
155 }
156
157 $mailSettings = new CRM_Core_DAO_MailSettings();
158 $mailSettings->copyValues($params);
159 $result = $mailSettings->save();
160
161 return $result;
162 }
163
164 /**
165 * Takes an associative array and creates a mail settings object.
166 *
167 * @param array $params
168 * (reference ) an assoc array of name/value pairs.
169 *
170 * @return CRM_Core_BAO_MailSettings
171 */
172 public static function create(&$params) {
173 $transaction = new CRM_Core_Transaction();
174
175 $mailSettings = self::add($params);
176 if (is_a($mailSettings, 'CRM_Core_Error')) {
177 $mailSettings->rollback();
178 return $mailSettings;
179 }
180
181 $transaction->commit();
182 CRM_Core_BAO_MailSettings::defaultDAO(TRUE);
183 return $mailSettings;
184 }
185
186 /**
187 * Delete the mail settings.
188 *
189 * @param int $id
190 * Mail settings id.
191 *
192 * @return mixed|null
193 */
194 public static function deleteMailSettings($id) {
195 $results = NULL;
196 $transaction = new CRM_Core_Transaction();
197
198 $mailSettings = new CRM_Core_DAO_MailSettings();
199 $mailSettings->id = $id;
200 $results = $mailSettings->delete();
201
202 $transaction->commit();
203
204 return $results;
205 }
206
207 }