| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
| 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 | /* |
| 31 | * This script recaches the display_name and sort_name values |
| 32 | * |
| 33 | */ |
| 34 | |
| 35 | /** |
| 36 | * Class CRM_UpdateNameCache |
| 37 | */ |
| 38 | class CRM_UpdateNameCache { |
| 39 | /** |
| 40 | * |
| 41 | */ |
| 42 | function __construct() { |
| 43 | // you can run this program either from an apache command, or from the cli |
| 44 | if (php_sapi_name() == "cli") { |
| 45 | require_once ("cli.php"); |
| 46 | $cli = new civicrm_cli(); |
| 47 | //if it doesn't die, it's authenticated |
| 48 | } |
| 49 | else { |
| 50 | //from the webserver |
| 51 | $this->initialize(); |
| 52 | |
| 53 | $config = CRM_Core_Config::singleton(); |
| 54 | |
| 55 | // this does not return on failure |
| 56 | CRM_Utils_System::authenticateScript(TRUE); |
| 57 | |
| 58 | //log the execution time of script |
| 59 | CRM_Core_Error::debug_log_message('UpdateNameCache.php'); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | function initialize() { |
| 64 | require_once '../civicrm.config.php'; |
| 65 | require_once 'CRM/Core/Config.php'; |
| 66 | |
| 67 | $config = CRM_Core_Config::singleton(); |
| 68 | } |
| 69 | |
| 70 | public function updateConstructedNames() { |
| 71 | require_once 'CRM/Utils/Address.php'; |
| 72 | require_once 'CRM/Core/BAO/Preferences.php'; |
| 73 | require_once 'CRM/Core/DAO.php'; |
| 74 | require_once 'CRM/Core/PseudoConstant.php'; |
| 75 | require_once 'CRM/Contact/BAO/Contact.php'; |
| 76 | |
| 77 | //handle individuals using settings in the system |
| 78 | $query = "SELECT * FROM civicrm_contact WHERE contact_type = 'Individual';"; |
| 79 | $dao = CRM_Core_DAO::executeQuery($query); |
| 80 | |
| 81 | $prefixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'); |
| 82 | $suffixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id'); |
| 83 | |
| 84 | $tokens = array(); |
| 85 | CRM_Utils_Hook::tokens($tokens); |
| 86 | $tokenFields = array(); |
| 87 | foreach ($tokens as $category => $catTokens) { |
| 88 | foreach ($catTokens as $token) { |
| 89 | $tokenFields[] = $token; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | //determine sort name construction |
| 94 | $sortFormat = CRM_Core_BAO_Preferences::value('sort_name_format'); |
| 95 | $sortFormat = str_replace('contact.', '', $sortFormat); |
| 96 | |
| 97 | //determine display name construction |
| 98 | $displayFormat = CRM_Core_BAO_Preferences::value('display_name_format'); |
| 99 | $displayFormat = str_replace('contact.', '', $displayFormat); |
| 100 | |
| 101 | while ($dao->fetch()) { |
| 102 | $contactID = $dao->id; |
| 103 | $params = array('first_name' => $dao->first_name, |
| 104 | 'middle_name' => $dao->middle_name, |
| 105 | 'last_name' => $dao->last_name, |
| 106 | 'prefix_id' => $dao->prefix_id, |
| 107 | 'suffix_id' => $dao->suffix_id, |
| 108 | ); |
| 109 | $params['individual_prefix'] = $prefixes[$dao->prefix_id]; |
| 110 | $params['individual_suffix'] = $suffixes[$dao->suffix_id]; |
| 111 | |
| 112 | $sortName = CRM_Utils_Address::format($params, $sortFormat, FALSE, FALSE, $tokenFields); |
| 113 | $sortName = trim(CRM_Core_DAO::escapeString($sortName)); |
| 114 | |
| 115 | $displayName = CRM_Utils_Address::format($params, $displayFormat, FALSE, FALSE, $tokenFields); |
| 116 | $displayName = trim(CRM_Core_DAO::escapeString($displayName)); |
| 117 | |
| 118 | //check for email |
| 119 | if (empty($sortName) || empty($displayName)) { |
| 120 | $email = NULL; |
| 121 | $email = CRM_Contact_BAO_Contact::getPrimaryEmail($contactID); |
| 122 | if (empty($email)) { |
| 123 | $email = $contactID; |
| 124 | } |
| 125 | |
| 126 | if (empty($sortName)) { |
| 127 | |
| 128 | $sortName = $email; |
| 129 | } |
| 130 | if (empty($displayName)) { |
| 131 | $displayName = $email; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | //update record |
| 136 | $updateQuery = "UPDATE civicrm_contact SET display_name = '$displayName', sort_name = '$sortName' WHERE id = $contactID;"; |
| 137 | CRM_Core_DAO::executeQuery($updateQuery); |
| 138 | } |
| 139 | //end indiv |
| 140 | echo "\n Individuals recached... "; |
| 141 | |
| 142 | //set organizations |
| 143 | $query = "UPDATE civicrm_contact |
| 144 | SET display_name = organization_name, |
| 145 | sort_name = organization_name |
| 146 | WHERE contact_type = 'Organization';"; |
| 147 | $dao = CRM_Core_DAO::executeQuery($query); |
| 148 | echo "\n Organizations recached... "; |
| 149 | |
| 150 | //set households |
| 151 | $query = "UPDATE civicrm_contact |
| 152 | SET display_name = household_name, |
| 153 | sort_name = household_name |
| 154 | WHERE contact_type = 'Household';"; |
| 155 | $dao = CRM_Core_DAO::executeQuery($query); |
| 156 | echo "\n Households recached... "; |
| 157 | } |
| 158 | //end updateConstructedNames |
| 159 | } |
| 160 | |
| 161 | $obj = new CRM_UpdateNameCache(); |
| 162 | |
| 163 | echo "\n Updating display_name and sort_name for all contacts. "; |
| 164 | $obj->updateConstructedNames(); |
| 165 | echo "\n\n Processing complete. \n"; |
| 166 | |