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 $entity = $this->get('_entity');
23 //get the mapping name displayed if the mappingId is set
24 $mappingId = $this->get('loadMappingId');
26 $mapDAO = new CRM_Core_DAO_Mapping();
27 $mapDAO->id
= $mappingId;
30 $this->assign('savedMappingName', $mappingId ?
$mapDAO->name
: NULL);
32 if ($invalidRowCount) {
33 $urlParams = 'type=' . CRM_Import_Parser
::ERROR
. $this->_importParserUrl
;
34 $this->set('downloadErrorRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));
44 'downloadErrorRecordsUrl',
47 foreach ($properties as $property) {
48 $this->assign($property, $this->get($property));
53 * Process the mapped fields and map it into the uploaded file.
54 * preview the file and extract some summary statistics
58 public function postProcess() {
59 $fileName = $this->getSubmittedValue('uploadFile');
60 $invalidRowCount = $this->get('invalidRowCount');
61 $onDuplicate = $this->get('onDuplicate');
62 $entity = $this->get('_entity');
64 $mapper = $this->controller
->exportValue('MapField', 'mapper');
67 foreach ($mapper as $key => $value) {
68 $mapperKeys[$key] = $mapper[$key][0];
71 $parser = new $this->_parser($mapperKeys);
72 $parser->setEntity($entity);
74 $mapFields = $this->get('fields');
76 foreach ($mapper as $key => $value) {
78 if (isset($mapFields[$mapper[$key][0]])) {
79 $header[] = $mapFields[$mapper[$key][0]];
81 $mapperFields[] = implode(' - ', $header);
83 $parser->run($this->getSubmittedValue('uploadFile'), $this->getSubmittedValue('fieldSeparator'),
85 $this->getSubmittedValue('skipColumnHeader'),
86 CRM_Import_Parser
::MODE_IMPORT
,
87 $this->get('contactType'),
91 // add all the necessary variables to the form
92 $parser->set($this, CRM_Import_Parser
::MODE_IMPORT
);
94 // check if there is any error occurred
96 $errorStack = CRM_Core_Error
::singleton();
97 $errors = $errorStack->getErrors();
100 if (is_array($errors)) {
101 foreach ($errors as $key => $value) {
102 $errorMessage[] = $value['message'];
105 $errorFile = $fileName['name'] . '.error.log';
107 if ($fd = fopen($errorFile, 'w')) {
108 fwrite($fd, implode('\n', $errorMessage));
112 $this->set('errorFile', $errorFile);
113 $urlParams = 'type=' . CRM_Import_Parser
::ERROR
. $this->_importParserUrl
;
114 $this->set('downloadErrorRecordsUrl', CRM_Utils_System
::url('civicrm/export', $urlParams));