<?php

function civitest_help( $section ) {
	switch ($section) { 
	case 'admin/help#civitest':
        return t( 'CiviTest module v0.01' );

    case 'admin/modules#description':
        return t( 'CiviTest module v0.01' );

    }
}

function civitest_civicrm_post( $op, $objectName, $objectId, &$objectRef ) {
    // only interested in the profile object and create operation for now
    if ( $objectName != 'Profile' || ( $op != 'create' && $op != 'edit' ) ) {
        // send it to custom hook
        return false;
    }

    // send an email to the user and cc administrator
    // with a welcome message
    civicrm_initialize( true );

    require_once 'CRM/Utils/Mail.php';

    $fromName  = 'My Org Administrator';
    $fromEmail = 'info@example.org';
    $from      = '"' . $fromName . '" <' . $fromEmail . '>';

    $toEmail   = $objectRef['email-1'];
    $toName    = "{$objectRef['first_name']} {$objectRef['last_name']}";

    $params    = print_r( $objectRef, true );
    $subject   = "Thank you for supporting My Org";
    $message   = "
Dear $toName:

Thank you for your show of support. The details u signed up with are:

$params

Regards

My Org Team
";
    $cc       = 'myorg@example.org';

    CRM_Utils_Mail::send( $from,
                          $toName,
                          $toEmail,
                          $subject,
                          $message,
                          $cc );

}

function civitest_civicrm_custom( $op, $groupID, $entityID, &$params ) {
    if ( $op != 'create' && $op != 'edit' ) {
        return;
    }
    
    // this is the custom group i am interested in updating when the row is updated
    if ( $groupID != 1 ) {
        return;
    }

    $tableName = CRM_Core_DAO::getFieldValue( 'CRM_Core_DAO_CustomGroup',
                                              $groupID,
                                              'table_name' );
    $sql = "
UPDATE $tableName
SET    random_code_data_3 = 23
WHERE  entity_id = $entityID
";
    CRM_Core_DAO::executeQuery( $sql,
                                CRM_Core_DAO::$_nullArray );
}

/**
 * Get the permissioned where clause for the user
 *
 * @param int $type the type of permission needed
 * @param  array $tables (reference ) add the tables that are needed for the select clause
 * @param  array $whereTables (reference ) add the tables that are needed for the where clause
 * @param int    $contactID the contactID for whom the check is made
 *
 * @return string the group where clause for this user
 * @access public
 */
function civitest_civicrm_aclWhereClause( $type, &$tables, &$whereTables, &$contactID, &$where ) {
    if ( ! $contactID ) {
        return;
    }

    $permissionTable = 'civicrm_value_permission';
    $regionTable     = 'civicrm_value_region';
    $fields          = array( 'electorate' => 'Integer',
                              'province'   => 'Integer',
                              'branch'     => 'Integer' );

    // get all the values from the permission table for this contact
    $keys = implode( ', ', array_keys( $fields ) );
    $sql = "
SELECT $keys
FROM   {$permissionTable}
WHERE  entity_id = $contactID
";
    $dao = CRM_Core_DAO::executeQuery( $sql,
                                       CRM_Core_DAO::$_nullArray );
    if ( ! $dao->fetch( ) ) {
        return;
    }

    $tables[$regionTable] = $whereTables[$regionTable] =
        "LEFT JOIN {$regionTable} regionTable ON contact_a.id = regionTable.entity_id";

    $clauses = array( );
    foreach( $fields as $field => $fieldType ) {
        if ( ! empty( $dao->$field ) ) {
            if ( strpos( CRM_Core_DAO::VALUE_SEPARATOR, $dao->$field ) !== false ) {
                $value = substr( $dao->$field, 1, -1 );
                $values = explode( CRM_Core_DAO::VALUE_SEPARATOR, $value );
                foreach ( $values as $v ) {
                    $clauses[] = "regionTable.{$field} = $v";
                }
            } else {
                if ( $fieldType == 'String' ) {
                    $clauses[] = "regionTable.{$field} = '{$dao->$field}'";
                } else {
                    $clauses[] = "regionTable.{$field} = {$dao->$field}";
                }
            }
        }
    }

    if ( ! empty( $clauses ) ) {
        $where .= ' AND (' . implode( ' OR ', $clauses ) . ')';
    }
}

