Ejemplo n.º 1
0
/**
 * take the input parameter list as specified in the data model and
 * convert it into the same format that we use in QF and BAO object
 *
 * @param array  $params       Associative array of property name/value
 *                             pairs to insert in new contact.
 * @param array  $values       The reformatted properties that we can use internally
 *                            '
 *
 * @return array|CRM_Error
 * @access public
 */
function _civicrm_pledge_format_params(&$params, &$values, $create = FALSE)
{
    // based on contribution apis - copy all the pledge fields - this function filters out non -valid fields but unfortunately
    // means we have to put them back where there are 2 names for the field (name in table & unique name)
    // since there is no clear std to use one or the other. Generally either works ? but not for create date
    // perhaps we should just copy $params across rather than run it through the 'filter'?
    // but at least the filter forces anomalies into the open. In several cases it turned out the unique names wouldn't work
    // even though they are 'generally' what is returned in the GET - implying they should
    $fields = CRM_Pledge_DAO_Pledge::fields();
    _civicrm_store_values($fields, $params, $values);
    //add back the fields we know of that got dropped by the previous function
    if ($params['pledge_create_date']) {
        //pledge_create_date will not be formatted by the format params function so change back to create_date
        $values['create_date'] = $params['pledge_create_date'];
    }
    if ($params['create_date']) {
        //create_date may have been dropped by the $fields function so retrieve it
        $values['create_date'] = $params['create_date'];
    }
    if (array_key_exists('installment_amount', $params)) {
        //field has been renamed - don't lose it! Note that this must be called
        // installment amount not pledge_installment_amount, pledge_original_installment_amount
        // or original_installment_amount to avoid error
        // Division by zero in CRM\Pledge\BAO\PledgePayment.php:162
        // but we should accept the variant because they are all 'logical assumptions' based on the
        // 'standards'
        $values['installment_amount'] = $params['installment_amount'];
    }
    if (array_key_exists('original_installment_amount', $params)) {
        $values['installment_amount'] = $params['original_installment_amount'];
    }
    if (array_key_exists('pledge_original_installment_amount', $params)) {
        $values['installment_amount'] = $params['pledge_original_installment_amount'];
    }
    if (array_key_exists('status_id', $params)) {
        $values['pledge_status_id'] = $params['status_id'];
    }
    if ($params['contact_id']) {
        //this is validity checked further down to make sure the contact exists
        $values['pledge_contact_id'] = $params['contact_id'];
    }
    if (array_key_exists('id', $params)) {
        //retrieve the id key dropped from params. Note we can't use pledge_id because it
        //causes an error in CRM_Pledge_BAO_PledgePayment - approx line 302
        $values['id'] = $params['id'];
    }
    if (array_key_exists('pledge_id', $params)) {
        //retrieve the id key dropped from params. Note we can't use pledge_id because it
        //causes an error in CRM_Pledge_BAO_PledgePayment - approx line 302
        $values['id'] = $params['pledge_id'];
        unset($values['pledge_id']);
    }
    if (array_key_exists('status_id', $params)) {
        $values['pledge_status_id'] = $params['status_id'];
    }
    if (empty($values['id'])) {
        //at this point both should be the same so unset both if not set - passing in empty
        //value causes crash rather creating new - do it before next section as null values ignored in 'switch'
        unset($values['id']);
    }
    if (!empty($params['scheduled_date'])) {
        //scheduled date is required to set next payment date - defaults to start date
        $values['scheduled_date'] = $params['scheduled_date'];
    } elseif (array_key_exists('start_date', $params)) {
        $values['scheduled_date'] = $params['start_date'];
    }
    if (CRM_Utils_Array::value('contribution_type_id', $params)) {
        $values['contribution_type_id'] = $params['contribution_type_id'];
    }
    foreach ($values as $key => $value) {
        // ignore empty values or empty arrays etc
        if (CRM_Utils_System::isNull($value)) {
            continue;
        }
        switch ($key) {
            case 'pledge_contact_id':
                if (!CRM_Utils_Rule::integer($value)) {
                    return civicrm_create_error("contact_id not valid: {$value}");
                }
                $dao = new CRM_Core_DAO();
                $qParams = array();
                $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = {$value}", $qParams);
                if (!$svq) {
                    return civicrm_create_error("Invalid Contact ID: There is no contact record with contact_id = {$value}.");
                }
                $values['contact_id'] = $values['pledge_contact_id'];
                unset($values['pledge_contact_id']);
                break;
            case 'pledge_id':
                if (!CRM_Utils_Rule::integer($value)) {
                    return civicrm_create_error("contact_id not valid: {$value}");
                }
                $dao = new CRM_Core_DAO();
                $qParams = array();
                $svq = $dao->singleValueQuery("SELECT id FROM civicrm_pledge WHERE id = {$value}", $qParams);
                if (!$svq) {
                    return civicrm_create_error("Invalid Contact ID: There is no contact record with contact_id = {$value}.");
                }
                break;
            case 'create_date':
            case 'scheduled_date':
            case 'start_date':
                if (!CRM_Utils_Rule::datetime($value)) {
                    return civicrm_create_error("{$key} not a valid date: {$value}");
                }
                break;
            case 'installment_amount':
            case 'amount':
                if (!CRM_Utils_Rule::money($value)) {
                    return civicrm_create_error("{$key} not a valid amount: {$value}");
                }
                break;
            case 'currency':
                if (!CRM_Utils_Rule::currencyCode($value)) {
                    return civicrm_create_error("currency not a valid code: {$value}");
                }
                break;
            case 'contribution_type_id':
                require_once 'CRM/Contribute/PseudoConstant.php';
                $typeId = CRM_Contribute_PseudoConstant::contributionType($value);
                if (!CRM_Utils_Rule::integer($value) || !$typeId) {
                    return civicrm_create_error("contribution type id is not valid: {$value}");
                }
            default:
                break;
        }
    }
    //format the parameters
    _civicrm_custom_format_params($params, $values, 'Pledge');
    return array();
}