/** * build the form elements for a phone object * * @param CRM_Core_Form $form reference to the form object * @param array $location the location object to store all the form elements in * @param int $locationId the locationId we are dealing with * @param int $count the number of blocks to create * * @return void * @access public * @static */ function buildPhoneBlock(&$form, &$location, $locationId, $count) { for ($i = 1; $i <= $count; $i++) { $label = $i == 1 ? ts('Phone (preferred)') : ts('Phone'); CRM_Core_ShowHideBlocks::linksForArray($form, $i, $count, "location[{$locationId}][phone]", ts('another phone'), ts('hide this phone')); $location[$locationId]['phone'][$i]['phone_type'] = $form->addElement('select', "location[{$locationId}][phone][{$i}][phone_type]", null, CRM_Core_SelectValues::phoneType()); $location[$locationId]['phone'][$i]['phone'] = $form->addElement('text', "location[{$locationId}][phone][{$i}][phone]", $label, CRM_Core_DAO::getAttribute('CRM_Core_DAO_Phone', 'phone')); // TODO: set this up as a group, we need a valid phone_type_id if we have a phone number // $form->addRule( "location[$locationId][phone][$i][phone]", ts('Phone number is not valid.'), 'phone' ); } }
/** * Process the mapped fields and map it into the uploaded file * preview the file and extract some summary statistics * * @return void * @access public */ function postProcess() { $fileName = $this->controller->exportValue('UploadFile', 'uploadFile'); $skipColumnHeader = $this->controller->exportValue('UploadFile', 'skipColumnHeader'); $invalidRowCount = $this->get('invalidRowCount'); $conflictRowCount = $this->get('conflictRowCount'); $onDuplicate = $this->get('onDuplicate'); $newGroup = $this->controller->exportValue($this->_name, 'newGroup'); $newGroupName = $this->controller->exportValue($this->_name, 'newGroupName'); $newGroupDesc = $this->controller->exportValue($this->_name, 'newGroupDesc'); $groups = $this->controller->exportValue($this->_name, 'groups'); $allGroups = $this->get('groups'); $tagForContact = $this->controller->exportValue($this->_name, 'tag'); $allTags = $this->get('tag'); $seperator = ','; $mapper = $this->controller->exportValue('MapField', 'mapper'); $mapperKeys = array(); $mapperLocTypes = array(); $mapperPhoneTypes = array(); $mapperRelated = array(); $mapperRelatedContactType = array(); $mapperRelatedContactDetails = array(); $mapperRelatedContactLocType = array(); $mapperRelatedContactPhoneType = array(); foreach ($mapper as $key => $value) { $mapperKeys[$key] = $mapper[$key][0]; if (is_numeric($mapper[$key][1])) { $mapperLocTypes[$key] = $mapper[$key][1]; } else { $mapperLocTypes[$key] = null; } if (!is_numeric($mapper[$key][2])) { $mapperPhoneTypes[$key] = $mapper[$key][2]; } else { $mapperPhoneTypes[$key] = null; } list($id, $first, $second) = explode('_', $mapper[$key][0]); if ($first == 'a' && $second == 'b' || $first == 'b' && $second == 'a') { $relationType =& new CRM_Contact_DAO_RelationshipType(); $relationType->id = $id; $relationType->find(true); eval('$mapperRelatedContactType[$key] = $relationType->contact_type_' . $second . ';'); $mapperRelated[$key] = $mapper[$key][0]; $mapperRelatedContactDetails[$key] = $mapper[$key][1]; $mapperRelatedContactLocType[$key] = $mapper[$key][2]; $mapperRelatedContactPhoneType[$key] = $mapper[$key][3]; } else { $mapperRelated[$key] = null; $mapperRelatedContactType[$key] = null; $mapperRelatedContactDetails[$key] = null; $mapperRelatedContactLocType[$key] = null; $mapperRelatedContactPhoneType[$key] = null; } } $parser =& new CRM_Import_Parser_Contact($mapperKeys, $mapperLocTypes, $mapperPhoneTypes, $mapperRelated, $mapperRelatedContactType, $mapperRelatedContactDetails, $mapperRelatedContactLocType, $mapperRelatedContactPhoneType); $mapFields = $this->get('fields'); $locationTypes = CRM_Core_PseudoConstant::locationType(); $phoneTypes = CRM_Core_SelectValues::phoneType(); foreach ($mapper as $key => $value) { $header = array(); list($id, $first, $second) = explode('_', $mapper[$key][0]); if ($first == 'a' && $second == 'b' || $first == 'b' && $second == 'a') { $relationType =& new CRM_Contact_DAO_RelationshipType(); $relationType->id = $id; $relationType->find(true); $header[] = $relationType->name_a_b; $header[] = ucwords(str_replace("_", " ", $mapper[$key][1])); if (isset($mapper[$key][2])) { $header[] = $locationTypes[$mapper[$key][2]]; } if (isset($mapper[$key][3])) { $header[] = $phoneTypes[$mapper[$key][3]]; } } else { if (isset($mapFields[$mapper[$key][0]])) { $header[] = $mapFields[$mapper[$key][0]]; if (isset($mapper[$key][1])) { $header[] = $locationTypes[$mapper[$key][1]]; } if (isset($mapper[$key][2])) { $header[] = $phoneTypes[$mapper[$key][2]]; } } } $mapperFields[] = implode(' - ', $header); } $parser->run($fileName, $seperator, $mapperFields, $skipColumnHeader, CRM_IMPORT_PARSER_MODE_IMPORT, $this->get('contactType'), $onDuplicate); // add the new contacts to selected groups $contactIds =& $parser->getImportedContacts(); // add the new related contacts to selected groups $relatedContactIds =& $parser->getRelatedImportedContacts(); $this->set('relatedCount', count($relatedContactIds)); $newGroupId = null; //changed below if-statement "if ($newGroup) {" to "if ($newGroupName) {" if ($newGroupName) { /* Create a new group */ $gParams = array('domain_id' => CRM_Core_Config::domainID(), 'name' => $newGroupName, 'title' => $newGroupName, 'description' => $newGroupDesc, 'is_active' => true); $group =& CRM_Contact_BAO_Group::create($gParams); $groups[] = $newGroupId = $group->id; } if (is_array($groups)) { $groupAdditions = array(); foreach ($groups as $groupId) { $addCount =& CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $groupId); if (!empty($relatedContactIds)) { $addRelCount =& CRM_Contact_BAO_GroupContact::addContactsToGroup($relatedContactIds, $groupId); } $totalCount = $addCount[1] + $addRelCount[1]; if ($groupId == $newGroupId) { $name = $newGroupName; $new = true; } else { $name = $allGroups[$groupId]; $new = false; } $groupAdditions[] = array('url' => CRM_Utils_System::url('civicrm/group/search', 'reset=1&force=1&context=smog&gid=' . $groupId), 'name' => $name, 'added' => $totalCount, 'notAdded' => $addCount[2], 'new' => $new); } $this->set('groupAdditions', $groupAdditions); } if (is_array($tagForContact)) { $tagAddition = array(); require_once "CRM/Core/BAO/EntityTag.php"; foreach ($tagForContact as $tagId => $selected) { $taggedContacts = CRM_Core_BAO_EntityTag::addContactsToTag($contactIds, $tagId); $tagName = $allTags[$tagId]; $tagAdditions[] = array('name' => $tagName, 'added' => $taggedContacts[1], 'notAdded' => $taggedContacts[2]); } $this->set('tagAdditions', $tagAdditions); } // add all the necessary variables to the form $parser->set($this, CRM_IMPORT_PARSER_MODE_IMPORT); // check if there is any error occured $errorStack =& CRM_Core_Error::singleton(); $errors = $errorStack->getErrors(); $errorMessage = array(); $config =& CRM_Core_Config::singleton(); if (is_array($errors)) { foreach ($errors as $key => $value) { $errorMessage[] = $value['message']; } $errorFile = $fileName . '.error.log'; if ($fd = fopen($errorFile, 'w')) { fwrite($fd, implode('\\n', $errorMessage)); } fclose($fd); $this->set('errorFile', $errorFile); $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', 'type=1')); $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', 'type=2')); $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', 'type=4')); } }
/** * Function to actually build the form * * @return void * @access 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); $defaults['field_name'] = array($defaults['field_type'], $defaults['field_name'], $defaults['location_type_id'], $defaults['phone_type']); $this->_gid = $defaults['uf_group_id']; } else { $defaults['is_active'] = 1; } if ($this->_action & CRM_CORE_ACTION_ADD) { $uf =& new CRM_Core_DAO(); $sql = "SELECT weight FROM civicrm_uf_field WHERE uf_group_id = " . $this->_gid . " ORDER BY weight DESC LIMIT 0, 1"; $uf->query($sql); while ($uf->fetch()) { $defaults['weight'] = $uf->weight + 1; } if (empty($defaults['weight'])) { $defaults['weight'] = 1; } } // 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 = array(); $fields['Individual'] =& CRM_Contact_BAO_Contact::exportableFields('Individual'); $fields['Household'] =& CRM_Contact_BAO_Contact::exportableFields('Household'); $fields['Organization'] =& CRM_Contact_BAO_Contact::exportableFields('Organization'); $contribFields =& CRM_Contribute_BAO_Contribution::getContributionFields(); if (!empty($contribFields)) { $fields['Contribution'] =& $contribFields; } foreach ($fields as $key => $value) { foreach ($value as $key1 => $value1) { $this->_mapperFields[$key][$key1] = $value1['title']; $hasLocationTypes[$key][$key1] = $value1['hasLocationType']; } } require_once 'CRM/Core/BAO/LocationType.php'; $this->_location_types =& CRM_Core_PseudoConstant::locationType(); $defaultLocationType =& CRM_Core_BAO_LocationType::getDefault(); /* 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; } $sel1 = array('' => '-select-') + CRM_Core_SelectValues::contactType(); if (!empty($contribFields)) { $sel1['Contribution'] = 'Contributions'; } foreach ($sel1 as $key => $sel) { if ($key) { $sel2[$key] = $this->_mapperFields[$key]; } } $sel3[''] = null; $phoneTypes = CRM_Core_SelectValues::phoneType(); foreach ($sel1 as $k => $sel) { if ($k) { foreach ($this->_location_types as $key => $value) { $sel4[$k]['phone'][$key] =& $phoneTypes; } } } foreach ($sel1 as $k => $sel) { if ($k) { foreach ($this->_mapperFields[$k] as $key => $value) { if ($hasLocationTypes[$k][$key]) { $sel3[$k][$key] = $this->_location_types; } else { $sel3[$key] = null; } } } } $this->_defaults = array(); $js = "<script type='text/javascript'>\n"; $formName = "document.{$this->_name}"; $sel =& $this->addElement('hierselect', "field_name", ts('Field Name'), 'onclick="showLabel();"'); $formValues = array(); //$formValues = $this->controller->exportValues( $this->_name ); $formValues = $_POST; // using $_POST since export values don't give values on first submit if (empty($formValues)) { for ($k = 1; $k < 4; $k++) { if (!$defaults['field_name'][$k]) { $js .= "{$formName}['field_name[{$k}]'].style.display = 'none';\n"; } } } else { foreach ($formValues['field_name'] as $value) { for ($k = 1; $k < 4; $k++) { if (!$formValues['field_name'][$k]) { $js .= "{$formName}['field_name[{$k}]'].style.display = 'none';\n"; } } } } $sel->setOptions(array($sel1, $sel2, $sel3, $sel4)); $js .= "</script>\n"; $this->assign('initHideBoxes', $js); $this->add('select', 'visibility', ts('Visibility'), CRM_Core_SelectValues::ufVisibility(), true); // should the field appear in selector? $this->add('checkbox', 'in_selector', ts('In Selector?')); // weight $this->add('text', 'weight', ts('Weight'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFField', 'weight'), true); $this->addRule('weight', ts(' is a numeric field'), 'numeric'); $this->add('textarea', 'help_post', ts('Field Help'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFField', 'help_post')); // listings title $this->add('text', 'listings_title', ts('Listings Title'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFField', 'listings_title')); $this->addRule('listings_title', ts('Please enter a valid title for this field when displayed in user listings.'), 'title'); $this->add('checkbox', 'is_required', ts('Required?')); $this->add('checkbox', 'is_active', ts('Active?')); $this->add('checkbox', 'is_searchable', ts('Searchable?')); $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'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFField', 'label')); // add buttons $this->addButtons(array(array('type' => 'next', 'name' => ts('Save'), 'isDefault' => true), array('type' => 'cancel', 'name' => ts('Cancel')))); $this->addFormRule(array('CRM_UF_Form_Field', 'formRule')); // 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); }
/** * Function to actually build the form * * @return void * @access public */ function buildQuickForm() { //get the saved mapping details $mappingDAO =& new CRM_Core_DAO_Mapping(); $mappingDAO->domain_id = CRM_Core_Config::domainID(); $mappingDAO->mapping_type = 'Import'; $mappingDAO->find(); $mappingArray = array(); while ($mappingDAO->fetch()) { $mappingArray[$mappingDAO->id] = $mappingDAO->name; } $this->assign('savedMapping', $mappingArray); $this->add('select', 'savedMapping', ts('Mapping Option'), array('' => ts('- select -')) + $mappingArray); $this->addElement('submit', 'loadMapping', ts('Load Mapping'), null, array('onclick' => 'checkSelect()')); //to save the current mappings if (!$this->get('savedMapping')) { $saveDetailsName = ts('Save this field mapping'); $this->add('text', 'saveMappingName', ts('Name')); $this->add('text', 'saveMappingDesc', ts('Description')); } else { $savedMapping = $this->get('savedMapping'); //mapping is to be loaded from database $mapping =& new CRM_Core_DAO_MappingField(); $mapping->mapping_id = $savedMapping; $mapping->orderBy('column_number'); $mapping->find(); $mappingName = array(); $mappingLocation = array(); $mappingContactType = array(); $mappingPhoneType = array(); $mappingRelation = array(); while ($mapping->fetch()) { $mappingName[$mapping->column_number] = $mapping->name; $mappingContactType[] = $mapping->contact_type; if (!empty($mapping->location_type_id)) { $mappingLocation[$mapping->column_number] = $mapping->location_type_id; } if (!empty($mapping->phone_type)) { $mappingPhoneType[$mapping->column_number] = $mapping->phone_type; } if (!empty($mapping->relationship_type_id)) { $mappingRelation[$mapping->column_number] = $mapping->relationship_type_id; } } $this->assign('loadedMapping', $savedMapping); $getMappingName =& new CRM_Core_DAO_Mapping(); $getMappingName->id = $savedMapping; $getMappingName->mapping_type = 'Import'; $getMappingName->find(); while ($getMappingName->fetch()) { $mapperName = $getMappingName->name; } $this->assign('savedName', $mapperName); $this->add('hidden', 'mappingId', $savedMapping); $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), null); $saveDetailsName = ts('Save as a new field mapping'); $this->add('text', 'saveMappingName', ts('Name')); $this->add('text', 'saveMappingDesc', ts('Description')); } $this->addElement('checkbox', 'saveMapping', $saveDetailsName, null, array('onclick' => "showSaveDetails(this)")); $this->addFormRule(array('CRM_Import_Form_MapField', 'formRule')); //-------- end of saved mapping stuff --------- $defaults = array(); $mapperKeys = array_keys($this->_mapperFields); $hasHeaders = !empty($this->_columnHeaders); $headerPatterns = $this->get('headerPatterns'); $dataPatterns = $this->get('dataPatterns'); $hasLocationTypes = $this->get('fieldTypes'); $this->_location_types =& CRM_Core_PseudoConstant::locationType(); $defaultLocationType =& CRM_Core_BAO_LocationType::getDefault(); /* 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; } /* Initialize all field usages to false */ foreach ($mapperKeys as $key) { $this->_fieldUsed[$key] = false; } $sel1 = $this->_mapperFields; $sel2[''] = null; $phoneTypes = CRM_Core_SelectValues::phoneType(); foreach ($this->_location_types as $key => $value) { $sel3['phone'][$key] =& $phoneTypes; } foreach ($mapperKeys as $key) { list($id, $first, $second) = explode('_', $key); if ($first == 'a' && $second == 'b' || $first == 'b' && $second == 'a') { $contactRelation =& new CRM_Contact_DAO_RelationshipType(); $contactRelation->id = $id; $contactRelation->find(true); eval('$cType = $contactRelation->contact_type_' . $second . ';'); if (!$cType) { $cType = 'All'; } $relatedFields = array(); require_once 'CRM/Contact/BAO/Contact.php'; $relatedFields =& CRM_Contact_BAO_Contact::importableFields($cType); unset($relatedFields['']); $values = array(); foreach ($relatedFields as $name => $field) { $values[$name] = $field['title']; if ($hasLocationTypes[$name]) { $sel3[$key][$name] = $this->_location_types; } else { $sel3[$name] = null; } } $sel2[$key] = $values; foreach ($this->_location_types as $k => $value) { $sel4[$key]['phone'][$k] =& $phoneTypes; } } else { if ($hasLocationTypes[$key]) { $sel2[$key] = $this->_location_types; } else { $sel2[$key] = null; } } } $js = "<script type='text/javascript'>\n"; $formName = 'document.forms.' . $this->_name; //used to warn for mismatch column count or mismatch mapping $warning = 0; for ($i = 0; $i < $this->_columnCount; $i++) { $sel =& $this->addElement('hierselect', "mapper[{$i}]", ts('Mapper for Field %1', array(1 => $i)), null); $jsSet = false; if ($this->get('savedMapping')) { if (isset($mappingName[$i])) { if ($mappingName[$i] != ts('- do not import -')) { if (isset($mappingRelation[$i])) { // relationship mapping switch ($this->get('contactType')) { case CRM_IMPORT_PARSER_CONTACT_INDIVIDUAL: $contactType = 'Individual'; break; case CRM_IMPORT_PARSER_CONTACT_HOUSEHOLD: $contactType = 'Household'; break; case CRM_IMPORT_PARSER_CONTACT_ORGANIZATION: $contactType = 'Organization'; } $relations = CRM_Contact_BAO_Relationship::getContactRelationshipType(null, null, null, $contactType); foreach ($relations as $key => $var) { list($type) = explode('_', $key); if ($type == $mappingRelation[$i]) { $relation = $key; break; } } $contactDetails = strtolower(str_replace(" ", "_", $mappingName[$i])); $locationId = isset($mappingLocation[$i]) ? $mappingLocation[$i] : 0; $phoneType = isset($mappingPhoneType[$i]) ? $mappingPhoneType[$i] : null; $defaults["mapper[{$i}]"] = array($relation, $contactDetails, $locationId, $phoneType); if (!$contactDetails) { $js .= "{$formName}['mapper[{$i}][1]'].style.display = 'none';\n"; } if (!$locationId) { $js .= "{$formName}['mapper[{$i}][2]'].style.display = 'none';\n"; } if (!$phoneType) { $js .= "{$formName}['mapper[{$i}][3]'].style.display = 'none';\n"; } //$js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n"; $jsSet = true; } else { $mappingHeader = array_keys($this->_mapperFields, $mappingName[$i]); $locationId = isset($mappingLocation[$i]) ? $mappingLocation[$i] : 0; $phoneType = isset($mappingPhoneType[$i]) ? $mappingPhoneType[$i] : null; if (!$locationId) { $js .= "{$formName}['mapper[{$i}][1]'].style.display = 'none';\n"; } if (!$phoneType) { $js .= "{$formName}['mapper[{$i}][2]'].style.display = 'none';\n"; } $js .= "{$formName}['mapper[{$i}][3]'].style.display = 'none';\n"; $defaults["mapper[{$i}]"] = array($mappingHeader[0], $locationId, $phoneType); $jsSet = true; } } else { $defaults["mapper[{$i}]"] = array(); } if (!$jsSet) { for ($k = 1; $k < 4; $k++) { $js .= "{$formName}['mapper[{$i}][{$k}]'].style.display = 'none';\n"; } } } else { // this load section to help mapping if we ran out of saved columns when doing Load Mapping $js .= "swapOptions({$formName}, 'mapper[{$i}]', 0, 3, 'hs_mapper_" . $i . "_');\n"; if ($hasHeaders) { $defaults["mapper[{$i}]"] = array($this->defaultFromHeader($this->_columnHeaders[$i], $headerPatterns)); } else { $defaults["mapper[{$i}]"] = array($this->defaultFromData($dataPatterns, $i)); } } //end of load mapping } else { $js .= "swapOptions({$formName}, 'mapper[{$i}]', 0, 3, 'hs_mapper_" . $i . "_');\n"; if ($hasHeaders) { // Infer the default from the skipped headers if we have them $defaults["mapper[{$i}]"] = array($this->defaultFromHeader($this->_columnHeaders[$i], $headerPatterns), 0); } else { // Otherwise guess the default from the form of the data $defaults["mapper[{$i}]"] = array($this->defaultFromData($dataPatterns, $i), 0); } } $sel->setOptions(array($sel1, $sel2, $sel3, $sel4)); } $js .= "</script>\n"; $this->assign('initHideBoxes', $js); //set warning if mismatch in more than if ($this->_columnCount != count($mappingName)) { $warning++; } if ($warning != 0 && $this->get('savedMapping')) { $session =& CRM_Core_Session::singleton(); $session->setStatus(ts('The data columns in this import file appear to be different from the saved mapping. Please verify that you have selected the correct saved mapping before continuing.')); } else { $session =& CRM_Core_Session::singleton(); $session->setStatus(null); } $this->setDefaults($defaults); $this->addButtons(array(array('type' => 'back', 'name' => ts('<< Previous')), array('type' => 'next', 'name' => ts('Continue >>'), 'spacing' => ' ', 'isDefault' => true), array('type' => 'cancel', 'name' => ts('Cancel')))); }
function buildQuickForm() { //get the saved mapping details require_once 'CRM/Core/DAO/Mapping.php'; require_once 'CRM/Contact/BAO/Contact.php'; require_once 'CRM/Core/BAO/LocationType.php'; $mappingDAO =& new CRM_Core_DAO_Mapping(); $mappingDAO->domain_id = CRM_Core_Config::domainID(); $mappingDAO->mapping_type = 'Export'; $mappingDAO->find(); $mappingArray = array(); while ($mappingDAO->fetch()) { $mappingArray[$mappingDAO->id] = $mappingDAO->name; } if (!empty($mappingArray)) { $this->assign('savedMapping', $mappingArray); $this->add('select', 'savedMapping', ts('Mapping Option'), array('' => '-select-') + $mappingArray); //if ( !isset($this->_loadedMappingId) ) { //$this->addRule('savedMapping',ts('Please select saved mappings.'), 'required'); //} $this->addElement('submit', 'loadMapping', ts('Load Mapping'), array('class' => 'form-submit')); } //to save the current mappings if (!isset($this->_loadedMappingId)) { $saveDetailsName = ts('Save this field mapping'); $this->add('text', 'saveMappingName', ts('Name')); $this->add('text', 'saveMappingDesc', ts('Description')); } else { //mapping is to be loaded from database $mapping =& new CRM_Core_DAO_MappingField(); $mapping->mapping_id = $this->_loadedMappingId; $mapping->orderBy('column_number'); $mapping->find(); $mappingName = array(); $mappingLocation = array(); $mappingContactType = array(); $mappingPhoneType = array(); while ($mapping->fetch()) { if ($mapping->name) { $mappingName[] = $mapping->name; } if ($mapping->contact_type) { $mappingContactType[] = $mapping->contact_type; } if (!empty($mapping->location_type_id)) { $mappingLocation[$mapping->column_number] = $mapping->location_type_id; } if (!empty($mapping->phone_type)) { $mappingPhoneType[$mapping->column_number] = $mapping->phone_type; } } $this->assign('loadedMapping', $this->_loadedMappingId); $getMappingName =& new CRM_Core_DAO_Mapping(); $getMappingName->id = $savedMapping; $getMappingName->mapping_type = 'Export'; $getMappingName->find(); while ($getMappingName->fetch()) { $mapperName = $getMappingName->name; } $this->assign('savedName', $mapperName); $this->add('hidden', 'mappingId', $this->_loadedMappingId); $this->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), null); $saveDetailsName = ts('Save as a new field mapping'); $this->add('text', 'saveMappingName', ts('Name')); $this->add('text', 'saveMappingDesc', ts('Description')); } $this->addElement('checkbox', 'saveMapping', $saveDetailsName, null, array('onclick' => "showSaveDetails(this)")); $this->addFormRule(array('CRM_Contact_Form_Task_Export_Map', 'formRule')); //-------- end of saved mapping stuff --------- $this->_defaults = array(); $hasLocationTypes = array(); $contactId = array(); $fields = array(); $fields['Individual'] =& CRM_Contact_BAO_Contact::exportableFields('Individual', false, true); $fields['Household'] =& CRM_Contact_BAO_Contact::exportableFields('Household', false, true); $fields['Organization'] =& CRM_Contact_BAO_Contact::exportableFields('Organization', false, true); foreach ($fields as $key => $value) { foreach ($value as $key1 => $value1) { $this->_mapperFields[$key][$key1] = $value1['title']; $hasLocationTypes[$key][$key1] = $value1['hasLocationType']; } } $mapperKeys = array_keys($this->_mapperFields); $this->_location_types =& CRM_Core_PseudoConstant::locationType(); $defaultLocationType =& CRM_Core_BAO_LocationType::getDefault(); /* 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; } $sel1 = array('' => '-select-') + CRM_Core_SelectValues::contactType(); foreach ($sel1 as $key => $sel) { if ($key) { $sel2[$key] = $this->_mapperFields[$key]; } } $sel3[''] = null; $phoneTypes = CRM_Core_SelectValues::phoneType(); foreach ($sel1 as $k => $sel) { if ($k) { foreach ($this->_location_types as $key => $value) { $sel4[$k]['phone'][$key] =& $phoneTypes; } } } foreach ($sel1 as $k => $sel) { if ($k) { foreach ($this->_mapperFields[$k] as $key => $value) { if ($hasLocationTypes[$k][$key]) { $sel3[$k][$key] = $this->_location_types; } else { $sel3[$key] = null; } } } } // print_r($sel3); $this->_defaults = array(); $js = "<script type='text/javascript'>\n"; $formName = "document.{$this->_name}"; //used to warn for mismatch column count or mismatch mapping $warning = 0; for ($i = 0; $i < $this->_columnCount; $i++) { $sel =& $this->addElement('hierselect', "mapper[{$i}]", ts('Mapper for Field %1', array(1 => $i)), null); $jsSet = false; if (isset($this->_loadedMappingId)) { $locationId = isset($mappingLocation[$i]) ? $mappingLocation[$i] : 0; if (isset($mappingName[$i])) { if (is_array($this->_mapperFields[$mappingContactType[$i]])) { $phoneType = isset($mappingPhoneType[$i]) ? $mappingPhoneType[$i] : null; $mappingHeader = array_keys($this->_mapperFields[$mappingContactType[$i]], $mappingName[$i]); $defaults["mapper[{$i}]"] = array($mappingContactType[$i], $mappingHeader[0], $locationId, $phoneType); if (!$mappingHeader[0]) { $js .= "{$formName}['mapper[{$i}][1]'].style.display = 'none';\n"; } if (!$locationId) { $js .= "{$formName}['mapper[{$i}][2]'].style.display = 'none';\n"; } if (!$phoneType) { $js .= "{$formName}['mapper[{$i}][3]'].style.display = 'none';\n"; } $jsSet = true; } } } //$formValues = $this->controller->exportValues( $this->_name ); $formValues = $_POST; // using $_POST since export values don't give values on first submit /* if ( ! $jsSet && empty( $formValues ) ) { for ( $k = 1; $k < 4; $k++ ) { $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n"; } } */ if (!$jsSet) { if (empty($formValues)) { for ($k = 1; $k < 4; $k++) { $js .= "{$formName}['mapper[{$i}][{$k}]'].style.display = 'none';\n"; } } else { foreach ($formValues['mapper'] as $value) { for ($k = 1; $k < 4; $k++) { if (!$formValues['mapper'][$i][$k]) { $js .= "{$formName}['mapper[{$i}][{$k}]'].style.display = 'none';\n"; } } } } } $sel->setOptions(array($sel1, $sel2, $sel3, $sel4)); //set the defaults on load mapping } $js .= "</script>\n"; $this->assign('initHideBoxes', $js); $this->assign('columnCount', $this->_columnCount); $this->setDefaults($defaults); $this->addElement('submit', 'addMore', ts('Select more fields'), array('class' => 'form-submit')); $this->setDefaultAction('refresh'); $this->addButtons(array(array('type' => 'back', 'name' => ts('<< Previous')), array('type' => 'next', 'name' => ts('Export >>'), 'spacing' => ' '), array('type' => 'cancel', 'name' => ts('Done')))); }