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