Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/diviner/controller/DivinerBookController.php
12256 views
1
<?php
2
3
final class DivinerBookController extends DivinerController {
4
5
public function shouldAllowPublic() {
6
return true;
7
}
8
9
public function handleRequest(AphrontRequest $request) {
10
$viewer = $request->getViewer();
11
12
$book_name = $request->getURIData('book');
13
14
$book = id(new DivinerBookQuery())
15
->setViewer($viewer)
16
->withNames(array($book_name))
17
->needRepositories(true)
18
->executeOne();
19
20
if (!$book) {
21
return new Aphront404Response();
22
}
23
24
$actions = $this->buildActionView($viewer, $book);
25
26
$crumbs = $this->buildApplicationCrumbs();
27
$crumbs->setBorder(true);
28
$crumbs->addTextCrumb(
29
$book->getShortTitle(),
30
'/book/'.$book->getName().'/');
31
32
$header = id(new PHUIHeaderView())
33
->setHeader($book->getTitle())
34
->setUser($viewer)
35
->setPolicyObject($book)
36
->setEpoch($book->getDateModified())
37
->setActionList($actions);
38
39
// TODO: This could probably look better.
40
if ($book->getRepositoryPHID()) {
41
$header->addTag(
42
id(new PHUITagView())
43
->setType(PHUITagView::TYPE_STATE)
44
->setBackgroundColor(PHUITagView::COLOR_BLUE)
45
->setName($book->getRepository()->getMonogram()));
46
}
47
48
$document = new PHUIDocumentView();
49
$document->setHeader($header);
50
$document->addClass('diviner-view');
51
52
$atoms = id(new DivinerAtomQuery())
53
->setViewer($viewer)
54
->withBookPHIDs(array($book->getPHID()))
55
->withGhosts(false)
56
->withIsDocumentable(true)
57
->execute();
58
59
$atoms = msort($atoms, 'getSortKey');
60
61
$group_spec = $book->getConfig('groups');
62
if (!is_array($group_spec)) {
63
$group_spec = array();
64
}
65
66
$groups = mgroup($atoms, 'getGroupName');
67
$groups = array_select_keys($groups, array_keys($group_spec)) + $groups;
68
if (isset($groups[''])) {
69
$no_group = $groups[''];
70
unset($groups['']);
71
$groups[''] = $no_group;
72
}
73
74
$out = array();
75
foreach ($groups as $group => $atoms) {
76
$group_name = $book->getGroupName($group);
77
if (!strlen($group_name)) {
78
$group_name = pht('Free Radicals');
79
}
80
$section = id(new DivinerSectionView())
81
->setHeader($group_name);
82
$section->addContent($this->renderAtomList($atoms));
83
$out[] = $section;
84
}
85
86
$preface = $book->getPreface();
87
$preface_view = null;
88
if (strlen($preface)) {
89
$preface_view = new PHUIRemarkupView($viewer, $preface);
90
}
91
92
$document->appendChild($preface_view);
93
$document->appendChild($out);
94
95
return $this->newPage()
96
->setTitle($book->getTitle())
97
->setCrumbs($crumbs)
98
->appendChild(array(
99
$document,
100
));
101
}
102
103
private function buildActionView(
104
PhabricatorUser $user,
105
DivinerLiveBook $book) {
106
107
$can_edit = PhabricatorPolicyFilter::hasCapability(
108
$user,
109
$book,
110
PhabricatorPolicyCapability::CAN_EDIT);
111
112
$action_view = id(new PhabricatorActionListView())
113
->setUser($user)
114
->setObject($book);
115
116
$action_view->addAction(
117
id(new PhabricatorActionView())
118
->setName(pht('Edit Book'))
119
->setIcon('fa-pencil')
120
->setHref('/book/'.$book->getName().'/edit/')
121
->setDisabled(!$can_edit));
122
123
return $action_view;
124
}
125
126
}
127
128