function civitest_civicrm_dashboard( $contactID, &$contentPlacement ) {
    // REPLACE Activity Listing with custom content
    $contentPlacement = 3;
    return array( 'Custom Content' => "Here is some custom content: $contactID",
                  'Custom Table' => "
<table>
<tr><th>Contact Name</th><th>Date</th></tr>
<tr><td>Foo</td><td>Bar</td></tr>
<tr><td>Goo</td><td>Tar</td></tr>
</table>
",
                  );
}

function civitest_civicrm_buildAmount( $pageType,
                                      &$form,
                                      &$amount ) {

    // only modify the contributon page with id = 1
    if ( $pageType != 'contribution' ||
         $form->_id != 1 ) {
       return;
    }

    // lets add an arbitrary amount here, just to show folks
    // the power of a hook :)
    $amount[1000] = array( 'value'    => 400,
                           'label'     => 'Hook',
                           'amount_id' => 1000 );
    

    // now lets get a bit more ambitious
    // *GOAL*: lets plan to give 20% discount to students
    $membershipType  = 'Student';
    $discountPercent = 20; 

    // get the membership-type-id for the membership-type
    $membershipTypeId = CRM_Core_DAO::getFieldValue( 'CRM_Member_DAO_MembershipType', 
                                                     $membershipType,
                                                     'id',
                                                     'name' );

    // get the logged in user id 
    $session =& CRM_Core_Session::singleton();
    $userID  = $session->get( 'userID' );

    if ( $userID ) {
        // check if logged in user has 'Student' membership
        require_once 'CRM/Member/BAO/Membership.php';
        $membership = CRM_Member_BAO_Membership::getContactMembership( $userID, $membershipTypeId, null );
        
        // If logged in contact is a member as on today, modify the amount
        // to reflect the discount.
        if ( CRM_Utils_Array::value( 'is_current_member', $membership ) ) {
            foreach ( $amount as $amountId => $amountInfo ) {
                $amount[$amountId]['value'] = $amount[$amountId]['value'] - 
                    ceil($amount[$amountId]['value'] * $discountPercent / 100);
                $amount[$amountId]['label'] = $amount[$amountId]['label'] . 
                    "\t - with {$discountPercent}% discount (for $membershipType)";
            }
        }
    }
}

function civitest_civicrm_aclGroup( $type, $contactID, $tableName, &$allGroups, &$currentGroups ) {
    // only process saved search
    if ( $tableName != 'civicrm_saved_search' ) {
        return;
    }

    hrd_initialize( );

    $currentGroups = $allGroups;

    if ( ! CRM_Core_Permission::check( 'access secure contacts' ) ) {
        unset( $currentGroups[HRD_SECURE_GROUP_ID] );
    }

    $currentGroups = array_keys( $currentGroups );
}

function civitest_civicrm_tabs( &$tabs, $contactID ) {

    // unset the contribition tab
    unset( $tabs[1] );
    
    // lets rename the contribution tab with a differnt name and put it last
    // this is just a demo, in the real world, you would create a url which would
    // return an html snippet etc
    $url = CRM_Utils_System::url( 'civicrm/contact/view/contribution',
                                  "reset=1&snippet=1&force=1&cid=$contactID" );
    $tabs[] = array( 'id'    => 'mySupercoolTab',
                     'url'   => $url,
                     'title' => 'Contribution Tab Renamed',
                     'weight' => 300 );
}


function civitest_civicrm_tokens( &$tokens ) { 
    $tokens['contribution'] = array( 'contribution.amount', 'contribution.date' );
}

