Ejemplo n.º 1
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);
     }
 }
Ejemplo n.º 2
0
 /**
  * Function to actually build the form
  *
  * @return void
  * @access public
  */
 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 = array('street_address', 'supplemental_address_1', 'supplemental_address_2', 'city', 'postal_code', 'postal_code_suffix', 'geo_code_1', 'geo_code_2', 'state_province', 'country', 'county', 'phone', 'email', 'im', 'address_name');
         if (!$defaults['location_type_id'] && in_array($defaults['field_name'], $specialFields)) {
             $defaults['location_type_id'] = 0;
         }
         $defaults['field_name'] = array($defaults['field_type'], $defaults['field_name'], $defaults['location_type_id'], CRM_Utils_Array::value('phone_type_id', $defaults));
         $this->_gid = $defaults['uf_group_id'];
     } else {
         $defaults['is_active'] = 1;
     }
     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 = array();
     $fields['Individual'] =& CRM_Contact_BAO_Contact::importableFields('Individual', false, false, true);
     $fields['Household'] =& CRM_Contact_BAO_Contact::importableFields('Household', false, false, true);
     $fields['Organization'] =& CRM_Contact_BAO_Contact::importableFields('Organization', false, false, true);
     // add current employer for individuals
     $fields['Individual']['current_employer'] = array('name' => 'organization_name', 'title' => ts('Current Employer'));
     // unset unwanted fields
     $unsetFieldArray = array('note', 'email_greeting_custom', 'postal_greeting_custom', 'addressee_custom', 'id');
     foreach ($unsetFieldArray as $value) {
         unset($fields['Individual'][$value]);
         unset($fields['Household'][$value]);
         unset($fields['Organization'][$value]);
     }
     require_once 'CRM/Core/BAO/Preferences.php';
     $addressOptions = CRM_Core_BAO_Preferences::valueOptions('address_options', true, null, true);
     if (!$addressOptions['county']) {
         unset($fields['Individual']['county']);
         unset($fields['Household']['county']);
         unset($fields['Organization']['county']);
     }
     //build the common contact fields array CRM-3037.
     foreach ($fields['Individual'] as $key => $value) {
         if (CRM_Utils_Array::value($key, $fields['Household']) && CRM_Utils_Array::value($key, $fields['Organization'])) {
             $fields['Contact'][$key] = $value;
             //as we move common fields to contacts. There fore these fields
             //are unset from resoective array's.
             unset($fields['Individual'][$key]);
             unset($fields['Household'][$key]);
             unset($fields['Organization'][$key]);
         }
     }
     // add current employer for individuals
     $fields['Contact']['id'] = array('name' => 'id', 'title' => ts('Internal Contact ID'));
     unset($fields['Contact']['contact_type']);
     // 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['']);
     // include Subtypes For Profile
     $subTypes = CRM_Contact_BAO_ContactType::subTypeInfo();
     foreach ($subTypes as $name => $val) {
         //custom fields for sub type
         $subTypeFields = CRM_Core_BAO_CustomField::getFieldsForImport($name);
         if (array_key_exists($val['parent'], $fields)) {
             $fields[$name] = $fields[$val['parent']] + $subTypeFields;
         } else {
             $fields[$name] = $subTypeFields;
         }
     }
     unset($subTypes);
     if (CRM_Core_Permission::access('Quest')) {
         require_once 'CRM/Quest/BAO/Student.php';
         $fields['Student'] =& CRM_Quest_BAO_Student::exportableFields();
     }
     if (CRM_Core_Permission::access('CiviContribute')) {
         $contribFields =& CRM_Contribute_BAO_Contribution::getContributionFields();
         if (!empty($contribFields)) {
             unset($contribFields['is_test']);
             unset($contribFields['is_pay_later']);
             unset($contribFields['contribution_id']);
             $fields['Contribution'] =& $contribFields;
         }
     }
     if (CRM_Core_Permission::access('CiviEvent')) {
         require_once 'CRM/Event/BAO/Query.php';
         $participantFields =& CRM_Event_BAO_Query::getParticipantFields(true);
         if (!empty($participantFields)) {
             unset($participantFields['external_identifier']);
             unset($participantFields['event_id']);
             unset($participantFields['participant_contact_id']);
             unset($participantFields['participant_is_test']);
             unset($participantFields['participant_fee_level']);
             unset($participantFields['participant_id']);
             unset($participantFields['participant_is_pay_later']);
             $fields['Participant'] =& $participantFields;
         }
     }
     if (CRM_Core_Permission::access('CiviMember')) {
         require_once 'CRM/Member/BAO/Membership.php';
         $membershipFields =& CRM_Member_BAO_Membership::getMembershipFields();
         unset($membershipFields['membership_id']);
         unset($membershipFields['join_date']);
         unset($membershipFields['membership_start_date']);
         unset($membershipFields['membership_type_id']);
         unset($membershipFields['membership_end_date']);
         unset($membershipFields['member_is_test']);
         unset($membershipFields['is_override']);
         unset($membershipFields['status_id']);
         unset($membershipFields['member_is_pay_later']);
         $fields['Membership'] =& $membershipFields;
     }
     $noSearchable = array();
     foreach ($fields as $key => $value) {
         foreach ($value as $key1 => $value1) {
             //CRM-2676, replacing the conflict for same custom field name from different custom group.
             require_once 'CRM/Core/BAO/CustomField.php';
             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;
             } else {
                 $this->_mapperFields[$key][$key1] = $value1['title'];
             }
             $hasLocationTypes[$key][$key1] = CRM_Utils_Array::value('hasLocationType', $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);
     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;
     }
     $this->_location_types = array('Primary') + $this->_location_types;
     $contactTypes = !empty($contactTypes) ? array('Contact' => 'Contacts') + $contactTypes : array();
     $sel1 = array('' => '- select -') + $contactTypes;
     if (CRM_Core_Permission::access('Quest')) {
         $sel1['Student'] = 'Students';
     }
     if (CRM_Core_Permission::access('CiviEvent')) {
         $sel1['Participant'] = 'Participants';
     }
     if (!empty($contribFields)) {
         $sel1['Contribution'] = 'Contributions';
     }
     if (!empty($membershipFields)) {
         $sel1['Membership'] = 'Membership';
     }
     foreach ($sel1 as $key => $sel) {
         if ($key) {
             $sel2[$key] = $this->_mapperFields[$key];
         }
     }
     $sel3[''] = null;
     $phoneTypes = CRM_Core_PseudoConstant::phoneType();
     ksort($phoneTypes);
     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) {
             if (is_array($this->_mapperFields[$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}";
     $alreadyMixProfile = false;
     if (CRM_Core_BAO_UFField::checkProfileType($this->_gid)) {
         $alreadyMixProfile = true;
     }
     $this->assign('alreadyMixProfile', $alreadyMixProfile);
     $attributes = array('onclick' => "showLabel();mixProfile();", 'onblur' => 'showLabel();mixProfile();');
     $sel =& $this->addElement('hierselect', "field_name", ts('Field Name'), $attributes);
     $formValues = array();
     $formValues = $this->exportValues();
     if (empty($formValues)) {
         for ($k = 1; $k < 4; $k++) {
             if (!$defaults['field_name'][$k]) {
                 $js .= "{$formName}['field_name[{$k}]'].style.display = 'none';\n";
             }
         }
     } else {
         if (!empty($formValues['field_name'])) {
             foreach ($formValues['field_name'] as $value) {
                 for ($k = 1; $k < 4; $k++) {
                     if (!isset($formValues['field_name'][$k]) || !$formValues['field_name'][$k]) {
                         $js .= "{$formName}['field_name[{$k}]'].style.display = 'none';\n";
                     } else {
                         $js .= "{$formName}['field_name[{$k}]'].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));
     $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('onclick' => "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);
     // weight
     $this->add('text', 'weight', ts('Order'), 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_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'));
     $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);
 }
Ejemplo n.º 3
0
 /**
  * Browse all CiviCRM Profile group fields.
  *
  * @return void
  * @access public
  * @static
  */
 function browse()
 {
     $ufField = array();
     $ufFieldBAO = new CRM_Core_BAO_UFField();
     // fkey is gid
     $ufFieldBAO->uf_group_id = $this->_gid;
     $ufFieldBAO->orderBy('weight', 'field_name');
     $ufFieldBAO->find();
     require_once "CRM/Core/BAO/UFField.php";
     $profileType = CRM_Core_BAO_UFField::getProfileType($this->_gid);
     if ($profileType == 'Contribution' || $profileType == 'Membership' || $profileType == 'Activity' || $profileType == 'Participant') {
         $this->assign('skipCreate', true);
     }
     $locationType = array();
     $locationType =& CRM_Core_PseudoConstant::locationType();
     require_once 'CRM/Contact/BAO/Contact.php';
     $fields =& CRM_Contact_BAO_Contact::exportableFields('All', false, true);
     require_once "CRM/Contribute/BAO/Contribution.php";
     $fields = array_merge(CRM_Contribute_BAO_Contribution::getContributionFields(), $fields);
     if (CRM_Core_Permission::access('Quest')) {
         require_once 'CRM/Quest/BAO/Student.php';
         $fields = array_merge(CRM_Quest_BAO_Student::exportableFields(), $fields);
     }
     $select = array();
     foreach ($fields as $name => $field) {
         if ($name) {
             $select[$name] = $field['title'];
         }
     }
     $select['group'] = ts('Group(s)');
     $select['tag'] = ts('Tag(s)');
     while ($ufFieldBAO->fetch()) {
         $ufField[$ufFieldBAO->id] = array();
         $phoneType = $locType = '';
         CRM_Core_DAO::storeValues($ufFieldBAO, $ufField[$ufFieldBAO->id]);
         CRM_Core_DAO_UFField::addDisplayEnums($ufField[$ufFieldBAO->id]);
         $ufField[$ufFieldBAO->id]['label'] = $ufFieldBAO->label;
         $action = array_sum(array_keys($this->actionLinks()));
         if ($ufFieldBAO->is_active) {
             $action -= CRM_Core_Action::ENABLE;
         } else {
             $action -= CRM_Core_Action::DISABLE;
         }
         if ($ufFieldBAO->is_reserved) {
             $action -= CRM_Core_Action::UPDATE;
             $action -= CRM_Core_Action::DISABLE;
             $action -= CRM_Core_Action::DELETE;
         }
         $ufField[$ufFieldBAO->id]['order'] = $ufField[$ufFieldBAO->id]['weight'];
         $ufField[$ufFieldBAO->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), $action, array('id' => $ufFieldBAO->id, 'gid' => $this->_gid));
     }
     $returnURL = CRM_Utils_System::url('civicrm/admin/uf/group/field', "reset=1&action=browse&gid={$this->_gid}");
     $filter = "uf_group_id = {$this->_gid}";
     require_once 'CRM/Utils/Weight.php';
     CRM_Utils_Weight::addOrder($ufField, 'CRM_Core_DAO_UFField', 'id', $returnURL, $filter);
     $this->assign('ufField', $ufField);
     // retrieve showBestResult from session
     $session = CRM_Core_Session::singleton();
     $showBestResult = $session->get('showBestResult');
     $this->assign('showBestResult', $showBestResult);
     $session->set('showBestResult', 0);
 }
Ejemplo n.º 4
0
 /**
  * Function to build the mapping form
  *
  * @params object $form        form object
  * @params string $mappingType mapping type (Export/Import/Search Builder)
  * @params int    $mappingId   mapping id
  * @params mixed  $columnCount column count is int for and array for search builder
  * @params int    $blockCount  block count (no of blocks shown) 
  *
  * @return none
  * @access public
  * @static
  */
 static function buildMappingForm(&$form, $mappingType = 'Export', $mappingId = null, $columnNo, $blockCount = 3, $exportMode = null)
 {
     if ($mappingType == 'Export') {
         $name = "Map";
         $columnCount = array('1' => $columnNo);
     } else {
         if ($mappingType == 'Search Builder') {
             $name = "Builder";
             $columnCount = $columnNo;
         }
     }
     //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';
     if ($mappingType == 'Export') {
         $form->applyFilter('saveMappingName', 'trim');
         //to save the current mappings
         if (!isset($mappingId)) {
             $saveDetailsName = ts('Save this field mapping');
             $form->add('text', 'saveMappingName', ts('Name'));
             $form->add('text', 'saveMappingDesc', ts('Description'));
         } else {
             $form->assign('loadedMapping', $mappingId);
             $params = array('id' => $mappingId);
             $temp = array();
             $mappingDetails = CRM_Core_BAO_Mapping::retrieve($params, $temp);
             $form->assign('savedName', $mappingDetails->name);
             $form->add('hidden', 'mappingId', $mappingId);
             $form->addElement('checkbox', 'updateMapping', ts('Update this field mapping'), null);
             $saveDetailsName = ts('Save as a new field mapping');
             $form->add('text', 'saveMappingName', ts('Name'));
             $form->add('text', 'saveMappingDesc', ts('Description'));
         }
         $form->addElement('checkbox', 'saveMapping', $saveDetailsName, null, array('onclick' => "showSaveDetails(this)"));
         $form->addFormRule(array('CRM_Export_Form_Map', 'formRule'), $form->get('mappingTypeId'));
     } else {
         if ($mappingType == 'Search Builder') {
             $form->addElement('submit', 'addBlock', ts('Also include contacts where'), array('class' => 'submit-link'));
         }
     }
     $defaults = array();
     $hasLocationTypes = array();
     $hasRelationTypes = array();
     $fields = array();
     if ($mappingType == 'Export') {
         $required = true;
     } else {
         if ($mappingType == 'Search Builder') {
             $required = false;
         }
     }
     $contactType = array('Individual', 'Household', 'Organization');
     foreach ($contactType as $value) {
         $relationfields[$value] = $fields[$value] =& CRM_Contact_BAO_Contact::exportableFields($value, false, $required);
         if ($mappingType == 'Export') {
             $relationships = array();
             $relationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(null, null, null, $value);
             asort($relationshipTypes);
             foreach ($relationshipTypes as $key => $var) {
                 list($type) = explode('_', $key);
                 $relationships[$key]['title'] = $var;
                 $relationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
                 $relationships[$key]['export'] = true;
                 $relationships[$key]['relationship_type_id'] = $type;
                 $relationships[$key]['related'] = true;
                 $relationships[$key]['hasRelationType'] = 1;
             }
             if (!empty($relationships)) {
                 $fields[$value] = array_merge($fields[$value], array('related' => array('title' => ts('- related contact info -'))), $relationships);
             }
         }
     }
     //get the current employer for mapping.
     if ($required) {
         $fields['Individual']['current_employer']['title'] = ts('Current Employer');
     }
     // add component fields
     $compArray = array();
     if (CRM_Core_Permission::access('Quest')) {
         require_once 'CRM/Quest/BAO/Student.php';
         $fields['Student'] =& CRM_Quest_BAO_Student::exportableFields();
         $compArray['Student'] = 'Student';
     }
     //we need to unset groups, tags, notes for component export
     require_once 'CRM/Export/Form/Select.php';
     if ($exportMode != CRM_Export_Form_Select::CONTACT_EXPORT) {
         foreach (array('groups', 'tags', 'notes') as $value) {
             unset($fields['Individual'][$value]);
             unset($fields['Household'][$value]);
             unset($fields['Organization'][$value]);
         }
     }
     if ($mappingType == 'Search Builder' || $exportMode == CRM_Export_Form_Select::CONTRIBUTE_EXPORT) {
         if (CRM_Core_Permission::access('CiviContribute')) {
             require_once 'CRM/Contribute/BAO/Contribution.php';
             $fields['Contribution'] =& CRM_Contribute_BAO_Contribution::exportableFields();
             unset($fields['Contribution']['contribution_contact_id']);
             $compArray['Contribution'] = ts('Contribution');
         }
     }
     if ($mappingType == 'Search Builder' || $exportMode == CRM_Export_Form_Select::EVENT_EXPORT) {
         if (CRM_Core_Permission::access('CiviEvent')) {
             require_once 'CRM/Event/BAO/Participant.php';
             $fields['Participant'] =& CRM_Event_BAO_Participant::exportableFields();
             unset($fields['Participant']['participant_contact_id']);
             $compArray['Participant'] = ts('Participant');
         }
     }
     if ($mappingType == 'Search Builder' || $exportMode == CRM_Export_Form_Select::MEMBER_EXPORT) {
         if (CRM_Core_Permission::access('CiviMember')) {
             require_once 'CRM/Member/BAO/Membership.php';
             $fields['Membership'] =& CRM_Member_BAO_Membership::getMembershipFields();
             unset($fields['Membership']['membership_contact_id']);
             $compArray['Membership'] = ts('Membership');
         }
     }
     if ($mappingType == 'Search Builder' || $exportMode == CRM_Export_Form_Select::PLEDGE_EXPORT) {
         if (CRM_Core_Permission::access('CiviPledge')) {
             require_once 'CRM/Pledge/BAO/Pledge.php';
             $fields['Pledge'] =& CRM_Pledge_BAO_Pledge::exportableFields();
             unset($fields['Pledge']['pledge_contact_id']);
             $compArray['Pledge'] = ts('Pledge');
         }
     }
     if ($mappingType == 'Search Builder' || $exportMode == CRM_Export_Form_Select::CASE_EXPORT) {
         if (CRM_Core_Permission::access('CiviCase')) {
             require_once 'CRM/Case/BAO/Case.php';
             $fields['Case'] =& CRM_Case_BAO_Case::exportableFields();
             $compArray['Case'] = ts('Case');
             require_once 'CRM/Activity/BAO/Activity.php';
             $fields['Activity'] =& CRM_Activity_BAO_Activity::exportableFields();
             $compArray['Activity'] = ts('Case Activity');
             unset($fields['Case']['case_contact_id']);
         }
     }
     if ($mappingType == 'Search Builder' || $exportMode == CRM_Export_Form_Select::GRANT_EXPORT) {
         if (CRM_Core_Permission::access('CiviGrant')) {
             require_once 'CRM/Grant/BAO/Grant.php';
             $fields['Grant'] =& CRM_Grant_BAO_Grant::exportableFields();
             unset($fields['Grant']['grant_contact_id']);
             $compArray['Grant'] = ts('Grant');
         }
     }
     //Contact Sub Type For export
     $contactSubTypes = array();
     $subTypes = CRM_Contact_BAO_ContactType::subTypeInfo();
     foreach ($subTypes as $subType => $val) {
         //adding subtype specific relationships CRM-5256
         $csRelationships = array();
         $subTypeRelationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(null, null, null, $val['parent'], false, 'label', true, $subType);
         foreach ($subTypeRelationshipTypes as $key => $var) {
             if (!array_key_exists($key, $fields[$val['parent']])) {
                 list($type) = explode('_', $key);
                 $csRelationships[$key]['title'] = $var;
                 $csRelationships[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
                 $csRelationships[$key]['export'] = true;
                 $csRelationships[$key]['relationship_type_id'] = $type;
                 $csRelationships[$key]['related'] = true;
                 $csRelationships[$key]['hasRelationType'] = 1;
             }
         }
         $fields[$subType] = $fields[$val['parent']] + $csRelationships;
         //custom fields for sub type
         $subTypeFields = CRM_Core_BAO_CustomField::getFieldsForImport($subType);
         $fields[$subType] += $subTypeFields;
         if (!empty($subTypeFields) || !empty($csRelationships)) {
             $contactSubTypes[$subType] = $val['label'];
         }
     }
     unset($subTypes);
     foreach ($fields as $key => $value) {
         foreach ($value as $key1 => $value1) {
             //CRM-2676, replacing the conflict for same custom field name from different custom group.
             $customGroupName = self::getCustomGroupName($key1);
             if ($customGroupName) {
                 $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $customGroupName . ': ' . $value1['title'];
             } else {
                 $relatedMapperFields[$key][$key1] = $mapperFields[$key][$key1] = $value1['title'];
             }
             if (isset($value1['hasLocationType'])) {
                 $hasLocationTypes[$key][$key1] = $value1['hasLocationType'];
             }
             if (isset($value1['hasRelationType'])) {
                 $hasRelationTypes[$key][$key1] = $value1['hasRelationType'];
                 unset($relatedMapperFields[$key][$key1]);
             }
         }
         if (array_key_exists('related', $relatedMapperFields[$key])) {
             unset($relatedMapperFields[$key]['related']);
         }
     }
     $mapperKeys = array_keys($mapperFields);
     $locationTypes =& 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 = $locationTypes[$defaultLocationType->id];
         unset($locationTypes[$defaultLocationType->id]);
         $locationTypes = array($defaultLocationType->id => $defaultLocation) + $locationTypes;
     }
     $locationTypes = array(' ' => ts('Primary')) + $locationTypes;
     // 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);
     $sel1 = array('' => ts('- select record type -')) + $contactTypes + $compArray;
     foreach ($sel1 as $key => $sel) {
         if ($key) {
             $sel2[$key] = $mapperFields[$key];
         }
     }
     $sel3[''] = null;
     $sel5[''] = null;
     $phoneTypes = CRM_Core_PseudoConstant::phoneType();
     $imProviders = CRM_Core_PseudoConstant::IMProvider();
     asort($phoneTypes);
     foreach ($sel1 as $k => $sel) {
         if ($k) {
             foreach ($locationTypes as $key => $value) {
                 if (trim($key) != '') {
                     $sel4[$k]['phone'][$key] =& $phoneTypes;
                     $sel4[$k]['im'][$key] =& $imProviders;
                 }
             }
         }
     }
     foreach ($sel1 as $k => $sel) {
         if ($k) {
             foreach ($mapperFields[$k] as $key => $value) {
                 if (isset($hasLocationTypes[$k][$key])) {
                     $sel3[$k][$key] = $locationTypes;
                 } else {
                     $sel3[$key] = null;
                 }
             }
         }
     }
     //Array for core fields and relationship custom data
     $relationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(null, null, null, null, true);
     if ($mappingType == 'Export') {
         foreach ($sel1 as $k => $sel) {
             if ($k) {
                 foreach ($mapperFields[$k] as $field => $dontCare) {
                     if (isset($hasRelationTypes[$k][$field])) {
                         list($id, $first, $second) = explode('_', $field);
                         $relationshipCustomFields = self::getRelationTypeCustomGroupData($id);
                         asort($relationshipCustomFields);
                         require_once 'CRM/Contact/BAO/RelationshipType.php';
                         $relationshipType =& new CRM_Contact_BAO_RelationshipType();
                         $relationshipType->id = $id;
                         if ($relationshipType->find(true)) {
                             $direction = "contact_sub_type_{$second}";
                             if (isset($relationshipType->{$direction})) {
                                 $relatedFields = array_merge((array) $relatedMapperFields[$relationshipType->{$direction}], (array) $relationshipCustomFields);
                             } else {
                                 $target_type = 'contact_type_' . $second;
                                 $relatedFields = array_merge((array) $relatedMapperFields[$relationshipType->{$target_type}], (array) $relationshipCustomFields);
                             }
                         }
                         $relationshipType->free();
                         $sel5[$k][$field] = $relatedFields;
                     }
                 }
             }
         }
         //Location Type for relationship fields
         foreach ($sel5 as $k => $v) {
             if ($v) {
                 foreach ($v as $rel => $fields) {
                     foreach ($fields as $field => $fieldLabel) {
                         if (isset($hasLocationTypes[$k][$field])) {
                             $sel6[$k][$rel][$field] = $locationTypes;
                         }
                     }
                 }
             }
         }
         //PhoneTypes for  relationship fields
         $sel7[''] = null;
         foreach ($sel6 as $k => $rel) {
             if ($k) {
                 foreach ($rel as $phonekey => $phonevalue) {
                     foreach ($locationTypes as $locType => $loc) {
                         if (trim($locType) != '') {
                             $sel7[$k][$phonekey]['phone'][$locType] =& $phoneTypes;
                             $sel7[$k][$phonekey]['im'][$locType] =& $imProviders;
                         }
                     }
                 }
             }
         }
     }
     //special fields that have location, hack for primary location
     $specialFields = array('street_address', 'supplemental_address_1', 'supplemental_address_2', 'city', 'postal_code', 'postal_code_suffix', 'geo_code_1', 'geo_code_2', 'state_province', 'country', 'phone', 'email', 'im');
     $relationFields = array();
     if (isset($mappingId)) {
         $colCnt = 0;
         list($mappingName, $mappingContactType, $mappingLocation, $mappingPhoneType, $mappingImProvider, $mappingRelation, $mappingOperator, $mappingValue) = CRM_Core_BAO_Mapping::getMappingFields($mappingId);
         $blkCnt = count($mappingName);
         if ($blkCnt >= $blockCount) {
             $blockCount = $blkCnt + 1;
         }
         for ($x = 1; $x < $blockCount; $x++) {
             $colCnt = count($mappingName[$x]);
             if ($colCnt >= $columnCount[$x]) {
                 $columnCount[$x] = $colCnt;
             }
         }
     }
     $defaults = array();
     $noneArray = array();
     $nullArray = array();
     //used to warn for mismatch column count or mismatch mapping
     $warning = 0;
     for ($x = 1; $x < $blockCount; $x++) {
         for ($i = 0; $i < $columnCount[$x]; $i++) {
             $sel =& $form->addElement('hierselect', "mapper[{$x}][{$i}]", ts('Mapper for Field %1', array(1 => $i)), null);
             $jsSet = false;
             if (isset($mappingId)) {
                 $locationId = isset($mappingLocation[$x][$i]) ? $mappingLocation[$x][$i] : 0;
                 if (isset($mappingName[$x][$i])) {
                     if (is_array($mapperFields[$mappingContactType[$x][$i]])) {
                         if (isset($mappingRelation[$x][$i])) {
                             $contactDetails = strtolower(str_replace(" ", "_", $mappingName[$x][$i]));
                             $relLocationId = isset($mappingLocation[$x][$i]) ? $mappingLocation[$x][$i] : 0;
                             if (!$relLocationId && in_array($mappingName[$x][$i], $specialFields)) {
                                 $relLocationId = " ";
                             }
                             $relPhoneType = isset($mappingPhoneType[$x][$i]) ? $mappingPhoneType[$x][$i] : null;
                             $defaults["mapper[{$x}][{$i}]"] = array($mappingContactType[$x][$i], $mappingRelation[$x][$i], $locationId, $phoneType, $mappingName[$x][$i], $relLocationId, $relPhoneType);
                             if (!$locationId) {
                                 $noneArray[] = array($x, $i, 2);
                             }
                             if (!$phoneType && !$imProvider) {
                                 $noneArray[] = array($x, $i, 3);
                             }
                             if (!$mappingName[$x][$i]) {
                                 $noneArray[] = array($x, $i, 4);
                             }
                             if (!$relLocationId) {
                                 $noneArray[] = array($x, $i, 5);
                             }
                             if (!$relPhoneType) {
                                 $noneArray[] = array($x, $i, 6);
                             }
                             $noneArray[] = array($x, $i, 2);
                         } else {
                             $phoneType = isset($mappingPhoneType[$x][$i]) ? $mappingPhoneType[$x][$i] : null;
                             if (!$locationId && in_array($mappingName[$x][$i], $specialFields)) {
                                 $locationId = " ";
                             }
                             $defaults["mapper[{$x}][{$i}]"] = array($mappingContactType[$x][$i], $mappingName[$x][$i], $locationId, $phoneType);
                             if (!$mappingName[$x][$i]) {
                                 $noneArray[] = array($x, $i, 1);
                             }
                             if (!$locationId) {
                                 $noneArray[] = array($x, $i, 2);
                             }
                             if (!$phoneType && !$imProvider) {
                                 $noneArray[] = array($x, $i, 3);
                             }
                             $noneArray[] = array($x, $i, 4);
                             $noneArray[] = array($x, $i, 5);
                             $noneArray[] = array($x, $i, 6);
                         }
                         $jsSet = true;
                         if (CRM_Utils_Array::value($i, $mappingOperator[$x])) {
                             $defaults["operator[{$x}][{$i}]"] = CRM_Utils_Array::value($i, $mappingOperator[$x]);
                         }
                         if (CRM_Utils_Array::value($i, $mappingValue[$x])) {
                             $defaults["value[{$x}][{$i}]"] = CRM_Utils_Array::value($i, $mappingValue[$x]);
                         }
                     }
                 }
             }
             //Fix for Search Builder
             if ($mappingType == 'Export') {
                 $j = 7;
             } else {
                 $j = 4;
             }
             $formValues = $form->exportValues();
             if (!$jsSet) {
                 if (empty($formValues)) {
                     // Incremented length for third select box(relationship type)
                     for ($k = 1; $k < $j; $k++) {
                         $noneArray[] = array($x, $i, $k);
                     }
                 } else {
                     if (!empty($formValues['mapper'][$x])) {
                         foreach ($formValues['mapper'][$x] as $value) {
                             for ($k = 1; $k < $j; $k++) {
                                 if (!isset($formValues['mapper'][$x][$i][$k]) || !$formValues['mapper'][$x][$i][$k]) {
                                     $noneArray[] = array($x, $i, $k);
                                 } else {
                                     $nullArray[] = array($x, $i, $k);
                                 }
                             }
                         }
                     } else {
                         for ($k = 1; $k < $j; $k++) {
                             $noneArray[] = array($x, $i, $k);
                         }
                     }
                 }
             }
             //Fix for Search Builder
             if ($mappingType == 'Export') {
                 if (!isset($mappingId)) {
                     if (array_key_exists($formValues['mapper'][$x][$i][1], $relationshipTypes)) {
                         $sel->setOptions(array($sel1, $sel2, $sel5, $sel6, $sel7, $sel3, $sel4));
                     } else {
                         $sel->setOptions(array($sel1, $sel2, $sel3, $sel4, $sel5, $sel6, $sel7));
                     }
                 } else {
                     $sel->setOptions(array($sel1, $sel2, $sel3, $sel4, $sel5, $sel6, $sel7));
                 }
             } else {
                 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4));
             }
             if ($mappingType == 'Search Builder') {
                 //CRM -2292, restricted array set
                 $operatorArray = array('' => ts('-operator-'), '=' => '=', '!=' => '!=', '>' => '>', '<' => '<', '>=' => '>=', '<=' => '<=', 'IN' => 'IN', 'LIKE' => 'LIKE', 'IS NULL' => 'IS NULL', 'IS NOT NULL' => 'IS NOT NULL');
                 $form->add('select', "operator[{$x}][{$i}]", '', $operatorArray);
                 $form->add('text', "value[{$x}][{$i}]", '');
             }
         }
         //end of columnCnt for
         if ($mappingType == 'Search Builder') {
             $title = ts('Another search field');
         } else {
             $title = ts('Select more fields');
         }
         $form->addElement('submit', "addMore[{$x}]", $title, array('class' => 'submit-link'));
     }
     //end of block for
     $js = "<script type='text/javascript'>\n";
     $formName = "document.{$name}";
     if (!empty($nullArray)) {
         $js .= "var nullArray = [";
         $elements = array();
         $seen = array();
         foreach ($nullArray as $element) {
             $key = "{$element[0]}, {$element[1]}, {$element[2]}";
             if (!isset($seen[$key])) {
                 $elements[] = "[{$key}]";
                 $seen[$key] = 1;
             }
         }
         $js .= implode(', ', $elements);
         $js .= "]";
         $js .= "\nfor(var i=0;i<nullArray.length;i++) {\n  {$formName}['mapper['+nullArray[i][0]+']['+nullArray[i][1]+']['+nullArray[i][2]+']'].style.display = '';\n}\n";
     }
     if (!empty($noneArray)) {
         $js .= "var noneArray = [";
         $elements = array();
         $seen = array();
         foreach ($noneArray as $element) {
             $key = "{$element[0]}, {$element[1]}, {$element[2]}";
             if (!isset($seen[$key])) {
                 $elements[] = "[{$key}]";
                 $seen[$key] = 1;
             }
         }
         $js .= implode(', ', $elements);
         $js .= "]";
         $js .= "\nfor(var i=0;i<noneArray.length;i++) {\n  {$formName}['mapper['+noneArray[i][0]+']['+noneArray[i][1]+']['+noneArray[i][2]+']'].style.display = 'none';  \n}\n";
     }
     $js .= "</script>\n";
     $form->assign('initHideBoxes', $js);
     $form->assign('columnCount', $columnCount);
     $form->assign('blockCount', $blockCount);
     $form->setDefaults($defaults);
     $form->setDefaultAction('refresh');
 }
Ejemplo n.º 5
0
 /**
  * function to add/edit/register contacts through profile.
  *
  * @params  array  $params        Array of profile fields to be edited/added.
  * @params  int    $contactID     contact_id of the contact to be edited/added.
  * @params  array  $fields        array of fields from UFGroup
  * @params  int    $addToGroupID  specifies the default group to which contact is added.
  * @params  int    $ufGroupId     uf group id (profile id)
  * @param   string $ctype         contact type
  *
  * @return  int                   contact id created/edited
  * @static
  * @access public
  */
 static function createProfileContact(&$params, &$fields, $contactID = null, $addToGroupID = null, $ufGroupId = null, $ctype = null, $visibility = false)
 {
     // add ufGroupID to params array ( CRM-2012 )
     if ($ufGroupId) {
         $params['uf_group_id'] = $ufGroupId;
     }
     require_once 'CRM/Utils/Hook.php';
     if ($contactID) {
         $editHook = true;
         CRM_Utils_Hook::pre('edit', 'Profile', $contactID, $params);
     } else {
         $editHook = false;
         CRM_Utils_Hook::pre('create', 'Profile', null, $params);
     }
     $data = $contactDetails = array();
     // get the contact details (hier)
     if ($contactID) {
         list($details, $options) = self::getHierContactDetails($contactID, $fields);
         $contactDetails = $details[$contactID];
         $data['contact_type'] = CRM_Utils_Array::value('contact_type', $contactDetails);
     } else {
         //we should get contact type only if contact
         if ($ufGroupId) {
             require_once "CRM/Core/BAO/UFField.php";
             $data['contact_type'] = CRM_Core_BAO_UFField::getProfileType($ufGroupId);
             //special case to handle profile with only contact fields
             if ($data['contact_type'] == 'Contact') {
                 $data['contact_type'] = 'Individual';
             } else {
                 if (CRM_Contact_BAO_ContactType::isaSubType($data['contact_type'])) {
                     $data['contact_type'] = CRM_Contact_BAO_ContactType::getBasicType($data['contact_type']);
                 }
             }
         } else {
             if ($ctype) {
                 $data['contact_type'] = $ctype;
             } else {
                 $data['contact_type'] = 'Individual';
             }
         }
     }
     //fix contact sub type CRM-5125
     if ($subType = CRM_Utils_Array::value('contact_sub_type', $params)) {
         $data['contact_sub_type'] = $subType;
     } else {
         if ($ufGroupId) {
             $data['contact_sub_type'] = CRM_Core_BAO_UFField::getProfileSubType($ufGroupId, $data['contact_type']);
         }
     }
     if ($ctype == "Organization") {
         $data["organization_name"] = $contactDetails["organization_name"];
     } else {
         if ($ctype == "Household") {
             $data["household_name"] = $contactDetails["household_name"];
         }
     }
     $locationType = array();
     $count = 1;
     if ($contactID) {
         //add contact id
         $data['contact_id'] = $contactID;
         $primaryLocationType = self::getPrimaryLocationType($contactID);
     } else {
         require_once "CRM/Core/BAO/LocationType.php";
         $defaultLocation =& CRM_Core_BAO_LocationType::getDefault();
         $defaultLocationId = $defaultLocation->id;
     }
     // get the billing location type
     $locationTypes =& CRM_Core_PseudoConstant::locationType();
     $billingLocationTypeId = array_search('Billing', $locationTypes);
     $blocks = array('email', 'phone', 'im', 'openid');
     // prevent overwritten of formatted array, reset all block from
     // params if it is not in valid format (since import pass valid format)
     foreach ($blocks as $blk) {
         if (array_key_exists($blk, $params) && !is_array($params[$blk])) {
             unset($params[$blk]);
         }
     }
     $primaryPhoneLoc = null;
     foreach ($params as $key => $value) {
         $fieldName = $locTypeId = $typeId = null;
         list($fieldName, $locTypeId, $typeId) = CRM_Utils_System::explode('-', $key, 3);
         //store original location type id
         $actualLocTypeId = $locTypeId;
         if ($locTypeId == 'Primary') {
             if ($contactID) {
                 $locTypeId = $primaryLocationType;
             } else {
                 $locTypeId = $defaultLocationId;
             }
         }
         if (is_numeric($locTypeId)) {
             $index = $locTypeId;
             if (is_numeric($typeId)) {
                 $index .= '-' . $typeId;
             }
             if (!in_array($index, $locationType)) {
                 $locationType[$count] = $index;
                 $count++;
             }
             require_once 'CRM/Utils/Array.php';
             $loc = CRM_Utils_Array::key($index, $locationType);
             $blockName = 'address';
             if (in_array($fieldName, $blocks)) {
                 $blockName = $fieldName;
             }
             $data[$blockName][$loc]['location_type_id'] = $locTypeId;
             //set is_billing true, for location type "Billing"
             if ($locTypeId == $billingLocationTypeId) {
                 $data[$blockName][$loc]['is_billing'] = 1;
             }
             if ($contactID) {
                 //get the primary location type
                 if ($locTypeId == $primaryLocationType) {
                     $data[$blockName][$loc]['is_primary'] = 1;
                 }
             } else {
                 if (($locTypeId == $defaultLocationId || $locTypeId == $billingLocationTypeId) && ($loc == 1 || !CRM_Utils_Array::retrieveValueRecursive($data['location'][$loc - 1], 'is_primary'))) {
                     $data[$blockName][$loc]['is_primary'] = 1;
                 }
             }
             if ($fieldName == 'phone') {
                 if ($typeId) {
                     $data['phone'][$loc]['phone_type_id'] = $typeId;
                 } else {
                     $data['phone'][$loc]['phone_type_id'] = '';
                 }
                 $data['phone'][$loc]['phone'] = $value;
                 //special case to handle primary phone with different phone types
                 // in this case we make first phone type as primary
                 if (isset($data['phone'][$loc]['is_primary']) && !$primaryPhoneLoc) {
                     $primaryPhoneLoc = $loc;
                 }
                 if ($loc != $primaryPhoneLoc) {
                     unset($data['phone'][$loc]['is_primary']);
                 }
             } else {
                 if ($fieldName == 'email') {
                     $data['email'][$loc]['email'] = $value;
                 } else {
                     if ($fieldName == 'im') {
                         if (isset($params[$key . '-provider_id'])) {
                             $data['im'][$loc]['provider_id'] = $params[$key . '-provider_id'];
                         }
                         $data['im'][$loc]['name'] = $value;
                     } else {
                         if ($fieldName == 'openid') {
                             # $value should be a hash of the OpenID fields
                             foreach ($value as $key => $val) {
                                 $data['openid'][$loc][$key] = $val;
                             }
                         } else {
                             if ($fieldName === 'state_province') {
                                 // CRM-3393
                                 if (is_numeric($value) && (int) $value >= 1000) {
                                     $data['address'][$loc]['state_province_id'] = $value;
                                 } else {
                                     $data['address'][$loc]['state_province'] = $value;
                                 }
                             } else {
                                 if ($fieldName === 'country') {
                                     // CRM-3393
                                     if (is_numeric($value) && (int) $value >= 1000) {
                                         $data['address'][$loc]['country_id'] = $value;
                                     } else {
                                         $data['address'][$loc]['country'] = $value;
                                     }
                                 } else {
                                     if ($fieldName === 'county') {
                                         $data['address'][$loc]['address']['county_id'] = $value;
                                     } else {
                                         if ($fieldName == 'address_name') {
                                             $data['address'][$loc]['name'] = $value;
                                         } else {
                                             $data['address'][$loc][$fieldName] = $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 ($key === 'email_greeting') {
                             //save email/postal greeting and addressee values if any, CRM-4575
                             $data['email_greeting_id'] = $value;
                         } else {
                             if ($key === 'postal_greeting') {
                                 $data['postal_greeting_id'] = $value;
                             } else {
                                 if ($key === 'addressee') {
                                     $data['addressee_id'] = $value;
                                 } else {
                                     if ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($key)) {
                                         // for autocomplete transfer hidden value instead of label
                                         if (isset($params[$key . '_id'])) {
                                             $value = $params[$key . '_id'];
                                         }
                                         $type = CRM_Utils_Array::value('contact_sub_type', $data) ? $data['contact_sub_type'] : $data['contact_type'];
                                         CRM_Core_BAO_CustomField::formatCustomField($customFieldId, $data['custom'], $value, $type, null, $contactID);
                                     } else {
                                         if ($key == 'edit') {
                                             continue;
                                         } else {
                                             if ($key == 'location') {
                                                 foreach ($value as $locationTypeId => $field) {
                                                     foreach ($field as $block => $val) {
                                                         if ($block == 'address' && array_key_exists('address_name', $val)) {
                                                             $value[$locationTypeId][$block]['name'] = $value[$locationTypeId][$block]['address_name'];
                                                         }
                                                     }
                                                 }
                                             }
                                             $data[$key] = $value;
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     // FIX ME: need to check if we need this code
     // //make sure primary location is at first position in location array
     // if ( isset( $data['location'] ) && count( $data['location'] ) > 1 ) {
     //     // if first location is primary skip manipulation
     //     if ( !isset($data['location'][1]['is_primary']) ) {
     //         //find the key for primary location
     //         foreach ( $data['location'] as $primaryLocationKey => $value ) {
     //             if ( isset( $value['is_primary'] ) ) {
     //                 break;
     //             }
     //         }
     //
     //         // swap first location with primary location
     //         $tempLocation        = $data['location'][1];
     //         $data['location'][1] = $data['location'][$primaryLocationKey];
     //         $data['location'][$primaryLocationKey] = $tempLocation;
     //     }
     // }
     if (!isset($data['contact_type'])) {
         $data['contact_type'] = 'Individual';
     }
     if (CRM_Core_Permission::access('Quest')) {
         $studentFieldPresent = 0;
         foreach ($fields as $name => $field) {
             // check if student fields present
             require_once 'CRM/Quest/BAO/Student.php';
             if (!$studentFieldPresent && array_key_exists($name, CRM_Quest_BAO_Student::exportableFields())) {
                 $studentFieldPresent = 1;
             }
         }
     }
     //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, $fields)) {
             if ($params[$key]) {
                 $data[$key] = $params[$key];
             } else {
                 $data[$key] = 0;
             }
         }
     }
     // manage is_opt_out
     if (array_key_exists('is_opt_out', $fields)) {
         $wasOptOut = CRM_Utils_Array::value('is_opt_out', $contactDetails, false);
         $isOptOut = CRM_Utils_Array::value('is_opt_out', $params, false);
         $data['is_opt_out'] = $isOptOut;
         // on change, create new civicrm_subscription_history entry
         if ($wasOptOut != $isOptOut && CRM_Utils_Array::value('contact_id', $contactDetails)) {
             $shParams = array('contact_id' => $contactDetails['contact_id'], 'status' => $isOptOut ? 'Removed' : 'Added', 'method' => 'Web');
             CRM_Contact_BAO_SubscriptionHistory::create($shParams);
         }
     }
     require_once 'CRM/Contact/BAO/Contact.php';
     if ($data['contact_type'] != 'Student') {
         $contact =& self::create($data);
     }
     // contact is null if the profile does not have any contact fields
     if ($contact) {
         $contactID = $contact->id;
     }
     if (!$contactID) {
         CRM_Core_Error::fatal('Cannot proceed without a valid contact id');
     }
     // Process group and tag
     if (CRM_Utils_Array::value('group', $fields)) {
         $method = 'Admin';
         // this for sure means we are coming in via profile since i added it to fix
         // removing contacts from user groups -- lobo
         if ($visibility) {
             $method = 'Web';
         }
         CRM_Contact_BAO_GroupContact::create($params['group'], $contactID, $visibility, $method);
     }
     if (CRM_Utils_Array::value('tag', $fields)) {
         require_once 'CRM/Core/BAO/EntityTag.php';
         CRM_Core_BAO_EntityTag::create($params['tag'], $contactID);
     }
     //to add profile in default group
     if (is_array($addToGroupID)) {
         $contactIds = array($contactID);
         foreach ($addToGroupID as $groupId) {
             CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $groupId);
         }
     } else {
         if ($addToGroupID) {
             $contactIds = array($contactID);
             CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $addToGroupID);
         }
     }
     //to update student record
     if (CRM_Core_Permission::access('Quest') && $studentFieldPresent) {
         $ids = array();
         $dao =& new CRM_Quest_DAO_Student();
         $dao->contact_id = $contactID;
         if ($dao->find(true)) {
             $ids['id'] = $dao->id;
         }
         $ssids = array();
         $studentSummary =& new CRM_Quest_DAO_StudentSummary();
         $studentSummary->contact_id = $contactID;
         if ($studentSummary->find(true)) {
             $ssids['id'] = $studentSummary->id;
         }
         $params['contact_id'] = $contactID;
         //fixed for check boxes
         $specialFields = array('educational_interest', 'college_type', 'college_interest', 'test_tutoring');
         foreach ($specialFields as $field) {
             if ($params[$field]) {
                 $params[$field] = implode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, array_keys($params[$field]));
             }
         }
         CRM_Quest_BAO_Student::create($params, $ids);
         CRM_Quest_BAO_Student::createStudentSummary($params, $ssids);
     }
     // reset the group contact cache for this group
     require_once 'CRM/Contact/BAO/GroupContactCache.php';
     CRM_Contact_BAO_GroupContactCache::remove();
     if ($editHook) {
         CRM_Utils_Hook::post('edit', 'Profile', $contactID, $params);
     } else {
         CRM_Utils_Hook::post('create', 'Profile', $contactID, $params);
     }
     return $contactID;
 }
Ejemplo n.º 6
0
 static function formatProfileContactParams(&$params, &$fields, $contactID = NULL, $ufGroupId = NULL, $ctype = NULL, $skipCustom = FALSE)
 {
     $data = $contactDetails = array();
     // get the contact details (hier)
     if ($contactID) {
         list($details, $options) = self::getHierContactDetails($contactID, $fields);
         $contactDetails = $details[$contactID];
         $data['contact_type'] = CRM_Utils_Array::value('contact_type', $contactDetails);
         $data['contact_sub_type'] = CRM_Utils_Array::value('contact_sub_type', $contactDetails);
     } else {
         //we should get contact type only if contact
         if ($ufGroupId) {
             $data['contact_type'] = CRM_Core_BAO_UFField::getProfileType($ufGroupId);
             //special case to handle profile with only contact fields
             if ($data['contact_type'] == 'Contact') {
                 $data['contact_type'] = 'Individual';
             } elseif (CRM_Contact_BAO_ContactType::isaSubType($data['contact_type'])) {
                 $data['contact_type'] = CRM_Contact_BAO_ContactType::getBasicType($data['contact_type']);
             }
         } elseif ($ctype) {
             $data['contact_type'] = $ctype;
         } else {
             $data['contact_type'] = 'Individual';
         }
     }
     //fix contact sub type CRM-5125
     if (array_key_exists('contact_sub_type', $params) && !empty($params['contact_sub_type'])) {
         $data['contact_sub_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, (array) $params['contact_sub_type']) . CRM_Core_DAO::VALUE_SEPARATOR;
     } elseif (array_key_exists('contact_sub_type_hidden', $params) && !empty($params['contact_sub_type_hidden'])) {
         // if profile was used, and had any subtype, we obtain it from there
         $data['contact_sub_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, (array) $params['contact_sub_type_hidden']) . CRM_Core_DAO::VALUE_SEPARATOR;
     }
     if ($ctype == 'Organization') {
         $data['organization_name'] = CRM_Utils_Array::value('organization_name', $contactDetails);
     } elseif ($ctype == 'Household') {
         $data['household_name'] = CRM_Utils_Array::value('household_name', $contactDetails);
     }
     $locationType = array();
     $count = 1;
     if ($contactID) {
         //add contact id
         $data['contact_id'] = $contactID;
         $primaryLocationType = self::getPrimaryLocationType($contactID);
     } else {
         $defaultLocation = CRM_Core_BAO_LocationType::getDefault();
         $defaultLocationId = $defaultLocation->id;
     }
     // get the billing location type
     $locationTypes = CRM_Core_PseudoConstant::locationType();
     $billingLocationTypeId = array_search('Billing', $locationTypes);
     $blocks = array('email', 'phone', 'im', 'openid');
     $multiplFields = array('url');
     // prevent overwritten of formatted array, reset all block from
     // params if it is not in valid format (since import pass valid format)
     foreach ($blocks as $blk) {
         if (array_key_exists($blk, $params) && !is_array($params[$blk])) {
             unset($params[$blk]);
         }
     }
     $primaryPhoneLoc = NULL;
     foreach ($params as $key => $value) {
         $fieldName = $locTypeId = $typeId = NULL;
         list($fieldName, $locTypeId, $typeId) = CRM_Utils_System::explode('-', $key, 3);
         //store original location type id
         $actualLocTypeId = $locTypeId;
         if ($locTypeId == 'Primary') {
             if ($contactID) {
                 $locTypeId = $primaryLocationType;
             } else {
                 $locTypeId = $defaultLocationId;
             }
         }
         if (is_numeric($locTypeId) && !in_array($fieldName, $multiplFields) && substr($fieldName, 0, 7) != 'custom_') {
             $index = $locTypeId;
             if (is_numeric($typeId)) {
                 $index .= '-' . $typeId;
             }
             if (!in_array($index, $locationType)) {
                 $locationType[$count] = $index;
                 $count++;
             }
             $loc = CRM_Utils_Array::key($index, $locationType);
             $blockName = 'address';
             if (in_array($fieldName, $blocks)) {
                 $blockName = $fieldName;
             }
             $data[$blockName][$loc]['location_type_id'] = $locTypeId;
             //set is_billing true, for location type "Billing"
             if ($locTypeId == $billingLocationTypeId) {
                 $data[$blockName][$loc]['is_billing'] = 1;
             }
             if ($contactID) {
                 //get the primary location type
                 if ($locTypeId == $primaryLocationType) {
                     $data[$blockName][$loc]['is_primary'] = 1;
                 }
             } elseif (($locTypeId == $defaultLocationId || $locTypeId == $billingLocationTypeId) && ($loc == 1 || !CRM_Utils_Array::retrieveValueRecursive($data['location'][$loc - 1], 'is_primary'))) {
                 $data[$blockName][$loc]['is_primary'] = 1;
             }
             if ($fieldName == 'phone') {
                 if ($typeId) {
                     $data['phone'][$loc]['phone_type_id'] = $typeId;
                 } else {
                     $data['phone'][$loc]['phone_type_id'] = '';
                 }
                 $data['phone'][$loc]['phone'] = $value;
                 //special case to handle primary phone with different phone types
                 // in this case we make first phone type as primary
                 if (isset($data['phone'][$loc]['is_primary']) && !$primaryPhoneLoc) {
                     $primaryPhoneLoc = $loc;
                 }
                 if ($loc != $primaryPhoneLoc) {
                     unset($data['phone'][$loc]['is_primary']);
                 }
             } elseif ($fieldName == 'email') {
                 $data['email'][$loc]['email'] = $value;
             } elseif ($fieldName == 'im') {
                 if (isset($params[$key . '-provider_id'])) {
                     $data['im'][$loc]['provider_id'] = $params[$key . '-provider_id'];
                 }
                 if (strpos($key, '-provider_id') !== FALSE) {
                     $data['im'][$loc]['provider_id'] = $params[$key];
                 } else {
                     $data['im'][$loc]['name'] = $value;
                 }
             } elseif ($fieldName == 'openid') {
                 $data['openid'][$loc]['openid'] = $value;
             } else {
                 if ($fieldName === 'state_province') {
                     // CRM-3393
                     if (is_numeric($value) && (int) $value >= 1000) {
                         $data['address'][$loc]['state_province_id'] = $value;
                     } else {
                         $data['address'][$loc]['state_province'] = $value;
                     }
                 } elseif ($fieldName === 'country') {
                     // CRM-3393
                     if (is_numeric($value) && (int) $value >= 1000) {
                         $data['address'][$loc]['country_id'] = $value;
                     } else {
                         $data['address'][$loc]['country'] = $value;
                     }
                 } elseif ($fieldName === 'county') {
                     $data['address'][$loc]['county_id'] = $value;
                 } elseif ($fieldName == 'address_name') {
                     $data['address'][$loc]['name'] = $value;
                 } elseif (substr($fieldName, 0, 14) === 'address_custom') {
                     $data['address'][$loc][substr($fieldName, 8)] = $value;
                 } else {
                     $data['address'][$loc][$fieldName] = $value;
                 }
             }
         } else {
             if (substr($key, 0, 4) === 'url-') {
                 $websiteField = explode('-', $key);
                 if (isset($websiteField[2])) {
                     $data['website'][$websiteField[1]]['website_type_id'] = $value;
                 } else {
                     $data['website'][$websiteField[1]]['url'] = $value;
                 }
             } elseif ($key === 'individual_suffix') {
                 $data['suffix_id'] = $value;
             } elseif ($key === 'individual_prefix') {
                 $data['prefix_id'] = $value;
             } elseif ($key === 'gender') {
                 $data['gender_id'] = $value;
             } elseif (in_array($key, self::$_greetingTypes, TRUE)) {
                 $data[$key . '_id'] = $value;
             } elseif (!$skipCustom && ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($key))) {
                 // for autocomplete transfer hidden value instead of label
                 if ($params[$key] && isset($params[$key . '_id'])) {
                     $value = $params[$key . '_id'];
                 }
                 // we need to append time with date
                 if ($params[$key] && isset($params[$key . '_time'])) {
                     $value .= ' ' . $params[$key . '_time'];
                 }
                 $type = $data['contact_type'];
                 if (CRM_Utils_Array::value('contact_sub_type', $data)) {
                     $type = $data['contact_sub_type'];
                     $type = explode(CRM_Core_DAO::VALUE_SEPARATOR, trim($type, CRM_Core_DAO::VALUE_SEPARATOR));
                     // generally a contact even if, has multiple subtypes the parent-type is going to be one only
                     // and since formatCustomField() would be interested in parent type, lets consider only one subtype
                     // as the results going to be same.
                     $type = $type[0];
                 }
                 CRM_Core_BAO_CustomField::formatCustomField($customFieldId, $data['custom'], $value, $type, NULL, $contactID);
             } elseif ($key == 'edit') {
                 continue;
             } else {
                 if ($key == 'location') {
                     foreach ($value as $locationTypeId => $field) {
                         foreach ($field as $block => $val) {
                             if ($block == 'address' && array_key_exists('address_name', $val)) {
                                 $value[$locationTypeId][$block]['name'] = $value[$locationTypeId][$block]['address_name'];
                             }
                         }
                     }
                 }
                 $data[$key] = $value;
             }
         }
     }
     if (!isset($data['contact_type'])) {
         $data['contact_type'] = 'Individual';
     }
     if (CRM_Core_Permission::access('Quest')) {
         $studentFieldPresent = 0;
         foreach ($fields as $name => $field) {
             // check if student fields present
             if (!$studentFieldPresent && array_key_exists($name, CRM_Quest_BAO_Student::exportableFields())) {
                 $studentFieldPresent = 1;
             }
         }
     }
     //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, $fields)) {
             if (array_key_exists($key, $params)) {
                 $data[$key] = $params[$key];
                 // dont reset it for existing contacts
             } elseif (!$contactID) {
                 $data[$key] = 0;
             }
         }
     }
     return array($data, $contactDetails);
 }