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';
10 * Function to 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));
61 'dataValues', 'columnCount',
62 'totalRowCount', 'validRowCount',
63 'invalidRowCount', 'conflictRowCount',
64 'downloadErrorRecordsUrl',
65 'downloadConflictRecordsUrl',
66 'downloadMismatchRecordsUrl',
69 foreach ($properties as $property) {
70 $this->assign($property, $this->get($property));
75 * Process the mapped fields and map it into the uploaded file
76 * preview the file and extract some summary statistics
81 public function postProcess() {
82 $fileName = $this->controller
->exportValue('DataSource', 'uploadFile');
83 $skipColumnHeader = $this->controller
->exportValue('DataSource', 'skipColumnHeader');
84 $invalidRowCount = $this->get('invalidRowCount');
85 $conflictRowCount = $this->get('conflictRowCount');
86 $onDuplicate = $this->get('onDuplicate');
87 $entity = $this->get('_entity');
89 $config = CRM_Core_Config
::singleton();
90 $separator = $config->fieldSeparator
;
92 $mapper = $this->controller
->exportValue('MapField', 'mapper');
93 $mapperKeys = array();
95 foreach ($mapper as $key => $value) {
96 $mapperKeys[$key] = $mapper[$key][0];
99 $parser = new $this->_parser($mapperKeys);
100 $parser->setEntity($entity);
102 $mapFields = $this->get('fields');
104 foreach ($mapper as $key => $value) {
106 if (isset($mapFields[$mapper[$key][0]])) {
107 $header[] = $mapFields[$mapper[$key][0]];
109 $mapperFields[] = implode(' - ', $header);
111 $parser->run($fileName, $separator,
114 CRM_Import_Parser
::MODE_IMPORT
,
115 $this->get('contactType'),
119 // add all the necessary variables to the form
120 $parser->set($this, CRM_Import_Parser
::MODE_IMPORT
);
122 // check if there is any error occured
124 $errorStack = CRM_Core_Error
::singleton();
125 $errors = $errorStack->getErrors();
126 $errorMessage = array();
128 if (is_array($errors)) {
129 foreach ($errors as $key => $value) {
130 $errorMessage[] = $value['message'];
133 $errorFile = $fileName['name'] . '.error.log';
135 if ($fd = fopen($errorFile, 'w')) {
136 fwrite($fd, implode('\n', $errorMessage));
140 $this->set('errorFile', $errorFile);
141 $urlParams = 'type=' . CRM_Import_Parser
::ERROR
. $this->_importParserUrl
;
142 $this->set('downloadErrorRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
143 $urlParams = 'type=' . CRM_Import_Parser
::CONFLICT
. $this->_importParserUrl
;
144 $this->set('downloadConflictRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
145 $urlParams = 'type=' . CRM_Import_Parser
::NO_MATCH
. $this->_importParserUrl
;
146 $this->set('downloadMismatchRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));