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