absences_RightRule::setRight()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 9.4285
ccs 0
cts 3
cp 0
crap 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 1
require_once dirname(__FILE__).'/record.class.php';
26 1
require_once dirname(__FILE__).'/right.class.php';
27 1
require_once dirname(__FILE__).'/type.class.php';
28
29
/**
30
 * Abscence Right
31
 *
32
 * @property	int		$id_right
33
 * @property	int		$validoverlap
34
 * @property	float	$trigger_nbdays_min
35
 * @property 	float	$trigger_nbdays_max
36
 * @property	int		$trigger_type
37
 * @property	string	$trigger_p1_begin
38
 * @property	string	$trigger_p1_end
39
 * @property	string	$trigger_p2_begin
40
 * @property 	string	$trigger_p2_end
41
 * 
42
 */
43
class absences_RightRule extends absences_Record 
44
{
45
	private $right;
46
	
47
	private $type;
48
	
49
	private $_id_right;
50
	
51
	public static function getFromId($id)
52
	{
53
		$rightrule = new absences_RightRule;
54
		$rightrule->id = $id;
55
		return $rightrule;
56
	}
57
	
58 10
	public static function getFromRight($id_right)
59
	{
60 10
		$rightrule = new absences_RightRule;
61 10
		$rightrule->_id_right = $id_right;
62 10
		return $rightrule;
63
	}
64
	
65
	/**
66
	 * 
67
	 * @return array
68
	 */
69 8 View Code Duplication
	public function getRow()
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...
70
	{
71 8
		if (null === $this->row)
72 8
		{
73 8
			if (!isset($this->id) && !isset($this->_id_right))
74 8
			{
75
				$this->row = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type array of property $row.

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...
76
				return false;
77
			}
78
			
79
			
80 8
			global $babDB;
81
			
82 8
			$query = 'SELECT * FROM absences_rights_rules WHERE ';
83
			
84 8
			if (isset($this->id))
85 8
			{
86
				$query .= 'id='.$babDB->quote($this->id);
87
			}
88
			
89 8
			if (isset($this->_id_right))
90 8
			{
91 8
				$query .= 'id_right='.$babDB->quote($this->_id_right);
92 8
			}
93
			
94 8
			$res = $babDB->db_query($query);
95 8
			$this->setRow($babDB->db_fetch_assoc($res));
96 8
		}
97
		
98 8
		return $this->row;
99
	}
100
	
101
	/**
102
	 * 
103
	 * @param absences_Right $right
104
	 * @return absences_RightRule
105
	 */
106
	public function setRight(absences_Right $right)
107
	{
108
		$this->right = $right;
109
		return $this;
110
	}
111
	
112
	/**
113
	 * @return absences_Right
114
	 */
115
	public function getRight()
116
	{
117
		if (!isset($this->right))
118
		{
119
			$row = $this->getRow();
120
			$this->right = new absences_Right($row['id_right']);
121
		}
122
		
123
		return $this->right;
124
	}
125
	
126
	
127
	
128
	/**
129
	 * @return absences_Type
130
	 */
131 View Code Duplication
	public function getType()
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...
132
	{
133
		if (!isset($this->type))
134
		{
135
			$row = $this->getRow();
136
			
137
			if (empty($row['trigger_type']))
138
			{
139
				return null;
140
			}
141
			
142
			$this->type = new absences_Type($row['trigger_type']);
143
		}
144
	
145
		return $this->type;
146
	}
147
	
148
	
149
	
150
	public function setType(absences_Type $type)
151
	{
152
		$this->type = $type;
153
		return $this;
154
	}
155
	
156
	
157
	
158
	/**
159
	 * @return ressource
160
	 */
161 10
	public function getInPeriodRes()
162
	{
163 10
		if (isset($this->_id_right))
164 10
		{
165 10
			$id_right = $this->_id_right;
166 10
		} else if(isset($this->id_right))
167
		{
168
			$id_right = $this->id_right;
169
		} else if(isset($this->right))
170
		{
171
			$id_right = $this->right->id;
172
		} else {
173
			throw new Exception('Failed');
174
		}
175
		
176 10
		global $babDB;
177 10
		return $babDB->db_query('SELECT * FROM '.ABSENCES_RIGHTS_INPERIOD_TBL.' WHERE id_right='.$babDB->quote($id_right));
178
	}
179
	
180
	
181
	
182
	
183
	/**
184
	 * Disponibilite en fonction de la periode de conges demandee
185
	 * 
186
	 * @param	int		&$access_include	Cumul des tests sur les periodes d'inclusion
187
	 * @param	int		&$access_exclude	Cumul des tests sur les periodes d'exclusion
188
	 * @param 	int 	$beginp				Timestamp, debut de la periode demandee
189
	 * @param 	int 	$endp				Timestamp, fin de la periode demandee
190
	 * @param	int		$inperiod			1: Dans l'intervalle, 2: en dehors de l'intervalle
191
	 * @param	int		$validoverlap		1: Permettre le chevauchement de la periode de conges avec les periodes de test
192
	 * @param	string	$start				ISO datetime, Debut de la periode a tester
193
	 * @param	string	$end				ISO datetime, Fin de la periode a tester
194
	 * 
195
	 * 			
196
	 */
197
	public function addPeriodTest(&$access_include, &$access_exclude, $beginp, $endp, $inperiod, $validoverlap, $start, $end)
198
	{
199
		// periode de test
200
		$period_start 	= bab_mktime($start);
201
		$period_end 	= 86400 + bab_mktime($end);
202
		
203
		
204
		$period_access = ((int) $inperiod) + (((int) $validoverlap)*10);
205
		
206
		switch ($period_access)
207
		{
208
			case 0: // Toujours
209
			case 10:
210
		
211
				break;
212
		
213 View Code Duplication
			case 1: // Dans la periode de la regle
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...
214
				if ($period_start <= $beginp && $period_end >= $endp) {
215
					$access_include |= 1;
216
					$debug_result = 'TRUE';
217
				} else {
218
					$access_include |= 0;
219
					$debug_result = 'FALSE';
220
				}
221
		
222
				bab_debug(
223
						"Disponibilite en fonction de la periode de conges demandee\n".
224
						"Dans l'intervale\n".
225
						'id = '.$this->id_right."\n".
226
						'description = '.$this->getRight()->description."\n".
227
						bab_shortDate($period_start).' <= '.bab_shortDate($beginp).
228
						' && '.bab_shortDate($period_end).' >= '.bab_shortDate($endp)."\n".
229
						' => '.$debug_result
230
				);
231
		
232
		
233
				break;
234
		
235 View Code Duplication
			case 2: // En dehors de la periode de la regle
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...
236
				if ($period_end <= $beginp || $period_start >= $endp) {
237
					$access_exclude &= 1;
238
					$debug_result = 'TRUE';
239
				} else {
240
					$access_exclude &= 0;
241
					$debug_result = 'FALSE';
242
				}
243
		
244
		
245
				bab_debug(
246
						"Disponibilite en fonction de la periode de conges demandee\n".
247
						"En dehors de l'intervale\n".
248
						'id = '.$this->id_right."\n".
249
						'description = '.$this->getRight()->description."\n".
250
						bab_shortDate($period_end).' <= '.bab_shortDate($beginp).
251
						' && '.bab_shortDate($period_start).' >= '.bab_shortDate($endp)."\n".
252
						' => '.$debug_result
253
				);
254
				break;
255
		
256 View Code Duplication
			case 11: // Dans la periode de la regle mais peut depasser a l'exterieur
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...
257
		
258
				if ($period_start < $endp && $period_end > $beginp ) {
259
					$access_include |= 1;
260
					$debug_result = 'TRUE';
261
				} else {
262
					$access_include |= 0;
263
					$debug_result = 'FALSE';
264
				}
265
		
266
		
267
				bab_debug(
268
						"Disponibilite en fonction de la periode de conges demandee\n".
269
						"Dans l'intervale mais peut depasser a l'exterieur\n".
270
						'id = '.$this->id_right."\n".
271
						'description = '.$this->getRight()->description."\n".
272
						bab_shortDate($period_start).' < '.bab_shortDate($endp).
273
						' && '.bab_shortDate($period_end).' > '.bab_shortDate($beginp)."\n".
274
						' => '.$debug_result
275
				);
276
				break;
277
		
278 View Code Duplication
			case 12: // En dehors de la periode de la regle mais peut depasser a l'interieur
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...
279
				if ($period_start > $beginp || $period_end < $endp) {
280
					$access_exclude &= 1;
281
					$debug_result = 'TRUE';
282
				} else {
283
					$access_exclude &= 0;
284
					$debug_result = 'FALSE';
285
				}
286
		
287
				bab_debug(
288
						"acces sur la periode, en fonction de la periode de la demande\n".
289
						"En dehors de l'intervale mais peut depasser a l'interieur\n".
290
						'id = '.$this->id_right."\n".
291
						'description = '.$this->getRight()->description."\n".
292
						bab_shortDate($period_start).' < '.bab_shortDate($endp).
293
						' && '.bab_shortDate($period_end).' > '.bab_shortDate($beginp)."\n".
294
						' => '.$debug_result
295
				);
296
				break;
297
		}
298
	}
299
	
300
	
301
	
302
	
303
	/**
304
	 * Disponibilite en fonction de la periode de conges demandee
305
	 * le doit est accessible si on ne test pas de demande (premiere page de la demande, ne pas appeller cette methode dans ce cas)
306
	 * 
307
	 * @param int $beginp	Timestamp, debut de la periode demandee
308
	 * @param int $endp		Timestamp, fin de la periode demandee
309
	 * 
310
	 * @return bool
311
	 */
312 10
	public function isAccessibleOnPeriod($beginp, $endp)
313
	{
314 10
		global $babDB;
315
		
316 10
		$res = $this->getInPeriodRes();
317
		
318 10
		if( !$res || $babDB->db_num_rows($res) == 0 )
319 10
		{
320 10
			return true;
321
		}
322
		
323
		
324
		$access_include = 0;
325
		$access_exclude = 1;
326
		
327
		
328
		while ($arr = $babDB->db_fetch_assoc($res)) {
329
		
330
			$this->addPeriodTest(
331
				$access_include, // reference
332
				$access_exclude, // reference
333
				$beginp, 
334
				$endp, 
335
				$arr['right_inperiod'], 
336
				$this->validoverlap, 
337
				$arr['period_start'], 
338
				$arr['period_end']
339
			);
340
			
341
		}
342
		
343
		$debug_include = $access_include ? 'TRUE' : 'FALSE';
344
		$debug_exclude = $access_exclude ? 'TRUE' : 'FALSE';
345
		
346
		bab_debug(sprintf("id = %d \ntests de periodes d'inclusion %s \ntests de periodes d'exclusion %s\n",$this->id_right, $debug_include, $debug_exclude));
347
		
348
		return ($access_include && $access_exclude);
349
	
350
	}
351
	
352
353
	
354
	/**
355
	 * Attribution du droit en fonction des jours demandes et valides
356
	 * @return bool
357
	 */
358
	public function isAccessibleAccordingToRequests($id_user)
359
	{
360
		global $babDB;
361
		
362
		$p1 = '';
363
		$p2 = '';
364
		$req = '';
365
		
366
		if ('0000-00-00' != $this->trigger_p1_begin && '0000-00-00' != $this->trigger_p1_end) {
367
			$p1 = "(e.date_begin < ".$babDB->quote($this->trigger_p1_end)." AND e.date_end > ".$babDB->quote($this->trigger_p1_begin).')';
368
		}
369
		
370
		if ('0000-00-00' != $this->trigger_p2_begin && '0000-00-00' != $this->trigger_p2_end) {
371
			$p2 = "(e.date_begin < ".$babDB->quote($this->trigger_p2_end)." AND e.date_end > ".$babDB->quote($this->trigger_p2_begin).')';
372
		}
373
		
374
		if ($p1 && $p2) {
375
			$req = 'AND ('.$p1.' OR '.$p2.')';
376
		} else if ($p1 || $p2) {
377
			$req = 'AND '.$p1.$p2;
378
		}
379
		
380
		
381
		if (!$req) {
382
			
383
			return true;	
384
		}
385
		
386
	
387
	
388
		if (!empty($this->trigger_type))
389
		{
390
			$table = ", ".ABSENCES_RIGHTS_TBL." r ";
391
			$where = " AND el.id_right=r.id AND r.id_type=".$babDB->quote($this->trigger_type)." ";
392
		}
393
		else
394
		{
395
			$table = '';
396
			$where = '';
397
		}
398
	
399
		$req = "SELECT
400
		e.date_begin,
401
		e.date_end,
402
		sum(el.quantity) total
403
		FROM
404
		".ABSENCES_ENTRIES_ELEM_TBL." el,
405
		".ABSENCES_ENTRIES_TBL." e
406
		".$table."
407
		WHERE
408
		e.id_user=".$babDB->quote($id_user)."
409
		and e.status='Y'
410
		and el.id_entry=e.id
411
		".$req.$where."
412
		GROUP BY e.id";
413
	
414
		$nbdays = 0;
415
		$res_entry = $babDB->db_query($req);
416
		while ($entry = $babDB->db_fetch_assoc($res_entry)) {
417
	
418
			list($entry_date_begin) = explode(' ',$entry['date_begin']);
419
			list($entry_date_end) = explode(' ',$entry['date_end']);
420
	
421
			$intersect_p1 = BAB_DateTime::periodIntersect(
422
					$entry_date_begin,
423
					$entry_date_end,
424
					$this->trigger_p1_begin,
425
					$this->trigger_p1_end
426
			);
427
	
428 View Code Duplication
			if (false !== $intersect_p1) {
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...
429
				$period_length = 1 + BAB_DateTime::dateDiffIso($intersect_p1['begin'], $intersect_p1['end']);
430
				// + 1 for end day
431
				if ($period_length < $entry['total']) {
432
					$nbdays += $period_length;
433
				} else {
434
					$nbdays += $entry['total'];
435
				}
436
			}
437
	
438
			$intersect_p2 = BAB_DateTime::periodIntersect(
439
					$entry['date_begin'],
440
					$entry['date_end'],
441
					$this->trigger_p2_begin,
442
					$this->trigger_p2_end
443
			);
444
	
445 View Code Duplication
			if (false !== $intersect_p2) {
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...
446
				$period_length = 1 + BAB_DateTime::dateDiffIso($intersect_p2['begin'], $intersect_p2['end']);
447
				// + 1 for end day
448
				if ($period_length < $entry['total']) {
449
					$nbdays += $period_length;
450
				} else {
451
					$nbdays += $entry['total'];
452
				}
453
			}
454
		}
455
	
456
		if ( '' !== $this->trigger_nbdays_min && '' !== $this->trigger_nbdays_max && $this->trigger_nbdays_min <= $nbdays && $nbdays < $this->trigger_nbdays_max ) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison !== seems to always evaluate to true as the types of '' (string) and $this->trigger_nbdays_min (double) can never be identical. Maybe you want to use a loose comparison != instead?
Loading history...
Unused Code Bug introduced by
The strict comparison !== seems to always evaluate to true as the types of '' (string) and $this->trigger_nbdays_max (double) can never be identical. Maybe you want to use a loose comparison != instead?
Loading history...
457
	
458
			$right = $this->getRight();
459
			
460
			bab_debug(
461
					"Attribution du droit en fonction des jours demandes et valides\n".
462
					"Le droit est accorde si l'utilisateur a pris entre ".$this->trigger_nbdays_min." et ".$this->trigger_nbdays_max." jours\n".
463
					$right->description."\n".
464
					"nb de jours pris : ".$nbdays
465
			);
466
			return true;
467
		}
468
		
469
		
470
		return false;
471
	
472
	}
