Exemplo n.º 1
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);
             }
         }
     }
 }
Exemplo n.º 2
0
 /**
  * Function to set profile defaults
  *
  * @params int     $contactId      contact id
  * @params array   $fields         associative array of fields
  * @params array   $defaults       defaults array
  * @params boolean $singleProfile  true for single profile else false(batch update)
  * @params int     $componentId    id for specific components like contribute, event etc
  *
  * @return null
  * @static
  * @access public
  */
 static function setProfileDefaults($contactId, &$fields, &$defaults, $singleProfile = true, $componentId = null, $component = null)
 {
     if (!$componentId) {
         //get the contact details
         require_once 'CRM/Contact/BAO/Contact.php';
         list($contactDetails, $options) = CRM_Contact_BAO_Contact::getHierContactDetails($contactId, $fields);
         $details = $contactDetails[$contactId];
         require_once 'CRM/Contact/Form/Edit/TagsAndGroups.php';
         //start of code to set the default values
         foreach ($fields as $name => $field) {
             //set the field name depending upon the profile mode(single/batch)
             if ($singleProfile) {
                 $fldName = $name;
             } else {
                 $fldName = "field[{$contactId}][{$name}]";
             }
             if ($name == 'group') {
                 CRM_Contact_Form_Edit_TagsAndGroups::setDefaults($contactId, $defaults, CRM_Contact_Form_Edit_TagsAndGroups::GROUP, $fldName);
             }
             if ($name == 'tag') {
                 CRM_Contact_Form_Edit_TagsAndGroups::setDefaults($contactId, $defaults, CRM_Contact_Form_Edit_TagsAndGroups::TAG, $fldName);
             }
             if (CRM_Utils_Array::value($name, $details) || isset($details[$name])) {
                 //to handle custom data (checkbox) to be written
                 // to handle gender / suffix / prefix / greeting_type
                 if ($name == 'gender') {
                     $defaults[$fldName] = $details['gender_id'];
                 } else {
                     if ($name == 'individual_prefix') {
                         $defaults[$fldName] = $details['individual_prefix_id'];
                     } else {
                         if ($name == 'individual_suffix') {
                             $defaults[$fldName] = $details['individual_suffix_id'];
                         } else {
                             if ($name == 'birth_date' || $name == 'deceased_date') {
                                 list($defaults[$fldName]) = CRM_Utils_Date::setDateDefaults($details[$name], 'birth');
                             } else {
                                 if ($name == 'email_greeting') {
                                     $defaults[$fldName] = $details['email_greeting_id'];
                                     $defaults['email_greeting_custom'] = $details['email_greeting_custom'];
                                 } else {
                                     if ($name == 'postal_greeting') {
                                         $defaults[$fldName] = $details['postal_greeting_id'];
                                         $defaults['postal_greeting_custom'] = $details['postal_greeting_custom'];
                                     } else {
                                         if ($name == 'addressee') {
                                             $defaults[$fldName] = $details['addressee_id'];
                                             $defaults['addressee_custom'] = $details['addressee_custom'];
                                         } else {
                                             if ($name == 'preferred_communication_method') {
                                                 $v = explode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, $details[$name]);
                                                 foreach ($v as $item) {
                                                     if ($item) {
                                                         $defaults[$fldName . "[{$item}]"] = 1;
                                                     }
                                                 }
                                             } else {
                                                 if ($name == 'world_region') {
                                                     $defaults[$fldName] = $details['worldregion_id'];
                                                 } else {
                                                     if ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($name)) {
                                                         //fix for custom fields
                                                         $customFields = CRM_Core_BAO_CustomField::getFields(CRM_Utils_Array::value('Individual', $values));
                                                         // hack to add custom data for components
                                                         $components = array("Contribution", "Participant", "Membership");
                                                         foreach ($components as $value) {
                                                             $customFields = CRM_Utils_Array::crmArrayMerge($customFields, CRM_Core_BAO_CustomField::getFieldsForImport($value));
                                                         }
                                                         switch ($customFields[$customFieldId]['html_type']) {
                                                             case 'Multi-Select State/Province':
                                                             case 'Multi-Select Country':
                                                             case 'AdvMulti-Select':
                                                             case 'Multi-Select':
                                                                 $v = explode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, $details[$name]);
                                                                 foreach ($v as $item) {
                                                                     if ($item) {
                                                                         $defaults[$fldName][$item] = $item;
                                                                     }
                                                                 }
                                                                 break;
                                                             case 'CheckBox':
                                                                 $v = explode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, $details[$name]);
                                                                 foreach ($v as $item) {
                                                                     if ($item) {
                                                                         $defaults[$fldName][$item] = 1;
                                                                         // seems like we need this for QF style checkboxes in profile where its multiindexed
                                                                         // CRM-2969
                                                                         $defaults["{$fldName}[{$item}]"] = 1;
                                                                     }
                                                                 }
                                                                 break;
                                                             case 'Autocomplete-Select':
                                                                 if ($customFields[$customFieldId]['data_type'] == "ContactReference") {
                                                                     require_once 'CRM/Contact/BAO/Contact.php';
                                                                     if (is_numeric($details[$name])) {
                                                                         $defaults[$fldName . '_id'] = $details[$name];
                                                                         $defaults[$fldName] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $details[$name], 'sort_name');
                                                                     }
                                                                 } else {
                                                                     $label = CRM_Core_BAO_CustomOption::getOptionLabel($customFieldId, $details[$name]);
                                                                     $defaults[$fldName . '_id'] = $details[$name];
                                                                     $defaults[$fldName] = $label;
                                                                 }
                                                                 break;
                                                             case 'Select Date':
                                                                 list($defaults[$fldName], $defaults[substr($fldName, 0, -1) . '_time]']) = CRM_Utils_Date::setDateDefaults($details[$name]);
                                                                 break;
                                                             default:
                                                                 $defaults[$fldName] = $details[$name];
                                                                 break;
                                                         }
                                                     } else {
                                                         $defaults[$fldName] = $details[$name];
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             } else {
                 list($fieldName, $locTypeId, $phoneTypeId) = CRM_Utils_System::explode('-', $name, 3);
                 if (is_array($details)) {
                     foreach ($details as $key => $value) {
                         // when we fixed CRM-5319 - get primary loc
                         // type as per loc field and removed below code.
                         if ($locTypeId == 'Primary') {
                             $locTypeId = CRM_Contact_BAO_Contact::getPrimaryLocationType($contactId);
                         }
                         if (is_numeric($locTypeId)) {
                             //fixed for CRM-665
                             if ($locTypeId == CRM_Utils_Array::value('location_type_id', $value)) {
                                 if (CRM_Utils_Array::value($fieldName, $value)) {
                                     //to handle stateprovince and country
                                     if ($fieldName == 'state_province') {
                                         $defaults[$fldName] = $value['state_province_id'];
                                     } else {
                                         if ($fieldName == 'county') {
                                             $defaults[$fldName] = $value['county_id'];
                                         } else {
                                             if ($fieldName == 'country') {
                                                 $defaults[$fldName] = $value['country_id'];
                                                 if (!isset($value['country_id']) || !$value['country_id']) {
                                                     $config =& CRM_Core_Config::singleton();
                                                     if ($config->defaultContactCountry) {
                                                         $defaults[$fldName] = $config->defaultContactCountry;
                                                     }
                                                 }
                                             } else {
                                                 if ($fieldName == 'phone') {
                                                     if ($phoneTypeId) {
                                                         if ($value['phone'][$phoneTypeId]) {
                                                             $defaults[$fldName] = $value['phone'][$phoneTypeId];
                                                         }
                                                     } else {
                                                         $defaults[$fldName] = $value['phone'];
                                                     }
                                                 } else {
                                                     if ($fieldName == 'email') {
                                                         //adding the first email (currently we don't support multiple emails of same location type)
                                                         $defaults[$fldName] = $value['email'];
                                                     } else {
                                                         if ($fieldName == 'im') {
                                                             //adding the first im (currently we don't support multiple ims of same location type)
                                                             $defaults[$fldName] = $value['im'];
                                                             $defaults[$fldName . "-provider_id"] = $value['im_provider_id'];
                                                         } else {
                                                             $defaults[$fldName] = $value[$fieldName];
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         if (CRM_Core_Permission::access('Quest', false)) {
             require_once 'CRM/Quest/BAO/Student.php';
             // Checking whether the database contains quest_student table.
             // Now there are two different schemas for core and quest.
             // So if only core schema in use then withought following check gets the DB error.
             $student = new CRM_Quest_BAO_Student();
             $tableStudent = $student->getTableName();
             if ($tableStudent) {
                 //set student defaults
                 CRM_Quest_BAO_Student::retrieve($details, $studentDefaults, $ids);
                 $studentFields = array('educational_interest', 'college_type', 'college_interest', 'test_tutoring');
                 foreach ($studentFields as $fld) {
                     if ($studentDefaults[$fld]) {
                         $values = explode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, $studentDefaults[$fld]);
                     }
                     $studentDefaults[$fld] = array();
                     if (is_array($values)) {
                         foreach ($values as $v) {
                             $studentDefaults[$fld][$v] = 1;
                         }
                     }
                 }
                 foreach ($fields as $name => $field) {
                     $fldName = "field[{$contactId}][{$name}]";
                     if (array_key_exists($name, $studentDefaults)) {
                         $defaults[$fldName] = $studentDefaults[$name];
                     }
                 }
             }
         }
     }
     //Handling Contribution Part of the batch profile
     if (CRM_Core_Permission::access('CiviContribute') && $component == 'Contribute') {
         self::setComponentDefaults($fields, $componentId, $component, $defaults);
     }
     //Handling Event Participation Part of the batch profile
     if (CRM_Core_Permission::access('CiviEvent') && $component == 'Event') {
         self::setComponentDefaults($fields, $componentId, $component, $defaults);
     }
     //Handling membership Part of the batch profile
     if (CRM_Core_Permission::access('CiviMember') && $component == 'Membership') {
         self::setComponentDefaults($fields, $componentId, $component, $defaults);
     }
 }
Exemplo n.º 3
0
 /**
  * Set profile defaults.
  *
  * @param int $contactId
  *   Contact id.
  * @param array $fields
  *   Associative array of fields.
  * @param array $defaults
  *   Defaults array.
  * @param bool $singleProfile
  *   True for single profile else false(batch update).
  * @param int $componentId
  *   Id for specific components like contribute, event etc.
  * @param null $component
  */
 public static function setProfileDefaults($contactId, &$fields, &$defaults, $singleProfile = TRUE, $componentId = NULL, $component = NULL)
 {
     if (!$componentId) {
         //get the contact details
         list($contactDetails, $options) = CRM_Contact_BAO_Contact::getHierContactDetails($contactId, $fields);
         $details = CRM_Utils_Array::value($contactId, $contactDetails);
         $multipleFields = array('website' => 'url');
         //start of code to set the default values
         foreach ($fields as $name => $field) {
             // skip pseudo fields
             if (substr($name, 0, 9) == 'phone_ext') {
                 continue;
             }
             //set the field name depending upon the profile mode(single/batch)
             if ($singleProfile) {
                 $fldName = $name;
             } else {
                 $fldName = "field[{$contactId}][{$name}]";
             }
             if ($name == 'group') {
                 CRM_Contact_Form_Edit_TagsAndGroups::setDefaults($contactId, $defaults, CRM_Contact_Form_Edit_TagsAndGroups::GROUP, $fldName);
             }
             if ($name == 'tag') {
                 CRM_Contact_Form_Edit_TagsAndGroups::setDefaults($contactId, $defaults, CRM_Contact_Form_Edit_TagsAndGroups::TAG, $fldName);
             }
             if (!empty($details[$name]) || isset($details[$name])) {
                 //to handle custom data (checkbox) to be written
                 // to handle birth/deceased date, greeting_type and few other fields
                 if ($name == 'birth_date' || $name == 'deceased_date') {
                     list($defaults[$fldName]) = CRM_Utils_Date::setDateDefaults($details[$name], 'birth');
                 } elseif (in_array($name, CRM_Contact_BAO_Contact::$_greetingTypes)) {
                     $defaults[$fldName] = $details[$name . '_id'];
                     $defaults[$name . '_custom'] = $details[$name . '_custom'];
                 } elseif ($name == 'preferred_communication_method') {
                     $v = explode(CRM_Core_DAO::VALUE_SEPARATOR, $details[$name]);
                     foreach ($v as $item) {
                         if ($item) {
                             $defaults[$fldName . "[{$item}]"] = 1;
                         }
                     }
                 } elseif ($name == 'contact_sub_type') {
                     $defaults[$fldName] = explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($details[$name], CRM_Core_DAO::VALUE_SEPARATOR));
                 } elseif ($name == 'world_region') {
                     $defaults[$fldName] = $details['worldregion_id'];
                 } elseif ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($name)) {
                     //fix for custom fields
                     $customFields = CRM_Core_BAO_CustomField::getFields(CRM_Utils_Array::value('contact_type', $details));
                     // hack to add custom data for components
                     $components = array('Contribution', 'Participant', 'Membership', 'Activity');
                     foreach ($components as $value) {
                         $customFields = CRM_Utils_Array::crmArrayMerge($customFields, CRM_Core_BAO_CustomField::getFieldsForImport($value));
                     }
                     switch ($customFields[$customFieldId]['html_type']) {
                         case 'Multi-Select State/Province':
                         case 'Multi-Select Country':
                         case 'AdvMulti-Select':
                         case 'Multi-Select':
                             $v = explode(CRM_Core_DAO::VALUE_SEPARATOR, $details[$name]);
                             foreach ($v as $item) {
                                 if ($item) {
                                     $defaults[$fldName][$item] = $item;
                                 }
                             }
                             break;
                         case 'CheckBox':
                             $v = explode(CRM_Core_DAO::VALUE_SEPARATOR, $details[$name]);
                             foreach ($v as $item) {
                                 if ($item) {
                                     $defaults[$fldName][$item] = 1;
                                     // seems like we need this for QF style checkboxes in profile where its multiindexed
                                     // CRM-2969
                                     $defaults["{$fldName}[{$item}]"] = 1;
                                 }
                             }
                             break;
                         case 'Select Date':
                             // CRM-6681, set defult values according to date and time format (if any).
                             $dateFormat = NULL;
                             if (!empty($customFields[$customFieldId]['date_format'])) {
                                 $dateFormat = $customFields[$customFieldId]['date_format'];
                             }
                             if (empty($customFields[$customFieldId]['time_format'])) {
                                 list($defaults[$fldName]) = CRM_Utils_Date::setDateDefaults($details[$name], NULL, $dateFormat);
                             } else {
                                 $timeElement = $fldName . '_time';
                                 if (substr($fldName, -1) == ']') {
                                     $timeElement = substr($fldName, 0, -1) . '_time]';
                                 }
                                 list($defaults[$fldName], $defaults[$timeElement]) = CRM_Utils_Date::setDateDefaults($details[$name], NULL, $dateFormat, $customFields[$customFieldId]['time_format']);
                             }
                             break;
                         default:
                             $defaults[$fldName] = $details[$name];
                             break;
                     }
                 } else {
                     $defaults[$fldName] = $details[$name];
                 }
             } else {
                 $blocks = array('email', 'phone', 'im', 'openid');
                 list($fieldName, $locTypeId, $phoneTypeId) = CRM_Utils_System::explode('-', $name, 3);
                 if (!in_array($fieldName, $multipleFields)) {
                     if (is_array($details)) {
                         foreach ($details as $key => $value) {
                             // when we fixed CRM-5319 - get primary loc
                             // type as per loc field and removed below code.
                             $primaryLocationType = FALSE;
                             if ($locTypeId == 'Primary') {
                                 if (is_array($value) && array_key_exists($fieldName, $value)) {
                                     $primaryLocationType = TRUE;
                                     if (in_array($fieldName, $blocks)) {
                                         $locTypeId = CRM_Contact_BAO_Contact::getPrimaryLocationType($contactId, FALSE, $fieldName);
                                     } else {
                                         $locTypeId = CRM_Contact_BAO_Contact::getPrimaryLocationType($contactId, FALSE, 'address');
                                     }
                                 }
                             }
                             // fixed for CRM-665
                             if (is_numeric($locTypeId)) {
                                 if ($primaryLocationType || $locTypeId == CRM_Utils_Array::value('location_type_id', $value)) {
                                     if (!empty($value[$fieldName])) {
                                         //to handle stateprovince and country
                                         if ($fieldName == 'state_province') {
                                             $defaults[$fldName] = $value['state_province_id'];
                                         } elseif ($fieldName == 'county') {
                                             $defaults[$fldName] = $value['county_id'];
                                         } elseif ($fieldName == 'country') {
                                             if (!isset($value['country_id']) || !$value['country_id']) {
                                                 $config = CRM_Core_Config::singleton();
                                                 if ($config->defaultContactCountry) {
                                                     $defaults[$fldName] = $config->defaultContactCountry;
                                                 }
                                             } else {
                                                 $defaults[$fldName] = $value['country_id'];
                                             }
                                         } elseif ($fieldName == 'phone') {
                                             if ($phoneTypeId) {
                                                 if (isset($value['phone'][$phoneTypeId])) {
                                                     $defaults[$fldName] = $value['phone'][$phoneTypeId];
                                                 }
                                                 if (isset($value['phone_ext'][$phoneTypeId])) {
                                                     $defaults[str_replace('phone', 'phone_ext', $fldName)] = $value['phone_ext'][$phoneTypeId];
                                                 }
                                             } else {
                                                 $phoneDefault = CRM_Utils_Array::value('phone', $value);
                                                 // CRM-9216
                                                 if (!is_array($phoneDefault)) {
                                                     $defaults[$fldName] = $phoneDefault;
                                                 }
                                             }
                                         } elseif ($fieldName == 'email') {
                                             //adding the first email (currently we don't support multiple emails of same location type)
                                             $defaults[$fldName] = $value['email'];
                                         } elseif ($fieldName == 'im') {
                                             //adding the first im (currently we don't support multiple ims of same location type)
                                             $defaults[$fldName] = $value['im'];
                                             $defaults[$fldName . '-provider_id'] = $value['im_provider_id'];
                                         } else {
                                             $defaults[$fldName] = $value[$fieldName];
                                         }
                                     } elseif (substr($fieldName, 0, 14) === 'address_custom' && CRM_Utils_Array::value(substr($fieldName, 8), $value)) {
                                         $defaults[$fldName] = $value[substr($fieldName, 8)];
                                     }
                                 }
                             }
                         }
                     }
                 } else {
                     if (is_array($details)) {
                         if ($fieldName === 'url' && !empty($details['website']) && !empty($details['website'][$locTypeId])) {
                             $defaults[$fldName] = CRM_Utils_Array::value('url', $details['website'][$locTypeId]);
                         }
                     }
                 }
             }
         }
     }
     //Handling Contribution Part of the batch profile
     if (CRM_Core_Permission::access('CiviContribute') && $component == 'Contribute') {
         self::setComponentDefaults($fields, $componentId, $component, $defaults);
     }
     //Handling Event Participation Part of the batch profile
     if (CRM_Core_Permission::access('CiviEvent') && $component == 'Event') {
         self::setComponentDefaults($fields, $componentId, $component, $defaults);
     }
     //Handling membership Part of the batch profile
     if (CRM_Core_Permission::access('CiviMember') && $component == 'Membership') {
         self::setComponentDefaults($fields, $componentId, $component, $defaults);
     }
     //Handling Activity Part of the batch profile
     if ($component == 'Activity') {
         self::setComponentDefaults($fields, $componentId, $component, $defaults);
     }
 }
Exemplo n.º 4
0
 /**
  * Process the user submitted custom data values.
  *
  * @access public
  * @return void
  */
 function postProcess()
 {
     $params = $this->controller->exportValues($this->_name);
     $data = array();
     $data['contact_type'] = 'Individual';
     //get the custom fields for the contact
     $customFields = CRM_Core_BAO_CustomField::getFields($data['contact_type']);
     $locationType = array();
     $count = 1;
     if ($this->_id) {
         $primaryLocationType = CRM_Contact_BAO_Contact::getPrimaryLocationType($this->_id);
     }
     $phoneLoc = 0;
     foreach ($params as $key => $value) {
         $keyValue = explode('-', $key);
         if (is_numeric($keyValue[1])) {
             if (!in_array($keyValue[1], $locationType)) {
                 $locationType[$count] = $keyValue[1];
                 $count++;
             }
             require_once 'CRM/Utils/Array.php';
             $loc = CRM_Utils_Array::key($keyValue[1], $locationType);
             $data['location'][$loc]['location_type_id'] = $keyValue[1];
             if ($this->_id) {
                 //get the primary location type
                 if ($keyValue[1] == $primaryLocationType) {
                     $data['location'][$loc]['is_primary'] = 1;
                 }
             } else {
                 if ($loc == 1) {
                     $data['location'][$loc]['is_primary'] = 1;
                 }
             }
             if ($keyValue[0] == 'name') {
                 $data['location'][$loc]['name'] = $value;
             } else {
                 if ($keyValue[0] == 'phone') {
                     $phoneLoc++;
                     if ($keyValue[2]) {
                         $data['location'][$loc]['phone'][$phoneLoc]['phone_type'] = $keyValue[2];
                     } else {
                         $data['location'][$loc]['phone'][$phoneLoc]['phone_type'] = '';
                         $data['location'][$loc]['phone'][$phoneLoc]['is_primary'] = 1;
                     }
                     $data['location'][$loc]['phone'][$phoneLoc]['phone'] = $value;
                 } else {
                     if ($keyValue[0] == 'email') {
                         $data['location'][$loc]['email'][1]['email'] = $value;
                         $data['location'][$loc]['email'][1]['is_primary'] = 1;
                     } else {
                         if ($keyValue[0] == 'im') {
                             $data['location'][$loc]['im'][1]['name'] = $value;
                             $data['location'][$loc]['im'][1]['is_primary'] = 1;
                         } else {
                             if ($keyValue[0] === 'state_province') {
                                 $data['location'][$loc]['address']['state_province_id'] = $value;
                             } else {
                                 if ($keyValue[0] === 'country') {
                                     $data['location'][$loc]['address']['country_id'] = $value;
                                 } else {
                                     $data['location'][$loc]['address'][$keyValue[0]] = $value;
                                 }
                             }
                         }
                     }
                 }
             }
         } else {
             if ($key === 'individual_suffix') {
                 $data['suffix_id'] = $value;
             } else {
                 if ($key === 'individual_prefix') {
                     $data['prefix_id'] = $value;
                 } else {
                     if ($key === 'gender') {
                         $data['gender_id'] = $value;
                     } else {
                         if (substr($key, 0, 6) === 'custom') {
                             if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
                                 //fix checkbox
                                 if ($customFields[$customFieldID][3] == 'CheckBox') {
                                     $value = implode(CRM_CORE_BAO_CUSTOMOPTION_VALUE_SEPERATOR, array_keys($value));
                                 }
                                 if ($customFields[$customFieldID][3] == 'Multi-Select') {
                                     $value = implode(CRM_CORE_BAO_CUSTOMOPTION_VALUE_SEPERATOR, $value);
                                 }
                                 // fix the date field
                                 if ($customFields[$customFieldID][2] == 'Date') {
                                     $date = CRM_Utils_Date::format($value);
                                     if (!$date) {
                                         $date = '';
                                     }
                                     $value = $date;
                                 }
                                 //to add the id of custom value if exits
                                 //$this->_contact['custom_value_5_id'] = 123;
                                 $str = 'custom_value_' . $customFieldID . '_id';
                                 if ($this->_contact[$str]) {
                                     $id = $this->_contact[$str];
                                 }
                                 $data['custom'][$customFieldID] = array('id' => $id, 'value' => $value, 'extends' => $customFields[$customFieldID][3], 'type' => $customFields[$customFieldID][2], 'custom_field_id' => $customFieldID);
                             }
                         } else {
                             if ($key == 'edit') {
                                 continue;
                             } else {
                                 $data[$key] = $value;
                             }
                         }
                     }
                 }
             }
         }
     }
     // fix all the custom field checkboxes which are empty
     foreach ($this->_fields as $name => $field) {
         $cfID = CRM_Core_BAO_CustomField::getKeyID($name);
         // if there is a custom field of type checkbox and it has not been set
         // then set it to null, thanx to html protocol
         if ($cfID && $customFields[$cfID][3] == 'CheckBox' && CRM_Utils_Array::value('custom', $data) && !CRM_Utils_Array::value($cfID, $data['custom'])) {
             $str = 'custom_value_' . $cfID . '_id';
             if ($this->_contact[$str]) {
                 $id = $this->_contact[$str];
             }
             $data['custom'][$cfID] = array('id' => $id, 'value' => '', 'extends' => $customFields[$cfID][3], 'type' => $customFields[$cfID][2], 'custom_field_id' => $cfID);
         }
     }
     if ($this->_id) {
         $objects = array('contact_id', 'individual_id', 'location_id', 'address_id');
         $ids = array();
         $phoneLoc = 0;
         foreach ($this->_fields as $name => $field) {
             $nameValue = explode('-', $name);
             foreach ($this->_contact as $key => $value) {
                 if (in_array($key, $objects)) {
                     $ids[substr($key, 0, strlen($key) - 3)] = $value;
                 } else {
                     if (is_array($value)) {
                         //fixed for CRM-665
                         if ($nameValue[1] == $value['location_type_id']) {
                             $locations[$value['location_type_id']] = 1;
                             $loc_no = count($locations);
                             if ($nameValue[0] == 'phone') {
                                 $phoneLoc++;
                                 if (isset($nameValue[2])) {
                                     $ids['location'][$loc_no]['phone'][$phoneLoc] = $value['phone'][$nameValue[2] . '_id'];
                                 } else {
                                     $ids['location'][$loc_no]['phone'][$phoneLoc] = $value['phone']['1_id'];
                                 }
                             } else {
                                 if ($nameValue[0] == 'im') {
                                     $ids['location'][$loc_no]['im'][1] = $value['im']['1_id'];
                                 } else {
                                     if ($nameValue[0] == 'email') {
                                         $ids['location'][$loc_no]['email'][1] = $value['email']['1_id'];
                                     } else {
                                         $ids['location'][$loc_no]['address'] = $value['address_id'];
                                     }
                                 }
                             }
                             $ids['location'][$loc_no]['id'] = $value['location_id'];
                         }
                     }
                 }
             }
         }
     }
     //set the values for checkboxes (do_not_email, do_not_mail, do_not_trade, do_not_phone)
     $privacy = CRM_Core_SelectValues::privacy();
     foreach ($privacy as $key => $value) {
         if (array_key_exists($key, $this->_fields)) {
             if ($params[$key]) {
                 $data[$key] = $params[$key];
             } else {
                 $data[$key] = 0;
             }
         }
     }
     // manage is_opt_out
     if (array_key_exists('is_opt_out', $this->_fields)) {
         $wasOptOut = $this->_contact['is_opt_out'] ? true : false;
         $isOptOut = $params['is_opt_out'] ? true : false;
         $data['is_opt_out'] = $isOptOut;
         // on change, create new civicrm_subscription_history entry
         if ($wasOptOut != $isOptOut) {
             $shParams = array('contact_id' => $this->_contact['contact_id'], 'status' => $isOptOut ? 'Removed' : 'Added', 'method' => 'Web');
             CRM_Contact_BAO_SubscriptionHistory::create($shParams);
         }
     }
     if ($this->_mode == CRM_PROFILE_FORM_MODE_REGISTER) {
         require_once 'CRM/Core/BAO/Address.php';
         CRM_Core_BAO_Address::setOverwrite(false);
     }
     require_once 'CRM/Contact/BAO/Contact.php';
     $contact = CRM_Contact_BAO_Contact::create($data, $ids, count($data['location']));
     // Process group and tag
     if (CRM_Utils_Array::value('group', $this->_fields)) {
         CRM_Contact_BAO_GroupContact::create($params['group'], $contact->id);
     }
     if (CRM_Utils_Array::value('tag', $this->_fields)) {
         require_once 'CRM/Core/BAO/EntityTag.php';
         CRM_Core_BAO_EntityTag::create($params['tag'], $contact->id);
     }
 }