Beispiel #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 _crm_format_params(&$params, &$values)
{
    // copy all the contact and contact_type fields as is
    $fields =& CRM_Contact_DAO_Contact::fields();
    _crm_store_values($fields, $params, $values);
    require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_Contact_DAO_" . $values['contact_type']) . ".php";
    eval('$fields =& CRM_Contact_DAO_' . $values['contact_type'] . '::fields( );');
    _crm_store_values($fields, $params, $values);
    $ids = array("prefix", "suffix", "gender");
    foreach ($ids as $id) {
        if (array_key_exists($id, $params)) {
            $values[$id] = $params[$id];
        }
    }
    $locationTypeNeeded = false;
    $values['location'] = array();
    $values['location'][1] = array();
    $fields =& CRM_Core_DAO_Location::fields();
    if (_crm_store_values($fields, $params, $values['location'][1])) {
        $locationTypeNeeded = true;
    }
    if (array_key_exists('location_type', $params)) {
        $locationTypes = CRM_Core_PseudoConstant::locationType();
        $locationType = $locationTypeId = '';
        //fix for CRM-707
        if (!is_numeric($params['location_type'])) {
            $locationTypeName = $params['location_type'];
            $locationTypeId = CRM_Utils_Array::value($params['location_type'], $locationTypes);
        } else {
            $locationTypeName = CRM_Utils_Array::value($params['location_type'], $locationTypes);
            $locationTypeId = $params['location_type'];
        }
        $values['location'][1]['location_type'] = $locationTypeName;
        $values['location'][1]['location_type_id'] = $locationTypeId;
    }
    $values['location'][1]['address'] = array();
    $fields =& CRM_Core_DAO_Address::fields();
    // ignore the note field in address for now
    unset($fields['note']);
    if (_crm_store_values($fields, $params, $values['location'][1]['address'])) {
        $locationTypeNeeded = true;
    }
    $ids = array('county', 'country', 'state_province', 'supplemental_address_1', 'supplemental_address_2', 'StateProvince.name');
    foreach ($ids as $id) {
        if (array_key_exists($id, $params)) {
            $values['location'][1]['address'][$id] = $params[$id];
            $locationTypeNeeded = true;
        }
    }
    $blocks = array('Email', 'Phone', 'IM');
    foreach ($blocks as $block) {
        $name = strtolower($block);
        $values['location'][1][$name] = array();
        $values['location'][1][$name][1] = array();
        require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_Core_DAO_" . $block) . ".php";
        eval('$fields =& CRM_Core_DAO_' . $block . '::fields( );');
        if (_crm_store_values($fields, $params, $values['location'][1][$name][1])) {
            $locationTypeNeeded = true;
            $values['location'][1][$name][1]['is_primary'] = 1;
        }
    }
    if (!array_key_exists('first_name', $params) || !array_key_exists('last_name', $params)) {
        // make sure phone and email are valid strings
        if (array_key_exists('email', $params) && !CRM_Utils_Rule::email($params['email'])) {
            return _crm_error("Email not valid " . $params['email']);
        }
    }
    if (array_key_exists('im', $params)) {
        $values['location'][1]['im'][1]['name'] = $params['im'];
        $locationTypeNeeded = true;
    }
    if (array_key_exists('im_provider', $params)) {
        $values['location'][1]['im'][1]['provider'] = $params['im_provider'];
        $locationTypeNeeded = true;
    }
    if ($locationTypeNeeded) {
        if (!array_key_exists('location_type_id', $values['location'][1])) {
            require_once 'CRM/Core/BAO/LocationType.php';
            $locationType =& CRM_Core_BAO_LocationType::getDefault();
            $values['location'][1]['location_type_id'] = $locationType->id;
            $values['location'][1]['location_type'] = $locationType->name;
        }
        $values['location'][1]['is_primary'] = true;
    } else {
        unset($values['location']);
    }
    if (array_key_exists('note', $params)) {
        $values['note'] = $params['note'];
    }
    $values['custom'] = array();
    $customFields = CRM_Core_BAO_CustomField::getFields($values['contact_type']);
    foreach ($params as $key => $value) {
        if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
            /* check if it's a valid custom field id */
            if (!array_key_exists($customFieldID, $customFields)) {
                return _crm_error('Invalid custom field ID');
            }
            /* validate the data against the CF type */
            //CRM_Core_Error::debug( $value, $customFields[$customFieldID] );
            $valid = CRM_Core_BAO_CustomValue::typecheck($customFields[$customFieldID][2], $value);
            if (!$valid) {
                return _crm_error('Invalid value for custom field ' . $customFields[$customFieldID][0]);
            }
            // fix the date field if so
            if ($customFields[$customFieldID][2] == 'Date') {
                $value = str_replace('-', '', $value);
            }
            $newMulValues = array();
            if ($customFields[$customFieldID][3] == 'CheckBox' || $customFields[$customFieldID][3] == 'Multi-Select') {
                $value = str_replace("|", ",", $value);
                $mulValues = explode(',', $value);
                $custuomOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true);
                foreach ($mulValues as $v1) {
                    foreach ($custuomOption as $v2) {
                        if (strtolower($v2['label']) == strtolower(trim($v1))) {
                            $newMulValues[] = $v2['value'];
                        }
                    }
                }
                $value = implode(CRM_CORE_BAO_CUSTOMOPTION_VALUE_SEPERATOR, $newMulValues);
            } else {
                if ($customFields[$customFieldID][3] == 'Select' || $customFields[$customFieldID][3] == 'Radio') {
                    $custuomOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true);
                    foreach ($custuomOption as $v2) {
                        if (strtolower($v2['label']) == strtolower(trim($value))) {
                            $value = $v2['value'];
                            break;
                        }
                    }
                }
            }
            $values['custom'][$customFieldID] = array('value' => $value, 'extends' => $customFields[$customFieldID][3], 'type' => $customFields[$customFieldID][2], 'custom_field_id' => $customFieldID);
        }
    }
    CRM_Contact_BAO_Contact::resolveDefaults($values, true);
    return null;
}
 /**
  * returns the list of fields that can be exported
  *
  * @access public
  * return array
  */
 function &export($prefix = false)
 {
     if (!$GLOBALS['_CRM_CORE_DAO_LOCATION']['_export']) {
         $GLOBALS['_CRM_CORE_DAO_LOCATION']['_export'] = array();
         $fields =& CRM_Core_DAO_Location::fields();
         foreach ($fields as $name => $field) {
             if (CRM_Utils_Array::value('export', $field)) {
                 if ($prefix) {
                     $GLOBALS['_CRM_CORE_DAO_LOCATION']['_export']['location'] =& $fields[$name];
                 } else {
                     $GLOBALS['_CRM_CORE_DAO_LOCATION']['_export'][$name] =& $fields[$name];
                 }
             }
         }
         $GLOBALS['_CRM_CORE_DAO_LOCATION']['_export'] = array_merge($GLOBALS['_CRM_CORE_DAO_LOCATION']['_export'], CRM_Core_DAO_LocationType::export(true));
     }
     return $GLOBALS['_CRM_CORE_DAO_LOCATION']['_export'];
 }