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