Merge branch 'master' of https://github.com/civicrm/civicrm-core into CRM-17745
[civicrm-core.git] / CRM / Import / Form / DataSource.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
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 * @package CRM
30 * @copyright CiviCRM LLC (c) 2004-2015
31 */
32
33 /**
34 * Base class for upload-only import forms (all but Contact import).
35 */
36 abstract class CRM_Import_Form_DataSource extends CRM_Core_Form {
37
38 /**
39 * Set variables up before form is built.
40 */
41 public function preProcess() {
42 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE);
43 $params = "reset=1";
44 if ($this->_id) {
45 $params .= "&id={$this->_id}";
46 }
47 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url(static::PATH, $params));
48
49 // check for post max size
50 CRM_Utils_Number::formatUnitSize(ini_get('post_max_size'), TRUE);
51 }
52
53 /**
54 * Common form elements.
55 */
56 public function buildQuickForm() {
57 $config = CRM_Core_Config::singleton();
58
59 $uploadFileSize = CRM_Utils_Number::formatUnitSize($config->maxFileSize . 'm', TRUE);
60 $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
61
62 $this->assign('uploadSize', $uploadSize);
63
64 $this->add('File', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE);
65 $this->setMaxFileSize($uploadFileSize);
66 $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', array(
67 1 => $uploadSize,
68 2 => $uploadFileSize,
69 )), 'maxfilesize', $uploadFileSize);
70 $this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile');
71 $this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File');
72
73 $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
74
75 $this->add('text', 'fieldSeparator', ts('Import Field Separator'), array('size' => 2), TRUE);
76 $this->setDefaults(array('fieldSeparator' => $config->fieldSeparator));
77
78 //get the saved mapping details
79 $mappingArray = CRM_Core_BAO_Mapping::getMappings(CRM_Core_OptionGroup::getValue('mapping_type',
80 'Import ' . static::IMPORT_ENTITY,
81 'name'
82 ));
83 $this->assign('savedMapping', $mappingArray);
84 $this->add('select', 'savedMapping', ts('Mapping Option'), array('' => ts('- select -')) + $mappingArray);
85
86 if ($loadedMapping = $this->get('loadedMapping')) {
87 $this->assign('loadedMapping', $loadedMapping);
88 $this->setDefaults(array('savedMapping' => $loadedMapping));
89 }
90
91 //build date formats
92 CRM_Core_Form_Date::buildAllowedDateFormats($this);
93
94 $this->addButtons(array(
95 array(
96 'type' => 'upload',
97 'name' => ts('Continue'),
98 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
99 'isDefault' => TRUE,
100 ),
101 array(
102 'type' => 'cancel',
103 'name' => ts('Cancel'),
104 ),
105 )
106 );
107 }
108
109 /**
110 * A long-winded way to add one radio element to the form.
111 */
112 protected function addContactTypeSelector() {
113 //contact types option
114 $contactOptions = array();
115 if (CRM_Contact_BAO_ContactType::isActive('Individual')) {
116 $contactOptions[] = $this->createElement('radio',
117 NULL, NULL, ts('Individual'), CRM_Import_Parser::CONTACT_INDIVIDUAL
118 );
119 }
120 if (CRM_Contact_BAO_ContactType::isActive('Household')) {
121 $contactOptions[] = $this->createElement('radio',
122 NULL, NULL, ts('Household'), CRM_Import_Parser::CONTACT_HOUSEHOLD
123 );
124 }
125 if (CRM_Contact_BAO_ContactType::isActive('Organization')) {
126 $contactOptions[] = $this->createElement('radio',
127 NULL, NULL, ts('Organization'), CRM_Import_Parser::CONTACT_ORGANIZATION
128 );
129 }
130
131 $this->addGroup($contactOptions, 'contactType',
132 ts('Contact Type')
133 );
134
135 $this->setDefaults(array(
136 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL,
137 ));
138 }
139
140 /**
141 * Store form values.
142 *
143 * @param array $names
144 */
145 protected function storeFormValues($names) {
146 foreach ($names as $name) {
147 $this->set($name, $this->controller->exportValue($this->_name, $name));
148 }
149 }
150
151 /**
152 * Common form postProcess.
153 *
154 * @param string $parserClassName
155 */
156 protected function submitFileForMapping($parserClassName) {
157 $this->controller->resetPage('MapField');
158
159 $fileName = $this->controller->exportValue($this->_name, 'uploadFile');
160 $skipColumnHeader = $this->controller->exportValue($this->_name, 'skipColumnHeader');
161
162 $session = CRM_Core_Session::singleton();
163 $session->set("dateTypes", $this->get('dateFormats'));
164
165 $separator = $this->controller->exportValue($this->_name, 'fieldSeparator');
166
167 $mapper = array();
168
169 $parser = new $parserClassName($mapper);
170 $parser->setMaxLinesToProcess(100);
171 $parser->run($fileName,
172 $separator,
173 $mapper,
174 $skipColumnHeader,
175 CRM_Import_Parser::MODE_MAPFIELD,
176 $this->get('contactType')
177 );
178
179 // add all the necessary variables to the form
180 $parser->set($this);
181 }
182
183 /**
184 * Return a descriptive name for the page, used in wizard header.
185 *
186 * @return string
187 */
188 public function getTitle() {
189 return ts('Upload Data');
190 }
191
192 }