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