Completed
Push — master ( 68a26d...f82207 )
by Paul
03:20
created

absences_RightExportCsv   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 18
c 3
b 0
f 1
lcom 1
cbo 10
dl 0
loc 201
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 4
A selectAgents() 0 11 2
A selectAgentRights() 0 12 2
B getHeader() 0 24 2
B getRow() 0 35 4
A outputArr() 0 5 1
A download() 0 15 3
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
require_once dirname(__FILE__).'/csv.class.php';
26
27
/**
28
 * Export all rights or year rights for each agents on a list of agents
29
 *
30
 */
31
class absences_RightExportCsv extends absences_Csv
32
{
33
    
34
    /**
35
     * 
36
     * @var int
37
     */
38
    protected $org;
39
    
40
    /**
41
     * 
42
     * @var int
43
     */
44
    protected $year;
45
    
46
    
47
    /**
48
     * @var array
49
     */
50
    protected $dirFields;
51
    
52
    
53
    /**
54
     * 
55
     * @var string
56
     */
57
    protected $separator = ',';
58
    
59
    /**
60
     * 
61
     * @var string
62
     */
63
    protected $decsep = '.';
64
    
65
    /**
66
     * 
67
     * @var string
68
     */
69
    protected $outputCharset = null;
70
    
71
    /**
72
     * @param int    $org
73
     * @param int    $year
74
     * @param array  $dirFields         List of additional directory fields
75
     */
76
    public function __construct($org, $year, Array $dirFields, $separator = null, $decsep = null, $outputCharset = null)
77
    {
78
        $this->org = $org;
79
        $this->year = $year;
80
        $this->dirFields = $dirFields;
81
        
82
        if (isset($separator)) {
83
            $this->separator = $separator;
84
        }
85
        
86
        if (isset($decsep)) {
87
            $this->decsep = $decsep;
88
        }
89
        
90
        $this->outputCharset = bab_Charset::getIso();
91
        
92
        if (isset($outputCharset)) {
93
            $this->outputCharset = $outputCharset;
94
        }
95
    }
96
    
97
    
98
    /**
99
     * @return absences_AgentIterator
100
     */
101
    protected function selectAgents()
102
    {
103
        $res = new absences_AgentIterator();
104
        
105
        if (isset($this->org)) {
106
            $organization = absences_Organization::getById($this->org);
107
            $res->setOrganization($organization);
108
        }
109
        
110
        return $res;
111
    }
112
    
113
    
114
    
115
    
116
    /**
117
     * 
118
     * @param absences_Agent $agent
119
     * @return absences_AgentRightManagerIterator
120
     */
121
    protected function selectAgentRights(absences_Agent $agent)
122
    {
123
        require_once dirname(__FILE__).'/agent_right.class.php';
124
		$I = new absences_AgentRightManagerIterator;
125
		$I->setAgent($agent);
126
		
127
		if (isset($this->year)) {
128
		    $I->year = $this->year;
129
		}
130
		
131
		return $I;
132
    }
133
    
134
    
135
    /**
136
     * @return array
137
     */
138
    protected function getHeader()
139
    {
140
        $row = array(
141
            absences_translate('Lastname'),
142
            absences_translate('First name'),
143
            absences_translate('Right collection'),
144
            absences_translate('Organization'),
145
            absences_translate('Right description'),
146
            absences_translate('Type'),
147
            absences_translate('Initial quantity'),
148
    		absences_translate('Consumed'),
149
    		absences_translate('Waiting approval'),
150
    		absences_translate('Balance'),
151
    		absences_translate('Begin date'),
152
    		absences_translate('End date'),
153
    		absences_translate('Accessible')
154
        );
155
        
156
        foreach ($this->dirFields as $field) {
157
            $row[] = $field;
158
        }
159
        
160
        return $row;
161
    }
162
    
163
    /**
164
     * @return array
165
     */
166
    protected function getRow(absences_Agent $agent, absences_AgentRight $agentRight)
167
    {
168
        $collectionName = '';
169
        $collection = $agent->getCollection();
170
        if ($collection->getRow()) {
171
            $collectionName = $collection->name;
172
        }
173
        
174
        $organizationName = '';
175
        $organization = $agent->getOrganization();
176
        if ($organization->getRow()) {
177
            $organizationName = $organization->name;
178
        }
179
        
180
        $right = $agentRight->getRight();
181
        
182
        $row = array(
183
            $collectionName,
184
            $organizationName,
185
            $right->description,
186
            $right->getType()->name,
187
            $agentRight->getQuantity(),
188
            $agentRight->getConfirmedQuantity(),
189
            $agentRight->getWaitingQuantity(),
190
            $agentRight->getBalance(),
191
            $this->date($right->date_begin),
192
            $this->date($right->date_end)
193
        );
194
        
195
        foreach ($this->dirFields as $field) {
196
            $row[] = $this->getAgentDirValue($agent, $field);
197
        }
198
        
199
        return $row;
200
    }
201
    
202
    
203
204
    /**
205
     * output an array on raw values
206
     * @param array $arr
207
     *
208
     */
209
    protected function outputArr(Array $arr)
210
    {
211
        $line = array_map(array($this, 'encodeCsv'), $arr);
212
        echo implode($this->separator, $line);
213
    }
214
    
215
    
216
    public function download()
217
    {
218
        $this->setHeaders(absences_translate('rights'));
219
        
220
        $this->outputArr($this->getHeader());
221
        
222
        foreach ($this->selectAgents() as $agent) {
223
            foreach ($this->selectAgentRights($agent) as $agentRight) {
224
                
225
                $this->outputArr($this->getRow($agent, $agentRight));
226
            }
227
        }
228
        
229
        die();
230
    }
231
}