Completed
Push — master ( a182e4...7f1093 )
by Paul
04:26
created

absences_RightExportCsv::__construct()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 26
rs 8.439
cc 6
eloc 13
nc 32
nop 6
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 $outputCharset = null;
58
    
59
    /**
60
     * @param int    $org
61
     * @param int    $year
62
     * @param array  $dirFields         List of additional directory fields
63
     */
64
    public function __construct($org, $year, Array $dirFields, $separator = null, $sepdec = null, $outputCharset = null)
65
    {
66
        if (!empty($org)) {
67
            $this->org = $org;
68
        }
69
        
70
        if (!empty($year)) {
71
            $this->year = $year;
72
        }
73
        
74
        $this->dirFields = $dirFields;
75
        
76
        if (isset($separator)) {
77
            $this->separator = $separator;
78
        }
79
        
80
        if (isset($sepdec)) {
81
            $this->sepdec = $sepdec;
82
        }
83
        
84
        $this->outputCharset = bab_Charset::getIso();
85
        
86
        if (isset($outputCharset)) {
87
            $this->outputCharset = $outputCharset;
88
        }
89
    }
90
    
91
    
92
    /**
93
     * @return absences_AgentIterator
94
     */
95
    protected function selectAgents()
96
    {
97
        require_once dirname(__FILE__).'/organization.class.php';
98
        
99
        $res = new absences_AgentIterator();
100
        
101
        if (isset($this->org)) {
102
            $organization = absences_Organization::getById($this->org);
103
            $res->setOrganization($organization);
104
        }
105
        
106
        return $res;
107
    }
108
    
109
    
110
    
111
    
112
    /**
113
     * 
114
     * @param absences_Agent $agent
115
     * @return absences_AgentRightManagerIterator
116
     */
117
    protected function selectAgentRights(absences_Agent $agent)
118
    {
119
        require_once dirname(__FILE__).'/agent_right.class.php';
120
		$I = new absences_AgentRightManagerIterator;
121
		$I->setAgent($agent);
122
		
123
		if (isset($this->year)) {
124
		    $I->year = $this->year;
125
		}
126
		
127
		return $I;
128
    }
129
    
130
    
131
    /**
132
     * @return array
133
     */
134
    protected function getHeader()
135
    {
136
        $row = array(
137
            absences_translate('Lastname'),
138
            absences_translate('Firstname'),
139
            absences_translate('Right collection'),
140
            absences_translate('Organization'),
141
            absences_translate('Right description'),
142
            absences_translate('Type'),
143
            absences_translate('Initial quantity'),
144
    		absences_translate('Consumed'),
145
    		absences_translate('Waiting approval'),
146
    		absences_translate('Balance'),
147
    		absences_translate('Begin date'),
148
    		absences_translate('End date'),
149
    		absences_translate('Accessible')
150
        );
151
        
152
        $dir = bab_getDirEntry(BAB_REGISTERED_GROUP, BAB_DIR_ENTRY_ID_GROUP);
153
154
        foreach ($this->dirFields as $fieldname) {
155
            $row[] = $dir[$fieldname]['name'];
156
        }
157
        
158
        return $row;
159
    }
160
    
161
    /**
162
     * @return array
163
     */
164
    protected function getRow(absences_Agent $agent, absences_AgentRight $agentRight)
165
    {
166
        $collectionName = '';
167
        $collection = $agent->getCollection();
168
        if ($collection->getRow()) {
169
            $collectionName = $collection->name;
170
        }
171
        
172
        $organizationName = '';
173
        if ($organization = $agent->getOrganization()) {
174
            $organizationName = $organization->name;
175
        }
176
        
177
        $right = $agentRight->getRight();
178
        
179
        $name = bab_getUserName($agent->getIdUser(), false);
180
        
181
        
182
        if (!$name['lastname'] && !$name['firstname']) {
183
            // this is a deleted user
184
            return null;
185
        }
186
        
187
        $row = array(
188
            $name['lastname'],
189
            $name['firstname'],
190
            $collectionName,
191
            $organizationName,
192
            $right->description,
193
            $right->getType()->name,
194
            $agentRight->getQuantity(),
195
            $agentRight->getConfirmedQuantity(),
196
            $agentRight->getWaitingQuantity(),
197
            $agentRight->getBalance(),
198
            $this->date($right->date_begin),
199
            $this->date($right->date_end),
200
            ($right->isAccessibleByValidityPeriod() && $right->isAccessibleIfFixed()) ? '1' : '0'
201
        );
202
        
203
        foreach ($this->dirFields as $field) {
204
            $row[] = $this->getAgentDirValue($agent, $field);
205
        }
206
        
207
        return $row;
208
    }
209
    
210
    
211
212
213
    
214
    
215
    public function download()
