Exemplo n.º 1
0
/**
 * 
 * Retrieves an array of valid values for "enum" type properties 
 *
 * @param  $customField  Object  custom fields object
 *
 * @return  Array of custom field options. 
 *
 * @access public
 *
 */
function crm_get_option_values($customField)
{
    _crm_initialize();
    if (!isset($customField->id)) {
        return _crm_error("custom_field is not valid custom_field object");
    }
    $fieldId = $customField->id;
    require_once 'CRM/Core/BAO/CustomOption.php';
    return CRM_Core_BAO_CustomOption::getCustomOption($fieldId);
}
Exemplo n.º 2
0
/**
 * 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;
}
Exemplo n.º 3
0
 /**
  * Format common params data to proper format to store.
  *
  * @param array $params
  *   Contain record values.
  * @param array $formatted
  *   Array of formatted data.
  * @param array $contactFields
  *   Contact DAO fields.
  */
 public function formatCommonData($params, &$formatted, &$contactFields)
 {
     $csType = array(CRM_Utils_Array::value('contact_type', $formatted));
     //CRM-5125
     //add custom fields for contact sub type
     if (!empty($this->_contactSubType)) {
         $csType = $this->_contactSubType;
     }
     if ($relCsType = CRM_Utils_Array::value('contact_sub_type', $formatted)) {
         $csType = $relCsType;
     }
     $customFields = CRM_Core_BAO_CustomField::getFields($formatted['contact_type'], FALSE, FALSE, $csType);
     $addressCustomFields = CRM_Core_BAO_CustomField::getFields('Address');
     $customFields = $customFields + $addressCustomFields;
     //if a Custom Email Greeting, Custom Postal Greeting or Custom Addressee is mapped, and no "Greeting / Addressee Type ID" is provided, then automatically set the type = Customized, CRM-4575
     $elements = array('email_greeting_custom' => 'email_greeting', 'postal_greeting_custom' => 'postal_greeting', 'addressee_custom' => 'addressee');
     foreach ($elements as $k => $v) {
         if (array_key_exists($k, $params) && !array_key_exists($v, $params)) {
             $label = key(CRM_Core_OptionGroup::values($v, TRUE, NULL, NULL, 'AND v.name = "Customized"'));
             $params[$v] = $label;
         }
     }
     //format date first
     $session = CRM_Core_Session::singleton();
     $dateType = $session->get("dateTypes");
     foreach ($params as $key => $val) {
         $customFieldID = CRM_Core_BAO_CustomField::getKeyID($key);
         if ($customFieldID && !array_key_exists($customFieldID, $addressCustomFields)) {
             //we should not update Date to null, CRM-4062
             if ($val && $customFields[$customFieldID]['data_type'] == 'Date') {
                 self::formatCustomDate($params, $formatted, $dateType, $key);
             } elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
                 if (empty($val) && !is_numeric($val) && $this->_onDuplicate == CRM_Import_Parser::DUPLICATE_FILL) {
                     //retain earlier value when Import mode is `Fill`
                     unset($params[$key]);
                 } else {
                     $params[$key] = CRM_Utils_String::strtoboolstr($val);
                 }
             }
             if ($key == 'birth_date' && $val) {
                 CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key);
             } elseif ($key == 'deceased_date' && $val) {
                 CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key);
             } elseif ($key == 'is_deceased' && $val) {
                 $params[$key] = CRM_Utils_String::strtoboolstr($val);
             } elseif ($key == 'gender') {
                 //CRM-4360
                 $params[$key] = $this->checkGender($val);
             }
         }
     }
     //now format custom data.
     foreach ($params as $key => $field) {
         if (is_array($field)) {
             $isAddressCustomField = FALSE;
             foreach ($field as $value) {
                 $break = FALSE;
                 if (is_array($value)) {
                     foreach ($value as $name => $testForEmpty) {
                         if ($addressCustomFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) {
                             $isAddressCustomField = TRUE;
                             break;
                         }
                         // check if $value does not contain IM provider or phoneType
                         if (($name !== 'phone_type_id' || $name !== 'provider_id') && ($testForEmpty === '' || $testForEmpty == NULL)) {
                             $break = TRUE;
                             break;
                         }
                     }
                 } else {
                     $break = TRUE;
                 }
                 if (!$break) {
                     require_once 'CRM/Utils/DeprecatedUtils.php';
                     _civicrm_api3_deprecated_add_formatted_param($value, $formatted);
                 }
             }
             if (!$isAddressCustomField) {
                 continue;
             }
         }
         $formatValues = array($key => $field);
         if ($key !== 'preferred_communication_method' && array_key_exists($key, $contactFields)) {
             // due to merging of individual table and
             // contact table, we need to avoid
             // preferred_communication_method forcefully
             $formatValues['contact_type'] = $formatted['contact_type'];
         }
         if ($key == 'id' && isset($field)) {
             $formatted[$key] = $field;
         }
         require_once 'CRM/Utils/DeprecatedUtils.php';
         _civicrm_api3_deprecated_add_formatted_param($formatValues, $formatted);
         //Handling Custom Data
         // note: Address custom fields will be handled separately inside _civicrm_api3_deprecated_add_formatted_param
         if (($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) && array_key_exists($customFieldID, $customFields) && !array_key_exists($customFieldID, $addressCustomFields)) {
             $extends = CRM_Utils_Array::value('extends', $customFields[$customFieldID]);
             $htmlType = CRM_Utils_Array::value('html_type', $customFields[$customFieldID]);
             switch ($htmlType) {
                 case 'Select':
                 case 'Radio':
                 case 'Autocomplete-Select':
                     if ($customFields[$customFieldID]['data_type'] == 'String') {
                         $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
                         foreach ($customOption as $customFldID => $customValue) {
                             $val = CRM_Utils_Array::value('value', $customValue);
                             $label = CRM_Utils_Array::value('label', $customValue);
                             $label = strtolower($label);
                             $value = strtolower(trim($formatted[$key]));
                             if ($value == $label || $value == strtolower($val)) {
                                 $params[$key] = $formatted[$key] = $val;
                             }
                         }
                     }
                     break;
                 case 'CheckBox':
                 case 'AdvMulti-Select':
                 case 'Multi-Select':
                     if (!empty($formatted[$key]) && !empty($params[$key])) {
                         $mulValues = explode(',', $formatted[$key]);
                         $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
                         $formatted[$key] = array();
                         $params[$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') {
                                         $params[$key][$v2['value']] = $formatted[$key][$v2['value']] = 1;
                                     } else {
                                         $params[$key][] = $formatted[$key][] = $v2['value'];
                                     }
                                 }
                             }
                         }
                     }
                     break;
             }
         }
     }
     if (($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) && array_key_exists($customFieldID, $customFields) && !array_key_exists($customFieldID, $addressCustomFields)) {
         // @todo calling api functions directly is not supported
         _civicrm_api3_custom_format_params($params, $formatted, $extends);
     }
     // to check if not update mode and unset the fields with empty value.
     if (!$this->_updateWithId && array_key_exists('custom', $formatted)) {
         foreach ($formatted['custom'] as $customKey => $customvalue) {
             if (empty($formatted['custom'][$customKey][-1]['is_required'])) {
                 $formatted['custom'][$customKey][-1]['is_required'] = $customFields[$customKey]['is_required'];
             }
             $emptyValue = CRM_Utils_Array::value('value', $customvalue[-1]);
             if (!isset($emptyValue)) {
                 unset($formatted['custom'][$customKey]);
             }
         }
     }
     // parse street address, CRM-5450
     if ($this->_parseStreetAddress) {
         if (array_key_exists('address', $formatted) && is_array($formatted['address'])) {
             foreach ($formatted['address'] as $instance => &$address) {
                 $streetAddress = CRM_Utils_Array::value('street_address', $address);
                 if (empty($streetAddress)) {
                     continue;
                 }
                 // parse address field.
                 $parsedFields = CRM_Core_BAO_Address::parseStreetAddress($streetAddress);
                 //street address consider to be parsed properly,
                 //If we get street_name and street_number.
                 if (empty($parsedFields['street_name']) || empty($parsedFields['street_number'])) {
                     $parsedFields = array_fill_keys(array_keys($parsedFields), '');
                 }
                 // merge parse address w/ main address block.
                 $address = array_merge($address, $parsedFields);
             }
         }
     }
 }
