ImageStorage.php
3.28 KB
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
<?php
namespace Nextend\Framework\Image;
use Nextend\Framework\Database\AbstractPlatformConnectorTable;
use Nextend\Framework\Database\Database;
use Nextend\Framework\Misc\Base64;
class ImageStorage {
/**
* @var AbstractPlatformConnectorTable
*/
private $tableImageStorage;
public static $emptyImage = array(
'desktop-retina' => array(
'image' => ''
),
'tablet' => array(
'image' => ''
),
'mobile' => array(
'image' => ''
)
);
public function __construct() {
$this->tableImageStorage = Database::getTable("nextend2_image_storage");
}
public function getById($id) {
return $this->tableImageStorage->findByAttributes(array(
"id" => $id
));
}
public function getByImage($image) {
static $cache = array();
if (!isset($cache[$image])) {
$cache[$image] = $this->tableImageStorage->findByAttributes(array(
"hash" => md5($image)
));
}
return $cache[$image];
}
public function setById($id, $value) {
if (is_array($value)) {
$value = Base64::encode(json_encode($value));
}
$result = $this->getById($id);
if ($result !== null) {
$this->tableImageStorage->update(array('value' => $value), array(
"id" => $id
));
return true;
}
return false;
}
public function setByImage($image, $value) {
if (is_array($value)) {
$value = Base64::encode(json_encode($value));
}
$result = $this->getByImage($image);
if ($result !== null) {
$this->tableImageStorage->update(array('value' => $value), array(
"id" => $result['id']
));
return true;
}
return false;
}
public function getAll() {
return $this->tableImageStorage->findAllByAttributes(array(), array(
"id",
"hash",
"image",
"value"
));
}
public function set($image, $value) {
if (is_array($value)) {
$value = Base64::encode(json_encode($value));
}
$result = $this->getByImage($image);
if (empty($result)) {
return $this->add($image, $value);
} else {
$attributes = array(
"id" => $result['id']
);
$this->tableImageStorage->update(array('value' => $value), $attributes);
return true;
}
}
public function add($image, $value) {
if (is_array($value)) {
$value = Base64::encode(json_encode($value));
}
$this->tableImageStorage->insert(array(
"hash" => md5($image),
"image" => $image,
"value" => $value
));
return $this->tableImageStorage->insertId();
}
public function deleteById($id) {
$this->tableImageStorage->deleteByAttributes(array(
"id" => $id
));
return true;
}
public function deleteByImage($image) {
$this->tableImageStorage->deleteByAttributes(array(
"hash" => md5($image)
));
return true;
}
}