1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* FlexiPeeHP - Třída pro zápis do FlexiBee. |
4
|
|
|
* |
5
|
|
|
* @author Vítězslav Dvořák <[email protected]> |
6
|
|
|
* @copyright (C) 2015-2017 Spoje.Net |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace FlexiPeeHP; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Základní třída pro zápis do FlexiBee |
13
|
|
|
* |
14
|
|
|
* @url https://demo.flexibee.eu/devdoc/http-operations |
15
|
|
|
*/ |
16
|
|
|
class FlexiBeeRW extends FlexiBeeRO |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Sloupeček obsahující datum vložení záznamu do shopu. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
public $myCreateColumn = 'false'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Slopecek obsahujici datum poslení modifikace záznamu do shopu. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
public $myLastModifiedColumn = 'lastUpdate'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Last Inserted ID. |
34
|
|
|
* |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
public $lastInsertedID = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Array of fields for next curl POST operation |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
public $postFields = null; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Save record (if evidence allow to). |
48
|
|
|
* Uloží záznam (pokud to evidence dovoluje) |
49
|
|
|
* |
50
|
|
|
* @param array $data Data to save |
51
|
|
|
* @throws Exception Evidence does not support Import |
52
|
|
|
* |
53
|
|
|
* @return array odpověď |
54
|
|
|
*/ |
55
|
|
|
public function insertToFlexiBee($data = null) |
56
|
|
|
{ |
57
|
|
|
$info = $this->getEvidenceInfo(); |
58
|
|
|
switch ($info['importStatus']) { |
59
|
|
|
case 'DISALLOWED': |
60
|
|
|
throw new \Exception(sprintf('Inserting data to r/o evidence %s', |
61
|
|
|
$this->getEvidence())); |
62
|
|
|
case 'NOT_DIRECT': |
63
|
|
|
throw new \Exception(sprintf('Inserting data to slave only evidence %s', |
64
|
|
|
$this->getEvidence())); |
65
|
|
|
case 'NOT_DOCUMENTED': |
66
|
|
|
if ($this->debug === true) { |
67
|
|
|
$this->addStatusMessage(sprintf('Inserting data to undocumneted evidence %s', |
68
|
|
|
$this->getEvidence())); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
break; |
72
|
|
|
case 'SUPPORTED': |
73
|
|
|
default: |
74
|
|
|
break; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (is_null($data)) { |
78
|
|
|
$data = $this->getData(); |
79
|
|
|
} |
80
|
|
|
if ($this->debug === true) { |
81
|
|
|
$missingColumns = $this->controlMandatoryColumns($data); |
82
|
|
|
if (count($missingColumns)) { |
83
|
|
|
$this->addStatusMessage(sprintf('Given data does not contain requied Columns: %s', |
84
|
|
|
'['.implode(',', array_keys($missingColumns)).'] '.implode(',', |
85
|
|
|
$missingColumns)) |
86
|
|
|
, 'warning'); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
$this->postFields = $this->jsonizeData($data); |
90
|
|
|
return $this->performRequest($this->evidence.'.'.$this->format, 'PUT'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Give you last inserted record ID. |
95
|
|
|
* |
96
|
|
|
* @return int |
97
|
|
|
*/ |
98
|
|
|
public function getLastInsertedId() |
99
|
|
|
{ |
100
|
|
|
return $this->lastInsertedID; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Smaže záznam |
105
|
|
|
* Delete record in FlexiBee |
106
|
|
|
* |
107
|
|
|
* @param int|string $id identifikátor záznamu |
108
|
|
|
* @return boolean Response code is 200 ? |
109
|
|
|
*/ |
110
|
|
|
public function deleteFromFlexiBee($id = null) |
111
|
|
|
{ |
112
|
|
|
if (is_null($id)) { |
113
|
|
|
$id = $this->getMyKey(); |
114
|
|
|
} |
115
|
|
|
$this->performRequest($this->evidence.'/'.$id.'.'.$this->format, |
116
|
|
|
'DELETE'); |
117
|
|
|
return $this->lastResponseCode == 200; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Control for existing column names in evidence and take data |
122
|
|
|
* |
123
|
|
|
* @param array $data Data to keep |
124
|
|
|
* @return int number of records taken |
125
|
|
|
*/ |
126
|
|
|
public function takeData($data) |
127
|
|
|
{ |
128
|
|
|
if ($this->debug === true) { |
129
|
|
|
$fbColumns = $this->getColumnsInfo(); |
130
|
|
|
foreach ($this->getRelationsInfo() as $relation) { |
131
|
|
|
if (is_array($relation) && isset($relation['url'])) { |
132
|
|
|
$fbRelations[$relation['url']] = $relation['url']; |
|
|
|
|
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (count($fbColumns)) { |
137
|
|
|
foreach ($data as $key => $value) { |
138
|
|
|
if (!array_key_exists($key, $fbColumns)) { |
139
|
|
|
|
140
|
|
|
if (!array_key_exists($key, $fbRelations)) { |
|
|
|
|
141
|
|
|
$this->addStatusMessage(sprintf('unknown column %s for evidence %s', |
142
|
|
|
$key, $this->getEvidence()), 'warning'); |
143
|
|
|
} else { |
144
|
|
|
if (!is_array($value)) { |
145
|
|
|
$this->addStatusMessage(sprintf('subevidence %s in evidence %s must bee an array', |
146
|
|
|
$key, $this->getEvidence()), 'warning'); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
return parent::takeData($data); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Control data for mandatory columns presence. |
158
|
|
|
* |
159
|
|
|
* @param array $data |
160
|
|
|
* @return array List of missing columns. Empty if all is ok |
161
|
|
|
*/ |
162
|
|
View Code Duplication |
public function controlMandatoryColumns($data = null) |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
if (is_null($data)) { |
165
|
|
|
$data = $this->getData(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$missingMandatoryColumns = []; |
169
|
|
|
|
170
|
|
|
$fbColumns = $this->getColumnsInfo(); |
171
|
|
|
foreach ($fbColumns as $columnName => $columnInfo) { |
172
|
|
|
$mandatory = ($columnInfo['mandatory'] == 'true'); |
173
|
|
|
if ($mandatory && !array_key_exists($columnName, $data)) { |
174
|
|
|
$missingMandatoryColumns[$columnName] = $columnInfo['name']; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $missingMandatoryColumns; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Control data for readonly columns presence. |
183
|
|
|
* |
184
|
|
|
* @param array $data |
185
|
|
|
* @return array List of ReadOnly columns. Empty if all is ok |
186
|
|
|
*/ |
187
|
|
View Code Duplication |
public function controlReadOnlyColumns($data = null) |
|
|
|
|
188
|
|
|
{ |
189
|
|
|
if (is_null($data)) { |
190
|
|
|
$data->getData(); |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$readonlyColumns = []; |
194
|
|
|
|
195
|
|
|
$fbColumns = $this->getColumnsInfo(); |
196
|
|
|
foreach ($fbColumns as $columnName => $columnInfo) { |
197
|
|
|
$writable = ($columnInfo['isWritable'] == 'true'); |
198
|
|
|
if (!$writable && !array_key_exists($columnName, $data)) { |
199
|
|
|
$readonlyColumns[$columnName] = $columnInfo['name']; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $readonlyColumns; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Convert Timestamp to FlexiBee Date format. |
208
|
|
|
* |
209
|
|
|
* @param int $timpestamp |
210
|
|
|
* |
211
|
|
|
* @return string FlexiBee Date or NULL |
212
|
|
|
*/ |
213
|
|
View Code Duplication |
public static function timestampToFlexiDate($timpestamp = null) |
|
|
|
|
214
|
|
|
{ |
215
|
|
|
$flexiDate = null; |
216
|
|
|
if (!is_null($timpestamp)) { |
217
|
|
|
$date = new \DateTime(); |
218
|
|
|
$date->setTimestamp($timpestamp); |
219
|
|
|
$flexiDate = $date->format('Y-m-d'); |
220
|
|
|
} |
221
|
|
|
return $flexiDate; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Convert Timestamp to Flexi DateTime format. |
226
|
|
|
* |
227
|
|
|
* @param int $timpestamp |
228
|
|
|
* |
229
|
|
|
* @return string FlexiBee DateTime or NULL |
230
|
|
|
*/ |
231
|
|
View Code Duplication |
public static function timestampToFlexiDateTime($timpestamp = null) |
|
|
|
|
232
|
|
|
{ |
233
|
|
|
$flexiDateTime = null; |
234
|
|
|
if (!is_null($timpestamp)) { |
235
|
|
|
$date = new \DateTime(); |
236
|
|
|
$date->setTimestamp($timpestamp); |
237
|
|
|
$flexiDateTime = $date->format('Y-m-dTH:i:s'); |
238
|
|
|
} |
239
|
|
|
return $flexiDateTime; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Add Data to evidence Branch |
244
|
|
|
* Přidá data do větve |
245
|
|
|
* |
246
|
|
|
* @thanksto Karel Běl |
247
|
|
|
* @param array $data pole dat |
248
|
|
|
* @param string $relationPath path evidence (relation) pro vkládaná data |
249
|
|
|
* @see Relations |
250
|
|
|
*/ |
251
|
|
|
public function addArrayToBranch($data, $relationPath) |
252
|
|
|
{ |
253
|
|
|
if ($this->debug === true) { |
254
|
|
|
$relationsByUrl = \Ease\Sand::reindexArrayBy($this->getRelationsInfo(), |
255
|
|
|
'url'); |
256
|
|
|
if (!array_key_exists($relationPath, $relationsByUrl)) { |
257
|
|
|
$this->addStatusMessage("Relation to $relationPath does not exist for evidence ".$this->getEvidence(), |
258
|
|
|
'warning'); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
$currentBranchData = $this->getDataValue($relationPath); |
262
|
|
|
$branchData = $currentBranchData; |
263
|
|
|
$branchData[] = $data; |
264
|
|
|
$this->setDataValue($relationPath, $branchData); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Vloží do větve data z objektu |
269
|
|
|
* |
270
|
|
|
* @param FlexiBeeRO $object objekt evidence |
271
|
|
|
*/ |
272
|
|
|
public function addObjectToBranch($object) |
273
|
|
|
{ |
274
|
|
|
$this->addArrayToBranch($object->getData(), $object->getEvidence()); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @see https://www.flexibee.eu/api/dokumentace/ref/uzivatelske-vazby/ |
279
|
|
|
*/ |
280
|
|
|
public function vazbaAdd() |
281
|
|
|
{ |
282
|
|
|
$this->addArrayToBranch(['uzivatelska-vazba' => $vazba], |
|
|
|
|
283
|
|
|
'uzivatelske-vazby'); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @ |
288
|
|
|
*/ |
289
|
|
|
public function vazbaDel() |
290
|
|
|
{ |
291
|
|
|
|
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Převede data do Json formátu pro FlexiBee. |
296
|
|
|
* Pokud jsou štítky pole, jsou převedeny na seznam oddělený čárkou. |
297
|
|
|
* Convert data to FlexiBee like Json format. |
298
|
|
|
* Array of Labels is converted to coma separated list |
299
|
|
|
* |
300
|
|
|
* @param array $data |
301
|
|
|
* |
302
|
|
|
* @return string |
303
|
|
|
*/ |
304
|
|
|
public function jsonizeData($data) |
305
|
|
|
{ |
306
|
|
|
if (array_key_exists('stitky', $data)) { |
307
|
|
|
if (is_array($data['stitky'])) { |
308
|
|
|
$data['stitky'] = implode(',', $data['stitky']); |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
return parent::jsonizeData($data); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Insert current data into FlexiBee and load actual record data back |
316
|
|
|
*/ |
317
|
|
|
public function refresh() |
318
|
|
|
{ |
319
|
|
|
$this->insertToFlexiBee(); |
320
|
|
|
$this->loadFromFlexiBee(); |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.