/**
  * @deprecated - this function formats params according to v2 standards but
  * need to be sure about the impact of not calling it so retaining on the import class
  * 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 membership_format_params($params, &$values, $create = FALSE)
 {
     require_once 'api/v3/utils.php';
     $fields = CRM_Member_DAO_Membership::fields();
     _civicrm_api3_store_values($fields, $params, $values);
     $customFields = CRM_Core_BAO_CustomField::getFields('Membership');
     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' || $type == 'AdvMulti-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;
                             }
                         }
                     }
                 }
             }
         }
         switch ($key) {
             case 'membership_contact_id':
                 if (!CRM_Utils_Rule::integer($value)) {
                     throw new Exception("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) {
                     throw new Exception("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 'membership_type_id':
                 if (!CRM_Utils_Array::value($value, CRM_Member_PseudoConstant::membershipType())) {
                     throw new Exception('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']) {
                         throw new Exception('Mismatched membership Type and Membership Type Id');
                     }
                 } else {
                     throw new Exception('Invalid Membership Type');
                 }
                 $values['membership_type_id'] = $membershipTypeId;
                 break;
             case 'status_id':
                 if (!CRM_Utils_Array::value($value, CRM_Member_PseudoConstant::membershipStatus())) {
                     throw new Exception('Invalid Membership Status Id');
                 }
                 $values[$key] = $value;
                 break;
             case 'membership_status':
                 $membershipStatusId = CRM_Utils_Array::key(ucfirst($value), CRM_Member_PseudoConstant::membershipStatus());
                 if ($membershipStatusId) {
                     if (CRM_Utils_Array::value('status_id', $values) && $membershipStatusId != $values['status_id']) {
                         throw new Exception('Mismatched membership Status and Membership Status Id');
                     }
                 } else {
                     throw new Exception('Invalid Membership Status');
                 }
                 $values['status_id'] = $membershipStatusId;
                 break;
             default:
                 break;
         }
     }
     _civicrm_api3_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;
 }
/**
 * This function format location blocks w/ v3.0 format.
 *
 * @param array $values
 *   The variable(s) to be added.
 * @param array $params
 *   The structured parameter list.
 *
 * @return bool
 */
