1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-29 18:22:41 +01:00

Allow builtin named queries to be disabled

Summary:
Applications come with builtin queries, but users might want to get rid of them. Allow users to disable named queries if they prefer.

This has one funky behavior, which is that the first time you disable a named query it goes to the top of your list. That will be fixed in the next diff, which will make them reorderable.

Test Plan: Added/edited/removed named queries, disabled/enabled builtin named queries.

Reviewers: chad

Reviewed By: chad

CC: aran

Differential Revision: https://secure.phabricator.com/D6128
This commit is contained in:
epriestley 2013-06-05 05:28:25 -07:00
parent 7fbfeca802
commit 758586abda
7 changed files with 144 additions and 54 deletions

View file

@ -0,0 +1,8 @@
ALTER TABLE {$NAMESPACE}_search.search_namedquery
ADD isBuiltin BOOL NOT NULL DEFAULT 0;
ALTER TABLE {$NAMESPACE}_search.search_namedquery
ADD isDisabled BOOL NOT NULL DEFAULT 0;
ALTER TABLE {$NAMESPACE}_search.search_namedquery
ADD sequence INT UNSIGNED NOT NULL DEFAULT 0;

View file

@ -35,7 +35,8 @@ final class PhabricatorApplicationSearch extends PhabricatorApplication {
'hovercard/(?P<mode>retrieve|test)/' => 'hovercard/(?P<mode>retrieve|test)/' =>
'PhabricatorSearchHovercardController', 'PhabricatorSearchHovercardController',
'edit/(?P<queryKey>[^/]+)/' => 'PhabricatorSearchEditController', 'edit/(?P<queryKey>[^/]+)/' => 'PhabricatorSearchEditController',
'delete/(?P<queryKey>[^/]+)/' => 'PhabricatorSearchDeleteController', 'delete/(?P<queryKey>[^/]+)/(?P<engine>[^/]+)/'
=> 'PhabricatorSearchDeleteController',
), ),
); );
} }

View file

