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