3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
30 * This script recaches the display_name and sort_name values
35 * Class CRM_UpdateNameCache
37 class CRM_UpdateNameCache
{
41 function __construct() {
42 // you can run this program either from an apache command, or from the cli
43 if (php_sapi_name() == "cli") {
44 require_once("cli.php");
45 $cli = new civicrm_cli();
46 //if it doesn't die, it's authenticated
52 $config = CRM_Core_Config
::singleton();
54 // this does not return on failure
55 CRM_Utils_System
::authenticateScript(TRUE);
57 //log the execution time of script
58 CRM_Core_Error
::debug_log_message('UpdateNameCache.php');
62 function initialize() {
63 require_once '../civicrm.config.php';
64 require_once 'CRM/Core/Config.php';
66 $config = CRM_Core_Config
::singleton();
69 public function updateConstructedNames() {
70 require_once 'CRM/Utils/Address.php';
71 require_once 'CRM/Core/BAO/Preferences.php';
72 require_once 'CRM/Core/DAO.php';
73 require_once 'CRM/Core/PseudoConstant.php';
74 require_once 'CRM/Contact/BAO/Contact.php';
76 //handle individuals using settings in the system
77 $query = "SELECT * FROM civicrm_contact WHERE contact_type = 'Individual';";
78 $dao = CRM_Core_DAO
::executeQuery($query);
80 $prefixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
81 $suffixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
84 CRM_Utils_Hook
::tokens($tokens);
86 foreach ($tokens as $category => $catTokens) {
87 foreach ($catTokens as $token) {
88 $tokenFields[] = $token;
92 //determine sort name construction
93 $sortFormat = CRM_Core_BAO_Preferences
::value('sort_name_format');
94 $sortFormat = str_replace('contact.', '', $sortFormat);
96 //determine display name construction
97 $displayFormat = CRM_Core_BAO_Preferences
::value('display_name_format');
98 $displayFormat = str_replace('contact.', '', $displayFormat);
100 while ($dao->fetch()) {
101 $contactID = $dao->id
;
103 '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
,
109 $params['individual_prefix'] = $prefixes[$dao->prefix_id
];
110 $params['individual_suffix'] = $suffixes[$dao->suffix_id
];
112 $sortName = CRM_Utils_Address
::format($params, $sortFormat, FALSE, FALSE, $tokenFields);
113 $sortName = trim(CRM_Core_DAO
::escapeString($sortName));
115 $displayName = CRM_Utils_Address
::format($params, $displayFormat, FALSE, FALSE, $tokenFields);
116 $displayName = trim(CRM_Core_DAO
::escapeString($displayName));
119 if (empty($sortName) ||
empty($displayName)) {
121 $email = CRM_Contact_BAO_Contact
::getPrimaryEmail($contactID);
126 if (empty($sortName)) {
130 if (empty($displayName)) {
131 $displayName = $email;
136 $updateQuery = "UPDATE civicrm_contact SET display_name = '$displayName', sort_name = '$sortName' WHERE id = $contactID;";
137 CRM_Core_DAO
::executeQuery($updateQuery);
140 echo "\n Individuals recached... ";
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... ";
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... ";
158 //end updateConstructedNames
161 $obj = new CRM_UpdateNameCache();
163 echo "\n Updating display_name and sort_name for all contacts. ";
164 $obj->updateConstructedNames();
165 echo "\n\n Processing complete. \n";