Merge pull request #10439 from jitendrapurohit/CRM-20657
[civicrm-core.git] / CRM / Import / Form / DataSource.php
CommitLineData
81c3812a
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
81c3812a 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
81c3812a
CW
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/**
81c3812a 29 * @package CRM
0f03f337 30 * @copyright CiviCRM LLC (c) 2004-2017
81c3812a
CW
31 */
32
33/**
2b4bc760 34 * Base class for upload-only import forms (all but Contact import).
81c3812a
CW
35 */
36abstract class CRM_Import_Form_DataSource extends CRM_Core_Form {
37
38 /**
39 * Set variables up before form is built.
81c3812a
CW
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
2e966dd5 50 CRM_Utils_Number::formatUnitSize(ini_get('post_max_size'), TRUE);
81c3812a
CW
51 }
52
53 /**
54 * Common form elements.
81c3812a
CW
55 */
56 public function buildQuickForm() {
57 $config = CRM_Core_Config::singleton();
58
2e966dd5 59 $uploadFileSize = CRM_Utils_Number::formatUnitSize($config->maxFileSize . 'm', TRUE);
ebcf0a88
JP
60
61 //Fetch uploadFileSize from php_ini when $config->maxFileSize is set to "no limit".
62 if (empty($uploadFileSize)) {
63 $uploadFileSize = CRM_Utils_Number::formatUnitSize(ini_get('upload_max_filesize'), TRUE);
64 }
81c3812a
CW
65 $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
66
67 $this->assign('uploadSize', $uploadSize);
68
69 $this->add('File', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE);
70 $this->setMaxFileSize($uploadFileSize);
71 $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', array(
72 1 => $uploadSize,
73 2 => $uploadFileSize,
74 )), 'maxfilesize', $uploadFileSize);
75 $this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile');
76 $this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File');
77
78 $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
79
19a68bae
CW
80 $this->add('text', 'fieldSeparator', ts('Import Field Separator'), array('size' => 2), TRUE);
81 $this->setDefaults(array('fieldSeparator' => $config->fieldSeparator));
82
81c3812a 83 //get the saved mapping details
f8df7165 84 $mappingArray = CRM_Core_BAO_Mapping::getMappings('Import ' . static::IMPORT_ENTITY);
81c3812a
CW
85 $this->assign('savedMapping', $mappingArray);
86 $this->add('select', 'savedMapping', ts('Mapping Option'), array('' => ts('- select -')) + $mappingArray);
87
88 if ($loadedMapping = $this->get('loadedMapping')) {
89 $this->assign('loadedMapping', $loadedMapping);
90 $this->setDefaults(array('savedMapping' => $loadedMapping));
91 }
92
93 //build date formats
94 CRM_Core_Form_Date::buildAllowedDateFormats($this);
95
96 $this->addButtons(array(
97 array(
98 'type' => 'upload',
99 'name' => ts('Continue'),
100 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
101 'isDefault' => TRUE,
102 ),
103 array(
104 'type' => 'cancel',
105 'name' => ts('Cancel'),
106 ),
107 )
108 );
109 }
110
111 /**
2b4bc760 112 * A long-winded way to add one radio element to the form.
81c3812a
CW
113 */
114 protected function addContactTypeSelector() {
115 //contact types option
116 $contactOptions = array();
117 if (CRM_Contact_BAO_ContactType::isActive('Individual')) {
118 $contactOptions[] = $this->createElement('radio',
119 NULL, NULL, ts('Individual'), CRM_Import_Parser::CONTACT_INDIVIDUAL
120 );
121 }
122 if (CRM_Contact_BAO_ContactType::isActive('Household')) {
123 $contactOptions[] = $this->createElement('radio',
124 NULL, NULL, ts('Household'), CRM_Import_Parser::CONTACT_HOUSEHOLD
125 );
126 }
127 if (CRM_Contact_BAO_ContactType::isActive('Organization')) {
128 $contactOptions[] = $this->createElement('radio',
129 NULL, NULL, ts('Organization'), CRM_Import_Parser::CONTACT_ORGANIZATION
130 );
131 }
132
133 $this->addGroup($contactOptions, 'contactType',
134 ts('Contact Type')
135 );
136
137 $this->setDefaults(array(
138 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL,
139 ));
140 }
141
142 /**
2b4bc760 143 * Store form values.
144 *
81c3812a
CW
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 /**
2b4bc760 154 * Common form postProcess.
81c3812a
CW
155 *
156 * @param string $parserClassName
cef29526
SL
157 *
158 * @param string|null $entity
159 * Entity to set for paraser currently only for custom import
81c3812a 160 */
cef29526 161 protected function submitFileForMapping($parserClassName, $entity = NULL) {
81c3812a
CW
162 $this->controller->resetPage('MapField');
163
164 $fileName = $this->controller->exportValue($this->_name, 'uploadFile');
165 $skipColumnHeader = $this->controller->exportValue($this->_name, 'skipColumnHeader');
166
167 $session = CRM_Core_Session::singleton();
168 $session->set("dateTypes", $this->get('dateFormats'));
169
19a68bae 170 $separator = $this->controller->exportValue($this->_name, 'fieldSeparator');
81c3812a
CW
171
172 $mapper = array();
173
174 $parser = new $parserClassName($mapper);
cef29526
SL
175 if ($entity) {
176 $parser->setEntity($this->get($entity));
177 }
81c3812a
CW
178 $parser->setMaxLinesToProcess(100);
179 $parser->run($fileName,
19a68bae 180 $separator,
81c3812a
CW
181 $mapper,
182 $skipColumnHeader,
183 CRM_Import_Parser::MODE_MAPFIELD,
184 $this->get('contactType')
185 );
186
187 // add all the necessary variables to the form
188 $parser->set($this);
189 }
190
191 /**
2b4bc760 192 * Return a descriptive name for the page, used in wizard header.
81c3812a
CW
193 *
194 * @return string
195 */
196 public function getTitle() {
197 return ts('Upload Data');
198 }
199
200}