-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStatus.php
175 lines (147 loc) · 5.08 KB
/
Status.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
<?php
namespace dokuwiki\plugin\structstatus;
use dokuwiki\plugin\struct\meta\Column;
use dokuwiki\plugin\struct\meta\QueryBuilder;
use dokuwiki\plugin\struct\meta\Search;
use dokuwiki\plugin\struct\types\AbstractBaseType;
use dokuwiki\plugin\struct\types\Lookup;
class Status extends Lookup {
protected $config = array(
'schema' => ''
);
/**
* @inheritDoc
*/
protected function getLookupColumn() {
if($this->column !== null) return $this->column;
$this->column = $this->getColumn($this->config['schema'], 'name_$LANG');
return $this->column;
}
/**
* @inheritDoc
*/
public function renderValue($value, \Doku_Renderer $R, $mode) {
list(, $value, $color, $icon) = json_decode($value);
if($mode == 'xhtml') {
$R->doc .= $this->xhtmlStatus($value, $color, $icon);
} else {
$R->cdata($value);
}
return true;
}
/**
* Creates a single status entry
*
* @param string $label
* @param string $color
* @param string $icon
* @param string $rid the identifier in the linked status lookup table
* @param array $classes
* @param bool $button
* @return string
*/
public function xhtmlStatus($label, $color, $icon='', $rid = 0, $classes=array(), $button=false) {
$html = '';
$classes[] = 'struct_status';
if($icon) $classes[] = 'struct_status_icon_'.$icon;
$class = hsc(join(' ', $classes));
$tag = $button ? 'button' : 'div';
$html .= "<$tag class=\"" . $class . '" style="border-color:' . hsc($color) . '; fill: ' . hsc($color) . ';" data-rid="'.hsc($rid).'">';
$html .= $this->inlineSVG($icon);
$html .= hsc($label);
$html .= "</$tag>";
return $html;
}
/**
* Returns the svg code of the given icon
*
* @param string $icon The icon identifier (no .svg extension)
* @return string
*/
protected function inlineSVG($icon) {
$icon = preg_replace('@[\.\\\\/]+@', '', $icon);
$file = __DIR__ . '/svg/' . $icon . '.svg';
if(!file_exists($file)) return '';
$data = file_get_contents($file);
$data = preg_replace('/<\?xml .*?\?>/', '', $data);
$data = preg_replace('/<!DOCTYPE .*?>/', '', $data);
return $data;
}
/**
* @inheritDoc
*/
public function renderMultiValue($values, \Doku_Renderer $R, $mode) {
foreach($values as $value) {
$this->renderValue($value, $R, $mode);
}
return true;
}
/**
* Merge with lookup table
*
* @param QueryBuilder $QB
* @param string $tablealias
* @param string $colname
* @param string $alias
*/
public function select(QueryBuilder $QB, $tablealias, $colname, $alias) {
$schema = 'data_' . $this->config['schema'];
$rightalias = $QB->generateTableAlias();
// main status
$col_status = $this->getLookupColumn();
if(!$col_status) {
AbstractBaseType::select($QB, $tablealias, $colname, $alias);
return;
}
$field_status = $rightalias . '.' . $col_status->getColName();
// color
$col_color = $this->getColumn($this->config['schema'], 'color');
if(!$col_color) {
$field_color = "'#ffffff'"; // static fallback
} else {
$field_color = $rightalias . '.' . $col_color->getColName(true);
}
// icon
$col_icon = $this->getColumn($this->config['schema'], 'icon');
if(!$col_icon) {
$field_icon = "''"; // static fallback
} else {
$field_icon = $rightalias . '.' . $col_icon->getColName(true);
}
// join the lookup
$QB->addLeftJoin(
$tablealias, $schema, $rightalias,
"$tablealias.$colname = STRUCT_JSON($rightalias.pid, CAST($rightalias.rid AS DECIMAL)) AND $rightalias.latest = 1"
);
// get the values (pid, status, color)
$QB->addSelectStatement("STRUCT_JSON($tablealias.$colname, $field_status, $field_color, $field_icon)", $alias);
}
/**
* Returns a list of available statuses for this type
*
* This is similar to getOptions but returns some more info about each status
*
* @return array
*/
public function getAllStatuses() {
$col = $this->getLookupColumn();
$colname = $col->getLabel();
$search = new Search();
$search->addSchema($col->getTable());
$search->addColumn($colname);
$search->addColumn('color');
$search->addColumn('icon');
$search->addSort($colname);
$values = $search->execute();
$rids = $search->getRids();
$statuses = array();
foreach($values as $status) {
$rid = json_encode(["", (int)array_shift($rids)]);
$label = $status[0]->getValue();
$color = $status[1]->getValue();
$icon = $status[2]->getValue();
$statuses[] = compact('rid', 'label', 'color', 'icon');
}
return $statuses;
}
}