Completed
Push — master ( 32d516...69805d )
by Paul
03:43
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
        foreach ($this->dirFields as $field) {
153
            $row[] = $field;
154
        }
155
        
156
        return $row;
157
    }
158
    
159
    /**
160
     * @return array
161
     */
162
    protected function getRow(absences_Agent $agent, absences_AgentRight $agentRight)
163
    {
164
        $collectionName = '';
165
        $collection = $agent->getCollection();
166
        if ($collection->getRow()) {
167
            $collectionName = $collection->name;
168
        }
169
        
170
        $organizationName = '';
171
        if ($organization = $agent->getOrganization()) {
172
            $organizationName = $organization->name;
173
        }
174
        
175
        $right = $agentRight->getRight();
176
        
177
        $name = bab_getUserName($agent->getIdUser(), false);
178
        
179
        $row = array(
180
            $name['lastname'],
181
            $name['firstname'],
182
            $collectionName,
183
            $organizationName,
184
            $right->description,
185
            $right->getType()->name,
186
            $agentRight->getQuantity(),
187
            $agentRight->getConfirmedQuantity(),
188
            $agentRight->getWaitingQuantity(),
189
            $agentRight->getBalance(),
190
            $this->date($right->date_begin),
191
            $this->date($right->date_end)
192
        );
193
        
194
        foreach ($this->dirFields as $field) {
195
            $row[] = $this->getAgentDirValue($agent, $field);
196
        }
197
        
198
        return $row;
199
    }
200
    
201
    
202
203
204
    
205
    
206
    public function download()
207
    {
208
        $this->setHeaders(absences_translate('rights'));
209
        
210
        $this->outputArr($this->getHeader());
211
        
212
        foreach ($this->selectAgents() as $agent) {
213
            
214
            // allocated duration per agent
215
            bab_setTimeLimit(10);
216
            
217
            $res = $this->selectAgentRights($agent);
218
            
219
            foreach ($res as $agentRight) {
220
                $this->outputArr($this->getRow($agent, $agentRight));
221
            }
222
        }
223
        
224
        die();
225
    }
226
}
227
228
229
230
231
232
233
234
class absences_RightExportTemplate
235
{
236
    public $separatortxt;
237
    public $other;
238
    public $comma;
239
    public $tab;
240
    public $semicolon;
241
    public $export;
242
    public $sepdectxt;
243
    public $t_year;
244
    public $t_organization;
245
    public $additional_fields;
246
    
247
    protected $resYears;
248
    protected $resOrganization;
249
    protected $dirfields;
250
    
251
    public $year;
252
    public $fieldname;
253
    public $organization;
254
    public $id_organization;
255
    
256
    public function __construct()
257
    {
258
        global $babDB;
259
        
260
        $this->separatortxt = absences_translate("Separator");
261
        $this->other = absences_translate("Other");
262
        $this->comma = absences_translate("Comma");
263
        $this->tab = absences_translate("Tab");
264
        $this->semicolon = absences_translate("Semicolon");
265
        $this->export = absences_translate("Export");
266
        $this->sepdectxt = absences_translate("Decimal separator");
267
        $this->t_year = absences_translate('Year filter');
268
        $this->t_organization = absences_translate('Organization');
269
        $this->additional_fields = absences_translate("Additional fields to export:");
270
271
        $this->resYears = $babDB->db_query("SELECT YEAR(date_begin) year FROM absences_rights WHERE YEAR(date_begin)<>'0' GROUP BY year");
272
        
273
        $this->resOrganization = $babDB->db_query("SELECT * FROM `absences_organization` ORDER BY name ASC");
274
275
        $this->dirfields = bab_getDirEntry(BAB_REGISTERED_GROUP, BAB_DIR_ENTRY_ID_GROUP);
276
        
277
        unset($this->dirfields['sn']);
278
        unset($this->dirfields['givenname']);
279
        unset($this->dirfields['jpegphoto']);
280
    }
281
    
282
    
283
284
    /**
285
     * template method to list available years
286
     */
287 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...
288
    {
289
        global $babDB;
290
    
291
        if ($arr = $babDB->db_fetch_assoc($this->resYears))
292
        {
293
            $this->year = bab_toHtml($arr['year']);
294
            return true;
295
        }
296
    
297
        return false;
298
    }
299
    
300
    
301 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...
302
    {
303
        if (list($name,$arr) = each($this->dirfields))
304
        {
305
            $this->fieldname = bab_toHtml($name);
306
            $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...
307
            return true;
308
        }
309
        return false;
310
    }
311
    
312
    
313 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...
314
    {
315
        global $babDB;
316
        
317
        if ($arr = $babDB->db_fetch_assoc($this->resOrganization))
318
        {
319
            $this->organization = bab_toHtml($arr['name']);
320
            $this->id_organization = bab_toHtml($arr['id']);
321
            return true;
322
        }
323
        
324
        return false;
325
    }
326
}
327
328
329
330
331
332
333
334
335
336
337
338
339
function absences_exportForm()
340
{
341
    if (!empty($_POST)) {
342
        
343
        switch (bab_rp('wsepar')) {
344
            case '0':
345
                $separator = bab_rp('separ');
346
                break;
347
                
348
            case '1':
349
                $separator = ',';
350
                break;
351
                
352
            case '2':
353
                $separator = "\t";
354
                break;
355
            
356
            case '3':
357
                $separator = ';';
358
                break;
359
        }
360
        
361
        $export = new absences_RightExportCsv(bab_rp('org'), bab_rp('year'), (array) bab_rp('dirfields'), $separator, bab_rp('sepdec'));
0 ignored issues
show
Bug introduced by
The variable $separator 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...
362
        $export->download();
363
    }
364
    
365
    
366
    $addon = bab_getAddonInfosInstance('absences');
367
    
368
    $template = new absences_RightExportTemplate();
369
    bab_getBody()->babecho($addon->printTemplate($template, 'rightexport.html'));
370
}