Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/dashboard/layoutconfig/PhabricatorDashboardPanelRefList.php
13444 views
1
<?php
2
3
final class PhabricatorDashboardPanelRefList
4
extends Phobject {
5
6
private $refs;
7
private $columns;
8
9
public static function newFromDictionary($config) {
10
if (!is_array($config)) {
11
$config = array();
12
}
13
14
$mode_map = PhabricatorDashboardLayoutMode::getAllLayoutModes();
15
$mode_key = idx($config, 'layoutMode');
16
if (!isset($mode_map[$mode_key])) {
17
$mode_key = head_key($mode_map);
18
}
19
$mode = $mode_map[$mode_key];
20
21
$columns = $mode->getLayoutModeColumns();
22
$columns = mpull($columns, null, 'getColumnKey');
23
$default_column = head($columns);
24
25
$panels = idx($config, 'panels');
26
if (!is_array($panels)) {
27
$panels = array();
28
}
29
30
$seen_panels = array();
31
$refs = array();
32
foreach ($panels as $panel) {
33
$panel_phid = idx($panel, 'panelPHID');
34
if (!strlen($panel_phid)) {
35
continue;
36
}
37
38
$panel_key = idx($panel, 'panelKey');
39
if (!strlen($panel_key)) {
40
continue;
41
}
42
43
if (isset($seen_panels[$panel_key])) {
44
continue;
45
}
46
$seen_panels[$panel_key] = true;
47
48
$column_key = idx($panel, 'columnKey');
49
$column = idx($columns, $column_key, $default_column);
50
51
$ref = id(new PhabricatorDashboardPanelRef())
52
->setPanelPHID($panel_phid)
53
->setPanelKey($panel_key)
54
->setColumnKey($column->getColumnKey());
55
56
$column->addPanelRef($ref);
57
$refs[] = $ref;
58
}
59
60
$list = new self();
61
62
$list->columns = $columns;
63
$list->refs = $refs;
64
65
return $list;
66
}
67
68
public function getColumns() {
69
return $this->columns;
70
}
71
72
public function getPanelRefs() {
73
return $this->refs;
74
}
75
76
public function getPanelRef($panel_key) {
77
foreach ($this->getPanelRefs() as $ref) {
78
if ($ref->getPanelKey() === $panel_key) {
79
return $ref;
80
}
81
}
82
83
return null;
84
}
85
86
public function toDictionary() {
87
return array_values(mpull($this->getPanelRefs(), 'toDictionary'));
88
}
89
90
public function newPanelRef(
91
PhabricatorDashboardPanel $panel,
92
$column_key = null) {
93
94
if ($column_key === null) {
95
$column_key = head_key($this->columns);
96
}
97
98
$ref = id(new PhabricatorDashboardPanelRef())
99
->setPanelKey($this->newPanelKey())
100
->setPanelPHID($panel->getPHID())
101
->setColumnKey($column_key);
102
103
$this->refs[] = $ref;
104
105
return $ref;
106
}
107
108
public function removePanelRef(PhabricatorDashboardPanelRef $target) {
109
foreach ($this->refs as $key => $ref) {
110
if ($ref->getPanelKey() !== $target->getPanelKey()) {
111
continue;
112
}
113
114
unset($this->refs[$key]);
115
return $ref;
116
}
117
118
return null;
119
}
120
121
public function movePanelRef(
122
PhabricatorDashboardPanelRef $target,
123
$column_key,
124
PhabricatorDashboardPanelRef $after = null) {
125
126
$target->setColumnKey($column_key);
127
128
$results = array();
129
130
if (!$after) {
131
$results[] = $target;
132
}
133
134
foreach ($this->refs as $ref) {
135
if ($ref->getPanelKey() === $target->getPanelKey()) {
136
continue;
137
}
138
139
$results[] = $ref;
140
141
if ($after) {
142
if ($ref->getPanelKey() === $after->getPanelKey()) {
143
$results[] = $target;
144
}
145
}
146
}
147
148
$this->refs = $results;
149
150
$column_map = mgroup($results, 'getColumnKey');
151
foreach ($this->columns as $column_key => $column) {
152
$column->setPanelRefs(idx($column_map, $column_key, array()));
153
}
154
155
return $ref;
156
}
157
158
private function newPanelKey() {
159
return Filesystem::readRandomCharacters(8);
160
}
161
162
163
}
164
165