-
Notifications
You must be signed in to change notification settings - Fork 36
/
csv.php
75 lines (64 loc) · 2.17 KB
/
csv.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
<?php
class CsvHelper extends AppHelper{
/**
* fields to ignore
*
* @var unknown_type
*/
var $ignore = array(
'id'
);
var $helpers = array(
'Session'
);
/**
* convert data to csv
*
* @param array $rows the data from a find
* @param $params
*/
function output($rows = null, $params = array(), $generated = true){
if(!$rows || empty($params)){
return false;
}
$row = array();
if (!empty($rows)){
foreach($params['needed'][key($params['needed'])] as $head){
if (!in_array($head, $this->ignore)){
if($head == 'id'){
$parts[] = __(Inflector::humanize(key($params['needed'])), true).' #';
continue;
}
$parts[] = __(Inflector::humanize(str_replace('_id', ' #', $head)), true);
}
}
$row[] = implode(',', $parts);
foreach($rows as $k => $array){
$parts = array();
foreach($array[key($params['needed'])] as $field => $value){
if (!in_array($field, $this->ignore)){
if($field == 'id'){
$parts[] = str_pad($value, 5, 0, STR_PAD_LEFT);
}
else if (strpos($field, '_id') && in_array($field, $params['needed'][key($params['needed'])])){
$parts[] = $array[Inflector::camelize(str_replace('_id', '' , $field))][ClassRegistry::init(Inflector::camelize(str_replace('_id', '' , $field)))->displayField];
}
else if (in_array($field, $params['needed'][key($params['needed'])])){
$parts[] = $value;
}
else{
$parts[] = '';
}
}
}
$row[] = implode(',', $parts);
unset($parts);
}
}
if($generated){
$row[] = '';
$row[] = sprintf(__('Generated on the %s at %s by %s', true), date('Y-m-d'), date('H:m:s'), $this->Session->read('Auth.User.username'));
}
return $csv = implode("\r\n", $row);
}
}