Exemplo n.º 4
0
function _crm_format_custom_params(&$params, &$values, $extends)
{
    $values['custom'] = array();
    $customFields = CRM_Core_BAO_CustomField::getFields($extends);
    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');
            }
            $fieldType = null;
            // modified for CRM-1586
            // check data type for importing custom field (labels) with data type Integer/Float/Money
            /* validate the data against the CF type */
            if ($customFields[$customFieldID]['data_type'] == "Int" || $customFields[$customFieldID]['data_type'] == "Float" || $customFields[$customFieldID]['data_type'] == "Money") {
                if ($customFields[$customFieldID]['html_type'] == "Text") {
                    $fieldType = $customFields[$customFieldID]['data_type'];
                } else {
                    $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID);
                    foreach ($customOption as $customValue => $customLabel) {
                        //check wether $value is label or value
                        if (strtolower($customValue) == strtolower(trim($value))) {
                            $fieldType = "String";
                        } else {
                            if (strtolower($customValue) == strtolower(trim($value))) {
                                $fieldType = $customFields[$customFieldID]['data_type'];
                            }
                        }
                    }
                }
            } else {
                //set the Field type
                $fieldType = $customFields[$customFieldID]['data_type'];
            }
            $valid = null;
            //Validate the datatype of $value
            $valid = CRM_Core_BAO_CustomValue::typecheck($fieldType, $value);
            //return error, if not valid custom field
            if (!$valid) {
                return _crm_error('Invalid value for custom field ' . $customFields[$customFieldID][1]);
            }
            // fix the date field if so
            if ($customFields[$customFieldID]['data_type'] == 'Date') {
                $value = str_replace('-', '', $value);
            }
            // fixed for checkbox and multiselect
            $newMulValues = array();
            if ($customFields[$customFieldID]['html_type'] == 'CheckBox' || $customFields[$customFieldID]['html_type'] == 'Multi-Select') {
                $value = str_replace(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, ',', trim($value, CRM_Core_BAO_CustomOption::VALUE_SEPERATOR));
                $value = str_replace("|", ",", $value);
                $mulValues = explode(',', $value);
                $custumOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true);
                foreach ($mulValues as $v1) {
                    foreach ($customOption as $customValue => $customLabel) {
                        if (strtolower($customLabel) == strtolower(trim($v1)) || strtolower($customValue) == strtolower(trim($v1))) {
                            $newMulValues[] = $customValue;
                        }
                    }
                }
                $value = CRM_Core_BAO_CustomOption::VALUE_SEPERATOR . implode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, $newMulValues) . CRM_Core_BAO_CustomOption::VALUE_SEPERATOR;
            } else {
                if ($customFields[$customFieldID]['html_type'] == 'Select' || $customFields[$customFieldID]['html_type'] == 'Radio') {
                    $custumOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true);
                    foreach ($customOption as $customValue => $customLabel) {
                        if (strtolower($customLabel) == strtolower(trim($value)) || strtolower($customValue) == strtolower(trim($value))) {
                            $value = $customValue;
                            break;
                        }
                    }
                }
            }
            $values['custom'][$customFieldID] = array('value' => $value, 'extends' => $customFields[$customFieldID]['extends'], 'type' => $customFields[$customFieldID]['data_type'], 'custom_field_id' => $customFieldID);
        }
    }
}
Exemplo n.º 5
0
 /**
  * format common params data to proper format to store.
  *
  * @param array  $params        contain record values.
  * @param array  $formatted     array of formatted data.
  * @param array  $contactFields contact DAO fields.
  * @static
  */
 function formatCommonData($params, &$formatted, &$contactFields)
 {
     $csType = array(CRM_Utils_Array::value('contact_type', $formatted));
     //CRM-5125
     //add custom fields for contact sub type
     if (!empty($this->_contactSubType)) {
         $csType = $this->_contactSubType;
     }
     if ($relCsType = CRM_Utils_Array::value('contact_sub_type', $formatted)) {
         $csType = $relCsType;
     }
     $customFields = CRM_Core_BAO_CustomField::getFields($formatted['contact_type'], false, false, $csType);
     //if a Custom Email Greeting, Custom Postal Greeting or Custom Addressee is mapped, and no "Greeting / Addressee Type ID" is provided, then automatically set the type = Customized, CRM-4575
     $elements = array('email_greeting_custom' => 'email_greeting', 'postal_greeting_custom' => 'postal_greeting', 'addressee_custom' => 'addressee');
     foreach ($elements as $k => $v) {
         if (array_key_exists($k, $params) && !array_key_exists($v, $params)) {
             $label = key(CRM_Core_OptionGroup::values($v, true, null, null, 'AND v.name = "Customized"'));
             $params[$v] = $label;
         }
     }
     //format date first
     $session =& CRM_Core_Session::singleton();
     $dateType = $session->get("dateTypes");
     foreach ($params as $key => $val) {
         if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
             //we should not update Date to null, CRM-4062
             if ($val && $customFields[$customFieldID]['data_type'] == 'Date') {
                 self::formatCustomDate($params, $formatted, $dateType, $key);
                 unset($params[$key]);
             } else {
                 if ($customFields[$customFieldID]['data_type'] == 'Boolean') {
                     $params[$key] = CRM_Utils_String::strtoboolstr($val);
                 }
             }
         }
         if ($key == 'birth_date' && $val) {
             CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key);
         } else {
             if ($key == 'deceased_date' && $val) {
                 CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key);
             } else {
                 if ($key == 'is_deceased' && $val) {
                     $params[$key] = CRM_Utils_String::strtoboolstr($val);
                 } else {
                     if ($key == 'gender') {
                         //CRM-4360
                         $params[$key] = $this->checkGender($val);
                     }
                 }
             }
         }
     }
     //now format custom data.
     foreach ($params as $key => $field) {
         if ($field == null || $field === '') {
             continue;
         }
         if (is_array($field)) {
             foreach ($field as $value) {
                 $break = false;
                 if (is_array($value)) {
                     foreach ($value as $name => $testForEmpty) {
                         // check if $value does not contain IM provider or phoneType
                         if (($name !== 'phone_type_id' || $name !== 'provider_id') && ($testForEmpty === '' || $testForEmpty == null)) {
                             $break = true;
                             break;
                         }
                     }
                 } else {
                     $break = true;
                 }
                 if (!$break) {
                     _civicrm_add_formatted_param($value, $formatted);
                 }
             }
             continue;
         }
         $formatValues = array($key => $field);
         if ($key !== 'preferred_communication_method' && array_key_exists($key, $contactFields)) {
             // due to merging of individual table and
             // contact table, we need to avoid
             // preferred_communication_method forcefully
             $formatValues['contact_type'] = $formatted['contact_type'];
         }
         if ($key == 'id' && isset($field)) {
             $formatted[$key] = $field;
         }
         _civicrm_add_formatted_param($formatValues, $formatted);
         //Handling Custom Data
         if (($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) && array_key_exists($customFieldID, $customFields)) {
             //get the html type.
             $type = $customFields[$customFieldID]['html_type'];
             switch ($type) {
                 case 'CheckBox':
                 case 'AdvMulti-Select':
                 case 'Multi-Select':
                     $mulValues = explode(',', $field);
                     $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true);
                     $formatted[$key] = array();
                     foreach ($mulValues as $v1) {
                         foreach ($customOption as $v2) {
                             if (strtolower($v2['label']) == strtolower(trim($v1)) || strtolower($v2['value']) == strtolower(trim($v1))) {
                                 if ($type == 'CheckBox') {
                                     $formatted[$key][$v2['value']] = 1;
                                 } else {
                                     $formatted[$key][] = $v2['value'];
                                 }
                             }
                         }
                     }
                     break;
                 case 'Select':
                 case 'Radio':
                     $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true);
                     foreach ($customOption as $v2) {
                         if (strtolower($v2['label']) == strtolower(trim($field)) || strtolower($v2['value']) == strtolower(trim($field))) {
                             $formatted[$key] = $v2['value'];
                         }
                     }
                     break;
                 case 'Multi-Select State/Province':
                     $mulValues = explode(',', $field);
                     $stateAbbr = CRM_Core_PseudoConstant::stateProvinceAbbreviation();
                     $stateName = CRM_Core_PseudoConstant::stateProvince();
                     $formatted[$key] = $stateValues = array();
                     foreach ($mulValues as $values) {
                         if ($val = CRM_Utils_Array::key($values, $stateAbbr)) {
                             $formatted[$key][] = $val;
                         } else {
                             if ($val = CRM_Utils_Array::key($values, $stateName)) {
                                 $formatted[$key][] = $val;
                             }
                         }
                     }
                     break;
                 case 'Multi-Select Country':
                     $config =& CRM_Core_Config::singleton();
                     $limitCodes = $config->countryLimit();
                     $mulValues = explode(',', $field);
                     $formatted[$key] = array();
                     CRM_Core_PseudoConstant::populate($countryNames, 'CRM_Core_DAO_Country', true, 'name', 'is_active');
                     CRM_Core_PseudoConstant::populate($countryIsoCodes, 'CRM_Core_DAO_Country', true, 'iso_code');
                     foreach ($mulValues as $values) {
                         if ($val = CRM_Utils_Array::key($values, $countryNames)) {
                             $formatted[$key][] = $val;
                         } else {
                             if ($val = CRM_Utils_Array::key($values, $countryIsoCodes)) {
                                 $formatted[$key][] = $val;
                             } else {
                                 if ($val = CRM_Utils_Array::key($values, $limitCodes)) {
                                     $formatted[$key][] = $val;
                                 }
                             }
                         }
                     }
                     break;
             }
         }
     }
     // check for primary location type, whether it is already present for the contact or not, CRM-4423
     if (CRM_Utils_Array::value('id', $formatted) && isset($formatted['location'])) {
         $primaryLocationTypeId = CRM_Contact_BAO_Contact::getPrimaryLocationType($formatted['id'], true);
         if (isset($primaryLocationTypeId)) {
             foreach ($formatted['location'] as $loc => $details) {
                 if ($primaryLocationTypeId == CRM_Utils_Array::value('location_type_id', $details)) {
                     $formatted['location'][$loc]['is_primary'] = 1;
                     break;
                 } else {
                     $formatted['location'][$loc]['is_primary'] = 0;
                 }
             }
         }
     }
     // parse street address, CRM-5450
     if ($this->_parseStreetAddress) {
         require_once 'CRM/Core/BAO/Address.php';
         if (array_key_exists('address', $formatted) && is_array($formatted['address'])) {
             foreach ($formatted['address'] as $instance => &$address) {
                 $streetAddress = CRM_Utils_Array::value('street_address', $address);
                 if (empty($streetAddress)) {
                     continue;
                 }
                 // parse address field.
                 $parsedFields = CRM_Core_BAO_Address::parseStreetAddress($streetAddress);
                 //street address consider to be parsed properly,
                 //If we get street_name and street_number.
                 if (!CRM_Utils_Array::value('street_name', $parsedFields) || !CRM_Utils_Array::value('street_number', $parsedFields)) {
                     $parsedFields = array_fill_keys(array_keys($parsedFields), '');
                 }
                 // merge parse address w/ main address block.
                 $address = array_merge($address, $parsedFields);
             }
         }
     }
 }
 /**
  * Browse the listing
  *
  * @return void
  * @access public
  */
 function browse()
 {
     $dateFields = NULL;
     $cgcount = 0;
     $dateFieldsVals = NULL;
     if ($this->_pageViewType == 'profileDataView' && $this->_profileId) {
         $fields = CRM_Core_BAO_UFGroup::getFields($this->_profileId, FALSE, NULL, NULL, NULL, FALSE, NULL, FALSE, NULL, CRM_Core_Permission::EDIT);
         $multiRecordFields = array();
         $fieldIDs = NULL;
         $result = NULL;
         $multiRecordFieldsWithSummaryListing = CRM_Core_BAO_UFGroup::shiftMultiRecordFields($fields, $multiRecordFields, TRUE);
         $multiFieldId = CRM_Core_BAO_CustomField::getKeyID(key($multiRecordFields));
         $customGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $multiFieldId, 'custom_group_id');
         $reached = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupId, $this->_contactId);
         if (!$reached) {
             $this->assign('contactId', $this->_contactId);
             $this->assign('gid', $this->_profileId);
         }
         $this->assign('reachedMax', $reached);
         if ($multiRecordFieldsWithSummaryListing && !empty($multiRecordFieldsWithSummaryListing)) {
             $fieldIDs = array_keys($multiRecordFieldsWithSummaryListing);
         }
     } elseif ($this->_pageViewType == 'customDataView') {
         // require custom group id for _pageViewType of customDataView
         $customGroupId = $this->_customGroupId;
         $reached = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupId, $this->_contactId);
         if (!$reached) {
             $this->assign('contactId', $this->_contactId);
             $this->assign('customGroupId', $customGroupId);
             $this->assign('ctype', $this->_contactType);
         }
         $this->assign('reachedMax', $reached);
         // custom group info : this consists of the field title of group fields
         $groupDetail = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId, NULL, CRM_Core_DAO::$_nullObject, TRUE);
         // field ids of fields in_selector for the custom group id provided
         $fieldIDs = array_keys($groupDetail[$customGroupId]['fields']);
         // field labels for headers
         $fieldLabels = $groupDetail[$customGroupId]['fields'];
         // from the above customGroupInfo we can get $this->_customGroupTitle
         $this->_customGroupTitle = $groupDetail[$customGroupId]['title'];
     }
     if ($fieldIDs && !empty($fieldIDs) && $this->_contactId) {
         $options = array();
         $returnProperities = array('html_type', 'data_type', 'date_format', 'time_format');
         foreach ($fieldIDs as $key => $fieldID) {
             $fieldIDs[$key] = !is_numeric($fieldID) ? CRM_Core_BAO_CustomField::getKeyID($fieldID) : $fieldID;
             $param = array('id' => $fieldIDs[$key]);
             $returnValues = array();
             CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $param, $returnValues, $returnProperities);
             if ($returnValues['data_type'] == 'Date') {
                 $dateFields[$fieldIDs[$key]] = 1;
             }
             $optionValuePairs = CRM_Core_BAO_CustomOption::getCustomOption($fieldIDs[$key]);
             if (!empty($optionValuePairs)) {
                 foreach ($optionValuePairs as $optionPairs) {
                     $options[$fieldIDs[$key]][$optionPairs['value']] = $optionPairs['label'];
                 }
             }
             $options[$fieldIDs[$key]]['attributes']['html_type'] = $returnValues['html_type'];
             $options[$fieldIDs[$key]]['attributes']['data_type'] = $returnValues['data_type'];
             $options[$fieldIDs[$key]]['attributes']['format'] = $options[$fieldIDs[$key]]['attributes']['date_format'] = CRM_Utils_Array::value('date_format', $returnValues);
             $options[$fieldIDs[$key]]['attributes']['time_format'] = CRM_Utils_Array::value('time_format', $returnValues);
         }
         // commonly used for both views i.e profile listing view (profileDataView) and custom data listing view (customDataView)
         $result = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_contactId, NULL, $fieldIDs, TRUE);
         if ($this->_pageViewType == 'profileDataView') {
             if (!empty($fieldIDs)) {
                 //get the group info of multi rec fields in listing view
                 $fieldInput = $fieldIDs;
                 $fieldIdInput = $fieldIDs[0];
             } else {
                 //if no listing fields exist, take the group title for display
                 $nonListingFieldIds = array_keys($multiRecordFields);
                 $singleField = CRM_Core_BAO_CustomField::getKeyID($nonListingFieldIds[0]);
                 $fieldIdInput = $singleField;
                 $singleField = array($singleField);
                 $fieldInput = $singleField;
             }
             $customGroupInfo = CRM_Core_BAO_CustomGroup::getGroupTitles($fieldInput);
             $this->_customGroupTitle = $customGroupInfo[$fieldIdInput]['groupTitle'];
         }
         // $cgcount is defined before 'if' condition as enitiy may have no record
         // and $cgcount is used to build new record url
         $cgcount = 1;
         if ($result && !empty($result)) {
             $links = self::links();
             if ($this->_pageViewType == 'profileDataView') {
                 $pageCheckSum = $this->get('pageCheckSum');
                 if ($pageCheckSum) {
                     foreach ($links as $key => $link) {
                         $links[$key] = $link['qs'] . "&cs=%%cs%%";
                     }
                 }
             }
             $linkAction = array_sum(array_keys($this->links()));
             if ($reached) {
                 unset($links[CRM_Core_Action::COPY]);
             }
             $newCgCount = !$reached ? count($result) + 1 : NULL;
             foreach ($result as $recId => &$value) {
                 foreach ($value as $fieldId => &$val) {
                     if (is_numeric($fieldId)) {
                         $customValue =& $val;
                         if (!empty($dateFields) && array_key_exists($fieldId, $dateFields)) {
                             // formated date capture value capture
                             $dateFieldsVals[$fieldId][$recId] = CRM_Core_BAO_CustomField::getDisplayValue($customValue, $fieldId, $options);
                         } else {
                             // assign to $result
                             $customValue = CRM_Core_BAO_CustomField::getDisplayValue($customValue, $fieldId, $options);
                         }
                         if (!$customValue) {
                             $customValue = "";
                         }
                         $op = NULL;
                         if ($this->_pageViewType == 'profileDataView') {
                             $actionParams = array('recordId' => $recId, 'gid' => $this->_profileId, 'id' => $this->_contactId);
                             $op = 'profile.multiValue.row';
                         } else {
                             // different set of url params
                             $actionParams['gid'] = $actionParams['groupID'] = $this->_customGroupId;
                             $actionParams['cid'] = $actionParams['entityID'] = $this->_contactId;
                             $actionParams['recId'] = $recId;
                             $actionParams['type'] = $this->_contactType;
                             $actionParams['cgcount'] = $cgcount;
                             $actionParams['newCgCount'] = $newCgCount;
                             // DELETE action links
                             $deleteData = array('valueID' => $recId, 'groupID' => $this->_customGroupId, 'contactId' => $this->_contactId, 'key' => CRM_Core_Key::get('civicrm/ajax/customvalue'));
                             $links[CRM_Core_Action::DELETE]['url'] = '#';
                             $links[CRM_Core_Action::DELETE]['extra'] = ' data-delete_params="' . htmlspecialchars(json_encode($deleteData)) . '"';
                             $links[CRM_Core_Action::DELETE]['class'] = 'delete-custom-row';
                         }
                         if (!empty($pageCheckSum)) {
                             $actionParams['cs'] = $pageCheckSum;
                         }
                         $value['action'] = CRM_Core_Action::formLink($links, $linkAction, $actionParams, ts('more'), FALSE, $op, 'customValue', $fieldId);
                     }
                 }
                 $cgcount++;
             }
         }
     }
     $headers = array();
     if (!empty($fieldIDs)) {
         foreach ($fieldIDs as $fieldID) {
             $headers[$fieldID] = $this->_pageViewType == 'profileDataView' ? $customGroupInfo[$fieldID]['fieldLabel'] : $fieldLabels[$fieldID]['label'];
         }
     }
     $this->assign('dateFields', $dateFields);
     $this->assign('dateFieldsVals', $dateFieldsVals);
     $this->assign('cgcount', $cgcount);
     $this->assign('customGroupTitle', $this->_customGroupTitle);
     $this->assign('headers', $headers);
     $this->assign('records', $result);
 }
 /**
  * Browse the listing
  *
  * @return void
  * @access public
  */
 function browse()
 {
     if ($this->_profileId) {
         $fields = CRM_Core_BAO_UFGroup::getFields($this->_profileId, FALSE, NULL, NULL, NULL, FALSE, NULL, FALSE, NULL, CRM_Core_Permission::EDIT);
         $multiRecordFields = array();
         $fieldIDs = NULL;
         $result = NULL;
         $multiRecordFieldsWithSummaryListing = CRM_Core_BAO_UFGroup::shiftMultiRecordFields($fields, $multiRecordFields, TRUE);
         $multiFieldId = CRM_Core_BAO_CustomField::getKeyID(key($multiRecordFields));
         $customGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $multiFieldId, 'custom_group_id');
         $reached = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupId, $this->_contactId);
         if (!$reached) {
             $this->assign('contactId', $this->_contactId);
             $this->assign('gid', $this->_profileId);
         }
         $this->assign('reachedMax', $reached);
         if ($multiRecordFieldsWithSummaryListing && !empty($multiRecordFieldsWithSummaryListing)) {
             $fieldIDs = array_keys($multiRecordFieldsWithSummaryListing);
         }
     }
     if ($fieldIDs && !empty($fieldIDs) && $this->_contactId) {
         $options = array();
         $returnProperities = array('html_type', 'data_type', 'date_format', 'time_format');
         foreach ($fieldIDs as $key => $fieldID) {
             $fieldIDs[$key] = CRM_Core_BAO_CustomField::getKeyID($fieldID);
             $param = array('id' => $fieldIDs[$key]);
             $returnValues = array();
             CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $param, $returnValues, $returnProperities);
             $optionValuePairs = CRM_Core_BAO_CustomOption::getCustomOption($fieldIDs[$key]);
             if (!empty($optionValuePairs)) {
                 foreach ($optionValuePairs as $optionPairs) {
                     $options[$fieldIDs[$key]][$optionPairs['value']] = $optionPairs['label'];
                 }
             }
             $options[$fieldIDs[$key]]['attributes']['html_type'] = $returnValues['html_type'];
             $options[$fieldIDs[$key]]['attributes']['data_type'] = $returnValues['data_type'];
             $options[$fieldIDs[$key]]['attributes']['format'] = $options[$fieldIDs[$key]]['attributes']['date_format'] = CRM_Utils_Array::value('date_format', $returnValues);
             $options[$fieldIDs[$key]]['attributes']['time_format'] = CRM_Utils_Array::value('time_format', $returnValues);
         }
         $result = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_contactId, NULL, $fieldIDs, TRUE);
         if (!empty($fieldIDs)) {
             //get the group info of multi rec fields in listing view
             $fieldInput = $fieldIDs;
             $fieldIdInput = $fieldIDs[0];
         } else {
             //if no listing fields exist, take the group title for display
             $nonListingFieldIds = array_keys($multiRecordFields);
             $singleField = CRM_Core_BAO_CustomField::getKeyID($nonListingFieldIds[0]);
             $fieldIdInput = $singleField;
             $singleField = array($singleField);
             $fieldInput = $singleField;
         }
         $customGroupInfo = CRM_Core_BAO_CustomGroup::getGroupTitles($fieldInput);
         $this->_customGroupTitle = $customGroupInfo[$fieldIdInput]['groupTitle'];
         if ($result && !empty($result)) {
             $links = self::links();
             $pageCheckSum = $this->get('pageCheckSum');
             if ($pageCheckSum) {
                 foreach ($links as $key => $link) {
                     $links[$key] = $link['qs'] . "&cs=%%cs%%";
                 }
             }
             $linkAction = array_sum(array_keys($this->links()));
             foreach ($result as $recId => &$value) {
                 foreach ($value as $fieldId => &$val) {
                     if (is_numeric($fieldId)) {
                         $customValue =& $val;
                         $customValue = CRM_Core_BAO_CustomField::getDisplayValue($customValue, $fieldId, $options);
                         if (!$customValue) {
                             $customValue = "";
                         }
                         $actionParams = array('recordId' => $recId, 'gid' => $this->_profileId, 'id' => $this->_contactId, 'onPopupClose' => $this->_onPopupClose);
                         if ($pageCheckSum) {
                             $actionParams['cs'] = $pageCheckSum;
                         }
                         $value['action'] = CRM_Core_Action::formLink($links, $linkAction, $actionParams, ts('more'), FALSE, 'profile.multiValue.row', 'customValue', $fieldId);
                     }
                 }
             }
         }
     }
     $headers = array();
     if (!empty($fieldIDs)) {
         foreach ($fieldIDs as $fieldID) {
             $headers[$fieldID] = $customGroupInfo[$fieldID]['fieldLabel'];
         }
     }
     $this->assign('customGroupTitle', $this->_customGroupTitle);
     $this->assign('headers', $headers);
     $this->assign('records', $result);
 }
