Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / search / controller / PhabricatorSearchEditController.php
bloba5260915031c6ea9323040131d925987ccebc151
1 <?php
3 final class PhabricatorSearchEditController
4 extends PhabricatorSearchBaseController {
6 public function handleRequest(AphrontRequest $request) {
7 $viewer = $this->getViewer();
9 $id = $request->getURIData('id');
10 if ($id) {
11 $named_query = id(new PhabricatorNamedQueryQuery())
12 ->setViewer($viewer)
13 ->withIDs(array($id))
14 ->requireCapabilities(
15 array(
16 PhabricatorPolicyCapability::CAN_VIEW,
17 PhabricatorPolicyCapability::CAN_EDIT,
19 ->executeOne();
20 if (!$named_query) {
21 return new Aphront404Response();
24 $query_key = $named_query->getQueryKey();
25 } else {
26 $query_key = $request->getURIData('queryKey');
27 $named_query = null;
30 $saved_query = id(new PhabricatorSavedQueryQuery())
31 ->setViewer($viewer)
32 ->withQueryKeys(array($query_key))
33 ->executeOne();
34 if (!$saved_query) {
35 return new Aphront404Response();
38 $engine = $saved_query->newEngine()->setViewer($viewer);
40 $complete_uri = $engine->getQueryManagementURI();
41 $cancel_uri = $complete_uri;
43 if (!$named_query) {
44 $named_query = id(new PhabricatorNamedQuery())
45 ->setUserPHID($viewer->getPHID())
46 ->setQueryKey($saved_query->getQueryKey())
47 ->setEngineClassName($saved_query->getEngineClassName());
49 // If we haven't saved the query yet, this is a "Save..." operation, so
50 // take the user back to the query if they cancel instead of back to the
51 // management interface.
52 $cancel_uri = $engine->getQueryResultsPageURI(
53 $saved_query->getQueryKey());
55 $is_new = true;
56 } else {
57 $is_new = false;
60 $can_global = ($viewer->getIsAdmin() && $is_new);
62 $v_global = false;
64 $e_name = true;
65 $errors = array();
67 if ($request->isFormPost()) {
68 if ($can_global) {
69 $v_global = $request->getBool('global');
70 if ($v_global) {
71 $named_query->setUserPHID(PhabricatorNamedQuery::SCOPE_GLOBAL);
75 $named_query->setQueryName($request->getStr('name'));
76 if (!strlen($named_query->getQueryName())) {
77 $e_name = pht('Required');
78 $errors[] = pht('You must name the query.');
79 } else {
80 $e_name = null;
83 if (!$errors) {
85 $named_query->save();
86 return id(new AphrontRedirectResponse())->setURI($complete_uri);
90 $form = id(new AphrontFormView())
91 ->setUser($viewer);
93 $form->appendChild(
94 id(new AphrontFormTextControl())
95 ->setName('name')
96 ->setLabel(pht('Query Name'))
97 ->setValue($named_query->getQueryName())
98 ->setError($e_name));
100 if ($can_global) {
101 $form->appendChild(
102 id(new AphrontFormCheckboxControl())
103 ->addCheckbox(
104 'global',
105 '1',
106 pht(
107 'Save this query as a global query, making it visible to '.
108 'all users.'),
109 $v_global));
112 $form->appendChild(
113 id(new AphrontFormSubmitControl())
114 ->setValue(pht('Save Query'))
115 ->addCancelButton($cancel_uri));
117 if ($named_query->getID()) {
118 $title = pht('Edit Saved Query');
119 $header_icon = 'fa-pencil';
120 } else {
121 $title = pht('Save Query');
122 $header_icon = 'fa-search';
125 $form_box = id(new PHUIObjectBoxView())
126 ->setHeaderText(pht('Query'))
127 ->setFormErrors($errors)
128 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
129 ->setForm($form);
131 $crumbs = $this->buildApplicationCrumbs();
132 $crumbs->addTextCrumb($title);
133 $crumbs->setBorder(true);
135 $header = id(new PHUIHeaderView())
136 ->setHeader($title)
137 ->setHeaderIcon($header_icon);
139 $view = id(new PHUITwoColumnView())
140 ->setHeader($header)
141 ->setFooter($form_box);
143 return $this->newPage()
144 ->setTitle($title)
145 ->setCrumbs($crumbs)
146 ->appendChild($view);