/**
  * @param array $commit_phids phids of the commits to render
  * @param PhabricatorUser $user phabricator user
  * @return AphrontTableView
  */
 private static function createCommitTable(array $commit_phids, PhabricatorUser $user)
 {
     $loader = new PhabricatorObjectHandleData($commit_phids);
     $handles = $loader->loadHandles();
     $objects = $loader->loadObjects();
     $rows = array();
     foreach ($commit_phids as $phid) {
         $handle = $handles[$phid];
         $object = $objects[$phid];
         $summary = null;
         if ($object) {
             $commit_data = $object->getCommitData();
             if ($commit_data) {
                 $summary = $commit_data->getSummary();
             }
         }
         $epoch = $handle->getTimeStamp();
         $date = phabricator_date($epoch, $user);
         $time = phabricator_time($epoch, $user);
         $link = phutil_render_tag('a', array('href' => $handle->getURI()), phutil_escape_html($handle->getName()));
         $rows[] = array($link, $date, $time, phutil_escape_html($summary));
     }
     $commit_table = new AphrontTableView($rows);
     $commit_table->setHeaders(array('Commit', 'Date', 'Time', 'Summary'));
     $commit_table->setColumnClasses(array('', '', 'right', 'wide'));
     return $commit_table;
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $handle_data = new PhabricatorObjectHandleData(array($this->phid));
     $handles = $handle_data->loadHandles();
     $handle = $handles[$this->phid];
     $object_type = $handle->getType();
     $attach_type = $this->type;
     $objects = $handle_data->loadObjects();
     $object = idx($objects, $this->phid);
     if (!$object) {
         return new Aphront404Response();
     }
     if ($request->isFormPost()) {
         $phids = explode(';', $request->getStr('phids'));
         $phids = array_filter($phids);
         $phids = array_values($phids);
         switch ($this->action) {
             case self::ACTION_MERGE:
                 return $this->performMerge($object, $handle, $phids);
             case self::ACTION_DEPENDENCIES:
             case self::ACTION_ATTACH:
                 $two_way = true;
                 if ($this->action == self::ACTION_DEPENDENCIES) {
                     $two_way = false;
                     $this->detectGraphCycles($object, $attach_type, $phids);
                 }
                 $editor = new PhabricatorObjectAttachmentEditor($object_type, $object);
                 $editor->setUser($this->getRequest()->getUser());
                 $editor->attachObjects($attach_type, $phids, $two_way);
                 return id(new AphrontReloadResponse())->setURI($handle->getURI());
             default:
                 throw new Exception("Unsupported attach action.");
         }
     } else {
         switch ($this->action) {
             case self::ACTION_ATTACH:
             case self::ACTION_DEPENDENCIES:
                 $phids = $object->getAttachedPHIDs($attach_type);
                 break;
             default:
                 $phids = array();
                 break;
         }
     }
     $strings = $this->getStrings();
     $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles();
     $obj_dialog = new PhabricatorObjectSelectorDialog();
     $obj_dialog->setUser($user)->setHandles($handles)->setFilters(array('assigned' => 'Assigned to Me', 'created' => 'Created By Me', 'open' => 'All Open ' . $strings['target_plural_noun'], 'all' => 'All ' . $strings['target_plural_noun']))->setSelectedFilter($strings['selected'])->setCancelURI($handle->getURI())->setSearchURI('/search/select/' . $attach_type . '/')->setTitle($strings['title'])->setHeader($strings['header'])->setButtonText($strings['button'])->setInstructions($strings['instructions']);
     $dialog = $obj_dialog->buildDialog();
     return id(new AphrontDialogResponse())->setDialog($dialog);
 }
 protected function loadEdges(array $nodes)
 {
     if (!$this->edgeType) {
         throw new Exception("Set edge type before loading graph!");
     }
     $handle_data = new PhabricatorObjectHandleData($nodes);
     $objects = $handle_data->loadObjects();
     $result = array();
     foreach ($nodes as $phid) {
         $object = idx($objects, $phid);
         if ($object) {
             $result[$phid] = $object->getAttachedPHIDs($this->edgeType);
         } else {
             $result[$phid] = array();
         }
     }
     return $result;
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     if ($this->key) {
         $query = id(new PhabricatorSearchQuery())->loadOneWhere('queryKey = %s', $this->key);
         if (!$query) {
             return new Aphront404Response();
         }
     } else {
         $query = new PhabricatorSearchQuery();
         if ($request->isFormPost()) {
             $query_str = $request->getStr('query');
             $pref_jump = PhabricatorUserPreferences::PREFERENCE_SEARCHBAR_JUMP;
             if ($request->getStr('jump') != 'no' && $user && $user->loadPreferences()->getPreference($pref_jump, 1)) {
                 $response = PhabricatorJumpNavHandler::jumpPostResponse($query_str);
             } else {
                 $response = null;
             }
             if ($response) {
                 return $response;
             } else {
                 $query->setQuery($query_str);
                 if ($request->getStr('scope')) {
                     switch ($request->getStr('scope')) {
                         case PhabricatorSearchScope::SCOPE_OPEN_REVISIONS:
                             $query->setParameter('open', 1);
                             $query->setParameter('type', PhabricatorPHIDConstants::PHID_TYPE_DREV);
                             break;
                         case PhabricatorSearchScope::SCOPE_OPEN_TASKS:
                             $query->setParameter('open', 1);
                             $query->setParameter('type', PhabricatorPHIDConstants::PHID_TYPE_TASK);
                             break;
                         case PhabricatorSearchScope::SCOPE_WIKI:
                             $query->setParameter('type', PhabricatorPHIDConstants::PHID_TYPE_WIKI);
                             break;
                         case PhabricatorSearchScope::SCOPE_COMMITS:
                             $query->setParameter('type', PhabricatorPHIDConstants::PHID_TYPE_CMIT);
                             break;
                         default:
                             break;
                     }
                 } else {
                     if (strlen($request->getStr('type'))) {
                         $query->setParameter('type', $request->getStr('type'));
                     }
                     if ($request->getArr('author')) {
                         $query->setParameter('author', $request->getArr('author'));
                     }
                     if ($request->getArr('owner')) {
                         $query->setParameter('owner', $request->getArr('owner'));
                     }
                     if ($request->getInt('open')) {
                         $query->setParameter('open', $request->getInt('open'));
                     }
                     if ($request->getArr('project')) {
                         $query->setParameter('project', $request->getArr('project'));
                     }
                 }
                 $query->save();
                 return id(new AphrontRedirectResponse())->setURI('/search/' . $query->getQueryKey() . '/');
             }
         }
     }
     $options = array('' => 'All Documents') + PhabricatorSearchAbstractDocument::getSupportedTypes();
     $status_options = array(0 => 'Open and Closed Documents', 1 => 'Open Documents');
     $phids = array_merge($query->getParameter('author', array()), $query->getParameter('owner', array()), $query->getParameter('project', array()));
     $handles = $this->loadViewerHandles($phids);
     $author_value = array_select_keys($handles, $query->getParameter('author', array()));
     $author_value = mpull($author_value, 'getFullName', 'getPHID');
     $owner_value = array_select_keys($handles, $query->getParameter('owner', array()));
     $owner_value = mpull($owner_value, 'getFullName', 'getPHID');
     $project_value = array_select_keys($handles, $query->getParameter('project', array()));
     $project_value = mpull($project_value, 'getFullName', 'getPHID');
     $search_form = new AphrontFormView();
     $search_form->setUser($user)->setAction('/search/')->appendChild(phutil_render_tag('input', array('type' => 'hidden', 'name' => 'jump', 'value' => 'no')))->appendChild(id(new AphrontFormTextControl())->setLabel('Search')->setName('query')->setValue($query->getQuery()))->appendChild(id(new AphrontFormSelectControl())->setLabel('Document Type')->setName('type')->setOptions($options)->setValue($query->getParameter('type')))->appendChild(id(new AphrontFormSelectControl())->setLabel('Document Status')->setName('open')->setOptions($status_options)->setValue($query->getParameter('open')))->appendChild(id(new AphrontFormTokenizerControl())->setName('author')->setLabel('Author')->setDatasource('/typeahead/common/users/')->setValue($author_value))->appendChild(id(new AphrontFormTokenizerControl())->setName('owner')->setLabel('Owner')->setDatasource('/typeahead/common/searchowner/')->setValue($owner_value)->setCaption('Tip: search for "Up For Grabs" to find unowned documents.'))->appendChild(id(new AphrontFormTokenizerControl())->setName('project')->setLabel('Project')->setDatasource('/typeahead/common/projects/')->setValue($project_value))->appendChild(id(new AphrontFormSubmitControl())->setValue('Search'));
     $search_panel = new AphrontPanelView();
     $search_panel->setHeader('Search Phabricator');
     $search_panel->appendChild($search_form);
     require_celerity_resource('phabricator-search-results-css');
     if ($query->getID()) {
         $limit = 20;
         $pager = new AphrontPagerView();
         $pager->setURI($request->getRequestURI(), 'page');
         $pager->setPageSize($limit);
         $pager->setOffset($request->getInt('page'));
         $query->setParameter('limit', $limit + 1);
         $query->setParameter('offset', $pager->getOffset());
         $engine = PhabricatorSearchEngineSelector::newSelector()->newEngine();
         $results = $engine->executeSearch($query);
         $results = $pager->sliceResults($results);
         if (!$request->getInt('page')) {
             $jump = PhabricatorPHID::fromObjectName($query->getQuery());
             if ($jump) {
                 array_unshift($results, $jump);
             }
         }
         if ($results) {
             $loader = new PhabricatorObjectHandleData($results);
             $handles = $loader->loadHandles();
             $objects = $loader->loadObjects();
             $results = array();
             foreach ($handles as $phid => $handle) {
                 $view = new PhabricatorSearchResultView();
                 $view->setHandle($handle);
                 $view->setQuery($query);
                 $view->setObject(idx($objects, $phid));
                 $results[] = $view->render();
             }
             $results = '<div class="phabricator-search-result-list">' . implode("\n", $results) . '<div class="search-results-pager">' . $pager->render() . '</div>' . '</div>';
         } else {
             $results = '<div class="phabricator-search-result-list">' . '<p class="phabricator-search-no-results">No search results.</p>' . '</div>';
         }
     } else {
         $results = null;
     }
     return $this->buildStandardPageResponse(array($search_panel, $results), array('title' => 'Search Results'));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $handle_data = new PhabricatorObjectHandleData(array($this->phid));
     $handles = $handle_data->loadHandles();
     $handle = $handles[$this->phid];
     $object_type = $handle->getType();
     $attach_type = $this->type;
     $objects = $handle_data->loadObjects();
     $object = idx($objects, $this->phid);
     if (!$object) {
         return new Aphront404Response();
     }
     $edge_type = null;
     switch ($this->action) {
         case self::ACTION_EDGE:
         case self::ACTION_DEPENDENCIES:
         case self::ACTION_ATTACH:
             $edge_type = $this->getEdgeType($object_type, $attach_type);
             break;
     }
     if ($request->isFormPost()) {
         $phids = explode(';', $request->getStr('phids'));
         $phids = array_filter($phids);
         $phids = array_values($phids);
         if ($edge_type) {
             $old_phids = PhabricatorEdgeQuery::loadDestinationPHIDs($this->phid, $edge_type);
             $add_phids = $phids;
             $rem_phids = array_diff($old_phids, $add_phids);
             $editor = id(new PhabricatorEdgeEditor());
             $editor->setUser($user);
             foreach ($add_phids as $phid) {
                 $editor->addEdge($this->phid, $edge_type, $phid);
             }
             foreach ($rem_phids as $phid) {
                 $editor->removeEdge($this->phid, $edge_type, $phid);
             }
             try {
                 $editor->save();
             } catch (PhabricatorEdgeCycleException $ex) {
                 $this->raiseGraphCycleException($ex);
             }
             return id(new AphrontReloadResponse())->setURI($handle->getURI());
         } else {
             return $this->performMerge($object, $handle, $phids);
         }
     } else {
         if ($edge_type) {
             $phids = PhabricatorEdgeQuery::loadDestinationPHIDs($this->phid, $edge_type);
         } else {
             // This is a merge.
             $phids = array();
         }
     }
     $strings = $this->getStrings();
     $handles = $this->loadViewerHandles($phids);
     $obj_dialog = new PhabricatorObjectSelectorDialog();
     $obj_dialog->setUser($user)->setHandles($handles)->setFilters(array('assigned' => 'Assigned to Me', 'created' => 'Created By Me', 'open' => 'All Open ' . $strings['target_plural_noun'], 'all' => 'All ' . $strings['target_plural_noun']))->setSelectedFilter($strings['selected'])->setExcluded($this->phid)->setCancelURI($handle->getURI())->setSearchURI('/search/select/' . $attach_type . '/')->setTitle($strings['title'])->setHeader($strings['header'])->setButtonText($strings['button'])->setInstructions($strings['instructions']);
     $dialog = $obj_dialog->buildDialog();
     return id(new AphrontDialogResponse())->setDialog($dialog);
 }
 public function execute()
 {
     $table = new PhabricatorFlag();
     $conn_r = $table->establishConnection('r');
     $where = $this->buildWhereClause($conn_r);
     $limit = $this->buildLimitClause($conn_r);
     $order = $this->buildOrderClause($conn_r);
     $data = queryfx_all($conn_r, 'SELECT * FROM %T flag %Q %Q %Q', $table->getTableName(), $where, $order, $limit);
     $flags = $table->loadAllFromArray($data);
     if ($this->needHandles || $this->needObjects) {
         $phids = ipull($data, 'objectPHID');
         $query = new PhabricatorObjectHandleData($phids);
         if ($this->needHandles) {
             $handles = $query->loadHandles();
             foreach ($flags as $flag) {
                 $handle = idx($handles, $flag->getObjectPHID());
                 if ($handle) {
                     $flag->attachHandle($handle);
                 }
             }
         }
         if ($this->needObjects) {
             $objects = $query->loadObjects();
             foreach ($flags as $flag) {
                 $object = idx($objects, $flag->getObjectPHID());
                 if ($object) {
                     $flag->attachObject($object);
                 }
             }
         }
     }
     return $flags;
 }
 public function processRequest()
 {
     $this->request = $this->getRequest();
     $this->user = $this->request->getUser();
     $this->commitPHID = $this->request->getStr('c-phid');
     $this->packagePHID = $this->request->getStr('p-phid');
     $relationship = id(new PhabricatorOwnersPackageCommitRelationship())->loadOneWhere('commitPHID = %s AND packagePHID=%s', $this->commitPHID, $this->packagePHID);
     if (!$relationship) {
         return new Aphront404Response();
     }
     $package = id(new PhabricatorOwnersPackage())->loadOneWhere("phid = %s", $this->packagePHID);
     $owners = id(new PhabricatorOwnersOwner())->loadAllWhere('packageID = %d', $package->getID());
     $owners_phids = mpull($owners, 'getUserPHID');
     if (!$this->user->getIsAdmin() && !in_array($this->user->getPHID(), $owners_phids)) {
         return $this->buildStandardPageResponse(id(new AphrontErrorView())->setSeverity(AphrontErrorView::SEVERITY_ERROR)->setTitle("Only admin or owner of the package can audit the " . "commit."), array('title' => 'Audit a Commit'));
     }
     if ($this->request->isFormPost()) {
         return $this->saveAuditComments();
     }
     $package_link = phutil_render_tag('a', array('href' => '/owners/package/' . $package->getID() . '/'), phutil_escape_html($package->getName()));
     $phids = array($this->commitPHID);
     $loader = new PhabricatorObjectHandleData($phids);
     $handles = $loader->loadHandles();
     $objects = $loader->loadObjects();
     $commit_handle = $handles[$this->commitPHID];
     $commit_object = $objects[$this->commitPHID];
     $commit_data = $commit_object->getCommitData();
     $commit_epoch = $commit_handle->getTimeStamp();
     $commit_datetime = phabricator_datetime($commit_epoch, $this->user);
     $commit_link = $this->renderHandleLink($commit_handle);
     $revision_author_phid = null;
     $revision_reviewedby_phid = null;
     $revision_link = null;
     $revision_id = $commit_data->getCommitDetail('differential.revisionID');
     if ($revision_id) {
         $revision = id(new DifferentialRevision())->load($revision_id);
         if ($revision) {
             $revision->loadRelationships();
             $revision_author_phid = $revision->getAuthorPHID();
             $revision_reviewedby_phid = $revision->loadReviewedBy();
             $revision_link = phutil_render_tag('a', array('href' => '/D' . $revision->getID()), phutil_escape_html($revision->getTitle()));
         }
     }
     $commit_author_phid = $commit_data->getCommitDetail('authorPHID');
     $commit_reviewedby_phid = $commit_data->getCommitDetail('reviewerPHID');
     $conn_r = id(new PhabricatorAuditComment())->establishConnection('r');
     $latest_comment = queryfx_one($conn_r, 'SELECT * FROM %T
     WHERE targetPHID = %s and actorPHID in (%Ls)
     ORDER BY ID DESC LIMIT 1', id(new PhabricatorAuditComment())->getTableName(), $this->commitPHID, $owners_phids);
     $auditor_phid = $latest_comment['actorPHID'];
     $user_phids = array_unique(array_filter(array($revision_author_phid, $revision_reviewedby_phid, $commit_author_phid, $commit_reviewedby_phid, $auditor_phid)));
     $user_loader = new PhabricatorObjectHandleData($user_phids);
     $user_handles = $user_loader->loadHandles();
     if ($commit_author_phid && isset($handles[$commit_author_phid])) {
         $commit_author_link = $handles[$commit_author_phid]->renderLink();
     } else {
         $commit_author_link = phutil_escape_html($commit_data->getAuthorName());
     }
     $reasons = $relationship->getAuditReasons();
     $reasons = array_map('phutil_escape_html', $reasons);
     $reasons = implode($reasons, '<br>');
     $latest_comment_content = id(new AphrontFormTextAreaControl())->setLabel('Audit comments')->setName('latest_comments')->setReadOnly(true)->setValue($latest_comment['content']);
     $latest_comment_epoch = $latest_comment['dateModified'];
     $latest_comment_datetime = phabricator_datetime($latest_comment_epoch, $this->user);
     $select = id(new AphrontFormSelectControl())->setLabel('Audit it')->setName('action')->setValue(PhabricatorAuditActionConstants::ACCEPT)->setOptions(PhabricatorAuditActionConstants::getActionNameMap());
     $comment = id(new AphrontFormTextAreaControl())->setLabel('Audit comments')->setName('comments')->setCaption("Explain the audit.");
     $submit = id(new AphrontFormSubmitControl())->setValue('Save')->addCancelButton('/owners/related/view/audit/?phid=' . $this->packagePHID);
     $form = id(new AphrontFormView())->setUser($this->user)->appendChild(id(new AphrontFormMarkupControl())->setLabel('Package')->setValue($package_link))->appendChild(id(new AphrontFormMarkupControl())->setLabel('Commit')->setValue($commit_link))->appendChild(new AphrontFormDividerControl())->appendChild(id(new AphrontFormStaticControl())->setLabel('Commit Summary')->setValue(phutil_escape_html($commit_data->getSummary())))->appendChild(id(new AphrontFormMarkupControl())->setLabel('Commit Author')->setValue($commit_author_link))->appendChild(id(new AphrontFormMarkupControl())->setLabel('Commit Reviewed By')->setValue($this->renderHandleLink(idx($user_handles, $commit_reviewedby_phid))))->appendChild(id(new AphrontFormStaticControl())->setLabel('Commit Time')->setValue($commit_datetime))->appendChild(new AphrontFormDividerControl())->appendChild(id(new AphrontFormMarkupControl())->setLabel('Revision')->setValue($revision_link))->appendChild(id(new AphrontFormMarkupControl())->setLabel('Revision Author')->setValue($this->renderHandleLink(idx($user_handles, $revision_author_phid))))->appendChild(id(new AphrontFormMarkupControl())->setLabel('Revision Reviewed By')->setValue($this->renderHandleLink(idx($user_handles, $revision_reviewedby_phid))))->appendChild(new AphrontFormDividerControl())->appendChild(id(new AphrontFormMarkupControl())->setLabel('Audit Reasons')->setValue($reasons))->appendChild(id(new AphrontFormMarkupControl())->setLabel('Latest Auditor')->setValue($this->renderHandleLink(idx($user_handles, $auditor_phid))))->appendChild(id(new AphrontFormStaticControl())->setLabel('Latest Audit Status')->setValue(idx(PhabricatorAuditStatusConstants::getStatusNameMap(), $relationship->getAuditStatus())))->appendChild(id(new AphrontFormStaticControl())->setLabel('Latest Audit Time')->setValue($latest_comment_datetime))->appendChild($latest_comment_content)->appendChild(new AphrontFormDividerControl())->appendChild($select)->appendChild($comment)->appendChild($submit);
     $panel = id(new AphrontPanelView())->setHeader('Audit a Commit')->setWidth(AphrontPanelView::WIDTH_WIDE)->appendChild($form);
     return $this->buildStandardPageResponse($panel, array('title' => 'Audit a Commit'));
 }
 private function renderCommitTable($data, PhabricatorOwnersPackage $package)
 {
     $commit_phids = array_keys($data);
     $loader = new PhabricatorObjectHandleData($commit_phids);
     $handles = $loader->loadHandles();
     $objects = $loader->loadObjects();
     $owners = id(new PhabricatorOwnersOwner())->loadAllWhere('packageID = %d', $package->getID());
     $owners_phids = mpull($owners, 'getUserPHID');
     if ($this->user->getIsAdmin() || in_array($this->user->getPHID(), $owners_phids)) {
         $allowed_to_audit = true;
     } else {
         $allowed_to_audit = false;
     }
     $rows = array();
     foreach ($commit_phids as $commit_phid) {
         $handle = $handles[$commit_phid];
         $object = $objects[$commit_phid];
         $commit_data = $object->getCommitData();
         $epoch = $handle->getTimeStamp();
         $date = phabricator_date($epoch, $this->user);
         $time = phabricator_time($epoch, $this->user);
         $link = phutil_render_tag('a', array('href' => $handle->getURI()), phutil_escape_html($handle->getName()));
         $row = array($link, $date, $time, phutil_escape_html($commit_data->getSummary()));
         if ($this->view === 'audit') {
             $relationship = $data[$commit_phid];
             $status_link = phutil_escape_html(idx(PhabricatorAuditStatusConstants::getStatusNameMap(), $relationship['auditStatus']));
             if ($allowed_to_audit) {
                 $status_link = phutil_render_tag('a', array('href' => sprintf('/audit/edit/?c-phid=%s&p-phid=%s', idx($relationship, 'commitPHID'), $this->packagePHID)), $status_link);
             }
             $reasons = json_decode($relationship['auditReasons'], true);
             $reasons = array_map('phutil_escape_html', $reasons);
             $reasons = implode($reasons, '<br>');
             $row = array_merge($row, array($status_link, $reasons));
         }
         $rows[] = $row;
     }
     $commit_table = new AphrontTableView($rows);
     $headers = array('Commit', 'Date', 'Time', 'Summary');
     if ($this->view === 'audit') {
         $headers = array_merge($headers, array('Audit Status', 'Audit Reasons'));
     }
     $commit_table->setHeaders($headers);
     $column_classes = array('', '', 'right', 'wide');
     if ($this->view === 'audit') {
         $column_classes = array_merge($column_classes, array('', ''));
     }
     $commit_table->setColumnClasses($column_classes);
     $list_panel = new AphrontPanelView();
     $list_panel->setHeader('Commits Related to package "' . phutil_render_tag('a', array('href' => '/owners/package/' . $package->getID() . '/'), phutil_escape_html($package->getName())) . '"' . ($this->view === 'audit' ? ' and need attention' : ''));
     $list_panel->appendChild($commit_table);
     return $list_panel;
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     if ($this->id) {
         $query = id(new PhabricatorSearchQuery())->load($this->id);
         if (!$query) {
             return new Aphront404Response();
         }
     } else {
         $query = new PhabricatorSearchQuery();
         if ($request->isFormPost()) {
             $query->setQuery($request->getStr('query'));
             if (strlen($request->getStr('type'))) {
                 $query->setParameter('type', $request->getStr('type'));
             }
             if ($request->getArr('author')) {
                 $query->setParameter('author', $request->getArr('author'));
             }
             if ($request->getArr('owner')) {
                 $query->setParameter('owner', $request->getArr('owner'));
             }
             if ($request->getInt('open')) {
                 $query->setParameter('open', $request->getInt('open'));
             }
             if ($request->getArr('project')) {
                 $query->setParameter('project', $request->getArr('project'));
             }
             $query->save();
             return id(new AphrontRedirectResponse())->setURI('/search/' . $query->getID() . '/');
         }
     }
     $more = PhabricatorEnv::getEnvConfig('search.more-document-types', array());
     $options = array('' => 'All Documents', PhabricatorPHIDConstants::PHID_TYPE_DREV => 'Differential Revisions', PhabricatorPHIDConstants::PHID_TYPE_CMIT => 'Repository Commits', PhabricatorPHIDConstants::PHID_TYPE_TASK => 'Maniphest Tasks', PhabricatorPHIDConstants::PHID_TYPE_WIKI => 'Phriction Documents', PhabricatorPHIDConstants::PHID_TYPE_USER => 'Phabricator Users') + $more;
     $status_options = array(0 => 'Open and Closed Documents', 1 => 'Open Documents');
     $phids = array_merge($query->getParameter('author', array()), $query->getParameter('owner', array()), $query->getParameter('project', array()));
     $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles();
     $author_value = array_select_keys($handles, $query->getParameter('author', array()));
     $author_value = mpull($author_value, 'getFullName', 'getPHID');
     $owner_value = array_select_keys($handles, $query->getParameter('owner', array()));
     $owner_value = mpull($owner_value, 'getFullName', 'getPHID');
     $project_value = array_select_keys($handles, $query->getParameter('project', array()));
     $project_value = mpull($project_value, 'getFullName', 'getPHID');
     $search_form = new AphrontFormView();
     $search_form->setUser($user)->setAction('/search/')->appendChild(id(new AphrontFormTextControl())->setLabel('Search')->setName('query')->setValue($query->getQuery()))->appendChild(id(new AphrontFormSelectControl())->setLabel('Document Type')->setName('type')->setOptions($options)->setValue($query->getParameter('type')))->appendChild(id(new AphrontFormSelectControl())->setLabel('Document Status')->setName('open')->setOptions($status_options)->setValue($query->getParameter('open')))->appendChild(id(new AphrontFormTokenizerControl())->setName('author')->setLabel('Author')->setDatasource('/typeahead/common/users/')->setValue($author_value))->appendChild(id(new AphrontFormTokenizerControl())->setName('owner')->setLabel('Owner')->setDatasource('/typeahead/common/searchowner/')->setValue($owner_value)->setCaption('Tip: search for "Up For Grabs" to find unowned documents.'))->appendChild(id(new AphrontFormTokenizerControl())->setName('project')->setLabel('Project')->setDatasource('/typeahead/common/projects/')->setValue($project_value))->appendChild(id(new AphrontFormSubmitControl())->setValue('Search'));
     $search_panel = new AphrontPanelView();
     $search_panel->setHeader('Search Phabricator');
     $search_panel->appendChild($search_form);
     require_celerity_resource('phabricator-search-results-css');
     if ($query->getID()) {
         $limit = 20;
         $pager = new AphrontPagerView();
         $pager->setURI($request->getRequestURI(), 'page');
         $pager->setPageSize($limit);
         $pager->setOffset($request->getInt('page'));
         $query->setParameter('limit', $limit + 1);
         $query->setParameter('offset', $pager->getOffset());
         $executor = new PhabricatorSearchMySQLExecutor();
         $results = $executor->executeSearch($query);
         $results = ipull($results, 'phid');
         $results = $pager->sliceResults($results);
         if ($results) {
             $loader = new PhabricatorObjectHandleData($results);
             $handles = $loader->loadHandles();
             $objects = $loader->loadObjects();
             $results = array();
             foreach ($handles as $phid => $handle) {
                 $view = new PhabricatorSearchResultView();
                 $view->setHandle($handle);
                 $view->setQuery($query);
                 $view->setObject($objects[$phid]);
                 $results[] = $view->render();
             }
             $results = '<div class="phabricator-search-result-list">' . implode("\n", $results) . '<div class="search-results-pager">' . $pager->render() . '</div>' . '</div>';
         } else {
             $results = '<div class="phabricator-search-result-list">' . '<p class="phabricator-search-no-results">No search results.</p>' . '</div>';
         }
     } else {
         $results = null;
     }
     return $this->buildStandardPageResponse(array($search_panel, $results), array('title' => 'Results: what'));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $viewer_phid = $user->getPHID();
     $poll = id(new PhabricatorSlowvotePoll())->load($this->id);
     if (!$poll) {
         return new Aphront404Response();
     }
     $options = id(new PhabricatorSlowvoteOption())->loadAllWhere('pollID = %d', $poll->getID());
     $choices = id(new PhabricatorSlowvoteChoice())->loadAllWhere('pollID = %d', $poll->getID());
     $comments = id(new PhabricatorSlowvoteComment())->loadAllWhere('pollID = %d', $poll->getID());
     $choices_by_option = mgroup($choices, 'getOptionID');
     $comments_by_user = mpull($comments, null, 'getAuthorPHID');
     $choices_by_user = mgroup($choices, 'getAuthorPHID');
     $viewer_choices = idx($choices_by_user, $viewer_phid, array());
     $viewer_comment = idx($comments_by_user, $viewer_phid, null);
     $comment_text = null;
     if ($viewer_comment) {
         $comment_text = $viewer_comment->getCommentText();
     }
     if ($request->isFormPost()) {
         $comment = idx($comments_by_user, $viewer_phid, null);
         if ($comment) {
             $comment->delete();
         }
         $comment_text = $request->getStr('comments');
         if (strlen($comment_text)) {
             id(new PhabricatorSlowvoteComment())->setAuthorPHID($viewer_phid)->setPollID($poll->getID())->setCommentText($comment_text)->save();
         }
         $votes = $request->getArr('vote');
         switch ($poll->getMethod()) {
             case PhabricatorSlowvotePoll::METHOD_PLURALITY:
                 // Enforce only one vote.
                 $votes = array_slice($votes, 0, 1);
                 break;
             case PhabricatorSlowvotePoll::METHOD_APPROVAL:
                 // No filtering.
                 break;
             default:
                 throw new Exception("Unknown poll method!");
         }
         foreach ($viewer_choices as $viewer_choice) {
             $viewer_choice->delete();
         }
         foreach ($votes as $vote) {
             id(new PhabricatorSlowvoteChoice())->setAuthorPHID($viewer_phid)->setPollID($poll->getID())->setOptionID($vote)->save();
         }
         return id(new AphrontRedirectResponse())->setURI('/V' . $poll->getID());
     }
     require_celerity_resource('phabricator-slowvote-css');
     $phids = array_merge(mpull($choices, 'getAuthorPHID'), mpull($comments, 'getAuthorPHID'), array($poll->getAuthorPHID()));
     $query = new PhabricatorObjectHandleData($phids);
     $handles = $query->loadHandles();
     $objects = $query->loadObjects();
     if ($poll->getShuffle()) {
         shuffle($options);
     }
     $option_markup = array();
     foreach ($options as $option) {
         $option_markup[] = $this->renderPollOption($poll, $viewer_choices, $option);
     }
     $option_markup = implode("\n", $option_markup);
     $comments_by_option = array();
     switch ($poll->getMethod()) {
         case PhabricatorSlowvotePoll::METHOD_PLURALITY:
             $choice_ids = array();
             foreach ($choices_by_user as $user_phid => $user_choices) {
                 $choice_ids[$user_phid] = head($user_choices)->getOptionID();
             }
             foreach ($comments as $comment) {
                 $choice = idx($choice_ids, $comment->getAuthorPHID());
                 if ($choice) {
                     $comments_by_option[$choice][] = $comment;
                 }
             }
             break;
         case PhabricatorSlowvotePoll::METHOD_APPROVAL:
             // All comments are grouped in approval voting.
             break;
         default:
             throw new Exception("Unknown poll method!");
     }
     $result_markup = $this->renderResultMarkup($poll, $options, $choices, $comments, $viewer_choices, $choices_by_option, $comments_by_option, $handles, $objects);
     if ($viewer_choices) {
         $instructions = 'Your vote has been recorded... but there is still ample time to ' . 'rethink your position. Have you thoroughly considered all possible ' . 'eventualities?';
     } else {
         $instructions = 'This is a weighty matter indeed. Consider your choices with the ' . 'greatest of care.';
     }
     $form = id(new AphrontFormView())->setUser($user)->appendChild('<p class="aphront-form-instructions">' . $instructions . '</p>')->appendChild(id(new AphrontFormMarkupControl())->setLabel('Vote')->setValue($option_markup))->appendChild(id(new AphrontFormTextAreaControl())->setLabel('Comments')->setHeight(AphrontFormTextAreaControl::HEIGHT_SHORT)->setName('comments')->setValue($comment_text))->appendChild(id(new AphrontFormSubmitControl())->setValue('Cautiously Engage in Deliberations'));
     $panel = new AphrontPanelView();
     $panel->setHeader(phutil_escape_html($poll->getQuestion()));
     $panel->setWidth(AphrontPanelView::WIDTH_WIDE);
     $panel->appendChild($form);
     $panel->appendChild('<br /><br />');
     $panel->appendChild($result_markup);
     return $this->buildStandardPageResponse($panel, array('title' => 'V' . $poll->getID() . ' ' . $poll->getQuestion()));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $handle_data = new PhabricatorObjectHandleData(array($this->phid));
     $handles = $handle_data->loadHandles();
     $handle = $handles[$this->phid];
     $object_phid = $this->phid;
     $object_type = $handle->getType();
     $attach_type = $this->type;
     $objects = $handle_data->loadObjects();
     $object = idx($objects, $this->phid);
     if (!$object) {
         return new Aphront404Response();
     }
     if ($request->isFormPost()) {
         $phids = explode(';', $request->getStr('phids'));
         $phids = array_filter($phids);
         $phids = array_values($phids);
         switch ($this->action) {
             case self::ACTION_MERGE:
                 return $this->performMerge($object, $handle, $phids);
             case self::ACTION_ATTACH:
                 // Fall through to the workflow below.
                 break;
             default:
                 throw new Exception("Unsupported attach action.");
         }
         // sort() so that removing [X, Y] and then adding [Y, X] is correctly
         // detected as a no-op.
         sort($phids);
         $old_phids = $object->getAttachedPHIDs($attach_type);
         sort($old_phids);
         if (($phids || $old_phids) && $phids !== $old_phids) {
             $all_phids = array_merge($phids, $old_phids);
             $attach_objs = id(new PhabricatorObjectHandleData($all_phids))->loadObjects();
             // Remove PHIDs which don't actually exist, to prevent silliness.
             $phids = array_keys(array_select_keys($attach_objs, $phids));
             if ($phids) {
                 $phids = array_combine($phids, $phids);
             }
             // Update the primary object.
             switch ($object_type) {
                 case PhabricatorPHIDConstants::PHID_TYPE_DREV:
                     $object->setAttachedPHIDs($attach_type, $phids);
                     $object->save();
                     break;
                 case PhabricatorPHIDConstants::PHID_TYPE_TASK:
                     $this->applyTaskTransaction($object, $attach_type, $phids);
                     break;
             }
             // Loop through all of the attached/detached objects and update them.
             foreach ($attach_objs as $phid => $attach_obj) {
                 $attached_phids = $attach_obj->getAttachedPHIDs($object_type);
                 // Figure out if we're attaching or detaching this object.
                 if (isset($phids[$phid])) {
                     $attached_phids[] = $object_phid;
                 } else {
                     $attached_phids = array_fill_keys($attached_phids, true);
                     unset($attached_phids[$object_phid]);
                     $attached_phids = array_keys($attached_phids);
                 }
                 switch ($attach_type) {
                     case PhabricatorPHIDConstants::PHID_TYPE_DREV:
                         $attach_obj->setAttachedPHIDs($object_type, $attached_phids);
                         $attach_obj->save();
                         break;
                     case PhabricatorPHIDConstants::PHID_TYPE_TASK:
                         $this->applyTaskTransaction($attach_obj, $object_type, $attached_phids);
                         break;
                 }
             }
         }
         return id(new AphrontReloadResponse())->setURI($handle->getURI());
     } else {
         switch ($this->action) {
             case self::ACTION_ATTACH:
                 $phids = $object->getAttachedPHIDs($attach_type);
                 break;
             default:
                 $phids = array();
                 break;
         }
     }
     switch ($this->type) {
         case PhabricatorPHIDConstants::PHID_TYPE_DREV:
             $noun = 'Revisions';
             $selected = 'created';
             break;
         case PhabricatorPHIDConstants::PHID_TYPE_TASK:
             $noun = 'Tasks';
             $selected = 'assigned';
             break;
     }
     switch ($this->action) {
         case self::ACTION_ATTACH:
             $dialog_title = "Manage Attached {$noun}";
             $header_text = "Currently Attached {$noun}";
             $button_text = "Save {$noun}";
             $instructions = null;
             break;
         case self::ACTION_MERGE:
             $dialog_title = "Merge Duplicate Tasks";
             $header_text = "Tasks To Merge";
             $button_text = "Merge {$noun}";
             $instructions = "These tasks will be merged into the current task and then closed. " . "The current task (\"" . phutil_escape_html($handle->getName()) . "\") " . "will grow stronger.";
             break;
     }
     $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles();
     $obj_dialog = new PhabricatorObjectSelectorDialog();
     $obj_dialog->setUser($user)->setHandles($handles)->setFilters(array('assigned' => 'Assigned to Me', 'created' => 'Created By Me', 'open' => 'All Open ' . $noun, 'all' => 'All ' . $noun))->setSelectedFilter($selected)->setCancelURI($handle->getURI())->setSearchURI('/search/select/' . $attach_type . '/')->setTitle($dialog_title)->setHeader($header_text)->setButtonText($button_text)->setInstructions($instructions);
     $dialog = $obj_dialog->buildDialog();
     return id(new AphrontDialogResponse())->setDialog($dialog);
 }