initialize( ); $config = CRM_Core_Config::singleton(); // this does not return on failure CRM_Utils_System::authenticateScript( true ); //log the execution time of script CRM_Core_Error::debug_log_message( 'UpdateGreeting.php'); } function initialize( ) { require_once '../civicrm.config.php'; require_once 'CRM/Core/Config.php'; require_once 'CRM/Utils/Request.php'; require_once 'CRM/Core/PseudoConstant.php'; require_once 'CRM/Contact/BAO/Contact.php'; } public function updateGreeting( ) { $config = CRM_Core_Config::singleton(); $contactType = CRM_Utils_Request::retrieve( 'ct', 'String', CRM_Core_DAO::$_nullArray, false, null, 'REQUEST' ); if ( ! in_array( $contactType, array( 'Individual', 'Household', 'Organization' ) ) ) { CRM_Core_Error::fatal( ts('Invalid Contact Type.') ); } $greeting = CRM_Utils_Request::retrieve( 'gt', 'String', CRM_Core_DAO::$_nullArray, false, null, 'REQUEST' ); if ( ! in_array( $greeting, array( 'email_greeting', 'postal_greeting', 'addressee' ) ) ) { CRM_Core_Error::fatal( ts('Invalid Greeting Type.') ); } if ( in_array( $greeting, array( 'email_greeting', 'postal_greeting' ) ) && $contactType == 'Organization' ) { CRM_Core_Error::fatal( ts('You cannot use %1 for contact type %2.', array( 1 => $greeting, 2 => $contactType) ) ); } $valueID = $id = CRM_Utils_Request::retrieve( 'id', 'Positive', CRM_Core_DAO::$_nullArray, false, null, 'REQUEST' ); // if valueID is not passed use default value if ( !$valueID ) { require_once 'CRM/Core/OptionGroup.php'; $contactTypeFilters = array( 1 => 'Individual', 2 => 'Household', 3 => 'Organization' ); $filter = CRM_Utils_Array::key( $contactType, $contactTypeFilters ); $defaulValueID = CRM_Core_OptionGroup::values( $greeting, null, null, null, " AND is_default = 1 AND ( filter = {$filter} OR filter = 0 )", "value"); $valueID = array_pop( $defaulValueID ); } $filter = array( 'contact_type' => $contactType, 'greeting_type' => $greeting ); $allGreetings = CRM_Core_PseudoConstant::greeting( $filter ); $originalGreetingString = $greetingString = CRM_Utils_Array::value( $valueID, $allGreetings ); if ( !$greetingString ) { CRM_Core_Error::fatal( ts('Incorrect greeting value id %1.', array( 1 => $valueID ) ) ); } // build return properties based on tokens require_once 'CRM/Activity/BAO/Activity.php'; $greetingTokens = CRM_Activity_BAO_Activity::getTokens( $greetingString ); $tokens = CRM_Utils_Array::value( 'contact', $greetingTokens ); $greetingsReturnProperties = array( ); if ( is_array( $tokens ) ) { $greetingsReturnProperties = array_fill_keys( array_values( $tokens ), 1 ); } //process all contacts only when force pass. $force = CRM_Utils_Request::retrieve( 'force', 'String', CRM_Core_DAO::$_nullArray, false, null, 'REQUEST' ); $processAll = false; if ( in_array( $force, array( 1, 'true' ) ) ) { $processAll = true; } elseif ( $force == 2 ) { $processOnlyIdSet = true; } //FIXME : apiQuery should handle these clause. $filterContactFldIds = $filterIds = array( ); if ( !$processAll ) { $idFldName = $displayFldName = null; if ( $greeting == 'email_greeting' || $greeting == 'postal_greeting' || $greeting == 'addressee' ) { $idFldName = $greeting . '_id'; $displayFldName = $greeting . '_display'; } if ( $idFldName ) { $sql = " SELECT DISTINCT id, $idFldName FROM civicrm_contact WHERE contact_type = %1 AND ( {$idFldName} IS NULL OR ( {$idFldName} IS NOT NULL AND {$displayFldName} IS NULL ) ) "; $dao = CRM_Core_DAO::executeQuery( $sql, array( 1 => array( $contactType, 'String' ) ) ); while ( $dao->fetch( ) ) { $filterContactFldIds[$dao->id] = $dao->$idFldName; if (!CRM_Utils_System::isNull( $dao->$idFldName)) { $filterIds[$dao->id] = $dao->$idFldName; } } } if ( empty( $filterContactFldIds ) ) { $filterContactFldIds[] = 0; } } // retrieve only required contact information require_once 'CRM/Mailing/BAO/Mailing.php'; $extraParams[] = array( 'contact_type', '=', $contactType, 0, 0 ); list($greetingDetails) = CRM_Mailing_BAO_Mailing::getDetails( array_keys( $filterContactFldIds ), $greetingsReturnProperties, false, false, $extraParams ); // perform token replacement and build update SQL $contactIds = array( ); $cacheFieldQuery = "UPDATE civicrm_contact SET {$greeting}_display = CASE id "; foreach ( $greetingDetails as $contactID => $contactDetails ) { if ( !$processAll && !array_key_exists( $contactID, $filterContactFldIds ) ) { continue; } if ( $processOnlyIdSet ) { if ( !array_key_exists( $contactID, $filterIds ) ) { continue; } if ( $id ) { $greetingString = $originalGreetingString; $contactIds[] = $contactID; } else { if ( $greetingBuffer = CRM_Utils_Array::value($filterContactFldIds[$contactID], $allGreetings) ) { $greetingString = $greetingBuffer; } } $allContactIds[] = $contactID; } else { $greetingString = $originalGreetingString; if ( $greetingBuffer = CRM_Utils_Array::value($filterContactFldIds[$contactID], $allGreetings) ) { $greetingString = $greetingBuffer; } else { $contactIds[] = $contactID; } } CRM_Activity_BAO_Activity::replaceGreetingTokens($greetingString, $contactDetails, $contactID ); $greetingString = CRM_Core_DAO::escapeString( $greetingString ); $cacheFieldQuery .= " WHEN {$contactID} THEN '{$greetingString}' "; $allContactIds[] = $contactID; } if ( !empty( $allContactIds ) ) { $cacheFieldQuery .= " ELSE {$greeting}_display END;"; if ( !empty( $contactIds ) ) { // need to update greeting _id field. $queryString = " UPDATE civicrm_contact SET {$greeting}_id = {$valueID} WHERE id IN (" . implode( ',', $contactIds ) . ")"; CRM_Core_DAO::executeQuery( $queryString ); } // now update cache field CRM_Core_DAO::executeQuery( $cacheFieldQuery ); } } } $obj = new CRM_UpdateGreeting( ); $obj->updateGreeting( ); echo "\n\n Greeting is updated for contact(s). (Done) \n";