Merge pull request #4892 from colemanw/INFRA-132
[civicrm-core.git] / CRM / Core / BAO / MailSettings.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Core_BAO_MailSettings extends CRM_Core_DAO_MailSettings {
36
37 /**
100fef9d 38 * Class constructor
6a488035 39 */
00be9182 40 public function __construct() {
6a488035
TO
41 parent::__construct();
42 }
43
44 /**
45 * Return the DAO object containing to the default row of
46 * civicrm_mail_settings and cache it for further calls
47 *
07b3c7ff
EM
48 * @param bool $reset
49 *
50 * @return CRM_Core_BAO_MailSettings DAO with the default mail settings set
6a488035 51 */
00be9182 52 public static function defaultDAO($reset = FALSE) {
07b3c7ff
EM
53 static $mailSettings = array();
54 $domainID = CRM_Core_Config::domainID();
55 if (empty($mailSettings[$domainID]) || $reset) {
56 $dao = new self;
6a488035 57 $dao->is_default = 1;
07b3c7ff 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 /**
66 * Return the domain from the default set of settings
67 *
68 * @return string default domain
69 */
00be9182 70 public static function defaultDomain() {
6a488035
TO
71 return self::defaultDAO()->domain;
72 }
73
74 /**
75 * Return the localpart from the default set of settings
76 *
77 * @return string default localpart
78 */
00be9182 79 public static function defaultLocalpart() {
6a488035
TO
80 return self::defaultDAO()->localpart;
81 }
82
83 /**
84 * Return the return path from the default set of settings
85 *
86 * @return string default return path
87 */
00be9182 88 public static function defaultReturnPath() {
6a488035
TO
89 return self::defaultDAO()->return_path;
90 }
91
92 /**
93 * Return the "include message ID" flag from the default set of settings.
94 *
95 * @return boolean default include message ID
96 */
00be9182 97 public static function includeMessageId() {
6a488035
TO
98 return CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME,
99 'include_message_id',
100 NULL,
101 FALSE
102 );
103 }
104
105 /**
106 * Takes a bunch of params that are needed to match certain criteria and
107 * retrieves the relevant objects. Typically the valid params are only
108 * mail settings id. It also stores all the retrieved
109 * values in the default array
110 *
6a0b768e
TO
111 * @param array $params
112 * (reference ) an assoc array of name/value pairs.
113 * @param array $defaults
114 * (reference ) an assoc array to hold the flattened values.
6a488035 115 *
c490a46a 116 * @return CRM_Core_BAO_MailSettings object
6a488035
TO
117 * @static
118 */
00be9182 119 public static function retrieve(&$params, &$defaults) {
6a488035
TO
120 $mailSettings = new CRM_Core_DAO_MailSettings();
121 $mailSettings->copyValues($params);
122
123 $result = NULL;
124 if ($mailSettings->find(TRUE)) {
125 CRM_Core_DAO::storeValues($mailSettings, $defaults);
126 $result = $mailSettings;
127 }
128
129 return $result;
130 }
131
132 /**
100fef9d 133 * Add new mail Settings.
6a488035 134 *
6a0b768e
TO
135 * @param array $params
136 * Reference array contains the values submitted by the form.
6a488035 137 *
6a488035
TO
138 * @static
139 *
140 * @return object
141 */
00be9182 142 public static function add(&$params) {
6a488035
TO
143 $result = NULL;
144 if (empty($params)) {
145 return $result;
146 }
147
22e263ad 148 if (empty($params['id'])) {
e123fef4
EM
149 $params['is_ssl'] = CRM_Utils_Array::value('is_ssl', $params, FALSE);
150 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
151 }
6a488035
TO
152
153 //handle is_default.
e123fef4 154 if (!empty($params['is_default'])) {
6a488035
TO
155 $query = 'UPDATE civicrm_mail_settings SET is_default = 0 WHERE domain_id = %1';
156 $queryParams = array(1 => array(CRM_Core_Config::domainID(), 'Integer'));
157 CRM_Core_DAO::executeQuery($query, $queryParams);
158 }
159
160 $mailSettings = new CRM_Core_DAO_MailSettings();
161 $mailSettings->copyValues($params);
162 $result = $mailSettings->save();
163
164 return $result;
165 }
166
167 /**
100fef9d 168 * Takes an associative array and creates a mail settings object
6a488035 169 *
6a0b768e
TO
170 * @param array $params
171 * (reference ) an assoc array of name/value pairs.
6a488035 172 *
c490a46a 173 * @return CRM_Core_BAO_MailSettings object
6a488035
TO
174 * @static
175 */
00be9182 176 public static function create(&$params) {
6a488035
TO
177 $transaction = new CRM_Core_Transaction();
178
179 $mailSettings = self::add($params);
180 if (is_a($mailSettings, 'CRM_Core_Error')) {
181 $mailSettings->rollback();
182 return $mailSettings;
183 }
184
185 $transaction->commit();
e123fef4 186 CRM_Core_BAO_MailSettings::defaultDAO(TRUE);
6a488035
TO
187 return $mailSettings;
188 }
189
190 /**
100fef9d 191 * Delete the mail settings.
6a488035 192 *
6a0b768e
TO
193 * @param int $id
194 * Mail settings id.
6a488035 195 *
77b97be7 196 * @return mixed|null
6a488035
TO
197 * @static
198 *
199 */
00be9182 200 public static function deleteMailSettings($id) {
6a488035
TO
201 $results = NULL;
202 $transaction = new CRM_Core_Transaction();
203
204 $mailSettings = new CRM_Core_DAO_MailSettings();
205 $mailSettings->id = $id;
206 $results = $mailSettings->delete();
207
208 $transaction->commit();
209
210 return $results;
211 }
212}