Exemplo n.º 8
0
 /**
  * Extract the get params from the url, validate and store it in session.
  *
  * @param CRM_Core_Form $form
  *   The form object.
  * @param string $type
  *   The type of custom group we are using.
  *
  * @return array
  */
 public static function extractGetParams(&$form, $type)
 {
     if (empty($_GET)) {
         return array();
     }
     $groupTree = CRM_Core_BAO_CustomGroup::getTree($type);
     $customValue = array();
     $htmlType = array('CheckBox', 'Multi-Select', 'AdvMulti-Select', 'Select', 'Radio');
     foreach ($groupTree as $group) {
         if (!isset($group['fields'])) {
             continue;
         }
         foreach ($group['fields'] as $key => $field) {
             $fieldName = 'custom_' . $key;
             $value = CRM_Utils_Request::retrieve($fieldName, 'String', $form, FALSE, NULL, 'GET');
             if ($value) {
                 $valid = FALSE;
                 if (!in_array($field['html_type'], $htmlType) || $field['data_type'] == 'Boolean') {
                     $valid = CRM_Core_BAO_CustomValue::typecheck($field['data_type'], $value);
                 }
                 if ($field['html_type'] == 'CheckBox' || $field['html_type'] == 'AdvMulti-Select' || $field['html_type'] == 'Multi-Select') {
                     $value = str_replace("|", ",", $value);
                     $mulValues = explode(',', $value);
                     $customOption = CRM_Core_BAO_CustomOption::getCustomOption($key, TRUE);
                     $val = array();
                     foreach ($mulValues as $v1) {
                         foreach ($customOption as $coID => $coValue) {
                             if (strtolower(trim($coValue['label'])) == strtolower(trim($v1))) {
                                 $val[$coValue['value']] = 1;
                             }
                         }
                     }
                     if (!empty($val)) {
                         $value = $val;
                         $valid = TRUE;
                     } else {
                         $value = NULL;
                     }
                 } elseif ($field['html_type'] == 'Select' || $field['html_type'] == 'Radio' && $field['data_type'] != 'Boolean') {
                     $customOption = CRM_Core_BAO_CustomOption::getCustomOption($key, TRUE);
                     foreach ($customOption as $customID => $coValue) {
                         if (strtolower(trim($coValue['label'])) == strtolower(trim($value))) {
                             $value = $coValue['value'];
                             $valid = TRUE;
                         }
                     }
                 } elseif ($field['data_type'] == 'Date') {
                     if (!empty($value)) {
                         $time = NULL;
                         if (!empty($field['time_format'])) {
                             $time = CRM_Utils_Request::retrieve($fieldName . '_time', 'String', $form, FALSE, NULL, 'GET');
                         }
                         list($value, $time) = CRM_Utils_Date::setDateDefaults($value . ' ' . $time);
                         if (!empty($field['time_format'])) {
                             $customValue[$fieldName . '_time'] = $time;
                         }
                     }
                     $valid = TRUE;
                 }
                 if ($valid) {
                     $customValue[$fieldName] = $value;
                 }
             }
         }
     }
     return $customValue;
 }
