Пример #1
0
/**
 * function convert params to v3.0 format before add location.
 */
function _civicrm_format_params_v2_to_v3(&$params, $locationTypeId = null)
{
    // get the loc type id.
    if (!$locationTypeId) {
        // get location type.
        $locationTypeId = CRM_Utils_Array::value('location_type_id', $params);
        if (!$locationTypeId && array_key_exists('location_type', $params)) {
            require_once 'CRM/Core/PseudoConstant.php';
            $locTypes =& CRM_Core_PseudoConstant::locationType();
            $locType = $params['location_type'];
            if (is_array($params['location_type'])) {
                $locType = array_pop($params['location_type']);
            }
            $locationTypeId = CRM_Utils_Array::key($locType, $locTypes);
        }
    }
    // convert params into v3.0 format.
    $primary = $billing = array();
    $blocks = array('Email', 'Phone', 'IM', 'OpenID');
    // format params array.
    $firstBlockCount = null;
    foreach ($blocks as $block) {
        require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_Core_DAO_" . $block) . ".php";
        eval('$fields =& CRM_Core_DAO_' . $block . '::fields( );');
        $name = strtolower($block);
        $blockCount = 0;
        if (CRM_Utils_Array::value($name, $params)) {
            if (is_array($params[$name])) {
                $values = $params[$name];
                $params[$name] = array();
                foreach ($values as $val) {
                    _civicrm_store_values($fields, $val, $params[$name][++$blockCount]);
                    // check for primary and billing.
                    if (CRM_Utils_Array::value('is_primary', $val)) {
                        $primary[$name][$blockCount] = true;
                    }
                    if (CRM_Utils_Array::value('is_billing', $val)) {
                        $primary[$name][$blockCount] = true;
                    }
                    if (!$firstBlockCount) {
                        $firstBlockCount = $blockCount;
                    }
                }
            } else {
                //need to get ids.
                if (in_array($name, array('im', 'phone'))) {
                    require_once 'CRM/Core/PseudoConstant.php';
                    if ($name == 'im') {
                        CRM_Utils_Array::lookupValue($params, 'provider', CRM_Core_PseudoConstant::IMProvider(), true);
                    } else {
                        CRM_Utils_Array::lookupValue($params, 'phone_type', CRM_Core_PseudoConstant::phoneType(), true);
                    }
                }
                $locValues[$name] = array();
                _civicrm_store_values($fields, $params, $locValues[$name][++$blockCount]);
                $params[$name] = $locValues[$name];
                $firstBlockCount = $blockCount;
                unset($locValues[$name]);
            }
            // make first block as default primary when is_primary
            // is not set in sub array and set in main params array.
            if (!CRM_Utils_Array::value($name, $primary) && CRM_Utils_Array::value('is_primary', $params)) {
                $primary[$name][$firstBlockCount] = true;
                $params[$name][$firstBlockCount]['is_primary'] = true;
            }
            if (!CRM_Utils_Array::value($name, $billing) && CRM_Utils_Array::value('is_billing', $params)) {
                $billing[$name][$firstBlockCount] = true;
                $params[$name][$firstBlockCount]['is_billing'] = true;
            }
        }
    }
    //get the address fields.
    $addressCount = 1;
    $ids = array('county', 'country_id', 'country', 'state_province_id', 'state_province', 'supplemental_address_1', 'supplemental_address_2', 'StateProvince.name', 'city', 'street_address');
    $addressTaken = false;
    foreach ($ids as $id) {
        if (array_key_exists($id, $params)) {
            if (!$addressTaken) {
                require_once 'CRM/Core/DAO/Address.php';
                $fields =& CRM_Core_DAO_Address::fields();
                _civicrm_store_values($fields, $params, $params['address'][$addressCount]);
                $addressTaken = true;
            }
            $params['address'][$addressCount][$id] = $params[$id];
            unset($params[$id]);
        }
    }
    // format state and country.
    foreach (array('state_province', 'country') as $field) {
        $fName = $field == 'state_province' ? 'stateProvinceAbbreviation' : 'countryIsoCode';
        if (CRM_Utils_Array::value('address', $params) && CRM_Utils_Array::value($field, $params['address'][$addressCount]) && is_numeric($params['address'][$addressCount][$field])) {
            $fValue =& $params['address'][$addressCount][$field];
            eval('$fValue = CRM_Core_PseudoConstant::' . $fName . '( $fValue );');
            //kill the reference.
            unset($fValue);
        }
    }
    // check for primary address.
    if (CRM_Utils_Array::value('is_primary', $params)) {
        if ($addressTaken) {
            $primary['address'][$addressCount] = true;
            $params['address'][$addressCount]['is_primary'] = true;
        }
        unset($params['is_primary']);
    }
    if (CRM_Utils_Array::value('is_billing', $params)) {
        if ($addressTaken) {
            $billing['address'][$addressCount] = true;
            $params['address'][$addressCount]['is_billing'] = true;
        }
        unset($params['is_billing']);
    }
    // handle primary and billing reset.
    foreach (array('email', 'phone', 'im', 'address', 'openid') as $name) {
        if (!array_key_exists($name, $params) || CRM_Utils_System::isNull($params[$name])) {
            continue;
        }
        $errorMsg = null;
        $primaryBlockIndex = $billingBlockIndex = 0;
        if (array_key_exists($name, $primary)) {
            if (count($primary[$name]) > 1) {
                $errorMsg .= ts("<br />Multiple Primary %1.", array(1 => $block));
            } else {
                $primaryBlockIndex = key($primary[$name]);
            }
        }
        if (array_key_exists($name, $billing)) {
            if (count($billing[$name]) > 1) {
                $errorMsg .= ts("<br />Multiple Billing %1.", array(1 => $block));
            } else {
                $billingBlockIndex = key($billing[$name]);
            }
        }
        if ($errorMsg) {
            return civicrm_create_error($errorMsg);
        }
        foreach ($params[$name] as $count => &$values) {
            if ($primaryBlockIndex && $count != $primaryBlockIndex) {
                $values['is_primary'] = false;
            }
            if ($billingBlockIndex && $count != $billingBlockIndex) {
                $values['is_billing'] = false;
            }
            // get location type if not present in sub array.
            if (!CRM_Utils_Array::value('location_type_id', $values)) {
                $values['location_type_id'] = $locationTypeId;
            }
            //kill the reference.
            unset($values);
        }
    }
    // finally unset location_type and location type id.
    foreach (array('location_type', 'location_type_id') as $f) {
        if (isset($params[$f])) {
            unset($params[$f]);
        }
    }
    return $params;
}
Пример #2
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();
}
Пример #3
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
 *
 * @param array  $create       Is the formatted Values array going to
 *                             be used for CRM_Member_BAO_Membership:create()
 *
 * @return array|error
 * @access public
 */
