|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package midcom.grid |
|
4
|
|
|
* @author CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
5
|
|
|
* @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
6
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace midcom\grid; |
|
10
|
|
|
|
|
11
|
|
|
use Symfony\Component\HttpFoundation\InputBag; |
|
12
|
|
|
use midcom_error; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Helper class for editable jqgrids |
|
17
|
|
|
* |
|
18
|
|
|
* @package midcom.grid |
|
19
|
|
|
*/ |
|
20
|
|
|
class editor |
|
21
|
|
|
{ |
|
22
|
|
|
private string $operation; |
|
23
|
|
|
|
|
24
|
|
|
private $id; |
|
25
|
|
|
|
|
26
|
|
|
private array $data = []; |
|
27
|
|
|
|
|
28
|
1 |
|
public function __construct(InputBag $post, array $required_fields) |
|
29
|
|
|
{ |
|
30
|
1 |
|
if (!$post->has('id') || !$post->has('oper')) { |
|
31
|
|
|
throw new midcom_error('Incomplete POST data'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
1 |
|
$this->id = $post->get('id'); |
|
35
|
1 |
|
$this->operation = $post->get('oper'); |
|
36
|
|
|
|
|
37
|
1 |
|
if (!in_array($this->operation, ['edit', 'del'])) { |
|
38
|
|
|
throw new midcom_error('Invalid operation "' . $this->operation . '"'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
foreach ($required_fields as $field) { |
|
42
|
1 |
|
if (!$post->has($field)) { |
|
43
|
|
|
throw new midcom_error('Incomplete POST data'); |
|
44
|
|
|
} |
|
45
|
1 |
|
$this->data[$field] = $post->get($field); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
public function get_data() : array |
|
50
|
|
|
{ |
|
51
|
1 |
|
return $this->data; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
public function is_delete() : bool |
|
55
|
|
|
{ |
|
56
|
1 |
|
return $this->operation == 'del'; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
public function get_id() : ?int |
|
60
|
|
|
{ |
|
61
|
1 |
|
return str_starts_with($this->id, 'new_') ? null : (int) $this->id; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
public function get_response(array $data) : JsonResponse |
|
65
|
|
|
{ |
|
66
|
1 |
|
$data['oldid'] = $this->id; |
|
67
|
1 |
|
return new JsonResponse($data); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|