/** * FIXME: Move to somewhere more useful * FIXME: Do real mapping of "types" * * @param string $extends * Entity type; note: "Individual" means "Individual|Contact"; "Household" means "Household|Contact". * @param string $title * A string to use in section headers. * @param array $availableFields * List of fields that are allowed in profiles, e.g. $availableFields['my_field']['field_type']. * @return array * with keys 'sections' and 'schema' * @see js/model/crm.core.js * @see js/model/crm.mappedcore.js */ public static function convertCiviModelToBackboneModel($extends, $title, $availableFields) { $locationFields = CRM_Core_BAO_UFGroup::getLocationFields(); $result = array('schema' => array(), 'sections' => array()); // build field list foreach ($availableFields as $fieldName => $field) { switch ($extends) { case 'Individual': case 'Organization': case 'Household': if ($field['field_type'] != $extends && $field['field_type'] != 'Contact' && !in_array($field['field_type'], CRM_Contact_BAO_ContactType::subTypes($extends))) { continue 2; } break; default: if ($field['field_type'] != $extends) { continue 2; } } $result['schema'][$fieldName] = array('type' => 'Text', 'title' => $field['title'], 'civiFieldType' => $field['field_type']); if (in_array($fieldName, $locationFields)) { $result['schema'][$fieldName]['civiIsLocation'] = TRUE; } if ($fieldName == 'url') { $result['schema'][$fieldName]['civiIsWebsite'] = TRUE; } if (in_array($fieldName, array('phone', 'phone_and_ext'))) { // FIXME what about phone_ext? $result['schema'][$fieldName]['civiIsPhone'] = TRUE; } } // build section list $result['sections']['default'] = array('title' => $title, 'is_addable' => FALSE); $customGroup = CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity($extends); $customGroup->orderBy('weight'); $customGroup->is_active = 1; $customGroup->find(); while ($customGroup->fetch()) { $sectionName = 'cg_' . $customGroup->id; $section = array('title' => ts('%1: %2', array(1 => $title, 2 => $customGroup->title)), 'is_addable' => $customGroup->is_reserved ? FALSE : TRUE, 'custom_group_id' => $customGroup->id, 'extends_entity_column_id' => $customGroup->extends_entity_column_id, 'extends_entity_column_value' => CRM_Utils_Array::explodePadded($customGroup->extends_entity_column_value), 'is_reserved' => $customGroup->is_reserved ? TRUE : FALSE); $result['sections'][$sectionName] = $section; } // put fields in their sections $fields = CRM_Core_BAO_CustomField::getFields($extends); foreach ($fields as $fieldId => $field) { $sectionName = 'cg_' . $field['custom_group_id']; $fieldName = 'custom_' . $fieldId; if (isset($result['schema'][$fieldName])) { $result['schema'][$fieldName]['section'] = $sectionName; $result['schema'][$fieldName]['civiIsMultiple'] = (bool) CRM_Core_BAO_CustomField::isMultiRecordField($fieldId); } } return $result; }
/** * Build the form object. * * @return void */ public function buildQuickForm() { if ($this->_action & CRM_Core_Action::DELETE) { $this->addButtons(array(array('type' => 'next', 'name' => ts('Delete Profile Field'), 'spacing' => ' ', 'isDefault' => TRUE), array('type' => 'cancel', 'name' => ts('Cancel')))); return; } if (isset($this->_id)) { $params = array('id' => $this->_id); CRM_Core_BAO_UFField::retrieve($params, $defaults); // set it to null if so (avoids crappy E_NOTICE errors below $defaults['location_type_id'] = CRM_Utils_Array::value('location_type_id', $defaults); $specialFields = CRM_Core_BAO_UFGroup::getLocationFields(); if (!$defaults['location_type_id'] && $defaults["field_type"] != "Formatting" && in_array($defaults['field_name'], $specialFields)) { $defaults['location_type_id'] = 0; } $defaults['field_name'] = array($defaults['field_type'], $defaults['field_type'] == "Formatting" ? "" : $defaults['field_name'], $defaults['field_name'] == "url" ? $defaults['website_type_id'] : $defaults['location_type_id'], CRM_Utils_Array::value('phone_type_id', $defaults)); $this->_gid = $defaults['uf_group_id']; } else { $defaults['is_active'] = 1; } $otherModules = array_values(CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_gid)); $this->assign('otherModules', $otherModules); if ($this->_action & CRM_Core_Action::ADD) { $fieldValues = array('uf_group_id' => $this->_gid); $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_UFField', $fieldValues); } // lets trim all the whitespace $this->applyFilter('__ALL__', 'trim'); //hidden field to catch the group id in profile $this->add('hidden', 'group_id', $this->_gid); //hidden field to catch the field id in profile $this->add('hidden', 'field_id', $this->_id); $fields = CRM_Core_BAO_UFField::getAvailableFields($this->_gid, $defaults); $noSearchable = $hasWebsiteTypes = array(); $addressCustomFields = array_keys(CRM_Core_BAO_CustomField::getFieldsForImport('Address')); foreach ($fields as $key => $value) { foreach ($value as $key1 => $value1) { //CRM-2676, replacing the conflict for same custom field name from different custom group. if ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($key1)) { $customGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $customFieldId, 'custom_group_id'); $customGroupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'title'); $this->_mapperFields[$key][$key1] = $value1['title'] . ' :: ' . $customGroupName; if (in_array($key1, $addressCustomFields)) { $noSearchable[] = $value1['title'] . ' :: ' . $customGroupName; } } else { $this->_mapperFields[$key][$key1] = $value1['title']; } $hasLocationTypes[$key][$key1] = CRM_Utils_Array::value('hasLocationType', $value1); $hasWebsiteTypes[$key][$key1] = CRM_Utils_Array::value('hasWebsiteType', $value1); // hide the 'is searchable' field for 'File' custom data if (isset($value1['data_type']) && isset($value1['html_type']) && ($value1['data_type'] == 'File' && $value1['html_type'] == 'File' || $value1['data_type'] == 'Link' && $value1['html_type'] == 'Link')) { if (!in_array($value1['title'], $noSearchable)) { $noSearchable[] = $value1['title']; } } } } $this->assign('noSearchable', $noSearchable); $this->_location_types = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); $this->_website_types = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id'); /** * FIXME: dirty hack to make the default option show up first. This * avoids a mozilla browser bug with defaults on dynamically constructed * selector widgets. */ if ($defaultLocationType) { $defaultLocation = $this->_location_types[$defaultLocationType->id]; unset($this->_location_types[$defaultLocationType->id]); $this->_location_types = array($defaultLocationType->id => $defaultLocation) + $this->_location_types; } $this->_location_types = array('Primary') + $this->_location_types; // since we need a hierarchical list to display contact types & subtypes, // this is what we going to display in first selector $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, FALSE); unset($contactTypes['']); $contactTypes = !empty($contactTypes) ? array('Contact' => 'Contacts') + $contactTypes : array(); $sel1 = array('' => '- select -') + $contactTypes; if (!empty($fields['Activity'])) { $sel1['Activity'] = 'Activity'; } if (CRM_Core_Permission::access('CiviEvent')) { $sel1['Participant'] = 'Participants'; } if (!empty($fields['Contribution'])) { $sel1['Contribution'] = 'Contributions'; } if (!empty($fields['Membership'])) { $sel1['Membership'] = 'Membership'; } if (!empty($fields['Case'])) { $sel1['Case'] = 'Case'; } if (!empty($fields['Formatting'])) { $sel1['Formatting'] = 'Formatting'; } foreach ($sel1 as $key => $sel) { if ($key) { $sel2[$key] = $this->_mapperFields[$key]; } } $sel3[''] = NULL; $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'); ksort($phoneTypes); foreach ($sel1 as $k => $sel) { if ($k) { foreach ($this->_location_types as $key => $value) { $sel4[$k]['phone'][$key] =& $phoneTypes; $sel4[$k]['phone_and_ext'][$key] =& $phoneTypes; } } } foreach ($sel1 as $k => $sel) { if ($k) { if (is_array($this->_mapperFields[$k])) { foreach ($this->_mapperFields[$k] as $key => $value) { if ($hasLocationTypes[$k][$key]) { $sel3[$k][$key] = $this->_location_types; } elseif ($hasWebsiteTypes[$k][$key]) { $sel3[$k][$key] = $this->_website_types; } else { $sel3[$key] = NULL; } } } } } $this->_defaults = array(); $js = "<script type='text/javascript'>\n"; $formName = "document.{$this->_name}"; $alreadyMixProfile = FALSE; if (CRM_Core_BAO_UFField::checkProfileType($this->_gid)) { $alreadyMixProfile = TRUE; } $this->assign('alreadyMixProfile', $alreadyMixProfile); $sel =& $this->addElement('hierselect', 'field_name', ts('Field Name')); $formValues = $this->exportValues(); if (empty($formValues)) { for ($k = 1; $k < 4; $k++) { if (!isset($defaults['field_name'][$k])) { $js .= "{$formName}['field_name[{$k}]'].style.display = 'none';\n"; } } } else { if (!empty($formValues['field_name'])) { for ($key = 1; $key < 4; $key++) { if (!isset($formValues['field_name'][$key])) { $js .= "{$formName}['field_name[{$key}]'].style.display = 'none';\n"; } else { $js .= "{$formName}['field_name[{$key}]'].style.display = '';\n"; } } } else { for ($k = 1; $k < 4; $k++) { if (!isset($defaults['field_name'][$k])) { $js .= "{$formName}['field_name[{$k}]'].style.display = 'none';\n"; } } } } foreach ($sel2 as $k => $v) { if (is_array($sel2[$k])) { asort($sel2[$k]); } } $sel->setOptions(array($sel1, $sel2, $sel3, $sel4)); // proper interpretation of spec in CRM-8732 if (!isset($this->_id) && in_array('Search Profile', $otherModules)) { $defaults['visibility'] = 'Public Pages and Listings'; } $js .= "</script>\n"; $this->assign('initHideBoxes', $js); $this->add('select', 'visibility', ts('Visibility'), CRM_Core_SelectValues::ufVisibility(), TRUE, array('onChange' => "showHideSeletorSearch(this.value);")); //CRM-4363 $js = array('onChange' => "mixProfile();"); // should the field appear in selectors (as a column)? $this->add('checkbox', 'in_selector', ts('Results Column?'), NULL, NULL, $js); $this->add('checkbox', 'is_searchable', ts('Searchable?'), NULL, NULL, $js); $attributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFField'); // weight $this->add('text', 'weight', ts('Order'), $attributes['weight'], TRUE); $this->addRule('weight', ts('is a numeric field'), 'numeric'); $this->add('textarea', 'help_pre', ts('Field Pre Help'), $attributes['help_pre']); $this->add('textarea', 'help_post', ts('Field Post Help'), $attributes['help_post']); $this->add('checkbox', 'is_required', ts('Required?')); $this->add('checkbox', 'is_multi_summary', ts('Include in multi-record listing?')); $this->add('checkbox', 'is_active', ts('Active?')); $this->add('checkbox', 'is_view', ts('View Only?')); // $this->add( 'checkbox', 'is_registration', ts( 'Display in Registration Form?' ) ); //$this->add( 'checkbox', 'is_match' , ts( 'Key to Match Contacts?' ) ); $this->add('text', 'label', ts('Field Label'), $attributes['label']); $js = NULL; if ($this->_hasSearchableORInSelector) { $js = array('onclick' => "return verify( );"); } // add buttons $this->addButtons(array(array('type' => 'next', 'name' => ts('Save'), 'isDefault' => TRUE, 'js' => $js), array('type' => 'next', 'name' => ts('Save and New'), 'subName' => 'new', 'js' => $js), array('type' => 'cancel', 'name' => ts('Cancel')))); $this->addFormRule(array('CRM_UF_Form_Field', 'formRule'), $this); // if view mode pls freeze it with the done button. if ($this->_action & CRM_Core_Action::VIEW) { $this->freeze(); $this->addElement('button', 'done', ts('Done'), array('onclick' => "location.href='civicrm/admin/uf/group/field?reset=1&action=browse&gid=" . $this->_gid . "'")); } $this->setDefaults($defaults); }