-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.php
More file actions
235 lines (207 loc) · 5.48 KB
/
map.php
File metadata and controls
235 lines (207 loc) · 5.48 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
require_once 'vendor/autoload.php';
/**
* Whole Class analog to user.php
* Created by PhpStorm.
* User: User
* Date: 17.06.2017
* Time: 18:30
*/
class map extends udvide
{
/** @var string */
protected $name;
/** @var resource */
protected $image;
public function __construct(){}
//<editor-fold desc="CRUD DB">
public function read()
{
$sql = <<<'SQL'
SELECT name, image
FROM udvide.Maps m
WHERE name = ?
SQL;
$db = access_DB::prepareExecuteFetchStatement($sql, [$this->name]);
$this->set($db[0]);
return $this;
}
public static function readAll()
{
$sql = <<<'SQL'
SELECT name, image
FROM udvide.Maps
SQL;
$db = access_DB::prepareExecuteFetchStatement($sql);
foreach ($db as $key => $userArr) {
$temp = new self();
$temp->setImage($userArr['image']);
$db[$key]['image'] = $temp->getImageAsDataUrlJpg();
} // todo refactor
return $db;
}
public function create()
{
if (user::getLoggedInUser()->getRole() < MIN_ALLOW_MAP_CREATE)
throw new PermissionException(ERR_PERMISSION_INSUFFICIENT,1);
if (!isset($this->name) || !isset($this->image))
throw new IncompleteObjectException(ERR_USER_DATASET_INVALID,1);
$sql = <<<'SQL'
INSERT INTO udvide.Maps
(name,image)
VALUES (?,?);
SQL;
$values = [
$this->name,
$this->getImageAsRawJpg()
];
access_DB::prepareExecuteStatementGetAffected($sql,$values);
return $this;
}
public function update(string $subject = null)
{
$subject = empty($subject) ? $this->name : $subject;
// If not allowed to update and self-update (in case of self update)
if (user::getLoggedInUser()->getRole() < MIN_ALLOW_MAP_UPDATE)
throw new PermissionException(ERR_PERMISSION_INSUFFICIENT,1);
$updateDB = false;
$sql = '';
foreach ($this as $key => $value) {
if(isset($this->{$key})) {
$sql .= " $key = ? , ";
if ($key == "image") {
$value = $this->getImageAsRawJpg();
}
$ins[] = $value;
$updateDB = true;
}
}
$sql = rtrim(rtrim($sql),',');
if ($updateDB) {
$sql = <<<SQL
UPDATE udvide.maps
SET $sql
WHERE name = ?;
SQL;
$ins[] = $subject;
//echo $sql ."\n";
//var_dump($ins);
access_DB::prepareExecuteFetchStatement($sql, $ins);
}
return $this;
}
public function delete()
{
if (user::getLoggedInUser()->getRole() < MIN_ALLOW_MAP_DELETE)
throw new PermissionException(ERR_PERMISSION_INSUFFICIENT,1);
$sql = 'DELETE FROM udvide.Maps WHERE name = ?';
access_DB::prepareExecuteFetchStatement($sql,[$this->name]);
}
//</editor-fold>
public function __set(string $name, $value): map
{
switch($name) {
case 'name':
return $this->setName($value);
case 'image':
return $this->setImage($value);
default:
return $this;
}
}
public function __get(string $name) {
switch($name) {
case 'name':
return $this->getName();
case 'image':
return $this->getImage();
case 'width':
return $this->getWidth();
case 'height':
return $this->getHeight();
default:
return null;
}
}
//<editor-fold desc="Fluent Setter">
/**
* @param string $name
* @return map
*/
public function setName(string $name = null):map
{
$this->name = $name;
return $this;
}
/**
* @param resource|string $image
* @return map
*/
public function setImage($image = null):map
{
if (isset($image)) {
if (is_string($image)) {
if (!helper::strIsJpg($image)) {
// if it is a data url we want to resolve it
$image = helper::base64ImgToDecodeAbleBase64($image);
$image = base64_decode($image);
}
$image = imagecreatefromstring($image);
}
$this->image = helper::imgAssistant($image, ['maxFileSize' => VUFORIA_DATA_SIZE_LIMIT]);
}
return $this;
}
//</editor-fold>
//<editor-fold desc="Getter">
/**
* @return string
*/
public function getName():string
{
return $this->name;
}
/**
* @return resource
*/
public function getImage()
{
return $this->image;
}
/**
* @return string
*/
public function getImageAsRawJpg()
{
return helper::imgResToJpgString($this->image); // quality defaults to 95
}
/**
* @return string
*/
public function getImageAsBase64Jpg()
{
return base64_encode($this->getImageAsRawJpg());
}
/**
* @return string
*/
public function getImageAsDataUrlJpg()
{
return 'data:image/jpeg;base64,' . $this->getImageAsBase64Jpg();
}
/**
* @return int
*/
public function getHeight():int
{
return imagesy($this->image);
}
/**
* @return int
*/
public function getWidth():int
{
return imagesx($this->image);
}
//</editor-fold>
}