2013-05-27 22:42:01 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group search
|
|
|
|
*/
|
|
|
|
final class PhabricatorSearchEditController
|
|
|
|
extends PhabricatorSearchBaseController {
|
|
|
|
|
|
|
|
private $queryKey;
|
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->queryKey = idx($data, 'queryKey');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$user = $request->getUser();
|
|
|
|
|
|
|
|
$saved_query = id(new PhabricatorSavedQueryQuery())
|
|
|
|
->setViewer($user)
|
|
|
|
->withQueryKeys(array($this->queryKey))
|
|
|
|
->executeOne();
|
|
|
|
|
|
|
|
if (!$saved_query) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
2013-05-27 22:42:44 +02:00
|
|
|
$engine = $saved_query->newEngine()->setViewer($user);
|
|
|
|
|
|
|
|
$complete_uri = $engine->getQueryManagementURI();
|
|
|
|
$cancel_uri = $complete_uri;
|
2013-05-27 22:42:01 +02:00
|
|
|
|
|
|
|
$named_query = id(new PhabricatorNamedQueryQuery())
|
|
|
|
->setViewer($user)
|
|
|
|
->withQueryKeys(array($saved_query->getQueryKey()))
|
|
|
|
->withUserPHIDs(array($user->getPHID()))
|
|
|
|
->executeOne();
|
|
|
|
if (!$named_query) {
|
|
|
|
$named_query = id(new PhabricatorNamedQuery())
|
|
|
|
->setUserPHID($user->getPHID())
|
|
|
|
->setQueryKey($saved_query->getQueryKey())
|
|
|
|
->setEngineClassName($saved_query->getEngineClassName());
|
2013-05-27 22:42:44 +02:00
|
|
|
|
|
|
|
// If we haven't saved the query yet, this is a "Save..." operation, so
|
|
|
|
// take the user back to the query if they cancel instead of back to the
|
|
|
|
// management interface.
|
|
|
|
$cancel_uri = $engine->getQueryResultsPageURI($saved_query);
|
2013-05-27 22:42:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$e_name = true;
|
|
|
|
$errors = array();
|
|
|
|
|
|
|
|
if ($request->isFormPost()) {
|
|
|
|
$named_query->setQueryName($request->getStr('name'));
|
|
|
|
if (!strlen($named_query->getQueryName())) {
|
|
|
|
$e_name = pht('Required');
|
|
|
|
$errors[] = pht('You must name the query.');
|
|
|
|
} else {
|
|
|
|
$e_name = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$errors) {
|
|
|
|
$named_query->save();
|
2013-05-27 22:42:44 +02:00
|
|
|
return id(new AphrontRedirectResponse())->setURI($complete_uri);
|
2013-05-27 22:42:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($errors) {
|
|
|
|
$errors = id(new AphrontErrorView())
|
|
|
|
->setErrors($errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
$form = id(new AphrontFormView())
|
|
|
|
->setUser($user);
|
|
|
|
|
|
|
|
$form->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
|
|
|
->setName('name')
|
|
|
|
->setLabel(pht('Query Name'))
|
|
|
|
->setValue($named_query->getQueryName())
|
|
|
|
->setError($e_name));
|
|
|
|
|
|
|
|
$form->appendChild(
|
|
|
|
id(new AphrontFormSubmitControl())
|
2013-05-27 22:42:44 +02:00
|
|
|
->setValue(pht('Save Query'))
|
|
|
|
->addCancelButton($cancel_uri));
|
2013-05-27 22:42:01 +02:00
|
|
|
|
|
|
|
if ($named_query->getID()) {
|
|
|
|
$title = pht('Edit Saved Query');
|
|
|
|
} else {
|
|
|
|
$title = pht('Save Query');
|
|
|
|
}
|
|
|
|
|
|
|
|
$crumbs = $this->buildApplicationCrumbs();
|
|
|
|
$crumbs->addCrumb(
|
|
|
|
id(new PhabricatorCrumbView())
|
|
|
|
->setName($title));
|
|
|
|
|
|
|
|
return $this->buildApplicationPage(
|
|
|
|
array(
|
|
|
|
$crumbs,
|
|
|
|
$errors,
|
|
|
|
$form,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'title' => $title,
|
|
|
|
'device' => true,
|
|
|
|
'dust' => true,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|