function hrui_civicrm_buildForm($formName, &$form) { CRM_Core_Resources::singleton()->addStyleFile('org.civicrm.hrui', 'css/hrui.css')->addScriptFile('org.civicrm.hrui', 'js/hrui.js'); if ($form instanceof CRM_Contact_Form_Contact) { CRM_Core_Resources::singleton()->addSetting(array('formName' => 'contactForm')); //HR-358 - Set default values //set default value to phone location and type $locationId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_LocationType', 'Main', 'id', 'name'); $result = civicrm_api3('LocationType', 'create', array('id' => $locationId, 'is_default' => 1, 'is_active' => 1)); if ($form->elementExists('phone[2][phone_type_id]') && $form->elementExists('phone[2][phone_type_id]')) { $phoneType = $form->getElement('phone[2][phone_type_id]'); $phoneValue = CRM_Core_OptionGroup::values('phone_type'); $phoneKey = CRM_Utils_Array::key('Mobile', $phoneValue); $phoneType->setSelected($phoneKey); $phoneLocation = $form->getElement('phone[2][location_type_id]'); $phoneLocation->setSelected($locationId); } } $ogID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'type_20130502144049', 'id', 'name'); //HR-355 -- Add Government ID if ($formName == 'CRM_Contact_Form_Contact' && $ogID && $form->_contactType == 'Individual') { //add government fields $contactID = CRM_Utils_Request::retrieve('cid', 'Integer', $form); $templatePath = CRM_Extension_System::singleton()->getMapper()->keyToBasePath('org.civicrm.hrui') . '/templates'; $form->add('text', 'GovernmentId', ts('Government ID')); $form->addElement('select', "govTypeOptions", '', CRM_Core_BAO_OptionValue::getOptionValuesAssocArray($ogID)); CRM_Core_Region::instance('page-body')->add(array('template' => "{$templatePath}/CRM/HRUI/Form/contactField.tpl")); $action = CRM_Utils_Request::retrieve('action', 'String', $form); $govVal = CRM_HRIdent_Page_HRIdent::retreiveContactFieldValue($contactID); //set default to government type option $default = array(); $default['govTypeOptions'] = CRM_Core_BAO_CustomField::getOptionGroupDefault($ogID, 'select'); if ($action == CRM_Core_Action::UPDATE && !empty($govVal)) { //set key for updating specific record of contact id in custom value table $default['govTypeOptions'] = CRM_Utils_Array::value('type', $govVal); $default['GovernmentId'] = CRM_Utils_Array::value('typeNumber', $govVal); } $form->setDefaults($default); } }
/** * Format custom value according to data, view mode * * @param array $values associated array of custom values * @param array $field associated array * @param boolean $dncOptionPerLine true if optionPerLine should not be consider * */ static function formatCustomValues(&$values, &$field, $dncOptionPerLine = FALSE) { $value = $values['data']; //changed isset CRM-4601 if (CRM_Utils_System::isNull($value)) { return; } $htmlType = CRM_Utils_Array::value('html_type', $field); $dataType = CRM_Utils_Array::value('data_type', $field); $option_group_id = CRM_Utils_Array::value('option_group_id', $field); $timeFormat = CRM_Utils_Array::value('time_format', $field); $optionPerLine = CRM_Utils_Array::value('options_per_line', $field); $freezeString = ""; $freezeStringChecked = ""; switch ($dataType) { case 'Date': $customTimeFormat = ''; $customFormat = NULL; switch ($timeFormat) { case 1: $customTimeFormat = '%l:%M %P'; break; case 2: $customTimeFormat = '%H:%M'; break; default: // if time is not selected remove time from value $value = substr($value, 0, 10); } $supportableFormats = array('mm/dd' => "%B %E%f {$customTimeFormat}", 'dd-mm' => "%E%f %B {$customTimeFormat}", 'yy' => "%Y {$customTimeFormat}", 'M yy' => "%b %Y {$customTimeFormat}", 'yy-mm' => "%Y-%m {$customTimeFormat}"); if ($format = CRM_Utils_Array::value('date_format', $field)) { if (array_key_exists($format, $supportableFormats)) { $customFormat = $supportableFormats["{$format}"]; } } $retValue = CRM_Utils_Date::customFormat($value, $customFormat); break; case 'Boolean': if ($value == '1') { $retValue = $freezeStringChecked . ts('Yes') . "\n"; } else { $retValue = $freezeStringChecked . ts('No') . "\n"; } break; case 'Link': if ($value) { $retValue = CRM_Utils_System::formatWikiURL($value); } break; case 'File': $retValue = $values; break; case 'ContactReference': if (CRM_Utils_Array::value('data', $values)) { $retValue = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $values['data'], 'display_name'); } break; case 'Memo': $retValue = $value; break; case 'Float': if ($htmlType == 'Text') { $retValue = (double) $value; break; } case 'Money': if ($htmlType == 'Text') { $retValue = CRM_Utils_Money::format($value, NULL, '%a'); break; } case 'String': case 'Int': if (in_array($htmlType, array('Text', 'TextArea'))) { $retValue = $value; break; } // note that if its not text / textarea, the code falls thru and executes // the below case also // note that if its not text / textarea, the code falls thru and executes // the below case also case 'StateProvince': case 'Country': $options = array(); $coDAO = NULL; //added check for Multi-Select in the below if-statement $customData[] = $value; //form custom data for multiple-valued custom data switch ($htmlType) { case 'Multi-Select Country': case 'Select Country': $customData = $value; if (!is_array($value)) { $customData = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value); } $query = "\n SELECT id as value, name as label\n FROM civicrm_country"; $coDAO = CRM_Core_DAO::executeQuery($query); break; case 'Select State/Province': case 'Multi-Select State/Province': $customData = $value; if (!is_array($value)) { $customData = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value); } $query = "\n SELECT id as value, name as label\n FROM civicrm_state_province"; $coDAO = CRM_Core_DAO::executeQuery($query); break; case 'Select': $customData = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value); if ($option_group_id) { $options = CRM_Core_BAO_OptionValue::getOptionValuesAssocArray($option_group_id); } break; case 'CheckBox': case 'AdvMulti-Select': case 'Multi-Select': $customData = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value); default: if ($option_group_id) { $options = CRM_Core_BAO_OptionValue::getOptionValuesAssocArray($option_group_id); } } if (is_object($coDAO)) { while ($coDAO->fetch()) { if ($dataType == 'Country') { // NB: using ts() on a variable here is OK, since the value is pre-determined, not variable // and already extracted to .pot files. $options[$coDAO->value] = ts($coDAO->label, array('context' => 'country')); } elseif ($dataType == 'StateProvince') { $options[$coDAO->value] = ts($coDAO->label, array('context' => 'province')); } else { $options[$coDAO->value] = $coDAO->label; } } } CRM_Utils_Hook::customFieldOptions($field['id'], $options, FALSE); $retValue = NULL; foreach ($options as $optionValue => $optionLabel) { if ($dataType == 'Money') { foreach ($customData as $k => $v) { $customData[] = CRM_Utils_Money::format($v, NULL, '%a'); } } //to show only values that are checked if (in_array((string) $optionValue, $customData)) { $checked = in_array($optionValue, $customData) ? $freezeStringChecked : $freezeString; if (!$optionPerLine || $dncOptionPerLine) { if ($retValue) { $retValue .= ", "; } $retValue .= $checked . $optionLabel; } else { $retValue[] = $checked . $optionLabel; } } } break; } //special case for option per line formatting if ($optionPerLine > 1 && is_array($retValue)) { $rowCounter = 0; $fieldCounter = 0; $displayValues = array(); $displayString = ''; foreach ($retValue as $val) { if ($displayString) { $displayString .= ", "; } $displayString .= $val; $rowCounter++; $fieldCounter++; if ($rowCounter == $optionPerLine || $fieldCounter == count($retValue)) { $displayValues[] = $displayString; $displayString = ''; $rowCounter = 0; } } $retValue = $displayValues; } $retValue = isset($retValue) ? $retValue : NULL; return $retValue; }