| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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-2015 |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * This class previews the uploaded file and returns summary statistics. |
| 36 | */ |
| 37 | class CRM_Activity_Import_Form_Preview extends CRM_Import_Form_Preview { |
| 38 | |
| 39 | /** |
| 40 | * Set variables up before form is built. |
| 41 | */ |
| 42 | public function preProcess() { |
| 43 | $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); |
| 44 | |
| 45 | //get the data from the session |
| 46 | $dataValues = $this->get('dataValues'); |
| 47 | $mapper = $this->get('mapper'); |
| 48 | $invalidRowCount = $this->get('invalidRowCount'); |
| 49 | $conflictRowCount = $this->get('conflictRowCount'); |
| 50 | $mismatchCount = $this->get('unMatchCount'); |
| 51 | |
| 52 | //get the mapping name displayed if the mappingId is set |
| 53 | $mappingId = $this->get('loadMappingId'); |
| 54 | if ($mappingId) { |
| 55 | $mapDAO = new CRM_Core_DAO_Mapping(); |
| 56 | $mapDAO->id = $mappingId; |
| 57 | $mapDAO->find(TRUE); |
| 58 | $this->assign('loadedMapping', $mappingId); |
| 59 | $this->assign('savedName', $mapDAO->name); |
| 60 | } |
| 61 | |
| 62 | if ($skipColumnHeader) { |
| 63 | $this->assign('skipColumnHeader', $skipColumnHeader); |
| 64 | $this->assign('rowDisplayCount', 3); |
| 65 | } |
| 66 | else { |
| 67 | $this->assign('rowDisplayCount', 2); |
| 68 | } |
| 69 | |
| 70 | if ($invalidRowCount) { |
| 71 | $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Activity_Import_Parser'; |
| 72 | $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
| 73 | } |
| 74 | |
| 75 | if ($conflictRowCount) { |
| 76 | $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Activity_Import_Parser'; |
| 77 | $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
| 78 | } |
| 79 | |
| 80 | if ($mismatchCount) { |
| 81 | $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Activity_Import_Parser'; |
| 82 | $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
| 83 | } |
| 84 | |
| 85 | $properties = array( |
| 86 | 'mapper', |
| 87 | 'dataValues', |
| 88 | 'columnCount', |
| 89 | 'totalRowCount', |
| 90 | 'validRowCount', |
| 91 | 'invalidRowCount', |
| 92 | 'conflictRowCount', |
| 93 | 'downloadErrorRecordsUrl', |
| 94 | 'downloadConflictRecordsUrl', |
| 95 | 'downloadMismatchRecordsUrl', |
| 96 | ); |
| 97 | |
| 98 | foreach ($properties as $property) { |
| 99 | $this->assign($property, $this->get($property)); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Process the mapped fields and map it into the uploaded file. |
| 105 | * |
| 106 | * Preview the file and extract some summary statistics |
| 107 | */ |
| 108 | public function postProcess() { |
| 109 | $fileName = $this->controller->exportValue('DataSource', 'uploadFile'); |
| 110 | $skipColumnHeader = $this->controller->exportValue('DataSource', 'skipColumnHeader'); |
| 111 | $invalidRowCount = $this->get('invalidRowCount'); |
| 112 | $conflictRowCount = $this->get('conflictRowCount'); |
| 113 | $onDuplicate = $this->get('onDuplicate'); |
| 114 | |
| 115 | $config = CRM_Core_Config::singleton(); |
| 116 | $seperator = $config->fieldSeparator; |
| 117 | |
| 118 | $mapper = $this->controller->exportValue('MapField', 'mapper'); |
| 119 | $mapperKeys = array(); |
| 120 | $mapperLocType = array(); |
| 121 | $mapperPhoneType = array(); |
| 122 | |
| 123 | foreach ($mapper as $key => $value) { |
| 124 | $mapperKeys[$key] = $mapper[$key][0]; |
| 125 | |
| 126 | if (!empty($mapper[$key][1]) && is_numeric($mapper[$key][1])) { |
| 127 | $mapperLocType[$key] = $mapper[$key][1]; |
| 128 | } |
| 129 | else { |
| 130 | $mapperLocType[$key] = NULL; |
| 131 | } |
| 132 | |
| 133 | if (!empty($mapper[$key][2]) && (!is_numeric($mapper[$key][2]))) { |
| 134 | $mapperPhoneType[$key] = $mapper[$key][2]; |
| 135 | } |
| 136 | else { |
| 137 | $mapperPhoneType[$key] = NULL; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | $parser = new CRM_Activity_Import_Parser_Activity($mapperKeys, $mapperLocType, $mapperPhoneType); |
| 142 | |
| 143 | $mapFields = $this->get('fields'); |
| 144 | |
| 145 | foreach ($mapper as $key => $value) { |
| 146 | $header = array(); |
| 147 | if (isset($mapFields[$mapper[$key][0]])) { |
| 148 | $header[] = $mapFields[$mapper[$key][0]]; |
| 149 | } |
| 150 | $mapperFields[] = implode(' - ', $header); |
| 151 | } |
| 152 | $parser->run($fileName, $seperator, |
| 153 | $mapperFields, |
| 154 | $skipColumnHeader, |
| 155 | CRM_Import_Parser::MODE_IMPORT, |
| 156 | $onDuplicate |
| 157 | ); |
| 158 | |
| 159 | // add all the necessary variables to the form |
| 160 | $parser->set($this, CRM_Import_Parser::MODE_IMPORT); |
| 161 | |
| 162 | // check if there is any error occurred |
| 163 | |
| 164 | $errorStack = CRM_Core_Error::singleton(); |
| 165 | $errors = $errorStack->getErrors(); |
| 166 | $errorMessage = array(); |
| 167 | |
| 168 | if (is_array($errors)) { |
| 169 | foreach ($errors as $key => $value) { |
| 170 | $errorMessage[] = $value['message']; |
| 171 | } |
| 172 | |
| 173 | $errorFile = $fileName['name'] . '.error.log'; |
| 174 | |
| 175 | if ($fd = fopen($errorFile, 'w')) { |
| 176 | fwrite($fd, implode('\n', $errorMessage)); |
| 177 | } |
| 178 | fclose($fd); |
| 179 | |
| 180 | $this->set('errorFile', $errorFile); |
| 181 | $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Activity_Import_Parser'; |
| 182 | $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
| 183 | $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Activity_Import_Parser'; |
| 184 | $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
| 185 | $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Activity_Import_Parser'; |
| 186 | $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams)); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | } |