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