-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEngine.php
186 lines (163 loc) · 4.84 KB
/
Engine.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
namespace Acms\Plugins\GoogleSheets;
use Field;
use Google_Service_Sheets;
use Google_Service_Sheets_Request;
use Google_Service_Sheets_RowData;
use Google_Service_Sheets_AppendCellsRequest;
use Google_Service_Sheets_BatchUpdateSpreadsheetRequest;
use Google_Service_Sheets_CellData;
use Google_Service_Sheets_ExtendedValue;
class Engine
{
/**
* @var \ACMS_POST
*/
protected $module;
/**
* @var string
*/
protected $code;
/**
* @var \Field
*/
protected $config;
/**
* @var string
*/
protected $glue;
/**
* Engine constructor.
* @param string $code
*/
public function __construct($code, $module)
{
$info = $module->loadForm($code);
if (empty($info)) {
throw new \RuntimeException('Not Found Form.');
}
$this->module = $module;
$this->code = $code;
$this->config = $info['data']->getChild('mail');
$this->glue = $this->config->get('cell_glue', ',');
}
/**
* Update Google Sheets
*/
public function send()
{
$field = $this->module->Post->getChild('field');
$checkItems = [
'formId' => $this->config->get('spreadsheet_submit_formid'),
'time' => $this->config->get('spreadsheet_submit_date'),
'url' => $this->config->get('spreadsheet_submit_url'),
'ipAddr' => $this->config->get('spreadsheet_submit_ip'),
'ua' => $this->config->get('spreadsheet_submit_agent'),
];
$values = [];
foreach ($checkItems as $item => $check) {
if ($check !== 'true') {
continue;
}
$method = 'get' . ucwords($item);
if (is_callable([$this, $method])) {
$values[] = $this->$method();
}
}
foreach ($field->_aryField as $key => $val) {
$values[] = $this->getCellData($field->getArray($key), $this->glue);
}
if ($this->config->get('spreadsheet_id')) {
$this->update($values);
}
}
/**
* Send Google Sheets Api
*
* @param array $values
*/
protected function update($values)
{
$client = (new Api())->getClient();
if (!$client->getAccessToken()) {
throw new \RuntimeException('Failed to get the access token.');
}
$service = new Google_Service_Sheets($client);
$spreadsheetId = $this->config->get('spreadsheet_id');
$spreadsheetGid = $this->config->get('sheet_id', 0);
// Build the RowData
$rowData = new Google_Service_Sheets_RowData();
$rowData->setValues($values);
// Prepare the request
$append_request = new Google_Service_Sheets_AppendCellsRequest();
$append_request->setSheetId($spreadsheetGid);
$append_request->setRows($rowData);
$append_request->setFields('userEnteredValue');
// Set the request
$request = new Google_Service_Sheets_Request();
$request->setAppendCells($append_request);
// Add the request to the requests array
$requests = [];
$requests[] = $request;
// Prepare the update
$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
'requests' => $requests
]);
// Execute the request
$response = $service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest);
if (!$response->valid()) {
throw new \RuntimeException('Failed to update the sheet.');
}
}
/**
* @param array|string $value
* @param string $glue
* @return \Google_Service_Sheets_CellData
*/
private function getCellData($value, $glue = ',')
{
if (is_array($value)) {
$value = implode($glue, $value);
}
$cellData = new Google_Service_Sheets_CellData();
$extendedValue = new Google_Service_Sheets_ExtendedValue();
$extendedValue->setStringValue($value);
$cellData->setUserEnteredValue($extendedValue);
return $cellData;
}
/**
* @return \Google_Service_Sheets_CellData
*/
private function getTime()
{
return $this->getCellData(date('Y-m-d H:i:s', REQUEST_TIME));
}
/**
* @return \Google_Service_Sheets_CellData
*/
private function getFormId()
{
return $this->getCellData($this->code);
}
/**
* @return \Google_Service_Sheets_CellData
*/
private function getUrl()
{
return $this->getCellData(REQUEST_URL);
}
/**
* @return \Google_Service_Sheets_CellData
*/
private function getIpAddr()
{
return $this->getCellData(REMOTE_ADDR);
}
/**
* @return \Google_Service_Sheets_CellData
*/
private function getUa()
{
return $this->getCellData(UA);
}
}