Completed
Push — master ( f82207...3a9802 )
by Paul
03:49
created

absences_RightExportTemplate   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 40.23 %

Coupling/Cohesion

Components 3
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 3
cbo 0
dl 35
loc 87
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 28 1
A getnextyear() 12 12 2
A getnextfield() 10 10 2
A getnextorganization() 13 13 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
        require_once dirname(__FILE__).'/organization.class.php';
104
        
105
        $res = new absences_AgentIterator();
106
        
107
        if (isset($this->org)) {
108
            $organization = absences_Organization::getById($this->org);
109
            $res->setOrganization($organization);
110
        }
111
        
112
        return $res;
113
    }
114
    
115
    
116
    
117
    
118
    /**
119
     * 
120
     * @param absences_Agent $agent
121
     * @return absences_AgentRightManagerIterator
122
     */
123
    protected function selectAgentRights(absences_Agent $agent)
124
    {
125
        require_once dirname(__FILE__).'/agent_right.class.php';
126
		$I = new absences_AgentRightManagerIterator;
127
		$I->setAgent($agent);
128
		
129
		if (isset($this->year)) {
130
		    $I->year = $this->year;
131
		}
132
		
133
		return $I;
134
    }
135
    
136
    
137
    /**
138
     * @return array
139
     */
140
    protected function getHeader()
141
    {
142
        $row = array(
143
            absences_translate('Lastname'),
144
            absences_translate('First name'),
145
            absences_translate('Right collection'),
146
            absences_translate('Organization'),
147
            absences_translate('Right description'),
148
            absences_translate('Type'),
149
            absences_translate('Initial quantity'),
150
    		absences_translate('Consumed'),
151
    		absences_translate('Waiting approval'),
152
    		absences_translate('Balance'),
153
    		absences_translate('Begin date'),
154
    		absences_translate('End date'),
155
    		absences_translate('Accessible')
156
        );
157
        
158
        foreach ($this->dirFields as $field) {
159
            $row[] = $field;
160
        }
161
        
162
        return $row;
163
    }
164
    
165
    /**
166
     * @return array
167
     */
168
    protected function getRow(absences_Agent $agent, absences_AgentRight $agentRight)
169
    {
170
        $collectionName = '';
171
        $collection = $agent->getCollection();
172
        if ($collection->getRow()) {
173
            $collectionName = $collection->name;
174
        }
175
        
176
        $organizationName = '';
177
        $organization = $agent->getOrganization();
178
        if ($organization->getRow()) {
179
            $organizationName = $organization->name;
180
        }
181
        
182
        $right = $agentRight->getRight();
183
        
184
        $row = array(
185
            $collectionName,
186
            $organizationName,
187
            $right->description,
188
            $right->getType()->name,
189
            $agentRight->getQuantity(),
190
            $agentRight->getConfirmedQuantity(),
191
            $agentRight->getWaitingQuantity(),
192
            $agentRight->getBalance(),
193
            $this->date($right->date_begin),
194
            $this->date($right->date_end)
195
        );
196
        
197
        foreach ($this->dirFields as $field) {
198
            $row[] = $this->getAgentDirValue($agent, $field);
199
        }
200
        
201
        return $row;
202
    }
203
    
204
    
205
206
    /**
207
     * output an array on raw values
208
     * @param array $arr
209
     *
210
     */
211
    protected function outputArr(Array $arr)
212
    {
213
        array_walk($arr, array($this, 'csvEncode'));
214
        echo implode($this->separator, $arr);
215
    }
216
    
217
    
218
    public function download()
219
    {
220
        $this->setHeaders(absences_translate('rights'));
221
        
222
        $this->outputArr($this->getHeader());
223
        
224
        foreach ($this->selectAgents() as $agent) {
225
            foreach ($this->selectAgentRights($agent) as $agentRight) {
226
                
227
                $this->outputArr($this->getRow($agent, $agentRight));
228
            }
229
        }
230
        
231
        die();
232
    }
233
}
234
235
236
237
238
239
240
241
class absences_RightExportTemplate
242
{
243
    public $separatortxt;
244
    public $other;
245
    public $comma;
246
    public $tab;
247
    public $semicolon;
248
    public $export;
249
    public $sepdectxt;
250
    public $t_year;
251
    public $t_organization;
252
    public $additional_fields;
253
    
254
    public function __construct()
255
    {
256
        global $babDB;
257
        
258
        $this->separatortxt = absences_translate("Separator");
259
        $this->other = absences_translate("Other");
260
        $this->comma = absences_translate("Comma");
261
        $this->tab = absences_translate("Tab");
262
        $this->semicolon = absences_translate("Semicolon");
263
        $this->export = absences_translate("Export");
264
        $this->sepdectxt = absences_translate("Decimal separator");
265
        $this->t_year = absences_translate('Year filter');
266
        $this->t_organization = absences_translate('Organization');
267
        $this->additional_fields = absences_translate("Additional fields to export:");
268
        
269
        $W = bab_Widgets();
0 ignored issues
show
Unused Code introduced by
$W is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
270
271
        $this->resYears = $babDB->db_query("SELECT YEAR(date_begin) year FROM absences_rights WHERE YEAR(date_begin)<>'0' GROUP BY year");
0 ignored issues
show
Bug introduced by
The property resYears 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...
272
        
273
        $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...
274
        
275
        
276
        $this->dirfields = bab_getDirEntry(BAB_REGISTERED_GROUP, BAB_DIR_ENTRY_ID_GROUP);
0 ignored issues
show
Bug introduced by
The property dirfields 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...
277
        
278
        unset($this->dirfields['sn']);
279
        unset($this->dirfields['givenname']);
280
        unset($this->dirfields['jpegphoto']);
281
    }
282
    
283
    
284
285
    /**
286
     * template method to list available years
287
     */
288 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...
289
    {
290
        global $babDB;
291
    
292
        if ($arr = $babDB->db_fetch_assoc($this->resYears))
293
        {
294
            $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...
295
            return true;
296
        }
297
    
298
        return false;
299
    }
300
    
301
    
302 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...
303
    {
304
        if (list($name,$arr) = each($this->dirfields))
305
        {
306
            $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...
307
            $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...
308
            return true;
309
        }
310
        return false;
311
    }
312
    
313
    
314 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...
315
    {
316
        global $babDB;
317
        
318
        if ($arr = $babDB->db_fetch_assoc($this->resOrganization))
319
        {
320
            $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...
321
            $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...
322
            return true;
323
        }
324
        
325
        return false;
326
    }
327
}
328
329
330
331
332
333
334
335
336
337
338
339
340
function absences_exportForm()
341
{
342
    if (!empty($_POST)) {
343
        
344
        $export = new absences_RightExportCsv(bab_rp('org'), bab_rp('year'), (array) bab_rp('dirfields'));
345
        $export->download();
346
    }
347
    
348
    
349
    $addon = bab_getAddonInfosInstance('absences');
350
    
351
    $template = new absences_RightExportTemplate();
352
    bab_getBody()->babecho($addon->printTemplate($template, 'rightexport.html'));
353
}