@ -226,40 +226,47 @@ final class PhabricatorApplicationSearchController
$engine = $this->getSearchEngine(); $engine = $this->getSearchEngine();
$nav = $this->getNavigation(); $nav = $this->getNavigation();
$named_queries = id(new PhabricatorNamedQueryQuery()) $named_queries = $engine->loadAllNamedQueries();
->setViewer($user)
->withUserPHIDs(array($user->getPHID()))
->withEngineClassNames(array(get_class($engine)))
->execute();
$named_queries += $engine->getBuiltinQueries();
$list = new PhabricatorObjectItemListView(); $list = new PhabricatorObjectItemListView();
$list->setUser($user); $list->setUser($user);
foreach ($named_queries as $named_query) { foreach ($named_queries as $named_query) {
$class = get_class($engine);
$key = $named_query->getQueryKey();
$date_created = phabricator_datetime( $date_created = phabricator_datetime(
$named_query->getDateCreated(), $named_query->getDateCreated(),
$user); $user);
$item = id(new PhabricatorObjectItemView()) $item = id(new PhabricatorObjectItemView())
->setHeader($named_query->getQueryName()) ->setHeader($named_query->getQueryName())
->setHref($engine->getQueryResultsPageURI($named_query->getQueryKey())); ->setHref($engine->getQueryResultsPageURI($key));
if ($named_query->getIsBuiltin()) { if ($named_query->getIsBuiltin() && $named_query->getIsDisabled()) {
$item->addIcon('lock-grey', pht('Builtin')); $icon = 'new';
$item->setBarColor('grey');
} else { } else {
$item->addIcon('none', $date_created); $icon = 'delete';
}
$item->addAction( $item->addAction(
id(new PhabricatorMenuItemView()) id(new PhabricatorMenuItemView())
->setIcon('delete') ->setIcon($icon)
->setHref('/search/delete/'.$named_query->getQueryKey().'/') ->setHref('/search/delete/'.$key.'/'.$class.'/')
->setWorkflow(true)); ->setWorkflow(true));
if ($named_query->getIsBuiltin()) {
if ($named_query->getIsDisabled()) {
$item->addIcon('delete-grey', pht('Disabled'));
} else {
$item->addIcon('lock-grey', pht('Builtin'));
}
$item->setBarColor('grey');
} else {
$item->addAction( $item->addAction(
id(new PhabricatorMenuItemView()) id(new PhabricatorMenuItemView())
->setIcon('edit') ->setIcon('edit')
->setHref('/search/edit/'.$named_query->getQueryKey().'/')); ->setHref('/search/edit/'.$key.'/'));
} }
$list->addItem($item); $list->addItem($item);

View file

@ -7,52 +7,96 @@ final class PhabricatorSearchDeleteController
extends PhabricatorSearchBaseController { extends PhabricatorSearchBaseController {
private $queryKey; private $queryKey;
private $engineClass;
public function willProcessRequest(array $data) { public function willProcessRequest(array $data) {
$this->queryKey = idx($data, 'queryKey'); $this->queryKey = idx($data, 'queryKey');
$this->engineClass = idx($data, 'engine');
} }
public function processRequest() { public function processRequest() {
$request = $this->getRequest(); $request = $this->getRequest();
$user = $request->getUser(); $user = $request->getUser();
$saved_query = id(new PhabricatorSavedQueryQuery()) $key = $this->queryKey;
->setViewer($user)
->withQueryKeys(array($this->queryKey))
->executeOne();
if (!$saved_query) { $base_class = 'PhabricatorApplicationSearchEngine';
return new Aphront404Response(); if (!is_subclass_of($this->engineClass, $base_class)) {
return new Aphront400Response();
} }
$engine = $saved_query->newEngine(); $engine = newv($this->engineClass, array());
$engine->setViewer($user);
$named_query = id(new PhabricatorNamedQueryQuery()) $named_query = id(new PhabricatorNamedQueryQuery())
->setViewer($user) ->setViewer($user)
->withQueryKeys(array($saved_query->getQueryKey())) ->withEngineClassNames(array($this->engineClass))
->withQueryKeys(array($key))
->withUserPHIDs(array($user->getPHID())) ->withUserPHIDs(array($user->getPHID()))
->executeOne(); ->executeOne();
if (!$named_query && $engine->isBuiltinQuery($key)) {
$named_query = id(new PhabricatorNamedQuery())
->setUserPHID($user->getPHID())
->setQueryName('(BUILTIN)')
->setQueryKey($key)
->setEngineClassName($this->engineClass)
->setIsBuiltin(true);
}
if (!$named_query) { if (!$named_query) {
return new Aphront404Response(); return new Aphront404Response();
} }
$builtin = null;
if ($engine->isBuiltinQuery($key)) {
$builtin = $engine->getBuiltinQuery($key);
}
$return_uri = $engine->getQueryManagementURI(); $return_uri = $engine->getQueryManagementURI();
if ($request->isDialogFormPost()) { if ($request->isDialogFormPost()) {
if ($named_query->getIsBuiltin()) {
$named_query->setIsDisabled((int)(!$named_query->getIsDisabled()));
$named_query->save();
} else {
$named_query->delete(); $named_query->delete();
}
return id(new AphrontRedirectResponse())->setURI($return_uri); return id(new AphrontRedirectResponse())->setURI($return_uri);
} }
if ($named_query->getIsBuiltin()) {
if ($named_query->getIsDisabled()) {
$title = pht('Enable Query?');
$desc = pht(
'Enable the built-in query "%s"? It will appear in your menu again.',
$builtin->getQueryName());
$button = pht('Enable Query');
} else {
$title = pht('Disable Query?');
$desc = pht(
'This built-in query can not be deleted, but you can disable it so '.
'it does not appear in your query menu. You can enable it again '.
'later. Disable built-in query "%s"?',
$builtin->getQueryName());
$button = pht('Disable Query');
}
} else {
$title = pht('Really Delete Query?');
$desc = pht(
'Really delete the query "%s"? You can not undo this. Remember '.
'all the great times you had filtering results together?',
$named_query->getQueryName());
$button = pht('Delete Query');
}
$dialog = id(new AphrontDialogView()) $dialog = id(new AphrontDialogView())
->setUser($user) ->setUser($user)
->setTitle(pht("Really Delete Query?")) ->setTitle($title)
->appendChild( ->appendChild($desc)
pht(
'Really delete the query "%s"? You can not undo this. Remember '.
'all the great times you had filtering results together?',
$named_query->getQueryName()))
->addCancelButton($return_uri) ->addCancelButton($return_uri)
->addSubmitButton(pht('Delete Query')); ->addSubmitButton($button);
return id(new AphrontDialogResponse())->setDialog($dialog); return id(new AphrontDialogResponse())->setDialog($dialog);
} }

View file

@ -111,13 +111,7 @@ abstract class PhabricatorApplicationSearchEngine {
$menu->newLabel(pht('Queries')); $menu->newLabel(pht('Queries'));
$named_queries = id(new PhabricatorNamedQueryQuery()) $named_queries = $this->loadEnabledNamedQueries();
->setViewer($viewer)
->withUserPHIDs(array($viewer->getPHID()))
->withEngineClassNames(array(get_class($this)))
->execute();
$named_queries = $named_queries + $this->getBuiltinQueries($viewer);
foreach ($named_queries as $query) { foreach ($named_queries as $query) {
$key = $query->getQueryKey(); $key = $query->getQueryKey();
@ -137,6 +131,45 @@ abstract class PhabricatorApplicationSearchEngine {
return $this; return $this;
} }
public function loadAllNamedQueries() {
$viewer = $this->requireViewer();
$named_queries = id(new PhabricatorNamedQueryQuery())
->setViewer($viewer)
->withUserPHIDs(array($viewer->getPHID()))
->withEngineClassNames(array(get_class($this)))
->execute();
$named_queries = mpull($named_queries, null, 'getQueryKey');
$builtin = $this->getBuiltinQueries($viewer);
$builtin = mpull($builtin, null, 'getQueryKey');
foreach ($named_queries as $key => $named_query) {
if ($named_query->getIsBuiltin()) {
if (isset($builtin[$key])) {
$named_queries[$key]->setQueryName($builtin[$key]->getQueryName());
unset($builtin[$key]);
} else {
unset($named_queries[$key]);
}
}
unset($builtin[$key]);
}
return $named_queries + $builtin;
}
public function loadEnabledNamedQueries() {
$named_queries = $this->loadAllNamedQueries();
foreach ($named_queries as $key => $named_query) {
if ($named_query->getIsBuiltin() && $named_query->getIsDisabled()) {
unset($named_queries[$key]);
}
}
return $named_queries;
}
/* -( Builtin Queries )---------------------------------------------------- */ /* -( Builtin Queries )---------------------------------------------------- */

View file

@ -6,21 +6,14 @@
final class PhabricatorNamedQuery extends PhabricatorSearchDAO final class PhabricatorNamedQuery extends PhabricatorSearchDAO
implements PhabricatorPolicyInterface { implements PhabricatorPolicyInterface {
protected $queryKey = ""; protected $queryKey;
protected $queryName = ""; protected $queryName;
protected $userPHID = ""; protected $userPHID;
protected $engineClassName = ""; protected $engineClassName;
private $isBuiltin; protected $isBuiltin = 0;
protected $isDisabled = 0;
public function setIsBuiltin($is_builtin) { protected $sequence = 0;
$this->isBuiltin = $is_builtin;
return $this;
}
public function getIsBuiltin() {
return $this->isBuiltin;
}
/* -( PhabricatorPolicyInterface )----------------------------------------- */ /* -( PhabricatorPolicyInterface )----------------------------------------- */

View file

@ -1346,6 +1346,10 @@ final class PhabricatorBuiltinPatchList extends PhabricatorSQLPatchList {
'type' => 'sql', 'type' => 'sql',
'name' => $this->getPatchPath('20130602.morediviner.sql'), 'name' => $this->getPatchPath('20130602.morediviner.sql'),
), ),
'20130602.namedqueries.sql' => array(
'type' => 'sql',
'name' => $this->getPatchPath('20130602.namedqueries.sql'),
),
); );
} }
} }