3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
30 * @copyright CiviCRM LLC (c) 2004-2017
34 * Base class for upload-only import forms (all but Contact import).
36 abstract class CRM_Import_Form_DataSource
extends CRM_Core_Form
{
39 * Set variables up before form is built.
41 public function preProcess() {
42 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this, FALSE);
45 $params .= "&id={$this->_id}";
47 CRM_Core_Session
::singleton()->pushUserContext(CRM_Utils_System
::url(static::PATH
, $params));
49 // check for post max size
50 CRM_Utils_Number
::formatUnitSize(ini_get('post_max_size'), TRUE);
54 * Common form elements.
56 public function buildQuickForm() {
57 $config = CRM_Core_Config
::singleton();
59 $uploadFileSize = CRM_Utils_Number
::formatUnitSize($config->maxFileSize
. 'm', TRUE);
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);
65 $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
67 $this->assign('uploadSize', $uploadSize);
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(
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');
78 $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
80 $this->add('text', 'fieldSeparator', ts('Import Field Separator'), array('size' => 2), TRUE);
81 $this->setDefaults(array('fieldSeparator' => $config->fieldSeparator
));
83 //get the saved mapping details
84 $mappingArray = CRM_Core_BAO_Mapping
::getMappings(CRM_Core_OptionGroup
::getValue('mapping_type',
85 'Import ' . static::IMPORT_ENTITY
,
88 $this->assign('savedMapping', $mappingArray);
89 $this->add('select', 'savedMapping', ts('Mapping Option'), array('' => ts('- select -')) +
$mappingArray);
91 if ($loadedMapping = $this->get('loadedMapping')) {
92 $this->assign('loadedMapping', $loadedMapping);
93 $this->setDefaults(array('savedMapping' => $loadedMapping));
97 CRM_Core_Form_Date
::buildAllowedDateFormats($this);
99 $this->addButtons(array(
102 'name' => ts('Continue'),
103 'spacing' => ' ',
108 'name' => ts('Cancel'),
115 * A long-winded way to add one radio element to the form.
117 protected function addContactTypeSelector() {
118 //contact types option
119 $contactOptions = array();
120 if (CRM_Contact_BAO_ContactType
::isActive('Individual')) {
121 $contactOptions[] = $this->createElement('radio',
122 NULL, NULL, ts('Individual'), CRM_Import_Parser
::CONTACT_INDIVIDUAL
125 if (CRM_Contact_BAO_ContactType
::isActive('Household')) {
126 $contactOptions[] = $this->createElement('radio',
127 NULL, NULL, ts('Household'), CRM_Import_Parser
::CONTACT_HOUSEHOLD
130 if (CRM_Contact_BAO_ContactType
::isActive('Organization')) {
131 $contactOptions[] = $this->createElement('radio',
132 NULL, NULL, ts('Organization'), CRM_Import_Parser
::CONTACT_ORGANIZATION
136 $this->addGroup($contactOptions, 'contactType',
140 $this->setDefaults(array(
141 'contactType' => CRM_Import_Parser
::CONTACT_INDIVIDUAL
,
148 * @param array $names
150 protected function storeFormValues($names) {
151 foreach ($names as $name) {
152 $this->set($name, $this->controller
->exportValue($this->_name
, $name));
157 * Common form postProcess.
159 * @param string $parserClassName
161 * @param string|null $entity
162 * Entity to set for paraser currently only for custom import
164 protected function submitFileForMapping($parserClassName, $entity = NULL) {
165 $this->controller
->resetPage('MapField');
167 $fileName = $this->controller
->exportValue($this->_name
, 'uploadFile');
168 $skipColumnHeader = $this->controller
->exportValue($this->_name
, 'skipColumnHeader');
170 $session = CRM_Core_Session
::singleton();
171 $session->set("dateTypes", $this->get('dateFormats'));
173 $separator = $this->controller
->exportValue($this->_name
, 'fieldSeparator');
177 $parser = new $parserClassName($mapper);
179 $parser->setEntity($this->get($entity));
181 $parser->setMaxLinesToProcess(100);
182 $parser->run($fileName,
186 CRM_Import_Parser
::MODE_MAPFIELD
,
187 $this->get('contactType')
190 // add all the necessary variables to the form
195 * Return a descriptive name for the page, used in wizard header.
199 public function getTitle() {
200 return ts('Upload Data');