Merge pull request #18879 from colemanw/fixDAOStyle
[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
26 /**
27 * Return the DAO object containing to the default row of
28 * civicrm_mail_settings and cache it for further calls
29 *
07b3c7ff
EM
30 * @param bool $reset
31 *
16b10e64
CW
32 * @return CRM_Core_BAO_MailSettings
33 * DAO with the default mail settings set
6a488035 34 */
00be9182 35 public static function defaultDAO($reset = FALSE) {
be2fb01f 36 static $mailSettings = [];
07b3c7ff
EM
37 $domainID = CRM_Core_Config::domainID();
38 if (empty($mailSettings[$domainID]) || $reset) {
c301f76e 39 $dao = new self();
6a488035 40 $dao->is_default = 1;
353ffa53 41 $dao->domain_id = $domainID;
6a488035 42 $dao->find(TRUE);
07b3c7ff 43 $mailSettings[$domainID] = $dao;
6a488035 44 }
07b3c7ff 45 return $mailSettings[$domainID];
6a488035
TO
46 }
47
48 /**
fe482240 49 * Return the domain from the default set of settings.
6a488035 50 *
a6c01b45
CW
51 * @return string
52 * default domain
6a488035 53 */
00be9182 54 public static function defaultDomain() {
6a488035
TO
55 return self::defaultDAO()->domain;
56 }
57
58 /**
fe482240 59 * Return the localpart from the default set of settings.
6a488035 60 *
a6c01b45
CW
61 * @return string
62 * default localpart
6a488035 63 */
00be9182 64 public static function defaultLocalpart() {
6a488035
TO
65 return self::defaultDAO()->localpart;
66 }
67
68 /**
fe482240 69 * Return the return path from the default set of settings.
6a488035 70 *
a6c01b45
CW
71 * @return string
72 * default return path
6a488035 73 */
00be9182 74 public static function defaultReturnPath() {
6a488035
TO
75 return self::defaultDAO()->return_path;
76 }
77
78 /**
79 * Return the "include message ID" flag from the default set of settings.
80 *
c301f76e 81 * @return bool
a6c01b45 82 * default include message ID
6a488035 83 */
00be9182 84 public static function includeMessageId() {
89595c92 85 return Civi::settings()->get('include_message_id');
6a488035
TO
86 }
87
88 /**
fe482240
EM
89 * Retrieve DB object based on input parameters.
90 *
91 * It also stores all the retrieved values in the default array.
6a488035 92 *
6a0b768e
TO
93 * @param array $params
94 * (reference ) an assoc array of name/value pairs.
95 * @param array $defaults
96 * (reference ) an assoc array to hold the flattened values.
6a488035 97 *
16b10e64 98 * @return CRM_Core_BAO_MailSettings
6a488035 99 */
00be9182 100 public static function retrieve(&$params, &$defaults) {
6a488035
TO
101 $mailSettings = new CRM_Core_DAO_MailSettings();
102 $mailSettings->copyValues($params);
103
104 $result = NULL;
105 if ($mailSettings->find(TRUE)) {
106 CRM_Core_DAO::storeValues($mailSettings, $defaults);
107 $result = $mailSettings;
108 }
109
110 return $result;
111 }
112
113 /**
100fef9d 114 * Add new mail Settings.
6a488035 115 *
6a0b768e
TO
116 * @param array $params
117 * Reference array contains the values submitted by the form.
6a488035 118 *
6a488035
TO
119 *
120 * @return object
121 */
00be9182 122 public static function add(&$params) {
6a488035
TO
123 $result = NULL;
124 if (empty($params)) {
125 return $result;
126 }
127
22e263ad 128 if (empty($params['id'])) {
e123fef4
EM
129 $params['is_ssl'] = CRM_Utils_Array::value('is_ssl', $params, FALSE);
130 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
131 }
6a488035
TO
132
133 //handle is_default.
e123fef4 134 if (!empty($params['is_default'])) {
6a488035 135 $query = 'UPDATE civicrm_mail_settings SET is_default = 0 WHERE domain_id = %1';
be2fb01f 136 $queryParams = [1 => [CRM_Core_Config::domainID(), 'Integer']];
6a488035
TO
137 CRM_Core_DAO::executeQuery($query, $queryParams);
138 }
139
140 $mailSettings = new CRM_Core_DAO_MailSettings();
141 $mailSettings->copyValues($params);
142 $result = $mailSettings->save();
143
144 return $result;
145 }
146
147 /**
fe482240 148 * Takes an associative array and creates a mail settings object.
6a488035 149 *
6a0b768e
TO
150 * @param array $params
151 * (reference ) an assoc array of name/value pairs.
6a488035 152 *
16b10e64 153 * @return CRM_Core_BAO_MailSettings
6a488035 154 */
00be9182 155 public static function create(&$params) {
6a488035
TO
156 $transaction = new CRM_Core_Transaction();
157
158 $mailSettings = self::add($params);
159 if (is_a($mailSettings, 'CRM_Core_Error')) {
160 $mailSettings->rollback();
161 return $mailSettings;
162 }
163
164 $transaction->commit();
e123fef4 165 CRM_Core_BAO_MailSettings::defaultDAO(TRUE);
6a488035
TO
166 return $mailSettings;
167 }
168
169 /**
100fef9d 170 * Delete the mail settings.
6a488035 171 *
6a0b768e
TO
172 * @param int $id
173 * Mail settings id.
6a488035 174 *
77b97be7 175 * @return mixed|null
6a488035 176 */
00be9182 177 public static function deleteMailSettings($id) {
6a488035
TO
178 $results = NULL;
179 $transaction = new CRM_Core_Transaction();
180
353ffa53 181 $mailSettings = new CRM_Core_DAO_MailSettings();
6a488035 182 $mailSettings->id = $id;
353ffa53 183 $results = $mailSettings->delete();
6a488035
TO
184
185 $transaction->commit();
186
187 return $results;
188 }
96025800 189
6a488035 190}