3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This code is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
14 * This script recaches the display_name and sort_name values
19 * Class CRM_UpdateNameCache
21 class CRM_UpdateNameCache
{
25 function __construct() {
26 // you can run this program either from an apache command, or from the cli
27 if (php_sapi_name() == "cli") {
28 require_once("cli.php");
29 $cli = new civicrm_cli();
30 //if it doesn't die, it's authenticated
36 $config = CRM_Core_Config
::singleton();
38 // this does not return on failure
39 CRM_Utils_System
::authenticateScript(TRUE);
41 //log the execution time of script
42 CRM_Core_Error
::debug_log_message('UpdateNameCache.php');
46 function initialize() {
47 require_once '../civicrm.config.php';
48 require_once 'CRM/Core/Config.php';
50 $config = CRM_Core_Config
::singleton();
53 public function updateConstructedNames() {
54 require_once 'CRM/Utils/Address.php';
55 require_once 'CRM/Core/BAO/Preferences.php';
56 require_once 'CRM/Core/DAO.php';
57 require_once 'CRM/Core/PseudoConstant.php';
58 require_once 'CRM/Contact/BAO/Contact.php';
60 //handle individuals using settings in the system
61 $query = "SELECT * FROM civicrm_contact WHERE contact_type = 'Individual';";
62 $dao = CRM_Core_DAO
::executeQuery($query);
64 $prefixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
65 $suffixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
68 CRM_Utils_Hook
::tokens($tokens);
70 foreach ($tokens as $category => $catTokens) {
71 foreach ($catTokens as $token) {
72 $tokenFields[] = $token;
76 //determine sort name construction
77 $sortFormat = CRM_Core_BAO_Preferences
::value('sort_name_format');
78 $sortFormat = str_replace('contact.', '', $sortFormat);
80 //determine display name construction
81 $displayFormat = CRM_Core_BAO_Preferences
::value('display_name_format');
82 $displayFormat = str_replace('contact.', '', $displayFormat);
84 while ($dao->fetch()) {
85 $contactID = $dao->id
;
87 'first_name' => $dao->first_name
,
88 'middle_name' => $dao->middle_name
,
89 'last_name' => $dao->last_name
,
90 'prefix_id' => $dao->prefix_id
,
91 'suffix_id' => $dao->suffix_id
,
93 $params['individual_prefix'] = $prefixes[$dao->prefix_id
];
94 $params['individual_suffix'] = $suffixes[$dao->suffix_id
];
96 $sortName = CRM_Utils_Address
::format($params, $sortFormat, FALSE, FALSE, $tokenFields);
97 $sortName = trim(CRM_Core_DAO
::escapeString($sortName));
99 $displayName = CRM_Utils_Address
::format($params, $displayFormat, FALSE, FALSE, $tokenFields);
100 $displayName = trim(CRM_Core_DAO
::escapeString($displayName));
103 if (empty($sortName) ||
empty($displayName)) {
105 $email = CRM_Contact_BAO_Contact
::getPrimaryEmail($contactID);
110 if (empty($sortName)) {
114 if (empty($displayName)) {
115 $displayName = $email;
120 $updateQuery = "UPDATE civicrm_contact SET display_name = '$displayName', sort_name = '$sortName' WHERE id = $contactID;";
121 CRM_Core_DAO
::executeQuery($updateQuery);
124 echo "\n Individuals recached... ";
127 $query = "UPDATE civicrm_contact
128 SET display_name = organization_name,
129 sort_name = organization_name
130 WHERE contact_type = 'Organization';";
131 $dao = CRM_Core_DAO
::executeQuery($query);
132 echo "\n Organizations recached... ";
135 $query = "UPDATE civicrm_contact
136 SET display_name = household_name,
137 sort_name = household_name
138 WHERE contact_type = 'Household';";
139 $dao = CRM_Core_DAO
::executeQuery($query);
140 echo "\n Households recached... ";
142 //end updateConstructedNames
145 $obj = new CRM_UpdateNameCache();
147 echo "\n Updating display_name and sort_name for all contacts. ";
148 $obj->updateConstructedNames();
149 echo "\n\n Processing complete. \n";