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';
11 * Set variables up before form is built.
15 public function preProcess() {
16 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
18 //get the data from the session
19 $dataValues = $this->get('dataValues');
20 $mapper = $this->get('mapper');
21 $invalidRowCount = $this->get('invalidRowCount');
22 $conflictRowCount = $this->get('conflictRowCount');
23 $mismatchCount = $this->get('unMatchCount');
24 $entity = $this->get('_entity');
26 //get the mapping name displayed if the mappingId is set
27 $mappingId = $this->get('loadMappingId');
29 $mapDAO = new CRM_Core_DAO_Mapping();
30 $mapDAO->id
= $mappingId;
32 $this->assign('loadedMapping', $mappingId);
33 $this->assign('savedName', $mapDAO->name
);
36 if ($skipColumnHeader) {
37 $this->assign('skipColumnHeader', $skipColumnHeader);
38 $this->assign('rowDisplayCount', 3);
41 $this->assign('rowDisplayCount', 2);
44 if ($invalidRowCount) {
45 $urlParams = 'type=' . CRM_Import_Parser
::ERROR
. $this->_importParserUrl
;
46 $this->set('downloadErrorRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
49 if ($conflictRowCount) {
50 $urlParams = 'type=' . CRM_Import_Parser
::CONFLICT
. $this->_importParserUrl
;
51 $this->set('downloadConflictRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
55 $urlParams = 'type=' . CRM_Import_Parser
::NO_MATCH
. $this->_importParserUrl
;
56 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
67 'downloadErrorRecordsUrl',
68 'downloadConflictRecordsUrl',
69 'downloadMismatchRecordsUrl',
72 foreach ($properties as $property) {
73 $this->assign($property, $this->get($property));
78 * Process the mapped fields and map it into the uploaded file.
79 * preview the file and extract some summary statistics
83 public function postProcess() {
84 $fileName = $this->controller
->exportValue('DataSource', 'uploadFile');
85 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
86 $invalidRowCount = $this->get('invalidRowCount');
87 $conflictRowCount = $this->get('conflictRowCount');
88 $onDuplicate = $this->get('onDuplicate');
89 $entity = $this->get('_entity');
91 $config = CRM_Core_Config
::singleton();
92 $separator = $config->fieldSeparator
;
94 $mapper = $this->controller
->exportValue('MapField', 'mapper');
95 $mapperKeys = array();
97 foreach ($mapper as $key => $value) {
98 $mapperKeys[$key] = $mapper[$key][0];
101 $parser = new $this->_parser($mapperKeys);
102 $parser->setEntity($entity);
104 $mapFields = $this->get('fields');
106 foreach ($mapper as $key => $value) {
108 if (isset($mapFields[$mapper[$key][0]])) {
109 $header[] = $mapFields[$mapper[$key][0]];
111 $mapperFields[] = implode(' - ', $header);
113 $parser->run($fileName, $separator,
116 CRM_Import_Parser
::MODE_IMPORT
,
117 $this->get('contactType'),
121 // add all the necessary variables to the form
122 $parser->set($this, CRM_Import_Parser
::MODE_IMPORT
);
124 // check if there is any error occured
126 $errorStack = CRM_Core_Error
::singleton();
127 $errors = $errorStack->getErrors();
128 $errorMessage = array();
130 if (is_array($errors)) {
131 foreach ($errors as $key => $value) {
132 $errorMessage[] = $value['message'];
135 $errorFile = $fileName['name'] . '.error.log';
137 if ($fd = fopen($errorFile, 'w')) {
138 fwrite($fd, implode('\n', $errorMessage));
142 $this->set('errorFile', $errorFile);
143 $urlParams = 'type=' . CRM_Import_Parser
::ERROR
. $this->_importParserUrl
;
144 $this->set('downloadErrorRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
145 $urlParams = 'type=' . CRM_Import_Parser
::CONFLICT
. $this->_importParserUrl
;
146 $this->set('downloadConflictRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
147 $urlParams = 'type=' . CRM_Import_Parser
::NO_MATCH
. $this->_importParserUrl
;
148 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));