|
1
|
|
|
<?php |
|
2
|
|
|
/************************************************************************ |
|
3
|
|
|
* OVIDENTIA http://www.ovidentia.org * |
|
4
|
|
|
************************************************************************ |
|
5
|
|
|
* Copyright (c) 2003 by CANTICO ( http://www.cantico.fr ) * |
|
6
|
|
|
* * |
|
7
|
|
|
* This file is part of Ovidentia. * |
|
8
|
|
|
* * |
|
9
|
|
|
* Ovidentia is free software; you can redistribute it and/or modify * |
|
10
|
|
|
* it under the terms of the GNU General Public License as published by * |
|
11
|
|
|
* the Free Software Foundation; either version 2, or (at your option) * |
|
12
|
|
|
* any later version. * |
|
13
|
|
|
* * |
|
14
|
|
|
* This program 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 General Public License for more details. * |
|
18
|
|
|
* * |
|
19
|
|
|
* You should have received a copy of the GNU General Public License * |
|
20
|
|
|
* along with this program; if not, write to the Free Software * |
|
21
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,* |
|
22
|
|
|
* USA. * |
|
23
|
|
|
************************************************************************/ |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
class absences_Csv |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $separator; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $sepdec; |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
protected function csvEncode($str) |
|
41
|
|
|
{ |
|
42
|
|
|
$str = str_replace("\n"," ",$str); |
|
43
|
|
|
return '"'.str_replace('"','""',$str).'"'; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* output an array on raw values |
|
49
|
|
|
* @param array $arr |
|
50
|
|
|
* |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function outputArr(Array $arr) |
|
53
|
|
|
{ |
|
54
|
|
|
$arr = $this->encodeFloats($arr); |
|
55
|
|
|
|
|
56
|
|
|
foreach ($arr as &$v) { |
|
57
|
|
|
$v = $this->csvEncode($v); |
|
58
|
|
|
} |
|
59
|
|
|
echo implode($this->separator, $arr)."\n"; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* |
|
65
|
|
|
* @return boolean |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function isRowEmpty(Array $currentRow) |
|
68
|
|
|
{ |
|
69
|
|
|
foreach($currentRow as &$val) |
|
70
|
|
|
{ |
|
71
|
|
|
if (is_float($val)) |
|
72
|
|
|
{ |
|
73
|
|
|
if ($val > 0) { |
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
protected function encodeFloats(Array $currentRow) |
|
84
|
|
|
{ |
|
85
|
|
|
// encode float |
|
86
|
|
|
foreach($currentRow as &$val) { |
|
87
|
|
|
if (is_float($val)) { |
|
88
|
|
|
$val = number_format($val, 1 , $this->sepdec , '' ); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $currentRow; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
View Code Duplication |
protected function getAgentDirValue(absences_Agent $agent, $fieldname) |
|
|
|
|
|
|
100
|
|
|
{ |
|
101
|
|
|
$direntry = $agent->getDirEntry(); |
|
102
|
|
|
|
|
103
|
|
|
if (isset($direntry[$fieldname])) |
|
104
|
|
|
{ |
|
105
|
|
|
return $direntry[$fieldname]['value']; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return ''; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $quantity_unit |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
View Code Duplication |
protected function getUnit($quantity_unit) |
|
|
|
|
|
|
119
|
|
|
{ |
|
120
|
|
|
switch($quantity_unit) |
|
121
|
|
|
{ |
|
122
|
|
|
case 'D': return absences_translate('days'); |
|
123
|
|
|
case 'H': return absences_translate('hours'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return ''; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
protected function setHeaders($filename) |
|
131
|
|
|
{ |
|
132
|
|
|
header("Content-Disposition: attachment; filename=\"".$filename.".csv\""."\n"); |
|
133
|
|
|
header("Content-Type: text/csv"."\n"); |
|
134
|
|
|
header("Content-transfert-encoding: binary"."\n"); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
protected function date($strDate) { |
|
140
|
|
|
return bab_shortDate(bab_mktime($strDate), false); |
|
141
|
|
|
} |
|
142
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.