PageRenderTime 26ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/CRM/Member/Import/Form/Preview.php

https://github.com/ksecor/civicrm
PHP | 228 lines | 123 code | 37 blank | 68 comment | 13 complexity | c80c453133cd18057fa7672582a58f9f MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.1 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2009 |
  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. |
  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 along with this program; if not, contact CiviCRM LLC |
  21. | at info[AT]civicrm[DOT]org. If you have questions about the |
  22. | GNU Affero General Public License or the licensing of CiviCRM, |
  23. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  24. +--------------------------------------------------------------------+
  25. */
  26. /**
  27. *
  28. * @package CRM
  29. * @copyright CiviCRM LLC (c) 2004-2009
  30. * $Id$
  31. *
  32. */
  33. require_once 'CRM/Core/Form.php';
  34. require_once 'CRM/Member/Import/Parser/Membership.php';
  35. /**
  36. * This class previews the uploaded file and returns summary
  37. * statistics
  38. */
  39. class CRM_Member_Import_Form_Preview extends CRM_Core_Form {
  40. /**
  41. * Function to set variables up before form is built
  42. *
  43. * @return void
  44. * @access public
  45. */
  46. public function preProcess()
  47. {
  48. $skipColumnHeader = $this->controller->exportValue( 'UploadFile', 'skipColumnHeader' );
  49. //get the data from the session
  50. $dataValues = $this->get('dataValues');
  51. $mapper = $this->get('mapper');
  52. $invalidRowCount = $this->get('invalidRowCount');
  53. $conflictRowCount = $this->get('conflictRowCount');
  54. $mismatchCount = $this->get('unMatchCount');
  55. //get the mapping name displayed if the mappingId is set
  56. $mappingId = $this->get('loadMappingId');
  57. if ( $mappingId ) {
  58. $mapDAO =& new CRM_Core_DAO_Mapping();
  59. $mapDAO->id = $mappingId;
  60. $mapDAO->find( true );
  61. $this->assign('loadedMapping', $mappingId);
  62. $this->assign('savedName', $mapDAO->name);
  63. }
  64. if ( $skipColumnHeader ) {
  65. $this->assign( 'skipColumnHeader' , $skipColumnHeader );
  66. $this->assign( 'rowDisplayCount', 3 );
  67. } else {
  68. $this->assign( 'rowDisplayCount', 2 );
  69. }
  70. if ($invalidRowCount) {
  71. $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', 'type=1&realm=membership'));
  72. }
  73. if ($conflictRowCount) {
  74. $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', 'type=2&realm=membership'));
  75. }
  76. if ($mismatchCount) {
  77. $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', 'type=4&realm=membership'));
  78. }
  79. $properties = array( 'mapper',
  80. 'dataValues', 'columnCount',
  81. 'totalRowCount', 'validRowCount',
  82. 'invalidRowCount', 'conflictRowCount',
  83. 'downloadErrorRecordsUrl',
  84. 'downloadConflictRecordsUrl',
  85. 'downloadMismatchRecordsUrl'
  86. );
  87. foreach ( $properties as $property ) {
  88. $this->assign( $property, $this->get( $property ) );
  89. }
  90. }
  91. /**
  92. * Function to actually build the form
  93. *
  94. * @return None
  95. * @access public
  96. */
  97. public function buildQuickForm( ) {
  98. $this->addButtons( array(
  99. array ( 'type' => 'back',
  100. 'name' => ts('<< Previous') ),
  101. array ( 'type' => 'next',
  102. 'name' => ts('Import Now >>'),
  103. 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
  104. 'isDefault' => true ),
  105. array ( 'type' => 'cancel',
  106. 'name' => ts('Cancel') ),
  107. )
  108. );
  109. }
  110. /**
  111. * Return a descriptive name for the page, used in wizard header
  112. *
  113. * @return string
  114. * @access public
  115. */
  116. public function getTitle( ) {
  117. return ts('Preview');
  118. }
  119. /**
  120. * Process the mapped fields and map it into the uploaded file
  121. * preview the file and extract some summary statistics
  122. *
  123. * @return void
  124. * @access public
  125. */
  126. public function postProcess( ) {
  127. $fileName = $this->controller->exportValue( 'UploadFile', 'uploadFile' );
  128. $skipColumnHeader = $this->controller->exportValue( 'UploadFile', 'skipColumnHeader' );
  129. $invalidRowCount = $this->get('invalidRowCount');
  130. $conflictRowCount = $this->get('conflictRowCount');
  131. $onDuplicate = $this->get('onDuplicate');
  132. $config =& CRM_Core_Config::singleton( );
  133. $seperator = $config->fieldSeparator;
  134. $mapper = $this->controller->exportValue( 'MapField', 'mapper' );
  135. $mapperKeys = array();
  136. $mapperLocType = array();
  137. $mapperPhoneType = array();
  138. // Note: we keep the multi-dimension array (even thought it's not
  139. // needed in the case of memberships import) so that we can merge
  140. // the common code with contacts import later and subclass contact
  141. // and membership imports from there
  142. foreach ($mapper as $key => $value) {
  143. $mapperKeys[$key] = $mapper[$key][0];
  144. if (is_numeric($mapper[$key][1])) {
  145. $mapperLocType[$key] = $mapper[$key][1];
  146. } else {
  147. $mapperLocType[$key] = null;
  148. }
  149. if (!is_numeric($mapper[$key][2])) {
  150. $mapperPhoneType[$key] = $mapper[$key][2];
  151. } else {
  152. $mapperPhoneType[$key] = null;
  153. }
  154. }
  155. $parser =& new CRM_Member_Import_Parser_Membership( $mapperKeys ,$mapperLocType ,$mapperPhoneType );
  156. $mapFields = $this->get('fields');
  157. foreach ($mapper as $key => $value) {
  158. $header = array();
  159. if ( isset($mapFields[$mapper[$key][0]]) ) {
  160. $header[] = $mapFields[$mapper[$key][0]];
  161. }
  162. $mapperFields[] = implode(' - ', $header);
  163. }
  164. $parser->run( $fileName, $seperator,
  165. $mapperFields,
  166. $skipColumnHeader,
  167. CRM_Member_Import_Parser::MODE_IMPORT,
  168. $this->get('contactType'),
  169. $onDuplicate);
  170. // add all the necessary variables to the form
  171. $parser->set( $this, CRM_Member_Import_Parser::MODE_IMPORT );
  172. // check if there is any error occured
  173. $errorStack =& CRM_Core_Error::singleton();
  174. $errors = $errorStack->getErrors();
  175. $errorMessage = array();
  176. if( is_array( $errors ) ) {
  177. foreach($errors as $key => $value) {
  178. $errorMessage[] = $value['message'];
  179. }
  180. $errorFile = $fileName['name'] . '.error.log';
  181. if ( $fd = fopen( $errorFile, 'w' ) ) {
  182. fwrite($fd, implode('\n', $errorMessage));
  183. }
  184. fclose($fd);
  185. $this->set('errorFile', $errorFile);
  186. $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', 'type=1&realm=membership'));
  187. $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', 'type=2&realm=membership'));
  188. $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', 'type=4&realm=membership'));
  189. }
  190. }
  191. }