4 * Class CRM_Custom_Import_Form_Preview
6 class CRM_Custom_Import_Form_Preview
extends CRM_Import_Form_Preview
{
7 public $_parser = 'CRM_Custom_Import_Parser_Api';
8 protected $_importParserUrl = '&parser=CRM_Custom_Import_Parser_Api';
11 * Set variables up before form is built.
15 public function preProcess() {
17 //get the data from the session
18 $dataValues = $this->get('dataValues');
19 $mapper = $this->get('mapper');
20 $invalidRowCount = $this->get('invalidRowCount');
21 $conflictRowCount = $this->get('conflictRowCount');
22 $mismatchCount = $this->get('unMatchCount');
23 $entity = $this->get('_entity');
25 //get the mapping name displayed if the mappingId is set
26 $mappingId = $this->get('loadMappingId');
28 $mapDAO = new CRM_Core_DAO_Mapping();
29 $mapDAO->id
= $mappingId;
32 $this->assign('savedMappingName', $mappingId ?
$mapDAO->name
: NULL);
34 if ($invalidRowCount) {
35 $urlParams = 'type=' . CRM_Import_Parser
::ERROR
. $this->_importParserUrl
;
36 $this->set('downloadErrorRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
39 if ($conflictRowCount) {
40 $urlParams = 'type=' . CRM_Import_Parser
::CONFLICT
. $this->_importParserUrl
;
41 $this->set('downloadConflictRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
45 $urlParams = 'type=' . CRM_Import_Parser
::NO_MATCH
. $this->_importParserUrl
;
46 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
57 'downloadErrorRecordsUrl',
58 'downloadConflictRecordsUrl',
59 'downloadMismatchRecordsUrl',
62 foreach ($properties as $property) {
63 $this->assign($property, $this->get($property));
68 * Process the mapped fields and map it into the uploaded file.
69 * preview the file and extract some summary statistics
73 public function postProcess() {
74 $fileName = $this->getSubmittedValue('uploadFile');
75 $invalidRowCount = $this->get('invalidRowCount');
76 $conflictRowCount = $this->get('conflictRowCount');
77 $onDuplicate = $this->get('onDuplicate');
78 $entity = $this->get('_entity');
80 $mapper = $this->controller
->exportValue('MapField', 'mapper');
83 foreach ($mapper as $key => $value) {
84 $mapperKeys[$key] = $mapper[$key][0];
87 $parser = new $this->_parser($mapperKeys);
88 $parser->setEntity($entity);
90 $mapFields = $this->get('fields');
92 foreach ($mapper as $key => $value) {
94 if (isset($mapFields[$mapper[$key][0]])) {
95 $header[] = $mapFields[$mapper[$key][0]];
97 $mapperFields[] = implode(' - ', $header);
99 $parser->run($this->getSubmittedValue('uploadFile'), $this->getSubmittedValue('fieldSeparator'),
101 $this->getSubmittedValue('skipColumnHeader'),
102 CRM_Import_Parser
::MODE_IMPORT
,
103 $this->get('contactType'),
107 // add all the necessary variables to the form
108 $parser->set($this, CRM_Import_Parser
::MODE_IMPORT
);
110 // check if there is any error occurred
112 $errorStack = CRM_Core_Error
::singleton();
113 $errors = $errorStack->getErrors();
116 if (is_array($errors)) {
117 foreach ($errors as $key => $value) {
118 $errorMessage[] = $value['message'];
121 $errorFile = $fileName['name'] . '.error.log';
123 if ($fd = fopen($errorFile, 'w')) {
124 fwrite($fd, implode('\n', $errorMessage));
128 $this->set('errorFile', $errorFile);
129 $urlParams = 'type=' . CRM_Import_Parser
::ERROR
. $this->_importParserUrl
;
130 $this->set('downloadErrorRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
131 $urlParams = 'type=' . CRM_Import_Parser
::CONFLICT
. $this->_importParserUrl
;
132 $this->set('downloadConflictRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
133 $urlParams = 'type=' . CRM_Import_Parser
::NO_MATCH
. $this->_importParserUrl
;
134 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));