Mass cleanup of docblocks/code/comments
[civicrm-core.git] / CRM / Core / BAO / MailSettings.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 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 DAO with the default mail settings set
51 */
52 static function defaultDAO($reset = FALSE) {
53 static $mailSettings = array();
54 $domainID = CRM_Core_Config::domainID();
55 if (empty($mailSettings[$domainID]) || $reset) {
56 $dao = new self;
57 $dao->is_default = 1;
58 $dao->domain_id = $domainID;
59 $dao->find(TRUE);
60 $mailSettings[$domainID] = $dao;
61 }
62 return $mailSettings[$domainID];
63 }
64
65 /**
66 * Return the domain from the default set of settings
67 *
68 * @return string default domain
69 */
70 static function defaultDomain() {
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 */
79 static function defaultLocalpart() {
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 */
88 static function defaultReturnPath() {
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 */
97 static function includeMessageId() {
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 *
111 * @param array $params (reference ) an assoc array of name/value pairs
112 * @param array $defaults (reference ) an assoc array to hold the flattened values
113 *
114 * @return CRM_Core_BAO_MailSettings object
115 * @access public
116 * @static
117 */
118 static function retrieve(&$params, &$defaults) {
119 $mailSettings = new CRM_Core_DAO_MailSettings();
120 $mailSettings->copyValues($params);
121
122 $result = NULL;
123 if ($mailSettings->find(TRUE)) {
124 CRM_Core_DAO::storeValues($mailSettings, $defaults);
125 $result = $mailSettings;
126 }
127
128 return $result;
129 }
130
131 /**
132 * add new mail Settings.
133 *
134 * @param array $params reference array contains the values submitted by the form
135 *
136 * @access public
137 * @static
138 *
139 * @return object
140 */
141 static function add(&$params) {
142 $result = NULL;
143 if (empty($params)) {
144 return $result;
145 }
146
147 if(empty($params['id'])) {
148 $params['is_ssl'] = CRM_Utils_Array::value('is_ssl', $params, FALSE);
149 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
150 }
151
152 //handle is_default.
153 if (!empty($params['is_default'])) {
154 $query = 'UPDATE civicrm_mail_settings SET is_default = 0 WHERE domain_id = %1';
155 $queryParams = array(1 => array(CRM_Core_Config::domainID(), 'Integer'));
156 CRM_Core_DAO::executeQuery($query, $queryParams);
157 }
158
159 $mailSettings = new CRM_Core_DAO_MailSettings();
160 $mailSettings->copyValues($params);
161 $result = $mailSettings->save();
162
163 return $result;
164 }
165
166 /**
167 * takes an associative array and creates a mail settings object
168 *
169 * @param array $params (reference ) an assoc array of name/value pairs
170 *
171 * @return CRM_Core_BAO_MailSettings object
172 * @access public
173 * @static
174 */
175 static function create(&$params) {
176 $transaction = new CRM_Core_Transaction();
177
178 $mailSettings = self::add($params);
179 if (is_a($mailSettings, 'CRM_Core_Error')) {
180 $mailSettings->rollback();
181 return $mailSettings;
182 }
183
184 $transaction->commit();
185 CRM_Core_BAO_MailSettings::defaultDAO(TRUE);
186 return $mailSettings;
187 }
188
189 /**
190 * delete the mail settings.
191 *
192 * @param int $id mail settings id
193 *
194 * @return mixed|null
195 * @access public
196 * @static
197 *
198 */
199 static function deleteMailSettings($id) {
200 $results = NULL;
201 $transaction = new CRM_Core_Transaction();
202
203 $mailSettings = new CRM_Core_DAO_MailSettings();
204 $mailSettings->id = $id;
205 $results = $mailSettings->delete();
206
207 $transaction->commit();
208
209 return $results;
210 }
211 }