/**
  * singleton function used to manage this object
  *
  * @return CRM_Hrjobcontract_ExportImportValuesConverter
  * @static
  */
 static function &singleton()
 {
     if (self::$_singleton === NULL) {
         self::$_singleton = new self();
     }
     return self::$_singleton;
 }
Ejemplo n.º 2
0
 function alterDisplay(&$rows)
 {
     // custom code to alter rows
     $entryFound = FALSE;
     $entities = array('HRJobContract', 'HRJobDetails', 'HRJobHour', 'HRJobHealth', 'HRJobLeave', 'HRJobPay', 'HRJobPension', 'HRJobRole');
     $ei = CRM_Hrjobcontract_ExportImportValuesConverter::singleton();
     $changeReasonOptions = array();
     CRM_Core_OptionGroup::getAssoc('hrjc_revision_change_reason', $changeReasonOptions);
     foreach ($rows as $rowNum => $row) {
         // make count columns point to detail report
         // convert sort name to links
         if (!empty($row['civicrm_contact_sort_name']) && !empty($row['civicrm_hrjobcontract_contact_id'])) {
             $url = CRM_Report_Utils_Report::getNextUrl('contact/detail', 'reset=1&force=1&id_op=eq&id_value=' . $row['civicrm_hrjobcontract_contact_id'], $this->_absoluteUrl, $this->_id, $this->_drilldownReport);
             $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
             $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Constituent Detail Report for this contact.");
             $entryFound = TRUE;
         }
         // Convert revisionable HRJobContract Entities values for export
         foreach ($entities as $entity) {
             $tableName = _civicrm_get_table_name($entity);
             $fields = $this->_columns['civicrm_hrjobcontract_' . $tableName]['fields'];
             foreach ($fields as $key => $value) {
                 $fieldName = substr($key, strlen($tableName) + 1);
                 $rowKey = 'civicrm_hrjobcontract_' . $tableName . '_' . $tableName . '_' . $fieldName;
                 if (isset($row[$rowKey])) {
                     $rows[$rowNum][$rowKey] = $ei->export($tableName, $fieldName, $row[$rowKey]);
                 }
             }
         }
         // Convert non-revisionable HRJobContract Entities values for export
         if (!empty($row['civicrm_hrjobcontract_revision_change_reason'])) {
             $rows[$rowNum]['civicrm_hrjobcontract_revision_change_reason'] = $changeReasonOptions['label'][$rows[$rowNum]['civicrm_hrjobcontract_revision_change_reason']];
             $entryFound = TRUE;
         }
     }
 }