473
	
474
	
475
	
476
	
477
	
478
	
479
	
480
	
481
	public function insert()
482
	{
483
		global $babDB;
484
		
485
		$babDB->db_query('INSERT INTO absences_rights_rules 
486
			(
487
				id_right,
488
				validoverlap,
489
				trigger_nbdays_min,
490
  				trigger_nbdays_max,
491
  				trigger_type,
492
  				trigger_p1_begin,
493
  				trigger_p1_end,
494
  				trigger_p2_begin,
495
  				trigger_p2_end,
496
  				trigger_overlap
497
			) 
498
				VALUES 
499
			(
500
				'.$babDB->quote($this->id_right).',
501
				'.$babDB->quote($this->validoverlap).',
502
				'.$babDB->quote($this->trigger_nbdays_min).',
503
				'.$babDB->quote($this->trigger_nbdays_max).',
504
				'.$babDB->quote($this->trigger_type).',
505
				'.$babDB->quote($this->trigger_p1_begin).',
506
				'.$babDB->quote($this->trigger_p1_end).',
507
				'.$babDB->quote($this->trigger_p2_begin).',
508
				'.$babDB->quote($this->trigger_p2_end).',
509
				'.$babDB->quote($this->trigger_overlap).'
0 ignored issues
show
Documentation introduced by
The property trigger_overlap does not exist on object<absences_RightRule>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
510
			)
511
		');
512
		
513
		
514
		$this->id = $babDB->db_insert_id();
515
	}
516
	
517
	
518
	
519
	public function update()
520
	{
521
		global $babDB;
522
		
523
		$babDB->db_query('UPDATE absences_rights_rules SET 
524
				
525
			validoverlap 		= '.$babDB->quote($this->validoverlap).',
526
			trigger_nbdays_min 	= '.$babDB->quote($this->trigger_nbdays_min).',
527
  			trigger_nbdays_max 	= '.$babDB->quote($this->trigger_nbdays_max).',
528
  			trigger_p1_begin 	= '.$babDB->quote($this->trigger_p1_begin).',
529
  			trigger_p1_end 		= '.$babDB->quote($this->trigger_p1_end).',
530
  			trigger_p2_begin 	= '.$babDB->quote($this->trigger_p2_begin).',
531
  			trigger_p2_end 		= '.$babDB->quote($this->trigger_p2_end).',
532
  			trigger_overlap 	= '.$babDB->quote($this->trigger_overlap).'	
0 ignored issues
show
Documentation introduced by
The property trigger_overlap does not exist on object<absences_RightRule>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
533
				
534
		WHERE id_right='.$babDB->quote($this->id_right));
535
		
536
	}
537
}
538
539
540
541
542
543
544