1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace midcom\datamanager\storage; |
7
|
|
|
|
8
|
|
|
use midcom\datamanager\helper\imagefilter; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Images storage |
12
|
|
|
* |
13
|
|
|
* Controls a list of images |
14
|
|
|
*/ |
15
|
|
|
class images extends blobs implements recreateable |
16
|
|
|
{ |
17
|
|
|
public function recreate() : bool |
18
|
|
|
{ |
19
|
|
|
$this->map = []; |
20
|
|
|
$map = $this->load(); |
21
|
|
|
|
22
|
|
|
foreach ($map as $identifier => &$images) { |
23
|
|
|
$filter = new imagefilter($this->config['type_config']); |
24
|
|
|
$images = $filter->process($images['main'], $images); |
25
|
|
|
|
26
|
|
|
foreach ($images as $name => $image) { |
27
|
|
|
$this->map[$identifier . $name] = $image; |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
return $this->save_image_map($map) && $this->save_attachment_list(); |
31
|
|
|
} |
32
|
|
|
|
33
|
4 |
|
public function load() |
34
|
|
|
{ |
35
|
4 |
|
$results = parent::load(); |
36
|
4 |
|
$grouped = []; |
37
|
|
|
|
38
|
4 |
|
$identifiers = []; |
39
|
4 |
|
if ($raw_list = $this->object->get_parameter('midcom.helper.datamanager2.type.images', "attachment_map_{$this->config['name']}")) { |
40
|
2 |
|
$identifiers = explode(',', $raw_list); |
41
|
|
|
} else { |
42
|
|
|
// Reconstruct from blobs data |
43
|
2 |
|
foreach (array_keys($results) as $identifier) { |
44
|
|
|
$identifiers[] = $identifier . ':' . substr($identifier, 0, 32) . ':main'; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
4 |
|
$map = []; |
49
|
4 |
|
foreach ($identifiers as $item) { |
50
|
2 |
|
[$identifier, $images_identifier, $images_name] = explode(':', $item); |
51
|
2 |
|
$map[$identifier] = [$images_identifier, $images_name]; |
52
|
|
|
} |
53
|
|
|
// we iterate over results since that takes sorting into account |
54
|
4 |
|
foreach ($results as $identifier => $image) { |
55
|
2 |
|
[$images_identifier, $images_name] = $map[$identifier]; |
56
|
2 |
|
if (!array_key_exists($images_identifier, $grouped)) { |
57
|
2 |
|
$grouped[$images_identifier] = []; |
58
|
|
|
} |
59
|
2 |
|
$grouped[$images_identifier][$images_name] = $image; |
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
|
return $grouped; |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
public function save() |
66
|
|
|
{ |
67
|
1 |
|
$this->map = []; |
68
|
1 |
|
$map = $this->load(); |
69
|
1 |
|
foreach ($this->value as $identifier => $images) { |
70
|
1 |
|
if (!empty($images['file'])) { |
71
|
1 |
|
if (is_numeric($identifier)) { |
72
|
1 |
|
$identifier = md5(time() . $images['file']->name . $images['file']->location); |
73
|
|
|
} |
74
|
1 |
|
$images['file']->parentguid = $this->object->guid; |
75
|
1 |
|
$existing = array_key_exists($identifier, $map) ? $map[$identifier] : []; |
76
|
1 |
|
$filter = new imagefilter($this->config['type_config']); |
77
|
1 |
|
$map[$identifier] = $filter->process($images['file'], $existing); |
78
|
|
|
} |
79
|
1 |
|
foreach ($map[$identifier] as $name => $image) { |
80
|
1 |
|
$this->map[$identifier . $name] = $image; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
if (!empty($images['main'])) { |
84
|
1 |
|
if (array_key_exists('description', $images)) { |
85
|
|
|
$images['main']->set_parameter('midcom.helper.datamanager2.type.blobs', 'description', $images['description']); |
86
|
|
|
} |
87
|
1 |
|
if (array_key_exists('title', $images) && $images['main']->title != $images['title']) { |
88
|
|
|
$images['main']->title = $images['title']; |
89
|
|
|
$images['main']->update(); |
90
|
|
|
} |
91
|
1 |
|
if (!empty($this->config['widget_config']['sortable'])) { |
92
|
|
|
$images['main']->update(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
$this->save_image_map($map); |
98
|
1 |
|
$this->save_attachment_list(); |
99
|
1 |
|
} |
100
|
|
|
|
101
|
1 |
|
private function save_image_map(array $map) : bool |
102
|
|
|
{ |
103
|
1 |
|
$list = []; |
104
|
|
|
|
105
|
1 |
|
foreach ($map as $identifier => $derived) { |
106
|
1 |
|
foreach (array_keys($derived) as $name) { |
107
|
1 |
|
$list[] = $identifier . $name . ':' . $identifier . ':' . $name; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
return $this->object->set_parameter('midcom.helper.datamanager2.type.images', "attachment_map_{$this->config['name']}", implode(',', $list)); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|