Merge pull request #17008 from ivan-compucorp/CPS-70-fix-radio-value
[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 * $Id$
17 *
18 */
19 class CRM_Core_BAO_MailSettings extends CRM_Core_DAO_MailSettings {
20
21 /**
22 * Class constructor.
23 */
24 public function __construct() {
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 *
32 * @param bool $reset
33 *
34 * @return CRM_Core_BAO_MailSettings
35 * DAO with the default mail settings set
36 */
37 public static function defaultDAO($reset = FALSE) {
38 static $mailSettings = [];
39 $domainID = CRM_Core_Config::domainID();
40 if (empty($mailSettings[$domainID]) || $reset) {
41 $dao = new self();
42 $dao->is_default = 1;
43 $dao->domain_id = $domainID;
44 $dao->find(TRUE);
45 $mailSettings[$domainID] = $dao;
46 }
47 return $mailSettings[$domainID];
48 }
49
50 /**
51 * Return the domain from the default set of settings.
52 *
53 * @return string
54 * default domain
55 */
56 public static function defaultDomain() {
57 return self::defaultDAO()->domain;
58 }
59
60 /**
61 * Return the localpart from the default set of settings.
62 *
63 * @return string
64 * default localpart
65 */
66 public static function defaultLocalpart() {
67 return self::defaultDAO()->localpart;
68 }
69
70 /**
71 * Return the return path from the default set of settings.
72 *
73 * @return string
74 * default return path
75 */
76 public static function defaultReturnPath() {
77 return self::defaultDAO()->return_path;
78 }
79
80 /**
81 * Return the "include message ID" flag from the default set of settings.
82 *
83 * @return bool
84 * default include message ID
85 */
86 public static function includeMessageId() {
87 return Civi::settings()->get('include_message_id');
88 }
89
90 /**
91 * Retrieve DB object based on input parameters.
92 *
93 * It also stores all the retrieved values in the default array.
94 *
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.
99 *
100 * @return CRM_Core_BAO_MailSettings
101 */
102 public static function retrieve(&$params, &$defaults) {
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 /**
116 * Add new mail Settings.
117 *
118 * @param array $params
119 * Reference array contains the values submitted by the form.
120 *
121 *
122 * @return object
123 */
124 public static function add(&$params) {
125 $result = NULL;
126 if (empty($params)) {
127 return $result;
128 }
129
130 if (empty($params['id'])) {
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 }
134
135 //handle is_default.
136 if (!empty($params['is_default'])) {
137 $query = 'UPDATE civicrm_mail_settings SET is_default = 0 WHERE domain_id = %1';
138 $queryParams = [1 => [CRM_Core_Config::domainID(), 'Integer']];
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 /**
150 * Takes an associative array and creates a mail settings object.
151 *
152 * @param array $params
153 * (reference ) an assoc array of name/value pairs.
154 *
155 * @return CRM_Core_BAO_MailSettings
156 */
157 public static function create(&$params) {
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();
167 CRM_Core_BAO_MailSettings::defaultDAO(TRUE);
168 return $mailSettings;
169 }
170
171 /**
172 * Delete the mail settings.
173 *
174 * @param int $id
175 * Mail settings id.
176 *
177 * @return mixed|null
178 */
179 public static function deleteMailSettings($id) {
180 $results = NULL;
181 $transaction = new CRM_Core_Transaction();
182
183 $mailSettings = new CRM_Core_DAO_MailSettings();
184 $mailSettings->id = $id;
185 $results = $mailSettings->delete();
186
187 $transaction->commit();
188
189 return $results;
190 }
191
192 }