absences_Request::getLastMovement()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 0
dl 0
loc 15
ccs 0
cts 7
cp 0
crap 12
rs 9.4285
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
26
require_once dirname(__FILE__).'/record.class.php';
27
require_once $GLOBALS['babInstallPath'].'utilit/wfincl.php';
28
29
30
31
32
33
/**
34
 * A user request (entry, cet deposit, workperiod recover)
35
 * 
36
 * @property	int 	$id
37
 * @property	int 	$id_user
38
 * @property 	int		$idfai
39
 * @property 	string	$status
40
 * @property 	int		$id_approver
41
 * @property 	int		$appr_notified	
42
 * @property	string	$comment2
43
 * @property    int     $todelete
44
 * @property    int     $firstconfirm
45
 * 
46
 */
47
abstract class absences_Request extends absences_Record
48
{
49
	
50
	/**
51
	 *
52
	 * @var absences_Agent
53
	 */
54
	protected $agent;
55
	
56
	
57
	/**
58
	 * 
59
	 * @var absences_Movement
60
	 */
61
	private $lastMovement;
62
	
63
	
64
	
65
	/**
66
	 * @param int $id
67
	 * @return absences_Request
68
	 */
69
	abstract static public function getById($id);
70
	
71
	
72
	/**
73
	 * @return absences_Agent
74
	 */
75 5 View Code Duplication
	public function getAgent()
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...
76
	{
77 5
		require_once dirname(__FILE__).'/agent.class.php';
78
	
79 5
		if (!isset($this->agent))
80 5
		{
81 5
			$row = $this->getRow();
82 5
			$this->agent = absences_Agent::getFromIdUser($row['id_user']);
83 5
		}
84
	
85 5
		return $this->agent;
86
	}
87
	
88
	
89
	
90
	
91
	/**
92
	 * Method to sort requests by creation date
93
	 */
94
	public function createdOn()
95
	{
96
		return $this->createdOn;
0 ignored issues
show
Documentation introduced by
The property createdOn does not exist on object<absences_Request>. 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...
97
	}
98
99
	/**
100
	 * Method to sort requests by modification date
101
	 */
102
	public function modifiedOn()
103
	{
104
		return $this->modifiedOn;
0 ignored issues
show
Documentation introduced by
The property modifiedOn does not exist on object<absences_Request>. 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...
105
	}
106
107
	/**
108
	 *
109
	 * @return string
110
	 */
111
	public function getUserName()
112
	{
113
		return bab_getUserName($this->id_user);
114
	}
115
116
117
	protected function labelledValue($title, $value)
118
	{
119
		$W = bab_Widgets();
120
		return $W->FlowItems($W->Label($title)->addClass('widget-strong')->colon(), $W->Label($value))->setHorizontalSpacing(.4,'em');
121
	}
122
	
123
	/**
124
	 * 
125
	 * @return string
126
	 */
127
	abstract public function getRequestType();
128
	
129
	/**
130
	 * Get status on date
131
	 * @param string $date
132
	 * @return string
133
	 */
134 10
	public function getDateStatus($date)
135
	{
136 10
        if (!isset($date) || '0000-00-00' === $date) {
137 4
            return $this->status;
138
        }
139
	    
140
	    
141 7
	    require_once dirname(__FILE__).'/movement.class.php';
142 7
	    $I = new absences_MovementIterator();
143 7
	    $I->setRequest($this);
144 7
	    $I->createdOn = $date;
145
	    
146 7
	    foreach($I as $movement) {
147
148
	        // mouvement le plus recent avant la date
149
	        
150 4
	        if (!isset($movement->status)) {
151
	            // movement created before status field in database
152
	            return $this->status;
153
	        }
154
	        
155 4
	        return $movement->status;
156
	        
157 6
	    }
158
	    
159 6
	    return $this->status;
160
	}
161
	
162
	/**
163
	 * @return string
164
	 */
165 1
	public function getShortStatusStr()
166
	{
167 1
	    switch($this->status)
168
	    {
169 1
	        case 'Y':
170 1
	            return absences_translate("Accepted");
171
	        case 'N':
172
	            return absences_translate("Refused");
173
	        case 'P':
174
	            return absences_translate("Previsional request");
175
	        default:
176
	            return absences_translate("Waiting approval");
177
	    }
178
	}
179
	
180
	
181
	
182
	private function endApprovalOnMissingApprovers()
183
	{
184
	    $arr = bab_WFGetWaitingApproversInstance($this->idfai);
185
	    
186
	    if (0 === count($arr)) {
187
	        try {
188
	            $this->acceptApprovalWorkflow();
189
	            $this->save();
190
	        } catch (Exception $e) {
191
	            // Ex: fail to update the event in caldav
192
	            bab_debug($this->getTitle().' : '.$e->getMessage(), DBG_FATAL);
193
	        }
194
	    }
195
	}
196
	
197
198
199
	/**
200
	 * Get status as a string
201
	 * @return string
202
	 */
203
	public function getStatusStr()
204
	{
205
	    if ('' === $this->status) {
206
	        $this->endApprovalOnMissingApprovers();
207
	    }
208
	    
209
		switch($this->status)
210
		{
211
			case 'Y':
212
				return absences_translate("Accepted");
213
			case 'N':
214
				return absences_translate("Refused");
215
			case 'P':
216
				return absences_translate("Previsional request");
217
			default:
218
			    if ($this->todelete) {
219
			        return sprintf(absences_translate("Will be deleted after approval by %s"), $this->getNextApprovers());
220
			    }
221
				return sprintf(absences_translate("Waiting approval by %s"), $this->getNextApprovers());
222
		}
223
	}
224
	
225
	
226
	
227
	/**
228
	 * @return Widget_Icon
229
	 */
230
	public function getStatusIcon()
231
	{
232
		$W = bab_Widgets();
233
		
234
		switch($this->status)
235
		{
236
			case 'Y':
237
				$icon = Func_Icons::ACTIONS_DIALOG_OK;
238
				break;
239
			case 'N':
240
				$icon = Func_Icons::ACTIONS_DIALOG_CANCEL;
241
				break;
242
			case 'P':
243 4
				$icon = Func_Icons::STATUS_DIALOG_QUESTION;
244 4
				break;
245
			default:
246
				$icon = Func_Icons::ACTIONS_VIEW_HISTORY;
247
				break;
248 4
		}
249
		
250
		return $W->Icon($this->getStatusStr(), $icon);
251
	}
252
	
253
	
254
	/**
255
	 * Test if the has been fixed by a manager (not modifiable by the user)
256
	 * @return bool
257
	 */
258
	public function isFixed()
259 4
	{
260
	    return false;
261 4
	
262
	}
263
264
265
	private function getNames($arr)
266
	{
267
		$return = array();
268 4
269
		foreach($arr as $k => $id_user)
270
		{
271
			$return[$k] = bab_getUserName($id_user);
272
		}
273
274
		return $return;
275
	}
276
277
278
	/**
279
	 * @return string
280 4
	 */
281
	public function getNextApprovers()
282
	{
283
		$arr = bab_WFGetWaitingApproversInstance($this->idfai);
284
285
		if (count($arr) <= 3)
286
		{
287 4
			return implode(', ', $this->getNames($arr));
288
		}
289
290
		$first = array_shift($arr);
291 4
		$second = array_shift($arr);
292
293 4
		return sprintf(absences_translate('%s, %s and %d other approvers'), bab_getUserName($first), bab_getUserName($second), count($arr));
294
	}
295
	
296
	/**
297 4
	 * Get the apporbation sheme ID for the request
298
	 * @return int
299
	 */
300
	abstract public function getApprobationId();
301
	
302
	
303 4
	
304 4
	/**
305
	 * @return bool
306
	 */
307 4
	protected function autoApproval()
308
	{
309
	    if (isset($this->todelete) && $this->todelete) {
310
	        $this->addMovement(sprintf(absences_translate('The %s has been deleted with auto-approval'), $this->getTitle()));
311
	        $this->delete();
312
	        return false;
313
	    }
314
	    
315
	    
316
	    $this->onConfirm();
317
	    $this->addMovement(sprintf(absences_translate('The %s has been accepted with auto-approval'), $this->getTitle()));
318
	    return false;
319 4
	}
320
	
321
	
322
	
323
	private function getAutoApprovalUserId()
324
	{
325
	    if (!absences_getVacationOption('auto_approval')) {
326
	        return 0;
327
	    }
328
	    
329
	    return bab_getUserId();
330
	}
331
	
332
	
333
	
334
	/**
335
	 * Create approbation instance and set it "notified" but do not send email notification
336
	 * retourne TRUE si la demande est bien devenue en attente d'approbation
337
	 * @return bool
338
	 */
339
	public function createApprobationInstance()
340
	{
341
		
342
		if (isset($this->idfai) && $this->idfai > 0)
343 4
		{
344 4
			bab_WFDeleteInstance($this->idfai);
345 4
		}
346
		
347 4
		// si le 3eme parametre est passe avec l'id user en cours, cela active l'auto-approbation
348 4
		// l'auto-approbation a ete active a partir de la version 2.21 du module
349 4
		// le dernier parametre necessite la version 8.0.100 d'ovidentia pour fonctionner (correction du bug de detection du supperieur hierarchique quand le gestionnaire depose l'absence)
350 4
		
351
		$idfai = bab_WFMakeInstance($this->getApprobationId(), get_class($this).':'.$this->id, $this->getAutoApprovalUserId(), $this->id_user);
352
		if (is_int($idfai))
353
		{
354
			$users = bab_WFGetWaitingApproversInstance($idfai, true);
355
			if (count($users) == 0)
356
			{
357
				$a = bab_WFGetApprobationInfos($this->getApprobationId());
358
				
359
				// l'instance n'a pas fonctionne
360
				bab_WFDeleteInstance($idfai);
361
				//TRANSLATORS: \n%1: request type (vacation request), \n%2: schema type (Staff schema), \n%3: schema name
362
				$this->addMovement(sprintf(absences_translate('The approval initialisation failed on the %s because no approvers were found on %s %s'), $this->getTitle(), mb_strtolower($a['type']), $a['name']));
363
				return $this->autoApproval();
364
			}
365
			
366
			if (isset($this->todelete) && $this->todelete) {
367
			    $this->addMovement(sprintf(absences_translate('The %s has been sent to deletion approval'), $this->getTitle()));
368
			} else {
369
			    $this->addMovement(sprintf(absences_translate('The %s has been sent for approval'), $this->getTitle()));
370
			}
371
			
372
		} else { // $idfai == TRUE : auto-approbation end schema
373
			return $this->autoApproval();
374
		}
375
		
376
		$this->idfai = $idfai;
377
		$this->status = '';
378
		$this->id_approver = 0;
379
		$this->appr_notified = 0;
380
		$this->comment2 = '';
381
		$this->save();
382
383
		return true;
384
	}
385
	
386
	
387
	protected function rejectApprovalWorkflow()
388
	{
389
	    bab_WFDeleteInstance($this->idfai);
390
	    $this->idfai = 0;
391
	    if ($this->todelete) {
392
	    
393
	        // demande de suppression refusee
394
	        $this->status = 'Y';
395
	    } else {
396
	        $this->status = 'N';
397
	    }
398
	    $this->onReject();
399
	}
400
	
401
	
402
	protected function acceptApprovalWorkflow()
403
	{
404
	    bab_WFDeleteInstance($this->idfai);
405
	    $this->idfai = 0;
406
	    $this->status = 'Y';
407
	    
408
	    if (!$this->firstconfirm) {
409
	        $this->firstconfirm = 1;
410
	    }
411
	    
412
	    if ($this->todelete) {
413
	        return $this->delete();
414
	    }
415
	    $this->onConfirm();
416
	}
417
	
418
	
419
	/**
420
	 * Approbator confirm
421
	 * move to next approbator or close instance and set the request accepted
422
	 * 
423
	 * 
424
	 * @param	bool	$status				Accept or reject approbation step
425
	 * @param	string	$approver_comment	
426
	 * 
427
	 */
428
	public function approbationNext($status, $approver_comment)
429
	{
430
		
431
		$res = bab_WFUpdateInstance($this->idfai, bab_getUserId(), $status);
432
		
433
		switch ($res) {
434
			case 0:
435
				$this->rejectApprovalWorkflow();
436
				break;
437
				
438
			case 1:
439
			    $this->acceptApprovalWorkflow();
440
				break;
441
442
				
443
			default: // -1
444
				$nfusers = bab_WFGetWaitingApproversInstance($this->idfai, true);
445
				if (count($nfusers) > 0) {
446
					$this->appr_notified = 0;
447
				} else {
448
					// no approvers found or approvers allready notified (all user must confirm without order)
449
					// do nothing
450
				}
451
				
452
			break;
453
		}
454
455
		$this->id_approver = bab_getUserId();
456
		$this->comment2 = $approver_comment;
457
		$this->save();
458
		
459
		
460
		$str_status = $status ? absences_translate('accepted') :absences_translate('rejected');
461
		
462
		// movement must be saved after status modification to save the new status in movement
463
		$this->addMovement(
464
		    sprintf(absences_translate('The %s has been %s by %s'), $this->getTitle(), $str_status, bab_getUserName(bab_getUserId())),
465
		    $approver_comment
466
		);
467
		
468
	}
469
	
470
	
471
	
472
	/**
473
	 * Auto confirm the request
474
	 */
475
	public function autoConfirm()
476
	{
477
		$this->addMovement(
478
			sprintf(absences_translate('The %s has been confirmed automatically because of the validation timout'), $this->getTitle())
479
		);
480
		
481
		
482
		if ($this->idfai > 0)
483
		{
484
			bab_WFDeleteInstance($this->idfai);
485
		}
486
		
487
		$this->idfai = 0;
488
		$this->status = 'Y';
489
		$this->onConfirm();
490
		
491
		$this->id_approver = 0;
492
		$this->save();
493
		$this->notifyOwner();
494
	}
495
	
496
	
497
	
498
	/**
499
	 * Test if the logged in user can modify the entry
500
	 * @return bool
501
	 */
502
	public function canModify()
503
	{
504
	    $modify_waiting = (bool) absences_getVacationOption('modify_waiting');
505
	    $modify_confirmed = (bool) absences_getVacationOption('modify_confirmed');
506
	    
507
		require_once dirname(__FILE__).'/agent.class.php';
508
		$agent = absences_Agent::getCurrentUser();
509
	
510
		$personal = ($agent->isInPersonnel() && $agent->id_user === $this->id_user);
511
	
512
		switch($this->status)
513
		{
514
			case 'Y': // confirmed
515
			    if ($this->isFixed()) {
516
			        return false;
517
			    }
518
519
			    if ($modify_confirmed) {
520
			        return true;
521
			    }
522
			    
523
			    return !$personal; // ce n'est pas ma propre demande, je suis gestionnaire delegue
524
525
				
526
	
527
			case 'N': // rejected
528
				return false;
529
	
530
			case 'P': // previsional
531
				return $personal;
532
	
533
			default: // waiting
534
				return ($personal && $modify_waiting);
535
		}
536
	}
537
	
538
	
539
	/**
540
	 * Test if the user can delete the entry
541
	 * @return bool
542
	 */
543
	public function canDelete()
544
	{
545
		return $this->canModify();
546
	}
547
	
548
	
549
	
550
	
551
	
552
	/**
553
	 * Process specific code when the request is rejected
554
	 *
555
	 */
556
	abstract protected function onReject();
557
	
558
	/**
559
	 * Process specific code when the request is confirmed
560
	 */
561
	abstract protected function onConfirm();
562
563
	/**
564
	 * request title
565
	 * @return string
566
	 */
567
	abstract public function getTitle();
568
569
570
	
571
	/**
572
	 * Get a list of field to display in the notifications for this request
573
	 * @return array
574
	 */
575
	abstract public function getNotifyFields();
576
	
577
	
578
	/**
579
	 * annee de la demande
580
	 * @return int
581
	 */
582
	abstract public function getYear();
583
	
584
	
585
	/**
586
	 * annee de la demande, pour l'archivage par annee, tiens compte du parametrage du mois de debut de periode.
587
	 * Utilise dans l'archivage par annee
588
	 * @return int
589
	 */
590
	abstract public function getArchiveYear();
591
	
592
	
593
	abstract public function archive();
594
	
595
	
596
	/**
597
	 * approver has been notified
598
	 */
599
	abstract public function setNotified();
600
	
601
	
602
	/**
603
	 * Url to edit request as a manager
604
	 */
605
	abstract public function getManagerEditUrl();
606
	
607
	/**
608
	 * Url to delete request as a manager
609
	 */
610
	abstract public function getManagerDeleteUrl();
611
	
612
	
613
	/**
614
	 * Notify request next approvers
615
	 */
616
	public function notifyApprovers()
617
	{
618
		$defer = (bool) absences_getVacationOption('approb_email_defer');
619
		if (!$defer)
620
		{
621
			require_once dirname(__FILE__).'/request.notify.php';
622
			absences_notifyRequestApprovers();
623
		}
624
	}
625
	
626
	
627
	/**
628
	 * Notify request owner about the approval or reject of the request
629
	 */
630
	public function notifyOwner()
631
	{
632
		$appliquant_email = (bool) absences_getVacationOption('appliquant_email');
633
		
634
		if (!$appliquant_email)
635
		{
636
			return;
637
		}
638
		
639
		require_once dirname(__FILE__).'/request.notify.php';
640
		
641
		
642
		
643
		switch($this->status)
644
		{
645
			case 'N':
646
				$subject = sprintf(absences_translate("Your %s has been refused"), $this->getTitle());
647
				break;
648
				
649
			case 'Y':
650
				$subject = sprintf(absences_translate("Your %s has been accepted"), $this->getTitle());
651
				break;
652
				
653
			default: // next approver
654
				return;
655
		}
656
		
657
		absences_notifyRequestAuthor(array($this), $subject, $subject, $this->id_user);
658
	}
659
660
661
	/**
662
	 * Get request card frame to display in requests list
663
	 * @param bool 			$display_username	Affiche le nom du demandeur dans la card frame ou non, utile pour les listes contenant plusieurs demandeurs possibles
664
	 * @param int			$rfrom				si les demandes de la liste sont modifiee par un gestionnaire ou gestionnaire delegue
665
 	 * @param int			$ide				id entite de l'organigramme >0 si gestionnaire delegue seulement
666
	 * @return Widget_Frame
667
	 */
668
	abstract public function getCardFrame($display_username, $rfrom, $ide);
669
670
	
671
	/**
672
	 * Get request frame to display in manager lists
673
	 * @return Widget_Frame
674
	 */
675
	abstract public function getManagerFrame();
676
	
677
678
	
679
	/**
680
	 * Delete request
681
	 */
682
	public function delete()
683
	{
684
		if ($this->idfai > 0)
685
		{
686 4
			bab_WFDeleteInstance($this->idfai);
687
		}
688 4
		
689
		global $babDB;
690 4
		$babDB->db_query("UPDATE absences_movement SET id_request='0' WHERE request_class=".$babDB->quote(get_class($this))." AND id_request=".$babDB->quote($this->id));
691 4
	}
692 4
	
693 4
	
694 4
	
695 4
	/**
696 4
	 * @return absences_MovementIterator
697 4
	 */
698
	public function getMovementIterator()
699
	{
700
		require_once dirname(__FILE__).'/movement.class.php';
701
	
702
		$I = new absences_MovementIterator;
703
		$I->setRequest($this);
704
	
705
		return $I;
706
	}
707
	
708
	
709
	
710
	/**
711
	 *
712
	 * @param string $message	Generated message
713
	 * @param string $comment	Author comment
714
	 */
715 View Code Duplication
	public function addMovement($message, $comment = '', $createdOn = null)
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...
716
	{
717
		require_once dirname(__FILE__).'/movement.class.php';
718
	
719
		$movement = new absences_Movement();
720
		$movement->message = $message;
721
		$movement->comment = $comment;
722
		$movement->setRequest($this);
723
		$movement->setAgent($this->getAgent());
724
		$movement->createdOn = $createdOn;
725
		$movement->save();
726
	}
727
	
728
	
729
	
730
	/**
731
	 * @return absences_Movement
732
	 */
733
	public function getLastMovement()
734
	{
735
		if (!isset($this->lastMovement))
736
		{
737
			$this->lastMovement = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type object<absences_Movement> of property $lastMovement.

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...
738
			$I = $this->getMovementIterator();
739
			foreach($I as $movement)
740
			{
741
				$this->lastMovement = $movement;
742
				break;
743
			}
744
		}
745
		
746
		return $this->lastMovement;
747
	}
748
	
749
	
750
	/**
751
	 * Faut-il allerter l'approbateur que le delai d'approbation est depasse
752
	 * @return bool
753
	 */
754
	public function approbAlert()
755
	{
756
		$last = $this->getLastMovement();
757
		
758
		if (!$last)
759
		{
760
			return false;
761
		}
762
			
763
		$s = (time() - bab_mktime($last->createdOn));
764
		$d = ($s / 86400);
765
		
766
		$approb_alert = (int) absences_getVacationOption('approb_alert');
767
		
768
		if ($approb_alert > $d)
769
		{
770
			return false;
771
		}
772
		
773
		return true;
774
	}
775
}
776
777
778
779
780
781
782
783
784
785
/**
786
 * Get the list of request from the users, requests are :
787
 * 
788
 * - vacations requests
789
 * - workday entiling recovery
790
 * - time saving account deposits
791
 *
792
 * Sorted by the most recent in first
793
 */