function _civicrm_membership_format_params(&$params, &$values, $create = false)
{
    require_once "CRM/Member/DAO/Membership.php";
    $fields =& CRM_Member_DAO_Membership::fields();
    _civicrm_store_values($fields, $params, $values);
    foreach ($params as $key => $value) {
        // ignore empty values or empty arrays etc
        if (CRM_Utils_System::isNull($value)) {
            continue;
        }
        switch ($key) {
            case 'membership_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['membership_contact_id'];
                unset($values['membership_contact_id']);
                break;
            case 'join_date':
            case 'membership_start_date':
            case 'membership_end_date':
                if (!CRM_Utils_Rule::date($value)) {
                    return civicrm_create_error("{$key} not a valid date: {$value}");
                }
                break;
            case 'membership_type_id':
                $id = CRM_Core_DAO::getFieldValue("CRM_Member_DAO_MembershipType", $value, 'id', 'name');
                $values[$key] = $id;
                break;
            case 'status_id':
                $id = CRM_Core_DAO::getFieldValue("CRM_Member_DAO_MembershipStatus", $value, 'id', 'name');
                $values[$key] = $id;
                break;
            default:
                break;
        }
    }
    _civicrm_custom_format_params($params, $values, 'Membership');
    if ($create) {
        // CRM_Member_BAO_Membership::create() handles membership_start_date,
        // membership_end_date and membership_source. So, if $values contains
        // membership_start_date, membership_end_date  or membership_source,
        // convert it to start_date, end_date or source
        $changes = array('membership_start_date' => 'start_date', 'membership_end_date' => 'end_date', 'membership_source' => 'source');
        foreach ($changes as $orgVal => $changeVal) {
            if (isset($values[$orgVal])) {
                $values[$changeVal] = $values[$orgVal];
                unset($values[$orgVal]);
            }
        }
    }
    return null;
}
Пример #4
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
 *
 * @param array  $create       Is the formatted Values array going to
 *                             be used for CRM_Activity_BAO_Activity::create()
 *
 * @return array|CRM_Error
 * @access public
 */
