public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $document = id(new PhrictionDocument())->loadOneWhere('slug = %s', PhabricatorSlug::normalize($this->slug));
     if (!$document) {
         return new Aphront404Response();
     }
     $current = id(new PhrictionContent())->load($document->getContentID());
     $pager = new AphrontPagerView();
     $pager->setOffset($request->getInt('page'));
     $pager->setURI($request->getRequestURI(), 'page');
     $history = id(new PhrictionContent())->loadAllWhere('documentID = %d ORDER BY version DESC LIMIT %d, %d', $document->getID(), $pager->getOffset(), $pager->getPageSize() + 1);
     $history = $pager->sliceResults($history);
     $author_phids = mpull($history, 'getAuthorPHID');
     $handles = id(new PhabricatorObjectHandleData($author_phids))->loadHandles();
     $rows = array();
     foreach ($history as $content) {
         $uri = PhrictionDocument::getSlugURI($document->getSlug());
         $version = $content->getVersion();
         $diff_uri = new PhutilURI('/phriction/diff/' . $document->getID() . '/');
         $vs_previous = '<em>Created</em>';
         if ($content->getVersion() != 1) {
             $uri = $diff_uri->alter('l', $content->getVersion() - 1)->alter('r', $content->getVersion());
             $vs_previous = phutil_render_tag('a', array('href' => $uri), 'Show Change');
         }
         $vs_head = '<em>Current</em>';
         if ($content->getID() != $document->getContentID()) {
             $uri = $diff_uri->alter('l', $content->getVersion())->alter('r', $current->getVersion());
             $vs_head = phutil_render_tag('a', array('href' => $uri), 'Show Later Changes');
         }
         $change_type = PhrictionChangeType::getChangeTypeLabel($content->getChangeType());
         $rows[] = array(phabricator_date($content->getDateCreated(), $user), phabricator_time($content->getDateCreated(), $user), phutil_render_tag('a', array('href' => $uri . '?v=' . $version), 'Version ' . $version), $handles[$content->getAuthorPHID()]->renderLink(), $change_type, phutil_escape_html($content->getDescription()), $vs_previous, $vs_head);
     }
     $crumbs = new AphrontCrumbsView();
     $crumbs->setCrumbs(array('Phriction', phutil_render_tag('a', array('href' => PhrictionDocument::getSlugURI($document->getSlug())), phutil_escape_html($current->getTitle())), 'History'));
     $table = new AphrontTableView($rows);
     $table->setHeaders(array('Date', 'Time', 'Version', 'Author', 'Type', 'Description', 'Against Previous', 'Against Current'));
     $table->setColumnClasses(array('', 'right', 'pri', '', '', 'wide', '', ''));
     $panel = new AphrontPanelView();
     $panel->setHeader('Document History');
     $panel->appendChild($table);
     $panel->appendChild($pager);
     return $this->buildStandardPageResponse(array($crumbs, $panel), array('title' => 'Document History'));
 }
 private function renderComparisonTable(array $content)
 {
     assert_instances_of($content, 'PhrictionContent');
     $user = $this->getRequest()->getUser();
     $phids = mpull($content, 'getAuthorPHID');
     $handles = $this->loadViewerHandles($phids);
     $list = new PHUIObjectItemListView();
     $list->setFlush(true);
     $first = true;
     foreach ($content as $c) {
         $author = $handles[$c->getAuthorPHID()]->renderLink();
         $item = id(new PHUIObjectItemView())->setHeader(pht('%s by %s, %s', PhrictionChangeType::getChangeTypeLabel($c->getChangeType()), $author, pht('Version %s', $c->getVersion())))->addAttribute(pht('%s %s', phabricator_date($c->getDateCreated(), $user), phabricator_time($c->getDateCreated(), $user)));
         if ($c->getDescription()) {
             $item->addAttribute($c->getDescription());
         }
         if ($first == true) {
             $item->setBarColor('green');
             $first = false;
         } else {
             $item->setBarColor('red');
         }
         $list->addItem($item);
     }
     return $list;
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $document = id(new PhrictionDocumentQuery())->setViewer($user)->withSlugs(array(PhabricatorSlug::normalize($this->slug)))->needContent(true)->executeOne();
     if (!$document) {
         return new Aphront404Response();
     }
     $current = $document->getContent();
     $pager = new AphrontPagerView();
     $pager->setOffset($request->getInt('page'));
     $pager->setURI($request->getRequestURI(), 'page');
     $history = id(new PhrictionContent())->loadAllWhere('documentID = %d ORDER BY version DESC LIMIT %d, %d', $document->getID(), $pager->getOffset(), $pager->getPageSize() + 1);
     $history = $pager->sliceResults($history);
     $author_phids = mpull($history, 'getAuthorPHID');
     $handles = $this->loadViewerHandles($author_phids);
     $list = new PHUIObjectItemListView();
     $list->setFlush(true);
     foreach ($history as $content) {
         $author = $handles[$content->getAuthorPHID()]->renderLink();
         $slug_uri = PhrictionDocument::getSlugURI($document->getSlug());
         $version = $content->getVersion();
         $diff_uri = new PhutilURI('/phriction/diff/' . $document->getID() . '/');
         $vs_previous = null;
         if ($content->getVersion() != 1) {
             $vs_previous = $diff_uri->alter('l', $content->getVersion() - 1)->alter('r', $content->getVersion());
         }
         $vs_head = null;
         if ($content->getID() != $document->getContentID()) {
             $vs_head = $diff_uri->alter('l', $content->getVersion())->alter('r', $current->getVersion());
         }
         $change_type = PhrictionChangeType::getChangeTypeLabel($content->getChangeType());
         switch ($content->getChangeType()) {
             case PhrictionChangeType::CHANGE_DELETE:
                 $color = 'red';
                 break;
             case PhrictionChangeType::CHANGE_EDIT:
                 $color = 'blue';
                 break;
             case PhrictionChangeType::CHANGE_MOVE_HERE:
                 $color = 'yellow';
                 break;
             case PhrictionChangeType::CHANGE_MOVE_AWAY:
                 $color = 'orange';
                 break;
             case PhrictionChangeType::CHANGE_STUB:
                 $color = 'green';
                 break;
             default:
                 throw new Exception('Unknown change type!');
                 break;
         }
         $item = id(new PHUIObjectItemView())->setHeader(pht('%s by %s', $change_type, $author))->setBarColor($color)->addAttribute(phutil_tag('a', array('href' => $slug_uri . '?v=' . $version), pht('Version %s', $version)))->addAttribute(pht('%s %s', phabricator_date($content->getDateCreated(), $user), phabricator_time($content->getDateCreated(), $user)));
         if ($content->getDescription()) {
             $item->addAttribute($content->getDescription());
         }
         if ($vs_previous) {
             $item->addIcon('fa-reply', pht('Show Change'), array('href' => $vs_previous));
         } else {
             $item->addIcon('fa-reply grey', phutil_tag('em', array(), pht('No previous change')));
         }
         if ($vs_head) {
             $item->addIcon('fa-reply-all', pht('Show Later Changes'), array('href' => $vs_head));
         } else {
             $item->addIcon('fa-reply-all grey', phutil_tag('em', array(), pht('No later changes')));
         }
         $list->addItem($item);
     }
     $crumbs = $this->buildApplicationCrumbs();
     $crumb_views = $this->renderBreadcrumbs($document->getSlug());
     foreach ($crumb_views as $view) {
         $crumbs->addCrumb($view);
     }
     $crumbs->addTextCrumb(pht('History'), PhrictionDocument::getSlugURI($document->getSlug(), 'history'));
     $header = new PHUIHeaderView();
     $header->setHeader(pht('Document History for %s', phutil_tag('a', array('href' => PhrictionDocument::getSlugURI($document->getSlug())), head($history)->getTitle())));
     $obj_box = id(new PHUIObjectBoxView())->setHeader($header)->appendChild($list)->appendChild($pager);
     return $this->buildApplicationPage(array($crumbs, $obj_box), array('title' => pht('Document History')));
 }
 private function sendMailToSubscribers(array $subscribers, $old_content)
 {
     if (!$subscribers) {
         return;
     }
     $author_phid = $this->getActor()->getPHID();
     $document = $this->document;
     $content = $document->getContent();
     $slug_uri = PhrictionDocument::getSlugURI($document->getSlug());
     $diff_uri = new PhutilURI('/phriction/diff/' . $document->getID() . '/');
     $prod_uri = PhabricatorEnv::getProductionURI('');
     $vs_head = $diff_uri->alter('l', $old_content->getVersion())->alter('r', $content->getVersion());
     $old_title = $old_content->getTitle();
     $title = $content->getTitle();
     $name = $this->getChangeTypeDescription($content->getChangeType(), $title);
     $action = PhrictionChangeType::getChangeTypeLabel($content->getChangeType());
     $body = array($name);
     // Content may have changed, you never know
     if ($content->getChangeType() == PhrictionChangeType::CHANGE_EDIT) {
         if ($old_title != $title) {
             $body[] = pht('Title was changed from "%s" to "%s"', $old_title, $title);
         }
         $body[] = pht("Link to new version:\n%s", $prod_uri . $slug_uri . '?v=' . $content->getVersion());
         $body[] = pht("Link to diff:\n%s", $prod_uri . $vs_head);
     } else {
         if ($content->getChangeType() == PhrictionChangeType::CHANGE_MOVE_AWAY) {
             $target_document = id(new PhrictionDocument())->load($content->getChangeRef());
             $slug_uri = PhrictionDocument::getSlugURI($target_document->getSlug());
             $body[] = pht("Link to destination document:\n%s", $prod_uri . $slug_uri);
         }
     }
     $body = implode("\n\n", $body);
     $subject_prefix = $this->getMailSubjectPrefix();
     $mail = new PhabricatorMetaMTAMail();
     $mail->setSubject($name)->setSubjectPrefix($subject_prefix)->setVarySubjectPrefix('[' . $action . ']')->addHeader('Thread-Topic', $name)->setFrom($author_phid)->addTos($subscribers)->setBody($body)->setRelatedPHID($document->getPHID())->setIsBulk(true);
     $mail->saveAndSend();
 }