1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* FlexiPeeHP - Třída pro práci s FlexiBee. |
4
|
|
|
* |
5
|
|
|
* @author Vítězslav Dvořák <[email protected]> |
6
|
|
|
* @copyright (C) 2015,2016 Spoje.Net |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace FlexiPeeHP; |
10
|
|
|
|
11
|
|
|
class FlexiBee extends \Ease\Brick |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Základní namespace pro komunikaci s FlexiBEE. |
15
|
|
|
* |
16
|
|
|
* @var string Jmený prostor datového bloku odpovědi |
17
|
|
|
*/ |
18
|
|
|
public $nameSpace = 'winstrom'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Datový blok v poli odpovědi. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
public $resultField = 'results'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Verze protokolu použitého pro komunikaci. |
29
|
|
|
* |
30
|
|
|
* @var string Verze použitého API |
31
|
|
|
*/ |
32
|
|
|
public $protoVersion = '1.0'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Evidence užitá objektem. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
public $evidence = null; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Výchozí formát pro komunikaci. |
43
|
|
|
* |
44
|
|
|
* @link https://www.flexibee.eu/api/dokumentace/ref/format-types Přehled možných formátů |
45
|
|
|
* |
46
|
|
|
* @var string json|xml|... |
47
|
|
|
*/ |
48
|
|
|
public $format = 'json'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Curl Handle. |
52
|
|
|
* |
53
|
|
|
* @var resource |
54
|
|
|
*/ |
55
|
|
|
public $curl = null; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var type |
59
|
|
|
*/ |
60
|
|
|
public $company = FLEXIBEE_COMPANY; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
public $url = FLEXIBEE_URL; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
public $user = FLEXIBEE_LOGIN; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
public $password = FLEXIBEE_PASSWORD; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Identifikační řetězec. |
79
|
|
|
* |
80
|
|
|
* @var string |
81
|
|
|
*/ |
82
|
|
|
public $init = null; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Sloupeček s názvem. |
86
|
|
|
* |
87
|
|
|
* @var string |
88
|
|
|
*/ |
89
|
|
|
public $nameColumn = 'nazev'; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Sloupeček obsahující datum vložení záznamu do shopu. |
93
|
|
|
* |
94
|
|
|
* @var string |
95
|
|
|
*/ |
96
|
|
|
public $myCreateColumn = 'false'; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Slopecek obsahujici datum poslení modifikace záznamu do shopu. |
100
|
|
|
* |
101
|
|
|
* @var string |
102
|
|
|
*/ |
103
|
|
|
public $myLastModifiedColumn = 'lastUpdate'; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Klíčový idendifikátor záznamu. |
107
|
|
|
* |
108
|
|
|
* @var string |
109
|
|
|
*/ |
110
|
|
|
public $fbKeyColumn = 'id'; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Informace o posledním HTTP requestu. |
114
|
|
|
* |
115
|
|
|
* @var array |
116
|
|
|
*/ |
117
|
|
|
public $info; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Informace o poslední HTTP chybě. |
121
|
|
|
* |
122
|
|
|
* @var array |
123
|
|
|
*/ |
124
|
|
|
public $error; |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Used codes storage. |
128
|
|
|
* |
129
|
|
|
* @var array |
130
|
|
|
*/ |
131
|
|
|
public $codes = null; |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Last Inserted ID. |
135
|
|
|
* |
136
|
|
|
* @var int |
137
|
|
|
*/ |
138
|
|
|
public $lastInsertedID = null; |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Default Line Prefix. |
142
|
|
|
* |
143
|
|
|
* @var string |
144
|
|
|
*/ |
145
|
|
|
public $prefix = '/c/'; |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* HTTP Response code of last request |
149
|
|
|
* |
150
|
|
|
* @var int |
151
|
|
|
*/ |
152
|
|
|
public $lastResponseCode = null; |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Array of fields for next curl POST operation |
156
|
|
|
* |
157
|
|
|
* @var array |
158
|
|
|
*/ |
159
|
|
|
protected $postFields = []; |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Třída pro práci s FlexiBee. |
163
|
|
|
* |
164
|
|
|
* @param string $init výchozí selektor dat |
165
|
|
|
*/ |
166
|
32 |
|
public function __construct($init = null) |
167
|
|
|
{ |
168
|
32 |
|
$this->init = $init; |
169
|
|
|
|
170
|
32 |
|
parent::__construct(); |
171
|
32 |
|
$this->curlInit(); |
172
|
32 |
|
} |
173
|
|
|
|
174
|
32 |
|
public function curlInit() |
175
|
|
|
{ |
176
|
32 |
|
$this->curl = \curl_init(); // create curl resource |
177
|
32 |
|
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); // return content as a string from curl_exec |
178
|
32 |
|
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true); // follow redirects (compatibility for future changes in FlexiBee) |
179
|
32 |
|
curl_setopt($this->curl, CURLOPT_HTTPAUTH, true); // HTTP authentication |
180
|
32 |
|
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false); // FlexiBee by default uses Self-Signed certificates |
181
|
32 |
|
curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false); |
182
|
32 |
|
curl_setopt($this->curl, CURLOPT_VERBOSE, true); // For debugging |
183
|
32 |
|
curl_setopt($this->curl, CURLOPT_USERPWD, |
184
|
32 |
|
$this->user.':'.$this->password); // set username and password |
185
|
32 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Nastaví Agendu pro Komunikaci. |
189
|
|
|
* |
190
|
|
|
* @param string $evidence |
191
|
|
|
*/ |
192
|
16 |
|
public function setEvidence($evidence) |
193
|
|
|
{ |
194
|
16 |
|
$this->evidence = $evidence; |
195
|
16 |
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Převede rekurzivně Objekt na pole. |
199
|
|
|
* |
200
|
|
|
* @param object|array $object |
201
|
|
|
* |
202
|
|
|
* @return array |
203
|
|
|
*/ |
204
|
16 |
|
public static function object2array($object) |
205
|
|
|
{ |
206
|
16 |
|
$result = null; |
207
|
16 |
|
if (is_object($object)) { |
208
|
16 |
|
$objectData = get_object_vars($object); |
209
|
16 |
|
if (is_array($objectData) && count($objectData)) { |
210
|
16 |
|
$result = array_map('self::object2array', $objectData); |
211
|
16 |
|
} |
212
|
16 |
|
} else { |
213
|
16 |
|
if (is_array($object)) { |
214
|
16 |
|
foreach ($object as $item => $value) { |
215
|
16 |
|
$result[$item] = self::object2array($value); |
216
|
16 |
|
} |
217
|
16 |
|
} else { |
218
|
16 |
|
$result = $object; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
16 |
|
return $result; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Funkce, která provede I/O operaci a vyhodnotí výsledek. |
227
|
|
|
* |
228
|
|
|
* @param string $urlSuffix část URL za identifikátorem firmy. |
229
|
|
|
* @param string $method HTTP/REST metoda |
230
|
|
|
* @param string $format Requested format |
231
|
|
|
*/ |
232
|
16 |
|
public function performRequest($urlSuffix = null, $method = 'GET', |
233
|
|
|
$format = null) |
234
|
|
|
{ |
235
|
16 |
|
if (is_null($format)) { |
236
|
16 |
|
$format = $this->format; |
237
|
16 |
|
} |
238
|
16 |
|
if (is_null($urlSuffix)) { |
239
|
3 |
|
$urlSuffix = $this->evidence.'.'.$format; |
240
|
3 |
|
} |
241
|
16 |
|
$url = $this->url.$this->prefix.$this->company.'/'.$urlSuffix; |
242
|
16 |
|
curl_setopt($this->curl, CURLOPT_URL, $url); |
243
|
|
|
// Nastavení samotné operace |
244
|
16 |
|
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method); |
245
|
|
|
//Vždy nastavíme byť i prázná postdata jako ochranu před chybou 411 |
246
|
16 |
|
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->postFields); |
247
|
|
|
|
248
|
|
|
|
249
|
|
|
// Proveď samotnou operaci |
250
|
16 |
|
$response = curl_exec($this->curl); |
251
|
|
|
|
252
|
16 |
|
$this->info = curl_getinfo($this->curl); |
|
|
|
|
253
|
|
|
|
254
|
16 |
|
$this->lastResponseCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); |
255
|
|
|
|
256
|
16 |
|
if ($this->lastResponseCode != 200 && $this->lastResponseCode != 201) { |
257
|
16 |
|
$this->error = curl_error($this->curl); |
|
|
|
|
258
|
|
|
switch ($format) { |
259
|
16 |
|
case 'json': |
260
|
16 |
|
$response = preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/', |
261
|
16 |
|
function ($match) { |
262
|
|
|
return mb_convert_encoding(pack('H*', $match[1]), |
263
|
|
|
'UTF-8', 'UCS-2BE'); |
264
|
16 |
|
}, $response); |
265
|
16 |
|
$response = (json_encode(json_decode($response, true, 10), |
266
|
16 |
|
JSON_PRETTY_PRINT)); |
267
|
16 |
|
break; |
268
|
|
|
case 'xml': |
269
|
|
|
if (strlen($response)) { |
270
|
|
|
$response = self::xml2array($response); |
271
|
|
|
} |
272
|
|
|
break; |
273
|
|
|
} |
274
|
|
|
|
275
|
16 |
|
if (is_array($response)) { |
276
|
|
|
$result = http_build_query($response); |
277
|
|
|
} elseif (strlen($response) && ($response != 'null')) { |
278
|
16 |
|
$result = http_build_query(self::object2array(current(json_decode($response)))); |
279
|
|
|
} else { |
280
|
|
|
$result = null; |
281
|
16 |
|
} |
282
|
2 |
|
|
283
|
2 |
|
if ($this->lastResponseCode == 400) { |
284
|
16 |
|
$this->logResult($result); |
|
|
|
|
285
|
16 |
|
} else { |
286
|
16 |
|
$this->addStatusMessage(sprintf('Error (HTTP %d): <pre>%s</pre> %s', |
287
|
16 |
|
curl_getinfo($this->curl, CURLINFO_HTTP_CODE), $result, |
288
|
|
|
$this->error), 'error'); |
289
|
16 |
|
$this->addStatusMessage($url, 'info'); |
290
|
|
|
$this->addStatusMessage($this->postFields, 'debug'); |
|
|
|
|
291
|
|
|
} |
292
|
|
|
if ($response == 'null') { |
293
|
|
|
if ($this->lastResponseCode == 200) { |
294
|
|
|
$response = true; |
295
|
|
|
} else { |
296
|
16 |
|
$response = null; |
297
|
16 |
|
} |
298
|
16 |
|
} else { |
299
|
|
|
if (is_string($response)) { |
300
|
16 |
|
$response = self::object2array(current(json_decode($response))); |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
return $response; |
304
|
|
|
} |
305
|
14 |
|
|
306
|
14 |
|
// Parse response |
307
|
14 |
|
switch ($format) { |
308
|
|
|
case 'json': |
309
|
|
|
$decoded = json_decode($response, true, 10); |
310
|
14 |
|
if (($method == 'PUT') && isset($decoded[$this->nameSpace][$this->resultField][0]['id'])) { |
311
|
|
|
$this->lastInsertedID = $decoded[$this->nameSpace][$this->resultField][0]['id']; |
312
|
|
|
} else { |
313
|
|
|
$this->lastInsertedID = null; |
314
|
|
|
} |
315
|
14 |
|
// $decodeError = json_last_error_msg(); |
|
|
|
|
316
|
3 |
|
// $this->addStatusMessage($decodeError); |
317
|
3 |
|
|
318
|
3 |
|
break; |
319
|
3 |
|
case 'xml': |
320
|
|
|
if (strlen($response)) { |
321
|
|
|
$decoded = self::xml2array($response); |
322
|
3 |
|
} else { |
323
|
|
|
$decoded = null; |
324
|
|
|
} |
325
|
|
|
break; |
326
|
14 |
|
} |
327
|
14 |
|
|
328
|
14 |
|
// Get response body root automatically |
329
|
|
|
if (isset($decoded[$this->nameSpace])) { |
330
|
14 |
|
$decoded = $decoded[$this->nameSpace]; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
return $decoded; |
|
|
|
|
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Give you last inserted record ID. |
338
|
|
|
* |
339
|
|
|
* @return int |
340
|
|
|
*/ |
341
|
|
|
public function getLastInsertedId() |
342
|
|
|
{ |
343
|
|
|
return $this->lastInsertedID; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Convert XML to array. |
348
|
|
|
* |
349
|
|
|
* @param string $xml |
350
|
16 |
|
* |
351
|
|
|
* @return array |
352
|
16 |
|
*/ |
353
|
|
|
public static function xml2array($xml) |
354
|
16 |
|
{ |
355
|
16 |
|
$arr = []; |
356
|
16 |
|
|
357
|
|
|
if (is_string($xml)) { |
358
|
16 |
|
$xml = simplexml_load_string($xml); |
359
|
16 |
|
} |
360
|
16 |
|
|
361
|
16 |
|
foreach ($xml->children() as $r) { |
362
|
16 |
|
$t = []; |
|
|
|
|
363
|
16 |
|
if (count($r->children()) == 0) { |
364
|
|
|
$arr[$r->getName()] = strval($r); |
365
|
16 |
|
} else { |
366
|
|
|
$arr[$r->getName()][] = self::xml2array($r); |
367
|
16 |
|
} |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
return $arr; |
371
|
|
|
} |
372
|
|
|
|
373
|
17 |
|
/** |
374
|
|
|
* Odpojení od FlexiBee. |
375
|
17 |
|
*/ |
376
|
17 |
|
public function disconnect() |
377
|
17 |
|
{ |
378
|
17 |
|
if (is_resource($this->curl)) { |
379
|
17 |
|
curl_close($this->curl); |
380
|
|
|
} |
381
|
17 |
|
$this->curl = null; |
382
|
|
|
} |
383
|
17 |
|
|
384
|
17 |
|
public function __destruct() |
385
|
|
|
{ |
386
|
|
|
$this->disconnect(); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Načte data z FlexiBee. |
391
|
|
|
* |
392
|
|
|
* @param string $suffix dotaz |
393
|
|
|
*/ |
394
|
|
|
public function loadFlexiData($suffix = null) |
395
|
|
|
{ |
396
|
|
|
return $this->takeData($this->getFlexiData($suffix)); |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Načte řádek dat z FlexiBee. |
401
|
|
|
* |
402
|
|
|
* @param int $recordID id požadovaného záznamu |
403
|
16 |
|
* |
404
|
|
|
* @return array |
405
|
16 |
|
*/ |
406
|
16 |
|
public function getFlexiRow($recordID) |
407
|
16 |
|
{ |
408
|
9 |
|
$record = null; |
409
|
9 |
|
$response = $this->performRequest($this->evidence.'/'.$recordID.'.json'); |
410
|
|
|
if (isset($response[$this->evidence])) { |
411
|
16 |
|
$record = $response[$this->evidence][0]; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
return $record; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* Načte data z FlexiBee. |
419
|
|
|
* |
420
|
15 |
|
* @param string $suffix dotaz |
421
|
|
|
* @param string $conditions Volitelný filtrovací výraz |
422
|
15 |
|
*/ |
423
|
10 |
|
public function getFlexiData($suffix = null, $conditions = null) |
424
|
10 |
|
{ |
425
|
10 |
|
if (!is_null($conditions)) { |
426
|
10 |
|
if ($conditions[0] != '/') { |
427
|
15 |
|
$conditions = '/'.rawurlencode('('.($conditions).')'); |
428
|
|
|
} |
429
|
15 |
|
} else { |
430
|
|
|
$conditions = ''; |
431
|
|
|
} |
432
|
|
|
if ($suffix) { |
|
|
|
|
433
|
15 |
|
$transactions = $this->performRequest($this->evidence.$conditions.'.'.$this->format.'?'.$suffix, |
434
|
15 |
|
'GET'); |
435
|
|
|
} else { |
436
|
15 |
|
$transactions = $this->performRequest($this->evidence.$conditions.'.'.$this->format, |
437
|
12 |
|
'GET'); |
438
|
12 |
|
} |
439
|
3 |
|
if (isset($transactions[$this->evidence])) { |
440
|
|
|
$result = $transactions[$this->evidence]; |
441
|
|
|
} else { |
442
|
15 |
|
$result = $transactions; |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
return $result; |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* Načte záznam z FlexiBee. |
450
|
|
|
* |
451
|
|
|
* @param int $id ID záznamu |
452
|
16 |
|
* |
453
|
|
|
* @return int počet načtených položek |
454
|
16 |
|
*/ |
455
|
16 |
|
public function loadFromFlexiBee($id = null) |
456
|
16 |
|
{ |
457
|
|
|
if (is_null($id)) { |
458
|
16 |
|
$id = $this->getMyKey(); |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
return $this->takeData($this->getFlexiData('/'.$id)); |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* Převede data do Json formátu pro FlexiBee. |
466
|
|
|
* |
467
|
|
|
* @param array $data |
468
|
|
|
* |
469
|
|
|
* @return string |
470
|
|
|
*/ |
471
|
|
|
public function jsonizeData($data) |
472
|
|
|
{ |
473
|
|
|
$jsonize = [ |
474
|
|
|
$this->nameSpace => [ |
475
|
|
|
'@version' => $this->protoVersion, |
476
|
|
|
$this->evidence => $data, |
477
|
|
|
], |
478
|
|
|
]; |
479
|
|
|
|
480
|
|
|
return json_encode($jsonize); |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Uloží záznam. |
485
|
|
|
* |
486
|
|
|
* @param array $data |
487
|
|
|
* |
488
|
16 |
|
* @return array odpověď |
489
|
|
|
*/ |
490
|
|
|
public function insertToFlexiBee($data = null) |
491
|
16 |
|
{ |
492
|
16 |
|
if (is_null($data)) { |
493
|
16 |
|
$data = $this->getData(); |
494
|
16 |
|
} |
495
|
16 |
|
$this->postFields = $this->jsonizeData($data); |
|
|
|
|
496
|
|
|
return $this->performRequest($this->evidence.'.'.$this->format, 'PUT'); |
497
|
16 |
|
} |
498
|
|
|
|
499
|
|
|
/** |
500
|
|
|
* Test if given record ID exists in FlexiBee. |
501
|
|
|
* |
502
|
|
|
* @param string|int $identifer |
503
|
|
|
*/ |
504
|
|
|
public function idExists($identifer = null) |
505
|
|
|
{ |
506
|
|
|
if (is_null($identifer)) { |
507
|
|
|
$identifer = $this->getMyKey(); |
508
|
|
|
} |
509
|
|
|
$flexiData = $this->getFlexiData( |
510
|
|
|
'detail=custom:'.$this->getmyKeyColumn(), $identifer); |
511
|
|
|
|
512
|
|
|
return $flexiData; |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* Test if given record exists in FlexiBee. |
517
|
|
|
* |
518
|
|
|
* @param array $data |
519
|
|
|
*/ |
520
|
|
|
public function recordExists($data = null) |
521
|
|
|
{ |
522
|
|
|
if (is_null($data)) { |
523
|
|
|
$data = $this->getData(); |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
$res = $this->getColumnsFromFlexibee([$this->myKeyColumn], |
527
|
|
|
self::flexiUrl($data)); |
528
|
|
|
|
529
|
|
|
return $res; |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
/** |
533
|
|
|
* Vrací z FlexiBee sloupečky podle podmínek. |
534
|
|
|
* |
535
|
|
|
* @param array|int|string $conditions pole podmínek nebo ID záznamu |
536
|
|
|
* @param array|string $orderBy třídit dle |
537
|
|
|
* @param string $indexBy klice vysledku naplnit hodnotou ze |
538
|
|
|
* sloupečku |
539
|
|
|
* @param int $limit maximální počet vrácených záznamů |
540
|
|
|
* |
541
|
|
|
* @return array |
542
|
|
|
*/ |
543
|
|
|
public function getAllFromFlexibee($conditions = null, $orderBy = null, |
|
|
|
|
544
|
|
|
$indexBy = null, $limit = null) |
|
|
|
|
545
|
|
|
{ |
546
|
|
|
if (is_int($conditions)) { |
547
|
|
|
$conditions = [$this->getmyKeyColumn() => $conditions]; |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
$flexiData = $this->getFlexiData('', $conditions); |
|
|
|
|
551
|
|
|
|
552
|
|
View Code Duplication |
if ($indexBy) { |
|
|
|
|
553
|
|
|
$flexiData2 = []; |
554
|
|
|
foreach ($flexiData as $dataID => $data) { |
555
|
|
|
$flexiData2[$data[$indexBy]] = $data; |
556
|
|
|
} |
557
|
|
|
$flexiData = $flexiData2; |
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
return $flexiData; |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
/** |
564
|
|
|
* Vrací z FlexiBee sloupečky podle podmínek. |
565
|
|
|
* |
566
|
|
|
* @param string[] $columnsList seznam položek |
567
|
|
|
* @param array|int|string $conditions pole podmínek nebo ID záznamu |
568
|
|
|
* @param array|string $orderBy třídit dle |
569
|
|
|
* @param string $indexBy klice vysledku naplnit hodnotou ze |
570
|
|
|
* sloupečku |
571
|
|
|
* @param int $limit maximální počet vrácených záznamů |
572
|
|
|
* |
573
|
|
|
* @return array |
574
|
|
|
*/ |
575
|
|
|
public function getColumnsFromFlexibee($columnsList, $conditions = null, |
576
|
|
|
$orderBy = null, $indexBy = null, |
|
|
|
|
577
|
|
|
$limit = null) |
|
|
|
|
578
|
|
|
{ |
579
|
|
|
if (($columnsList != '*') && !count($columnsList)) { |
580
|
|
|
$this->error('getColumnsFromFlexiBee: Missing ColumnList'); |
581
|
|
|
|
582
|
|
|
return; |
583
|
|
|
} |
584
|
|
|
|
585
|
|
|
if (is_int($conditions)) { |
586
|
|
|
$conditions = [$this->getmyKeyColumn() => $conditions]; |
587
|
|
|
} |
588
|
|
|
|
589
|
|
|
if (is_array($columnsList)) { |
590
|
|
|
$columns = implode(',', array_unique($columnsList)); |
591
|
|
|
} else { |
592
|
|
|
$columns = $columnsList; |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
$flexiData = $this->getFlexiData('detail=custom:'.$columns, $conditions); |
|
|
|
|
596
|
|
|
|
597
|
|
View Code Duplication |
if ($indexBy) { |
|
|
|
|
598
|
|
|
$flexiData2 = []; |
599
|
|
|
foreach ($flexiData as $dataID => $data) { |
600
|
|
|
$flexiData2[$data[$indexBy]] = $data; |
601
|
|
|
} |
602
|
|
|
$flexiData = $flexiData2; |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
return $flexiData; |
606
|
|
|
} |
607
|
|
|
|
608
|
|
|
/** |
609
|
|
|
* Vrací kód záznamu. |
610
|
|
|
* |
611
|
|
|
* @param mixed $data |
612
|
|
|
* |
613
|
|
|
* @todo papat i string |
614
|
|
|
* |
615
|
|
|
* @return string |
616
|
|
|
*/ |
617
|
|
|
public function getKod($data = null, $unique = true) |
618
|
|
|
{ |
619
|
|
|
$kod = null; |
620
|
|
|
|
621
|
|
|
if (is_null($data)) { |
622
|
|
|
$data = $this->getData(); |
623
|
|
|
} |
624
|
|
|
|
625
|
|
|
if (is_string($data)) { |
626
|
|
|
$data = [$this->nameColumn => $data]; |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
if (isset($data['kod'])) { |
630
|
|
|
$kod = $data['kod']; |
631
|
|
|
} else { |
632
|
|
|
if (isset($data[$this->nameColumn])) { |
633
|
|
|
$kod = preg_replace('/[^a-zA-Z0-9]/', '', |
634
|
16 |
|
\Ease\Sand::rip($data[$this->nameColumn])); |
635
|
|
|
} else { |
636
|
16 |
|
if (isset($data[$this->myKeyColumn])) { |
637
|
|
|
$kod = \Ease\Sand::rip($data[$this->myKeyColumn]); |
638
|
16 |
|
} |
639
|
16 |
|
} |
640
|
16 |
|
} |
641
|
|
|
|
642
|
16 |
|
if (!strlen($kod)) { |
643
|
16 |
|
$kod = 'NOTSET'; |
644
|
16 |
|
} |
645
|
|
|
|
646
|
16 |
|
if (strlen($kod) > 18) { |
647
|
16 |
|
$kodfinal = strtoupper(substr($kod, 0, 18)); |
648
|
16 |
|
} else { |
649
|
16 |
|
$kodfinal = strtoupper($kod); |
650
|
16 |
|
} |
651
|
16 |
|
|
652
|
16 |
|
if ($unique) { |
653
|
16 |
|
$counter = 0; |
654
|
16 |
|
if ($this->codes) { |
|
|
|
|
655
|
16 |
|
foreach ($this->codes as $codesearch => $keystring) { |
656
|
|
|
if (strstr($codesearch, $kodfinal)) { |
657
|
|
|
++$counter; |
658
|
|
|
} |
659
|
16 |
|
} |
660
|
16 |
|
} |
661
|
16 |
|
if ($counter) { |
662
|
|
|
$kodfinal = $kodfinal.$counter; |
663
|
16 |
|
} |
664
|
16 |
|
|
665
|
16 |
|
$this->codes[$kodfinal] = $kod; |
666
|
16 |
|
} |
667
|
|
|
|
668
|
|
|
return $kodfinal; |
669
|
16 |
|
} |
670
|
16 |
|
|
671
|
16 |
|
/** |
672
|
16 |
|
* Vyhledavani v záznamech objektu FlexiBee. |
673
|
16 |
|
* |
674
|
16 |
|
* @param string $what hledaný výraz |
675
|
16 |
|
* |
676
|
16 |
|
* @return array pole výsledků |
677
|
16 |
|
*/ |
678
|
16 |
|
public function searchString($what) |
679
|
16 |
|
{ |
680
|
16 |
|
$results = []; |
681
|
|
|
$conds = []; |
682
|
16 |
|
$columns[] = $this->myKeyColumn; |
|
|
|
|
683
|
16 |
|
foreach ($this->useKeywords as $keyword => $keywordInfo) { |
|
|
|
|
684
|
|
|
if (isset($this->keywordsInfo[$keyword]['virtual']) && ($this->keywordsInfo[$keyword]['virtual'] |
|
|
|
|
685
|
16 |
|
== true)) { |
686
|
|
|
if ($keyword == $this->nameColumn) { |
687
|
|
|
$this->nameColumn = $this->myKeyColumn; |
688
|
|
|
} |
689
|
|
|
continue; |
690
|
|
|
} |
691
|
|
|
switch ($keywordInfo) { |
692
|
|
|
case 'INT': |
693
|
|
View Code Duplication |
case 'FLOAT': |
|
|
|
|
694
|
|
|
if (is_numeric($what)) { |
695
|
|
|
$conds[] = "($keyword = ".$what.')'; |
696
|
|
|
$columns[] = "$keyword"; |
697
|
|
|
} |
698
|
|
|
break; |
699
|
|
|
case 'TEXT': |
700
|
|
View Code Duplication |
case 'STRING': |
|
|
|
|
701
|
|
|
if (is_string($what)) { |
702
|
|
|
$conds[] = "( $keyword like '".$what."')"; |
703
|
|
|
$columns[] = "$keyword"; |
704
|
|
|
} |
705
|
|
|
break; |
706
|
|
|
default: |
707
|
|
|
break; |
708
|
|
|
} |
709
|
|
|
} |
710
|
|
|
|
711
|
|
|
// $res = \Ease\Shared::db()->queryToArray('SELECT ' . implode(',', $columns) . ',' . $this->nameColumn . ' FROM ' . $this->myTable . ' WHERE ' . implode(' OR ', $conds) . ' ORDER BY ' . $this->nameColumn, $this->myKeyColumn); |
|
|
|
|
712
|
|
|
|
713
|
|
|
$res = $this->getColumnsFromFlexibee($columns, implode(' or ', $conds)); |
714
|
|
|
|
715
|
|
|
foreach ($res as $result) { |
716
|
|
|
$occurences = ''; |
717
|
|
|
foreach ($result as $key => $value) { |
718
|
|
|
if (is_array($value)) { |
719
|
|
|
continue; |
720
|
|
|
} |
721
|
|
|
if (mb_stristr($value, $what)) { |
722
|
|
|
$occurences .= '('.$key.': '.$value.')'; |
723
|
|
|
} |
724
|
|
|
} |
725
|
|
|
$results[$result[$this->myKeyColumn]] = [$this->nameColumn => $result[$this->nameColumn], |
726
|
|
|
'what' => $occurences,]; |
727
|
|
|
} |
728
|
|
|
|
729
|
|
|
return $results; |
730
|
|
|
} |
731
|
|
|
|
732
|
|
|
/** |
733
|
|
|
* Write Operation Result. |
734
|
|
|
* |
735
|
|
|
* @param array $resultData |
736
|
|
|
* @param string $url URL |
737
|
|
|
*/ |
738
|
|
|
public function logResult($resultData, $url = null) |
739
|
|
|
{ |
740
|
|
|
if ($url) { |
|
|
|
|
741
|
|
|
$this->logger->addStatusMessage($url); |
742
|
|
|
} |
743
|
|
|
|
744
|
|
|
if (isset($resultData['results'])) { |
745
|
|
|
$status = null; |
|
|
|
|
746
|
|
|
if ($resultData['success'] == 'false') { |
747
|
|
|
$status = 'error'; |
748
|
|
|
} else { |
749
|
|
|
$status = 'success'; |
750
|
|
|
} |
751
|
|
|
foreach ($resultData['results'] as $result) { |
752
|
|
|
if (isset($result['request-id'])) { |
753
|
|
|
$rid = $result['request-id']; |
754
|
|
|
} else { |
755
|
16 |
|
$rid = ''; |
756
|
|
|
} |
757
|
16 |
|
if (isset($result['errors'])) { |
758
|
16 |
|
foreach ($result['errors'] as $error) { |
759
|
16 |
|
$message = $error['message']; |
760
|
|
|
if (isset($error['for'])) { |
761
|
16 |
|
$message.=' for: '.$error['for']; |
762
|
16 |
|
} |
763
|
16 |
|
if (isset($error['value'])) { |
764
|
16 |
|
$message.=' value:'.$error['value']; |
765
|
16 |
|
} |
766
|
16 |
|
if (isset($error['code'])) { |
767
|
|
|
$message.=' code:'.$error['code']; |
768
|
16 |
|
} |
769
|
16 |
|
$this->logger->addStatusMessage($rid.': '.$message, |
770
|
16 |
|
$status); |
771
|
16 |
|
} |
772
|
16 |
|
} |
773
|
|
|
} |
774
|
16 |
|
} |
775
|
16 |
|
if (is_object($this->logger)) { |
776
|
16 |
|
$this->logger->flush(get_class($this)); |
777
|
16 |
|
} |
778
|
|
|
} |
779
|
|
|
|
780
|
16 |
|
/** |
781
|
|
|
* Generuje fragment url pro filtrování. |
782
|
|
|
* |
783
|
16 |
|
* @see https://www.flexibee.eu/api/dokumentace/ref/filters |
784
|
|
|
* |
785
|
|
|
* @param array $data |
786
|
16 |
|
* @param string $operator and/or |
787
|
16 |
|
* |
788
|
16 |
|
* @return string |
789
|
16 |
|
*/ |
790
|
16 |
|
public static function flexiUrl(array $data, $operator = 'and') |
791
|
16 |
|
{ |
792
|
16 |
|
$flexiUrl = ''; |
|
|
|
|
793
|
16 |
|
$parts = []; |
794
|
16 |
|
|
795
|
16 |
|
foreach ($data as $column => $value) { |
796
|
|
|
if (is_numeric($data[$column])) { |
797
|
|
|
$parts[$column] = $column.' = '.$data[$column]; |
798
|
|
|
} else { |
799
|
|
|
$parts[$column] = $column." = '".$data[$column]."'"; |
800
|
|
|
} |
801
|
|
|
} |
802
|
|
|
|
803
|
|
|
$flexiUrl = implode(' '.$operator.' ', $parts); |
804
|
|
|
|
805
|
|
|
return $flexiUrl; |
806
|
|
|
} |
807
|
16 |
|
|
808
|
|
|
/** |
809
|
16 |
|
* Smaže záznam |
810
|
16 |
|
* |
811
|
|
|
* @param int|string $id identifikátor záznamu |
812
|
16 |
|
* @return boolean Response code is 200 ? |
813
|
16 |
|
*/ |
814
|
16 |
|
public function deleteFromFlexiBee($id) |
815
|
16 |
|
{ |
816
|
16 |
|
$this->performRequest($this->evidence.'/'.$id.'.'.$this->format, |
817
|
|
|
'DELETE'); |
818
|
16 |
|
return $this->lastResponseCode == 200; |
819
|
|
|
} |
820
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..