Exemplo n.º 9
0
/**
 * This function adds the activity history variable in $values to the
 * parameter list $params.  For most cases, $values should have length 1.
 *
 * @param array  $values    The variable(s) to be added
 * @param array  $params    The structured parameter list
 * 
 * @return bool|CRM_Utils_Error
 * @access public
 */
function _crm_add_formatted_history_param(&$values, &$params)
{
    /* Cache the various object fields */
    static $fields = null;
    if ($fields == null) {
        $fields = array();
    }
    //print_r($values);
    //print_r($params);
    if (isset($values['activity_type'])) {
        $params['activity_type'] = $values['activity_type'];
        return true;
    }
    if (isset($values['activity_date'])) {
        $params['activity_date'] = $values['activity_date'];
        return true;
    }
    if (isset($values['activity_id'])) {
        $params['activity_id'] = $values['activity_id'];
        return true;
    }
    /* Check for custom field values */
    if ($fields['custom'] == null) {
        $fields['custom'] =& CRM_Core_BAO_CustomField::getFields('Contribution');
    }
    foreach ($values 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, $fields['custom'])) {
                return _crm_error('Invalid custom field ID');
            }
            if (!isset($params['custom'])) {
                $params['custom'] = array();
            }
            // fixed for Import
            $newMulValues = array();
            if ($fields['custom'][$customFieldID][3] == 'CheckBox' || $fields['custom'][$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 ($fields['custom'][$customFieldID][3] == 'Select' || $fields['custom'][$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;
                        }
                    }
                }
            }
            $customBlock = count($params['custom']) + 1;
            $params['custom'][$customBlock] = array('custom_field_id' => $customFieldID, 'value' => $value, 'type' => $fields['custom'][$customFieldID][2], 'name' => $fields['custom'][$customFieldID][0]);
        }
    }
    /* Finally, check for contribution fields */
    if (!isset($fields['History'])) {
        $fields['History'] =& CRM_Core_DAO_ActivityHistory::fields();
    }
    _crm_store_values($fields['History'], $values, $params);
}
Exemplo n.º 10
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;
}
Exemplo n.º 11
0
 /**
  *  function to check if an error in custom data
  *  
  *  @param String   $errorMessage   A string containing all the error-fields.
  *  
  *  @access public 
  */
 function isErrorInCustomData($params, &$errorMessage)
 {
     $session = CRM_Core_Session::singleton();
     $dateType = $session->get("dateTypes");
     //CRM-5125
     //add custom fields for contact sub type
     if (!empty($this->_contactSubType)) {
         $csType = $this->_contactSubType;
     }
     if (CRM_Utils_Array::value('contact_sub_type', $params)) {
         $csType = CRM_Utils_Array::value('contact_sub_type', $params);
     }
     $customFields = CRM_Core_BAO_CustomField::getFields($params['contact_type'], false, false, $csType);
     $addressCustomFields = CRM_Core_BAO_CustomField::getFields('Address');
     $customFields = $customFields + $addressCustomFields;
     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)) {
                 self::addToErrorMsg(ts('field ID'), $errorMessage);
             }
             //For address custom fields, we do get actual custom field value as an inner array of
             //values so need to modify
             if (array_key_exists($customFieldID, $addressCustomFields)) {
                 $value = $value[0][$key];
             }
             /* validate the data against the CF type */
             if ($value) {
                 if ($customFields[$customFieldID]['data_type'] == 'Date') {
                     if (CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key)) {
                         $value = $params[$key];
                     } else {
                         self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
                     }
                 } else {
                     if ($customFields[$customFieldID]['data_type'] == 'Boolean') {
                         if (CRM_Utils_String::strtoboolstr($value) === false) {
                             self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
                         }
                     }
                 }
                 // need not check for label filed import
                 $htmlType = array('CheckBox', 'Multi-Select', 'AdvMulti-Select', 'Select', 'Radio', 'Multi-Select State/Province', 'Multi-Select Country', 'Text');
                 if (!in_array($customFields[$customFieldID]['html_type'], $htmlType) || $customFields[$customFieldID]['data_type'] == 'Boolean' || $customFields[$customFieldID]['data_type'] == 'ContactReference') {
                     $valid = CRM_Core_BAO_CustomValue::typecheck($customFields[$customFieldID]['data_type'], $value);
                     if (!$valid) {
                         self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
                     }
                 }
                 // check for values for custom fields for checkboxes and multiselect
                 if ($customFields[$customFieldID]['html_type'] == 'CheckBox' || $customFields[$customFieldID]['html_type'] == 'AdvMulti-Select' || $customFields[$customFieldID]['html_type'] == 'Multi-Select') {
                     $value = trim($value);
                     $value = str_replace('|', ',', $value);
                     $mulValues = explode(',', $value);
                     $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true);
                     foreach ($mulValues as $v1) {
                         if (strlen($v1) == 0) {
                             continue;
                         }
                         $flag = false;
                         foreach ($customOption as $v2) {
                             if (strtolower(trim($v2['label'])) == strtolower(trim($v1)) || strtolower(trim($v2['value'])) == strtolower(trim($v1))) {
                                 $flag = true;
                             }
                         }
                         if (!$flag) {
                             self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
                         }
                     }
                 } else {
                     if ($customFields[$customFieldID]['html_type'] == 'Select' || $customFields[$customFieldID]['html_type'] == 'Radio' && $customFields[$customFieldID]['data_type'] != 'Boolean') {
                         $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true);
                         $flag = false;
                         foreach ($customOption as $v2) {
                             if (strtolower(trim($v2['label'])) == strtolower(trim($value)) || strtolower(trim($v2['value'])) == strtolower(trim($value))) {
                                 $flag = true;
                             }
                         }
                         if (!$flag) {
                             self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
                         }
                     } else {
                         if ($customFields[$customFieldID]['html_type'] == 'Multi-Select State/Province') {
                             $mulValues = explode(',', $value);
                             foreach ($mulValues as $stateValue) {
                                 if ($stateValue) {
                                     if (self::in_value(trim($stateValue), CRM_Core_PseudoConstant::stateProvinceAbbreviation()) || self::in_value(trim($stateValue), CRM_Core_PseudoConstant::stateProvince())) {
                                         continue;
                                     } else {
                                         self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
                                     }
                                 }
                             }
                         } else {
                             if ($customFields[$customFieldID]['html_type'] == 'Multi-Select Country') {
                                 $mulValues = explode(',', $value);
                                 foreach ($mulValues as $countryValue) {
                                     if ($countryValue) {
                                         CRM_Core_PseudoConstant::populate($countryNames, 'CRM_Core_DAO_Country', true, 'name', 'is_active');
                                         CRM_Core_PseudoConstant::populate($countryIsoCodes, 'CRM_Core_DAO_Country', true, 'iso_code');
                                         $config = CRM_Core_Config::singleton();
                                         $limitCodes = $config->countryLimit();
                                         $error = true;
                                         foreach (array($countryNames, $countryIsoCodes, $limitCodes) as $values) {
                                             if (in_array(trim($countryValue), $values)) {
                                                 $error = false;
                                                 break;
                                             }
                                         }
                                         if ($error) {
                                             self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage);
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         } else {
             if (is_array($params[$key]) && isset($params[$key]["contact_type"])) {
                 //CRM-5125
                 //supporting custom data of related contact subtypes
                 if (array_key_exists($key, $this->_relationships)) {
                     $relation = $key;
                 } else {
                     if (CRM_Utils_Array::key($key, $this->_relationships)) {
                         $relation = CRM_Utils_Array::key($key, $this->_relationships);
                     }
                 }
                 if (!empty($relation)) {
                     list($id, $first, $second) = CRM_Utils_System::explode('_', $relation, 3);
                     $direction = "contact_sub_type_{$second}";
                     require_once 'CRM/Contact/BAO/RelationshipType.php';
                     $relationshipType = new CRM_Contact_BAO_RelationshipType();
                     $relationshipType->id = $id;
                     if ($relationshipType->find(true)) {
                         if (isset($relationshipType->{$direction})) {
                             $params[$key]['contact_sub_type'] = $relationshipType->{$direction};
                         }
                     }
                     $relationshipType->free();
                 }
                 self::isErrorInCustomData($params[$key], $errorMessage);
             }
         }
     }
 }
Exemplo n.º 12
0
 /**
  *  function to check if an error in custom data
  *  
  *  @param String   $errorMessage   A string containing all the error-fields.
  *  
  *  @access public 
  */
 function isErrorInCustomData($params, &$errorMessage)
 {
     $customFields = CRM_Core_BAO_CustomField::getFields($params['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');
                 CRM_Import_Parser_Contact::addToErrorMsg('field ID', $errorMessage);
             }
             /* validate the data against the CF type */
             //CRM_Core_Error::debug( $value, $customFields[$customFieldID] );
             if ($value) {
                 // need not check for label filed import
                 $htmlType = array('CheckBox', 'Multi-Select', 'Select', 'Radio');
                 if (!in_array($customFields[$customFieldID][3], $htmlType) || $customFields[$customFieldID][2] == 'Boolean') {
                     $valid = CRM_Core_BAO_CustomValue::typecheck($customFields[$customFieldID][2], $value);
                     if (!$valid) {
                         //return _crm_error('Invalid value for custom field :' .$customFields[$customFieldID][0]);
                         CRM_Import_Parser_Contact::addToErrorMsg($customFields[$customFieldID][0], $errorMessage);
                     }
                 }
                 // check for values for custom fields for checkboxes and multiselect
                 if ($customFields[$customFieldID][3] == 'CheckBox' || $customFields[$customFieldID][3] == 'Multi-Select') {
                     $value = str_replace("|", ",", $value);
                     $mulValues = explode(',', $value);
                     $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true);
                     foreach ($mulValues as $v1) {
                         $flag = false;
                         foreach ($customOption as $v2) {
                             if (strtolower(trim($v2['label'])) == strtolower(trim($v1))) {
                                 $flag = true;
                             }
                         }
                         if (!$flag) {
                             //return _crm_error('Invalid value for custom field :' .$customFields[$customFieldID][0]);
                             CRM_Import_Parser_Contact::addToErrorMsg($customFields[$customFieldID][0], $errorMessage);
                         }
                     }
                 } else {
                     if ($customFields[$customFieldID][3] == 'Select' || $customFields[$customFieldID][3] == 'Radio' && $customFields[$customFieldID][2] != 'Boolean') {
                         $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true);
                         $flag = false;
                         foreach ($customOption as $v2) {
                             if (strtolower(trim($v2['label'])) == strtolower(trim($value))) {
                                 $flag = true;
                             }
                         }
                         if (!$flag) {
                             //return _crm_error('Invalid value for custom field :' .$customFields[$customFieldID][0]);
                             CRM_Import_Parser_Contact::addToErrorMsg($customFields[$customFieldID][0], $errorMessage);
                         }
                     }
                 }
             }
         }
     }
     //return true;
 }