Ejemplo n.º 3
0
 /**
  * handle the values in import mode
  *
  * @param int $onDuplicate the code for what action to take on duplicates
  * @param array $values the array of values belonging to this line
  *
  * @return boolean      the result of this processing
  * @access public
  */
 function import($onDuplicate, &$values)
 {
     $entityNames = array('details', 'hour', 'health', 'leave', 'pay', 'pension', 'role');
     $ei = CRM_Hrjobcontract_ExportImportValuesConverter::singleton();
     $response = $this->summary($values);
     $this->formatDateParams();
     $this->_params['skipRecentView'] = TRUE;
     $this->_params['check_permissions'] = TRUE;
     $params = $this->getActiveFieldParams();
     $formatValues = array();
     foreach ($params as $key => $field) {
         if ($field == NULL || $field === '') {
             continue;
         }
         $formatValues[$key] = $field;
     }
     $importedJobContractId = null;
     if (!empty($params['jobcontract_id'])) {
         $importedJobContractId = (int) $params['jobcontract_id'];
     }
     if (!$importedJobContractId) {
         $importedJobContractId = $this->_jobcontractIdIncremental++;
     }
     if (empty($params['contact_id']) && !empty($params['email'])) {
         $checkEmail = new CRM_Core_BAO_Email();
         $checkEmail->email = $params['email'];
         $checkEmail->find(TRUE);
         if (!empty($checkEmail->contact_id)) {
             $params['contact_id'] = $checkEmail->contact_id;
         }
     }
     if (!empty($formatValues['external_identifier'])) {
         $checkCid = new CRM_Contact_DAO_Contact();
         $checkCid->external_identifier = $formatValues['external_identifier'];
         $checkCid->find(TRUE);
         if (!empty($params['contact_id']) && $params['contact_id'] != $checkCid->id) {
             array_unshift($values, 'Mismatch of External identifier :' . $formatValues['external_identifier'] . ' and Contact Id:' . $params['contact_id']);
             return CRM_Import_Parser::ERROR;
         }
         if (!empty($checkCid->id)) {
             $params['contact_id'] = $checkCid->id;
         }
     }
     if (empty($params['contact_id'])) {
         $error = 'Missing "contact_id" / "email" / "external_identifier" value.';
         array_unshift($values, $error);
         return CRM_Import_Parser::ERROR;
     }
     $revisionParams = $this->getEntityParams('HRJobContractRevision');
     $revisionData = array();
     foreach ($entityNames as $value) {
         if (empty($revisionParams[$value . '_revision_id'])) {
             $revisionParams[$value . '_revision_id'] = $this->_revisionIdIncremental;
         }
         $revisionData[$value] = $revisionParams[$value . '_revision_id'];
     }
     $this->_revisionIdIncremental++;
     if (empty($revisionData)) {
         $error = 'Missing Revision data.';
         array_unshift($values, $error);
         return CRM_Import_Parser::ERROR;
     }
     $revisionId = max($revisionData);
     if (empty($this->_jobContractIds[$importedJobContractId])) {
         try {
             $jobContractCreateResponse = civicrm_api3('HRJobContract', 'create', array('contact_id' => $params['contact_id']));
         } catch (CiviCRM_API3_Exception $e) {
             $error = $e->getMessage();
             array_unshift($values, $error);
             return CRM_Import_Parser::ERROR;
         }
         $this->_jobContractIds[$importedJobContractId] = (int) $jobContractCreateResponse['id'];
         $this->_previousRevision = array();
         foreach ($entityNames as $value) {
             $this->_previousRevision['imported'][$value] = null;
             $this->_previousRevision['local'][$value] = null;
         }
         $this->_previousRevision['imported']['id'] = null;
         $this->_previousRevision['local']['id'] = null;
         $this->_revisionIds = array();
         $this->_revisionEntityMap = array();
     }
     $localJobContractId = $this->_jobContractIds[$importedJobContractId];
     $newRevisionInstance = null;
     if ($this->_previousRevision['imported']['id'] !== $revisionId) {
         // create new Revision:
         $newRevisionParams = $revisionParams;
         unset($newRevisionParams['id']);
         foreach ($entityNames as $value) {
             unset($newRevisionParams[$value . '_revision_id']);
         }
         $newRevisionParams['jobcontract_id'] = $localJobContractId;
         $newRevisionParams = $this->validateFields('HRJobContractRevision', $newRevisionParams);
         $newRevisionInstance = CRM_Hrjobcontract_BAO_HRJobContractRevision::create($newRevisionParams);
         if (!empty($this->_previousRevision['imported']['id'])) {
             foreach ($entityNames as $value) {
                 $field = $value . '_revision_id';
                 $newRevisionInstance->{$field} = $this->_previousRevision['local'][$value];
             }
             $newRevisionInstance->save();
         }
     }
     try {
         foreach ($this->_entity as $entity) {
             if (in_array($entity, array('HRJobContract', 'HRJobContractRevision'))) {
                 continue;
             }
             $entityClass = 'CRM_Hrjobcontract_BAO_' . $entity;
             $tableName = _civicrm_get_table_name($entity);
             if (empty($revisionParams[$tableName . '_revision_id'])) {
                 continue;
             }
             $params = $this->getEntityParams($entity);
             $params['jobcontract_id'] = $localJobContractId;
             foreach ($params as $key => $value) {
                 $params[$key] = $ei->import($tableName, $key, $value);
             }
             $params = $this->validateFields($entity, $params);
             $params['import'] = 1;
             if ($revisionParams[$tableName . '_revision_id'] === $revisionId) {
                 if ($tableName === 'leave' || $this->_previousRevision['imported'][$tableName] !== $revisionId) {
                     if (!empty($newRevisionInstance)) {
                         $params['jobcontract_revision_id'] = $newRevisionInstance->id;
                     } else {
                         $params['jobcontract_revision_id'] = $this->_previousRevision['local'][$tableName];
                     }
                     if ($tableName === 'leave') {
                         foreach ($params['leave_amount'] as $leaveTypeId => $leaveAmount) {
                             $params['leave_type'] = $leaveTypeId;
                             $params['leave_amount'] = $leaveAmount;
                             $entityInstance = $entityClass::create($params);
                         }
                     } else {
                         $entityInstance = $entityClass::create($params);
                     }
                     $this->_previousRevision['local'][$tableName] = $entityInstance->jobcontract_revision_id;
                 }
             }
             $this->_previousRevision['imported'][$tableName] = $revisionParams[$tableName . '_revision_id'];
         }
     } catch (CiviCRM_API3_Exception $e) {
         $error = $e->getMessage();
         array_unshift($values, $error);
         return CRM_Import_Parser::ERROR;
     }
     if (!empty($newRevisionInstance)) {
         foreach ($entityNames as $value) {
             $field = $value . '_revision_id';
             $newRevisionInstance->{$field} = $this->_previousRevision['local'][$value];
         }
         $newRevisionInstance->save();
     }
     $this->_previousRevision['imported']['id'] = $revisionId;
 }