3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.1 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2011 |
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 +--------------------------------------------------------------------+
31 * This script recaches the display_name and sort_name values
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
46 $config = CRM_Core_Config
::singleton();
48 // this does not return on failure
49 CRM_Utils_System
::authenticateScript(TRUE);
51 //log the execution time of script
52 CRM_Core_Error
::debug_log_message('UpdateNameCache.php');
56 function initialize() {
57 require_once '../civicrm.config.php';
58 require_once 'CRM/Core/Config.php';
60 $config = CRM_Core_Config
::singleton();
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';
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);
74 $prefixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
75 $suffixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
78 CRM_Utils_Hook
::tokens($tokens);
79 $tokenFields = array();
80 foreach ($tokens as $category => $catTokens) {
81 foreach ($catTokens as $token) {
82 $tokenFields[] = $token;
86 //determine sort name construction
87 $sortFormat = CRM_Core_BAO_Preferences
::value('sort_name_format');
88 $sortFormat = str_replace('contact.', '', $sortFormat);
90 //determine display name construction
91 $displayFormat = CRM_Core_BAO_Preferences
::value('display_name_format');
92 $displayFormat = str_replace('contact.', '', $displayFormat);
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
,
102 $params['individual_prefix'] = $prefixes[$dao->prefix_id
];
103 $params['individual_suffix'] = $suffixes[$dao->suffix_id
];
105 $sortName = CRM_Utils_Address
::format($params, $sortFormat, FALSE, FALSE, TRUE, $tokenFields);
106 $sortName = trim(CRM_Core_DAO
::escapeString($sortName));
108 $displayName = CRM_Utils_Address
::format($params, $displayFormat, FALSE, FALSE, TRUE, $tokenFields);
109 $displayName = trim(CRM_Core_DAO
::escapeString($displayName));
112 if (empty($sortName) ||
empty($displayName)) {
114 $email = CRM_Contact_BAO_Contact
::getPrimaryEmail($contactID);
119 if (empty($sortName)) {
123 if (empty($displayName)) {
124 $displayName = $email;
129 $updateQuery = "UPDATE civicrm_contact SET display_name = '$displayName', sort_name = '$sortName' WHERE id = $contactID;";
130 CRM_Core_DAO
::executeQuery($updateQuery);
133 echo "\n Individuals recached... ";
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... ";
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... ";
151 //end updateConstructedNames
154 $obj = new CRM_UpdateNameCache();
156 echo "\n Updating display_name and sort_name for all contacts. ";
157 $obj->updateConstructedNames();
158 echo "\n\n Processing complete. \n";