Exemplo n.º 13
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;
}
Exemplo n.º 14
0
 /**
  * Set default values for custom data used in profile.
  *
  * @param int $customFieldId
  *   Custom field id.
  * @param string $elementName
  *   Custom field name.
  * @param array $defaults
  *   Associated array of fields.
  * @param int $contactId
  *   Contact id.
  * @param int $mode
  *   Profile mode.
  * @param mixed $value
  *   If passed - dont fetch value from db,.
  *                               just format the given value
  *
  */
 public static function setProfileDefaults($customFieldId, $elementName, &$defaults, $contactId = NULL, $mode = NULL, $value = NULL)
 {
     //get the type of custom field
     $customField = new CRM_Core_BAO_CustomField();
     $customField->id = $customFieldId;
     $customField->find(TRUE);
     if (!$contactId) {
         if ($mode == CRM_Profile_Form::MODE_CREATE) {
             $value = $customField->default_value;
         }
     } else {
         if (!isset($value)) {
             $info = self::getTableColumnGroup($customFieldId);
             $query = "SELECT {$info[0]}.{$info[1]} as value FROM {$info[0]} WHERE {$info[0]}.entity_id = {$contactId}";
             $result = CRM_Core_DAO::executeQuery($query);
             if ($result->fetch()) {
                 $value = $result->value;
             }
         }
         if ($customField->data_type == 'Country') {
             if (!$value) {
                 $config = CRM_Core_Config::singleton();
                 if ($config->defaultContactCountry) {
                     $value = $config->defaultContactCountry();
                 }
             }
         }
     }
     //set defaults if mode is registration
     if (!trim($value) && $value !== 0 && !in_array($mode, array(CRM_Profile_Form::MODE_EDIT, CRM_Profile_Form::MODE_SEARCH))) {
         $value = $customField->default_value;
     }
     if ($customField->data_type == 'Money' && isset($value)) {
         $value = number_format($value, 2);
     }
     switch ($customField->html_type) {
         case 'CheckBox':
         case 'AdvMulti-Select':
         case 'Multi-Select':
             $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldId, FALSE);
             $defaults[$elementName] = array();
             $checkedValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($value, 1, -1));
             foreach ($customOption as $val) {
                 if (in_array($val['value'], $checkedValue)) {
                     if ($customField->html_type == 'CheckBox') {
                         $defaults[$elementName][$val['value']] = 1;
                     } elseif ($customField->html_type == 'Multi-Select' || $customField->html_type == 'AdvMulti-Select') {
                         $defaults[$elementName][$val['value']] = $val['value'];
                     }
                 }
             }
             break;
         case 'Select Date':
             if ($value) {
                 list($defaults[$elementName], $defaults[$elementName . '_time']) = CRM_Utils_Date::setDateDefaults($value, NULL, $customField->date_format, $customField->time_format);
             }
             break;
         case 'Autocomplete-Select':
             if ($customField->data_type == 'ContactReference') {
                 if (is_numeric($value)) {
                     $defaults[$elementName . '_id'] = $value;
                     $defaults[$elementName] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $value, 'sort_name');
                 }
             } else {
                 $defaults[$elementName] = $value;
             }
             break;
         default:
             $defaults[$elementName] = $value;
     }
 }
