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
|
|
|
use Wabel\Zoho\CRM\Exception\ZohoCRMException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Description of ZohoDatabaseSyncZoho |
14
|
|
|
* |
15
|
|
|
* @author rbergina |
16
|
|
|
*/ |
17
|
|
|
class ZohoDatabaseSyncZoho |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Connection |
22
|
|
|
*/ |
23
|
|
|
private $connection; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var LoggerInterface |
27
|
|
|
*/ |
28
|
|
|
private $logger; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $prefix; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Connection $connection |
38
|
|
|
* @param string $prefix |
39
|
|
|
* @param LoggerInterface $logger |
40
|
|
|
*/ |
41
|
|
|
public function __construct(Connection $connection, $prefix = 'zoho_', LoggerInterface $logger = null) |
42
|
|
|
{ |
43
|
|
|
$this->connection = $connection; |
44
|
|
|
$this->prefix = $prefix; |
45
|
|
|
if ($logger === null) { |
46
|
|
|
$this->logger = new NullLogger(); |
47
|
|
|
} else { |
48
|
|
|
$this->logger = $logger; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* |
54
|
|
|
* @param AbstractZohoDao $zohoDao |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
private function findMethodValues(AbstractZohoDao $zohoDao){ |
58
|
|
|
$fieldsMatching = array(); |
59
|
|
|
foreach ($zohoDao->getFields() as $fieldsDescriptor) { |
|
|
|
|
60
|
|
|
foreach (array_values($fieldsDescriptor) as $fieldDescriptor) { |
61
|
|
|
$fieldsMatching[$fieldDescriptor['name']] = [ |
62
|
|
|
'setter' => $fieldDescriptor['setter'], |
63
|
|
|
'type' => $fieldDescriptor['type'] |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
return $fieldsMatching; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Insert or Update rows. |
73
|
|
|
* @param AbstractZohoDao $zohoDao |
74
|
|
|
* @param string $localTable |
75
|
|
|
*/ |
76
|
|
|
public function pushDataToZoho(AbstractZohoDao $zohoDao, $localTable, $update = false){ |
77
|
|
|
|
78
|
|
|
$fieldsMatching = $this->findMethodValues($zohoDao); |
79
|
|
|
$tableName = ZohoDatabaseHelper::getTableName($zohoDao,$this->prefix); |
80
|
|
|
$rowsDeleted = []; |
81
|
|
|
$statement = $this->connection->createQueryBuilder(); |
82
|
|
|
$statement->select('zcrm.*'); |
83
|
|
|
if($update){ |
84
|
|
|
$statement->addSelect('l.field_name as updated_fieldname'); |
85
|
|
|
} |
86
|
|
|
$statement->from($localTable, 'l') |
87
|
|
|
->join('l', $tableName, 'zcrm', 'zcrm.uid = l.uid') |
88
|
|
|
->where('l.table_name=:table_name') |
89
|
|
|
->setParameters([ |
90
|
|
|
'table_name' => $tableName |
91
|
|
|
]); |
92
|
|
|
$results = $statement->execute(); |
93
|
|
|
/* @var $zohoBeans ZohoBeanInterface[] */ |
94
|
|
|
$zohoBeans = array(); |
95
|
|
|
while ($row = $results->fetch()) { |
96
|
|
|
$beanClassName = $zohoDao->getBeanClassName(); |
|
|
|
|
97
|
|
|
/* @var $zohoBean ZohoBeanInterface */ |
98
|
|
|
if(isset($zohoBeans[$row['uid']])){ |
99
|
|
|
$zohoBean = $zohoBeans[$row['uid']]; |
100
|
|
|
}else{ |
101
|
|
|
$zohoBean = new $beanClassName(); |
102
|
|
|
} |
103
|
|
|
if(!$update){ |
104
|
|
|
foreach ($row as $columnName => $columnValue) { |
105
|
|
|
if (!in_array($columnName,['id','uid']) || isset($fieldsMatching[$columnName])) { |
106
|
|
|
$value = $this->formatValueToBeans($zohoDao->getModule(), $fieldsMatching, $columnName, $columnValue, null, $row['uid']); |
|
|
|
|
107
|
|
|
if($columnValue){ |
108
|
|
|
$zohoBean->{$fieldsMatching[$columnName]['setter']}($value); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
$zohoBeans[$row['uid']] = $zohoBean; |
113
|
|
|
$rowsDeleted[] = $row['uid']; |
114
|
|
|
} else{ |
115
|
|
|
$columnName = $row['updated_fieldname']; |
116
|
|
|
$zohoBean->setZohoId($row['id']); |
117
|
|
|
if (!in_array($columnName,['id','uid']) || isset($fieldsMatching[$columnName])) { |
118
|
|
|
$value = $this->formatValueToBeans($zohoDao->getModule(), $fieldsMatching, $columnName, $row[$columnName], $row['id']); |
|
|
|
|
119
|
|
|
$zohoBean->{$fieldsMatching[$columnName]['setter']}($value); |
120
|
|
|
$zohoBeans[$row['uid']] = $zohoBean; |
121
|
|
|
$rowsDeleted[] = $row['uid']; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
$zohoDao->save($zohoBeans); |
126
|
|
|
if(!$update){ |
127
|
|
|
foreach ($zohoBeans as $uid => $zohoBean) { |
128
|
|
|
$this->connection->update($tableName, [ 'id'=>$zohoBean->getZohoId(),'lastActivityTime'=> date("Y-m-d H:i:s") ], ['uid'=>$uid ]); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
$statementDelete = $this->connection->prepare('delete from '.$localTable.' where uid in ( :rowsDeleted)'); |
132
|
|
|
$statementDelete->execute([ |
133
|
|
|
'rowsDeleted' => implode(',', $rowsDeleted) |
134
|
|
|
]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Change the value to the good format. |
139
|
|
|
* @param string $moduleName |
140
|
|
|
* @param array $fieldsMatching |
141
|
|
|
* @param string $columnName |
142
|
|
|
* @param mixed $value |
143
|
|
|
* @param int $id |
144
|
|
|
* @return mixed |
145
|
|
|
* @throws ZohoCRMException |
146
|
|
|
*/ |
147
|
|
|
private function formatValueToBeans($moduleName, $fieldsMatching,$columnName,$value,$id=null, $uid = null) |
148
|
|
|
{ |
149
|
|
|
$idrecord = $id?$id.'[ZOHO]':$uid.'[UID]'; |
150
|
|
|
if(isset($fieldsMatching[$columnName]) && $value){ |
151
|
|
|
switch ($fieldsMatching[$columnName]['type']) { |
152
|
|
|
case 'Date': |
153
|
|
|
if ($dateObj = \DateTime::createFromFormat('M/d/Y', $value)) { |
154
|
|
|
$value = $dateObj; |
155
|
|
|
} elseif ($dateObj = \DateTime::createFromFormat('Y-m-d', $value)) { |
156
|
|
|
$value = $dateObj; |
157
|
|
|
} else { |
158
|
|
|
throw new ZohoCRMException('Unable to convert the Date field "'.$columnName."\" into a DateTime PHP object from the the record $idrecord of the module ".$moduleName.'.'); |
159
|
|
|
} |
160
|
|
|
break; |
161
|
|
|
case 'DateTime': |
162
|
|
|
$value = \DateTime::createFromFormat('Y-m-d H:i:s', $value); |
163
|
|
|
break; |
164
|
|
|
default: |
165
|
|
|
break; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
return $value; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Find the row to delete and remove to Zoho. |
173
|
|
|
* @param AbstractZohoDao $zohoDao |
174
|
|
|
* @param string $localTable |
175
|
|
|
*/ |
176
|
|
|
public function deleteDataToZoho(AbstractZohoDao $zohoDao, $localTable){ |
177
|
|
|
$tableName = ZohoDatabaseHelper::getTableName($zohoDao,$this->prefix); |
178
|
|
|
$statement = $this->connection->createQueryBuilder(); |
179
|
|
|
$statement->select('l.id') |
180
|
|
|
->from($localTable, 'l') |
181
|
|
|
->where('l.table_name=:table_name') |
182
|
|
|
->setParameters([ |
183
|
|
|
'table_name' => $tableName |
184
|
|
|
]); |
185
|
|
|
$results = $statement->execute(); |
186
|
|
|
while ($row = $results->fetch()) { |
187
|
|
|
$zohoDao->delete($row['id']); |
188
|
|
|
$this->connection->delete($localTable, ['table_name' => $tableName,'id' => $row['id']]); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Run inserted rows to Zoho : local_insert. |
194
|
|
|
* @param AbstractZohoDao $zohoDao |
195
|
|
|
*/ |
196
|
|
|
public function pushInsertedRows(AbstractZohoDao $zohoDao){ |
197
|
|
|
return $this->pushDataToZoho($zohoDao, 'local_insert'); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Run updated rows to Zoho : local_update. |
202
|
|
|
* @param AbstractZohoDao $zohoDao |
203
|
|
|
*/ |
204
|
|
|
public function pushUpdatedRows(AbstractZohoDao $zohoDao){ |
205
|
|
|
$this->pushDataToZoho($zohoDao, 'local_update', true); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Run deleted rows to Zoho : local_delete. |
210
|
|
|
* @param AbstractZohoDao $zohoDao |
211
|
|
|
*/ |
212
|
|
|
public function pushDeletedRows(AbstractZohoDao $zohoDao){ |
213
|
|
|
$this->deleteDataToZoho($zohoDao, 'local_delete'); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param LoggerInterface $logger |
218
|
|
|
*/ |
219
|
|
|
public function setLogger(LoggerInterface $logger) |
220
|
|
|
{ |
221
|
|
|
$this->logger = $logger; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
} |
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.