Commit | Line | Data |
---|---|---|
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 | */ | |
39 | abstract 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 | ||
19a68bae CW |
82 | $this->add('text', 'fieldSeparator', ts('Import Field Separator'), array('size' => 2), TRUE); |
83 | $this->setDefaults(array('fieldSeparator' => $config->fieldSeparator)); | |
84 | ||
81c3812a CW |
85 | //get the saved mapping details |
86 | $mappingArray = CRM_Core_BAO_Mapping::getMappings(CRM_Core_OptionGroup::getValue('mapping_type', | |
87 | 'Import ' . static::IMPORT_ENTITY, | |
88 | 'name' | |
89 | )); | |
90 | $this->assign('savedMapping', $mappingArray); | |
91 | $this->add('select', 'savedMapping', ts('Mapping Option'), array('' => ts('- select -')) + $mappingArray); | |
92 | ||
93 | if ($loadedMapping = $this->get('loadedMapping')) { | |
94 | $this->assign('loadedMapping', $loadedMapping); | |
95 | $this->setDefaults(array('savedMapping' => $loadedMapping)); | |
96 | } | |
97 | ||
98 | //build date formats | |
99 | CRM_Core_Form_Date::buildAllowedDateFormats($this); | |
100 | ||
101 | $this->addButtons(array( | |
102 | array( | |
103 | 'type' => 'upload', | |
104 | 'name' => ts('Continue'), | |
105 | 'spacing' => ' ', | |
106 | 'isDefault' => TRUE, | |
107 | ), | |
108 | array( | |
109 | 'type' => 'cancel', | |
110 | 'name' => ts('Cancel'), | |
111 | ), | |
112 | ) | |
113 | ); | |
114 | } | |
115 | ||
116 | /** | |
117 | * A long-winded way to add one radio element to the form | |
118 | */ | |
119 | protected function addContactTypeSelector() { | |
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 | |
125 | ); | |
126 | } | |
127 | if (CRM_Contact_BAO_ContactType::isActive('Household')) { | |
128 | $contactOptions[] = $this->createElement('radio', | |
129 | NULL, NULL, ts('Household'), CRM_Import_Parser::CONTACT_HOUSEHOLD | |
130 | ); | |
131 | } | |
132 | if (CRM_Contact_BAO_ContactType::isActive('Organization')) { | |
133 | $contactOptions[] = $this->createElement('radio', | |
134 | NULL, NULL, ts('Organization'), CRM_Import_Parser::CONTACT_ORGANIZATION | |
135 | ); | |
136 | } | |
137 | ||
138 | $this->addGroup($contactOptions, 'contactType', | |
139 | ts('Contact Type') | |
140 | ); | |
141 | ||
142 | $this->setDefaults(array( | |
143 | 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL, | |
144 | )); | |
145 | } | |
146 | ||
147 | /** | |
148 | * @param array $names | |
149 | */ | |
150 | protected function storeFormValues($names) { | |
151 | foreach ($names as $name) { | |
152 | $this->set($name, $this->controller->exportValue($this->_name, $name)); | |
153 | } | |
154 | } | |
155 | ||
156 | /** | |
157 | * Common form postProcess | |
158 | * | |
159 | * @param string $parserClassName | |
160 | */ | |
161 | protected function submitFileForMapping($parserClassName) { | |
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); | |
175 | $parser->setMaxLinesToProcess(100); | |
176 | $parser->run($fileName, | |
19a68bae | 177 | $separator, |
81c3812a CW |
178 | $mapper, |
179 | $skipColumnHeader, | |
180 | CRM_Import_Parser::MODE_MAPFIELD, | |
181 | $this->get('contactType') | |
182 | ); | |
183 | ||
184 | // add all the necessary variables to the form | |
185 | $parser->set($this); | |
186 | } | |
187 | ||
188 | /** | |
189 | * Return a descriptive name for the page, used in wizard header | |
190 | * | |
191 | * @return string | |
192 | */ | |
193 | public function getTitle() { | |
194 | return ts('Upload Data'); | |
195 | } | |
196 | ||
197 | } |