Exemplo n.º 15
0
 /**
  * Update saved search for multi-select custom fields on DB upgrade
  *
  * @param CRM_Queue_TaskContext $ctx
  *
  * @return bool TRUE for success
  */
 public static function updateSavedSearch(CRM_Queue_TaskContext $ctx)
 {
     $sql = "SELECT id, form_values FROM civicrm_saved_search";
     $dao = CRM_Core_DAO::executeQuery($sql);
     while ($dao->fetch()) {
         $copy = $formValues = unserialize($dao->form_values);
         $update = FALSE;
         foreach ($copy as $field => $data_value) {
             if (preg_match('/^custom_/', $field) && is_array($data_value) && !array_key_exists("{$field}_operator", $formValues)) {
                 // Now check for CiviCRM_OP_OR as either key or value in the data_value array.
                 // This is the conclusive evidence of an old-style data format.
                 if (array_key_exists('CiviCRM_OP_OR', $data_value) || FALSE !== array_search('CiviCRM_OP_OR', $data_value)) {
                     // We have old style data. Mark this record to be updated.
                     $update = TRUE;
                     $op = 'and';
                     if (!preg_match('/^custom_([0-9]+)/', $field, $matches)) {
                         // fatal error?
                         continue;
                     }
                     $fieldID = $matches[1];
                     if (array_key_exists('CiviCRM_OP_OR', $data_value)) {
                         // This indicates data structure identified by jamie in the form:
                         // value1 => 1, value2 => , value3 => 1.
                         $data_value = array_keys($data_value, 1);
                         // If CiviCRM_OP_OR - change OP from default to OR
                         if ($data_value['CiviCRM_OP_OR'] == 1) {
                             $op = 'or';
                         }
                         unset($data_value['CiviCRM_OP_OR']);
                     } else {
                         // The value is here, but it is not set as a key.
                         // This is using the style identified by Monish - the existence of the value
                         // indicates an OR search and values are set in the form of:
                         // 0 => value1, 1 => value1, 3 => value2.
                         $key = array_search('CiviCRM_OP_OR', $data_value);
                         $op = 'or';
                         unset($data_value[$key]);
                     }
                     //If only Or operator has been chosen, means we need to select all values and
                     //so to execute OR operation between these values according to new data structure
                     if (count($data_value) == 0 && $op == 'or') {
                         $customOption = CRM_Core_BAO_CustomOption::getCustomOption($fieldID);
                         foreach ($customOption as $option) {
                             $data_value[] = CRM_Utils_Array::value('value', $option);
                         }
                     }
                     $formValues[$field] = $data_value;
                     $formValues["{$field}_operator"] = $op;
                 }
             }
         }
         if ($update) {
             $sql = "UPDATE civicrm_saved_search SET form_values = %0 WHERE id = %1";
             CRM_Core_DAO::executeQuery($sql, array(array(serialize($formValues), 'String'), array($dao->id, 'Integer')));
         }
     }
     return TRUE;
 }
Exemplo n.º 16
0
 /**
  * Function to extract the get params from the url, validate
  * and store it in session
  *
  * @param CRM_Core_Form $form the form object
  * @param string        $type the type of custom group we are using
  * @return void
  * @access public
  * @static
  */
 function extractGetParams(&$form, $type)
 {
     // if not GET params return
     if (empty($_GET)) {
         return;
     }
     $groupTree =& CRM_Core_BAO_CustomGroup::getTree($type);
     $customFields =& CRM_Core_BAO_CustomField::getFields($type);
     $customValue = array();
     $htmlType = array('CheckBox', 'Multi-Select', 'Select', 'Radio');
     foreach ($groupTree as $group) {
         foreach ($group['fields'] as $key => $field) {
             $fieldName = 'custom_' . $key;
             $value = CRM_Utils_Request::retrieve($fieldName, $form);
             if ($value) {
                 if (!in_array($customFields[$key][3], $htmlType) || $customFields[$key][2] == 'Boolean') {
                     $valid = CRM_Core_BAO_CustomValue::typecheck($customFields[$key][2], $value);
                 }
                 if ($customFields[$key][3] == 'CheckBox' || $customFields[$key][3] == 'Multi-Select') {
                     $value = str_replace("|", ",", $value);
                     $mulValues = explode(',', $value);
                     $customOption = CRM_Core_BAO_CustomOption::getCustomOption($key, true);
                     $val = array();
                     foreach ($mulValues as $v1) {
                         foreach ($customOption as $v2) {
                             if (strtolower(trim($v2['label'])) == strtolower(trim($v1))) {
                                 $val[$v2['value']] = 1;
                             }
                         }
                     }
                     if (!empty($val)) {
                         $value = $val;
                         $valid = true;
                     } else {
                         $value = null;
                     }
                 } else {
                     if ($customFields[$key][3] == 'Select' || $customFields[$key][3] == 'Radio' && $customFields[$key][2] != 'Boolean') {
                         $customOption = CRM_Core_BAO_CustomOption::getCustomOption($key, true);
                         foreach ($customOption as $v2) {
                             if (strtolower(trim($v2['label'])) == strtolower(trim($value))) {
                                 $value = $v2['value'];
                                 $valid = true;
                             }
                         }
                     } else {
                         if ($customFields[$key][2] == 'Date') {
                             require_once 'CRM/Utils/Date.php';
                             if (is_numeric($value)) {
                                 $value = CRM_Utils_Date::unformat($value, null);
                             } else {
                                 $value = CRM_Utils_Date::unformat($value, $separator = '-');
                             }
                             $valid = true;
                         }
                     }
                 }
                 if ($valid) {
                     $customValue[$fieldName] = $value;
                 }
             }
         }
     }
     $form->set('customGetValues', $customValue);
     $form->set('groupTree', $groupTree);
 }
