CRM-15789 - Remove ascii art from button text
[civicrm-core.git] / CRM / Activity / Import / Form / DataSource.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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_Activity_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/import/activity', '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 //Setting Upload File Size
60 $config = CRM_Core_Config::singleton();
61
62 $uploadFileSize = CRM_Core_Config_Defaults::formatUnitSize($config->maxFileSize . 'm', TRUE);
63 $uploadSize = round(($uploadFileSize / (1024 * 1024)), 2);
64
65 $this->assign('uploadSize', $uploadSize);
66 $this->setMaxFileSize($uploadFileSize);
67 $this->add('File', 'uploadFile', ts('Import Data File'), 'size=30 maxlength=255', TRUE);
68 $this->addRule('uploadFile', ts('File size should be less than %1 MBytes (%2 bytes)', array(1 => $uploadSize, 2 => $uploadFileSize)), 'maxfilesize', $uploadFileSize);
69 $this->addRule('uploadFile', ts('A valid file must be uploaded.'), 'uploadedfile');
70 $this->addRule('uploadFile', ts('Input file must be in CSV format'), 'utf8File');
71
72 $this->addElement('checkbox', 'skipColumnHeader', ts('First row contains column headers'));
73
74 $duplicateOptions = array();
75 $duplicateOptions[] = $this->createElement('radio',
76 NULL, NULL, ts('Skip'), CRM_Import_Parser::DUPLICATE_SKIP
77 );
78 $duplicateOptions[] = $this->createElement('radio',
79 NULL, NULL, ts('Update'), CRM_Import_Parser::DUPLICATE_UPDATE
80 );
81 $duplicateOptions[] = $this->createElement('radio',
82 NULL, NULL, ts('Fill'), CRM_Import_Parser::DUPLICATE_FILL
83 );
84
85 $this->addGroup($duplicateOptions, 'onDuplicate',
86 ts('On duplicate entries')
87 );
88
89 //get the saved mapping details
90 $mappingArray = CRM_Core_BAO_Mapping::getMappings(CRM_Core_OptionGroup::getValue('mapping_type',
91 'Import Activity',
92 'name'
93 ));
94 $this->assign('savedMapping', $mappingArray);
95 $this->add('select', 'savedMapping', ts('Mapping Option'), array('' => ts('- select -')) + $mappingArray);
96
97 if ($loadeMapping = $this->get('loadedMapping')) {
98 $this->assign('loadedMapping', $loadeMapping);
99 $this->setDefaults(array('savedMapping' => $loadeMapping));
100 }
101
102 $this->setDefaults(array(
103 'onDuplicate' => CRM_Import_Parser::DUPLICATE_SKIP,
104 ));
105
106 //build date formats
107 CRM_Core_Form_Date::buildAllowedDateFormats($this);
108
109 $this->addButtons(array(
110 array(
111 'type' => 'upload',
112 'name' => ts('Continue'),
113 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
114 'isDefault' => TRUE,
115 ),
116 array(
117 'type' => 'cancel',
118 'name' => ts('Cancel'),
119 ),
120 )
121 );
122 }
123
124 /**
125 * Process the uploaded file
126 *
127 * @return void
128 */
129 public function postProcess() {
130 $this->controller->resetPage('MapField');
131
132 $fileName = $this->controller->exportValue($this->_name, 'uploadFile');
133 $skipColumnHeader = $this->controller->exportValue($this->_name, 'skipColumnHeader');
134 $onDuplicate = $this->controller->exportValue($this->_name, 'onDuplicate');
135 $dateFormats = $this->controller->exportValue($this->_name, 'dateFormats');
136 $savedMapping = $this->controller->exportValue($this->_name, 'savedMapping');
137
138 $this->set('onDuplicate', $onDuplicate);
139 $this->set('dateFormats', $dateFormats);
140 $this->set('savedMapping', $savedMapping);
141
142 $session = CRM_Core_Session::singleton();
143 $session->set("dateTypes", $dateFormats);
144
145 $config = CRM_Core_Config::singleton();
146 $seperator = $config->fieldSeparator;
147
148 $mapper = array();
149
150 $parser = new CRM_Activity_Import_Parser_Activity($mapper);
151 $parser->setMaxLinesToProcess(100);
152 $parser->run($fileName, $seperator,
153 $mapper,
154 $skipColumnHeader,
155 CRM_Import_Parser::MODE_MAPFIELD
156 );
157
158 // add all the necessary variables to the form
159 $parser->set($this);
160 }
161
162 /**
163 * Return a descriptive name for the page, used in wizard header
164 *
165 * @return string
166 */
167 public function getTitle() {
168 return ts('Upload Data');
169 }
170 }