absences_ExportEntry::getElementsHeader()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 22
nc 4
nop 0
dl 0
loc 31
ccs 0
cts 24
cp 0
crap 12
rs 8.8571
c 0
b 0
f 0
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__).'/entry.class.php';
26
require_once dirname(__FILE__).'/agent.class.php';
27
require_once dirname(__FILE__).'/organization.class.php';
28
29
class absences_ExportEntry
30
{
31
    /**
32
     * @var BAB_DateTime
33
     */
34
    protected $dateb;
35
36
    /**
37
     * @var BAB_DateTime
38
     */
39
    protected $datee;
40
41
    
42
    /**
43
     * @var bool
44
     */
45
    private $splittype;
46
    
47
    /**
48
     * @var absences_EntryIterator
49
     */
50
    private $iterator;
51
52
    /**
53
     * @var string
54
     */
55
    private $separ;
56
57
    /**
58
     * @var string
59
     */
60
    private $sepdec;
61
62
    /**
63
     * @var array
64
     */
65
    private $users_with_requests = array();
66
67
    /**
68
     * @var bool
69
     */
70
    private $users_without_requests;
71
72
    /**
73
     * @var array
74
     */
75
    private $dirfields;
76
77
78
    private $types = array();
79
80
    /**
81
     * @var int
82
     */
83
    private $organization = '';
84
85 1
    public function __construct(BAB_DateTime $dateb, BAB_DateTime $datee, $idstatus, $wsepar, $separ, $sepdec, $users_without_requests, $dirfields, $organization, $splittype)
86
    {
87 1
        $this->dateb = $dateb;
88 1
        $this->datee = $datee;
89 1
        $this->organization = $organization;
90
91 1
        $status = array();
92 1
        if (in_array(0,$idstatus)) {
93 1
            $status[] = '';
94 1
        }
95
        
96 1
        if (in_array(1,$idstatus)) {
97 1
            $status[] = 'Y';
98 1
        }
99
        
100 1
        if (in_array(2,$idstatus)) {
101 1
            $status[] = 'N';
102 1
        }
103
        
104 1
        $this->splittype = (bool) $splittype;
105
106 1
        if ($this->splittype) {
107
            $this->iterator = $this->getRequestTypeIterator($status);
108
        } else {
109 1
            $this->iterator = $this->getRequestIterator($status);
110
        }
111
112
        switch($wsepar)
113
        {
114 1
            case "1":
115 1
                $this->separ = ",";
116 1
                break;
117
            case "2":
118
                $this->separ = "\t";
119
                break;
120
            case "3":
121
                $this->separ = ";";
122
                break;
123
            default:
124
                $this->separ = $separ;
125
                if( empty($separ)) {
126
                    $this->separ = ",";
127
                }
128
                break;
129
        }
130
131 1
        $this->sepdec = $sepdec;
132
133 1
        $this->users_without_requests = (bool) $users_without_requests;
134
135 1
        $this->dirfields = $dirfields;
136 1
    }
137
    
138
    
139 1 View Code Duplication
    private function getRequestIterator($status)
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...
140
    {
141 1
        $I = new absences_EntryIterator();
142 1
        $I->from = $this->dateb->getIsoDateTime();
143 1
        $I->to = $this->datee->getIsoDateTime();
144
        
145 1
        if($this->organization){
146
            $I->organization = (int) $this->organization ;
147
        }
148
        
149
        
150 1
        $I->status = $status;
151
        
152 1
        return $I;
153
    }
154
    
155
    
156
    
157 1 View Code Duplication
    private function getRequestTypeIterator($status)
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...
158
    {
159
        $I = new absences_ElementEntryIterator();
160
        $I->from = $this->dateb->getIsoDateTime();
161
        $I->to = $this->datee->getIsoDateTime();
162
        
163
        if($this->organization ){
164
            $I->organization = (int) $this->organization ;
165
        }
166
        
167 1
        $I->status = $status;
168
        
169
        return $I;
170
    }
171
172
173 1
    private function arr_csv(&$value)
174
    {
175 1
        $value = str_replace("\n"," ",$value);
176 1
        $value = str_replace('"',"'",$value);
177 1
        $value = '"'.$value.'"';
178 1
    }
179
180
181 1
    private function numberFormat($quantity)
182
    {
183 1
        return number_format($quantity, 1, $this->sepdec, '');
184
    }
185
    
186
    
187
    
188
    
189
190
191 1
    protected function getEntriesHeader()
192
    {
193 1
        global $babDB;
194
        
195 1
        $line = array();
196
197 1
        if ($this->dirfields) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->dirfields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
198
            $ov_fields = bab_getDirEntry(BAB_REGISTERED_GROUP, BAB_DIR_ENTRY_ID_GROUP);
199
        }
200
201 1
        $line[] = absences_translate("lastname");
202 1
        $line[] = absences_translate("firstname");
203 1
        $line[] = absences_translate("Created on");
204 1
        $line[] = absences_translate("Modified on");
205
        
206 1
        $dirfields = array_keys($this->dirfields);
207 1
        foreach ($dirfields as $name) {
208
            $line[] = $ov_fields[$name]['name'];
0 ignored issues
show
Bug introduced by
The variable $ov_fields does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
209 1
        }
210 1
        $line[] = absences_translate("Begin date");
211 1
        $line[] = absences_translate("Begin hour");
212 1
        $line[] = absences_translate("End date");
213 1
        $line[] = absences_translate("End hour");
214 1
        $line[] = absences_translate("Status");
215 1
        $line[] = absences_translate("Total days");
216 1
        $line[] = absences_translate("Total hours");
217
218 1
        $res = $babDB->db_query("SELECT 
219
                t.id,
220
                t.name, 
221
                COUNT(rd.id) with_day,
222
                COUNT(rh.id) with_hour 
223
            FROM absences_types t 
224
                LEFT JOIN absences_rights rd ON rd.id_type=t.id AND rd.quantity_unit='D' 
225
                LEFT JOIN absences_rights rh ON rh.id_type=t.id AND rh.quantity_unit='H' 
226
            GROUP BY t.id 
227
            ORDER BY t.name 
228 1
        ");
229 1
        while ($arr = $babDB->db_fetch_assoc($res))
230
        {
231 1
            if ((bool) $arr['with_day']) {
232 1
                $line[] = $arr['name'].' '.absences_translate('days');
233 1
            }
234
            
235 1
            if ((bool) $arr['with_hour']) {
236
                $line[] = $arr['name'].' '.absences_translate('hours');
237
            }
238
            
239 1
            $this->types[] = array(
240 1
                'id'        => $arr['id'],
241 1
                'with_day'  => (bool) $arr['with_day'],
242 1
                'with_hour' => (bool) $arr['with_hour']
243 1
            );
244 1
        }
245 1
        array_walk($line, array($this, 'arr_csv'));
246
247 1
        return $line;
248
    }
249
    
250
    
251
    protected function getElementsHeader()
252
    {
253
        $line = array();
254
        
255
        if ($this->dirfields) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->dirfields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
256
            $ov_fields = bab_getDirEntry(BAB_REGISTERED_GROUP, BAB_DIR_ENTRY_ID_GROUP);
257
        }
258
        
259
        $line[] = absences_translate("lastname");
260
        $line[] = absences_translate("firstname");
261
        $line[] = absences_translate("Created on");
262
        $line[] = absences_translate("Modified on");
263
        
264
        $dirfields = array_keys($this->dirfields);
265
        foreach ($dirfields as $name) {
266
            $line[] = $ov_fields[$name]['name'];
0 ignored issues
show
Bug introduced by
The variable $ov_fields does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
267
        }
268
        $line[] = absences_translate("Type");
269
        $line[] = absences_translate("Right");
270
        $line[] = absences_translate("Begin date");
271
        $line[] = absences_translate("Begin hour");
272
        $line[] = absences_translate("End date");
273
        $line[] = absences_translate("End hour");
274
        $line[] = absences_translate("Status");
275
        $line[] = absences_translate("Days");
276
        $line[] = absences_translate("Hours");
277
        
278
        array_walk($line, array($this, 'arr_csv'));
279
        
280
        return $line;
281
    }