Exemplo n.º 17
0
 /**
  * Browse the listing.
  *
  * @return void
  */
 public function browse()
 {
     $dateFields = NULL;
     $cgcount = 0;
     $attributes = array();
     $dateFieldsVals = NULL;
     if ($this->_pageViewType == 'profileDataView' && $this->_profileId) {
         $fields = CRM_Core_BAO_UFGroup::getFields($this->_profileId, FALSE, NULL, NULL, NULL, FALSE, NULL, FALSE, NULL, CRM_Core_Permission::EDIT);
         $multiRecordFields = array();
         $fieldIDs = NULL;
         $result = NULL;
         $multiRecordFieldsWithSummaryListing = CRM_Core_BAO_UFGroup::shiftMultiRecordFields($fields, $multiRecordFields, TRUE);
         $multiFieldId = CRM_Core_BAO_CustomField::getKeyID(key($multiRecordFields));
         $customGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $multiFieldId, 'custom_group_id');
         $reached = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupId, $this->_contactId);
         if (!$reached) {
             $this->assign('contactId', $this->_contactId);
             $this->assign('gid', $this->_profileId);
         }
         $this->assign('reachedMax', $reached);
         if ($multiRecordFieldsWithSummaryListing && !empty($multiRecordFieldsWithSummaryListing)) {
             $fieldIDs = array_keys($multiRecordFieldsWithSummaryListing);
         }
     } elseif ($this->_pageViewType == 'customDataView') {
         // require custom group id for _pageViewType of customDataView
         $customGroupId = $this->_customGroupId;
         $reached = CRM_Core_BAO_CustomGroup::hasReachedMaxLimit($customGroupId, $this->_contactId);
         if (!$reached) {
             $this->assign('contactId', $this->_contactId);
             $this->assign('customGroupId', $customGroupId);
             $this->assign('ctype', $this->_contactType);
         }
         $this->assign('reachedMax', $reached);
         // custom group info : this consists of the field title of group fields
         $groupDetail = CRM_Core_BAO_CustomGroup::getGroupDetail($customGroupId, NULL, CRM_Core_DAO::$_nullObject, TRUE);
         // field ids of fields in_selector for the custom group id provided
         $fieldIDs = array_keys($groupDetail[$customGroupId]['fields']);
         // field labels for headers
         $fieldLabels = $groupDetail[$customGroupId]['fields'];
         // from the above customGroupInfo we can get $this->_customGroupTitle
         $this->_customGroupTitle = $groupDetail[$customGroupId]['title'];
     }
     if ($fieldIDs && !empty($fieldIDs) && $this->_contactId) {
         $options = array();
         $returnProperities = array('html_type', 'data_type', 'date_format', 'time_format', 'default_value', 'is_required');
         foreach ($fieldIDs as $key => $fieldID) {
             $fieldIDs[$key] = !is_numeric($fieldID) ? CRM_Core_BAO_CustomField::getKeyID($fieldID) : $fieldID;
             $param = array('id' => $fieldIDs[$key]);
             $returnValues = array();
             CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_CustomField', $param, $returnValues, $returnProperities);
             if ($returnValues['data_type'] == 'Date') {
                 $dateFields[$fieldIDs[$key]] = 1;
                 $actualPHPFormats = CRM_Core_SelectValues::datePluginToPHPFormats();
                 $dateFormat = (array) CRM_Utils_Array::value($returnValues['date_format'], $actualPHPFormats);
                 $timeFormat = CRM_Utils_Array::value('time_format', $returnValues);
             }
             $optionValuePairs = CRM_Core_BAO_CustomOption::getCustomOption($fieldIDs[$key]);
             if (!empty($optionValuePairs)) {
                 foreach ($optionValuePairs as $optionPairs) {
                     $options[$fieldIDs[$key]][$optionPairs['value']] = $optionPairs['label'];
                 }
             }
             $options[$fieldIDs[$key]]['attributes']['html_type'] = $returnValues['html_type'];
             $options[$fieldIDs[$key]]['attributes']['data_type'] = $returnValues['data_type'];
             $options[$fieldIDs[$key]]['attributes']['is_required'] = !empty($returnValues['is_required']);
             $options[$fieldIDs[$key]]['attributes']['default_value'] = CRM_Utils_Array::value('default_value', $returnValues);
             $options[$fieldIDs[$key]]['attributes']['format'] = $options[$fieldIDs[$key]]['attributes']['date_format'] = CRM_Utils_Array::value('date_format', $returnValues);
             $options[$fieldIDs[$key]]['attributes']['time_format'] = CRM_Utils_Array::value('time_format', $returnValues);
         }
         // commonly used for both views i.e profile listing view (profileDataView) and custom data listing view (customDataView)
         $result = CRM_Core_BAO_CustomValueTable::getEntityValues($this->_contactId, NULL, $fieldIDs, TRUE);
         if ($this->_pageViewType == 'profileDataView') {
             if (!empty($fieldIDs)) {
                 //get the group info of multi rec fields in listing view
                 $fieldInput = $fieldIDs;
                 $fieldIdInput = $fieldIDs[0];
             } else {
                 //if no listing fields exist, take the group title for display
                 $nonListingFieldIds = array_keys($multiRecordFields);
                 $singleField = CRM_Core_BAO_CustomField::getKeyID($nonListingFieldIds[0]);
                 $fieldIdInput = $singleField;
                 $singleField = array($singleField);
                 $fieldInput = $singleField;
             }
             $customGroupInfo = CRM_Core_BAO_CustomGroup::getGroupTitles($fieldInput);
             $this->_customGroupTitle = $customGroupInfo[$fieldIdInput]['groupTitle'];
         }
         // $cgcount is defined before 'if' condition as enitiy may have no record
         // and $cgcount is used to build new record url
         $cgcount = 1;
         if ($result && !empty($result)) {
             $links = self::links();
             if ($this->_pageViewType == 'profileDataView') {
                 $pageCheckSum = $this->get('pageCheckSum');
                 if ($pageCheckSum) {
                     foreach ($links as $key => $link) {
                         $links[$key] = $link['qs'] . "&cs=%%cs%%";
                     }
                 }
             }
             $linkAction = array_sum(array_keys($this->links()));
             if ($reached) {
                 unset($links[CRM_Core_Action::COPY]);
             }
             $newCgCount = !$reached ? count($result) + 1 : NULL;
             foreach ($result as $recId => &$value) {
                 foreach ($value as $fieldId => &$val) {
                     if (is_numeric($fieldId)) {
                         $customValue =& $val;
                         if (!empty($dateFields) && array_key_exists($fieldId, $dateFields)) {
                             // formated date capture value capture
                             $dateFieldsVals[$fieldId][$recId] = CRM_Core_BAO_CustomField::getDisplayValue($customValue, $fieldId, $options);
                             //set date and time format
                             switch ($timeFormat) {
                                 case 1:
                                     $dateFormat[1] = 'g:iA';
                                     break;
                                 case 2:
                                     $dateFormat[1] = 'G:i';
                                     break;
                                 default:
                                     // if time is not selected remove time from value
                                     $result[$recId][$fieldId] = substr($result[$recId][$fieldId], 0, 10);
                             }
                             $result[$recId][$fieldId] = CRM_Utils_Date::processDate($result[$recId][$fieldId], NULL, FALSE, implode(" ", $dateFormat));
                         } else {
                             // assign to $result
                             $customValue = CRM_Core_BAO_CustomField::getDisplayValue($customValue, $fieldId, $options);
                         }
                         // FIXME: getDisplayValue should always return a string so why is this necessary?
                         if (!$customValue && $customValue !== '0') {
                             $customValue = "";
                         }
                         // Set field attributes to support crmEditable
                         // Note that $fieldAttributes[data-type] actually refers to the html type not the sql data type
                         // TODO: Not all widget types and validation rules are supported by crmEditable so some fields will not be in-place editable
                         $fieldAttributes = array('class' => "crmf-custom_{$fieldId}_{$recId}");
                         $editable = FALSE;
                         if ($linkAction & CRM_Core_Action::UPDATE) {
                             $spec = $options[$fieldId]['attributes'];
                             switch ($spec['html_type']) {
                                 case 'Text':
                                     // Other data types like money would require some extra validation
                                     // FIXME: crmEditable currently does not support any validation rules :(
                                     $supportedDataTypes = array('Float', 'String', 'Int');
                                     $editable = in_array($spec['data_type'], $supportedDataTypes);
                                     break;
                                 case 'TextArea':
                                     $editable = TRUE;
                                     $fieldAttributes['data-type'] = 'textarea';
                                     break;
                                 case 'Radio':
                                 case 'Select':
                                 case 'Select Country':
                                 case 'Select State/Province':
                                     $editable = TRUE;
                                     $fieldAttributes['data-type'] = $spec['data_type'] == 'Boolean' ? 'boolean' : 'select';
                                     if (!$spec['is_required']) {
                                         $fieldAttributes['data-empty-option'] = ts('- none -');
                                     }
                                     break;
                             }
                         }
                         if ($editable) {
                             $fieldAttributes['class'] .= ' crm-editable';
                         }
                         $attributes[$fieldId][$recId] = $fieldAttributes;
                         $op = NULL;
                         if ($this->_pageViewType == 'profileDataView') {
                             $actionParams = array('recordId' => $recId, 'gid' => $this->_profileId, 'id' => $this->_contactId);
                             $op = 'profile.multiValue.row';
                         } else {
                             // different set of url params
                             $actionParams['gid'] = $actionParams['groupID'] = $this->_customGroupId;
                             $actionParams['cid'] = $actionParams['entityID'] = $this->_contactId;
                             $actionParams['recId'] = $recId;
                             $actionParams['type'] = $this->_contactType;
                             $actionParams['cgcount'] = $cgcount;
                             $actionParams['newCgCount'] = $newCgCount;
                             // DELETE action links
                             $deleteData = array('valueID' => $recId, 'groupID' => $this->_customGroupId, 'contactId' => $this->_contactId, 'key' => CRM_Core_Key::get('civicrm/ajax/customvalue'));
                             $links[CRM_Core_Action::DELETE]['url'] = '#';
                             $links[CRM_Core_Action::DELETE]['extra'] = ' data-delete_params="' . htmlspecialchars(json_encode($deleteData)) . '"';
                             $links[CRM_Core_Action::DELETE]['class'] = 'delete-custom-row';
                         }
                         if (!empty($pageCheckSum)) {
                             $actionParams['cs'] = $pageCheckSum;
                         }
                         $value['action'] = CRM_Core_Action::formLink($links, $linkAction, $actionParams, ts('more'), FALSE, $op, 'customValue', $fieldId);
                     }
                 }
                 $cgcount++;
             }
         }
     }
     $headers = array();
     if (!empty($fieldIDs)) {
         foreach ($fieldIDs as $fieldID) {
             $headers[$fieldID] = $this->_pageViewType == 'profileDataView' ? $customGroupInfo[$fieldID]['fieldLabel'] : $fieldLabels[$fieldID]['label'];
         }
     }
     $this->assign('dateFields', $dateFields);
     $this->assign('dateFieldsVals', $dateFieldsVals);
     $this->assign('cgcount', $cgcount);
     $this->assign('customGroupTitle', $this->_customGroupTitle);
     $this->assign('headers', $headers);
     $this->assign('records', $result);
     $this->assign('attributes', $attributes);
 }