function _civicrm_api3_deprecated_add_formatted_location_blocks(&$values, &$params)
{
    static $fields = NULL;
    if ($fields == NULL) {
        $fields = array();
    }
    foreach (array('Phone', 'Email', 'IM', 'OpenID', 'Phone_Ext') as $block) {
        $name = strtolower($block);
        if (!array_key_exists($name, $values)) {
            continue;
        }
        if ($name == 'phone_ext') {
            $block = 'Phone';
        }
        // block present in value array.
        if (!array_key_exists($name, $params) || !is_array($params[$name])) {
            $params[$name] = array();
        }
        if (!array_key_exists($block, $fields)) {
            $className = "CRM_Core_DAO_{$block}";
            $fields[$block] =& $className::fields();
        }
        $blockCnt = count($params[$name]);
        // copy value to dao field name.
        if ($name == 'im') {
            $values['name'] = $values[$name];
        }
        _civicrm_api3_store_values($fields[$block], $values, $params[$name][++$blockCnt]);
        if (empty($params['id']) && $blockCnt == 1) {
            $params[$name][$blockCnt]['is_primary'] = TRUE;
        }
        // we only process single block at a time.
        return TRUE;
    }
    // handle address fields.
    if (!array_key_exists('address', $params) || !is_array($params['address'])) {
        $params['address'] = array();
    }
    $addressCnt = 1;
    foreach ($params['address'] as $cnt => $addressBlock) {
        if (CRM_Utils_Array::value('location_type_id', $values) == CRM_Utils_Array::value('location_type_id', $addressBlock)) {
            $addressCnt = $cnt;
            break;
        }
        $addressCnt++;
    }
    if (!array_key_exists('Address', $fields)) {
        require_once 'CRM/Core/DAO/Address.php';
        $fields['Address'] = CRM_Core_DAO_Address::fields();
    }
    // Note: we doing multiple value formatting here for address custom fields, plus putting into right format.
    // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving
    // the address in CRM_Core_BAO_Address::create method
    if (!empty($values['location_type_id'])) {
        static $customFields = array();
        if (empty($customFields)) {
            $customFields = CRM_Core_BAO_CustomField::getFields('Address');
        }
        // make a copy of values, as we going to make changes
        $newValues = $values;
        foreach ($values as $key => $val) {
            $customFieldID = CRM_Core_BAO_CustomField::getKeyID($key);
            if ($customFieldID && array_key_exists($customFieldID, $customFields)) {
                // mark an entry in fields array since we want the value of custom field to be copied
                $fields['Address'][$key] = NULL;
                $htmlType = CRM_Utils_Array::value('html_type', $customFields[$customFieldID]);
                switch ($htmlType) {
                    case 'CheckBox':
                    case 'AdvMulti-Select':
                    case 'Multi-Select':
                        if ($val) {
                            $mulValues = explode(',', $val);
                            $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
                            $newValues[$key] = array();
                            foreach ($mulValues as $v1) {
                                foreach ($customOption as $v2) {
                                    if (strtolower($v2['label']) == strtolower(trim($v1)) || strtolower($v2['value']) == strtolower(trim($v1))) {
                                        if ($htmlType == 'CheckBox') {
                                            $newValues[$key][$v2['value']] = 1;
                                        } else {
                                            $newValues[$key][] = $v2['value'];
                                        }
                                    }
                                }
                            }
                        }
                        break;
                }
            }
        }
        // consider new values
        $values = $newValues;
    }
    _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$addressCnt]);
    $addressFields = array('county', 'country', 'state_province', 'supplemental_address_1', 'supplemental_address_2', 'StateProvince.name');
    foreach ($addressFields as $field) {
        if (array_key_exists($field, $values)) {
            if (!array_key_exists('address', $params)) {
                $params['address'] = array();
            }
            $params['address'][$addressCnt][$field] = $values[$field];
        }
    }
    if ($addressCnt == 1) {
        $params['address'][$addressCnt]['is_primary'] = TRUE;
    }
    return TRUE;
}
Example #3
0
/**
 * This is a legacy wrapper for api_store_values.
 *
 * It checks suitable fields using getfields rather than DAO->fields.
 *
 * Getfields has handling for how to deal with unique names which dao->fields doesn't
 *
 * Note this is used by BAO type create functions - eg. contribution
 *
 * @param string $entity
 * @param array $params
 * @param array $values
 */
function _civicrm_api3_filter_fields_for_bao($entity, &$params, &$values)
{
    $fields = civicrm_api($entity, 'getfields', array('version' => 3, 'action' => 'create'));
    $fields = $fields['values'];
    _civicrm_api3_store_values($fields, $params, $values);
}
/**
 * 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
 *                            '
 *
 * @throws Exception
 * @return array|CRM_Error
 * @access public
 */
