Merge pull request #23232 from braders/nodefaults-tab-links
[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 and copy to defaults array.
107 *
108 * @param array $params
109 * Array of criteria values.
110 * @param array $defaults
111 * Array to be populated with found values.
112 *
113 * @return self|null
114 * The DAO object, if found.
115 *
116 * @deprecated
117 */
118 public static function retrieve($params, &$defaults) {
119 return self::commonRetrieve(self::class, $params, $defaults);
120 }
121
122 /**
123 * Add new mail Settings.
124 *
125 * @param array $params
126 * Reference array contains the values submitted by the form.
127 *
128 *
129 * @return CRM_Core_DAO_MailSettings
130 */
131 public static function add(&$params) {
132 $result = NULL;
133 if (empty($params)) {
134 return $result;
135 }
136
137 if (empty($params['id'])) {
138 $params['is_ssl'] = CRM_Utils_Array::value('is_ssl', $params, FALSE);
139 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
140 }
141
142 //handle is_default.
143 if (!empty($params['is_default'])) {
144 $query = 'UPDATE civicrm_mail_settings SET is_default = 0 WHERE domain_id = %1';
145 $queryParams = [1 => [CRM_Core_Config::domainID(), 'Integer']];
146 CRM_Core_DAO::executeQuery($query, $queryParams);
147 }
148
149 $mailSettings = new CRM_Core_DAO_MailSettings();
150 $mailSettings->copyValues($params);
151 $result = $mailSettings->save();
152
153 return $result;
154 }
155
156 /**
157 * Takes an associative array and creates a mail settings object.
158 *
159 * @param array $params
160 * (reference ) an assoc array of name/value pairs.
161 *
162 * @return CRM_Core_DAO_MailSettings|CRM_Core_Error
163 */
164 public static function create(&$params) {
165 $transaction = new CRM_Core_Transaction();
166
167 $mailSettings = self::add($params);
168 if (is_a($mailSettings, 'CRM_Core_Error')) {
169 $mailSettings->rollback();
170 return $mailSettings;
171 }
172
173 $transaction->commit();
174 CRM_Core_BAO_MailSettings::defaultDAO(TRUE);
175 return $mailSettings;
176 }
177
178 /**
179 * Delete the mail settings.
180 *
181 * @param int $id
182 * Mail settings id.
183 *
184 * @return mixed|null
185 */
186 public static function deleteMailSettings($id) {
187 $results = NULL;
188 $transaction = new CRM_Core_Transaction();
189
190 $mailSettings = new CRM_Core_DAO_MailSettings();
191 $mailSettings->id = $id;
192 $results = $mailSettings->delete();
193
194 $transaction->commit();
195
196 return $results;
197 }
198
199 }