INFRA-132 - Batch #7
[civicrm-core.git] / CRM / Core / BAO / MailSettings.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35 class CRM_Core_BAO_MailSettings extends CRM_Core_DAO_MailSettings {
36
37 /**
38 * Class constructor
39 */
40 public function __construct() {
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 *
48 * @param bool $reset
49 *
50 * @return CRM_Core_BAO_MailSettings
51 * DAO with the default mail settings set
52 */
53 public static function defaultDAO($reset = FALSE) {
54 static $mailSettings = array();
55 $domainID = CRM_Core_Config::domainID();
56 if (empty($mailSettings[$domainID]) || $reset) {
57 $dao = new self();
58 $dao->is_default = 1;
59 $dao->domain_id = $domainID;
60 $dao->find(TRUE);
61 $mailSettings[$domainID] = $dao;
62 }
63 return $mailSettings[$domainID];
64 }
65
66 /**
67 * Return the domain from the default set of settings
68 *
69 * @return string
70 * default domain
71 */
72 public static function defaultDomain() {
73 return self::defaultDAO()->domain;
74 }
75
76 /**
77 * Return the localpart from the default set of settings
78 *
79 * @return string
80 * default localpart
81 */
82 public static function defaultLocalpart() {
83 return self::defaultDAO()->localpart;
84 }
85
86 /**
87 * Return the return path from the default set of settings
88 *
89 * @return string
90 * default return path
91 */
92 public static function defaultReturnPath() {
93 return self::defaultDAO()->return_path;
94 }
95
96 /**
97 * Return the "include message ID" flag from the default set of settings.
98 *
99 * @return bool
100 * default include message ID
101 */
102 public static function includeMessageId() {
103 return CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME,
104 'include_message_id',
105 NULL,
106 FALSE
107 );
108 }
109
110 /**
111 * Takes a bunch of params that are needed to match certain criteria and
112 * retrieves the relevant objects. Typically the valid params are only
113 * mail settings id. It also stores all the retrieved
114 * values in the default array
115 *
116 * @param array $params
117 * (reference ) an assoc array of name/value pairs.
118 * @param array $defaults
119 * (reference ) an assoc array to hold the flattened values.
120 *
121 * @return CRM_Core_BAO_MailSettings
122 */
123 public static function retrieve(&$params, &$defaults) {
124 $mailSettings = new CRM_Core_DAO_MailSettings();
125 $mailSettings->copyValues($params);
126
127 $result = NULL;
128 if ($mailSettings->find(TRUE)) {
129 CRM_Core_DAO::storeValues($mailSettings, $defaults);
130 $result = $mailSettings;
131 }
132
133 return $result;
134 }
135
136 /**
137 * Add new mail Settings.
138 *
139 * @param array $params
140 * Reference array contains the values submitted by the form.
141 *
142 *
143 * @return object
144 */
145 public static function add(&$params) {
146 $result = NULL;
147 if (empty($params)) {
148 return $result;
149 }
150
151 if (empty($params['id'])) {
152 $params['is_ssl'] = CRM_Utils_Array::value('is_ssl', $params, FALSE);
153 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
154 }
155
156 //handle is_default.
157 if (!empty($params['is_default'])) {
158 $query = 'UPDATE civicrm_mail_settings SET is_default = 0 WHERE domain_id = %1';
159 $queryParams = array(1 => array(CRM_Core_Config::domainID(), 'Integer'));
160 CRM_Core_DAO::executeQuery($query, $queryParams);
161 }
162
163 $mailSettings = new CRM_Core_DAO_MailSettings();
164 $mailSettings->copyValues($params);
165 $result = $mailSettings->save();
166
167 return $result;
168 }
169
170 /**
171 * Takes an associative array and creates a mail settings object
172 *
173 * @param array $params
174 * (reference ) an assoc array of name/value pairs.
175 *
176 * @return CRM_Core_BAO_MailSettings
177 */
178 public static function create(&$params) {
179 $transaction = new CRM_Core_Transaction();
180
181 $mailSettings = self::add($params);
182 if (is_a($mailSettings, 'CRM_Core_Error')) {
183 $mailSettings->rollback();
184 return $mailSettings;
185 }
186
187 $transaction->commit();
188 CRM_Core_BAO_MailSettings::defaultDAO(TRUE);
189 return $mailSettings;
190 }
191
192 /**
193 * Delete the mail settings.
194 *
195 * @param int $id
196 * Mail settings id.
197 *
198 * @return mixed|null
199 */
200 public static function deleteMailSettings($id) {
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 }