|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * CATS |
| 4 | + * Update 371 - decode HTML entities until stable for text fields |
| 5 | + * |
| 6 | + * Copyright (C) 2005 - 2007 Cognizo Technologies, Inc. |
| 7 | + * |
| 8 | + * The contents of this file are subject to the CATS Public License |
| 9 | + * Version 1.1a (the "License"); you may not use this file except in |
| 10 | + * compliance with the License. You may obtain a copy of the License at |
| 11 | + * http://www.catsone.com/. |
| 12 | + * |
| 13 | + * Software distributed under the License is distributed on an "AS IS" |
| 14 | + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
| 15 | + * License for the specific language governing rights and limitations |
| 16 | + * under the License. |
| 17 | + * |
| 18 | + * The Original Code is "CATS Standard Edition". |
| 19 | + * |
| 20 | + * The Initial Developer of the Original Code is Cognizo Technologies, Inc. |
| 21 | + * Portions created by the Initial Developer are Copyright (C) 2005 - 2007 |
| 22 | + * (or from the year in which this file was created to the year 2007) by |
| 23 | + * Cognizo Technologies, Inc. All Rights Reserved. |
| 24 | + * |
| 25 | + * $Id: 371.php $ |
| 26 | + */ |
| 27 | + |
| 28 | +function update_371($db) |
| 29 | +{ |
| 30 | + $tables = array( |
| 31 | + 'joborder' => array( |
| 32 | + 'primaryKey' => 'joborder_id', |
| 33 | + 'columns' => array('title', 'description', 'notes', 'city', 'state', 'duration', 'rate_max', 'salary', 'client_job_id') |
| 34 | + ), |
| 35 | + 'company' => array( |
| 36 | + 'primaryKey' => 'company_id', |
| 37 | + 'columns' => array('name', 'address', 'city', 'state', 'zip', 'url', 'key_technologies', 'notes') |
| 38 | + ), |
| 39 | + 'contact' => array( |
| 40 | + 'primaryKey' => 'contact_id', |
| 41 | + 'columns' => array('first_name', 'last_name', 'title', 'email1', 'email2', 'address', 'city', 'state', 'zip', 'notes') |
| 42 | + ), |
| 43 | + 'candidate' => array( |
| 44 | + 'primaryKey' => 'candidate_id', |
| 45 | + 'columns' => array('first_name', 'middle_name', 'last_name', 'email1', 'email2', 'address', 'city', 'state', 'notes', 'key_skills', 'current_employer', 'current_position', 'source', 'web_site', 'best_time_to_call', 'desired_pay', 'current_pay') |
| 46 | + ), |
| 47 | + 'activity' => array( |
| 48 | + 'primaryKey' => 'activity_id', |
| 49 | + 'columns' => array('notes') |
| 50 | + ), |
| 51 | + 'calendar_event' => array( |
| 52 | + 'primaryKey' => 'calendar_event_id', |
| 53 | + 'columns' => array('title', 'description', 'location', 'reminder_email') |
| 54 | + ), |
| 55 | + 'history' => array( |
| 56 | + 'primaryKey' => 'history_id', |
| 57 | + 'columns' => array('previous_value', 'new_value', 'description') |
| 58 | + ), |
| 59 | + 'email_history' => array( |
| 60 | + 'primaryKey' => 'email_history_id', |
| 61 | + 'columns' => array('text', 'recipients') |
| 62 | + ), |
| 63 | + 'email_template' => array( |
| 64 | + 'primaryKey' => 'email_template_id', |
| 65 | + 'columns' => array('text', 'title', 'possible_variables') |
| 66 | + ), |
| 67 | + 'extra_field' => array( |
| 68 | + 'primaryKey' => 'extra_field_id', |
| 69 | + 'columns' => array('value') |
| 70 | + ), |
| 71 | + 'attachment' => array( |
| 72 | + 'primaryKey' => 'attachment_id', |
| 73 | + 'columns' => array('text') |
| 74 | + ), |
| 75 | + 'feedback' => array( |
| 76 | + 'primaryKey' => 'feedback_id', |
| 77 | + 'columns' => array('feedback', 'subject') |
| 78 | + ) |
| 79 | + ); |
| 80 | + |
| 81 | + foreach ($tables as $tableName => $tableData) |
| 82 | + { |
| 83 | + $tableExists = $db->getAllAssoc( |
| 84 | + 'SHOW TABLES LIKE ' . $db->makeQueryString($tableName) |
| 85 | + ); |
| 86 | + |
| 87 | + if (empty($tableExists)) |
| 88 | + { |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + $columnsToUpdate = array(); |
| 93 | + foreach ($tableData['columns'] as $columnName) |
| 94 | + { |
| 95 | + $columnExists = $db->getAllAssoc( |
| 96 | + 'SHOW COLUMNS FROM `' . $tableName . '` LIKE ' . $db->makeQueryString($columnName) |
| 97 | + ); |
| 98 | + if (!empty($columnExists)) |
| 99 | + { |
| 100 | + $columnsToUpdate[] = $columnName; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if (empty($columnsToUpdate)) |
| 105 | + { |
| 106 | + continue; |
| 107 | + } |
| 108 | + |
| 109 | + $selectColumns = array_merge(array($tableData['primaryKey']), $columnsToUpdate); |
| 110 | + $selectParts = array(); |
| 111 | + foreach ($selectColumns as $columnName) |
| 112 | + { |
| 113 | + $selectParts[] = '`' . $columnName . '`'; |
| 114 | + } |
| 115 | + |
| 116 | + $whereParts = array(); |
| 117 | + foreach ($columnsToUpdate as $columnName) |
| 118 | + { |
| 119 | + $whereParts[] = '`' . $columnName . "` LIKE '%&%'"; |
| 120 | + } |
| 121 | + |
| 122 | + $rs = $db->getAllAssoc( |
| 123 | + 'SELECT ' . implode(', ', $selectParts) . ' FROM `' . $tableName . '` WHERE ' . implode(' OR ', $whereParts) |
| 124 | + ); |
| 125 | + |
| 126 | + foreach ($rs as $rowIndex => $row) |
| 127 | + { |
| 128 | + $updates = array(); |
| 129 | + foreach ($columnsToUpdate as $columnName) |
| 130 | + { |
| 131 | + if (!isset($row[$columnName])) |
| 132 | + { |
| 133 | + continue; |
| 134 | + } |
| 135 | + |
| 136 | + $originalValue = $row[$columnName]; |
| 137 | + $decodedValue = $originalValue; |
| 138 | + $maxDecodePasses = 10; |
| 139 | + for ($i = 0; $i < $maxDecodePasses; $i++) |
| 140 | + { |
| 141 | + $nextValue = html_entity_decode($decodedValue, ENT_QUOTES, HTML_ENCODING); |
| 142 | + if ($nextValue === $decodedValue) |
| 143 | + { |
| 144 | + break; |
| 145 | + } |
| 146 | + $decodedValue = $nextValue; |
| 147 | + } |
| 148 | + |
| 149 | + if ($decodedValue !== $originalValue) |
| 150 | + { |
| 151 | + $updates[] = '`' . $columnName . '` = ' . $db->makeQueryString($decodedValue); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + if (!empty($updates)) |
| 156 | + { |
| 157 | + $db->query( |
| 158 | + 'UPDATE `' . $tableName . '` SET ' . implode(', ', $updates) |
| 159 | + . ' WHERE `' . $tableData['primaryKey'] . '` = ' |
| 160 | + . $db->makeQueryInteger($row[$tableData['primaryKey']]) |
| 161 | + ); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | + |
| 168 | +?> |
0 commit comments