Merge pull request #15778 from agh1/reminder-do-not-email-test
[civicrm-core.git] / tools / bin / scripts / updateNameCache.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12
13 /*
14 * This script recaches the display_name and sort_name values
15 *
16 */
17
18 /**
19 * Class CRM_UpdateNameCache
20 */
21 class CRM_UpdateNameCache {
22 /**
23 *
24 */
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
31 }
32 else {
33 //from the webserver
34 $this->initialize();
35
36 $config = CRM_Core_Config::singleton();
37
38 // this does not return on failure
39 CRM_Utils_System::authenticateScript(TRUE);
40
41 //log the execution time of script
42 CRM_Core_Error::debug_log_message('UpdateNameCache.php');
43 }
44 }
45
46 function initialize() {
47 require_once '../civicrm.config.php';
48 require_once 'CRM/Core/Config.php';
49
50 $config = CRM_Core_Config::singleton();
51 }
52
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';
59
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);
63
64 $prefixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id');
65 $suffixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id');
66
67 $tokens = [];
68 CRM_Utils_Hook::tokens($tokens);
69 $tokenFields = [];
70 foreach ($tokens as $category => $catTokens) {
71 foreach ($catTokens as $token) {
72 $tokenFields[] = $token;
73 }
74 }
75
76 //determine sort name construction
77 $sortFormat = CRM_Core_BAO_Preferences::value('sort_name_format');
78 $sortFormat = str_replace('contact.', '', $sortFormat);
79
80 //determine display name construction
81 $displayFormat = CRM_Core_BAO_Preferences::value('display_name_format');
82 $displayFormat = str_replace('contact.', '', $displayFormat);
83
84 while ($dao->fetch()) {
85 $contactID = $dao->id;
86 $params = [
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,
92 ];
93 $params['individual_prefix'] = $prefixes[$dao->prefix_id];
94 $params['individual_suffix'] = $suffixes[$dao->suffix_id];
95
96 $sortName = CRM_Utils_Address::format($params, $sortFormat, FALSE, FALSE, $tokenFields);
97 $sortName = trim(CRM_Core_DAO::escapeString($sortName));
98
99 $displayName = CRM_Utils_Address::format($params, $displayFormat, FALSE, FALSE, $tokenFields);
100 $displayName = trim(CRM_Core_DAO::escapeString($displayName));
101
102 //check for email
103 if (empty($sortName) || empty($displayName)) {
104 $email = NULL;
105 $email = CRM_Contact_BAO_Contact::getPrimaryEmail($contactID);
106 if (empty($email)) {
107 $email = $contactID;
108 }
109
110 if (empty($sortName)) {
111
112 $sortName = $email;
113 }
114 if (empty($displayName)) {
115 $displayName = $email;
116 }
117 }
118
119 //update record
120 $updateQuery = "UPDATE civicrm_contact SET display_name = '$displayName', sort_name = '$sortName' WHERE id = $contactID;";
121 CRM_Core_DAO::executeQuery($updateQuery);
122 }
123 //end indiv
124 echo "\n Individuals recached... ";
125
126 //set organizations
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... ";
133
134 //set households
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... ";
141 }
142 //end updateConstructedNames
143 }
144
145 $obj = new CRM_UpdateNameCache();
146
147 echo "\n Updating display_name and sort_name for all contacts. ";
148 $obj->updateConstructedNames();
149 echo "\n\n Processing complete. \n";
150