function _civicrm_activity_formatted_param(&$params, &$values, $create = false)
{
    $fields =& CRM_Activity_DAO_Activity::fields();
    _civicrm_store_values($fields, $params, $values);
    require_once 'CRM/Core/OptionGroup.php';
    $customFields = CRM_Core_BAO_CustomField::getFields('Activity');
    foreach ($params as $key => $value) {
        // ignore empty values or empty arrays etc
        if (CRM_Utils_System::isNull($value)) {
            continue;
        }
        //Handling Custom Data
        if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
            $values[$key] = $value;
            $type = $customFields[$customFieldID]['html_type'];
            if ($type == 'CheckBox' || $type == 'Multi-Select') {
                $mulValues = explode(',', $value);
                $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true);
                $values[$key] = array();
                foreach ($mulValues as $v1) {
                    foreach ($customOption as $customValueID => $customLabel) {
                        $customValue = $customLabel['value'];
                        if (strtolower($customLabel['label']) == strtolower(trim($v1)) || strtolower($customValue) == strtolower(trim($v1))) {
                            if ($type == 'CheckBox') {
                                $values[$key][$customValue] = 1;
                            } else {
                                $values[$key][] = $customValue;
                            }
                        }
                    }
                }
            } else {
                if ($type == 'Select' || $type == 'Radio') {
                    $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true);
                    foreach ($customOption as $customValue => $customLabel) {
                        if (strtolower($customLabel) == strtolower(trim($v1)) || strtolower($customValue) == strtolower(trim($v1))) {
                            $values[$key] = $customValue;
                        }
                    }
                }
            }
        }
    }
    return null;
}
/**
 * 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_relationship_format_params(&$params, &$values)
{
    // copy all the relationship fields as is
    $fields = CRM_Contact_DAO_Relationship::fields();
    _civicrm_store_values($fields, $params, $values);
    $relationTypes = CRM_Core_PseudoConstant::relationshipType('name', TRUE);
    foreach ($params as $key => $value) {
        // ignore empty values or empty arrays etc
        require_once 'CRM/Utils/System.php';
        if (CRM_Utils_System::isNull($value)) {
            continue;
        }
        switch ($key) {
            case 'contact_id_a':
            case 'contact_id_b':
                require_once 'CRM/Utils/Rule.php';
                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}.");
                }
                break;
            case 'start_date':
            case 'end_date':
                if (!CRM_Utils_Rule::qfDate($value)) {
                    return civicrm_create_error("{$key} not a valid date: {$value}");
                }
                break;
            case 'relationship_type':
                foreach ($relationTypes as $relTypId => $relValue) {
                    if (CRM_Utils_Array::key(ucfirst($value), $relValue)) {
                        $relationshipTypeId = $relTypId;
                        break;
                    }
                }
                if ($relationshipTypeId) {
                    if (CRM_Utils_Array::value('relationship_type_id', $values) && $relationshipTypeId != $values['relationship_type_id']) {
                        return civicrm_create_error('Mismatched Relationship Type and Relationship Type Id');
                    }
                    $values['relationship_type_id'] = $params['relationship_type_id'] = $relationshipTypeId;
                } else {
                    return civicrm_create_error('Invalid Relationship Type');
                }
            case 'relationship_type_id':
                if ($key == 'relationship_type_id' && !array_key_exists($value, $relationTypes)) {
                    return civicrm_create_error("{$key} not a valid: {$value}");
                }
                // execute for both relationship_type and relationship_type_id
                $relation = $relationTypes[$params['relationship_type_id']];
                require_once 'CRM/Contact/BAO/Contact.php';
                if ($relation['contact_type_a'] && $relation['contact_type_a'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_a'])) {
                    return civicrm_create_error("Contact ID :{$params['contact_id_a']} is not of contact type {$relation['contact_type_a']}");
                }
                if ($relation['contact_type_b'] && $relation['contact_type_b'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_b'])) {
                    return civicrm_create_error("Contact ID :{$params['contact_id_b']} is not of contact type {$relation['contact_type_b']}");
                }
                break;
            default:
                break;
        }
    }
    if (array_key_exists('note', $params)) {
        $values['note'] = $params['note'];
    }
    _civicrm_custom_format_params($params, $values, 'Relationship');
    return array();
}
/**
 * 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_pledgepayment_format_params(&$params, &$values, $create = FALSE)
{
    // copy all the pledge fields as is
    require_once 'CRM/Pledge/BAO/PledgePayment.php';
    require_once 'CRM/Pledge/DAO/Pledge.php';
    $fields = CRM_Pledge_DAO_Pledge::fields();
    _civicrm_store_values($fields, $params, $values);
    foreach ($params 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 'receive_date':
            case 'end_date':
            case 'pledge_create_date':
            case 'cancel_date':
            case 'receipt_date':
            case 'thankyou_date':
                if (!CRM_Utils_Rule::date($value)) {
                    return civicrm_create_error("{$key} not a valid date: {$value}");
                }
                break;
            case 'non_deductible_amount':
            case 'total_amount':
            case 'fee_amount':
            case 'net_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 'pledge_type':
                $values['pledge_type_id'] = CRM_Utils_Array::key(ucfirst($value), CRM_Pledge_PseudoConstant::pledgeType());
                break;
            case 'payment_instrument':
                require_once 'CRM/Core/OptionGroup.php';
                $values['payment_instrument_id'] = CRM_Core_OptionGroup::getValue('payment_instrument', $value);
                break;
            default:
                break;
        }
    }
    if (array_key_exists('note', $params)) {
        $values['note'] = $params['note'];
    }
    if (array_key_exists('installment_amount', $params)) {
        $values['installment_amount'] = $params['installment_amount'];
    }
    // testing testing - how do I make it take a create_date? It needs $values['create_date'] set but doesn't seem to like it because $fields calls it $pledge_create_date
    //ditto scheduled date. I don't know why this is needs to be done because I don't fully understand the code above
    if (array_key_exists('pledge_create_date', $params)) {
        $values['create_date'] = $params['pledge_create_date'];
    }
    if (array_key_exists('pledge_scheduled_date', $params)) {
        $values['scheduled_date'] = $params['pledge_scheduled_date'];
    }
    if (array_key_exists('pledge_create_date', $params)) {
        $values['create_date'] = $params['pledge_create_date'];
    }
    if (array_key_exists('status_id', $params)) {
        $values['status_id'] = $params['status_id'];
        $values['pledge_status_id'] = $params['status_id'];
    }
    _civicrm_custom_format_params($params, $values, 'Pledge');
    if ($create) {
        // CRM_pledge_BAO_Pledge::add() handles Pledge_source
        // So, if $values contains Pledge_source, convert it to source
        $changes = array('pledge_source' => 'source');
        foreach ($changes as $orgVal => $changeVal) {
            if (isset($values[$orgVal])) {
                $values[$changeVal] = $values[$orgVal];
                unset($values[$orgVal]);
            }
        }
    }
    return array();
}
Пример #7
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
 *
 * @param array  $create       Is the formatted Values array going to
 *                             be used for CRM_Activity_BAO_Activity::create()
 *
 * @return array|CRM_Error
 * @access public
 */
