Passed
Push — master ( 7fae15...7a46b9 )
by Andreas
18:45
created

org_openpsa_slideshow_image_dba   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Test Coverage

Coverage 14.43%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 90
dl 0
loc 151
ccs 14
cts 97
cp 0.1443
rs 10
c 1
b 0
f 0
wmc 24

9 Methods

Rating   Name   Duplication   Size   Complexity  
A _on_updated() 0 3 1
A _on_created() 0 3 1
A _on_deleted() 0 3 1
A create_folder_thumbnail() 0 25 4
A get_folder_thumbnail() 0 17 3
A generate_image() 0 27 5
A _check_folder_thumbnail() 0 12 3
A load_attachment() 0 7 2
A get_imagedata() 0 30 4
1
<?php
2
/**
3
 * @package org.openpsa.slideshow
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
/**
10
 * Image DBA class
11
 *
12
 * @property integer $topic
13
 * @property string $title
14
 * @property string $description
15
 * @property integer $position
16
 * @property integer $attachment
17
 * @property integer $thumbnail
18
 * @property integer $image
19
 * @package org.openpsa.slideshow
20
 */
21
class org_openpsa_slideshow_image_dba extends midcom_core_dbaobject
22
{
23
    const FOLDER_THUMBNAIL = 'folder_thumbnail';
24
25
    public $__midcom_class_name__ = __CLASS__;
26
    public $__mgdschema_class_name__ = 'org_openpsa_slideshow_image';
27
28 2
    public function _on_created()
29
    {
30 2
        $this->_check_folder_thumbnail();
31 2
    }
32
33 1
    public function _on_updated()
34
    {
35 1
        $this->_check_folder_thumbnail();
36 1
    }
37
38 2
    public function _on_deleted()
39
    {
40 2
        $this->_check_folder_thumbnail();
41 2
    }
42
43 2
    private function _check_folder_thumbnail()
44
    {
45 2
        if ($this->position > 0) {
46
            return;
47
        }
48
49
        try {
50 2
            $folder = midcom_db_topic::get_cached($this->topic);
51
        } catch (midcom_error $e) {
52
            $e->log();
53
        }
54 2
        $folder->delete_attachment(self::FOLDER_THUMBNAIL);
55 2
    }
56
57
    public function load_attachment(string $type) : ?midcom_db_attachment
58
    {
59
        try {
60
            return new midcom_db_attachment($this->$type);
61
        } catch (midcom_error $e) {
62
            $e->log();
63
            return null;
64
        }
65
    }
66
67
    public function generate_image(string $type, string $filter_chain) : bool
68
    {
69
        $original = $this->load_attachment('attachment');
70
        if (!$original) {
71
            return false;
72
        }
73
        $is_new = false;
74
        $derived = $this->load_attachment($type);
75
        if (!$derived) {
76
            $is_new = true;
77
            $derived = new midcom_db_attachment;
78
            $derived->parentguid = $original->parentguid;
79
            $derived->title = $original->title;
80
            $derived->mimetype = $original->mimetype;
81
            $derived->name = $type . '_' . $original->name;
82
        }
83
84
        $imagefilter = new midcom_helper_imagefilter($original);
85
        $imagefilter->process_chain($filter_chain);
86
        if ($is_new) {
87
            if (!$derived->create()) {
88
                throw new midcom_error('Failed to create derived image: ' . midcom_connection::get_error_string());
89
            }
90
            $this->$type = $derived->id;
91
            $this->update();
92
        }
93
        return $imagefilter->write($derived);
94
    }
95
96
    public static function get_folder_thumbnail(midcom_db_topic $folder) : ?midcom_db_attachment
97
    {
98
        $thumbnail = $folder->get_attachment(self::FOLDER_THUMBNAIL);
99
        if (empty($thumbnail)) {
100
            $qb = self::new_query_builder();
101
            $qb->add_constraint('topic', '=', $folder->id);
102
            $qb->add_order('position');
103
            $qb->set_limit(1);
104
            $results = $qb->execute();
105
            if (empty($results)) {
106
                return null;
107
            }
108
            midcom::get()->auth->request_sudo('org.openpsa.slideshow');
109
            $thumbnail = $results[0]->create_folder_thumbnail();
110
            midcom::get()->auth->drop_sudo();
111
        }
112
        return $thumbnail;
113
    }
114
115
    public function create_folder_thumbnail() : ?midcom_db_attachment
116
    {
117
        $original = $this->load_attachment('attachment');
118
        if (!$original) {
119
            return null;
120
        }
121
        $folder = midcom_db_topic::get_cached($this->topic);
122
        $thumbnail = new midcom_db_attachment;
123
        $thumbnail->parentguid = $folder->guid;
124
        $thumbnail->title = $original->title;
125
        $thumbnail->mimetype = $original->mimetype;
126
        $thumbnail->name = self::FOLDER_THUMBNAIL;
127
128
        $imagefilter = new midcom_helper_imagefilter($original);
129
        $config = midcom_baseclasses_components_configuration::get('org.openpsa.slideshow', 'config');
130
131
        $filter_chain = $config->get('folder_thumbnail_filter');
132
        $imagefilter->process_chain($filter_chain);
133
        if (!$thumbnail->create()) {
134
            throw new midcom_error('Failed to create folder thumbnail: ' . midcom_connection::get_error_string());
135
        }
136
        if (!$imagefilter->write($thumbnail)) {
137
            throw new midcom_error('Failed to write folder thumbnail: ' . midcom_connection::get_error_string());
138
        }
139
        return $thumbnail;
140
    }
141
142
    public static function get_imagedata(array $images) : array
143
    {
144
        $data = [];
145
        if (empty($images)) {
146
            return $data;
147
        }
148
        $ids = array_merge(array_column($images, 'attachment'), array_column($images, 'image'), array_column($images, 'thumbnail'));
149
150
        $mc = midcom_db_attachment::new_collector();
151
        $mc->add_constraint('id', 'IN', $ids);
152
        $rows = $mc->get_rows(['id', 'name', 'guid'], 'id');
153
154
        foreach ($images as $image) {
155
            if (!isset($rows[$image->attachment], $rows[$image->image], $rows[$image->thumbnail])) {
156
                continue;
157
            }
158
159
            $orig_data = $rows[$image->attachment];
160
            $image_data = $rows[$image->image];
161
            $thumb_data = $rows[$image->thumbnail];
162
            $data[] = [
163
                'big' => midcom_db_attachment::get_url($orig_data['guid'], $orig_data['name']),
164
                'image' => midcom_db_attachment::get_url($image_data['guid'], $image_data['name']),
165
                'thumb' => midcom_db_attachment::get_url($thumb_data['guid'], $thumb_data['name']),
166
                'title' => (string) $image->title,
167
                'description' => (string) $image->description
168
            ];
169
        }
170
171
        return $data;
172
    }
173
}
174