282
    
283
    
284 1
    public function getHeader()
285
    {
286 1
        if ($this->splittype) {
287
            return $this->getElementsHeader();
288
        }
289
        
290 1
        return $this->getEntriesHeader();
291
    }
292
293
294 1 View Code Duplication
    private function getAgentDirValue(absences_Agent $agent, $fieldname)
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...
295
    {
296 1
        $direntry = $agent->getDirEntry();
297
298 1
        if (isset($direntry[$fieldname]))
299 1
        {
300 1
            return $direntry[$fieldname]['value'];
301
        }
302
303
        return '';
304
    }
305
306
307
    /**
308
     * Quantity for one type
309
     * @return float
310
     */
311 1
    private function getTypeDays(absences_Entry $entry, $id_type)
312
    {
313 1
        return $entry->getPlannedDaysBetween($this->iterator->from, $this->iterator->to, $id_type);
314
    }
315
316
317
    /**
318
     * Quantity for one type
319
     * @return float
320
     */
321
    private function getTypeHours(absences_Entry $entry, $id_type)
322
    {
323
        return $entry->getPlannedHoursBetween($this->iterator->from, $this->iterator->to, $id_type);
324
    }
325
326
    /**
327
     * Entry begin timestamp
328
     *
329
     * if overlap with the end date :
330
     * First try with planned periods (in database for absences >= 2.46)
331
     * second with the current working periods (if not removed by configuration)
332
     * last with the iterator boundaries
333
     *
334
     * @return int
335
     */