794
class absences_RequestIterator implements SeekableIterator, Countable 
795
{
796
797
	/**
798
	 * 
799
	 * @var array
800
	 */
801
	public $users;
802
	
803
	/**
804
	 * Filter by organization
805
	 * @var int
806
	 */
807
	public $organization;
808
	
809
	/**
810
	 * 
811
	 * @var array
812
	 */
813
	private $data;
814
	
815
	/**
816
	 * 
817
	 * @var int
818
	 */
819
	private $pos;
820
	
821
	
822
	/**
823
	 * Return only one entry per folder
824
	 * @var bool
825
	 */
826
	public $one_per_folder = false;
827
	
828
	
829
	/**
830
	 * Filtrer les demandes necessitant ou pas un email aux approbateurs
831
	 * @var int
832
	 */
833
	public $appr_notified;
834
	
835
	/**
836
	 * Filtrer les demandes avec unes instance d'approbation
837
	 * @var bool
838
	 */
839
	public $idfai_set;
840
	
841
	/**
842
	 * Filtrer les demandes par status
843
	 * @var string
844
	 */
845
	public $status;
846
	
847
	
848
	/**
849
	 * Search all request created before this date
850
	 * @var string
851
	 */
852
	public $createdOn;
853
	
854
	
855
	/**
856
	 * Search all request modified before this date
857
	 * @var string
858
	 */
859
	public $modifiedOn;
860
	
861
	/**
862
	 * Filtrer les demandes par annee
863
	 * @var int
864
	 */
865
	public $year;
866
	
867
	/**
868
	 * Filtrer les demandes par date de debut superieur
869
	 * @var string
870
	 */
871
	public $startFrom;
872
	
873
	/**
874
	 * Filtrer les demandes par date de debut inferieur
875
	 * @var string
876
	 */
877
	public $startTo;
878
	
879
	/**
880
	 * Filtrer les demandes par statut d'archivage
881
	 * @var int
882
	 */
883
	public $archived = 0;
884
	
885
	
886
	public function __construct($users = null)
887
	{
888
		$this->users = $users;
889
	}
890
	
891
	/**
892
	 * Get all requests in one array (not sorted)
893
	 * @return array
894
	 */
895
	public function getArray()
896
	{
897
		require_once dirname(__FILE__).'/entry.class.php';
898
		require_once dirname(__FILE__).'/workperiod_recover_request.class.php';
899
		require_once dirname(__FILE__).'/cet_deposit_request.class.php';
900
		
901
		
902
		$return = array();
903
		
904
		$entries = new absences_EntryIterator;
905
		$entries->users = $this->users;
906
		$entries->organization = $this->organization;
907
		$entries->appr_notified = $this->appr_notified;
908
		$entries->idfai_set = $this->idfai_set;
909
		$entries->status = $this->status;
910
		$entries->createdOn = $this->createdOn;
911
		$entries->modifiedOn = $this->modifiedOn;
912
		$entries->year = $this->year;
913
		$entries->archived = $this->archived;
914
		$entries->startFrom = $this->startFrom;
915
		$entries->startTo = $this->startTo;
916
		
917
		$folders = array();
918
		
919
		foreach($entries as $entry)
920
		{
921
			if ($this->one_per_folder && isset($entry->folder) && $entry->folder > 0)
922
			{
923
				if (isset($folders[$entry->folder]))
924
				{
925
					continue;
926
				}
927
				
928
				$folders[$entry->folder] = 1;
929
			}
930
			
931
			$return[] = $entry;
932
		}
933
		
934
		
935
		$cetdeposits = new absences_CetDepositRequestIterator;
936
		$cetdeposits->users = $this->users;
937
		$cetdeposits->organization = $this->organization;
938
		$cetdeposits->appr_notified = $this->appr_notified;
939
		$cetdeposits->idfai_set = $this->idfai_set;
940
		$cetdeposits->status = $this->status;
941
		$cetdeposits->createdOn = $this->createdOn;
942
		$cetdeposits->modifiedOn = $this->modifiedOn;
943
		$cetdeposits->year = $this->year;
944
		$cetdeposits->archived = $this->archived;
945
		$cetdeposits->startFrom = $this->startFrom;
946
		$cetdeposits->startTo = $this->startTo;
947
		
948
		foreach($cetdeposits as $deposit)
949
		{
950
			$return[] = $deposit;
951
		}
952
		
953
		$workperiods = new absences_WorkperiodRecoverRequestIterator;
954
		$workperiods->users = $this->users;
955
		$workperiods->organization = $this->organization;
956
		$workperiods->appr_notified = $this->appr_notified;
957
		$workperiods->idfai_set = $this->idfai_set;
958
		$workperiods->status = $this->status;
959
		$workperiods->createdOn = $this->createdOn;
960
		$workperiods->modifiedOn = $this->modifiedOn;
961
		$workperiods->year = $this->year;
962
		$workperiods->archived = $this->archived;
963
		$workperiods->startFrom = $this->startFrom;
0 ignored issues
show
Documentation Bug introduced by
The property $startFrom was declared of type integer, but $this->startFrom is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
964
		$workperiods->startTo = $this->startTo;
0 ignored issues
show
Documentation Bug introduced by
The property $startTo was declared of type integer, but $this->startTo is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
965
		
966
		foreach($workperiods as $recovery)
967
		{
968
			$return[] = $recovery;
969
		}
970
		
971
		return $return;
972
	}
973
	
974
	/**
975
	 * Get all requests in one array
976
	 * @return array
977
	 */
978
	public function getSortedArray()
979
	{
980
		$arr = $this->getArray();
981
		bab_Sort::sortObjects($arr, 'createdOn', bab_Sort::CASE_INSENSITIVE);
982
		
983
		$arr = array_reverse($arr);
984
		
985
		return array_values($arr);
986
	}
987
	
988
	
989
	/**
990
	 * 
991
	 * @return multitype:absences_Request
0 ignored issues
show
Documentation introduced by
The doc-type multitype:absences_Request could not be parsed: Unknown type name "multitype:absences_Request" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
992
	 */
993
	private function getData()
994
	{
995
		if (!isset($this->data))
996
		{
997
			$this->data = $this->getSortedArray();
998
		}
999
		
1000
		return $this->data;
1001
	}
1002
	
1003
	
1004
	public function seek($position) {
1005
		$this->pos = $position;
1006
	}
1007
	
1008
	/**
1009
	 * (non-PHPdoc)
1010
	 * @see SeekableIterator::current()
1011
	 * 
1012
	 * @return absences_Request
1013
	 */
1014
	public function current() {
1015
		$data = $this->getData();
1016
		return $data[$this->pos];
1017
	}
1018
	
1019
	public function next() {
1020
		$this->pos++;
1021
	}
1022
	
1023
	public function key() {
1024
		return $this->pos;
1025
	}
1026
	
1027
	public function valid() {
1028
		$data = $this->getData();
1029
		return isset($data[$this->pos]);
1030
	}
1031
	
1032
	public function rewind() {
1033
		$this->pos = 0;
1034
	}
1035
	
1036
	/**
1037
	 * (non-PHPdoc)
1038
	 * @see Countable::count()
1039
	 * 
1040
	 * @return int
1041
	 */
1042
	public function count() {
1043
		return count($this->getData());
1044
	}
1045
}
1046
1047
1048
1049
1050