| 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. | |
| 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 along with this program; if not, contact CiviCRM LLC | |
| 21 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 22 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 23 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 24 | +--------------------------------------------------------------------+ |
| 25 | */ |
| 26 | |
| 27 | /** |
| 28 | * |
| 29 | * @package CRM |
| 30 | * @copyright CiviCRM LLC (c) 2004-2013 |
| 31 | * $Id$ |
| 32 | * |
| 33 | */ |
| 34 | class CRM_Upgrade_Incremental_php_FourFour { |
| 35 | const BATCH_SIZE = 5000; |
| 36 | |
| 37 | function verifyPreDBstate(&$errors) { |
| 38 | return TRUE; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Compute any messages which should be displayed beforeupgrade |
| 43 | * |
| 44 | * Note: This function is called iteratively for each upcoming |
| 45 | * revision to the database. |
| 46 | * |
| 47 | * @param $postUpgradeMessage string, alterable |
| 48 | * @param $rev string, a version number, e.g. '4.4.alpha1', '4.4.beta3', '4.4.0' |
| 49 | * @return void |
| 50 | */ |
| 51 | function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NULL) { |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Compute any messages which should be displayed after upgrade |
| 56 | * |
| 57 | * @param $postUpgradeMessage string, alterable |
| 58 | * @param $rev string, an intermediate version; note that setPostUpgradeMessage is called repeatedly with different $revs |
| 59 | * @return void |
| 60 | */ |
| 61 | function setPostUpgradeMessage(&$postUpgradeMessage, $rev) { |
| 62 | if ($rev == '4.4.alpha1') { |
| 63 | $config = CRM_Core_Config::singleton(); |
| 64 | if (!empty($config->useIDS)) { |
| 65 | $postUpgradeMessage .= '<br />' . ts("The setting to skip IDS check has been deprecated. Please use the permission 'skip IDS check' to bypass the IDS system."); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | function upgrade_4_4_alpha1($rev) { |
| 71 | // task to process sql |
| 72 | $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.4.alpha1')), 'task_4_4_x_runSql', $rev); |
| 73 | |
| 74 | // Consolidate activity contacts CRM-12274. |
| 75 | $this->addTask('Consolidate activity contacts', 'activityContacts'); |
| 76 | |
| 77 | return TRUE; |
| 78 | } |
| 79 | |
| 80 | function upgrade_4_4_beta1($rev) { |
| 81 | $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.4.beta1')), 'task_4_4_x_runSql', $rev); |
| 82 | |
| 83 | // add new 'data' column in civicrm_batch |
| 84 | $query = 'ALTER TABLE civicrm_batch ADD data LONGTEXT NULL COMMENT "cache entered data"'; |
| 85 | CRM_Core_DAO::executeQuery($query, array(), TRUE, NULL, FALSE, FALSE); |
| 86 | |
| 87 | // check if batch entry data exists in civicrm_cache table |
| 88 | $query = 'SELECT path, data FROM civicrm_cache WHERE group_name = "batch entry"'; |
| 89 | $dao = CRM_Core_DAO::executeQuery($query); |
| 90 | while ($dao->fetch()) { |
| 91 | // get batch id $batchId[2] |
| 92 | $batchId = explode('-', $dao->path); |
| 93 | $data = unserialize($dao->data); |
| 94 | |
| 95 | // move the data to civicrm_batch table |
| 96 | CRM_Core_DAO::setFieldValue('CRM_Batch_DAO_Batch', $batchId[2], 'data', json_encode(array('values' => $data))); |
| 97 | } |
| 98 | |
| 99 | // delete entries from civicrm_cache table |
| 100 | $query = 'DELETE FROM civicrm_cache WHERE group_name = "batch entry"'; |
| 101 | CRM_Core_DAO::executeQuery($query); |
| 102 | |
| 103 | $this->addTask('Migrate custom word-replacements', 'wordReplacements'); |
| 104 | } |
| 105 | |
| 106 | function upgrade_4_4_1($rev) { |
| 107 | // CRM-13327 upgrade handling for the newly added name badges |
| 108 | $ogID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'name_badge', 'id', 'name'); |
| 109 | $nameBadges = array_flip(array_values(CRM_Core_BAO_OptionValue::getOptionValuesAssocArrayFromName('name_badge'))); |
| 110 | unset($nameBadges['Avery 5395']); |
| 111 | if (!empty($nameBadges)) { |
| 112 | $dimension = '{"paper-size":"a4","orientation":"portrait","font-name":"times","font-size":6,"font-style":"","NX":2,"NY":4,"metric":"mm","lMargin":6,"tMargin":19,"SpaceX":0,"SpaceY":0,"width":100,"height":65,"lPadding":0,"tPadding":0}'; |
| 113 | $query = "UPDATE civicrm_option_value |
| 114 | SET value = '{$dimension}' |
| 115 | WHERE option_group_id = %1 AND name = 'Fattorini Name Badge 100x65'"; |
| 116 | |
| 117 | CRM_Core_DAO::executeQuery($query, array(1 => array($ogID, 'Integer'))); |
| 118 | } |
| 119 | else { |
| 120 | $dimensions = array( |
| 121 | 1 => '{"paper-size":"a4","orientation":"landscape","font-name":"times","font-size":6,"font-style":"","NX":2,"NY":1,"metric":"mm","lMargin":25,"tMargin":27,"SpaceX":0,"SpaceY":35,"width":106,"height":150,"lPadding":5,"tPadding":5}', |
| 122 | 2 => '{"paper-size":"a4","orientation":"portrait","font-name":"times","font-size":6,"font-style":"","NX":2,"NY":4,"metric":"mm","lMargin":6,"tMargin":19,"SpaceX":0,"SpaceY":0,"width":100,"height":65,"lPadding":0,"tPadding":0}', |
| 123 | 3 => '{"paper-size":"a4","orientation":"portrait","font-name":"times","font-size":6,"font-style":"","NX":2,"NY":2,"metric":"mm","lMargin":10,"tMargin":28,"SpaceX":0,"SpaceY":0,"width":96,"height":121,"lPadding":5,"tPadding":5}', |
| 124 | ); |
| 125 | $insertStatements = array( |
| 126 | 1 => "($ogID, %1, '{$dimensions[1]}', %1, NULL, 0, NULL, 2, NULL, 0, 0, 1, NULL, NULL)", |
| 127 | 2 => "($ogID, %2, '{$dimensions[2]}', %2, NULL, 0, NULL, 3, NULL, 0, 0, 1, NULL, NULL)", |
| 128 | 3 => "($ogID, %3, '{$dimensions[3]}', %3, NULL, 0, NULL, 4, NULL, 0, 0, 1, NULL, NULL)", |
| 129 | ); |
| 130 | |
| 131 | $queryParams = array( |
| 132 | 1 => array('A6 Badge Portrait 150x106', 'String'), |
| 133 | 2 => array('Fattorini Name Badge 100x65', 'String'), |
| 134 | 3 => array('Hanging Badge 3-3/4" x 4-3"/4', 'String'), |
| 135 | ); |
| 136 | |
| 137 | foreach ($insertStatements as $values) { |
| 138 | $query = 'INSERT INTO civicrm_option_value (`option_group_id`, `label`, `value`, `name`, `grouping`, `filter`, `is_default`, `weight`, `description`, `is_optgroup`, `is_reserved`, `is_active`, `component_id`, `visibility_id`) VALUES' . $values; |
| 139 | CRM_Core_DAO::executeQuery($query, $queryParams); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => '4.4.1')), 'task_4_4_x_runSql', $rev); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Update activity contacts CRM-12274 |
| 148 | * |
| 149 | * @return bool TRUE for success |
| 150 | */ |
| 151 | static function activityContacts(CRM_Queue_TaskContext $ctx) { |
| 152 | $upgrade = new CRM_Upgrade_Form(); |
| 153 | |
| 154 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
| 155 | $ovValue[] = $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
| 156 | $ovValue[] = $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
| 157 | $ovValue[] = $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
| 158 | |
| 159 | $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'activity_contacts', 'id', 'name'); |
| 160 | if (!empty($ovValue)) { |
| 161 | $ovValues = implode(', ', $ovValue); |
| 162 | $query = " |
| 163 | UPDATE civicrm_option_value |
| 164 | SET is_reserved = 1 |
| 165 | WHERE option_group_id = {$optionGroupID} AND value IN ($ovValues)"; |
| 166 | |
| 167 | $dao = CRM_Core_DAO::executeQuery($query); |
| 168 | } |
| 169 | |
| 170 | if (!$assigneeID) { |
| 171 | $assigneeID = 1; |
| 172 | $value[] = "({$optionGroupID}, 'Activity Assignees', 1, 'Activity Assignees', 1, 1, 1)"; |
| 173 | } |
| 174 | if (!$sourceID) { |
| 175 | $sourceID = 2; |
| 176 | $value[] = "({$optionGroupID}, 'Activity Source', 2, 'Activity Source', 2, 1, 1)"; |
| 177 | } |
| 178 | if (!$targetID) { |
| 179 | $targetID = 3; |
| 180 | $value[] = "({$optionGroupID}, 'Activity Targets', 3, 'Activity Targets', 3, 1, 1)"; |
| 181 | } |
| 182 | |
| 183 | if (!$assigneeID || !$sourceID || !$targetID ) { |
| 184 | $insert = " |
| 185 | INSERT INTO civicrm_option_value |
| 186 | (option_group_id, label, value, name, weight, is_reserved, is_active) |
| 187 | VALUES |
| 188 | |
| 189 | "; |
| 190 | $values = implode(', ', $value); |
| 191 | $query = $insert . $values; |
| 192 | $dao = CRM_Core_DAO::executeQuery($query); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | $query = " |
| 197 | CREATE TABLE IF NOT EXISTS civicrm_activity_contact ( |
| 198 | id int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Activity contact id', |
| 199 | activity_id int(10) unsigned NOT NULL COMMENT 'Foreign key to the activity for this record.', |
| 200 | contact_id int(10) unsigned NOT NULL COMMENT 'Foreign key to the contact for this record.', |
| 201 | record_type_id int(10) unsigned DEFAULT NULL COMMENT 'The record type id for this row', |
| 202 | PRIMARY KEY (id), |
| 203 | UNIQUE KEY UI_activity_contact (contact_id,activity_id,record_type_id), |
| 204 | KEY FK_civicrm_activity_contact_activity_id (activity_id) |
| 205 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; |
| 206 | "; |
| 207 | |
| 208 | $dao = CRM_Core_DAO::executeQuery($query); |
| 209 | |
| 210 | $query = " |
| 211 | INSERT INTO civicrm_activity_contact (activity_id, contact_id, record_type_id) |
| 212 | SELECT activity_id, target_contact_id, {$targetID} as record_type_id |
| 213 | FROM civicrm_activity_target"; |
| 214 | |
| 215 | $dao = CRM_Core_DAO::executeQuery($query); |
| 216 | |
| 217 | $query = " |
| 218 | INSERT INTO civicrm_activity_contact (activity_id, contact_id, record_type_id) |
| 219 | SELECT activity_id, assignee_contact_id, {$assigneeID} as record_type_id |
| 220 | FROM civicrm_activity_assignment"; |
| 221 | $dao = CRM_Core_DAO::executeQuery($query); |
| 222 | |
| 223 | $query = " |
| 224 | INSERT INTO civicrm_activity_contact (activity_id, contact_id, record_type_id) |
| 225 | SELECT id, source_contact_id, {$sourceID} as record_type_id |
| 226 | FROM civicrm_activity |
| 227 | WHERE source_contact_id IS NOT NULL"; |
| 228 | |
| 229 | $dao = CRM_Core_DAO::executeQuery($query); |
| 230 | |
| 231 | $query = "DROP TABLE civicrm_activity_target"; |
| 232 | $dao = CRM_Core_DAO::executeQuery($query); |
| 233 | |
| 234 | $query = "DROP TABLE civicrm_activity_assignment"; |
| 235 | $dao = CRM_Core_DAO::executeQuery($query); |
| 236 | |
| 237 | $query = "ALTER TABLE civicrm_activity |
| 238 | DROP FOREIGN KEY FK_civicrm_activity_source_contact_id"; |
| 239 | |
| 240 | $dao = CRM_Core_DAO::executeQuery($query); |
| 241 | |
| 242 | $query = "ALTER TABLE civicrm_activity DROP COLUMN source_contact_id"; |
| 243 | $dao = CRM_Core_DAO::executeQuery($query); |
| 244 | |
| 245 | return TRUE; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Migrate word-replacements from $config to civicrm_word_replacement |
| 250 | * |
| 251 | * @return bool TRUE for success |
| 252 | * @see http://issues.civicrm.org/jira/browse/CRM-13187 |
| 253 | */ |
| 254 | static function wordReplacements(CRM_Queue_TaskContext $ctx) { |
| 255 | $query = " |
| 256 | CREATE TABLE IF NOT EXISTS `civicrm_word_replacement` ( |
| 257 | `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'Word replacement ID', |
| 258 | `find_word` varchar(255) COLLATE utf8_bin COMMENT 'Word which need to be replaced', |
| 259 | `replace_word` varchar(255) COLLATE utf8_bin COMMENT 'Word which will replace the word in find', |
| 260 | `is_active` tinyint COMMENT 'Is this entry active?', |
| 261 | `match_type` enum('wildcardMatch', 'exactMatch') DEFAULT 'wildcardMatch', |
| 262 | `domain_id` int unsigned COMMENT 'FK to Domain ID. This is for Domain specific word replacement', |
| 263 | PRIMARY KEY ( `id` ), |
| 264 | UNIQUE INDEX `UI_find`(domain_id, find_word), |
| 265 | CONSTRAINT FK_civicrm_word_replacement_domain_id FOREIGN KEY (`domain_id`) REFERENCES `civicrm_domain`(`id`) |
| 266 | ) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ; |
| 267 | "; |
| 268 | $dao = CRM_Core_DAO::executeQuery($query); |
| 269 | |
| 270 | self::rebuildWordReplacementTable(); |
| 271 | return TRUE; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * (Queue Task Callback) |
| 276 | */ |
| 277 | static function task_4_4_x_runSql(CRM_Queue_TaskContext $ctx, $rev) { |
| 278 | $upgrade = new CRM_Upgrade_Form(); |
| 279 | $upgrade->processSQL($rev); |
| 280 | |
| 281 | return TRUE; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Syntatic sugar for adding a task which (a) is in this class and (b) has |
| 286 | * a high priority. |
| 287 | * |
| 288 | * After passing the $funcName, you can also pass parameters that will go to |
| 289 | * the function. Note that all params must be serializable. |
| 290 | */ |
| 291 | protected function addTask($title, $funcName) { |
| 292 | $queue = CRM_Queue_Service::singleton()->load(array( |
| 293 | 'type' => 'Sql', |
| 294 | 'name' => CRM_Upgrade_Form::QUEUE_NAME, |
| 295 | )); |
| 296 | |
| 297 | $args = func_get_args(); |
| 298 | $title = array_shift($args); |
| 299 | $funcName = array_shift($args); |
| 300 | $task = new CRM_Queue_Task( |
| 301 | array(get_class($this), $funcName), |
| 302 | $args, |
| 303 | $title |
| 304 | ); |
| 305 | $queue->createItem($task, array('weight' => -1)); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Get all the word-replacements stored in config-arrays |
| 310 | * and convert them to params for the WordReplacement.create API. |
| 311 | * |
| 312 | * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and |
| 313 | * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade |
| 314 | * step behaves consistently even as the BAO evolves in future versions. |
| 315 | * However, if there's a bug in here prior to 4.4.0, we should apply the |
| 316 | * bugfix in both places. |
| 317 | * |
| 318 | * @param bool $rebuildEach whether to perform rebuild after each individual API call |
| 319 | * @return array Each item is $params for WordReplacement.create |
| 320 | * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams |
| 321 | */ |
| 322 | static function getConfigArraysAsAPIParams($rebuildEach) { |
| 323 | $wordReplacementCreateParams = array(); |
| 324 | // get all domains |
| 325 | $result = civicrm_api3('domain', 'get', array( |
| 326 | 'return' => array('locale_custom_strings'), |
| 327 | )); |
| 328 | if (!empty($result["values"])) { |
| 329 | foreach ($result["values"] as $value) { |
| 330 | $params = array(); |
| 331 | $params["domain_id"] = $value["id"]; |
| 332 | $params["options"] = array('wp-rebuild' => $rebuildEach); |
| 333 | // unserialize word match string |
| 334 | $localeCustomArray = unserialize($value["locale_custom_strings"]); |
| 335 | if (!empty($localeCustomArray)) { |
| 336 | $wordMatchArray = array(); |
| 337 | // Traverse Language array |
| 338 | foreach ($localeCustomArray as $localCustomData) { |
| 339 | // Traverse status array "enabled" "disabled" |
| 340 | foreach ($localCustomData as $status => $matchTypes) { |
| 341 | $params["is_active"] = ($status == "enabled")?TRUE:FALSE; |
| 342 | // Traverse Match Type array "wildcardMatch" "exactMatch" |
| 343 | foreach ($matchTypes as $matchType => $words) { |
| 344 | $params["match_type"] = $matchType; |
| 345 | foreach ($words as $word => $replace) { |
| 346 | $params["find_word"] = $word; |
| 347 | $params["replace_word"] = $replace; |
| 348 | $wordReplacementCreateParams[] = $params; |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | return $wordReplacementCreateParams; |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Get all the word-replacements stored in config-arrays |
| 361 | * and write them out as records in civicrm_word_replacement. |
| 362 | * |
| 363 | * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and |
| 364 | * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade |
| 365 | * step behaves consistently even as the BAO evolves in future versions. |
| 366 | * However, if there's a bug in here prior to 4.4.0, we should apply the |
| 367 | * bugfix in both places. |
| 368 | */ |
| 369 | public static function rebuildWordReplacementTable() { |
| 370 | civicrm_api3('word_replacement', 'replace', array( |
| 371 | 'options' => array('match' => array('domain_id', 'find_word')), |
| 372 | 'values' => self::getConfigArraysAsAPIParams(FALSE), |
| 373 | )); |
| 374 | CRM_Core_BAO_WordReplacement::rebuild(); |
| 375 | } |
| 376 | } |