Fix searching legalpad documents by contributors
[phabricator.git] / src / applications / legalpad / query / LegalpadDocumentSearchEngine.php
blob8c4dd31ff1e60dc242dabf7264fc0ba9f5c860f6
1 <?php
3 final class LegalpadDocumentSearchEngine
4 extends PhabricatorApplicationSearchEngine {
6 public function getResultTypeDescription() {
7 return pht('Legalpad Documents');
10 public function getApplicationClassName() {
11 return 'PhabricatorLegalpadApplication';
14 public function newQuery() {
15 return id(new LegalpadDocumentQuery())
16 ->needViewerSignatures(true);
19 protected function buildCustomSearchFields() {
20 return array(
21 id(new PhabricatorUsersSearchField())
22 ->setLabel(pht('Signed By'))
23 ->setKey('signerPHIDs')
24 ->setAliases(array('signer', 'signers', 'signerPHID'))
25 ->setDescription(
26 pht('Search for documents signed by given users.')),
27 id(new PhabricatorUsersSearchField())
28 ->setLabel(pht('Creators'))
29 ->setKey('creatorPHIDs')
30 ->setAliases(array('creator', 'creators', 'creatorPHID'))
31 ->setDescription(
32 pht('Search for documents with given creators.')),
33 id(new PhabricatorUsersSearchField())
34 ->setLabel(pht('Contributors'))
35 ->setKey('contributorPHIDs')
36 ->setAliases(array('contributor', 'contributors', 'contributorPHID'))
37 ->setDescription(
38 pht('Search for documents with given contributors.')),
39 id(new PhabricatorSearchDateField())
40 ->setLabel(pht('Created After'))
41 ->setKey('createdStart'),
42 id(new PhabricatorSearchDateField())
43 ->setLabel(pht('Created Before'))
44 ->setKey('createdEnd'),
48 protected function buildQueryFromParameters(array $map) {
49 $query = $this->newQuery();
51 if ($map['signerPHIDs']) {
52 $query->withSignerPHIDs($map['signerPHIDs']);
55 if ($map['contributorPHIDs']) {
56 $query->withContributorPHIDs($map['contributorPHIDs']);
59 if ($map['creatorPHIDs']) {
60 $query->withCreatorPHIDs($map['creatorPHIDs']);
63 if ($map['createdStart']) {
64 $query->withDateCreatedAfter($map['createdStart']);
67 if ($map['createdEnd']) {
68 $query->withDateCreatedAfter($map['createdStart']);
71 return $query;
74 protected function getURI($path) {
75 return '/legalpad/'.$path;
78 protected function getBuiltinQueryNames() {
79 $names = array();
81 if ($this->requireViewer()->isLoggedIn()) {
82 $names['signed'] = pht('Signed Documents');
85 $names['all'] = pht('All Documents');
87 return $names;
90 public function buildSavedQueryFromBuiltin($query_key) {
91 $query = $this->newSavedQuery();
92 $query->setQueryKey($query_key);
94 $viewer = $this->requireViewer();
96 switch ($query_key) {
97 case 'signed':
98 return $query->setParameter('signerPHIDs', array($viewer->getPHID()));
99 case 'all':
100 return $query;
103 return parent::buildSavedQueryFromBuiltin($query_key);
106 protected function renderResultList(
107 array $documents,
108 PhabricatorSavedQuery $query,
109 array $handles) {
110 assert_instances_of($documents, 'LegalpadDocument');
112 $viewer = $this->requireViewer();
114 $list = new PHUIObjectItemListView();
115 $list->setUser($viewer);
116 foreach ($documents as $document) {
117 $last_updated = phabricator_date($document->getDateModified(), $viewer);
119 $title = $document->getTitle();
121 $item = id(new PHUIObjectItemView())
122 ->setObjectName($document->getMonogram())
123 ->setHeader($title)
124 ->setHref('/'.$document->getMonogram())
125 ->setObject($document);
127 $no_signatures = LegalpadDocument::SIGNATURE_TYPE_NONE;
128 if ($document->getSignatureType() == $no_signatures) {
129 $item->addIcon('none', pht('Not Signable'));
130 } else {
132 $type_name = $document->getSignatureTypeName();
133 $type_icon = $document->getSignatureTypeIcon();
134 $item->addIcon($type_icon, $type_name);
136 if ($viewer->getPHID()) {
137 $signature = $document->getUserSignature($viewer->getPHID());
138 } else {
139 $signature = null;
142 if ($signature) {
143 $item->addAttribute(
144 array(
145 id(new PHUIIconView())->setIcon('fa-check-square-o', 'green'),
146 ' ',
147 pht(
148 'Signed on %s',
149 phabricator_date($signature->getDateCreated(), $viewer)),
151 } else {
152 $item->addAttribute(
153 array(
154 id(new PHUIIconView())->setIcon('fa-square-o', 'grey'),
155 ' ',
156 pht('Not Signed'),
161 $item->addIcon(
162 'fa-pencil grey',
163 pht('Version %d (%s)', $document->getVersions(), $last_updated));
165 $list->addItem($item);
168 $result = new PhabricatorApplicationSearchResultView();
169 $result->setObjectList($list);
170 $result->setNoDataString(pht('No documents found.'));
172 return $result;
175 protected function getNewUserBody() {
176 $create_button = id(new PHUIButtonView())
177 ->setTag('a')
178 ->setText(pht('Create a Document'))
179 ->setHref('/legalpad/edit/')
180 ->setColor(PHUIButtonView::GREEN);
182 $icon = $this->getApplication()->getIcon();
183 $app_name = $this->getApplication()->getName();
184 $view = id(new PHUIBigInfoView())
185 ->setIcon($icon)
186 ->setTitle(pht('Welcome to %s', $app_name))
187 ->setDescription(
188 pht('Create documents and track signatures. Can also be re-used in '.
189 'other areas of Phabricator, like CLAs.'))
190 ->addAction($create_button);
192 return $view;