CRM/Upgrade add missing comment blocks
[civicrm-core.git] / CRM / Upgrade / TwoTwo / Form / Step3.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_Upgrade_TwoTwo_Form_Step3 extends CRM_Upgrade_Form {
36 /**
37 * @param $errorMessage
38 *
39 * @return bool
40 */
41 function verifyPreDBState(&$errorMessage) {
42 $errorMessage = ts('Pre-condition failed for upgrade step %1.', array(1 => '3'));
43 return $this->checkVersion('2.1.102');
44 }
45
46 function upgrade() {
47 //1.upgared the domain from email address.
48 self::upgradeDomainFromEmail();
49
50 //2.preserve mailer preferences.
51 self::mailerPreferences();
52
53 $this->setVersion('2.1.103');
54 }
55
56 /**
57 * @param $errorMessage
58 *
59 * @return bool
60 */
61 function verifyPostDBState(&$errorMessage) {
62 // check if Option Group & Option Values tables exists
63 if (!CRM_Core_DAO::checkTableExists('civicrm_option_group') ||
64 !CRM_Core_DAO::checkTableExists('civicrm_option_value')
65 ) {
66 $errorMessage .= ' option group or option value table is missing.';
67 return FALSE;
68 }
69 // check fields which MUST be present civicrm_option_group & civicrm_option_value
70 if (!CRM_Core_DAO::checkFieldExists('civicrm_option_group', 'id') ||
71 !CRM_Core_DAO::checkFieldExists('civicrm_option_group', 'name') ||
72 !CRM_Core_DAO::checkFieldExists('civicrm_option_group', 'label') ||
73 !CRM_Core_DAO::checkFieldExists('civicrm_option_group', 'description') ||
74 !CRM_Core_DAO::checkFieldExists('civicrm_option_group', 'is_reserved') ||
75 !CRM_Core_DAO::checkFieldExists('civicrm_option_group', 'is_active')
76 ) {
77 $errorMessage .= ' Few important fields were found missing in civicrm_option_group table.';
78 return FALSE;
79 }
80 if (!CRM_Core_DAO::checkFieldExists('civicrm_option_value', 'id') ||
81 !CRM_Core_DAO::checkFieldExists('civicrm_option_value', 'option_group_id') ||
82 !CRM_Core_DAO::checkFieldExists('civicrm_option_value', 'name') ||
83 !CRM_Core_DAO::checkFieldExists('civicrm_option_value', 'label') ||
84 !CRM_Core_DAO::checkFieldExists('civicrm_option_value', 'description') ||
85 !CRM_Core_DAO::checkFieldExists('civicrm_option_value', 'component_id') ||
86 !CRM_Core_DAO::checkFieldExists('civicrm_option_value', 'is_active')
87 ) {
88 $errorMessage .= ' Few important fields were found missing in civicrm_option_value table.';
89 return FALSE;
90 }
91 $errorMessage = ts('Post-condition failed for upgrade step %1.', array(1 => '2'));
92
93 return $this->checkVersion('2.1.103');
94 }
95
96 /**
97 * @return string
98 */
99 function getTitle() {
100 return ts('CiviCRM 2.2 Upgrade: Step Three (Option Group And Values)');
101 }
102
103 /**
104 * @return string
105 */
106 function getTemplateMessage() {
107 return '<p>' . ts('Step Three will upgrade the Option Group And Values in your database.') . '</p>';
108 }
109
110 /**
111 * @return string
112 */
113 function getButtonTitle() {
114 return ts('Upgrade & Continue');
115 }
116
117 /**
118 * This function preserve the civicrm_domain.email_name and civicrm_domain.email_address
119 * as a default option value into "from_email_address" option group
120 * and drop these columns from civicrm_domain table.
121 * @access public
122 *
123 * @return void
124 */
125 function upgradeDomainFromEmail() {
126 $query = "
127 SELECT id
128 FROM civicrm_option_group
129 WHERE name = 'from_Email_address'";
130
131 $fmaGroup = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
132 $fmaGroupId = NULL;
133 if ($fmaGroup->fetch()) {
134 $fmaGroupId = $fmaGroup->id;
135 }
136 else {
137 //insert 'from_mailing_address' option group.
138 $query = "
139 INSERT INTO civicrm_option_group ( name, description, is_reserved, is_active )
140 VALUES ('from_email_address', 'From Email Address', 0, 1)";
141
142 CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
143
144 //get the group id.
145 $query = "
146 SELECT id
147 FROM civicrm_option_group
148 WHERE name = 'from_email_address'";
149 $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
150 if ($dao->fetch()) {
151 $fmaGroupId = $dao->id;
152 }
153 }
154
155 if ($fmaGroupId) {
156 //get domain from email address and name as default value.
157 $domain = CRM_Core_BAO_Domain::getDomain();
158 $domain->selectAdd();
159 $domain->selectAdd('email_name', 'email_address');
160 $domain->find(TRUE);
161
162 $formEmailAddress = '"' . $domain->email_name . '"<' . $domain->email_address . '>';
163
164 //first check given domain email address exist in option
165 //value, if yes make it as domain email address by making
166 //it as default from email address..
167
168 //get the existing from email address.
169
170 $optionValues = array();
171 $grpParams['name'] = 'from_email_address';
172 CRM_Core_OptionValue::getValues($grpParams, $optionValues);
173
174 $maxVal = $maxWt = 1;
175 $insertEmailAddress = TRUE;
176
177 if (!empty($optionValues)) {
178
179 //make existing is_default = 0
180 $query = "
181 UPDATE civicrm_option_value
182 SET is_default = 0
183 WHERE option_group_id = %1";
184
185 $params = array(1 => array($fmaGroupId, 'Integer'));
186 CRM_Core_DAO::executeQuery($query, $params);
187
188 //if domain from name and email exist as name or label in option value
189 //table need to preserve that name and label and take care that label
190 //and name both remain unique in db.
191
192 $labelValues = $nameValues = array();
193 foreach ($optionValues as $id => $value) {
194 if ($value['label'] == $formEmailAddress) {
195 $labelValues = $value;
196 }
197 elseif ($value['name'] == $formEmailAddress) {
198 $nameValues = $value;
199 }
200 }
201
202 //as we consider label so label should preserve.
203 $updateValues = array();
204 if (!empty($labelValues)) {
205 $updateValues = $labelValues;
206 }
207
208 //if matching name found need to preserve it.
209 if (!empty($nameValues)) {
210
211 //copy domain from email address as label.
212 if (empty($updateValues)) {
213 $updateValues = $nameValues;
214 $updateValues['label'] = $formEmailAddress;
215 }
216 else {
217 //since name is also imp so preserve it
218 //as name for domain email address record.
219 $updateValues['name'] = $nameValues['name'];
220
221 //name is unique so drop name value record.
222 //since we transfer this name to found label record.
223 CRM_Core_BAO_OptionValue::del($nameValues['id']);
224 }
225 }
226
227 if (!empty($updateValues)) {
228 $insertEmailAddress = FALSE;
229 //update label/name found record w/ manupulated values.
230 $updateValues['is_active'] = $updateValues['is_default'] = 1;
231 $optionValue = new CRM_Core_DAO_OptionValue();
232 $optionValue->copyValues($updateValues);
233 $optionValue->save();
234 }
235
236 //get the max value and wt.
237 if ($insertEmailAddress) {
238 $query = "
239 SELECT max(ROUND(civicrm_option_value.value)) as maxVal,
240 max(civicrm_option_value.weight) as maxWt
241 FROM civicrm_option_value, civicrm_option_group
242 WHERE civicrm_option_group.name = 'from_Email_address'
243 AND civicrm_option_value.option_group_id = civicrm_option_group.id
244 GROUP BY civicrm_option_group.id";
245
246 $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
247 if ($dao->fetch()) {
248 $maxWt += $dao->maxWt;
249 $maxVal += $dao->maxVal;
250 }
251 }
252 }
253
254 if ($insertEmailAddress) {
255
256 //insert domain from email address and name.
257 $query = "
258 INSERT INTO `civicrm_option_value`
259 (`option_group_id`, `label`, `value`, `name` , `grouping`, `filter`, `is_default`,
260 `weight`, `description`, `is_optgroup`, `is_reserved`, `is_active`, `component_id`)
261 VALUES ( %1, %2, %3, %2, NULL, 0, 1, %4, 'Default domain email address and from name.', 0, 0, 1, NULL)";
262
263 $params = array(1 => array($fmaGroupId, 'Integer'),
264 2 => array($formEmailAddress, 'String'),
265 3 => array($maxVal, 'Integer'),
266 4 => array($maxWt, 'Integer'),
267 );
268 CRM_Core_DAO::executeQuery($query, $params);
269 }
270
271 //drop civicrm_domain.email_name and
272 //civicrm_domain.email_address.
273 $query = "
274 ALTER TABLE `civicrm_domain`
275 DROP `email_name`,
276 DROP `email_address`";
277
278 CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
279 }
280 }
281
282 /* preserve the mailer preferences from config backend to
283 * civicrm_preferences and unset these from config backend.
284 */
285 function mailerPreferences() {
286
287 $mailerValues = array();
288 $mailerFields = array(
289 'outBound_option', 'smtpServer', 'smtpPort', 'smtpAuth',
290 'smtpUsername', 'smtpPassword', 'sendmail_path', 'sendmail_args',
291 );
292
293 //get the mailer preferences from backend
294 //store in civicrm_preferences and unset from backend.
295 $domain = new CRM_Core_DAO_Domain();
296 $domain->find(TRUE);
297 if ($domain->config_backend) {
298 $backendValues = unserialize($domain->config_backend);
299 foreach ($mailerFields as $field) {
300 $mailerValues[$field] = CRM_Utils_Array::value($field, $backendValues);
301 if (array_key_exists($field, $backendValues)) {
302 unset($backendValues[$field]);
303 }
304 }
305
306 $domain->config_backend = serialize($backendValues);
307 $domain->save();
308
309 $sql = 'SELECT id, mailing_backend FROM civicrm_preferences';
310 $mailingDomain = CRM_Core_DAO::executeQuery($sql);
311 $mailingDomain->find(TRUE);
312 $mailingDomain->mailing_backend = serialize($mailerValues);
313 $mailingDomain->save();
314 }
315 }
316 }
317