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