PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/CRM/Member/Import/Form/Preview.php

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