forked from dalpo/cakephp_csv_datasource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CsvSource.php
executable file
·279 lines (240 loc) · 5.96 KB
/
CsvSource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/**
* CakePHP CSV datasource
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* Originally based on http://bakery.cakephp.org/articles/view/csv-datasource-for-reading-your-csv-files
*
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @author Andrea Dal Ponte <dalpo85@gmail.com> - http://github.com/dalpo/cakephp_csv_datasource
*
**/
class CsvSource extends DataSource {
/**
* Description string for this Data Source.
*
* @public unknown_type
*/
public $description = "CSV file datasource";
public $delimiter = null; // delimiter between the columns
public $maxCol = 0;
public $fields = null;
public $limit = false;
public $page = 1;
protected $_fileHeader = null;
protected $_rowNumber = 0;
/**
* Default configuration.
*
* @public unknown_type
*/
protected $_baseConfig = array(
'datasource' => 'csv',
'path' => '.', // local path on the server relative to WWW_ROOT
'recursive' => false, // only false is supported at the moment
'delimiter' => ',',
'header_row' => 1
);
/**
* Constructor
*/
public function __construct($config = null, $autoConnect = true) {
// included to fix end of line errors when csv files have been edited on a Mac
ini_set('auto_detect_line_endings', true);
parent::__construct($config);
$this->connected = false;
$this->delimiter = $this->config['delimiter'];
if ($autoConnect) {
return $this->connect();
} else {
return true;
}
}
/**
* Destructor
*/
public function __destruct() {
parent::__destruct();
}
/**
* Open csv file
*/
public function connect() {
$this->connected = false;
if ($this->_initConnection()) {
$this->connected = true;
$this->__getDescriptionFromFirstLine();
}
return $this->connected;
}
/**
* Returns a Model description (metadata) or null if none found.
*
* @return mixed
**/
public function describe($model) {
if (! $this->fields) {
$this->__getDescriptionFromFirstLine($model);
}
return $this->fields;
}
/**
* __getDescriptionFromFirstLine and store into class variables
*
*/
private function __getDescriptionFromFirstLine() {
if (! $this->config['header_row']) {
return false;
}
if (! $this->connected || $this->_rowNumber != $this->config['header_row']) {
$this->_initConnection();
}
$columns = $this->_getNextRow();
$this->fields = $columns;
$this->maxCol = count($columns);
return (bool)$this->maxCol;
}
protected function _initConnection() {
if ($this->connected) {
$this->close();
}
$this->_rowNumber = 0;
$this->_fileHeader = '';
if ($this->connection = fopen($this->config['path'], "r+")) {
while (! feof($this->connection) && ($this->_rowNumber + 1) < $this->config['header_row']) {
$this->_rowNumber++;
$this->_fileHeader .= fgets($this->connection);
}
return $this->connected = true;
} else {
return $this->connected = false;
}
}
/**
* Closes the current datasource connection.
*/
public function close() {
if ($this->connected || $this->connection || $this->_rowNumber) {
@fclose($this->connection);
$this->connection = null;
$this->connected = false;
$this->_rowNumber = 0;
}
return true;
}
/**
*
*/
public function read(Model $model, $queryData = array(), $recursive = null) {
if (!$this->connected) {
$this->_initConnection();
}
if (isset($queryData['page']) && !empty($queryData['page'])) {
$this->page = $queryData['page'];
}
// get the limit
if (isset($queryData['limit']) && !empty($queryData['limit'])) {
$this->limit = (int)$queryData['limit'];
}
// generate an index array of all wanted fields
if (empty($queryData['fields'])) {
$fields = $this->fields;
$allFields = true;
} else {
$fields = $queryData['fields'];
$allFields = false;
$_fieldIndex = array();
$index = 0;
foreach ($this->fields as $field) {
if (in_array($field, $fields)) {
$_fieldIndex[] = $index;
}
$index++;
}
}
// retrive data
$resultSet = array();
$recordCount = 0;
while (
(! $this->limit || ( $recordCount < ($this->limit * $this->page) - 1 ) ) && ($data = $this->_getNextRow())
) {
$recordCount = $this->_rowNumber - $this->config['header_row'];
if ($this->_rowNumber <= $this->config['header_row']) {
continue;
}
if ($this->limit) {
$_currentPage = floor($recordCount / $this->limit) + 1;
if ($this->page > $_currentPage) {
continue;
}
}
$record = array();
if ($allFields) {
$i = 0;
$record['id'] = $recordCount;
foreach ($fields as $field) {
$record[$field] = $data[$i++];
}
} else {
$record['id'] = $recordCount;
if (count($_fieldIndex) > 0) {
foreach ($_fieldIndex as $i) {
$record[$this->fields[$i]] = $data[$i];
}
}
}
$record['_row_number'] = $this->_rowNumber;
$resultSet[] = array($model->alias => $record);
unset($record);
}
$this->_initConnection();
$this->data = $resultSet;
if ($model->findQueryType === 'count') {
return array(array(array('count' => count($resultSet))));
} else {
return $this->data;
}
}
/**
* Private helper method to remove query metadata in given data array.
*
* @param array $data
* @return array
*/
private function __scrubQueryData($data) {
foreach (array('conditions', 'fields', 'joins', 'order', 'limit', 'offset', 'group') as $key) {
if (! isset($data[$key]) || empty($data[$key])) {
$data[$key] = array();
}
}
return $data;
}
/**
* Get the next cvs row
*/
protected function _getNextRow() {
if (! $this->connected) {
$this->_initConnection();
}
if (! feof($this->connection) && ($data = fgetcsv($this->connection, 0, $this->delimiter))) {
$this->_rowNumber++;
return $data;
} else {
return false;
}
}
/**
* Calculate
*
* @param Model $model
* @param mixed $func
* @param array $params
* @return array
* @access public
*/
public function calculate(&$model, $func, $params = array()) {
return array('count' => true);
}
}