1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* BEdita, API-first content management framework |
4
|
|
|
* Copyright 2019 ChannelWeb Srl, Chialab Srl |
5
|
|
|
* |
6
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
8
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
12
|
|
|
*/ |
13
|
|
|
namespace BEdita\WebTools\View\Helper; |
14
|
|
|
|
15
|
|
|
use BEdita\WebTools\ApiClientProvider; |
16
|
|
|
use Cake\Log\LogTrait; |
17
|
|
|
use Cake\View\Helper; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Helper to obtain thumbnail url |
21
|
|
|
*/ |
22
|
|
|
class ThumbHelper extends Helper |
23
|
|
|
{ |
24
|
|
|
use LogTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int Thumb not available |
28
|
|
|
*/ |
29
|
|
|
public const NOT_AVAILABLE = -10; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var int Thumb not ready |
33
|
|
|
*/ |
34
|
|
|
public const NOT_READY = -20; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int Thumb not acceptable |
38
|
|
|
*/ |
39
|
|
|
public const NOT_ACCEPTABLE = -30; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var int Thumb has no url |
43
|
|
|
*/ |
44
|
|
|
public const NO_URL = -40; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var int Thumb is OK |
48
|
|
|
*/ |
49
|
|
|
public const OK = 1; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Verify status of image thumb. |
53
|
|
|
* Return int representing status. |
54
|
|
|
* Possible values: |
55
|
|
|
* |
56
|
|
|
* NOT_AVAILABLE: something went wrong during api call |
57
|
|
|
* NOT_READY: thumb is available, but not ready |
58
|
|
|
* NOT_ACCEPTABLE: image is not acceptable, api won't create thumb |
59
|
|
|
* NO_URL: url not present in api response |
60
|
|
|
* OK: thumb available, ready and with a proper url |
61
|
|
|
* |
62
|
|
|
* @param int|string $imageId The image ID |
63
|
|
|
* @param array|null $options The thumbs options |
64
|
|
|
* @param string|null $url The thumb url to populate when static::OK |
65
|
|
|
* @return int|null |
66
|
|
|
*/ |
67
|
|
|
public function status($imageId, ?array $options = ['preset' => 'default'], &$url = ''): ?int |
68
|
|
|
{ |
69
|
|
|
if (empty($imageId) && empty($options['ids'])) { |
70
|
|
|
return static::NOT_ACCEPTABLE; |
71
|
|
|
} |
72
|
|
|
try { |
73
|
|
|
$apiClient = ApiClientProvider::getApiClient(); |
74
|
|
|
$response = $apiClient->thumbs($imageId, $options); |
75
|
|
|
if (empty($response['meta']['thumbnails'][0])) { |
76
|
|
|
return static::NOT_AVAILABLE; |
77
|
|
|
} |
78
|
|
|
$thumb = $response['meta']['thumbnails'][0]; |
79
|
|
|
// check thumb is ready |
80
|
|
|
if (!$this->isReady($thumb)) { |
81
|
|
|
return static::NOT_READY; |
82
|
|
|
} |
83
|
|
|
// check thumb is acceptable |
84
|
|
|
if (!$this->isAcceptable($thumb)) { |
85
|
|
|
return static::NOT_ACCEPTABLE; |
86
|
|
|
} |
87
|
|
|
// check thumb has url |
88
|
|
|
if (!$this->hasUrl($thumb)) { |
89
|
|
|
return static::NO_URL; |
90
|
|
|
} |
91
|
|
|
$url = $thumb['url']; |
92
|
|
|
} catch (\Exception $e) { |
93
|
|
|
$this->log($e, 'error'); |
94
|
|
|
|
95
|
|
|
return static::NOT_AVAILABLE; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return static::OK; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Obtain thumbnail using API thumbs. |
103
|
|
|
* |
104
|
|
|
* @param int $imageId The image ID. |
105
|
|
|
* @param array|null $options The thumbs options. |
106
|
|
|
* @return string|int The url if available, the status code otherwise (see Thumb constants). |
107
|
|
|
*/ |
108
|
|
|
public function url($imageId, $options) |
109
|
|
|
{ |
110
|
|
|
$url = null; |
111
|
|
|
$status = $this->status($imageId, $options, $url); |
112
|
|
|
if ($status === static::OK) { |
113
|
|
|
return $url; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $status; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Verify if thumb is acceptable |
121
|
|
|
* |
122
|
|
|
* @param array $thumb The thumbnail data |
123
|
|
|
* @return bool the acceptable flag |
124
|
|
|
*/ |
125
|
|
|
private function isAcceptable($thumb = []): bool |
126
|
|
|
{ |
127
|
|
|
if (isset($thumb['acceptable']) && $thumb['acceptable'] === false) { |
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Verify if thumb is ready |
136
|
|
|
* |
137
|
|
|
* @param array $thumb The thumbnail data |
138
|
|
|
* @return bool the ready flag |
139
|
|
|
*/ |
140
|
|
|
private function isReady($thumb = []): bool |
141
|
|
|
{ |
142
|
|
|
if (!empty($thumb['ready']) && $thumb['ready'] === true) { |
143
|
|
|
return true; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return false; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Verify if thumb has url |
151
|
|
|
* |
152
|
|
|
* @param array $thumb The thumbnail data |
153
|
|
|
* @return bool the url availability |
154
|
|
|
*/ |
155
|
|
|
private function hasUrl($thumb = []): bool |
156
|
|
|
{ |
157
|
|
|
if (!empty($thumb['url'])) { |
158
|
|
|
return true; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return false; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|