| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * This class previews the uploaded file and returns summary |
| 38 | * statistics |
| 39 | */ |
| 40 | class CRM_Event_Import_Form_Preview extends CRM_Import_Form_Preview { |
| 41 | |
| 42 | /** |
| 43 | * Set variables up before form is built. |
| 44 | * |
| 45 | * @return void |
| 46 | */ |
| 47 | public function preProcess() { |
| 48 | $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); |
| 49 | |
| 50 | //get the data from the session |
| 51 | $dataValues = $this->get('dataValues'); |
| 52 | $mapper = $this->get('mapper'); |
| 53 | $invalidRowCount = $this->get('invalidRowCount'); |
| 54 | $conflictRowCount = $this->get('conflictRowCount'); |
| 55 | $mismatchCount = $this->get('unMatchCount'); |
| 56 | |
| 57 | //get the mapping name displayed if the mappingId is set |
| 58 | $mappingId = $this->get('loadMappingId'); |
| 59 | if ($mappingId) { |
| 60 | $mapDAO = new CRM_Core_DAO_Mapping(); |
| 61 | $mapDAO->id = $mappingId; |
| 62 | $mapDAO->find(TRUE); |
| 63 | $this->assign('loadedMapping', $mappingId); |
| 64 | $this->assign('savedName', $mapDAO->name); |
| 65 | } |
| 66 | |
| 67 | if ($skipColumnHeader) { |
| 68 | $this->assign('skipColumnHeader', $skipColumnHeader); |
| 69 | $this->assign('rowDisplayCount', 3); |
| 70 | } |
| 71 | else { |
| 72 | $this->assign('rowDisplayCount', 2); |
| 73 | } |
| 74 | |
| 75 | if ($invalidRowCount) { |
| 76 | $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Event_Import_Parser'; |
| 77 | $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
| 78 | } |
| 79 | |
| 80 | if ($conflictRowCount) { |
| 81 | $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Event_Import_Parser'; |
| 82 | $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
| 83 | } |
| 84 | |
| 85 | if ($mismatchCount) { |
| 86 | $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Event_Import_Parser'; |
| 87 | $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
| 88 | } |
| 89 | |
| 90 | $properties = array( |
| 91 | 'mapper', |
| 92 | 'dataValues', |
| 93 | 'columnCount', |
| 94 | 'totalRowCount', |
| 95 | 'validRowCount', |
| 96 | 'invalidRowCount', |
| 97 | 'conflictRowCount', |
| 98 | 'downloadErrorRecordsUrl', |
| 99 | 'downloadConflictRecordsUrl', |
| 100 | 'downloadMismatchRecordsUrl', |
| 101 | ); |
| 102 | |
| 103 | foreach ($properties as $property) { |
| 104 | $this->assign($property, $this->get($property)); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Process the mapped fields and map it into the uploaded file |
| 110 | * preview the file and extract some summary statistics |
| 111 | * |
| 112 | * @return void |
| 113 | */ |
| 114 | public function postProcess() { |
| 115 | $fileName = $this->controller->exportValue('DataSource', 'uploadFile'); |
| 116 | $seperator = $this->controller->exportValue('DataSource', 'fieldSeparator'); |
| 117 | $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); |
| 118 | $invalidRowCount = $this->get('invalidRowCount'); |
| 119 | $conflictRowCount = $this->get('conflictRowCount'); |
| 120 | $onDuplicate = $this->get('onDuplicate'); |
| 121 | |
| 122 | $mapper = $this->controller->exportValue('MapField', 'mapper'); |
| 123 | $mapperKeys = array(); |
| 124 | |
| 125 | foreach ($mapper as $key => $value) { |
| 126 | $mapperKeys[$key] = $mapper[$key][0]; |
| 127 | } |
| 128 | |
| 129 | $parser = new CRM_Event_Import_Parser_Participant($mapperKeys); |
| 130 | |
| 131 | $mapFields = $this->get('fields'); |
| 132 | |
| 133 | foreach ($mapper as $key => $value) { |
| 134 | $header = array(); |
| 135 | if (isset($mapFields[$mapper[$key][0]])) { |
| 136 | $header[] = $mapFields[$mapper[$key][0]]; |
| 137 | } |
| 138 | $mapperFields[] = implode(' - ', $header); |
| 139 | } |
| 140 | $parser->run($fileName, $seperator, |
| 141 | $mapperFields, |
| 142 | $skipColumnHeader, |
| 143 | CRM_Import_Parser::MODE_IMPORT, |
| 144 | $this->get('contactType'), |
| 145 | $onDuplicate |
| 146 | ); |
| 147 | |
| 148 | // add all the necessary variables to the form |
| 149 | $parser->set($this, CRM_Import_Parser::MODE_IMPORT); |
| 150 | |
| 151 | // check if there is any error occurred |
| 152 | |
| 153 | $errorStack = CRM_Core_Error::singleton(); |
| 154 | $errors = $errorStack->getErrors(); |
| 155 | $errorMessage = array(); |
| 156 | |
| 157 | if (is_array($errors)) { |
| 158 | foreach ($errors as $key => $value) { |
| 159 | $errorMessage[] = $value['message']; |
| 160 | } |
| 161 | |
| 162 | $errorFile = $fileName['name'] . '.error.log'; |
| 163 | |
| 164 | if ($fd = fopen($errorFile, 'w')) { |
| 165 | fwrite($fd, implode('\n', $errorMessage)); |
| 166 | } |
| 167 | fclose($fd); |
| 168 | |
| 169 | $this->set('errorFile', $errorFile); |
| 170 | $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Event_Import_Parser'; |
| 171 | $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
| 172 | $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Event_Import_Parser'; |
| 173 | $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
| 174 | $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Event_Import_Parser'; |
| 175 | $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | } |