allow more log
[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
5f68c8c8
TO
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
6a488035
TO
43 /**
44 * Return the DAO object containing to the default row of
45 * civicrm_mail_settings and cache it for further calls
46 *
07b3c7ff
EM
47 * @param bool $reset
48 *
16b10e64
CW
49 * @return CRM_Core_BAO_MailSettings
50 * DAO with the default mail settings set
6a488035 51 */
00be9182 52 public static function defaultDAO($reset = FALSE) {
be2fb01f 53 static $mailSettings = [];
07b3c7ff
EM
54 $domainID = CRM_Core_Config::domainID();
55 if (empty($mailSettings[$domainID]) || $reset) {
c301f76e 56 $dao = new self();
6a488035 57 $dao->is_default = 1;
353ffa53 58 $dao->domain_id = $domainID;
6a488035 59 $dao->find(TRUE);
07b3c7ff 60 $mailSettings[$domainID] = $dao;
6a488035 61 }
07b3c7ff 62 return $mailSettings[$domainID];
6a488035
TO
63 }
64
65 /**
fe482240 66 * Return the domain from the default set of settings.
6a488035 67 *
a6c01b45
CW
68 * @return string
69 * default domain
6a488035 70 */
00be9182 71 public static function defaultDomain() {
6a488035
TO
72 return self::defaultDAO()->domain;
73 }
74
75 /**
fe482240 76 * Return the localpart from the default set of settings.
6a488035 77 *
a6c01b45
CW
78 * @return string
79 * default localpart
6a488035 80 */
00be9182 81 public static function defaultLocalpart() {
6a488035
TO
82 return self::defaultDAO()->localpart;
83 }
84
85 /**
fe482240 86 * Return the return path from the default set of settings.
6a488035 87 *
a6c01b45
CW
88 * @return string
89 * default return path
6a488035 90 */
00be9182 91 public static function defaultReturnPath() {
6a488035
TO
92 return self::defaultDAO()->return_path;
93 }
94
95 /**
96 * Return the "include message ID" flag from the default set of settings.
97 *
c301f76e 98 * @return bool
a6c01b45 99 * default include message ID
6a488035 100 */
00be9182 101 public static function includeMessageId() {
89595c92 102 return Civi::settings()->get('include_message_id');
6a488035
TO
103 }
104
105 /**
4f940304 106 * Retrieve DB object and copy to defaults array.
6a488035 107 *
6a0b768e 108 * @param array $params
4f940304 109 * Array of criteria values.
6a0b768e 110 * @param array $defaults
4f940304 111 * Array to be populated with found values.
6a488035 112 *
4f940304
CW
113 * @return self|null
114 * The DAO object, if found.
115 *
116 * @deprecated
6a488035 117 */
4f940304
CW
118 public static function retrieve($params, &$defaults) {
119 return self::commonRetrieve(self::class, $params, $defaults);
6a488035
TO
120 }
121
122 /**
100fef9d 123 * Add new mail Settings.
6a488035 124 *
6a0b768e
TO
125 * @param array $params
126 * Reference array contains the values submitted by the form.
6a488035 127 *
6a488035 128 *
a2f24340 129 * @return CRM_Core_DAO_MailSettings
6a488035 130 */
00be9182 131 public static function add(&$params) {
6a488035
TO
132 $result = NULL;
133 if (empty($params)) {
134 return $result;
135 }
136
22e263ad 137 if (empty($params['id'])) {
e123fef4
EM
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 }
6a488035
TO
141
142 //handle is_default.
e123fef4 143 if (!empty($params['is_default'])) {
6a488035 144 $query = 'UPDATE civicrm_mail_settings SET is_default = 0 WHERE domain_id = %1';
be2fb01f 145 $queryParams = [1 => [CRM_Core_Config::domainID(), 'Integer']];
6a488035
TO
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 /**
fe482240 157 * Takes an associative array and creates a mail settings object.
6a488035 158 *
6a0b768e
TO
159 * @param array $params
160 * (reference ) an assoc array of name/value pairs.
6a488035 161 *
a2f24340 162 * @return CRM_Core_DAO_MailSettings|CRM_Core_Error
6a488035 163 */
00be9182 164 public static function create(&$params) {
6a488035
TO
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();
e123fef4 174 CRM_Core_BAO_MailSettings::defaultDAO(TRUE);
6a488035
TO
175 return $mailSettings;
176 }
177
178 /**
100fef9d 179 * Delete the mail settings.
6a488035 180 *
6a0b768e
TO
181 * @param int $id
182 * Mail settings id.
6a488035 183 *
77b97be7 184 * @return mixed|null
6a488035 185 */
00be9182 186 public static function deleteMailSettings($id) {
6a488035
TO
187 $results = NULL;
188 $transaction = new CRM_Core_Transaction();
189
353ffa53 190 $mailSettings = new CRM_Core_DAO_MailSettings();
6a488035 191 $mailSettings->id = $id;
353ffa53 192 $results = $mailSettings->delete();
6a488035
TO
193
194 $transaction->commit();
195
196 return $results;
197 }
96025800 198
6a488035 199}