function _civicrm_api3_relationship_format_params($params, &$values)
{
    // copy all the relationship fields as is
    $fields = CRM_Contact_DAO_Relationship::fields();
    _civicrm_api3_store_values($fields, $params, $values);
    $relationTypes = CRM_Core_PseudoConstant::relationshipType('name');
    if (!empty($params['id'])) {
        $relation = new CRM_Contact_BAO_Relationship();
        $relation->id = $params['id'];
        if (!$relation->find(TRUE)) {
            throw new Exception('Relationship id is not valid');
        } else {
            if (isset($params['contact_id_a']) && $params['contact_id_a'] != $relation->contact_id_a || isset($params['contact_id_b']) && $params['contact_id_b'] != $relation->contact_id_b) {
                throw new Exception('Cannot change the contacts once relationship has been created');
            } else {
                // since the BAO function is not std & won't accept just 'id' (aargh) let's
                // at least return our BAO here
                $values = array();
                _civicrm_api3_object_to_array($relation, $values);
                $values = array_merge($values, $params);
                // and we need to reformat our date fields....
                $dateFields = array('start_date', 'end_date');
                foreach ($dateFields as $dateField) {
                    $values[$dateField] = CRM_Utils_Date::processDate($values[$dateField]);
                }
            }
        }
    }
    foreach ($params as $key => $value) {
        // ignore empty values or empty arrays etc
        if (CRM_Utils_System::isNull($value)) {
            continue;
        }
        switch ($key) {
            case 'contact_id_a':
            case 'contact_id_b':
                if (!CRM_Utils_Rule::integer($value)) {
                    throw new Exception("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) {
                    throw new Exception("Invalid Contact ID: There is no contact record with contact_id = {$value}.");
                }
                break;
            case 'relationship_type':
                foreach ($relationTypes as $relTypId => $relValue) {
                    if (CRM_Utils_Array::key(ucfirst($value), $relValue)) {
                        $relationshipTypeId = $relTypId;
                        break;
                    }
                }
                if ($relationshipTypeId) {
                    if (!empty($values['relationship_type_id']) && $relationshipTypeId != $values['relationship_type_id']) {
                        throw new Exception('Mismatched Relationship Type and Relationship Type Id');
                    }
                    $values['relationship_type_id'] = $params['relationship_type_id'] = $relationshipTypeId;
                } else {
                    throw new Exception('Invalid Relationship Type');
                }
            case 'relationship_type_id':
                if ($key == 'relationship_type_id' && !array_key_exists($value, $relationTypes)) {
                    throw new Exception("{$key} not a valid: {$value}");
                }
                // execute for both relationship_type and relationship_type_id
                $relation = $relationTypes[$params['relationship_type_id']];
                if (!empty($params['contact_id_a']) && $relation['contact_type_a'] && $relation['contact_type_a'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_a'])) {
                    throw new Exception("Contact ID :{$params['contact_id_a']} is not of contact type {$relation['contact_type_a']}");
                }
                if (!empty($params['contact_id_b']) && $relation['contact_type_b'] && $relation['contact_type_b'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_b'])) {
                    throw new Exception("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_api3_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_api3_relationship_format_params($params, &$values)
{
    // copy all the relationship fields as is
    $fields = CRM_Contact_DAO_Relationship::fields();
    _civicrm_api3_store_values($fields, $params, $values);
    $relationTypes = CRM_Core_PseudoConstant::relationshipType('name');
    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)) {
                    throw new Exception("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) {
                    throw new Exception("Invalid Contact ID: There is no contact record with contact_id = {$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']) {
                        throw new Exception('Mismatched Relationship Type and Relationship Type Id');
                    }
                    $values['relationship_type_id'] = $params['relationship_type_id'] = $relationshipTypeId;
                } else {
                    throw new Exception('Invalid Relationship Type');
                }
            case 'relationship_type_id':
                if ($key == 'relationship_type_id' && !array_key_exists($value, $relationTypes)) {
                    throw new Exception("{$key} not a valid: {$value}");
                }
                // execute for both relationship_type and relationship_type_id
                $relation = $relationTypes[$params['relationship_type_id']];
                if ($relation['contact_type_a'] && $relation['contact_type_a'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_a'])) {
                    throw new Exception("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'])) {
                    throw new Exception("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_api3_custom_format_params($params, $values, 'Relationship');
    return array();
}
/**
 * @deprecated
 * Deprecated function to support membership create. Do not call this. It will be removed in favour of
 * wrapper layer formatting
 * 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_api3_membership_format_params($params, &$values, $create = FALSE)
{
    $fields = CRM_Member_DAO_Membership::fields();
    _civicrm_api3_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_type':
                // @todo we still need to adequately figure out how to handle this @ the API layer.
                // it is an FK & a pseudoconstant - we should probably alias it onto membership_type_id &
                // then in the validate_integer function do an if(!is_integer && $fieldInfo['pseudoconstant) look
                // up pseudoconstant & flip it over. By the time it hits api it will be a valid membership_type & handling @
                // api layer not required
                $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_api3_create_error('Mismatched membership Type and Membership Type Id');
                    }
                } else {
                    return civicrm_api3_create_error('Invalid Membership Type');
                }
                $values['membership_type_id'] = $membershipTypeId;
                break;
            default:
                break;
        }
    }
    return NULL;
}