CRM-11254 - Add base class for Import/dataSource
[civicrm-core.git] / CRM / Import / Form / DataSource.php
CommitLineData
81c3812a
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 * $Id$
33 *
34 */
35
36/**
37 * Base class for upload-only import forms (all but Contact import)
38 */
39abstract class CRM_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 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE);
48 $params = "reset=1";
49 if ($this->_id) {
50 $params .= "&id={$this->_id}";
51 }
52 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url(static::PATH, $params));
53
54 // check for post max size
55 CRM_Core_Config_Defaults::formatUnitSize(ini_get('post_max_size'), TRUE);
56 }
57
58 /**
59 * Common form elements.
60 *
61 * @return void
62 */
63 public function buildQuickForm() {
64 $config = CRM_Core_Config::singleton();
65
66 $uploadFileSize = CRM_Core_Config_Defaults::formatUnitSize($config->maxFileSize . 'm', TRUE);
67 $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
68
69 $this->assign('uploadSize', $uploadSize);
70
71 $this->add('File', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE);
72 $this->setMaxFileSize($uploadFileSize);
73 $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', array(
74 1 => $uploadSize,
75 2 => $uploadFileSize,
76 )), 'maxfilesize', $uploadFileSize);
77 $this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile');
78 $this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File');
79
80 $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
81
82 //get the saved mapping details
83 $mappingArray = CRM_Core_BAO_Mapping::getMappings(CRM_Core_OptionGroup::getValue('mapping_type',
84 'Import ' . static::IMPORT_ENTITY,
85 'name'
86 ));
87 $this->assign('savedMapping', $mappingArray);
88 $this->add('select', 'savedMapping', ts('Mapping Option'), array('' => ts('- select -')) + $mappingArray);
89
90 if ($loadedMapping = $this->get('loadedMapping')) {
91 $this->assign('loadedMapping', $loadedMapping);
92 $this->setDefaults(array('savedMapping' => $loadedMapping));
93 }
94
95 //build date formats
96 CRM_Core_Form_Date::buildAllowedDateFormats($this);
97
98 $this->addButtons(array(
99 array(
100 'type' => 'upload',
101 'name' => ts('Continue'),
102 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
103 'isDefault' => TRUE,
104 ),
105 array(
106 'type' => 'cancel',
107 'name' => ts('Cancel'),
108 ),
109 )
110 );
111 }
112
113 /**
114 * A long-winded way to add one radio element to the form
115 */
116 protected function addContactTypeSelector() {
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
144 /**
145 * @param array $names
146 */
147 protected function storeFormValues($names) {
148 foreach ($names as $name) {
149 $this->set($name, $this->controller->exportValue($this->_name, $name));
150 }
151 }
152
153 /**
154 * Common form postProcess
155 *
156 * @param string $parserClassName
157 */
158 protected function submitFileForMapping($parserClassName) {
159 $this->controller->resetPage('MapField');
160
161 $fileName = $this->controller->exportValue($this->_name, 'uploadFile');
162 $skipColumnHeader = $this->controller->exportValue($this->_name, 'skipColumnHeader');
163
164 $session = CRM_Core_Session::singleton();
165 $session->set("dateTypes", $this->get('dateFormats'));
166
167 $config = CRM_Core_Config::singleton();
168 $seperator = $config->fieldSeparator;
169
170 $mapper = array();
171
172 $parser = new $parserClassName($mapper);
173 $parser->setMaxLinesToProcess(100);
174 $parser->run($fileName,
175 $seperator,
176 $mapper,
177 $skipColumnHeader,
178 CRM_Import_Parser::MODE_MAPFIELD,
179 $this->get('contactType')
180 );
181
182 // add all the necessary variables to the form
183 $parser->set($this);
184 }
185
186 /**
187 * Return a descriptive name for the page, used in wizard header
188 *
189 * @return string
190 */
191 public function getTitle() {
192 return ts('Upload Data');
193 }
194
195}