function _civicrm_activity_formatted_param(&$params, &$values, $create = false)
{
    $fields =& CRM_Activity_DAO_Activity::fields();
    _civicrm_store_values($fields, $params, $values);
    require_once 'CRM/Core/OptionGroup.php';
    $customFields = CRM_Core_BAO_CustomField::getFields('Activity');
    foreach ($params as $key => $value) {
        // ignore empty values or empty arrays etc
        if (CRM_Utils_System::isNull($value)) {
            continue;
        }
        //Handling Custom Data
        if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
            $values[$key] = $value;
            $type = $customFields[$customFieldID]['html_type'];
            if ($type == 'CheckBox' || $type == 'Multi-Select') {
                $mulValues = explode(',', $value);
                $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true);
                $values[$key] = array();
                foreach ($mulValues as $v1) {
                    foreach ($customOption as $customValueID => $customLabel) {
                        $customValue = $customLabel['value'];
                        if (strtolower($customLabel['label']) == strtolower(trim($v1)) || strtolower($customValue) == strtolower(trim($v1))) {
                            if ($type == 'CheckBox') {
                                $values[$key][$customValue] = 1;
                            } else {
                                $values[$key][] = $customValue;
                            }
                        }
                    }
                }
            } else {
                if ($type == 'Select' || $type == 'Radio') {
                    $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true);
                    foreach ($customOption as $customValue => $customLabel) {
                        if (strtolower($customLabel) == strtolower(trim($v1)) || strtolower($customValue) == strtolower(trim($v1))) {
                            $values[$key] = $customValue;
                        }
                    }
                }
            }
        } else {
            if ($key == 'target_contact_id') {
                if (!CRM_Utils_Rule::integer($value)) {
                    return civicrm_create_error("contact_id not valid: {$value}");
                }
                $contactID = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = {$value}");
                if (!$contactID) {
                    return civicrm_create_error("Invalid Contact ID: There is no contact record with contact_id = {$value}.");
                }
            }
        }
    }
    return null;
}
Пример #8
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_contribute_format_params(&$params, &$values, $create = false)
{
    // copy all the contribution fields as is
    $fields =& CRM_Contribute_DAO_Contribution::fields();
    _civicrm_store_values($fields, $params, $values);
    foreach ($params as $key => $value) {
        // ignore empty values or empty arrays etc
        if (CRM_Utils_System::isNull($value)) {
            continue;
        }
        switch ($key) {
            case 'contribution_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['contribution_contact_id'];
                unset($values['contribution_contact_id']);
                break;
            case 'receive_date':
            case 'cancel_date':
            case 'receipt_date':
            case 'thankyou_date':
                if (!CRM_Utils_Rule::date($value)) {
                    return civicrm_create_error("{$key} not a valid date: {$value}");
                }
                break;
            case 'non_deductible_amount':
            case 'total_amount':
            case 'fee_amount':
            case 'net_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':
                $values['contribution_type_id'] = CRM_Utils_Array::key(ucfirst($value), CRM_Contribute_PseudoConstant::contributionType());
                break;
            case 'payment_instrument':
                require_once 'CRM/Core/OptionGroup.php';
                $values['payment_instrument_id'] = CRM_Core_OptionGroup::getValue('payment_instrument', $value);
                break;
            default:
                break;
        }
    }
    if (array_key_exists('note', $params)) {
        $values['note'] = $params['note'];
    }
    _civicrm_custom_format_params($params, $values, 'Contribution');
    if ($create) {
        // CRM_Contribute_BAO_Contribution::add() handles contribution_source
        // So, if $values contains contribution_source, convert it to source
        $changes = array('contribution_source' => 'source');
        foreach ($changes as $orgVal => $changeVal) {
            if (isset($values[$orgVal])) {
                $values[$changeVal] = $values[$orgVal];
                unset($values[$orgVal]);
            }
        }
    }
    return array();
}
/**
 * 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
 *
 * @param array  $create       Is the formatted Values array going to
 *                             be used for CRM_Member_BAO_Membership:create()
 *
 * @return array|error
 * @access public
 */
