1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wabel\Zoho\CRM\Copy; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
6
|
|
|
use Psr\Log\LoggerInterface; |
7
|
|
|
use Psr\Log\NullLogger; |
8
|
|
|
use Wabel\Zoho\CRM\AbstractZohoDao; |
9
|
|
|
use Wabel\Zoho\CRM\ZohoBeanInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Description of ZohoDatabasePusher. |
13
|
|
|
* |
14
|
|
|
* @author rbergina |
15
|
|
|
*/ |
16
|
|
|
class ZohoDatabasePusher |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Connection |
20
|
|
|
*/ |
21
|
|
|
private $connection; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var LoggerInterface |
25
|
|
|
*/ |
26
|
|
|
private $logger; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $prefix; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Connection $connection |
35
|
|
|
* @param int $apiLimitInsertUpdateDelete |
36
|
|
|
* @param string $prefix |
37
|
|
|
* @param LoggerInterface $logger |
38
|
|
|
*/ |
39
|
|
|
public function __construct(Connection $connection, $apiLimitInsertUpdateDelete = 100, $prefix = 'zoho_', LoggerInterface $logger = null) |
40
|
|
|
{ |
41
|
|
|
$this->connection = $connection; |
42
|
|
|
$this->prefix = $prefix; |
43
|
|
|
if ($logger === null) { |
44
|
|
|
$this->logger = new NullLogger(); |
45
|
|
|
} else { |
46
|
|
|
$this->logger = $logger; |
47
|
|
|
} |
48
|
|
|
$this->apiLimitInsertUpdateDelete = $apiLimitInsertUpdateDelete; |
49
|
|
|
if($apiLimitInsertUpdateDelete === null){ |
50
|
|
|
$this->apiLimitInsertUpdateDelete = 100; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var int |
56
|
|
|
*/ |
57
|
|
|
private $apiLimitInsertUpdateDelete; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param AbstractZohoDao $zohoDao |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
private function findMethodValues(AbstractZohoDao $zohoDao) |
65
|
|
|
{ |
66
|
|
|
$fieldsMatching = array(); |
67
|
|
|
foreach ($zohoDao->getFields() as $fieldsDescriptor) { |
|
|
|
|
68
|
|
|
foreach (array_values($fieldsDescriptor) as $fieldDescriptor) { |
69
|
|
|
$fieldsMatching[$fieldDescriptor['name']] = [ |
70
|
|
|
'setter' => $fieldDescriptor['setter'], |
71
|
|
|
'type' => $fieldDescriptor['type'], |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $fieldsMatching; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Insert or Update rows. |
81
|
|
|
* |
82
|
|
|
* @param AbstractZohoDao $zohoDao |
83
|
|
|
*/ |
84
|
|
|
public function pushDataToZoho(AbstractZohoDao $zohoDao, $update = false) |
85
|
|
|
{ |
86
|
|
|
$localTable = $update ? 'local_update' : 'local_insert'; |
87
|
|
|
$fieldsMatching = $this->findMethodValues($zohoDao); |
88
|
|
|
$tableName = ZohoDatabaseHelper::getTableName($zohoDao, $this->prefix); |
89
|
|
|
$rowsDeleted = []; |
90
|
|
|
$statement = $this->connection->createQueryBuilder(); |
91
|
|
|
$statement->select('zcrm.*'); |
92
|
|
|
if ($update) { |
93
|
|
|
$statement->addSelect('l.field_name as updated_fieldname'); |
94
|
|
|
} |
95
|
|
|
$statement->from($localTable, 'l') |
96
|
|
|
->join('l', $tableName, 'zcrm', 'zcrm.uid = l.uid') |
97
|
|
|
->where('l.table_name=:table_name') |
98
|
|
|
->setParameters([ |
99
|
|
|
'table_name' => $tableName, |
100
|
|
|
]) |
101
|
|
|
; |
102
|
|
|
$results = $statement->execute(); |
103
|
|
|
/* @var $zohoBeans ZohoBeanInterface[] */ |
104
|
|
|
$zohoBeans = array(); |
105
|
|
|
while ($row = $results->fetch()) { |
106
|
|
|
$beanClassName = $zohoDao->getBeanClassName(); |
|
|
|
|
107
|
|
|
/* @var $zohoBean ZohoBeanInterface */ |
108
|
|
|
if (isset($zohoBeans[$row['uid']])) { |
109
|
|
|
$zohoBean = $zohoBeans[$row['uid']]; |
110
|
|
|
} else { |
111
|
|
|
$zohoBean = new $beanClassName(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (!$update) { |
115
|
|
|
$this->insertDataZohoBean($zohoBean, $fieldsMatching, $row); |
116
|
|
|
$zohoBeans[$row['uid']] = $zohoBean; |
117
|
|
|
$rowsDeleted[] = $row['uid']; |
118
|
|
|
} |
119
|
|
|
if ($update && isset($row['updated_fieldname'])) { |
120
|
|
|
$columnName = $row['updated_fieldname']; |
121
|
|
|
$zohoBean->getZohoId() ?: $zohoBean->setZohoId($row['id']); |
122
|
|
|
$this->updateDataZohoBean($zohoBean, $fieldsMatching, $columnName, $row[$columnName]); |
123
|
|
|
$zohoBeans[$row['uid']] = $zohoBean; |
124
|
|
|
$rowsDeleted[] = $row['uid']; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
$countItems = count($zohoBeans); |
128
|
|
|
|
129
|
|
|
//@see https://www.zoho.com/crm/help/api/api-limits.html |
130
|
|
|
//To optimize your API usage, get maximum 200 records with each request and insert, update or delete maximum 100 records with each request. |
131
|
|
|
$offsetTodo = 0; |
132
|
|
|
$doneBeans = 0; |
133
|
|
|
while($doneBeans < $countItems){ |
134
|
|
|
$zohoBeansToSend = array_slice($zohoBeans,$offsetTodo,$this->apiLimitInsertUpdateDelete,true); |
135
|
|
|
$finalRowsDeleted = array_keys($zohoBeansToSend); |
136
|
|
|
$this->sendDataToZohoCleanLocal($zohoDao,$zohoBeans,$finalRowsDeleted,$update); |
137
|
|
|
$offsetTodo += $this->apiLimitInsertUpdateDelete; |
138
|
|
|
$doneBeans += count($zohoBeansToSend); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param AbstractZohoDao $zohoDao |
144
|
|
|
* @param ZohoBeanInterface[] $zohoBeans |
145
|
|
|
* @param string[] $rowsDeleted |
146
|
|
|
* @param bool $update |
147
|
|
|
*/ |
148
|
|
|
private function sendDataToZohoCleanLocal(AbstractZohoDao $zohoDao, array $zohoBeans,$rowsDeleted, $update = false) |
149
|
|
|
{ |
150
|
|
|
$tableName = ZohoDatabaseHelper::getTableName($zohoDao, $this->prefix); |
151
|
|
|
$zohoDao->save($zohoBeans); |
152
|
|
|
if (!$update) { |
153
|
|
|
foreach ($zohoBeans as $uid => $zohoBean) { |
154
|
|
|
$countResult = (int) $this->connection->fetchColumn('select count(id) from '.$tableName.' where id = :id', ['id'=>$zohoBean->getZohoId()]); |
155
|
|
|
//If the sent data were duplicates Zoho can merged so we need to check if the Zoho ID already exist. |
156
|
|
|
if ($countResult === 0) { |
157
|
|
|
// ID not exist we can update the new row with the Zoho ID |
158
|
|
|
$this->connection->beginTransaction(); |
159
|
|
|
$this->connection->update($tableName, ['id' => $zohoBean->getZohoId()], ['uid' => $uid]); |
160
|
|
|
$this->connection->delete('local_insert', ['table_name'=>$tableName, 'uid' => $uid ]); |
161
|
|
|
$this->connection->commit(); |
162
|
|
|
} else { |
163
|
|
|
//ID already exist we need to delete the duplicate row. |
164
|
|
|
$this->connection->beginTransaction(); |
165
|
|
|
$this->connection->delete($tableName, ['uid' => $uid ]); |
166
|
|
|
$this->connection->delete('local_insert', ['table_name'=>$tableName, 'uid' => $uid ]); |
167
|
|
|
$this->connection->commit(); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
} else { |
171
|
|
|
$this->connection->executeUpdate('delete from local_update where uid in ( :rowsDeleted)', |
172
|
|
|
[ |
173
|
|
|
'rowsDeleted' => $rowsDeleted, |
174
|
|
|
], |
175
|
|
|
[ |
176
|
|
|
'rowsDeleted' => Connection::PARAM_INT_ARRAY, |
177
|
|
|
] |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Insert data to bean in order to insert zoho records. |
184
|
|
|
* |
185
|
|
|
* @param ZohoBeanInterface $zohoBean |
186
|
|
|
* @param array $fieldsMatching |
187
|
|
|
* @param array $row |
188
|
|
|
*/ |
189
|
|
View Code Duplication |
private function insertDataZohoBean(ZohoBeanInterface $zohoBean, array $fieldsMatching, array $row) |
|
|
|
|
190
|
|
|
{ |
191
|
|
|
foreach ($row as $columnName => $columnValue) { |
192
|
|
|
if ((!in_array($columnName, ['id', 'uid']) || isset($fieldsMatching[$columnName])) && !is_null($columnValue)) { |
193
|
|
|
$type = $fieldsMatching[$columnName]['type']; |
194
|
|
|
$value = $this->formatValueToBeans($type, $columnValue); |
195
|
|
|
$zohoBean->{$fieldsMatching[$columnName]['setter']}($value); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Insert data to bean in order to update zoho records. |
202
|
|
|
* |
203
|
|
|
* @param ZohoBeanInterface $zohoBean |
204
|
|
|
* @param array $fieldsMatching |
205
|
|
|
* @param type $columnName |
206
|
|
|
* @param type $valueDb |
207
|
|
|
*/ |
208
|
|
View Code Duplication |
private function updateDataZohoBean(ZohoBeanInterface $zohoBean, array $fieldsMatching, $columnName, $valueDb) |
|
|
|
|
209
|
|
|
{ |
210
|
|
|
if (!in_array($columnName, ['id', 'uid']) || (isset($fieldsMatching[$columnName]))) { |
211
|
|
|
$type = $fieldsMatching[$columnName]['type']; |
212
|
|
|
$value = is_null($valueDb) ? $valueDb : $this->formatValueToBeans($type, $valueDb); |
213
|
|
|
$zohoBean->{$fieldsMatching[$columnName]['setter']}($value); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Change the value to the good format. |
219
|
|
|
* |
220
|
|
|
* @param string $type |
221
|
|
|
* @param mixed $value |
222
|
|
|
* |
223
|
|
|
* @return mixed |
224
|
|
|
*/ |
225
|
|
|
private function formatValueToBeans($type, $value) |
226
|
|
|
{ |
227
|
|
|
switch ($type) { |
228
|
|
|
case 'Date': |
229
|
|
|
$value = \DateTime::createFromFormat('Y-m-d', $value); |
230
|
|
|
break; |
231
|
|
|
case 'DateTime': |
232
|
|
|
$value = \DateTime::createFromFormat('Y-m-d H:i:s', $value); |
233
|
|
|
break; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return $value; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Run deleted rows to Zoho : local_delete. |
241
|
|
|
* |
242
|
|
|
* @param AbstractZohoDao $zohoDao |
243
|
|
|
*/ |
244
|
|
|
public function pushDeletedRows(AbstractZohoDao $zohoDao) |
245
|
|
|
{ |
246
|
|
|
$localTable = 'local_delete'; |
247
|
|
|
$tableName = ZohoDatabaseHelper::getTableName($zohoDao, $this->prefix); |
248
|
|
|
$statement = $this->connection->createQueryBuilder(); |
249
|
|
|
$statement->select('l.id') |
250
|
|
|
->from($localTable, 'l') |
251
|
|
|
->where('l.table_name=:table_name') |
252
|
|
|
->setParameters([ |
253
|
|
|
'table_name' => $tableName, |
254
|
|
|
]); |
255
|
|
|
$results = $statement->execute(); |
256
|
|
|
while ($row = $results->fetch()) { |
257
|
|
|
$zohoDao->delete($row['id']); |
258
|
|
|
$this->connection->delete($localTable, ['table_name' => $tableName, 'id' => $row['id']]); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Run inserted rows to Zoho : local_insert. |
264
|
|
|
* |
265
|
|
|
* @param AbstractZohoDao $zohoDao |
266
|
|
|
*/ |
267
|
|
|
public function pushInsertedRows(AbstractZohoDao $zohoDao) |
268
|
|
|
{ |
269
|
|
|
return $this->pushDataToZoho($zohoDao); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Run updated rows to Zoho : local_update. |
274
|
|
|
* |
275
|
|
|
* @param AbstractZohoDao $zohoDao |
276
|
|
|
*/ |
277
|
|
|
public function pushUpdatedRows(AbstractZohoDao $zohoDao) |
278
|
|
|
{ |
279
|
|
|
$this->pushDataToZoho($zohoDao, true); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Push data from db to Zoho. |
284
|
|
|
* |
285
|
|
|
* @param AbstractZohoDao $zohoDao |
286
|
|
|
*/ |
287
|
|
|
public function pushToZoho(AbstractZohoDao $zohoDao) |
288
|
|
|
{ |
289
|
|
|
$this->logger->info(' > Insert new rows using {class_name}', ['class_name' => get_class($zohoDao)]); |
290
|
|
|
$this->pushInsertedRows($zohoDao); |
291
|
|
|
$this->logger->info(' > Update rows using {class_name}', ['class_name' => get_class($zohoDao)]); |
292
|
|
|
$this->pushUpdatedRows($zohoDao); |
293
|
|
|
$this->logger->info(' > Delete rows using {class_name}', ['class_name' => get_class($zohoDao)]); |
294
|
|
|
$this->pushDeletedRows($zohoDao); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
This check looks for access to methods that are not accessible from the current context.
If you need to make a method accessible to another context you can raise its visibility level in the defining class.