|
1
|
|
|
<?php |
|
2
|
|
|
/* Copyright (C) 2017 Michael Giesler |
|
3
|
|
|
* |
|
4
|
|
|
* This file is part of Dembelo. |
|
5
|
|
|
* |
|
6
|
|
|
* Dembelo is free software: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU Affero General Public License as published by |
|
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* Dembelo is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU Affero General Public License 3 for more details. |
|
15
|
|
|
* |
|
16
|
|
|
* You should have received a copy of the GNU Affero General Public License 3 |
|
17
|
|
|
* along with Dembelo. If not, see <http://www.gnu.org/licenses/>. |
|
18
|
|
|
*/ |
|
19
|
|
|
declare(strict_types = 1); |
|
20
|
|
|
|
|
21
|
|
|
namespace DembeloMain\Service; |
|
22
|
|
|
|
|
23
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class ReadpathUndoService |
|
27
|
|
|
* @package DembeloMain\Service |
|
28
|
|
|
*/ |
|
29
|
|
|
class ReadpathUndoService |
|
30
|
|
|
{ |
|
31
|
|
|
private const SESSION_KEY = 'readpathundo'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
private $data = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var Session |
|
40
|
|
|
*/ |
|
41
|
|
|
private $session; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* ReadpathUndoService constructor. |
|
45
|
|
|
* @param Session $session |
|
46
|
|
|
*/ |
|
47
|
14 |
|
public function __construct(Session $session) |
|
48
|
|
|
{ |
|
49
|
14 |
|
$this->session = $session; |
|
50
|
14 |
|
if ($session->has(self::SESSION_KEY)) { |
|
51
|
1 |
|
$this->data = $session->get(self::SESSION_KEY); |
|
52
|
|
|
} else { |
|
53
|
14 |
|
$this->data = [ |
|
54
|
|
|
'past' => [], |
|
55
|
|
|
'present' => null, |
|
56
|
|
|
'future' => [], |
|
57
|
|
|
'undoAvoid' => null, |
|
58
|
|
|
]; |
|
59
|
|
|
} |
|
60
|
14 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $textnodeId |
|
64
|
|
|
*/ |
|
65
|
10 |
|
public function add(string $textnodeId): void |
|
66
|
|
|
{ |
|
67
|
10 |
|
if ($textnodeId === $this->data['present']) { |
|
68
|
2 |
|
return; |
|
69
|
|
|
} |
|
70
|
10 |
|
if ($this->data['undoAvoid'] === $textnodeId) { |
|
71
|
|
|
$this->data['undoAvoid'] = null; |
|
72
|
|
|
$this->persist(); |
|
73
|
|
|
|
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
10 |
|
if ($this->data['present'] !== null) { |
|
77
|
7 |
|
$this->data['past'][] = $this->data['present']; |
|
78
|
|
|
} |
|
79
|
10 |
|
$this->data['present'] = $textnodeId; |
|
80
|
10 |
|
$this->data['future'] = []; |
|
81
|
10 |
|
$this->persist(); |
|
82
|
10 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return null|string |
|
86
|
|
|
*/ |
|
87
|
10 |
|
public function getCurrentItem(): ?string |
|
88
|
|
|
{ |
|
89
|
10 |
|
return $this->data['present']; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
8 |
|
public function undo(): bool |
|
96
|
|
|
{ |
|
97
|
8 |
|
if (empty($this->data['past'])) { |
|
98
|
2 |
|
return false; |
|
99
|
|
|
} |
|
100
|
6 |
|
$this->data['future'][] = $this->data['present']; |
|
101
|
6 |
|
$this->data['present'] = array_pop($this->data['past']); |
|
102
|
6 |
|
$this->data['undoAvoid'] = $this->data['present']; |
|
103
|
6 |
|
$this->persist(); |
|
104
|
|
|
|
|
105
|
6 |
|
return true; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return bool |
|
110
|
|
|
*/ |
|
111
|
4 |
|
public function redo(): bool |
|
112
|
|
|
{ |
|
113
|
4 |
|
if (empty($this->data['future'])) { |
|
114
|
2 |
|
return false; |
|
115
|
|
|
} |
|
116
|
2 |
|
$this->data['past'][] = $this->data['present']; |
|
117
|
2 |
|
$this->data['present'] = array_pop($this->data['future']); |
|
118
|
2 |
|
$this->persist(); |
|
119
|
|
|
|
|
120
|
2 |
|
return true; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
10 |
|
private function persist(): void |
|
124
|
|
|
{ |
|
125
|
10 |
|
$this->session->set(self::SESSION_KEY, $this->data); |
|
126
|
10 |
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|