function _civicrm_membership_format_params(&$params, &$values, $create = FALSE)
{
    require_once "CRM/Member/DAO/Membership.php";
    require_once "CRM/Member/PseudoConstant.php";
    $fields = CRM_Member_DAO_Membership::fields();
    _civicrm_store_values($fields, $params, $values);
    foreach ($params as $key => $value) {
        // ignore empty values or empty arrays etc
        if (CRM_Utils_System::isNull($value)) {
            continue;
        }
        switch ($key) {
            case 'membership_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['membership_contact_id'];
                unset($values['membership_contact_id']);
                break;
            case 'join_date':
            case 'start_date':
            case 'end_date':
            case 'reminder_date':
            case 'membership_start_date':
            case 'membership_end_date':
                if (!CRM_Utils_Rule::date($value)) {
                    return civicrm_create_error("{$key} not a valid date: {$value}");
                }
                // make sure we format dates to mysql friendly format
                $values[$key] = CRM_Utils_Date::processDate($value, NULL, FALSE, 'Ymd');
                break;
            case 'membership_type_id':
                if (!CRM_Utils_Array::value($value, CRM_Member_PseudoConstant::membershipType())) {
                    return civicrm_create_error('Invalid Membership Type Id');
                }
                $values[$key] = $value;
                break;
            case 'membership_type':
                $membershipTypeId = CRM_Utils_Array::key(ucfirst($value), CRM_Member_PseudoConstant::membershipType());
                if ($membershipTypeId) {
                    if (CRM_Utils_Array::value('membership_type_id', $values) && $membershipTypeId != $values['membership_type_id']) {
                        return civicrm_create_error('Mismatched membership Type and Membership Type Id');
                    }
                } else {
                    return civicrm_create_error('Invalid Membership Type');
                }
                $values['membership_type_id'] = $membershipTypeId;
                break;
            case 'status_id':
                if (!CRM_Utils_Array::value($value, CRM_Member_PseudoConstant::membershipStatus())) {
                    return civicrm_create_error('Invalid Membership Status Id');
                }
                $values[$key] = $value;
                break;
            default:
                break;
        }
    }
    _civicrm_custom_format_params($params, $values, 'Membership');
    if ($create) {
        // CRM_Member_BAO_Membership::create() handles membership_start_date,
        // membership_end_date and membership_source. So, if $values contains
        // membership_start_date, membership_end_date  or membership_source,
        // convert it to start_date, end_date or source
        $changes = array('membership_start_date' => 'start_date', 'membership_end_date' => 'end_date', 'membership_source' => 'source');
        foreach ($changes as $orgVal => $changeVal) {
            if (isset($values[$orgVal])) {
                $values[$changeVal] = $values[$orgVal];
                unset($values[$orgVal]);
            }
        }
    }
    return NULL;
}
/**
 * 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
 *
 * @param array  $create       Is the formatted Values array going to
 *                             be used for CRM_Activity_BAO_Activity::create()
 *
 * @return array|CRM_Error
 * @access public
 */
function _civicrm_activity_formatted_param(&$params, &$values, $create = FALSE)
{
    $fields = CRM_Activity_DAO_Activity::fields();
    _civicrm_store_values($fields, $params, $values);
    require_once 'CRM/Core/OptionGroup.php';
    $customFields = CRM_Core_BAO_CustomField::getFields('Activity');
    foreach ($params as $key => $value) {
        // ignore empty values or empty arrays etc
        if (CRM_Utils_System::isNull($value)) {
            continue;
        }
        //Handling Custom Data
        _civicrm_generic_handle_custom_data($key, $value, $values, $customFields);
        if ($key == 'target_contact_id') {
            if (!CRM_Utils_Rule::integer($value)) {
                return civicrm_create_error("contact_id not valid: {$value}");
            }
            $contactID = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = {$value}");
            if (!$contactID) {
                return civicrm_create_error("Invalid Contact ID: There is no contact record with contact_id = {$value}.");
            }
        }
    }
    return NULL;
}