|
1
|
|
|
<?php namespace SimpleTab; |
|
2
|
|
|
|
|
3
|
|
|
require_once(MODX_BASE_PATH . 'assets/lib/MODxAPI/autoTable.abstract.php'); |
|
4
|
|
|
require_once(MODX_BASE_PATH . 'assets/lib/Helpers/FS.php'); |
|
5
|
|
|
require_once(MODX_BASE_PATH . 'assets/lib/Helpers/PHPThumb.php'); |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class dataTable |
|
9
|
|
|
* @package SimpleTab |
|
10
|
|
|
*/ |
|
11
|
|
|
class dataTable extends \autoTable |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $params = array(); |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var \Helpers\FS|null |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $fs = null; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var null |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $indexName = null; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var null |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $rfName = null; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var null |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $thumbsCache = null; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* dataTable constructor. |
|
40
|
|
|
* @param \DocumentParser $modx |
|
41
|
|
|
* @param bool $debug |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct($modx, $debug = false) |
|
44
|
|
|
{ |
|
45
|
|
|
parent::__construct($modx, $debug); |
|
46
|
|
|
$this->modx = $modx; |
|
47
|
|
|
$this->fs = \Helpers\FS::getInstance(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param $ids |
|
52
|
|
|
* @param $rid |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function clearIndexes($ids, $rid) |
|
55
|
|
|
{ |
|
56
|
|
|
$ids = $this->cleanIDs($ids, ',', array(0)); |
|
57
|
|
|
$ids = $this->sanitarIn($ids); |
|
58
|
|
|
$table = $this->makeTable($this->table); |
|
59
|
|
|
$rows = $this->query("SELECT MIN(`{$this->indexName}`) FROM {$table} WHERE `{$this->pkName}` IN ({$ids})"); |
|
60
|
|
|
$index = $this->modx->db->getValue($rows); |
|
61
|
|
|
$index = $index - 1; |
|
62
|
|
|
$this->query("SET @index := " . $index); |
|
63
|
|
|
$this->query("UPDATE {$table} SET `{$this->indexName}` = (@index := @index + 1) WHERE (`{$this->indexName}`>{$index} AND `{$this->rfName}`={$rid} AND `{$this->pkName}` NOT IN ({$ids})) ORDER BY `{$this->indexName}` ASC"); |
|
64
|
|
|
$out = $this->modx->db->getAffectedRows(); |
|
65
|
|
|
|
|
66
|
|
|
return $out; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param $field |
|
71
|
|
|
* @return $this |
|
72
|
|
|
*/ |
|
73
|
|
|
public function touch($field) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->set($field, date('Y-m-d H:i:s', time() + $this->modx->config['server_offset_time'])); |
|
76
|
|
|
|
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param $ids |
|
82
|
|
|
* @param $dir |
|
83
|
|
|
* @param $rid |
|
84
|
|
|
*/ |
|
85
|
|
|
public function place($ids, $dir, $rid) |
|
86
|
|
|
{ |
|
87
|
|
|
$table = $this->makeTable($this->table); |
|
88
|
|
|
$ids = $this->cleanIDs($ids, ',', array(0)); |
|
89
|
|
|
if (empty($ids) || is_scalar($ids)) { |
|
90
|
|
|
return false; |
|
91
|
|
|
} |
|
92
|
|
|
$rows = $this->query("SELECT count(`{$this->pkName}`) FROM {$table} WHERE `{$this->rfName}`={$rid}"); |
|
93
|
|
|
$index = $this->modx->db->getValue($rows); |
|
94
|
|
|
$cnt = count($ids); |
|
95
|
|
|
$ids = implode(',', $ids); |
|
96
|
|
|
if ($dir == 'top') { |
|
97
|
|
|
$this->query("SET @index := " . ($index - $cnt - 1)); |
|
98
|
|
|
$this->query("UPDATE {$table} SET `{$this->indexName}` = (@index := @index + 1) WHERE (`{$this->pkName}` IN ({$ids})) ORDER BY `{$this->indexName}` ASC"); |
|
99
|
|
|
$this->query("SET @index := -1"); |
|
100
|
|
|
} else { |
|
101
|
|
|
$this->query("SET @index := -1"); |
|
102
|
|
|
$this->query("UPDATE {$table} SET `{$this->indexName}` = (@index := @index + 1) WHERE (`{$this->pkName}` IN ({$ids})) ORDER BY `{$this->indexName}` ASC"); |
|
103
|
|
|
$this->query("SET @index := " . ($cnt - 1)); |
|
104
|
|
|
} |
|
105
|
|
|
$this->query("UPDATE {$table} SET `{$this->indexName}` = (@index := @index + 1) WHERE (`{$this->pkName}` NOT IN ({$ids})) AND `{$this->rfName}` = {$rid} ORDER BY `{$this->indexName}` ASC"); |
|
106
|
|
|
$out = $this->modx->db->getAffectedRows(); |
|
107
|
|
|
|
|
108
|
|
|
return $out; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param $url |
|
113
|
|
|
* @param bool $cache |
|
114
|
|
|
*/ |
|
115
|
|
|
public function deleteThumb($url, $cache = false) |
|
116
|
|
|
{ |
|
117
|
|
|
$url = $this->fs->relativePath($url); |
|
118
|
|
|
if (empty($url)) { |
|
119
|
|
|
return; |
|
120
|
|
|
} |
|
121
|
|
|
if ($this->fs->checkFile($url)) { |
|
122
|
|
|
unlink(MODX_BASE_PATH . $url); |
|
123
|
|
|
} |
|
124
|
|
|
$dir = $this->fs->takeFileDir($url); |
|
125
|
|
|
$iterator = new \FilesystemIterator($dir); |
|
126
|
|
|
if (!$iterator->valid()) { |
|
127
|
|
|
rmdir($dir); |
|
128
|
|
|
} |
|
129
|
|
|
if ($cache) { |
|
130
|
|
|
return; |
|
131
|
|
|
} |
|
132
|
|
|
$thumbsCache = \APIhelpers::getkey($this->params,'thumbsCache',$this->thumbsCache); |
|
133
|
|
|
$thumb = $thumbsCache . $url; |
|
134
|
|
|
if ($this->fs->checkFile($thumb)) { |
|
135
|
|
|
$this->deleteThumb($thumb, true); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param $ids |
|
141
|
|
|
* @param null $fire_events |
|
142
|
|
|
* @return $this |
|
143
|
|
|
*/ |
|
144
|
|
|
public function delete($ids, $fire_events = false) |
|
145
|
|
|
{ |
|
146
|
|
|
$out = parent::delete($ids, $fire_events); |
|
|
|
|
|
|
147
|
|
|
$this->query("ALTER TABLE {$this->makeTable($this->table)} AUTO_INCREMENT = 1"); |
|
148
|
|
|
|
|
149
|
|
|
return $out; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param $ids |
|
154
|
|
|
* @param $rid |
|
155
|
|
|
* @param null $fire_events |
|
156
|
|
|
* @return $this |
|
157
|
|
|
*/ |
|
158
|
|
|
public function deleteAll($ids, $rid, $fire_events = false) |
|
159
|
|
|
{ |
|
160
|
|
|
$this->clearIndexes($ids, $rid); |
|
161
|
|
|
|
|
162
|
|
|
return $this->delete($ids, $fire_events); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @return array |
|
167
|
|
|
*/ |
|
168
|
|
|
public function fieldNames() |
|
169
|
|
|
{ |
|
170
|
|
|
$fields = array_keys($this->getDefaultFields()); |
|
171
|
|
|
$fields[] = $this->fieldPKName(); |
|
172
|
|
|
|
|
173
|
|
|
return $fields; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @param string $name |
|
178
|
|
|
* @return string |
|
179
|
|
|
*/ |
|
180
|
|
|
public function stripName($name) |
|
181
|
|
|
{ |
|
182
|
|
|
|
|
183
|
|
|
$filename = $this->fs->takeFileName($name); |
|
184
|
|
|
$ext = $this->fs->takeFileExt($name); |
|
185
|
|
|
|
|
186
|
|
|
return $this->modx->stripAlias($filename) . '.' . $ext; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param $source |
|
191
|
|
|
* @param $target |
|
192
|
|
|
* @param $point |
|
193
|
|
|
* @param $rid |
|
194
|
|
|
* @param $orderDir |
|
195
|
|
|
* @return int|void |
|
196
|
|
|
*/ |
|
197
|
|
|
public function reorder($source, $target, $point, $rid, $orderDir) |
|
198
|
|
|
{ |
|
199
|
|
|
$rid = (int)$rid; |
|
200
|
|
|
$point = strtolower($point); |
|
201
|
|
|
$orderDir = strtolower($orderDir); |
|
202
|
|
|
$sourceIndex = (int)$source[$this->indexName]; |
|
203
|
|
|
$targetIndex = (int)$target[$this->indexName]; |
|
204
|
|
|
$sourceId = (int)$source[$this->pkName]; |
|
205
|
|
|
$table = $this->makeTable($this->table); |
|
206
|
|
|
$rows = 0; |
|
207
|
|
|
/* more refactoring needed */ |
|
208
|
|
|
if ($targetIndex < $sourceIndex) { |
|
209
|
|
|
if (($point == 'top' && $orderDir == 'asc') || ($point == 'bottom' && $orderDir == 'desc')) { |
|
210
|
|
|
$this->modx->db->update("`{$this->indexName}`=`{$this->indexName}`+1", $table, |
|
211
|
|
|
"`{$this->indexName}`>={$targetIndex} AND `{$this->indexName}`<{$sourceIndex} AND `{$this->rfName}`={$rid}"); |
|
212
|
|
|
$rows = $this->modx->db->update("`{$this->indexName}`={$targetIndex}", $table, |
|
213
|
|
|
"`{$this->pkName}`={$sourceId}"); |
|
214
|
|
|
} elseif (($point == 'bottom' && $orderDir == 'asc') || ($point == 'top' && $orderDir == 'desc')) { |
|
215
|
|
|
$this->modx->db->update("`{$this->indexName}`=`{$this->indexName}`+1", $table, |
|
216
|
|
|
"`{$this->indexName}`>{$targetIndex} AND `{$this->indexName}`<{$sourceIndex} AND `{$this->rfName}`={$rid}"); |
|
217
|
|
|
$rows = $this->modx->db->update("`{$this->indexName}`=1+{$targetIndex}", $table, |
|
218
|
|
|
"`{$this->pkName}`={$sourceId}"); |
|
219
|
|
|
} |
|
220
|
|
|
} else { |
|
221
|
|
|
if (($point == 'bottom' && $orderDir == 'asc') || ($point == 'top' && $orderDir == 'desc')) { |
|
222
|
|
|
$this->modx->db->update("`{$this->indexName}`=`{$this->indexName}`-1", $table, |
|
223
|
|
|
"`{$this->indexName}`<={$targetIndex} AND `{$this->indexName}`>={$sourceIndex} AND `{$this->rfName}`={$rid}"); |
|
224
|
|
|
$rows = $this->modx->db->update("`{$this->indexName}`={$targetIndex}", $table, |
|
225
|
|
|
"`{$this->pkName}`={$sourceId}"); |
|
226
|
|
|
} elseif (($point == 'top' && $orderDir == 'asc') || ($point == 'bottom' && $orderDir == 'desc')) { |
|
227
|
|
|
$this->modx->db->update("`{$this->indexName}`=`{$this->indexName}`-1", $table, |
|
228
|
|
|
"`{$this->indexName}`<{$targetIndex} AND `{$this->indexName}`>={$sourceIndex} AND `{$this->rfName}`={$rid}"); |
|
229
|
|
|
$rows = $this->modx->db->update("`{$this->indexName}`=-1+{$targetIndex}", $table, |
|
230
|
|
|
"`{$this->pkName}`={$sourceId}"); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
return $rows; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @param $folder |
|
239
|
|
|
* @param $url |
|
240
|
|
|
* @param $options |
|
241
|
|
|
* @return bool |
|
242
|
|
|
*/ |
|
243
|
|
|
public function makeThumb($folder, $url, $options) |
|
244
|
|
|
{ |
|
245
|
|
|
if (empty($url)) { |
|
246
|
|
|
return false; |
|
247
|
|
|
} |
|
248
|
|
|
$thumb = new \Helpers\PHPThumb(); |
|
249
|
|
|
$inputFile = MODX_BASE_PATH . $this->fs->relativePath($url); |
|
250
|
|
|
$outputFile = MODX_BASE_PATH . $this->fs->relativePath($folder) . '/' . $this->fs->relativePath($url); |
|
251
|
|
|
$dir = $this->fs->takeFileDir($outputFile); |
|
252
|
|
|
$this->fs->makeDir($dir, $this->modx->config['new_folder_permissions']); |
|
253
|
|
|
if ($thumb->create($inputFile, $outputFile, $options)) { |
|
254
|
|
|
return true; |
|
255
|
|
|
} else { |
|
256
|
|
|
$this->modx->logEvent(0, 3, $thumb->debugMessages, __NAMESPACE__); |
|
257
|
|
|
|
|
258
|
|
|
return false; |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* @return array |
|
264
|
|
|
*/ |
|
265
|
|
|
public function getParams() |
|
266
|
|
|
{ |
|
267
|
|
|
return $this->params; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* @param array $params |
|
272
|
|
|
*/ |
|
273
|
|
|
public function setParams($params = array()) |
|
274
|
|
|
{ |
|
275
|
|
|
if (is_array($params)) $this->params = $params; |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.