/** * 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); } }
/** * Build the form object. */ public function buildQuickForm() { //load form for child blocks if ($this->_addBlockName) { $className = 'CRM_Contact_Form_Edit_' . $this->_addBlockName; return $className::buildQuickForm($this); } if ($this->_action == CRM_Core_Action::UPDATE) { $deleteExtra = json_encode(ts('Are you sure you want to delete contact image.')); $deleteURL = array(CRM_Core_Action::DELETE => array('name' => ts('Delete Contact Image'), 'url' => 'civicrm/contact/image', 'qs' => 'reset=1&cid=%%id%%&action=delete', 'extra' => 'onclick = "' . htmlspecialchars("if (confirm({$deleteExtra})) this.href+='&confirmed=1'; else return false;") . '"')); $deleteURL = CRM_Core_Action::formLink($deleteURL, CRM_Core_Action::DELETE, array('id' => $this->_contactId), ts('more'), FALSE, 'contact.image.delete', 'Contact', $this->_contactId); $this->assign('deleteURL', $deleteURL); } //build contact type specific fields $className = 'CRM_Contact_Form_Edit_' . $this->_contactType; $className::buildQuickForm($this); // build Custom data if Custom data present in edit option $buildCustomData = 'noCustomDataPresent'; if (array_key_exists('CustomData', $this->_editOptions)) { $buildCustomData = "customDataPresent"; } // subtype is a common field. lets keep it here $subtypes = CRM_Contact_BAO_Contact::buildOptions('contact_sub_type', 'create', array('contact_type' => $this->_contactType)); if (!empty($subtypes)) { $this->addField('contact_sub_type', array('label' => ts('Contact Type'), 'options' => $subtypes, 'class' => $buildCustomData, 'multiple' => 'multiple', 'options-url' => FALSE)); } // build edit blocks ( custom data, demographics, communication preference, notes, tags and groups ) foreach ($this->_editOptions as $name => $label) { if ($name == 'Address') { $this->_blocks['Address'] = $this->_editOptions['Address']; continue; } if ($name == 'TagsAndGroups') { continue; } $className = 'CRM_Contact_Form_Edit_' . $name; $className::buildQuickForm($this); } // build tags and groups CRM_Contact_Form_Edit_TagsAndGroups::buildQuickForm($this, 0, CRM_Contact_Form_Edit_TagsAndGroups::ALL, FALSE, NULL, 'Group(s)', 'Tag(s)', NULL, 'select'); // build location blocks. CRM_Contact_Form_Edit_Lock::buildQuickForm($this); CRM_Contact_Form_Location::buildQuickForm($this); // add attachment $this->addField('image_URL', array('maxlength' => '60', 'label' => ts('Browse/Upload Image'))); // add the dedupe button $this->addElement('submit', $this->_dedupeButtonName, ts('Check for Matching Contact(s)')); $this->addElement('submit', $this->_duplicateButtonName, ts('Save Matching Contact')); $this->addElement('submit', $this->getButtonName('next', 'sharedHouseholdDuplicate'), ts('Save With Duplicate Household')); $buttons = array(array('type' => 'upload', 'name' => ts('Save'), 'subName' => 'view', 'isDefault' => TRUE)); if (CRM_Core_Permission::check('add contacts')) { $buttons[] = array('type' => 'upload', 'name' => ts('Save and New'), 'spacing' => ' ', 'subName' => 'new'); } $buttons[] = array('type' => 'cancel', 'name' => ts('Cancel')); if (!empty($this->_values['contact_sub_type'])) { $this->_oldSubtypes = explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($this->_values['contact_sub_type'], CRM_Core_DAO::VALUE_SEPARATOR)); } $this->assign('oldSubtypes', json_encode($this->_oldSubtypes)); $this->addButtons($buttons); }
/** * 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); } }