Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
5b71fd5f | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
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 | ||
6a488035 TO |
29 | /* |
30 | * This script recaches the display_name and sort_name values | |
31 | * | |
32 | */ | |
a1a55b61 EM |
33 | |
34 | /** | |
35 | * Class CRM_UpdateNameCache | |
36 | */ | |
6a488035 | 37 | class CRM_UpdateNameCache { |
a1a55b61 EM |
38 | /** |
39 | * | |
40 | */ | |
6a488035 TO |
41 | function __construct() { |
42 | // you can run this program either from an apache command, or from the cli | |
43 | if (php_sapi_name() == "cli") { | |
b7c0a88f | 44 | require_once("cli.php"); |
6a488035 TO |
45 | $cli = new civicrm_cli(); |
46 | //if it doesn't die, it's authenticated | |
47 | } | |
48 | else { | |
49 | //from the webserver | |
50 | $this->initialize(); | |
51 | ||
52 | $config = CRM_Core_Config::singleton(); | |
53 | ||
54 | // this does not return on failure | |
55 | CRM_Utils_System::authenticateScript(TRUE); | |
56 | ||
57 | //log the execution time of script | |
58 | CRM_Core_Error::debug_log_message('UpdateNameCache.php'); | |
59 | } | |
60 | } | |
61 | ||
62 | function initialize() { | |
63 | require_once '../civicrm.config.php'; | |
64 | require_once 'CRM/Core/Config.php'; | |
65 | ||
66 | $config = CRM_Core_Config::singleton(); | |
67 | } | |
68 | ||
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'; | |
75 | ||
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); | |
79 | ||
e6c4755b CW |
80 | $prefixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'); |
81 | $suffixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id'); | |
6a488035 | 82 | |
b7c0a88f | 83 | $tokens = []; |
6a488035 | 84 | CRM_Utils_Hook::tokens($tokens); |
b7c0a88f | 85 | $tokenFields = []; |
6a488035 TO |
86 | foreach ($tokens as $category => $catTokens) { |
87 | foreach ($catTokens as $token) { | |
88 | $tokenFields[] = $token; | |
89 | } | |
90 | } | |
91 | ||
92 | //determine sort name construction | |
93 | $sortFormat = CRM_Core_BAO_Preferences::value('sort_name_format'); | |
94 | $sortFormat = str_replace('contact.', '', $sortFormat); | |
95 | ||
96 | //determine display name construction | |
97 | $displayFormat = CRM_Core_BAO_Preferences::value('display_name_format'); | |
98 | $displayFormat = str_replace('contact.', '', $displayFormat); | |
99 | ||
100 | while ($dao->fetch()) { | |
101 | $contactID = $dao->id; | |
b7c0a88f | 102 | $params = [ |
103 | 'first_name' => $dao->first_name, | |
6a488035 TO |
104 | 'middle_name' => $dao->middle_name, |
105 | 'last_name' => $dao->last_name, | |
106 | 'prefix_id' => $dao->prefix_id, | |
107 | 'suffix_id' => $dao->suffix_id, | |
b7c0a88f | 108 | ]; |
6a488035 TO |
109 | $params['individual_prefix'] = $prefixes[$dao->prefix_id]; |
110 | $params['individual_suffix'] = $suffixes[$dao->suffix_id]; | |
111 | ||
4c49535e | 112 | $sortName = CRM_Utils_Address::format($params, $sortFormat, FALSE, FALSE, $tokenFields); |
6a488035 TO |
113 | $sortName = trim(CRM_Core_DAO::escapeString($sortName)); |
114 | ||
4c49535e | 115 | $displayName = CRM_Utils_Address::format($params, $displayFormat, FALSE, FALSE, $tokenFields); |
6a488035 TO |
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 |