-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkdaypause.php
More file actions
86 lines (74 loc) · 2.13 KB
/
workdaypause.php
File metadata and controls
86 lines (74 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace Korus\Test;
use Exception;
use RedBeanPHP\R as R;
use RedBeanPHP\RedException\SQL;
class WorkDayPause extends WorkDay
{
const
_type = 'workdaypause';
protected
$workday,
$bean;
/**
* WorkDayPause constructor.
*
* @param WorkDay $workday
* @param int|bool $id
* @throws Exception
*/
function __construct($workday, $id = false)
{
if ($workday->bean && !$workday->bean->isEmpty()) {
$this->workday = $workday;
if ($id)
$this->bean = $this->workday->bean->ownWorkdaypauseList[$id];
else
$this->bean = reset($this->workday->bean->withCondition(
'(`date_start` > ? AND `date_stop` IS NULL) ORDER BY `date_start` DESC LIMIT 1',
[R::isoDateTime(strtotime($this->workday->bean->date_start))]
)->ownWorkdaypauseList);
} else
throw new Exception('Error. Workday is missing.');
}
/**
* Workday pause start.
*
* @param bool $t
* @return WorkDayPause
* @throws SQL
* @throws Exception
*/
public function start($t = false)
{
if ($t === false)
$t = time();
if (!$this->bean || $this->bean->isEmpty()) {
$this->bean = R::dispense(self::_type);
$this->bean->date_start = R::isoDateTime($t);
$this->workday->bean->ownWorkdaypauseList[] = $this->bean;
$this->workday->save();
} else
throw new Exception('Error. The workday already paused.');
return $this;
}
/**
* Workday pause stop.
*
* @param bool $t
* @return WorkDayPause
* @throws SQL
* @throws Exception
*/
public function stop($t = false)
{
if ($t === false)
$t = time();
if (!$this->workday->bean->isEmpty() && $this->bean && !$this->bean->isEmpty()) {
$this->bean->date_stop = R::isoDateTime($t);
$this->workday->save();
} else
throw new Exception('Error. The workday is not paused.');
return $this;
}
}