Merge remote-tracking branch 'upstream/4.6' into 4.6-master-2015-06-29-23-22-39
[civicrm-core.git] / CRM / Event / Import / Form / DataSource.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class gets the name of the file to upload
38 */
ac4410cd 39class CRM_Event_Import_Form_DataSource extends CRM_Core_Form {
6a488035
TO
40
41 /**
fe482240 42 * Set variables up before form is built.
6a488035
TO
43 *
44 * @return void
6a488035
TO
45 */
46 public function preProcess() {
47 $session = CRM_Core_Session::singleton();
48 $session->pushUserContext(CRM_Utils_System::url('civicrm/event/import', 'reset=1'));
66dc6009 49 // check for post max size
50 CRM_Core_Config_Defaults::formatUnitSize(ini_get('post_max_size'), TRUE);
6a488035
TO
51 }
52
53 /**
fe482240 54 * Build the form object.
6a488035 55 *
355ba699 56 * @return void
6a488035
TO
57 */
58 public function buildQuickForm() {
59 //Setting Upload File Size
60 $config = CRM_Core_Config::singleton();
66dc6009 61
92fcb95f 62 $uploadFileSize = CRM_Core_Config_Defaults::formatUnitSize($config->maxFileSize . 'm', TRUE);
6a488035
TO
63 $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
64
65 $this->assign('uploadSize', $uploadSize);
66
66dc6009 67 $this->add('File', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE);
6a488035 68 $this->setMaxFileSize($uploadFileSize);
353ffa53
TO
69 $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', array(
70 1 => $uploadSize,
bed98343 71 2 => $uploadFileSize,
353ffa53 72 )), 'maxfilesize', $uploadFileSize);
66dc6009 73 $this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile');
6a488035
TO
74 $this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File');
75
76 $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
77
78 $duplicateOptions = array();
79 $duplicateOptions[] = $this->createElement('radio',
a05662ef 80 NULL, NULL, ts('Skip'), CRM_Import_Parser::DUPLICATE_SKIP
6a488035
TO
81 );
82 $duplicateOptions[] = $this->createElement('radio',
a05662ef 83 NULL, NULL, ts('Update'), CRM_Import_Parser::DUPLICATE_UPDATE
6a488035
TO
84 );
85 $duplicateOptions[] = $this->createElement('radio',
a05662ef 86 NULL, NULL, ts('No Duplicate Checking'), CRM_Import_Parser::DUPLICATE_NOCHECK
6a488035
TO
87 );
88 // for contributions NOCHECK == SKIP
89 // $duplicateOptions[] = $this->createElement('radio',
a05662ef 90 // null, null, ts('No Duplicate Checking'), CRM_Import_Parser::DUPLICATE_NOCHECK);
6a488035
TO
91
92 $this->addGroup($duplicateOptions, 'onDuplicate',
93 ts('On Duplicate Entries')
94 );
95
96 //get the saved mapping details
97 $mappingArray = CRM_Core_BAO_Mapping::getMappings(CRM_Core_OptionGroup::getValue('mapping_type',
353ffa53
TO
98 'Import Participant',
99 'name'
100 ));
6a488035
TO
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 $this->setDefaults(array(
110 'onDuplicate' =>
a05662ef 111 CRM_Import_Parser::DUPLICATE_SKIP,
353ffa53 112 ));
6a488035
TO
113
114 //contact types option
115 $contactOptions = array();
116 if (CRM_Contact_BAO_ContactType::isActive('Individual')) {
117 $contactOptions[] = $this->createElement('radio',
a05662ef 118 NULL, NULL, ts('Individual'), CRM_Import_Parser::CONTACT_INDIVIDUAL
6a488035
TO
119 );
120 }
121 if (CRM_Contact_BAO_ContactType::isActive('Household')) {
122 $contactOptions[] = $this->createElement('radio',
a05662ef 123 NULL, NULL, ts('Household'), CRM_Import_Parser::CONTACT_HOUSEHOLD
6a488035
TO
124 );
125 }
126 if (CRM_Contact_BAO_ContactType::isActive('Organization')) {
127 $contactOptions[] = $this->createElement('radio',
a05662ef 128 NULL, NULL, ts('Organization'), CRM_Import_Parser::CONTACT_ORGANIZATION
6a488035
TO
129 );
130 }
131 $this->addGroup($contactOptions, 'contactType', ts('Contact Type'));
132
133 $this->setDefaults(array(
353ffa53 134 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL,
6a488035
TO
135 )
136 );
137
138 //build date formats
139 CRM_Core_Form_Date::buildAllowedDateFormats($this);
140
141 $this->addButtons(array(
142 array(
143 'type' => 'upload',
f212d37d 144 'name' => ts('Continue'),
6a488035
TO
145 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
146 'isDefault' => TRUE,
147 ),
148 array(
149 'type' => 'cancel',
150 'name' => ts('Cancel'),
151 ),
152 )
153 );
154 }
155
156 /**
fe482240 157 * Process the uploaded file.
6a488035
TO
158 *
159 * @return void
6a488035
TO
160 */
161 public function postProcess() {
162 $this->controller->resetPage('MapField');
163
353ffa53 164 $fileName = $this->controller->exportValue($this->_name, 'uploadFile');
6a488035 165 $skipColumnHeader = $this->controller->exportValue($this->_name, 'skipColumnHeader');
353ffa53
TO
166 $onDuplicate = $this->controller->exportValue($this->_name, 'onDuplicate');
167 $contactType = $this->controller->exportValue($this->_name, 'contactType');
168 $dateFormats = $this->controller->exportValue($this->_name, 'dateFormats');
169 $savedMapping = $this->controller->exportValue($this->_name, 'savedMapping');
6a488035
TO
170
171 $this->set('onDuplicate', $onDuplicate);
172 $this->set('contactType', $contactType);
173 $this->set('dateFormats', $dateFormats);
174 $this->set('savedMapping', $savedMapping);
175
176 $session = CRM_Core_Session::singleton();
177 $session->set("dateTypes", $dateFormats);
178
179 $config = CRM_Core_Config::singleton();
180 $seperator = $config->fieldSeparator;
181
182 $mapper = array();
183 $parser = new CRM_Event_Import_Parser_Participant($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
6a488035
TO
199 */
200 public function getTitle() {
201 return ts('Upload Data');
202 }
96025800 203
6a488035 204}