function civitest_civicrm_tokenValues( &$values, &$contactIDs ) {
    if ( is_array( $contactIDs ) ) {
        $contactIDString = implode( ',', array_values( $contactIDs ) );
        $single = false;
    } else {
        $contactIDString = "( $contactIDs )";
        $single = true;
    }

    $query = "
SELECT sum( total_amount ) as total_amount,
       contact_id,
       max( receive_date ) as receive_date
FROM   civicrm_contribution 
WHERE  contact_id IN ( $contactIDString )
AND    is_test = 0
GROUP BY contact_id
";

    $dao = CRM_Core_DAO::executeQuery( $query );
    while ( $dao->fetch( ) ) {
        if ( $single ) {
            $value =& $values;
        } else {
            if ( ! array_key_exists( $dao->contact_id, $values ) ) {
                $values[$dao->contact_id] = array( );
            }
            $value =& $values[$dao->contact_id];
        }

        $value['contribution.amount'] = $dao->total_amount;
        $value['contribution.date'  ] = $dao->receive_date;
    }
}

function civitest_civicrm_pageRun( &$page ) {
    // You can assign variables to the template using:
    // $page->assign( 'varName', $varValue );
    // in your template, {$varName} will output the contents of $varValue
    // you should customize your template if doing so

    $page->assign( 'varName', 'This is a variable assigned by the hook' );
}

/*
 * The hook_nodeapi implementation to set the node title to that of event title.
 * Could also be used set the title to anything we want. 
 *
 */
function civicrm_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
    if ($op == 'load' && $node->type == 'event') {
        $node->title = civicrm_cck_event_field_values('title', $node->field_title[0]['value']);
    }
}

/*
 * The function to pull out event data from civicrm for cck fields related to event. 
 *
 */
