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