From e0f4e19c3f3d9cde42cb9355fcb0f3ae29c93e5b Mon Sep 17 00:00:00 2001 From: epriestley Date: Fri, 19 Dec 2014 12:36:14 -0800 Subject: [PATCH] Add an Almanac service typeahead datasource Summary: Ref T5833. Ref T6238. This will probably be useful somewhere in the upstream eventually, and is materially useful in the Instances application right now. Test Plan: tippy typey typey Reviewers: btrahan Reviewed By: btrahan Subscribers: epriestley Maniphest Tasks: T6238, T5833 Differential Revision: https://secure.phabricator.com/D11016 --- src/__phutil_library_map__.php | 2 + .../almanac/query/AlmanacServiceQuery.php | 26 +++++++++++ .../typeahead/AlmanacServiceDatasource.php | 43 +++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 src/applications/almanac/typeahead/AlmanacServiceDatasource.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 4f7e81ffee..748103042d 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -78,6 +78,7 @@ phutil_register_library_map(array( 'AlmanacSchemaSpec' => 'applications/almanac/storage/AlmanacSchemaSpec.php', 'AlmanacService' => 'applications/almanac/storage/AlmanacService.php', 'AlmanacServiceController' => 'applications/almanac/controller/AlmanacServiceController.php', + 'AlmanacServiceDatasource' => 'applications/almanac/typeahead/AlmanacServiceDatasource.php', 'AlmanacServiceEditController' => 'applications/almanac/controller/AlmanacServiceEditController.php', 'AlmanacServiceEditor' => 'applications/almanac/editor/AlmanacServiceEditor.php', 'AlmanacServiceListController' => 'applications/almanac/controller/AlmanacServiceListController.php', @@ -3112,6 +3113,7 @@ phutil_register_library_map(array( 'AlmanacPropertyInterface', ), 'AlmanacServiceController' => 'AlmanacController', + 'AlmanacServiceDatasource' => 'PhabricatorTypeaheadDatasource', 'AlmanacServiceEditController' => 'AlmanacServiceController', 'AlmanacServiceEditor' => 'PhabricatorApplicationTransactionEditor', 'AlmanacServiceListController' => 'AlmanacServiceController', diff --git a/src/applications/almanac/query/AlmanacServiceQuery.php b/src/applications/almanac/query/AlmanacServiceQuery.php index 7dc9fea77a..002f8e71b1 100644 --- a/src/applications/almanac/query/AlmanacServiceQuery.php +++ b/src/applications/almanac/query/AlmanacServiceQuery.php @@ -9,6 +9,8 @@ final class AlmanacServiceQuery private $serviceClasses; private $devicePHIDs; private $locked; + private $namePrefix; + private $nameSuffix; private $needBindings; @@ -42,6 +44,16 @@ final class AlmanacServiceQuery return $this; } + public function withNamePrefix($prefix) { + $this->namePrefix = $prefix; + return $this; + } + + public function withNameSuffix($suffix) { + $this->nameSuffix = $suffix; + return $this; + } + public function needBindings($need_bindings) { $this->needBindings = $need_bindings; return $this; @@ -126,6 +138,20 @@ final class AlmanacServiceQuery (int)$this->locked); } + if ($this->namePrefix !== null) { + $where[] = qsprintf( + $conn_r, + 'service.name LIKE %>', + $this->namePrefix); + } + + if ($this->nameSuffix !== null) { + $where[] = qsprintf( + $conn_r, + 'service.name LIKE %<', + $this->nameSuffix); + } + $where[] = $this->buildPagingClause($conn_r); return $this->formatWhereClause($where); diff --git a/src/applications/almanac/typeahead/AlmanacServiceDatasource.php b/src/applications/almanac/typeahead/AlmanacServiceDatasource.php new file mode 100644 index 0000000000..00da7d4fd6 --- /dev/null +++ b/src/applications/almanac/typeahead/AlmanacServiceDatasource.php @@ -0,0 +1,43 @@ +getViewer(); + $raw_query = $this->getRawQuery(); + + $services = id(new AlmanacServiceQuery()) + ->setViewer($viewer) + ->withNamePrefix($raw_query) + ->setLimit($this->getLimit()) + ->execute(); + + if ($services) { + $handles = id(new PhabricatorHandleQuery()) + ->setViewer($viewer) + ->withPHIDs(mpull($services, 'getPHID')) + ->execute(); + } else { + $handles = array(); + } + + $results = array(); + foreach ($handles as $handle) { + $results[] = id(new PhabricatorTypeaheadResult()) + ->setName($handle->getName()) + ->setPHID($handle->getPHID()); + } + + return $results; + } + +}