<?php

/*
 +--------------------------------------------------------------------+
 | CiviCRM version 3.0                                               |
 +--------------------------------------------------------------------+
 | Copyright CiviCRM LLC (c) 2004-2009                                |
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007.                                       |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License along with this program; if not, contact CiviCRM LLC       |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
*/

/*
 * Using this script you can update Email Greetings, Postal Greetings and Addressee for a specific contact type
 *
 * params for this script
 * ct=Individual or ct=Household or ct=Organization (ct = contact type)
 * gt=email_greeting or gt=postal_greeting or gt=addressee (gt = greeting )
 * id=greeting option value 
 *
 * IMPORTANT: You must first create valid option value before using via admin interface. 
 * Check option lists for Email Greetings, Postal Greetings and Addressee 
 */

class CRM_UpdateGreeting {
    
    function __construct() 
    {
        $this->initialize( );

        $config =& CRM_Core_Config::singleton();

        // this does not return on failure
        CRM_Utils_System::authenticateScript( true );
    }

    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 );
        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 );	
        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 = CRM_Utils_Request::retrieve( 'id', 'Positive', CRM_Core_DAO::$_nullArray );
        
        // 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 );
        $greetingsReturnProperties = array_flip( CRM_Utils_Array::value( 'contact', $greetingTokens ) ); 

        // 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(CRM_Core_DAO::$_nullArray, $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 ) {
            $greetingString = $originalGreetingString;	 
            CRM_Activity_BAO_Activity::replaceGreetingTokens($greetingString, $contactDetails, $contactID );
            $greetingString = CRM_Core_DAO::escapeString( $greetingString );
            $cacheFieldQuery .= " WHEN {$contactID} THEN '{$greetingString}' ";
            
            $contactIds[] = $contactID; 
        }     

        $cacheFieldQuery .= " ELSE {$greeting}_display
                              END;"; 

        // 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";
