-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRockFinderField.class.php
141 lines (125 loc) · 4.13 KB
/
RockFinderField.class.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
<?php namespace ProcessWire;
abstract class RockFinderField extends WireData {
public $name;
protected $columns;
public $type;
public $alias;
public $siblingseparator = ':';
// if set to true this will query the current user's language in strict mode
// if set to false, it will return the value of the current language and fallback
// to the default language's value if the current language's value is NULL or empty
public $strictLanguage = false;
public $multiLang = true;
// todo: change parameters to one options array
public function __construct($name, $columns, $type) {
$this->name = $this->alias = $name;
$this->columns = array_merge(['data'], $columns ?: []);
$this->type = $type;
}
/**
* get sql
*/
public function getSql() {
$sql = "SELECT";
$sql .= "\n `pages_id` AS `pageid`";
foreach($this->columns as $column) {
$sql .= ",\n {$this->dataColumn($this->alias)} AS `{$this->fieldAlias($column)}`";
}
$sql .= "\nFROM `field_".strtolower($this->name)."` AS `$this->alias`";
return $sql;
}
/**
* get array of objects
*/
public function getObjects($limit = 0) {
$limit = $limit ? " LIMIT $limit" : '';
$results = $this->database->query($this->getSQL() . $limit);
return $results->fetchAll(\PDO::FETCH_OBJ);
}
/**
* return the field alias for given column
*/
protected function fieldAlias($column) {
/**
* bug: this does not work for custom fieldnames but makes joined pages with custom alias work
* $finder = new RockFinder("template=person, isFN=1, has_parent=7888", [
* 'lang' => 'bla', // does not work
* 'forename',
* 'surname',
* ]);
* $finder->addField('report', ['pdfs']);
* $finder->addField('report', ['charts'], ['alias'=>'test']);
*/
$alias = $column == 'data'
? "{$this->alias}"
: "{$this->alias}{$this->siblingseparator}$column"
;
return $alias;
}
/**
* select all columns of this fieldtype
*/
public function getJoinSelect() {
$sql = '';
foreach($this->columns as $column) {
$sql .= ",\n `$this->alias`.`{$this->fieldAlias($column)}` AS `{$this->fieldAlias($column)}`";
}
return $sql;
}
/**
* return the select statement for the data column
*/
public function dataColumn($column) {
// if multilang is switched off query "data" column directly
if($this->multiLang == false) return "`$column`.`data`";
// if the field does not support multilang return the data column
$field = $this->fields->get($column);
if(!$field) throw new WireException("Field $column does not exist");
if(!$field->type instanceof FieldtypeLanguageInterface) return "`$column`.`data`";
// multilang is ON, check for the user's language
$lang = $this->wire->user->language;
if($lang != $this->wire->languages->getDefault()) {
// in strict mode we return the language value
if($this->strictLanguage) return "`$column`.`data{$lang->id}`";
// otherwise we return the first non-empty value
else return "COALESCE(NULLIF(`$column`.`data{$lang->id}`, ''), `$column`.`data`)";
}
// user has default language active, return the value
return "`$column`.`data`";
}
/**
* get sql for joining this field's table
*/
public function getJoin() {
return "\n\n/* --- join $this->alias --- */\n".
"LEFT JOIN (" . $this->getSql() . ") AS `$this->alias` ".
"ON `$this->alias`.`pageid` = `pages`.`id`".
"\n/* --- end $this->alias --- */\n";
}
/**
* Add columns to this field.
*
* @param array $columns
* @return void
*/
public function addColumns($columns) {
$this->columns = array_merge($this->columns, $columns);
}
/**
* debugInfo PHP 5.6+ magic method
*
* This is used when you print_r() an object instance.
*
* @return array
*
*/
public function __debugInfo() {
$info = parent::__debugInfo();
$info['name'] = $this->name;
$info['columns'] = $this->columns;
$info['type'] = $this->type;
$info['alias'] = $this->alias;
$info['siblingseparator'] = $this->siblingseparator;
return $info;
}
}