3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
37 * This class gets the name of the file to upload
39 class CRM_Custom_Import_Form_DataSource
extends CRM_Core_Form
{
42 * Set variables up before form is built.
46 public function preProcess() {
47 $session = CRM_Core_Session
::singleton();
48 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this, FALSE);
50 $params = "reset=1&id={$this->_id}";
55 $session->pushUserContext(CRM_Utils_System
::url('civicrm/import/custom', $params));
57 // check for post max size
58 CRM_Core_Config_Defaults
::formatUnitSize(ini_get('post_max_size'), TRUE);
64 public function setDefaultValues() {
65 $config = CRM_Core_Config
::singleton();
67 'contactType' => CRM_Import_Parser
::CONTACT_INDIVIDUAL
,
68 'fieldSeparator' => $config->fieldSeparator
,
69 'multipleCustomData' => $this->_id
,
72 if ($loadeMapping = $this->get('loadedMapping')) {
73 $this->assign('loadedMapping', $loadeMapping);
74 $defaults['savedMapping'] = $loadeMapping;
81 * Build the form object.
85 public function buildQuickForm() {
86 $multipleCustomData = CRM_Core_BAO_CustomGroup
::getMultipleFieldGroup();
87 $this->add('select', 'multipleCustomData', ts('Multi-value Custom Data'), array('' => ts('- select -')) +
$multipleCustomData, TRUE);
89 //Setting Upload File Size
90 $config = CRM_Core_Config
::singleton();
92 $uploadFileSize = CRM_Core_Config_Defaults
::formatUnitSize($config->maxFileSize
. 'm', TRUE);
93 $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
95 $this->assign('uploadSize', $uploadSize);
96 $this->add('File', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE);
97 $this->setMaxFileSize($uploadFileSize);
98 $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', array(
100 2 => $uploadFileSize,
101 )), 'maxfilesize', $uploadFileSize);
102 $this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile');
103 $this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File');
105 $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
107 //get the saved mapping details
108 $mappingArray = CRM_Core_BAO_Mapping
::getMappings(CRM_Core_OptionGroup
::getValue('mapping_type',
109 'Import Multi value custom data',
112 $this->assign('savedMapping', $mappingArray);
113 $this->add('select', 'savedMapping', ts('Mapping Option'), array('' => ts('- select -')) +
$mappingArray);
115 if ($loadeMapping = $this->get('loadedMapping')) {
116 $this->assign('loadedMapping', $loadeMapping);
117 $this->setDefaults(array('savedMapping' => $loadeMapping));
120 //contact types option
121 $contactOptions = array();
122 if (CRM_Contact_BAO_ContactType
::isActive('Individual')) {
123 $contactOptions[] = $this->createElement('radio',
124 NULL, NULL, ts('Individual'), CRM_Import_Parser
::CONTACT_INDIVIDUAL
127 if (CRM_Contact_BAO_ContactType
::isActive('Household')) {
128 $contactOptions[] = $this->createElement('radio',
129 NULL, NULL, ts('Household'), CRM_Import_Parser
::CONTACT_HOUSEHOLD
132 if (CRM_Contact_BAO_ContactType
::isActive('Organization')) {
133 $contactOptions[] = $this->createElement('radio',
134 NULL, NULL, ts('Organization'), CRM_Import_Parser
::CONTACT_ORGANIZATION
138 $this->addGroup($contactOptions, 'contactType',
142 $this->setDefaults(array(
143 'contactType' => CRM_Import_Parser
::CONTACT_INDIVIDUAL
,
147 CRM_Core_Form_Date
::buildAllowedDateFormats($this);
149 $this->addButtons(array(
152 'name' => ts('Continue'),
153 'spacing' => ' ',
158 'name' => ts('Cancel'),
165 * Process the uploaded file.
169 public function postProcess() {
170 $this->controller
->resetPage('MapField');
172 $fileName = $this->controller
->exportValue($this->_name
, 'uploadFile');
173 $skipColumnHeader = $this->controller
->exportValue($this->_name
, 'skipColumnHeader');
174 $contactType = $this->controller
->exportValue($this->_name
, 'contactType');
175 $dateFormats = $this->controller
->exportValue($this->_name
, 'dateFormats');
176 $savedMapping = $this->controller
->exportValue($this->_name
, 'savedMapping');
177 $multipleCustomData = $this->controller
->exportValue($this->_name
, 'multipleCustomData');
179 $this->set('contactType', $contactType);
180 $this->set('dateFormats', $dateFormats);
181 $this->set('savedMapping', $savedMapping);
182 $this->set('multipleCustomData', $multipleCustomData);
184 $session = CRM_Core_Session
::singleton();
185 $session->set("dateTypes", $dateFormats);
187 $config = CRM_Core_Config
::singleton();
188 $seperator = $config->fieldSeparator
;
192 $parser = new CRM_Custom_Import_Parser_Api($mapper);
193 $parser->setEntity($multipleCustomData);
195 $parser->setMaxLinesToProcess(100);
196 $parser->run($fileName, $seperator,
199 CRM_Import_Parser
::MODE_MAPFIELD
, $contactType
202 // add all the necessary variables to the form
207 * Return a descriptive name for the page, used in wizard header
211 public function getTitle() {
212 return ts('Upload Data');