/**
  * @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 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 static function loadOneHandle($phid, $viewer = null)
 {
     $query = new PhabricatorObjectHandleData(array($phid));
     if ($viewer) {
         $query->setViewer($viewer);
     }
     $handles = $query->loadHandles();
     return $handles[$phid];
 }
 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);
 }
 public final function getBlameData()
 {
     $raw_data = $this->getRawData();
     $text_list = array();
     $rev_list = array();
     $blame_dict = array();
     if (!$this->getNeedsBlame()) {
         $text_list = explode("\n", rtrim($raw_data));
     } else {
         $lines = array();
         foreach (explode("\n", rtrim($raw_data)) as $k => $line) {
             $lines[$k] = $this->tokenizeLine($line);
             list($rev_id, $author, $text) = $lines[$k];
             $text_list[$k] = $text;
             $rev_list[$k] = $rev_id;
         }
         $rev_list = $this->processRevList($rev_list);
         foreach ($lines as $k => $line) {
             list($rev_id, $author, $text) = $line;
             $rev_id = $rev_list[$k];
             if (!isset($blame_dict[$rev_id])) {
                 $blame_dict[$rev_id]['author'] = $author;
             }
         }
         $repository = $this->getRequest()->getRepository();
         $commits = id(new PhabricatorAuditCommitQuery())->withIdentifiers($repository->getID(), array_unique($rev_list))->execute();
         foreach ($commits as $commit) {
             $blame_dict[$commit->getCommitIdentifier()]['epoch'] = $commit->getEpoch();
         }
         $commits_data = array();
         if ($commits) {
             $commits_data = id(new PhabricatorRepositoryCommitData())->loadAllWhere('commitID IN (%Ls)', mpull($commits, 'getID'));
         }
         $phids = array();
         foreach ($commits_data as $data) {
             $phids[] = $data->getCommitDetail('authorPHID');
         }
         $loader = new PhabricatorObjectHandleData(array_unique($phids));
         if ($this->viewer) {
             $loader->setViewer($this->viewer);
         }
         $handles = $loader->loadHandles();
         foreach ($commits_data as $data) {
             if ($data->getCommitDetail('authorPHID')) {
                 $commit_identifier = $commits[$data->getCommitID()]->getCommitIdentifier();
                 $blame_dict[$commit_identifier]['handle'] = $handles[$data->getCommitDetail('authorPHID')];
             }
         }
     }
     return array($text_list, $rev_list, $blame_dict);
 }
 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;
 }
 private function sendErrorEmail($error, PhabricatorMetaMTAReceivedMail $mail)
 {
     $template = new PhabricatorMetaMTAMail();
     $template->setSubject('Exception: unable to process your mail request');
     $template->setBody($this->buildErrorMailBody($error, $mail));
     $template->setRelatedPHID($mail->getRelatedPHID());
     $phid = $this->getActor()->getPHID();
     $tos = array($phid => PhabricatorObjectHandleData::loadOneHandle($phid));
     $mails = $this->multiplexMail($template, $tos, array());
     foreach ($mails as $email) {
         $email->saveAndSend();
     }
     return true;
 }
 public function renderValueForMail($phase)
 {
     $status = $this->getRevision()->getStatus();
     if ($status != ArcanistDifferentialRevisionStatus::NEEDS_REVISION && $status != ArcanistDifferentialRevisionStatus::ACCEPTED) {
         return null;
     }
     $diff = $this->getRevision()->loadActiveDiff();
     if ($diff) {
         $phid = $diff->getArcanistProjectPHID();
         if ($phid) {
             $handle = PhabricatorObjectHandleData::loadOneHandle($phid);
             return "ARCANIST PROJECT\n  " . $handle->getName();
         }
     }
 }
 public function reindexAbstractDocument(PhabricatorSearchAbstractDocument $doc)
 {
     $type = $doc->getDocumentType();
     $phid = $doc->getPHID();
     $handle = PhabricatorObjectHandleData::loadOneHandle($phid);
     // URL is not used internally but it can be useful externally.
     $spec = array('title' => $doc->getDocumentTitle(), 'url' => PhabricatorEnv::getProductionURI($handle->getURI()), 'dateCreated' => $doc->getDocumentCreated(), '_timestamp' => $doc->getDocumentModified(), 'field' => array(), 'relationship' => array());
     foreach ($doc->getFieldData() as $field) {
         $spec['field'][] = array_combine(array('type', 'corpus', 'aux'), $field);
     }
     foreach ($doc->getRelationshipData() as $relationship) {
         list($rtype, $to_phid, $to_type, $time) = $relationship;
         $spec['relationship'][$rtype][] = array('phid' => $to_phid, 'phidType' => $to_type, 'when' => $time);
     }
     $this->executeRequest("/phabricator/{$type}/{$phid}/", $spec, $is_write = true);
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $phid = $this->phid;
     $handle = PhabricatorObjectHandleData::loadOneHandle($phid);
     if (!$handle->isComplete()) {
         return new Aphront404Response();
     }
     $flag = PhabricatorFlagQuery::loadUserFlag($user, $phid);
     if (!$flag) {
         $flag = new PhabricatorFlag();
         $flag->setOwnerPHID($user->getPHID());
         $flag->setType($handle->getType());
         $flag->setObjectPHID($handle->getPHID());
         $flag->setReasonPHID($user->getPHID());
     }
     if ($request->isDialogFormPost()) {
         $flag->setColor($request->getInt('color'));
         $flag->setNote($request->getStr('note'));
         $flag->save();
         return id(new AphrontReloadResponse())->setURI('/flag/');
     }
     $type_name = $handle->getTypeName();
     $dialog = new AphrontDialogView();
     $dialog->setUser($user);
     $dialog->setTitle("Flag {$type_name}");
     require_celerity_resource('phabricator-flag-css');
     $form = new AphrontFormLayoutView();
     $is_new = !$flag->getID();
     if ($is_new) {
         $form->appendChild("<p>You can flag this {$type_name} if you want to remember to look " . "at it later.</p><br />");
     }
     $radio = new AphrontFormRadioButtonControl();
     foreach (PhabricatorFlagColor::getColorNameMap() as $color => $text) {
         $class = 'phabricator-flag-radio phabricator-flag-color-' . $color;
         $radio->addButton($color, $text, '', $class);
     }
     $form->appendChild($radio->setName('color')->setLabel('Flag Color')->setValue($flag->getColor()))->appendChild(id(new AphrontFormTextAreaControl())->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_SHORT)->setName('note')->setLabel('Note')->setValue($flag->getNote()));
     $dialog->appendChild($form);
     $dialog->addCancelButton($handle->getURI());
     $dialog->addSubmitButton($is_new ? "Flag {$type_name}" : 'Save');
     return id(new AphrontDialogResponse())->setDialog($dialog);
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     if (!$request->isFormPost()) {
         return new Aphront400Response();
     }
     switch ($this->action) {
         case 'add':
             $is_add = true;
             break;
         case 'delete':
             $is_add = false;
             break;
         default:
             return new Aphront400Response();
     }
     $user = $request->getUser();
     $phid = $this->phid;
     // TODO: This is a policy test because `loadObjects()` is not currently
     // policy-aware. Once it is, we can collapse this.
     $handle = PhabricatorObjectHandleData::loadOneHandle($phid, $user);
     if (!$handle->isComplete()) {
         return new Aphront404Response();
     }
     $objects = id(new PhabricatorObjectHandleData(array($phid)))->loadObjects();
     $object = idx($objects, $phid);
     if (!$object instanceof PhabricatorSubscribableInterface) {
         return $this->buildErrorResponse(pht('Bad Object'), pht('This object is not subscribable.'), $handle->getURI());
     }
     if ($object->isAutomaticallySubscribed($user->getPHID())) {
         return $this->buildErrorResponse(pht('Automatically Subscribed'), pht('You are automatically subscribed to this object.'), $handle->getURI());
     }
     $editor = id(new PhabricatorSubscriptionsEditor())->setUser($user)->setObject($object);
     if ($is_add) {
         $editor->subscribeExplicit(array($user->getPHID()), $explicit = true);
     } else {
         $editor->unsubscribe(array($user->getPHID()));
     }
     $editor->save();
     // TODO: We should just render the "Unsubscribe" action and swap it out
     // in the document for Ajax requests.
     return id(new AphrontReloadResponse())->setURI($handle->getURI());
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $paste = id(new PhabricatorPasteQuery())->setViewer($user)->withPasteIDs(array($this->id))->executeOne();
     if (!$paste) {
         return new Aphront404Response();
     }
     $file = id(new PhabricatorFile())->loadOneWhere('phid = %s', $paste->getFilePHID());
     if (!$file) {
         return new Aphront400Response();
     }
     $corpus = $this->buildCorpus($paste, $file);
     $paste_panel = new AphrontPanelView();
     $author_phid = $paste->getAuthorPHID();
     $header = 'Viewing Paste ' . $paste->getID() . ' by ' . PhabricatorObjectHandleData::loadOneHandle($author_phid)->renderLink();
     if (strlen($paste->getTitle())) {
         $header .= ' - ' . phutil_escape_html($paste->getTitle());
     }
     $paste_panel->setHeader($header);
     $paste_panel->setWidth(AphrontPanelView::WIDTH_FULL);
     $paste_panel->addButton(phutil_render_tag('a', array('href' => '/paste/?fork=' . $paste->getID(), 'class' => 'green button'), 'Fork This'));
     $raw_uri = $file->getBestURI();
     $paste_panel->addButton(phutil_render_tag('a', array('href' => $raw_uri, 'class' => 'button'), 'View Raw Text'));
     $paste_panel->appendChild($corpus);
     $forks_panel = null;
     $forks_of_this_paste = id(new PhabricatorPaste())->loadAllWhere('parentPHID = %s', $paste->getPHID());
     if ($forks_of_this_paste) {
         $forks_panel = new AphrontPanelView();
         $forks_panel->setHeader("Forks of this paste");
         $forks = array();
         foreach ($forks_of_this_paste as $fork) {
             $forks[] = array($fork->getID(), phutil_render_tag('a', array('href' => '/P' . $fork->getID()), phutil_escape_html($fork->getTitle())));
         }
         $forks_table = new AphrontTableView($forks);
         $forks_table->setHeaders(array('Paste ID', 'Title'));
         $forks_table->setColumnClasses(array(null, 'wide pri'));
         $forks_panel->appendChild($forks_table);
     }
     return $this->buildStandardPageResponse(array($paste_panel, $forks_panel), array('title' => 'Paste: ' . nonempty($paste->getTitle(), 'P' . $paste->getID()), 'tab' => 'view'));
 }
 public static function applyFlagEffect(HeraldEffect $effect, $phid)
 {
     $color = $effect->getTarget();
     // TODO: Silly that we need to load this again here.
     $rule = id(new HeraldRule())->load($effect->getRuleID());
     $user = id(new PhabricatorUser())->loadOneWhere('phid = %s', $rule->getAuthorPHID());
     $flag = PhabricatorFlagQuery::loadUserFlag($user, $phid);
     if ($flag) {
         return new HeraldApplyTranscript($effect, false, 'Object already flagged.');
     }
     $handle = PhabricatorObjectHandleData::loadOneHandle($phid);
     $flag = new PhabricatorFlag();
     $flag->setOwnerPHID($user->getPHID());
     $flag->setType($handle->getType());
     $flag->setObjectPHID($handle->getPHID());
     // TOOD: Should really be transcript PHID, but it doesn't exist yet.
     $flag->setReasonPHID($user->getPHID());
     $flag->setColor($color);
     $flag->setNote('Flagged by Herald Rule "' . $rule->getName() . '".');
     $flag->save();
     return new HeraldApplyTranscript($effect, true, 'Added flag.');
 }
 protected final function updateCommitData($author, $message, $committer = null)
 {
     $commit = $this->commit;
     $data = id(new PhabricatorRepositoryCommitData())->loadOneWhere('commitID = %d', $commit->getID());
     if (!$data) {
         $data = new PhabricatorRepositoryCommitData();
     }
     $data->setCommitID($commit->getID());
     $data->setAuthorName($author);
     $data->setCommitMessage($message);
     if ($committer) {
         $data->setCommitDetail('committer', $committer);
     }
     $repository = $this->repository;
     $detail_parser = $repository->getDetail('detail-parser', 'PhabricatorRepositoryDefaultCommitMessageDetailParser');
     if ($detail_parser) {
         $parser_obj = newv($detail_parser, array($commit, $data));
         $parser_obj->parseCommitDetails();
     }
     $author_phid = $this->lookupUser($commit, $data->getAuthorName(), $data->getCommitDetail('authorPHID'));
     $data->setCommitDetail('authorPHID', $author_phid);
     $committer_phid = $this->lookupUser($commit, $data->getCommitDetail('committer'), $data->getCommitDetail('committerPHID'));
     $data->setCommitDetail('committerPHID', $committer_phid);
     if ($author_phid != $commit->getAuthorPHID()) {
         $commit->setAuthorPHID($author_phid);
         $commit->save();
     }
     $conn_w = id(new DifferentialRevision())->establishConnection('w');
     // NOTE: The `differential_commit` table has a unique ID on `commitPHID`,
     // preventing more than one revision from being associated with a commit.
     // Generally this is good and desirable, but with the advent of hash
     // tracking we may end up in a situation where we match several different
     // revisions. We just kind of ignore this and pick one, we might want to
     // revisit this and do something differently. (If we match several revisions
     // someone probably did something very silly, though.)
     $revision_id = $data->getCommitDetail('differential.revisionID');
     if (!$revision_id) {
         $hashes = $this->getCommitHashes($this->repository, $this->commit);
         if ($hashes) {
             $query = new DifferentialRevisionQuery();
             $query->withCommitHashes($hashes);
             $revisions = $query->execute();
             if (!empty($revisions)) {
                 $revision = $this->identifyBestRevision($revisions);
                 $revision_id = $revision->getID();
             }
         }
     }
     if ($revision_id) {
         $revision = id(new DifferentialRevision())->load($revision_id);
         if ($revision) {
             queryfx($conn_w, 'INSERT IGNORE INTO %T (revisionID, commitPHID) VALUES (%d, %s)', DifferentialRevision::TABLE_COMMIT, $revision->getID(), $commit->getPHID());
             $committer_phid = $data->getCommitDetail('committerPHID');
             if ($committer_phid) {
                 $handle = PhabricatorObjectHandleData::loadOneHandle($committer_phid);
                 $committer_name = '@' . $handle->getName();
             } else {
                 $committer_name = $data->getCommitDetail('committer');
             }
             $author_phid = $data->getCommitDetail('authorPHID');
             if ($author_phid) {
                 $handle = PhabricatorObjectHandleData::loadOneHandle($author_phid);
                 $author_name = '@' . $handle->getName();
             } else {
                 $author_name = $data->getAuthorName();
             }
             $commit_name = $repository->formatCommitName($commit->getCommitIdentifier());
             $info = array();
             $info[] = "authored by {$author_name}";
             if ($committer_name && $committer_name != $author_name) {
                 $info[] = "committed by {$committer_name}";
             }
             $info = implode(', ', $info);
             $message = "Closed by commit {$commit_name} ({$info}).";
             $actor_phid = nonempty($committer_phid, $author_phid, $revision->getAuthorPHID());
             $status_closed = ArcanistDifferentialRevisionStatus::CLOSED;
             $should_close = $revision->getStatus() != $status_closed && $repository->shouldAutocloseCommit($commit, $data);
             if ($should_close) {
                 $diff = $this->attachToRevision($revision, $actor_phid);
                 $revision->setDateCommitted($commit->getEpoch());
                 $editor = new DifferentialCommentEditor($revision, $actor_phid, DifferentialAction::ACTION_CLOSE);
                 $editor->setIsDaemonWorkflow(true);
                 $vs_diff = $this->loadChangedByCommit($diff);
                 if ($vs_diff) {
                     $data->setCommitDetail('vsDiff', $vs_diff->getID());
                     $changed_by_commit = PhabricatorEnv::getProductionURI('/D' . $revision->getID() . '?vs=' . $vs_diff->getID() . '&id=' . $diff->getID() . '#toc');
                     $editor->setChangedByCommit($changed_by_commit);
                 }
                 $editor->setMessage($message)->save();
             }
         }
     }
     $data->save();
 }
 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_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);
 }
 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'));
 }
 private function loadUserName($user_phid, $default)
 {
     if (!$user_phid) {
         return $default;
     }
     $handle = PhabricatorObjectHandleData::loadOneHandle($user_phid);
     return '@' . $handle->getName();
 }
 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);
 }
 protected function attachHandleToFlag($flag)
 {
     $flag->attachHandle(PhabricatorObjectHandleData::loadOneHandle($flag->getObjectPHID()));
 }
 private function rejectObject($object, $policy, $capability)
 {
     if (!$this->raisePolicyExceptions) {
         return;
     }
     // TODO: clean this up
     $verb = $capability;
     $message = "You do not have permission to {$verb} this object.";
     switch ($policy) {
         case PhabricatorPolicies::POLICY_PUBLIC:
             $who = "This is curious, since anyone can {$verb} the object.";
             break;
         case PhabricatorPolicies::POLICY_USER:
             $who = "To {$verb} this object, you must be logged in.";
             break;
         case PhabricatorPolicies::POLICY_ADMIN:
             $who = "To {$verb} this object, you must be an administrator.";
             break;
         case PhabricatorPolicies::POLICY_NOONE:
             $who = "No one can {$verb} this object.";
             break;
         default:
             $type = phid_get_type($policy);
             if ($type == PhabricatorPHIDConstants::PHID_TYPE_PROJ) {
                 $handle = PhabricatorObjectHandleData::loadOneHandle($policy, $this->viewer);
                 $who = "To {$verb} this object, you must be a member of project " . "'" . $handle->getFullName() . "'.";
             } else {
                 $who = "It is unclear who can {$verb} this object.";
             }
             break;
     }
     throw new PhabricatorPolicyException("{$message} {$who}");
 }
 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'));
 }
 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'));
 }
 private function renderAuthorFilter($phid)
 {
     if ($phid) {
         $handle = PhabricatorObjectHandleData::loadOneHandle($phid);
         $tokens = array($phid => $handle->getFullName());
     } else {
         $tokens = array();
     }
     $form = id(new AphrontFormView())->setUser($this->getRequest()->getUser())->appendChild(id(new AphrontFormTokenizerControl())->setName('set_phid')->setValue($tokens)->setLimit(1)->setLabel('Filter Author')->setDataSource('/typeahead/common/accounts/'))->appendChild(id(new AphrontFormSubmitControl())->setValue('Apply Filter'));
     $filter = new AphrontListFilterView();
     $filter->appendChild($form);
     return $filter;
 }