Exemplo n.º 18
0
 /**
  * Function to set default values for custom data used in profile
  *
  * @params int    $customFieldId custom field id
  * @params string $elementName   custom field name
  * @params array  $defaults      associated array of fields
  * @params int    $contactId     contact id
  * @param  int    $mode          profile mode
  * 
  * @static
  * @access public
  */
 static function setProfileDefaults($customFieldId, $elementName, &$defaults, $contactId = null, $mode = null)
 {
     //get the type of custom field
     $customField =& new CRM_Core_BAO_CustomField();
     $customField->id = $customFieldId;
     $customField->find(true);
     require_once "CRM/Profile/Form.php";
     $value = null;
     if (!$contactId) {
         if ($mode == CRM_Profile_Form::MODE_CREATE) {
             $value = $customField->default_value;
         }
     } else {
         $info = self::getTableColumnGroup($customFieldId);
         $query = "SELECT {$info[0]}.{$info[1]} as value FROM {$info[0]} WHERE {$info[0]}.entity_id = {$contactId}";
         $result = CRM_Core_DAO::executeQuery($query);
         if ($result->fetch()) {
             $value = $result->value;
         }
         if ($customField->data_type == 'Country') {
             if (!$value) {
                 $config =& CRM_Core_Config::singleton();
                 if ($config->defaultContactCountry) {
                     $value = $config->defaultContactCountry();
                 }
             }
         }
     }
     //set defaults if mode is registration / edit
     if (!trim($value) && $value !== 0 && $mode != CRM_Profile_Form::MODE_SEARCH) {
         $value = $customField->default_value;
     }
     if ($customField->data_type == 'Money' && isset($value)) {
         $value = number_format($value, 2);
     }
     switch ($customField->html_type) {
         case 'CheckBox':
         case 'AdvMulti-Select':
         case 'Multi-Select':
             $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldId, false);
             $defaults[$elementName] = array();
             $checkedValue = explode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, substr($value, 1, -1));
             foreach ($customOption as $val) {
                 if (in_array($val['value'], $checkedValue)) {
                     if ($customField->html_type == 'CheckBox') {
                         $defaults[$elementName][$val['value']] = 1;
                     } elseif ($customField->html_type == 'Multi-Select' || $customField->html_type == 'AdvMulti-Select') {
                         $defaults[$elementName][$val['value']] = $val['value'];
                     }
                 }
             }
             break;
         case 'Select Date':
             if ($value) {
                 list($defaults[$elementName], $defaults[$elementName . '_time']) = CRM_Utils_Date::setDateDefaults($value, null, $customField->date_format, $customField->time_format);
             }
             break;
         default:
             $defaults[$elementName] = $value;
     }
 }
Exemplo n.º 19
0
 /**
  * Function to set default values for custom data used in profile
  *
  * @params int    $customFieldId custom field id
  * @params string $elementName   custom field name
  * @params array  $defaults      associated array of fields
  * @params int    $contactId     contact id
  * @param  int    $mode          profile mode
  * 
  * @static
  * @access public
  */
 function setProfileDefaults($customFieldId, $elementName, &$defaults, $contactId = null, $mode = null)
 {
     //get the type of custom field
     $customField =& new CRM_Core_BAO_CustomField();
     $customField->id = $customFieldId;
     $customField->find(true);
     require_once "CRM/Profile/Form.php";
     if (!$contactId) {
         if ($mode == CRM_PROFILE_FORM_MODE_CREATE) {
             $value = $customField->default_value;
         }
     } else {
         // make sure the custom value exists
         $cv =& new CRM_Core_BAO_CustomValue();
         $cv->custom_field_id = $customFieldId;
         $cv->entity_table = 'civicrm_contact';
         $cv->entity_id = $contactId;
         if ($cv->find(true)) {
             switch ($customField->data_type) {
                 case 'String':
                     $value = $cv->char_data;
                     break;
                 case 'Int':
                 case 'Boolean':
                 case 'StateProvince':
                 case 'Country':
                     $value = $cv->int_data;
                     break;
                 case 'Float':
                     $value = $cv->int_float;
                     break;
                 case 'Money':
                     $value = $cv->decimal_data;
                     break;
                 case 'Memo':
                     $value = $cv->memo_data;
                     break;
                 case 'Date':
                     $value = $cv->date_data;
                     break;
             }
         }
     }
     //set defaults if mode is registration / edit
     if (!trim($value) && $mode != CRM_PROFILE_FORM_MODE_SEARCH) {
         $value = $customField->default_value;
     }
     switch ($customField->html_type) {
         case 'CheckBox':
             $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldId, $inactiveNeeded);
             $defaults[$elementName] = array();
             $checkedValue = explode(CRM_CORE_BAO_CUSTOMOPTION_VALUE_SEPERATOR, $value);
             foreach ($customOption as $val) {
                 if (in_array($val['value'], $checkedValue)) {
                     $defaults[$elementName][$val['value']] = 1;
                 } else {
                     $defaults[$elementName][$val['value']] = 0;
                 }
             }
             break;
             //added a case for Multi-Select option
         //added a case for Multi-Select option
         case 'Multi-Select':
             $customOption = CRM_Core_BAO_CustomOption::getCustomOption($field['id'], $inactiveNeeded);
             $defaults[$elementName] = array();
             $checkedValue = explode(CRM_CORE_BAO_CUSTOMOPTION_VALUE_SEPERATOR, $value);
             foreach ($customOption as $val) {
                 if (in_array($val['value'], $checkedValue)) {
                     $defaults[$elementName][$val['value']] = $val['value'];
                 }
             }
             break;
         default:
             $defaults[$elementName] = $value;
     }
 }
Exemplo n.º 20
0
 /**
  * @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;
 }
Exemplo n.º 21
0
function _civicrm_add_custom_formatted_param($customFieldID, $key, $field, &$formatted, $type)
{
    require_once 'CRM/Core/BAO/CustomOption.php';
    require_once 'CRM/Core/PseudoConstant.php';
    if (empty($type)) {
        return;
    }
    switch ($type) {
        case 'Text':
            $formatted[$key] = $field;
            break;
        case 'CheckBox':
        case 'AdvMulti-Select':
        case 'Multi-Select':
            $mulValues = explode(',', $field);
            $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true);
            $formatted[$key] = array();
            foreach ($mulValues as $v1) {
                foreach ($customOption as $v2) {
                    if (strtolower($v2['label']) == strtolower(trim($v1)) || strtolower($v2['value']) == strtolower(trim($v1))) {
                        if ($type == 'CheckBox') {
                            $formatted[$key][$v2['value']] = 1;
                        } else {
                            $formatted[$key][] = $v2['value'];
                        }
                    }
                }
            }
            break;
        case 'Select':
        case 'Radio':
            $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true);
            foreach ($customOption as $v2) {
                if (strtolower($v2['label']) == strtolower(trim($field)) || strtolower($v2['value']) == strtolower(trim($field))) {
                    $formatted[$key] = $v2['value'];
                }
            }
            break;
        case 'Multi-Select State/Province':
            $mulValues = explode(',', $field);
            $stateAbbr = CRM_Core_PseudoConstant::stateProvinceAbbreviation();
            $stateName = CRM_Core_PseudoConstant::stateProvince();
            $formatted[$key] = $stateValues = array();
            foreach ($mulValues as $values) {
                if ($val = CRM_Utils_Array::key($values, $stateAbbr)) {
                    $formatted[$key][] = $val;
                } else {
                    if ($val = CRM_Utils_Array::key($values, $stateName)) {
                        $formatted[$key][] = $val;
                    }
                }
            }
            break;
        case 'Multi-Select Country':
            $config = CRM_Core_Config::singleton();
            $limitCodes = $config->countryLimit();
            $mulValues = explode(',', $field);
            $formatted[$key] = array();
            CRM_Core_PseudoConstant::populate($countryNames, 'CRM_Core_DAO_Country', true, 'name', 'is_active');
            CRM_Core_PseudoConstant::populate($countryIsoCodes, 'CRM_Core_DAO_Country', true, 'iso_code');
            foreach ($mulValues as $values) {
                if ($val = CRM_Utils_Array::key($values, $countryNames)) {
                    $formatted[$key][] = $val;
                } else {
                    if ($val = CRM_Utils_Array::key($values, $countryIsoCodes)) {
                        $formatted[$key][] = $val;
                    } else {
                        if ($val = CRM_Utils_Array::key($values, $limitCodes)) {
                            $formatted[$key][] = $val;
                        }
                    }
                }
            }
            break;
    }
}