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