This repository was archived by the owner on Jan 9, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathColumn.php
More file actions
104 lines (92 loc) · 2.82 KB
/
Column.php
File metadata and controls
104 lines (92 loc) · 2.82 KB
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
<?php namespace RockFinder3;
abstract class Column extends \ProcessWire\Wire {
public $name;
public $alias;
public $type;
public $table;
public $tableAlias;
public $select;
public function __construct() {
// set classname as "type" property so we can query the master array:
// $master->columnTypes->get("type=foo")->getNew();
$this->type = $this->className;
}
/**
* Abstract function that every columnType must implement
* @param \ProcessWire\RockFinder3
*/
abstract public function applyTo($finder);
/**
* Set the table name for this column
* @return string
*/
public function setTable() {
// name of a column can be fooBar
// the corresponding table in the DB is field_foobar (lowercase)
$this->table = "field_".strtolower($this->name);
}
/**
* Set table alias name for this column
*
* We prepend the original table name with an underscore
* to prevent "non unique table alias" errors.
*
* @param string $column
* @return string
*/
public function setTableAlias() {
$this->tableAlias = "_{$this->table}_".uniqid();
}
/**
* Get method to support $columns->each('name') etc.
*/
public function get($key) {
return $this->$key;
}
/**
* Get a new instance of this columnType
* @return Column
*/
public function getNew($name, $alias) {
$class = "\RockFinder3Column\\{$this->type}";
$col = new $class();
$col->name = $name;
$col->alias = $alias;
$col->setTable();
$col->setTableAlias();
$col->setSelect();
return $col;
}
/**
* Set the data-column that should be selected via SQL
*
* This is usually `table`.`data` but for multilang it is `table`.`data123`
* If the multilang column is empty it should return the default's lang value
* so it gets even more complicated. This method handles all that and can
* simply be used in columTypes via $query->select("{$this->select} AS `{$this->alias}`);
* @return void
*/
public function setSelect() {
$lang = $this->user->language;
$this->select = "`{$this->tableAlias}`.`data`";
// early exit if user has default language
if(!$lang OR $lang->isDefault) return;
// early exit if it is not a regular field (column of pages table)
$field = $this->fields->get($this->name);
if(!$field) return;
// early exit if field is single-language
if(!$field->type instanceof \ProcessWire\FieldtypeLanguageInterface) return;
// multi-lang field
$this->select = "COALESCE(NULLIF(`{$this->tableAlias}`.`data{$lang->id}`, ''), `{$this->tableAlias}`.`data`)";
}
public function __debugInfo() {
return [
'name' => $this->name,
'alias' => $this->alias,
'type' => $this->type,
'table' => $this->table,
'tableAlias' => $this->tableAlias,
'select' => $this->select,
];
}
}