|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Query Manager |
|
4
|
|
|
* Copyright (c) Webmatch GmbH |
|
5
|
|
|
* |
|
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU General Public License as published by |
|
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* This program is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU General Public License for more details. |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
use WbmQueryManager\Models\Query; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Shopware_Controllers_Backend_WbmQueryManager |
|
21
|
|
|
*/ |
|
22
|
|
|
class Shopware_Controllers_Backend_WbmQueryManager extends Shopware_Controllers_Backend_ExtJs { |
|
23
|
|
|
|
|
24
|
|
|
public function postDispatch() |
|
25
|
|
|
{ |
|
26
|
|
|
if ( |
|
27
|
|
|
$this->Request()->getActionName() === 'load' && |
|
28
|
|
|
$this->container->get('config')->getByNamespace('WbmQueryManager', 'autocomplete') |
|
29
|
|
|
) { |
|
30
|
|
|
$dbData = $this->container->getParameter('shopware.db'); |
|
31
|
|
|
$sql = "SELECT TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ?"; |
|
32
|
|
|
$tables = $this->container->get('shopware.db')->fetchAll( |
|
33
|
|
|
$sql, |
|
34
|
|
|
array($dbData['dbname']), \Zend_Db::FETCH_GROUP|\Zend_Db::FETCH_COLUMN |
|
35
|
|
|
); |
|
36
|
|
|
$columns = []; |
|
37
|
|
|
foreach ($tables as $table => $content) { |
|
38
|
|
|
foreach ($content as $column) { |
|
39
|
|
|
$columns[] = array( |
|
40
|
|
|
'caption' => $table . '.' . $column, |
|
41
|
|
|
'value' => '`' . $table . '`.`' . $column . '`', |
|
42
|
|
|
'meta' => 'Column' |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
$hintOptions = json_encode( |
|
47
|
|
|
array_merge( |
|
48
|
|
|
array_map( |
|
49
|
|
|
function($tableName) { |
|
50
|
|
|
return array( |
|
51
|
|
|
'caption' => $tableName, |
|
52
|
|
|
'value' => '`' . $tableName . '`', |
|
53
|
|
|
'meta' => 'Table' |
|
54
|
|
|
); |
|
55
|
|
|
}, |
|
56
|
|
|
array_keys($tables) |
|
57
|
|
|
), |
|
58
|
|
|
$columns |
|
59
|
|
|
) |
|
60
|
|
|
); |
|
61
|
|
|
$this->View()->hintOptions = $hintOptions; |
|
62
|
|
|
$this->View()->autocompleteActive = true; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function indexAction() |
|
67
|
|
|
{ |
|
68
|
|
|
$this->View()->loadTemplate("backend/wbm_query_manager/app.js"); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function listAction() |
|
72
|
|
|
{ |
|
73
|
|
|
$id = $this->Request()->getParam('id', null); |
|
74
|
|
|
|
|
75
|
|
|
/** @var \Doctrine\ORM\QueryBuilder $qb */ |
|
76
|
|
|
$qb = $this->container->get('models')->createQueryBuilder(); |
|
77
|
|
|
$qb->select( |
|
78
|
|
|
array( |
|
79
|
|
|
'query' |
|
80
|
|
|
) |
|
81
|
|
|
) |
|
82
|
|
|
->from('WbmQueryManager\Models\Query', 'query'); |
|
83
|
|
|
|
|
84
|
|
|
if(!empty($id)){ |
|
85
|
|
|
$qb->where('query.id = :id') |
|
86
|
|
|
->setParameter(':id', $id); |
|
87
|
|
|
} else { |
|
88
|
|
|
$qb->addOrderBy('query.name', 'ASC'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$data = $qb->getQuery()->getArrayResult(); |
|
92
|
|
|
|
|
93
|
|
|
$this->View()->assign( |
|
94
|
|
|
array('success' => true, 'data' => $data) |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function createAction() |
|
99
|
|
|
{ |
|
100
|
|
|
$params = $this->Request()->getPost(); |
|
101
|
|
|
|
|
102
|
|
|
if($params['hasCronjob'] && empty($params['nextRun'])){ |
|
103
|
|
|
$params['nextRun'] = new \DateTime(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$query = new Query(); |
|
107
|
|
|
$query->fromArray($params); |
|
108
|
|
|
|
|
109
|
|
|
$this->container->get('models')->persist($query); |
|
110
|
|
|
$this->container->get('models')->flush(); |
|
111
|
|
|
|
|
112
|
|
|
$this->View()->assign( |
|
113
|
|
|
array( |
|
114
|
|
|
'success' => true, |
|
115
|
|
|
'id' => $query->getId() |
|
116
|
|
|
) |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function updateAction() |
|
121
|
|
|
{ |
|
122
|
|
|
$params = $this->Request()->getPost(); |
|
123
|
|
|
$id = (int)$this->Request()->get('id'); |
|
124
|
|
|
|
|
125
|
|
|
if($params['hasCronjob'] && empty($params['nextRun'])){ |
|
126
|
|
|
$params['nextRun'] = new \DateTime(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** @var Query $query */ |
|
130
|
|
|
$query = $this->container->get('models')->getRepository('WbmQueryManager\Models\Query')->find($id); |
|
131
|
|
|
$query->fromArray($params); |
|
132
|
|
|
|
|
133
|
|
|
$this->container->get('models')->persist($query); |
|
134
|
|
|
$this->container->get('models')->flush(); |
|
135
|
|
|
|
|
136
|
|
|
$this->View()->assign( |
|
137
|
|
|
array('success' => true) |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function deleteAction() |
|
142
|
|
|
{ |
|
143
|
|
|
$id = (int)$this->Request()->get('id'); |
|
144
|
|
|
|
|
145
|
|
|
/** @var Query $query */ |
|
146
|
|
|
$query = $this->container->get('models')->getRepository('WbmQueryManager\Models\Query')->find($id); |
|
147
|
|
|
|
|
148
|
|
|
$this->container->get('models')->remove($query); |
|
149
|
|
|
$this->container->get('models')->flush(); |
|
150
|
|
|
|
|
151
|
|
|
$this->View()->assign( |
|
152
|
|
|
array('success' => true) |
|
153
|
|
|
); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function cloneAction() |
|
157
|
|
|
{ |
|
158
|
|
|
$id = (int)$this->Request()->get('id'); |
|
159
|
|
|
|
|
160
|
|
|
/** @var Query $query */ |
|
161
|
|
|
$query = $this->container->get('models')->getRepository('WbmQueryManager\Models\Query')->find($id); |
|
162
|
|
|
|
|
163
|
|
|
$query = clone $query; |
|
164
|
|
|
$query->setId(null); |
|
165
|
|
|
$query->setName('(Clone) ' . $query->getName()); |
|
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
$this->container->get('models')->persist($query); |
|
168
|
|
|
$this->container->get('models')->flush(); |
|
169
|
|
|
|
|
170
|
|
|
$this->View()->assign( |
|
171
|
|
|
array('success' => true) |
|
172
|
|
|
); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public function runAction() |
|
176
|
|
|
{ |
|
177
|
|
|
$sql = $this->Request()->get('query'); |
|
178
|
|
|
$download = $this->Request()->get('download'); |
|
179
|
|
|
$rowsetKey = $this->Request()->get('rowset'); |
|
180
|
|
|
|
|
181
|
|
|
try { |
|
182
|
|
|
/** @var \mysqli|\Zend_Db_Statement_Pdo $query */ |
|
183
|
|
|
$query = $this->container->get('wbm_query_manager.db')->query($sql); |
|
184
|
|
|
$data = array(); |
|
185
|
|
|
$i = 0; |
|
186
|
|
|
|
|
187
|
|
|
do { |
|
188
|
|
|
$data[$i]['rowsetKey'] = $i; |
|
189
|
|
|
|
|
190
|
|
|
if($this->container->get('wbm_query_manager.db')->getColumnCount($query)){ |
|
191
|
|
|
$records = $this->container->get('wbm_query_manager.db')->fetchAll($query); |
|
192
|
|
|
$data[$i]['rowCount'] = $this->container->get('wbm_query_manager.db')->getRowCount($query); |
|
193
|
|
|
|
|
194
|
|
|
if($download && $rowsetKey != $i){ |
|
195
|
|
|
$i++; |
|
196
|
|
|
continue; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
$recordFields = array_keys($records[0]); |
|
200
|
|
|
$columns = array(); |
|
201
|
|
|
foreach($recordFields as $recordField){ |
|
202
|
|
|
$columns[] = array( |
|
203
|
|
|
'header' => $recordField, |
|
204
|
|
|
'dataIndex' => $recordField, |
|
205
|
|
|
'flex' => 1 |
|
206
|
|
|
); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
$data[$i]['fetchData'] = array( |
|
210
|
|
|
'records' => $records, |
|
211
|
|
|
'recordFields' => $recordFields, |
|
212
|
|
|
'columns' => $columns |
|
213
|
|
|
); |
|
214
|
|
|
|
|
215
|
|
|
if($download){ |
|
216
|
|
|
$this->container->get('plugins')->Controller()->ViewRenderer()->setNoRender(); |
|
217
|
|
|
$now = new \DateTime(); |
|
218
|
|
|
$file = "query_" . $now->format('Y_m_d_h_i_s') . ".csv"; |
|
219
|
|
|
header("Content-Type: text/csv"); |
|
220
|
|
|
header("Content-Disposition: attachment; filename=\"$file\""); |
|
221
|
|
|
|
|
222
|
|
|
$outputBuffer = fopen("php://output", 'w'); |
|
223
|
|
View Code Duplication |
foreach(array_merge(array(0 => $recordFields),$records) as $val) { |
|
|
|
|
|
|
224
|
|
|
fputcsv($outputBuffer, $val, $this->container->get('config')->getByNamespace('WbmQueryManager', 'csv_field_separator')); |
|
225
|
|
|
} |
|
226
|
|
|
fclose($outputBuffer); |
|
227
|
|
|
|
|
228
|
|
|
exit(); |
|
229
|
|
|
} |
|
230
|
|
|
} else { |
|
231
|
|
|
$data[$i]['rowCount'] = $this->container->get('wbm_query_manager.db')->getRowCount($query); |
|
232
|
|
|
$data[$i]['fetchData'] = null; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
$i++; |
|
236
|
|
|
} while ($this->container->get('wbm_query_manager.db')->nextResult($query)); |
|
237
|
|
|
|
|
238
|
|
|
$this->container->get('wbm_query_manager.db')->close($query); |
|
239
|
|
|
|
|
240
|
|
|
$this->View()->assign( |
|
241
|
|
|
array('success' => true, 'data' => array_reverse($data)) |
|
242
|
|
|
); |
|
243
|
|
|
} catch (Exception $e) { |
|
244
|
|
|
$this->View()->assign( |
|
245
|
|
|
array('success' => false, 'data' => $e->getMessage()) |
|
246
|
|
|
); |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
|