3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
35 class CRM_Upgrade_TwoTwo_Form_Step3
extends CRM_Upgrade_Form
{
37 * @param $errorMessage
41 function verifyPreDBState(&$errorMessage) {
42 $errorMessage = ts('Pre-condition failed for upgrade step %1.', array(1 => '3'));
43 return $this->checkVersion('2.1.102');
47 //1.upgared the domain from email address.
48 self
::upgradeDomainFromEmail();
50 //2.preserve mailer preferences.
51 self
::mailerPreferences();
53 $this->setVersion('2.1.103');
57 * @param $errorMessage
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')
66 $errorMessage .= ' option group or option value table is missing.';
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')
77 $errorMessage .= ' Few important fields were found missing in civicrm_option_group table.';
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')
88 $errorMessage .= ' Few important fields were found missing in civicrm_option_value table.';
91 $errorMessage = ts('Post-condition failed for upgrade step %1.', array(1 => '2'));
93 return $this->checkVersion('2.1.103');
100 return ts('CiviCRM 2.2 Upgrade: Step Three (Option Group And Values)');
106 function getTemplateMessage() {
107 return '<p>' . ts('Step Three will upgrade the Option Group And Values in your database.') . '</p>';
113 function getButtonTitle() {
114 return ts('Upgrade & Continue');
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.
125 function upgradeDomainFromEmail() {
128 FROM civicrm_option_group
129 WHERE name = 'from_Email_address'";
131 $fmaGroup = CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
133 if ($fmaGroup->fetch()) {
134 $fmaGroupId = $fmaGroup->id
;
137 //insert 'from_mailing_address' option group.
139 INSERT INTO civicrm_option_group ( name, description, is_reserved, is_active )
140 VALUES ('from_email_address', 'From Email Address', 0, 1)";
142 CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
147 FROM civicrm_option_group
148 WHERE name = 'from_email_address'";
149 $dao = CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
151 $fmaGroupId = $dao->id
;
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');
162 $formEmailAddress = '"' . $domain->email_name
. '"<' . $domain->email_address
. '>';
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..
168 //get the existing from email address.
170 $optionValues = array();
171 $grpParams['name'] = 'from_email_address';
172 CRM_Core_OptionValue
::getValues($grpParams, $optionValues);
174 $maxVal = $maxWt = 1;
175 $insertEmailAddress = TRUE;
177 if (!empty($optionValues)) {
179 //make existing is_default = 0
181 UPDATE civicrm_option_value
183 WHERE option_group_id = %1";
185 $params = array(1 => array($fmaGroupId, 'Integer'));
186 CRM_Core_DAO
::executeQuery($query, $params);
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.
192 $labelValues = $nameValues = array();
193 foreach ($optionValues as $id => $value) {
194 if ($value['label'] == $formEmailAddress) {
195 $labelValues = $value;
197 elseif ($value['name'] == $formEmailAddress) {
198 $nameValues = $value;
202 //as we consider label so label should preserve.
203 $updateValues = array();
204 if (!empty($labelValues)) {
205 $updateValues = $labelValues;
208 //if matching name found need to preserve it.
209 if (!empty($nameValues)) {
211 //copy domain from email address as label.
212 if (empty($updateValues)) {
213 $updateValues = $nameValues;
214 $updateValues['label'] = $formEmailAddress;
217 //since name is also imp so preserve it
218 //as name for domain email address record.
219 $updateValues['name'] = $nameValues['name'];
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']);
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();
236 //get the max value and wt.
237 if ($insertEmailAddress) {
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";
246 $dao = CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
248 $maxWt +
= $dao->maxWt
;
249 $maxVal +
= $dao->maxVal
;
254 if ($insertEmailAddress) {
256 //insert domain from email address and name.
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)";
263 $params = array(1 => array($fmaGroupId, 'Integer'),
264 2 => array($formEmailAddress, 'String'),
265 3 => array($maxVal, 'Integer'),
266 4 => array($maxWt, 'Integer'),
268 CRM_Core_DAO
::executeQuery($query, $params);
271 //drop civicrm_domain.email_name and
272 //civicrm_domain.email_address.
274 ALTER TABLE `civicrm_domain`
276 DROP `email_address`";
278 CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
282 /* preserve the mailer preferences from config backend to
283 * civicrm_preferences and unset these from config backend.
285 function mailerPreferences() {
287 $mailerValues = array();
288 $mailerFields = array(
289 'outBound_option', 'smtpServer', 'smtpPort', 'smtpAuth',
290 'smtpUsername', 'smtpPassword', 'sendmail_path', 'sendmail_args',
293 //get the mailer preferences from backend
294 //store in civicrm_preferences and unset from backend.
295 $domain = new CRM_Core_DAO_Domain();
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]);
306 $domain->config_backend
= serialize($backendValues);
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();