whitepace y, std is => array( on same line,
[civicrm-core.git] / CRM / Member / Import / Form / DataSource.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class gets the name of the file to upload
38 */
39 class CRM_Member_Import_Form_DataSource extends CRM_Core_Form {
40
41 /**
42 * Set variables up before form is built
43 *
44 * @return void
45 * @access public
46 */
47 public function preProcess() {
48 $session = CRM_Core_Session::singleton();
49 $session->pushUserContext(CRM_Utils_System::url('civicrm/member/import', 'reset=1'));
50 // check for post max size
51 CRM_Core_Config_Defaults::formatUnitSize(ini_get('post_max_size'), TRUE);
52 }
53
54 /**
55 * Build the form object
56 *
57 * @return void
58 * @access public
59 */
60 public function buildQuickForm() {
61 //Setting Upload File Size
62 $config = CRM_Core_Config::singleton();
63
64 $uploadFileSize = CRM_Core_Config_Defaults::formatUnitSize($config->maxFileSize.'m');
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(1 => $uploadSize, 2 => $uploadFileSize)), 'maxfilesize', $uploadFileSize);
72 $this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile');
73 $this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File');
74
75 $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
76
77 $duplicateOptions = array();
78 $duplicateOptions[] = $this->createElement('radio',
79 NULL, NULL, ts('Insert new Membership'), CRM_Import_Parser::DUPLICATE_SKIP
80 );
81 $duplicateOptions[] = $this->createElement('radio',
82 NULL, NULL, ts('Update existing Membership'), CRM_Import_Parser::DUPLICATE_UPDATE
83 );
84
85 $this->addGroup($duplicateOptions, 'onDuplicate',
86 ts('Import mode')
87 );
88 $this->setDefaults(array(
89 'onDuplicate' => CRM_Import_Parser::DUPLICATE_SKIP,
90 ));
91
92 //get the saved mapping details
93 $mappingArray = CRM_Core_BAO_Mapping::getMappings(CRM_Core_OptionGroup::getValue('mapping_type',
94 'Import Membership',
95 'name'
96 ));
97 $this->assign('savedMapping', $mappingArray);
98 $this->add('select', 'savedMapping', ts('Mapping Option'), array('' => ts('- select -')) + $mappingArray);
99
100 if ($loadeMapping = $this->get('loadedMapping')) {
101 $this->assign('loadedMapping', $loadeMapping);
102 $this->setDefaults(array('savedMapping' => $loadeMapping));
103 }
104
105 //contact types option
106 $contactOptions = array();
107 if (CRM_Contact_BAO_ContactType::isActive('Individual')) {
108 $contactOptions[] = $this->createElement('radio',
109 NULL, NULL, ts('Individual'), CRM_Import_Parser::CONTACT_INDIVIDUAL
110 );
111 }
112 if (CRM_Contact_BAO_ContactType::isActive('Household')) {
113 $contactOptions[] = $this->createElement('radio',
114 NULL, NULL, ts('Household'), CRM_Import_Parser::CONTACT_HOUSEHOLD
115 );
116 }
117 if (CRM_Contact_BAO_ContactType::isActive('Organization')) {
118 $contactOptions[] = $this->createElement('radio',
119 NULL, NULL, ts('Organization'), CRM_Import_Parser::CONTACT_ORGANIZATION
120 );
121 }
122
123 $this->addGroup($contactOptions, 'contactType',
124 ts('Contact Type')
125 );
126
127 $this->setDefaults(array(
128 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL,
129 ));
130
131 //build date formats
132 CRM_Core_Form_Date::buildAllowedDateFormats($this);
133
134 $this->addButtons(array(
135 array(
136 'type' => 'upload',
137 'name' => ts('Continue >>'),
138 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
139 'isDefault' => TRUE,
140 ),
141 array(
142 'type' => 'cancel',
143 'name' => ts('Cancel'),
144 ),
145 )
146 );
147 }
148
149 /**
150 * Process the uploaded file
151 *
152 * @return void
153 * @access public
154 */
155 public function postProcess() {
156 $this->controller->resetPage('MapField');
157
158 $fileName = $this->controller->exportValue($this->_name, 'uploadFile');
159 $skipColumnHeader = $this->controller->exportValue($this->_name, 'skipColumnHeader');
160 $onDuplicate = $this->controller->exportValue($this->_name, 'onDuplicate');
161 $contactType = $this->controller->exportValue($this->_name, 'contactType');
162 $dateFormats = $this->controller->exportValue($this->_name, 'dateFormats');
163 $savedMapping = $this->controller->exportValue($this->_name, 'savedMapping');
164
165 $this->set('onDuplicate', $onDuplicate);
166 $this->set('contactType', $contactType);
167 $this->set('dateFormats', $dateFormats);
168 $this->set('savedMapping', $savedMapping);
169
170 $session = CRM_Core_Session::singleton();
171 $session->set("dateTypes", $dateFormats);
172
173 $config = CRM_Core_Config::singleton();
174 $seperator = $config->fieldSeparator;
175
176 $mapper = array();
177
178 $parser = new CRM_Member_Import_Parser_Membership($mapper);
179 $parser->setMaxLinesToProcess(100);
180 $parser->run($fileName, $seperator,
181 $mapper,
182 $skipColumnHeader,
183 CRM_Import_Parser::MODE_MAPFIELD, $contactType
184 );
185
186 // add all the necessary variables to the form
187 $parser->set($this);
188 }
189
190 /**
191 * Return a descriptive name for the page, used in wizard header
192 *
193 * @return string
194 * @access public
195 */
196 public function getTitle() {
197 return ts('Upload Data');
198 }
199 }
200