216
    {
217
        $this->setHeaders(absences_translate('rights'));
218
        
219
        $this->outputArr($this->getHeader());
220
        
221
        foreach ($this->selectAgents() as $agent) {
222
            
223
            // allocated duration per agent
224
            bab_setTimeLimit(10);
225
            
226
            $res = $this->selectAgentRights($agent);
227
            
228
            foreach ($res as $agentRight) {
229
                $arr = $this->getRow($agent, $agentRight);
230
                if (!isset($arr)) {
231
                    continue;
232
                }
233
                $this->outputArr($arr);
234
            }
235
        }
236
        
237
        die();
238
    }
239
}
240
241
242
243
244
245
246
247
class absences_RightExportTemplate
248
{
249
    public $separatortxt;
250
    public $other;
251
    public $comma;
252
    public $tab;
253
    public $semicolon;
254
    public $export;
255
    public $sepdectxt;
256
    public $t_year;
257
    public $t_organization;
258
    public $additional_fields;
259
    
260
    protected $resYears;
261
    protected $resOrganization;
262
    protected $dirfields;
263
    
264
    public $year;
265
    public $fieldname;
266
    public $organization;
267
    public $id_organization;
268
    
269
    public function __construct()
270
    {
271
        global $babDB;
272
        
273
        $this->separatortxt = absences_translate("Separator");
274
        $this->other = absences_translate("Other");
275
        $this->comma = absences_translate("Comma");
276
        $this->tab = absences_translate("Tab");
277
        $this->semicolon = absences_translate("Semicolon");
278
        $this->export = absences_translate("Export");
279
        $this->sepdectxt = absences_translate("Decimal separator");
280
        $this->t_year = absences_translate('Year filter');
281
        $this->t_organization = absences_translate('Organization');
282
        $this->additional_fields = absences_translate("Additional fields to export:");
283
284
        $this->resYears = $babDB->db_query("SELECT YEAR(date_begin) year FROM absences_rights WHERE YEAR(date_begin)<>'0' GROUP BY year");
285
        
286
        $this->resOrganization = $babDB->db_query("SELECT * FROM `absences_organization` ORDER BY name ASC");
287
288
        $this->dirfields = bab_getDirEntry(BAB_REGISTERED_GROUP, BAB_DIR_ENTRY_ID_GROUP);
289
        
290
        unset($this->dirfields['sn']);
291
        unset($this->dirfields['givenname']);
292
        unset($this->dirfields['jpegphoto']);
293
    }
294
    
295
    
296
297
    /**
298
     * template method to list available years
299
     */
300 View Code Duplication
    public function getnextyear()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
301
    {
302
        global $babDB;
303
    
304
        if ($arr = $babDB->db_fetch_assoc($this->resYears))
305
        {
306
            $this->year = bab_toHtml($arr['year']);
307
            return true;
308
        }
309
    
310
        return false;
311
    }
312
    
313
    
314 View Code Duplication
    public function getnextfield()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
315
    {
316
        if (list($name,$arr) = each($this->dirfields))
317
        {
318
            $this->fieldname = bab_toHtml($name);
319
            $this->fieldlabel = bab_toHtml($arr['name']);
0 ignored issues
show
Bug introduced by
The property fieldlabel does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
320
            return true;
321
        }
322
        return false;
323
    }
324
    
325
    
326 View Code Duplication
    public function getnextorganization()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
327
    {
328
        global $babDB;
329
        
330
        if ($arr = $babDB->db_fetch_assoc($this->resOrganization))
331
        {
332
            $this->organization = bab_toHtml($arr['name']);
333
            $this->id_organization = bab_toHtml($arr['id']);
334
            return true;
335
        }
336
        
337
        return false;
338
    }
339
}
340
341
342
343
344
345
346
347
348
349
350
351
352
function absences_exportForm()
353
{
354
    if (!empty($_POST)) {
355
        
356
        $separator = ',';
357
        
358
        switch (bab_rp('wsepar')) {
359
            case '0':
360
                $separator = bab_rp('separ');
361
                break;
362
                
363
            case '1':
364
                $separator = ',';
365
                break;
366
                
367
            case '2':
368
                $separator = "\t";
369
                break;
370
            
371
            case '3':
372
                $separator = ';';
373
                break;
374
        }
375
        
376
        $dirfields = array_keys((array) bab_pp('dirfields'));
377
378
        $export = new absences_RightExportCsv(bab_rp('organization'), bab_rp('year'), $dirfields, $separator, bab_rp('sepdec'));
379
        $export->download();
380
    }
381
    
382
    
383
    $addon = bab_getAddonInfosInstance('absences');
384
    
385
    $template = new absences_RightExportTemplate();
386
    bab_getBody()->babecho($addon->printTemplate($template, 'rightexport.html'));
387
}