-
Notifications
You must be signed in to change notification settings - Fork 1
/
start.php
185 lines (154 loc) · 4.09 KB
/
start.php
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
/**
* Images
*
* @author Ismayil Khayredinov <info@hypejunction.com>
* @copyright Copyright (c) 2015, Ismayil Khayredinov
*/
require_once __DIR__ . '/autoloader.php';
elgg_register_event_handler('init', 'system', 'images_init');
/**
* Initialize the plugin
* @return void
*/
function images_init() {
elgg_register_plugin_hook_handler('entity:icon:url', 'all', 'images_entity_icon_url');
elgg_register_event_handler('create', 'object', 'images_update_event_handler');
elgg_register_event_handler('update:after', 'object', 'images_update_event_handler');
elgg_register_event_handler('update:after', 'all', 'images_update_avatar_access');
elgg_register_event_handler('delete', 'object', 'images_delete_handler', 999);
elgg_register_plugin_hook_handler('thumb:sizes', 'object', 'images_file_thumb_sizes');
}
/**
* Image icon URL handler
*
* @param string $hook "entity:icon:url"
* @param string $type "object"
* @param string $return URL
* @param array $params Hook params
* @return array
*/
function images_entity_icon_url($hook, $type, $return, $params) {
$size = elgg_extract('size', $params, 'medium');
$entity = elgg_extract('entity', $params);
if (!$entity instanceof ElggEntity || !$entity->icontime) {
return;
}
if ($entity->getSubtype() == 'file' && !elgg_get_plugin_setting('override_file_thumb_dimensions', 'images')) {
return;
}
if (elgg_images_is_image($entity)) {
$thumb = elgg_images_get_thumb($entity, $size);
} else {
$avatar = elgg_images_get_avatar($entity);
if ($avatar) {
$thumb = elgg_images_get_thumb($avatar, $size);
}
}
if (!$thumb instanceof ElggFile) {
return;
}
$url = elgg_get_inline_url($thumb, true);
if (!$url) {
return;
}
return $url;
}
/**
* Post-save handler
*
* @param string $event "create"|"update:after"
* @param string $type "object"
* @param ElggEntity $entity Entity
* @return void
*/
function images_update_event_handler($event, $type, $entity) {
if (!elgg_images_is_image($entity)) {
return;
}
if ($entity->getSubtype() == 'file' && !elgg_get_plugin_setting('override_file_thumb_dimensions', 'images')) {
return;
}
if ($entity->icon_owner_guid && $entity->icon_owner_guid != $entity->owner_guid) {
// Owner has changed
elgg_images_clear_thumbs($entity);
}
$mtime = filemtime($entity->getFilenameOnFilestore());
if (!$entity->icontime || $entity->icontime != $mtime) {
if (elgg_images_create_thumbs($entity)) {
$entity->icontime = $mtime;
} else {
return false;
}
}
}
/**
* Remove image thumbnails on entity delete
*
* @param string $event "delete"
* @param string $type "object"
* @param ElggEntity $entity Entity
* @return void
*/
function images_delete_handler($event, $type, $entity) {
return elgg_images_clear_thumbs($entity);
}
/**
* Update avatar access id when entity is saved
*
* @param string $event "update:after"
* @param string $type "all"
* @param ElggEntity $entity Entity
*/
function images_update_avatar_access($event, $type, $entity) {
if (!$entity instanceof ElggEntity) {
return;
}
$access_id = (int) $entity->access_id;
$avatars = images()->getAvatars($entity);
if (!$avatars) {
return;
}
foreach ($avatars as $avatar) {
$avatar->access_id = $access_id;
$avatar->save();
}
}
/**
* Configure file object thumb sizes
*
* @param string $hook "thumb:sizes"
* @param string $type "object"
* @param array $return Thumb sizes
* @param array $params Hook params
* @return array
*/
function images_file_thumb_sizes($hook, $type, $return, $params) {
$entity = elgg_extract('entity', $params);
if (!$entity instanceof ElggFile || $entity->getSubtype() !== 'file') {
return;
}
if (elgg_get_plugin_setting('override_file_thumb_dimensions', 'images')) {
return;
}
return array(
'small' => array(
'w' => 60,
'h' => 60,
'square' => true,
'metadata_name' => 'thumbnail',
),
'medium' => array(
'w' => 153,
'h' => 153,
'square' => true,
'metadata_name' => 'smallthumb',
),
'large' => array(
'w' => 600,
'h' => 600,
'square' => false,
'metadata_name' => 'largethumb',
)
);
}