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