336 1
    private function getBeginTs(absences_Entry $entry)
337
    {
338 1
        if ($this->iterator->from > $entry->date_begin) {
339
340
            $day = substr($this->iterator->from, 0, 10);
341
            $planned = $entry->getDayPlannedPeriods($day);
342
343 View Code Duplication
            if (0 === count($planned)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
344
                $workingPeriods = $entry->getDayWorkingPeriods($day);
345
346
                if (0 === count($workingPeriods)) {
347
                    return bab_mktime($this->iterator->from);
348
                }
349
350
                $firstPeriod = reset($workingPeriods);
351
                return $firstPeriod->ts_begin;
352
            }
353
354
            $firstperiod = reset($planned);
355
356
            return bab_mktime($firstperiod->date_begin);
357
        }
358
359 1
        return bab_mktime($entry->date_begin);
360
    }
361
362
    /**
363
     * Entry end timestamp
364
     *
365
     * if overlap with the end date :
366
     * First try with planned periods (in database for absences >= 2.46)
367
     * second with the current working periods (if not removed by configuration)
368
     * last with the iterator boundaries
369
     *
370
     * @return int
371
     */
372 1
    private function getEndTs(absences_Entry $entry)
373
    {
374 1
        if ($this->iterator->to < $entry->date_end) {
375
376
            $end = $this->datee->cloneDate();
377
            $end->less(1, BAB_DATETIME_DAY); // un jour a ete ajoute pour inclure le dernier jour dans l'iterateur
378
                                             // on enleve 1 jour pour retrouver la date saisie
379
            $day = $end->getIsoDate();
380
            $planned = $entry->getDayPlannedPeriods($day);
381
382 View Code Duplication
            if (0 === count($planned)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
383
                $workingPeriods = $entry->getDayWorkingPeriods($day);
384
385
                if (0 === count($workingPeriods)) {
386
                    return bab_mktime($this->iterator->to);
387
                }
388
389
                $lastPeriod = end($workingPeriods);
390
                return $lastPeriod->ts_end;
391
            }
392
393
            $lastperiod = end($planned);
394
395
            return bab_mktime($lastperiod->date_end);
396
        }
397
398 1
        return bab_mktime($entry->date_end);
399
    }
400
    
401
    
402
    
403
    protected function getElementRow(absences_EntryElem $elem)
404
    {
405
        $entry = $elem->getEntry();
406
        $right = $elem->getRight();
407
        $type = $right->getType();
408
        $agent = $entry->getAgent();
409
        
410
        $line = array();
411
        
412
413
        $line[] = $this->getAgentDirValue($agent, 'sn');
414
        $line[] = $this->getAgentDirValue($agent, 'givenname');
415
        $line[] = bab_shortDate(bab_mktime($entry->createdOn), false);
416
        $line[] = bab_shortDate(bab_mktime($entry->modifiedOn()), false);
417
        foreach($this->dirfields as $name => $dummy)
418
        {
419
            $line[] = $this->getAgentDirValue($agent, $name);
420
        }
421
        $line[] = $type->name;
422
        $line[] = $right->description;
423
        
424
        $begin = bab_mktime($elem->date_begin);
425
        $end = bab_mktime($elem->date_end);
426
        
427
        $line[] = bab_shortDate($begin, false);
428
        $line[] = date('H:i', $begin);
429
        $line[] = bab_shortDate($end, false);
430
        $line[] = date('H:i', $end);
431
        $line[] = $entry->getShortStatusStr();
432
        
433
        $line[] = $this->numberFormat($elem->getDays());
434
        $line[] = $this->numberFormat($elem->getHours());
435
        
436
        array_walk($line, array($this, 'arr_csv'));
437
438
        return $line;
439
    }
440
441
442
    /**
443
     *
444
     * @return array
445
     */
446 1
    public function getEntryRow(absences_Entry $entry)
