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,2016 Spoje.Net |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace FlexiPeeHP; |
10
|
|
|
|
11
|
|
|
class FlexiBeeRW extends FlexiBeeRO |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Sloupeček obsahující datum vložení záznamu do shopu. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
public $myCreateColumn = 'false'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Slopecek obsahujici datum poslení modifikace záznamu do shopu. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
public $myLastModifiedColumn = 'lastUpdate'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Last Inserted ID. |
29
|
|
|
* |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
public $lastInsertedID = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Array of fields for next curl POST operation |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
public $postFields = null; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Save record (if evidence allow to). |
43
|
|
|
* Uloží záznam (pokud to evidence dovoluje) |
44
|
|
|
* |
45
|
|
|
* @param array $data Data to save |
46
|
|
|
* @throws Exception Evidence does not support Import |
47
|
|
|
* |
48
|
|
|
* @return array odpověď |
49
|
|
|
*/ |
50
|
|
|
public function insertToFlexiBee($data = null) |
51
|
|
|
{ |
52
|
|
|
$info = $this->getEvidenceInfo(); |
53
|
|
|
switch ($info['importStatus']) { |
54
|
|
|
case 'NOT_DOCUMENTED': |
55
|
|
|
$this->addStatusMessage(sprintf('Inserting data to undocumneted evidence %s', |
56
|
|
|
$this->getEvidence())); |
57
|
|
|
|
58
|
|
|
break; |
59
|
|
|
case 'DISALLOWED': |
60
|
|
|
throw new Exception(sprintf('Inserting data to r/o evidence %s', |
61
|
|
|
$this->getEvidence())); |
62
|
|
|
case 'SUPPORTED': |
63
|
|
|
default: |
64
|
|
|
break; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (is_null($data)) { |
68
|
|
|
$data = $this->getData(); |
69
|
|
|
} |
70
|
|
|
$this->postFields = $this->jsonizeData($data); |
71
|
|
|
return $this->performRequest($this->evidence.'.'.$this->format, 'PUT'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Give you last inserted record ID. |
76
|
|
|
* |
77
|
|
|
* @return int |
78
|
|
|
*/ |
79
|
|
|
public function getLastInsertedId() |
80
|
|
|
{ |
81
|
|
|
return $this->lastInsertedID; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Smaže záznam |
86
|
|
|
* |
87
|
|
|
* @param int|string $id identifikátor záznamu |
88
|
|
|
* @return boolean Response code is 200 ? |
89
|
|
|
*/ |
90
|
|
|
public function deleteFromFlexiBee($id = null) |
91
|
|
|
{ |
92
|
|
|
if (is_null($id)) { |
93
|
|
|
$id = $this->getMyKey(); |
94
|
|
|
} |
95
|
|
|
$this->performRequest($this->evidence.'/'.$id.'.'.$this->format, |
96
|
|
|
'DELETE'); |
97
|
|
|
return $this->lastResponseCode == 200; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Control for existing column names in evidence and take data |
102
|
|
|
* |
103
|
|
|
* @param array $data Data to keep |
104
|
|
|
* @return int number of records taken |
105
|
|
|
* @throws \Exception try to load data to unexistent column |
106
|
|
|
*/ |
107
|
|
|
public function takeData($data) |
108
|
|
|
{ |
109
|
|
|
$fbColumns = $this->getColumnsInfo(); |
110
|
|
|
foreach ($data as $key => $value) { |
111
|
|
|
if (!array_key_exists($key, $fbColumns)) { |
112
|
|
|
throw new \Exception(sprintf('unknown column %s for evidence %s', |
113
|
|
|
$key, $this->getEvidence())); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return parent::takeData($data); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Convert Timestamp to FlexiBee Date format. |
122
|
|
|
* |
123
|
|
|
* @param int $timpestamp |
124
|
|
|
* |
125
|
|
|
* @return string FlexiBee Date or NULL |
126
|
|
|
*/ |
127
|
|
View Code Duplication |
public static function timestampToFlexiDate($timpestamp = null) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
$flexiDate = null; |
130
|
|
|
if (!is_null($timpestamp)) { |
131
|
|
|
$date = new \DateTime(); |
132
|
|
|
$date->setTimestamp($timpestamp); |
133
|
|
|
$flexiDate = $date->format('Y-m-d'); |
134
|
|
|
} |
135
|
|
|
return $flexiDate; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Convert Timestamp to Flexi DateTime format. |
140
|
|
|
* |
141
|
|
|
* @param int $timpestamp |
142
|
|
|
* |
143
|
|
|
* @return string FlexiBee DateTime or NULL |
144
|
|
|
*/ |
145
|
|
View Code Duplication |
public static function timestampToFlexiDateTime($timpestamp = null) |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
$flexiDateTime = null; |
148
|
|
|
if (!is_null($timpestamp)) { |
149
|
|
|
$date = new \DateTime(); |
150
|
|
|
$date->setTimestamp($timpestamp); |
151
|
|
|
$flexiDateTime = $date->format('Y-m-dTH:i:s'); |
152
|
|
|
} |
153
|
|
|
return $flexiDateTime; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.