/
RockFinder3.module.php
562 lines (481 loc) · 14.5 KB
/
RockFinder3.module.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
<?php namespace ProcessWire;
/**
* Combine the power of ProcessWire selectors and SQL
*
* @author Bernhard Baumrock, 22.05.2020
* @license Licensed under MIT
* @link https://www.baumrock.com
*/
class RockFinder3 extends WireData implements Module {
public $name;
/** @var DatabaseQuerySelect */
public $query;
/** @var RockFinder3Master */
public $master;
/**
* Columns that are added to this finder
* @var WireArray
*/
public $columns;
/**
* Relations that are added to this finder
* @var WireArray
*/
public $relations;
/**
* Joins that are added to this finder
* @var WireArray
*/
public $joins;
/**
* Options that are added to this finder
* @var WireData
*/
public $options;
/** @var array */
private $rows;
public $selector;
/**
* Reference to the main finder (used by joined finders)
* @var RockFinder3
*/
public $main;
/** @var string */
private $joinColName;
/** @var bool */
private $removeJoinCol;
/** @var array */
private $limitRowsTo;
public static function getModuleInfo() {
return [
'title' => 'RockFinder3',
'version' => '1.0.0',
'summary' => 'Combine the power of ProcessWire selectors and SQL',
'autoload' => false,
'singular' => false,
'icon' => 'search',
'requires' => [
'RockFinder3Master',
],
'installs' => [], // the process module is optional
];
}
public function __construct() {
$this->name = uniqid();
$this->master = $this->modules->get('RockFinder3Master');
$this->columns = $this->wire(new WireArray);
$this->relations = $this->wire(new WireArray());
$this->joins = $this->wire(new WireArray());
$this->options = $this->wire(new WireData);
}
/** ########## CHAINABLE PUBLIC API METHODS ########## */
/**
* Add a single column by name
* @return RockFinder3
*/
public function addColumn($colname) {
return $this->addColumns([$colname]);
}
/**
* Add columns to finder
* @param array $columns
*/
public function addColumns($columns) {
if(!$this->query) throw new WireException("Setup the selector before calling addColumns()");
if(!is_array($columns)) throw new WireException("Parameter must be an array");
// add columns one by one
foreach($columns as $k=>$v) {
// skip null value columns
if($v === null) continue;
// if key is integer we take the value instead
if(is_int($k)) {
$k = $v;
$v = null;
}
// setup initial column name
$column = $k;
// if a type is set, get type
// syntax is type:column, eg addColumns(['mytype:myColumn'])
$type = null;
if(strpos($column, ":")) {
$arr = explode(":", $column);
$type = $arr[0];
$column = $arr[1];
}
// column name alias
$alias = $v;
// add this column
$this->_addColumn($column, $type, $alias);
}
return $this;
}
/**
* Add options from field
* @param array|string $field
* @return void
*/
public function addOptions($field) {
if(is_array($field)) {
foreach($field as $f) $this->addOptions($f);
return $this;
}
$fieldname = (string)$field;
$field = $this->fields->get($fieldname);
if(!$field) throw new WireException("Field $fieldname not found");
$data = [];
foreach($field->type->getOptions($field) as $option) {
$opt = $this->wire(new WireData()); /** @var WireData $opt */
$opt->value = $option->value;
$opt->title = $option->title;
$data[$option->id] = $opt;
}
$this->options->$fieldname = $data;
return $this;
}
/**
* Add relation to this finder
*
* @param RockFinder3 $relation
* @param bool $returnAll
* @return RockFinder3
*/
public function addRelation($relation, $returnAll = false) {
if(!$returnAll) $relation->limitRowsTo = $this;
if(!$this->columns->has($relation->name)) {
throw new WireException($relation->name . " not found: The name of your relation must exist as column in the main finder");
}
$this->relations->add($relation);
return $this;
}
/**
* Set selector of this finder
* @param string|array|DatabaseQuerySelect $selector
* @return RockFinder3
*/
public function find($selector) {
$this->selector = $selector;
// prepare the query selector property
if($selector instanceof DatabaseQuerySelect) {
$query = $selector;
}
else {
// get ids of base selector
$selector = $this->wire(new Selectors($selector));
$pf = $this->wire(new PageFinder());
$query = $pf->find($selector, ['returnQuery' => true]);
// modify the base query to our needs
// we only need the page id
// setting the alias via AS is necessary for hideColumns() feature
$query->set('select', ['`pages`.`id` AS `id`']);
}
// save this query object for later
$this->query = $query;
// support chaining
return $this;
}
/**
* Join slave finder to master finder
*
* @param RockFinder3 $slave
* @param array $options
* @return RockFinder3
*/
public function join($slave, $options = []) {
// check if a column with slave name exists
if(!$this->columns->has($slave->name)) {
throw new WireException($slave->name . " not found: The name of the finder to be joined must exist as column in the main finder");
}
// setup options
$opt = $this->wire(new WireData()); /** @var WireData $opt */
$opt->setArray([
'columns' => null, // null = join all columns, otherwise define an array
'removeID' => false, // dont remove the column used for the join
]);
$opt->setArray($options);
// create new join finder
/** @var RockFinder3 */
$join = $this->modules->get('RockFinder3');
$join->find($slave->selector);
$join->main = $this; // main finder
$join->joinColName = $slave->name; // colname for join
$join->removeJoinCol = $opt->removeID; // remove join base column?
$join->setName("join_{$slave->name}_".uniqid());
// add columns
if(!$opt->columns) $opt->columns = $slave->columns;
foreach($opt->columns as $colname) {
$col = $slave->columns->get($colname);
$join->addColumnType($col);
}
// apply join to this finder
$this->joins->add($join);
$this->applyJoin($join);
return $this;
}
/**
* Save this finder to the global array of finders
* The finder can then be joined by other finders etc.
*/
public function save($name = null) {
if($name) $this->setName($name);
$this->master->finders->add($this);
return $this;
}
/**
* Set name of this finder
* @return RockFinder3
*/
public function setName($name) {
$this->name = $name;
return $this;
}
/** ########## END CHAINABLE PUBLIC API METHODS ########## */
/** ########## GET DATA ########## */
/**
* Return options by name
* @return array
*/
public function getOptions($name) {
return $this->options->{$name};
}
/**
* Return option object by name and index
* @return WireData
*/
public function getOption($name, $index) {
return $this->getOptions($name)[$index];
}
/**
* Get plain row array ready for tabulator
* This returns only the array values without page-id-keys
* otherwise the resulting tabulator array on the client side is invalid.
* @return array
*/
public function getRowArray() {
return array_values($this->getRows());
}
/**
* Get object by its id property
* @param string|int $id
* @return stdClass
*/
public function getRowById($id) {
return $this->getRows()[(int)$id];
}
/**
* Get rows by id string
* @param string|array $ids
* @return array
*/
public function getRowsById($ids) {
$rows = [];
if(is_string($ids)) $ids = explode(",", $ids);
foreach($ids as $id) $rows[] = $this->getRowById($id);
return $rows;
}
/**
* Get all rows of this finder
* @return array
*/
public function getRows() {
if($this->rows) return $this->rows;
// check if a row limit is set for this finder
// this is the case when a relation is set to return only the subset
// of rows that are listed in the main finder
$this->applyRowLimit();
// now execute the query
$result = $this->query->execute();
$rows = $result->fetchAll(\PDO::FETCH_OBJ);
return $this->rows = $this->master->addRowIds($rows);
}
/** ########## END GET DATA ########## */
/**
* Add column to finder
* @param mixed $column
* @param mixed $type
* @param mixed $alias
* @return void
*/
private function _addColumn($column, $type = null, $alias = null) {
if(!$type) $type = $this->getType($column);
if(!$alias) $alias = $column;
// add this column to columns array
$colname = (string)$column;
if($this->columns->has($colname)) {
// if the column does already exist we append a unique id
// this can happen when requesting title and value of an options field
// https://i.imgur.com/woxCx78.png
$colname .= "_".uniqid();
}
// get the column object and apply its changes to the current finder
$colType = $this->master->columnTypes->get("type=$type");
if(!$colType) throw new WireException("No column type class for $type");
$col = $colType->getNew($colname, $alias);
$this->addColumnType($col);
}
/**
* Add column type to this finder
* @param \RockFinder3\Column $col
* @return void
*/
public function addColumnType($col) {
$col->applyTo($this);
$this->columns->add($col);
}
/**
* Apply join to current finder
* @param RockFinder3 $join
* @return void
*/
public function applyJoin($join) {
$this->query->leftjoin($join->getJoinSQL());
foreach($join->columns as $col) {
$this->query->select("GROUP_CONCAT(DISTINCT `{$join->name}`.`{$col->alias}`) AS `{$join->joinColName}:{$col->alias}`");
}
if($join->removeJoinCol) {
$select = $this->query->select;
foreach($select as $i=>$_select) {
if(strpos($_select, " AS `{$join->joinColName}`")) unset($select[$i]);
}
$this->query->set('select', array_values($select));
}
}
private function applyRowLimit() {
if(!$this->limitRowsTo) return;
// get ids that point to that relation
$ids = [];
$finder = $this->limitRowsTo;
$column = $this->name; // colname = name of current relation
foreach($finder->getRows() as $row) {
$ids = array_merge($ids, explode(",", $row->$column));
}
// now restrict the relation to these ids
$ids = implode(",", $ids);
$this->query->where("pages.id IN ($ids)");
}
/**
* Dump this finder to the tracy console
*
* Set title to TRUE to dump the finder object.
*
* @return void
*/
public function dump($title = null, $options = null) {
$settings = $this->wire(new WireData()); /** @var WireData $settings */
$settings->setArray([
'layout' => 'fitColumns',
'autoColumns' => true,
'pagination' => "local",
'paginationSize' => 10,
'paginationSizeSelector' => true,
]);
$settings->setArray($options ?: []);
$settings = $settings->getArray();
$settings['data'] = $this->getRowArray();
$json = json_encode($settings);
$id = uniqid();
if($title === true) db($this);
elseif($title) echo "<h2>$title</h2>";
echo "<div id='tab_$id'>loading...</div>
<script>
if(typeof Tabulator == 'undefined') {
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = 'https://unpkg.com/tabulator-tables@4.6.3/dist/css/tabulator.min.css';
document.getElementsByTagName('head')[0].appendChild(link);
tracyJSLoader.load('https://unpkg.com/tabulator-tables@4.6.3/dist/js/tabulator.min.js', function() {
new Tabulator('#tab_$id', $json);
});
}
else new Tabulator('#tab_$id', $json);
</script>";
return $this;
}
/**
* Dump SQL of current finder to console
* @return void
*/
public function dumpSQL() {
db($this->getSQL());
return $this;
}
/**
* Get the type of this column
*
* The type is then used for getting the proper data for the column.
*
* @param string $column
* @return string
*/
public function ___getType($column) {
// is this column part of the pages table?
if($this->isBaseColumn($column)) return 'BaseColumn';
// is it a pw field?
$field = $this->fields->get($column);
if($field) {
// file and image fields
if($field->type instanceof FieldtypeFile) return 'Multi';
if($field->type instanceof FieldtypePage) return 'Multi';
if($field->type instanceof FieldtypeOptions) return 'Multi';
// by default we take it as text field
return 'Text';
}
else return 'NotFound';
}
/**
* Return current sql query string
* @return string
*/
public function getSQL($pretty = true) {
if(!$this->query) return;
// make sure that the row limit is applied before returning the sql
$this->applyRowLimit();
// return sql
$sql = $this->query->getQuery();
return $pretty ? $this->prettify($sql) : $sql;
}
/**
* Get sql for join
* @return string
*/
public function getJoinSQL($pretty = true) {
// the current instance is the slave finder
// for better readability give it a good name
$slave = $this;
// get sql subquery
// for better readability we add spaces to indent the subquery
$subquery = str_replace("\n", "\n ", $slave->getSQL($pretty));
// get the column of the main finder where the join is based on
$col = $slave->main->columns->get($slave->joinColName);
// return the sql query to join the subquery as new table alias
return "(\n $subquery\n) AS `{$slave->name}` ON `{$slave->name}`.`id` = `{$col->tableAlias}`.`data`";
}
/**
* Is this column part of the 'pages' db table?
* @return bool
*/
private function isBaseColumn($column) {
return in_array($column, $this->master->baseColumns);
}
/**
* Prettify SQL string
* @return string
*/
private function prettify($sql) {
$str = str_replace("SELECT ", "SELECT\n ", $sql);
$str = str_replace("`,", "`,\n ", $str);
// undo double breaks on joined sql
$str = str_replace("`,\n \n", "`,\n", $str);
return $str;
}
public function __debugInfo() {
return [
'name' => $this->name,
'selector' => $this->selector,
'columns' => $this->columns,
'options' => $this->options,
'relations' => $this->relations,
'joins' => $this->joins,
'getRows()' => $this->getRows(),
];
}
}