447
    {
448 1
        $agent = $entry->getAgent();
449
450 1
        if (isset($agent)) {
451 1
            $this->users_with_requests[] = $agent->getIdUser();
452 1
        }
453
454 1
        $line = array();
455 1
        $line[] = $this->getAgentDirValue($agent, 'sn');
0 ignored issues
show
Bug introduced by
It seems like $agent can be null; however, getAgentDirValue() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
456 1
        $line[] = $this->getAgentDirValue($agent, 'givenname');
0 ignored issues
show
Bug introduced by
It seems like $agent can be null; however, getAgentDirValue() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
457
        
458
        
459 1
        if (empty($line[0]) && empty($line[1])) {
460
            // the user has been deleted?
461
            return null;
462
        }
463
        
464
        
465 1
        $line[] = bab_shortDate(bab_mktime($entry->createdOn), false);
466 1
        $line[] = bab_shortDate(bab_mktime($entry->modifiedOn()), false);
467
468 1
        foreach($this->dirfields as $name => $dummy)
469
        {
470
            $line[] = $this->getAgentDirValue($agent, $name);
0 ignored issues
show
Bug introduced by
It seems like $agent can be null; however, getAgentDirValue() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
471 1
        }
472
473 1
        $begin = $this->getBeginTs($entry);
474 1
        $end = $this->getEndTs($entry);
475
476 1
        $line[] = bab_shortDate($begin, false);
477 1
		$line[] = date('H:i', $begin);
478 1
		$line[] = bab_shortDate($end, false);
479 1
		$line[] = date('H:i', $end);
480 1
        $line[] = $entry->getShortStatusStr();
481
482 1
        $pos = count($line);
483 1
        $tdays = 0.0;
484 1
        $thours = 0.0;
485 1
        foreach ($this->types as $_arr) {
486
            
487 1
            $id_type = $_arr['id'];
488
            
489 1
            if ($_arr['with_day']) {
490 1
                $days = $this->getTypeDays($entry, $id_type);
491 1
                $line[] = $this->numberFormat($days);
492 1
                $tdays += $days;
493 1
            }
494
            
495 1
            if ($_arr['with_hour']) {
496
                $hours = $this->getTypeHours($entry, $id_type);
497
                $line[] = $this->numberFormat($hours);
498
                $thours += $hours;
499
            }
500
            
501 1
        }
502
        
503
        
504 1
        if (0 === (int) round(100 * $tdays) && 0 === (int) round(100 * $thours)) {
505
            return null;
506
        }
507
        
508
509 1
        array_splice($line, $pos++, 0, $this->numberFormat($tdays));
510 1
        array_splice($line, $pos++, 0, $this->numberFormat($thours));
511
512 1
        array_walk($line, array($this, 'arr_csv'));
513
514 1
        return $line;
515
    }
516
517
    
518
    
519
    public function getRow($object)
520
    {
521
        if ($this->splittype) {
522
            return $this->getElementRow($object);
523
        }
524
        
525
        return $this->getEntryRow($object);
526
    }
527
    
528
    
529
530
    /**
531
     * @return absences_Agent[]
532
     */
533
    private function getUsersWithoutRequests()
534
    {
535
        $I = new absences_AgentIterator();
536
        if (count($this->users_with_requests) > 0)
537
        {
538
            $I->exclude_users = $this->users_with_requests;
539
        }
540
        if($this->organization){
541
        	$Orga = absences_Organization::getById($this->organization);
542
        	$I->setOrganization($Orga);
543
        }
544
        return $I;
545
    }
546
547
548
    /**
549
     * @return array
550
     */
551 1
    public function getAgentRow(absences_Agent $agent)
552
    {
553
554 1
        $line = array();
555 1
        $line[] = $this->getAgentDirValue($agent, 'sn');
556 1
        $line[] = $this->getAgentDirValue($agent, 'givenname');
557 1
        $line[] = '';
558 1
        $line[] = '';
559 1
        foreach($this->dirfields as $name => $dummy)
560
        {
561
            $line[] = $this->getAgentDirValue($agent, $name);
562 1
        }
563
564 1
        $line[] = '';
565 1
        $line[] = '';
566 1
        $line[] = '';
567 1
        $line[] = '';
568 1
        $line[] = '';
569 1
        $line[] = 0;
570 1
        $line[] = 0;
571 1
        foreach ($this->types as $_arr) {
572 1
            if ($_arr['with_day']) {
573 1
                $line[] = 0;
574 1
            }
575
            
576 1
            if ($_arr['with_hour']) {
577
                $line[] = 0;
578
            }
579 1
        }
580
581 1
        array_walk($line, array($this, 'arr_csv'));
582
583 1
        return $line;
584
    }
585
586
587
588
    public function output()
589
    {
590
591
        header("Content-Disposition: attachment; filename=\"".absences_translate("Vacation").".csv\""."\n");
592
        header("Content-Type: text/csv"."\n");
593
594
        $header = $this->getHeader();
595
        echo implode($this->separ, $header)."\n";
596
597
        foreach ($this->iterator as $object) {
598
            $line = $this->getRow($object);
599
            
600
            
601
            if (!isset($line)) {
602
                // the entry contain no total quantity or no directory entry
603
                continue;
604
            }
605
606
            
607
608
            echo implode($this->separ, $line)."\n";
609
        }
610
611
        if ($this->users_without_requests) {
612
            foreach ($this->getUsersWithoutRequests() as $agent) {
613
                $line = $this->getAgentRow($agent);
614
                echo implode($this->separ, $line)."\n";
615
            }
616
        }
617
618
        exit;
619
    }
620
}
621
622