Merge stateMachine into one file CRM-11254
[civicrm-core.git] / CRM / Member / Import / Form / UploadFile.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 * $Id$
33 *
34 */
35
36/**
37 * This class gets the name of the file to upload
38 */
39class CRM_Member_Import_Form_UploadFile extends CRM_Core_Form {
40
41 /**
42 * Function to set variables up before form is built
43 *
44 * @return void
45 * @access public
46 */
47 public function preProcess() {
48 $session = CRM_Core_Session::singleton();
49 $session->pushUserContext(CRM_Utils_System::url('civicrm/member/import', 'reset=1'));
50 }
51
52 /**
53 * Function to actually build the form
54 *
55 * @return None
56 * @access public
57 */
58 public function buildQuickForm() {
59 //Setting Upload File Size
60 $config = CRM_Core_Config::singleton();
61 if ($config->maxImportFileSize >= 8388608) {
62 $uploadFileSize = 8388608;
63 }
64 else {
65 $uploadFileSize = $config->maxImportFileSize;
66 }
67 $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
68
69 $this->assign('uploadSize', $uploadSize);
70
71 $this->add('file', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE);
72
73 $this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile');
74 $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', array(1 => $uploadSize, 2 => $uploadFileSize)), 'maxfilesize', $uploadFileSize);
75 $this->setMaxFileSize($uploadFileSize);
76 $this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File');
77
78 $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
79
80 $duplicateOptions = array();
81 $duplicateOptions[] = $this->createElement('radio',
a05662ef 82 NULL, NULL, ts('Insert new Membership'), CRM_Import_Parser::DUPLICATE_SKIP
6a488035
TO
83 );
84 $duplicateOptions[] = $this->createElement('radio',
a05662ef 85 NULL, NULL, ts('Update existing Membership'), CRM_Import_Parser::DUPLICATE_UPDATE
6a488035
TO
86 );
87
88 $this->addGroup($duplicateOptions, 'onDuplicate',
89 ts('Import mode')
90 );
91 $this->setDefaults(array(
92 'onDuplicate' =>
a05662ef 93 CRM_Import_Parser::DUPLICATE_SKIP,
6a488035
TO
94 ));
95
96 //get the saved mapping details
97 $mappingArray = CRM_Core_BAO_Mapping::getMappings(CRM_Core_OptionGroup::getValue('mapping_type',
98 'Import Membership',
99 'name'
100 ));
101 $this->assign('savedMapping', $mappingArray);
102 $this->add('select', 'savedMapping', ts('Mapping Option'), array('' => ts('- select -')) + $mappingArray);
103
104 if ($loadeMapping = $this->get('loadedMapping')) {
105 $this->assign('loadedMapping', $loadeMapping);
106 $this->setDefaults(array('savedMapping' => $loadeMapping));
107 }
108
109 //contact types option
110 $contactOptions = array();
111 if (CRM_Contact_BAO_ContactType::isActive('Individual')) {
112 $contactOptions[] = $this->createElement('radio',
a05662ef 113 NULL, NULL, ts('Individual'), CRM_Import_Parser::CONTACT_INDIVIDUAL
6a488035
TO
114 );
115 }
116 if (CRM_Contact_BAO_ContactType::isActive('Household')) {
117 $contactOptions[] = $this->createElement('radio',
a05662ef 118 NULL, NULL, ts('Household'), CRM_Import_Parser::CONTACT_HOUSEHOLD
6a488035
TO
119 );
120 }
121 if (CRM_Contact_BAO_ContactType::isActive('Organization')) {
122 $contactOptions[] = $this->createElement('radio',
a05662ef 123 NULL, NULL, ts('Organization'), CRM_Import_Parser::CONTACT_ORGANIZATION
6a488035
TO
124 );
125 }
126
127 $this->addGroup($contactOptions, 'contactType',
128 ts('Contact Type')
129 );
130
131 $this->setDefaults(array(
132 'contactType' =>
a05662ef 133 CRM_Import_Parser::CONTACT_INDIVIDUAL,
6a488035
TO
134 ));
135
136 //build date formats
137 CRM_Core_Form_Date::buildAllowedDateFormats($this);
138
139 $this->addButtons(array(
140 array(
141 'type' => 'upload',
142 'name' => ts('Continue >>'),
143 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
144 'isDefault' => TRUE,
145 ),
146 array(
147 'type' => 'cancel',
148 'name' => ts('Cancel'),
149 ),
150 )
151 );
152 }
153
154 /**
155 * Process the uploaded file
156 *
157 * @return void
158 * @access public
159 */
160 public function postProcess() {
161 $this->controller->resetPage('MapField');
162
163 $fileName = $this->controller->exportValue($this->_name, 'uploadFile');
164 $skipColumnHeader = $this->controller->exportValue($this->_name, 'skipColumnHeader');
165 $onDuplicate = $this->controller->exportValue($this->_name, 'onDuplicate');
166 $contactType = $this->controller->exportValue($this->_name, 'contactType');
167 $dateFormats = $this->controller->exportValue($this->_name, 'dateFormats');
168 $savedMapping = $this->controller->exportValue($this->_name, 'savedMapping');
169
170 $this->set('onDuplicate', $onDuplicate);
171 $this->set('contactType', $contactType);
172 $this->set('dateFormats', $dateFormats);
173 $this->set('savedMapping', $savedMapping);
174
175 $session = CRM_Core_Session::singleton();
176 $session->set("dateTypes", $dateFormats);
177
178 $config = CRM_Core_Config::singleton();
179 $seperator = $config->fieldSeparator;
180
181 $mapper = array();
182
183 $parser = new CRM_Member_Import_Parser_Membership($mapper);
184 $parser->setMaxLinesToProcess(100);
185 $parser->run($fileName, $seperator,
186 $mapper,
187 $skipColumnHeader,
a05662ef 188 CRM_Import_Parser::MODE_MAPFIELD, $contactType
6a488035
TO
189 );
190
191 // add all the necessary variables to the form
192 $parser->set($this);
193 }
194
195 /**
196 * Return a descriptive name for the page, used in wizard header
197 *
198 * @return string
199 * @access public
200 */
201 public function getTitle() {
202 return ts('Upload Data');
203 }
204}
205