function civicrm_cck_event_field_values($field, $index = null) {
    static $eventInfo;
    if ( ! $eventInfo ) {
        if ( ! civicrm_initialize( ) ) {
            return;
        }
        require_once 'api/v2/Event.php';
        $params    = array( );
        $eventInfo = civicrm_event_search( $params );
    }

    if ( strpos($_GET['q'], 'edit') || 
         strpos($_GET['q'], 'add')  ) {
        $isAppend = true;
    }
    if ( isset( $index ) ) {
        $isAppend = false;
    }

    $retArray = array( );
    switch( $field ) {
    case 'when': 
        foreach ( $eventInfo as $info ) {
            $str = "{$info['start_date']} >  through > {$info['end_date']}";
            $retArray[] = $isAppend ? ("{$info['title']}:: " . $str) : $str;
        }
        break;

    case 'location': 
        foreach ( $eventInfo as $info ) {
            $params = array( 'entity_id' => $info['id'],'entity_table' => 'civicrm_event');
            require_once 'CRM/Core/BAO/Location.php';
            $values['location'] = CRM_Core_BAO_Location::getValues( $params, true );
            $str = $values['location']['address'][1]['display'];
            $retArray[] = $isAppend ? ("{$info['title']}:: " . $str) : $str;
        }
        break;

    case 'register_link': 
        foreach ( $eventInfo as $info ) {
            $str = '<a href="' . 
                CRM_Utils_System::url( 'civicrm/event/register', 
                                       "id={$info['id']}&reset=1", true ) . 
                '">&raquo; Register Now</a>';
            $retArray[] = $isAppend ? ("{$info['title']}:: " . $str) : $str;
        }
        break;

    case 'feeblock' : 
        require_once 'CRM/Core/BAO/Discount.php';
        require_once 'CRM/Core/OptionGroup.php';
        foreach ( $eventInfo as $info ) {
            $discountId = CRM_Core_BAO_Discount::findSet( $info['id'], 'civicrm_event' );
            if ( $discountId ) {
                CRM_Core_OptionGroup::getAssoc( CRM_Core_DAO::getFieldValue( 'CRM_Core_DAO_Discount',
                                                                             $discountId, 'option_group_id' ),
                                                $feeBlock, true, 'id' );
            } else {
                CRM_Core_OptionGroup::getAssoc( "civicrm_event.amount.{$info['id']}", $feeBlock, true );
            }
            
            $feeLabels = array();
            foreach ( $feeBlock as $block ) {
                $feeLabels[] = $block['label'] . "  $" . $block['value'];
            }
            $str  = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . implode($feeLabels, '<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
            $retArray[] = $isAppend ? ("{$info['title']}:: " . $str) : $str;
        }
        break;

    default:
        foreach ( $eventInfo as $info ) {
            if ( isset($info[$field]) ) {
                $str = $info[$field];
                $retArray[] = $isAppend ? ("{$info['title']}:: " . $str) : $str;
            }
        }
    }
    return empty($retArray) ? array() : isset($index) ? $retArray[$index] : $retArray;
}

function civitest_civicrm_customFieldOptions( $fieldID, &$options ) {
    if ( $fieldID == 1 ) {
        $options['Rocks'] = t( 'Hooks Rock' );
        unset( $options['Edu'] );
    } else if ( $fieldID == 2 ) {
        $options['H'] = t(' Hook me' );
        unset( $options['S'] );
    }
}

function civitest_civicrm_searchTasks( $objectType, &$tasks ) {
    $tasks[100] = array( 'title'  => t( 'Hook Action Task' ),
                         'class'  => 'CRM_Contact_Form_Task_HookSample',
                         'result' => false );
}

function civitest_civicrm_validate( $formName, &$fields, &$files, &$form ) {
    // sample implementation
    $errors = array( );
    if ( $formName == 'CRM_Contact_Form_Edit' ) {
       // ensure that external identifier is present and valid
       $externalID = CRM_Utils_Array::value( 'external_identifier', $fields );
       if ( ! $externalID ) {
          $errors['external_identifier'] = ts( 'External Identifier is a required field' );
       } else {
	 require_once "CRM/Utils/Rule.php";
	 if ( ! CRM_Utils_Rule::integer( $externalID ) ) {
	   $errors['external_identifier'] = ts( 'External Identifier is not an integer' );
	 }
       }
    }
    return empty( $errors ) ? true : $errors;

}

function civitest_civicrm_pageRun( &$page ) {
    // we are only interested in profile pages with gid = 1 and have a valid contact id
    if ( $page->getVar( '_name' ) != 'CRM_Profile_Page_View' ||
         $page->getVar( '_gid' ) != 1 ||
         ! CRM_Utils_Rule::positiveInteger( $page->getVar( '_id' ) ) ) {
        return;
    }

    // get all relationships of
    require_once 'CRM/Contact/BAO/Relationship.php';
    $relationships = CRM_Contact_BAO_Relationship::getRelationship( $page->getVar( '_id' ) );

    // if you want to filter and display only certain relationship, you can do so before assigninng to
    // smarty. Do a CRM_Core_Error::debug( $relationships ) to see all the fields
    $page->assign( 'relationships', $relationships );

    // in addition to this, you also need to customize: templates/CRM/Profile/Page/View.tpl
    // check: http://wiki.civicrm.org/confluence/display/CRMDOC/Customize+Built-in,+Profile,+Contribution+and+Event+Registration+Screens
    // some sample tpl code is included here, modify as needed
    /**
{if $relationships}
<table>
<tr>
  <th>Name</th>
  <th>Relation</th>
  <th>Country</th>
</tr>
{foreach from=$relationships item=relation}
<tr>
  <td>{$relation.name}</td>
  <td>{$relation.relation}</td>
  <td>{$relation.country}</td>
</tr>
{/foreach}
</table>
{/if}
    **/

}

function civitest_civicrm_eventDiscount( &$form, &$params ) {
	 require_once 'CRM/Utils/Money.php';

    // we only are interested in event id 1
    if ( $form->getVar( '_eventId' ) != 1 ) {
        return;
    }

    $numParticipants = 0;
    foreach ( $params as $key => $value ) {
        if ( isset( $params[$key]['amount'] ) &&
             $params[$key]['amount'] > 0 ) {
            $numParticipants++;
        }
    }

    // Set discount rule (Example: if more than 1 participant, 5% additional discount per paying participant, upto a max of 50%)
    if ( $numParticipants > 1 ) {
        $discountPercentage = $numParticipants * 5;
        
        if ( $discountPercentage > 50 ) {
            $discountPercentage = 50;
        }

        $totalDiscount = 0;
        foreach ( $params as $key => $value ) {
            if ( CRM_Utils_Array::value( 'amount', $params[$key] ) > 0 ) {
                $discount = round( ( $params[$key]['amount'] * $discountPercentage ) / 100.0 );
                $totalDiscount += $discount;
                $params[$key]['discountAmount']  = $discount;
                $discountDisplay = CRM_Utils_Money::format( $discount, null, null );
                // Set discount info added to participant.fee_level and contribution.amount_level
                $params[$key]['discountMessage'] = " (discount: $discountDisplay)";
            }
        }

        $totalDiscountDisplay = CRM_Utils_Money::format( $totalDiscount, null, null );
        // Set the message to show on confirmation page, thank-you page and receipt
        $params[0]['discount'] =
            array( 'message' => "a discount of $totalDiscountDisplay has been applied to the total amount",
                   'applied' => true );
    }
}


/**
 * buildForm hook sample
 *
 * we want the custom date to be today's date. custom_3 is marriage date
 * in the sample data
 */
function civitest_civicrm_buildForm( $formName, &$form ) {

    if ( $formName == 'CRM_Contact_Form_Edit' ) {
        $defaultDate = array( );
        CRM_Utils_Date::getAllDefaultValues( $defaultDate );
        $defaults['custom_3_-1'] = $defaultDate;
        $form->setDefaults( $defaults );
    }

    // enable tracking feature
    if ( ( $formName == 'CRM_Contribute_Form_Contribution_Main' ||
           $formName == 'CRM_Contribute_Form_Contribution_Confirm' ||
           $formName == 'CRM_Contribute_Form_Contribution_ThankYou' ) &&
         $form->getVar( '_id' ) == 1 ) { // use  CONTRIBUTION PAGE ID here
        // use the custom field ID and custom field label here
        $trackingFields = array( 'custom_4' => 'Campaign',
                                 'custom_5' => 'Appeal',
                                 'custom_6' => 'Fund' );
        $form->assign( 'trackingFields', $trackingFields );
    }

}

/**
 * buildForm hook that would allow contacts to renew only existing memberships.
 */
function civitest_civicrm_buildForm( $formName, &$form ) {
    if ( $formName == 'CRM_Contribute_Form_Contribution_Main' ) {
        if ( is_array( $form->_membershipBlock ) ) {
            //get logged in contact
            $session   = & CRM_Core_Session::singleton();
            $contactID = $session->get('userID');
            
            //check for existing membership
            $query = "SELECT membership_type_id 
                      FROM civicrm_membership 
                      WHERE membership_type_id IN ( {$form->_membershipBlock['membership_types']} )
                         AND civicrm_membership.contact_id = {$contactID}";
            $dao = CRM_Core_DAO::executeQuery( $query );

            $membershipTypeID = null;
            while ( $dao->fetch( ) ) {
                $membershipTypeID = $dao->membership_type_id;
            }

            if ( $membershipTypeID ) {
                $form->freeze(array('selectMembership'));    
                $defaults['selectMembership'] = $membershipTypeID;
                $form->setDefaults( $defaults );
            }    
        }
    }
}

function civitest_civicrm_mailingGroups( &$form, &$groups, &$mailings ) {
    unset( $groups[4] );
    $mailings[1] = 'This Mailing does not exist';
}

function _civitest_discountHelper( $eventID, $discountCode ) {
    $sql = "
SELECT v.id as id, v.value as value, v.weight as weight
FROM   civicrm_option_value v,
       civicrm_option_group g
WHERE  v.option_group_id = g.id
AND    v.name = %1
AND    g.name = %2

";
    $params = array( 1 => array( $discountCode              , 'String' ),
                     2 => array( "event_discount_{$eventID}", 'String' ) );
    $dao = CRM_Core_DAO::executeQuery( $sql, $params );
    if ( $dao->fetch( ) ) {
        // ensure discountPercent is a valid numeric number <= 100
        if ( $dao->value &&
             is_numeric( $dao->value ) &&
             $dao->value >= 0 &&
             $dao->value <= 100 &&
             is_numeric( $dao->weight ) ) {
            return array( $dao->id, $dao->value, $dao->weight );
        }
    }
    return array( null, null, null );
                     
}

function civitest_civicrm_buildForm( $formName, &$form ) {
    if ( $formName == 'CRM_Event_Form_Registration_Register' &&
         $form->getVar( '_eventId' ) == 3 ) { 
        $form->addElement( 'text', 'discountCode', ts( 'Discount Code' ) );

        // also assign to template
        $template =& CRM_Core_Smarty::singleton( );
        $beginHookFormElements = $template->get_template_vars( 'beginHookFormElements' );
        if ( ! $beginHookFormElements ) {
            $beginHookFormElements = array( );
        }
        $beginHookFormElements[] = 'discountCode';
        $form->assign( 'beginHookFormElements', $beginHookFormElements );

        $discountCode = CRM_Utils_Request::retrieve( 'discountCode', 'String', $form, false, null, $_REQUEST );
        if ( $discountCode ) {
            $defaults = array( 'discountCode' => $discountCode );
            $form->setDefaults( $defaults );
        }
    }
}

/*
 * Give random discounts for event signup.
 *
 * Warning : while implementing this hook, another post process hook 
 *         : also need implementing to make sure code is only used for 
 *         : the number of times that's allowed to. 
 */
function civitest_civicrm_buildAmount( $pageType,
                                       &$form,
                                       &$amount ) {

    $eventID = $form->getVar( '_eventId' );
    if ( $pageType != 'event' ||
         $eventID != 3 ) {
        return;
    }

    $discountCode = CRM_Utils_Request::retrieve( 'discountCode', 'String', $form, false, null, $_REQUEST );
    if ( ! $discountCode ) {
        return;
    }

    list( $discountID, $discountPercent, $discountNumber ) = _civitest_discountHelper( $eventID, $discountCode );
    if ( $discountNumber <= 0 ) {
        // no more discount left
        return;
    }

    foreach ( $amount as $amountId => $amountInfo ) {
        $amount[$amountId]['value'] = $amount[$amountId]['value'] -
            ceil($amount[$amountId]['value'] * $discountPercent / 100);
        $amount[$amountId]['label'] = $amount[$amountId]['label'] .
            "\t - with {$discountPercent}% discount";
    }
}

/*
 * The hook updates the random code used with event signup.
 */
function civitest_civicrm_postProcess( $class, &$form ) {
    $eventID = $form->getVar( '_eventId' );
    if ( ! is_a($form, 'CRM_Event_Form_Registration_Confirm') ||
         $eventID != 3 ) {
        return;
    }
        
    $discountCode = CRM_Utils_Request::retrieve( 'discountCode', 'String', $form, false, null, $_REQUEST );
    if ( ! $discountCode ) {
        return;
    }

    list( $discountID, $discountPercent, $discountNumber ) = _civitest_discountHelper( $eventID, $discountCode );
    if ( ! $discountID ||
         $discountNumber <= 0 ||
         $discountNumber == 123456789 ) {
        return;
    }

    $query = "
UPDATE civicrm_option_value v
SET    v.weight = v.weight - 1
WHERE  v.id = %1
AND    v.weight > 0
";
    $params = array( 1 => array( $discountID, 'Integer' ) );

    CRM_Core_DAO::executeQuery( $query, $params );
}

/**
 * Sample hook to add more shortcuts
 */
function civitest_civicrm_shortcuts( &$options ) {
    // add link to create new profile
    $options[] = array( 'url'   => '/civicrm/admin/uf/group?action=add&reset=1',
                        'title' => ts('New Profile'),
                        'ref'   => 'new-profile');
    
}


function civitest_civicrm_membershipTypeValues( &$form, &$membershipTypeValues ) {
    $membershipTypeValues[1]['name']        = 'General (50% discount)';
    $membershipTypeValues[1]['minimum_fee'] = '50.00';

    $membershipTypeValues[2]['name']        = 'Student (50% discount)';
    $membershipTypeValues[2]['minimum_fee'] = '25.00';
}

function civitest_civicrm_summary( $contactID, &$content, &$contentPlacement ) {
    // REPLACE default Contact Summary with your customized content
    $contentPlacement = 3;
    $content = "
<table>
<tr><th>Hook Data</th></tr>
<tr><td>Data 1</td></tr>
<tr><td>Data 2</td></tr>
</table>
";

}

function civitest_civicrm_contactListQuery( &$query, $name, $context, $id ) {
    // This example limits contacts in my contact reference field lookup to a specific group

    // Connect the hook to your Contact Reference custom field using the field ID (field id is 4 in this case)
    if ( $context == 'customfield' &&
         $id == 4 ) {
        // Now construct the query to select only the contacts we want
        // The query must return two columns - contact sort_name, and contact id 
        $query = "
SELECT c.sort_name, c.id
FROM civicrm_contact c, civicrm_group_contact cg
WHERE c.sort_name LIKE '$name%'
AND   cg.group_id IN ( 4 )
AND   cg.contact_id = c.id
AND   cg.status = 'Added'
ORDER BY c.sort_name ";            
    }
        
}

/**
 * Hook implementation for altering payment parameters before talking to a payment processor back end.
 *                                        
 * @param string $paymentObj
 *    instance of payment class of the payment processor invoked (e.g., 'CRM_Core_Payment_Dummy')
 * @param array &$rawParams
 *    array of params as passed to to the processor
 * @params array  &$cookedParams
 *     params after the processor code has translated them into its own key/value pairs
 * @return void
 */
function civitest_civicrm_alterPaymentProcessorParams( $paymentObj,
                                                       &$rawParams, 
                                                       &$cookedParams ) {
    $mode = $paymentObj->getVar('_mode');

    if ( $paymentObj instanceof CRM_Core_Payment_Dummy ) {
        $employer   = empty($rawParams['custom_1']) ? '' : $rawParams['custom_1'];
        $occupation = empty($rawParams['custom_2']) ? '' : $rawParams['custom_2'];
        $cookedParams['custom'] = "$employer|$occupation";
        
    } else if ( $paymentObj instanceof CRM_Contribute_Payment_AuthorizeNet ) {
        //Actual translation for one application:
        //Employer > Ship to Country (x_ship_to_country)
        //Occupation > Company (x_company)
        //Solicitor > Ship-to First Name (x_ship_to_first_name)
        //Event > Ship-to Last Name (x_ship_to_last_name)
        //Other > Ship-to Company (x_ship_to_company)
        
        $cookedParams['x_ship_to_country']   = $rawParams['custom_1'];
        $cookedParams['x_company']           = $rawParams['custom_2'];
        $cookedParams['x_ship_to_last_name'] = $rawParams['accountingCode']; //for now
        $country_info = da_core_fetch_country_data_by_crm_id($rawParams['country-1']);    
        $cookedParams['x_ship_to_company']   = $country_info['iso_code'];
    }
}
