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

exportAvailableBalancesCls::addDirValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
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__).'/agent_right.class.php';
26
require_once dirname(__FILE__).'/csv.class.php';
27
28
class exportAvailableBalancesCls extends absences_Csv
29
{
30
	private $resYears;
31
32
	/**
33
	 *
34
	 * @var array
35
	 */
36
	private $rights;
37
38
39
	/**
40
	 * @var array
41
	 */
42
	private $recoveries;
43
	
44
	
45
	/**
46
	 * @var string YYYY-MM-DD
47
	 */
48
	private $date = null;
49
	
50
	/**
51
	 * @var array
52
	 */
53
	private $dirfields;
54
55
56
	public function getHtml()
57
	{
58
		global $babDB;
59
60
		$this->separatortxt = absences_translate("Separator");
0 ignored issues
show
Bug introduced by
The property separatortxt 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...
61
		$this->other = absences_translate("Other");
0 ignored issues
show
Bug introduced by
The property other 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...
62
		$this->comma = absences_translate("Comma");
0 ignored issues
show
Bug introduced by
The property comma 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...
63
		$this->tab = absences_translate("Tab");
0 ignored issues
show
Bug introduced by
The property tab 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...
64
		$this->semicolon = absences_translate("Semicolon");
0 ignored issues
show
Bug introduced by
The property semicolon 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...
65
		$this->export = absences_translate("Export");
0 ignored issues
show
Bug introduced by
The property export 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...
66
		$this->sepdectxt = absences_translate("Decimal separator");
0 ignored issues
show
Bug introduced by
The property sepdectxt 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...
67
		$this->t_yes = absences_translate("Yes");
0 ignored issues
show
Bug introduced by
The property t_yes 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...
68
		$this->t_no = absences_translate("No");
0 ignored issues
show
Bug introduced by
The property t_no 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...
69
		$this->t_date = absences_translate('Export day date');
0 ignored issues
show
Bug introduced by
The property t_date 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...
70
		$this->t_year = absences_translate('Year filter');
0 ignored issues
show
Bug introduced by
The property t_year 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...
71
		$this->t_organization = absences_translate('Organization');
0 ignored issues
show
Bug introduced by
The property t_organization 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...
72
		$this->additional_fields = absences_translate("Additional fields to export:");
0 ignored issues
show
Bug introduced by
The property additional_fields 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...
73
		
74
		$W = bab_Widgets();
75
		$this->datePicker = $W->DatePicker()->setName('date')->display($W->HtmlCanvas()); 
0 ignored issues
show
Bug introduced by
The property datePicker 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...
76
77
		$this->resYears = $babDB->db_query("SELECT YEAR(date_begin) year FROM absences_rights WHERE YEAR(date_begin)<>'0' GROUP BY year");
78
79
		$this->resOrganization = $babDB->db_query("SELECT * FROM `absences_organization` ORDER BY name ASC");
0 ignored issues
show
Bug introduced by
The property resOrganization 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...
80
81
82
		$this->dirfields = bab_getDirEntry(BAB_REGISTERED_GROUP, BAB_DIR_ENTRY_ID_GROUP);
83
84
		unset($this->dirfields['sn']);
85
		unset($this->dirfields['givenname']);
86
		unset($this->dirfields['jpegphoto']);
87
88
		return bab_printTemplate($this, absences_addon()->getRelativePath()."vacadm.html", "abexport");
0 ignored issues
show
Deprecated Code introduced by
The method bab_addonInfos::getRelativePath() has been deprecated with message: Do not use relative path in addons Addons are subject to move out of the core folder in futures version for bab_printTemplate, replace with $addon->printTemplate() for babBody->addStyleSheet use $addon->getStylePath() instead of relative path the addStyleSheet method support full path starting with vendor/ since the 8.1.98 version

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
89
	}
90
91
	/**
92
	 * template method to list available years
93
	 */
94
	public function getnextyear()
95
	{
96
		global $babDB;
97
98
		if ($arr = $babDB->db_fetch_assoc($this->resYears))
99
		{
100
			$this->year = bab_toHtml($arr['year']);
0 ignored issues
show
Bug introduced by
The property year 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...
101
			return true;
102
		}
103
104
		return false;
105
	}
106
107
108 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...
109
	{
110
		if (list($name,$arr) = each($this->dirfields))
111
		{
112
			$this->fieldname = bab_toHtml($name);
0 ignored issues
show
Bug introduced by
The property fieldname 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...
113
			$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...
114
			return true;
115
		}
116
		return false;
117
	}
118
119
	/**
120
	 * template method to list available organization
121
	 */
122
	public function getnextorganization()
123
	{
124
		global $babDB;
125
126
		if ($arr = $babDB->db_fetch_assoc($this->resOrganization))
127
		{
128
			$this->organization = bab_toHtml($arr['name']);
0 ignored issues
show
Bug introduced by
The property organization does not seem to exist. Did you mean t_organization?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
129
			$this->id_organization = bab_toHtml($arr['id']);
0 ignored issues
show
Bug introduced by
The property id_organization 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...
130
			return true;
131
		}
132
133
		return false;
134
	}
135
136
137
138
	private function query($year, $groupby = '', $organization = '', $user = '')
139
	{
140
		global $babDB;
141
142
		if (!empty($year)) {
143
			$year = 'AND YEAR(r.date_begin)='.$babDB->quote($year);
144
		}
145
146
		if ($organization) {
147
			$organization = 'AND p.id_organization='.$babDB->quote($organization);
148
		}
149
		
150
		if ($user) {
151
		    $user = ' AND p.id_user='.$babDB->quote($user);
152
		}
153
154
155
		$query = "
156
			SELECT
157
				ur.* 
158
			FROM
159
				absences_users_rights ur, 
160
				absences_rights r,
161
				absences_personnel p,
162
				bab_users u
163
			WHERE
164
				ur.id_user=p.id_user
165
				AND p.id_user=u.id
166
				AND r.id = ur.id_right 
167
				AND r.active = 'Y'
168
				$year
169
				$organization
170
				$user
171
			$groupby
172
			ORDER BY
173
				u.lastname,
174
				u.firstname,
175
				r.description
176
		";
177
178
		return $babDB->db_query($query);
179
	}
180
181
	
182
183
	
184
	
185
	
186
	/**
187
	 * New row for the user
188
	 * 
189
	 * @param absences_AgentRight $agentRight
190
	 * @param array $remain
191
	 * 
192
	 * @return Array
193
	 */
194
	private function getNewCurrentRow(absences_AgentRight $agentRight, Array $remain)
195
	{
196
	    $arr = bab_getUserName($agentRight->id_user, false);
197
	    
198
	    $currentRow = array(
199
	            $this->csvEncode($arr['lastname']),
200
	            $this->csvEncode($arr['firstname']),
201
	            $remain['D'], // total days
202
	            $remain['H']  // total hours
203
	    );
204
	    
205
	    foreach($this->rights as $initcol) {
206
	        $currentRow[$initcol] = 0.0;
207
	    }
208
	     
209
	    foreach($this->recoveries as $quantity_unit => $initcol) {
210
	        if (isset($initcol)) {
211
	            $currentRow[$initcol] = 0.0;
212
	        }
213
	    }
214
	    
215
	    return $currentRow;
216
	}
217
218
219
	/**
220
	 * Process one row
221
	 * if user row exists, add remain to the correct right column
222
	 * if the user row does not exists, create it
223
	 *
224
	 * return the row only if it is the first user row
225
	 * else return null
226
	 *
227
	 * @return array | null
228
	 */
229
	private function processRow($arr)
230
	{
231
		static $currentUser = null;
232
		static $currentRow = null;
233
234
		$return = null;
235
236
		if (null === $arr) {
237
		    
238
		    if (!isset($currentRow)) {
239
		        return array();
240
		    }
241
		    
242
			$encoded = $this->encodeFloats($currentRow);
243
			$currentRow = null;
244
			$currentUser = null;
245
			return $encoded;
246
		}
247
		
248
		$agentRight = new absences_AgentRight();
249
		$agentRight->setRow($arr);
250
		
251
		$right = $agentRight->getRight();
252
253
		$remain = array('D' => 0.0, 'H' => 0.0);
254
255
		$remain_line = $remain[$right->quantity_unit] = $agentRight->getAvailableQuantity($this->date);
256
257
258
		if ($currentUser !== $agentRight->id_user) {
259
			$currentUser = $agentRight->id_user;
260
261
			if (null !== $currentRow && !$this->isRowEmpty($currentRow)) {
0 ignored issues
show
Documentation introduced by
$currentRow is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
262
				$return = $this->encodeFloats($currentRow);
0 ignored issues
show
Documentation introduced by
$currentRow is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
263
			}
264
265
			$currentRow = $this->getNewCurrentRow($agentRight, $remain);
266
			
267
			$this->addDirValues($arr['id_user'], $currentRow);
268
269
		} else {
270
271
			$currentRow[2] += $remain['D']; // total days
272
			$currentRow[3] += $remain['H']; // total hours
273
		}
274
275
276
        $this->setRightColumn($right, $currentRow, $remain, $remain_line);
277
		
278
279
		return $return;
280
	}
281
	
282
	
283
	/**
284
	 * Complete current row with right value
285
	 * @param absences_Right $right
286
	 * @param array &$currentRow
287
	 */
288
	private function setRightColumn(absences_Right $right, Array &$currentRow, Array $remain, $remain_line)
289
	{
290
	    
291
	    if (absences_Right::RECOVERY === $right->getKind()) {
292
	    
293
	        // recovery right
294
	        $col = $this->recoveries[$right->quantity_unit];
295
	        if ($col > 3) {
296
	            $currentRow[$col] += $remain[$right->quantity_unit];
297
	        }
298
	    
299
	    
300
	    } else {
301
	    
302
	    
303
	        //regular right, add to specific column
304
	        $col = $this->rights[$right->id];
305
	    
306
	        if ($col > 3) {
307
	            $currentRow[$col] = $remain_line;
308
	        }
309
	    }
310
	}
311
	
312
	/**
313
	 * Complete current row with directory entry values
314
	 * @param int $id_user
315
	 * @param array &$currentRow
316
	 */
317
	private function addDirValues($id_user, Array &$currentRow)
318
	{
319
	    $agent = absences_Agent::getFromIdUser($id_user);
320
	    
321
	    foreach ($this->dirfields as $name => $dummy) {
322
	        $currentRow[] = $this->csvEncode($this->getAgentDirValue($agent, $name));
323
	    }
324
	}
325
	
326
	
327
328
	
329
	/**
330
	 * @return string
331
	 */
332
	private function getSeparator()
333
	{
334
	    switch((int) bab_pp('wsepar'))
335
	    {
336
	        case 0: return bab_pp('separ');
337
	        case 1: return ',';
338
	        case 2: return "\t";
339
	        case 3: return ';';
340
	    }
341
	}
342
	
343
	
344
	
345
	protected function getDate()
346
	{
347
	    $W = bab_Widgets();
348
	    return $W->DatePicker()->getISODate(bab_rp('date'));
349
	}
350
	
351
	
352
	/**
353
	 * @return array
354
	 */
355
	private function getHeader($year = '', $organization = '')
356
	{
357
	    global $babDB;
358
	    
359
	    $columns = $this->query($year, 'GROUP BY r.id', $organization);
360
	    $this->rights = array();
361
	    
362
	    
363
	    if ($this->dirfields && !empty($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...
364
	    {
365
	        $ov_fields = bab_getDirEntry(BAB_REGISTERED_GROUP, BAB_DIR_ENTRY_ID_GROUP);
366
	    }
367
	    
368
	    $header = array(
369
	        $this->csvEncode(absences_translate('Lastname')),
370
	        $this->csvEncode(absences_translate('Firstname')),
371
	        $this->csvEncode(absences_translate('Total days')),
372
	        $this->csvEncode(absences_translate('Total hours'))
373
	    );
374
	    
375
	    
376
	    $this->recoveries = array(
377
	        'D' => null,
378
	        'H' => null
379
	    );
380
	    
381
	    $recovery = array(
382
	        'D' => false,
383
	        'H' => false
384
	    );
385
	    
386
	    while ($arr = $babDB->db_fetch_assoc($columns))
387
	    {
388
	        $agentRight = new absences_AgentRight();
389
	        $agentRight->setRow($arr);
390
	    
391
	        $right = $agentRight->getRight();
392
	    
393
	        if (absences_Right::RECOVERY === $right->getKind()) {
394
	            $recovery[$right->quantity_unit] = true;
395
	            continue;
396
	        }
397
	    
398
	        $header[] = $this->csvEncode($right->description.' ('.$right->getUnitLabel().')');
399
	        $this->rights[$arr['id_right']] = (count($header) -1);
400
	    }
401
	    
402 View Code Duplication
	    if ($recovery['D']) {
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...
403
	        $header[] = $this->csvEncode(absences_translate('Recoveries (days)'));
404
	        $this->recoveries['D'] = (count($header) -1);
405
	    }
406
	    
407 View Code Duplication
	    if ($recovery['H']) {
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...
408
	        $header[] = $this->csvEncode(absences_translate('Recoveries (hours)'));
409
	        $this->recoveries['H'] = (count($header) -1);
410
	    }
411
	    
412
	    foreach($this->dirfields as $name => $dummy)
413
	    {
414
	        $header[] = $this->csvEncode($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...
415
	    }
416
	    
417
	    return $header;
418
	}
419
	
420
421
422
	public function csv()
423
	{
424
425
		$W = bab_Widgets();
426
		 
427
		if ($date = $W->DatePicker()->getISODate(bab_rp('date'))) {
428
		    if ('0000-00-00' !== $date) {
429
		        $this->date = $date;
430
		    }
431
		}
432
		 
433
		$this->dirfields = bab_rp('dirfields', array());
0 ignored issues
show
Documentation Bug introduced by
It seems like bab_rp('dirfields', array()) of type * is incompatible with the declared type array of property $dirfields.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
434
435
		
436
		
437
        $separator = $this->getSeparator();
438
        $header = $this->getHeader(bab_rp('year'), bab_rp('organization'));
439
        
440
441
        $this->echoCsv($separator, $header);
442
		
443
	}
444
	
445
	
446
	
447
	
448
	
449
	private function echoCsv($separator, $header)
450
	{
451
	    global $babDB;
452
	    
453
	    $this->setHeaders(absences_translate("Vacation"));
454
455
	    echo implode($separator, $header)."\n";
456
457
	    $rows = $this->query(bab_rp('year'), 'GROUP BY u.id, r.id', bab_rp('organization'));
458
	    
459
	    while ($arr = $babDB->db_fetch_assoc($rows))
460
	    {
461
	        if (null !== $line = $this->processRow($arr))
462
	        {
463
	            echo implode($separator, $line)."\n";
464
	        }
465
	    }
466
	    echo implode($separator, $this->processRow(null))."\n";
467
	    
468
	    exit;
469
	}
470
	
471
	
472
	/**
473
	 * @return array
474
	 */
475
	private function getUserRow($id_user)
476
	{
477
	    global $babDB;
478
	    
479
	    $rows = $this->query('', 'GROUP BY u.id, r.id', '', $id_user);
480
	    
481
	    while ($arr = $babDB->db_fetch_assoc($rows))
482
	    {
483
	        if (null !== $line = $this->processRow($arr))
484
	        {
485
	            return $line;
486
	        }
487
	    }
488
	     
489
	    return $this->processRow(null);
490
	}
491
	
492
	
493
	
494
	/**
495
	 * @return Widget_Displayable_Interface
496
	 */
497
	public function compareDateForOneUser($id_user)
498
	{
499
	    require_once $GLOBALS['babInstallPath'].'utilit/dateTime.php';
500
	    
501
	    $this->dirfields = array();
502
	    
503
	    $header = $this->getHeader();
504
	    
505
	    
506
	    $yesterday = BAB_DateTime::now();
507
	    $yesterday->less(1, BAB_DATETIME_DAY);
508
	    
509
	    $this->date = $yesterday->getIsoDate();
510
	    $arr0 = $this->getUserRow($id_user);
511
	    
512
	    $this->date = date('Y-m-d');
513
	    $arr1 = $this->getUserRow($id_user);
514
	    
515
	    $this->date = null;
516
	    $arr2 = $this->getUserRow($id_user);
517
	    
518
	    
519
	    $W = bab_Widgets();
520
	    $table = $W->TableView();
521
	    
522
	    $row = 0;
523
	    
524
	    $table->addItem($W->Label(''), $row, 0 );
525
	    $table->addItem($W->Label('Yesterday'), $row, 1);
526
	    $table->addItem($W->Label('Today'), $row, 2);
527
	    $table->addItem($W->Label('No date'), $row, 3);
528
	    $row++;
529
	    
530
	    foreach ($header as $k => $label) {
531
	        
532
	        if ('"0,0"' === $arr1[$k] && '"0,0"' === $arr2[$k]) {
533
	            continue;
534
	        }
535
	        
536
	        $table->addItem($W->Label($label), $row, 0 );
537
	        $table->addItem($l0 = $W->Label($arr0[$k]), $row, 1);
538
	        $table->addItem($l1 = $W->Label($arr1[$k]), $row, 2);
539
	        $table->addItem($l2 = $W->Label($arr2[$k]), $row, 3);
540
	        
541
	        $values = array_unique(array($arr0[$k], $arr1[$k], $arr2[$k]));
542
	        
543
	        if (1 !== count($values)) {
544
	            $l0->addClass('widget-strong');
545
	            $l1->addClass('widget-strong');
546
	            $l2->addClass('widget-strong');
547
	        }
548
	        
549
	        $row++;
550
	    }
551
	    
552
	    return $table;
553
	}
554
}