protected function loadPage()
 {
     $table = new LegalpadDocument();
     $conn_r = $table->establishConnection('r');
     $data = queryfx_all($conn_r, 'SELECT d.* FROM %T d %Q %Q %Q %Q %Q', $table->getTableName(), $this->buildJoinClause($conn_r), $this->buildWhereClause($conn_r), $this->buildGroupClause($conn_r), $this->buildOrderClause($conn_r), $this->buildLimitClause($conn_r));
     $documents = $table->loadAllFromArray($data);
     return $documents;
 }
 private function buildAddCommentView(LegalpadDocument $document, $comment_form_id)
 {
     $viewer = $this->getViewer();
     $draft = PhabricatorDraft::newFromUserAndKey($viewer, $document->getPHID());
     $is_serious = PhabricatorEnv::getEnvConfig('phabricator.serious-business');
     $title = $is_serious ? pht('Add Comment') : pht('Debate Legislation');
     $form = id(new PhabricatorApplicationTransactionCommentView())->setUser($viewer)->setObjectPHID($document->getPHID())->setFormID($comment_form_id)->setHeaderText($title)->setDraft($draft)->setSubmitButtonName(pht('Add Comment'))->setAction($this->getApplicationURI('/comment/' . $document->getID() . '/'))->setRequestURI($this->getRequest()->getRequestURI());
     return $form;
 }
 private function sendVerifySignatureEmail(LegalpadDocument $doc, LegalpadDocumentSignature $signature)
 {
     $signature_data = $signature->getSignatureData();
     $email = new PhutilEmailAddress($signature_data['email']);
     $doc_name = $doc->getTitle();
     $doc_link = PhabricatorEnv::getProductionURI('/' . $doc->getMonogram());
     $path = $this->getApplicationURI(sprintf('/verify/%s/', $signature->getSecretKey()));
     $link = PhabricatorEnv::getProductionURI($path);
     $name = idx($signature_data, 'name');
     $body = pht("%s:\n\n" . "This email address was used to sign a Legalpad document " . "in Phabricator:\n\n" . "  %s\n\n" . "Please verify you own this email address and accept the " . "agreement by clicking this link:\n\n" . "  %s\n\n" . "Your signature is not valid until you complete this " . "verification step.\n\nYou can review the document here:\n\n" . "  %s\n", $name, $doc_name, $link, $doc_link);
     id(new PhabricatorMetaMTAMail())->addRawTos(array($email->getAddress()))->setSubject(pht('[Legalpad] Signature Verification'))->setForceDelivery(true)->setBody($body)->setRelatedPHID($signature->getDocumentPHID())->saveAndSend();
 }
 public function handleRequest(AphrontRequest $request)
 {
     $user = $request->getUser();
     $id = $request->getURIData('id');
     if (!$id) {
         $is_create = true;
         $this->requireApplicationCapability(LegalpadCreateDocumentsCapability::CAPABILITY);
         $document = LegalpadDocument::initializeNewDocument($user);
         $body = id(new LegalpadDocumentBody())->setCreatorPHID($user->getPHID());
         $document->attachDocumentBody($body);
         $document->setDocumentBodyPHID(PhabricatorPHIDConstants::PHID_VOID);
     } else {
         $is_create = false;
         $document = id(new LegalpadDocumentQuery())->setViewer($user)->needDocumentBodies(true)->requireCapabilities(array(PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_EDIT))->withIDs(array($id))->executeOne();
         if (!$document) {
             return new Aphront404Response();
         }
     }
     $e_title = true;
     $e_text = true;
     $title = $document->getDocumentBody()->getTitle();
     $text = $document->getDocumentBody()->getText();
     $v_signature_type = $document->getSignatureType();
     $v_preamble = $document->getPreamble();
     $v_require_signature = $document->getRequireSignature();
     $errors = array();
     $can_view = null;
     $can_edit = null;
     if ($request->isFormPost()) {
         $xactions = array();
         $title = $request->getStr('title');
         if (!strlen($title)) {
             $e_title = pht('Required');
             $errors[] = pht('The document title may not be blank.');
         } else {
             $xactions[] = id(new LegalpadTransaction())->setTransactionType(LegalpadTransaction::TYPE_TITLE)->setNewValue($title);
         }
         $text = $request->getStr('text');
         if (!strlen($text)) {
             $e_text = pht('Required');
             $errors[] = pht('The document may not be blank.');
         } else {
             $xactions[] = id(new LegalpadTransaction())->setTransactionType(LegalpadTransaction::TYPE_TEXT)->setNewValue($text);
         }
         $can_view = $request->getStr('can_view');
         $xactions[] = id(new LegalpadTransaction())->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY)->setNewValue($can_view);
         $can_edit = $request->getStr('can_edit');
         $xactions[] = id(new LegalpadTransaction())->setTransactionType(PhabricatorTransactions::TYPE_EDIT_POLICY)->setNewValue($can_edit);
         if ($is_create) {
             $v_signature_type = $request->getStr('signatureType');
             $xactions[] = id(new LegalpadTransaction())->setTransactionType(LegalpadTransaction::TYPE_SIGNATURE_TYPE)->setNewValue($v_signature_type);
         }
         $v_preamble = $request->getStr('preamble');
         $xactions[] = id(new LegalpadTransaction())->setTransactionType(LegalpadTransaction::TYPE_PREAMBLE)->setNewValue($v_preamble);
         $v_require_signature = $request->getBool('requireSignature', 0);
         if ($v_require_signature) {
             if (!$user->getIsAdmin()) {
                 $errors[] = pht('Only admins may require signature.');
             }
             $individual = LegalpadDocument::SIGNATURE_TYPE_INDIVIDUAL;
             if ($v_signature_type != $individual) {
                 $errors[] = pht('Only documents with signature type "individual" may require ' . 'signing to use Phabricator.');
             }
         }
         if ($user->getIsAdmin()) {
             $xactions[] = id(new LegalpadTransaction())->setTransactionType(LegalpadTransaction::TYPE_REQUIRE_SIGNATURE)->setNewValue($v_require_signature);
         }
         if (!$errors) {
             $editor = id(new LegalpadDocumentEditor())->setContentSourceFromRequest($request)->setContinueOnNoEffect(true)->setActor($user);
             $xactions = $editor->applyTransactions($document, $xactions);
             return id(new AphrontRedirectResponse())->setURI($this->getApplicationURI('view/' . $document->getID()));
         }
     }
     if ($errors) {
         // set these to what was specified in the form on post
         $document->setViewPolicy($can_view);
         $document->setEditPolicy($can_edit);
     }
     $form = id(new AphrontFormView())->setUser($user)->appendChild(id(new AphrontFormTextControl())->setID('document-title')->setLabel(pht('Title'))->setError($e_title)->setValue($title)->setName('title'));
     if ($is_create) {
         $form->appendChild(id(new AphrontFormSelectControl())->setLabel(pht('Who Should Sign?'))->setName(pht('signatureType'))->setValue($v_signature_type)->setOptions(LegalpadDocument::getSignatureTypeMap()));
         $show_require = true;
         $caption = pht('Applies only to documents individuals sign.');
     } else {
         $form->appendChild(id(new AphrontFormMarkupControl())->setLabel(pht('Who Should Sign?'))->setValue($document->getSignatureTypeName()));
         $individual = LegalpadDocument::SIGNATURE_TYPE_INDIVIDUAL;
         $show_require = $document->getSignatureType() == $individual;
         $caption = null;
     }
     if ($show_require) {
         $form->appendChild(id(new AphrontFormCheckboxControl())->setDisabled(!$user->getIsAdmin())->setLabel(pht('Require Signature'))->addCheckbox('requireSignature', 'requireSignature', pht('Should signing this document be required to use Phabricator?'), $v_require_signature)->setCaption($caption));
     }
     $form->appendChild(id(new PhabricatorRemarkupControl())->setUser($user)->setID('preamble')->setLabel(pht('Preamble'))->setValue($v_preamble)->setName('preamble')->setCaption(pht('Optional help text for users signing this document.')))->appendChild(id(new PhabricatorRemarkupControl())->setUser($user)->setID('document-text')->setLabel(pht('Document Body'))->setError($e_text)->setValue($text)->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)->setName('text'));
     $policies = id(new PhabricatorPolicyQuery())->setViewer($user)->setObject($document)->execute();
     $form->appendChild(id(new AphrontFormPolicyControl())->setUser($user)->setCapability(PhabricatorPolicyCapability::CAN_VIEW)->setPolicyObject($document)->setPolicies($policies)->setName('can_view'))->appendChild(id(new AphrontFormPolicyControl())->setUser($user)->setCapability(PhabricatorPolicyCapability::CAN_EDIT)->setPolicyObject($document)->setPolicies($policies)->setName('can_edit'));
     $crumbs = $this->buildApplicationCrumbs($this->buildSideNav());
     $submit = new AphrontFormSubmitControl();
     if ($is_create) {
         $submit->setValue(pht('Create Document'));
         $submit->addCancelButton($this->getApplicationURI());
         $title = pht('Create Document');
         $short = pht('Create');
     } else {
         $submit->setValue(pht('Save Document'));
         $submit->addCancelButton($this->getApplicationURI('view/' . $document->getID()));
         $title = pht('Edit Document');
         $short = pht('Edit');
         $crumbs->addTextCrumb($document->getMonogram(), $this->getApplicationURI('view/' . $document->getID()));
     }
     $form->appendChild($submit);
     $form_box = id(new PHUIObjectBoxView())->setHeaderText($title)->setFormErrors($errors)->setForm($form);
     $crumbs->addTextCrumb($short);
     $preview = id(new PHUIRemarkupPreviewPanel())->setHeader(pht('Document Preview'))->setPreviewURI($this->getApplicationURI('document/preview/'))->setControlID('document-text')->addClass('phui-document-view');
     return $this->buildApplicationPage(array($crumbs, $form_box, $preview), array('title' => $title));
 }
<?php

echo pht("Populating Legalpad Documents with titles, %s, and %s...\n", 'recentContributorPHIDs', 'contributorCounts');
$table = new LegalpadDocument();
$table->openTransaction();
foreach (new LiskMigrationIterator($table) as $document) {
    $updated = false;
    $id = $document->getID();
    echo pht('Document %d: ', $id);
    if (!$document->getTitle()) {
        $document_body = id(new LegalpadDocumentBody())->loadOneWhere('phid = %s', $document->getDocumentBodyPHID());
        $title = $document_body->getTitle();
        $document->setTitle($title);
        $updated = true;
        echo pht('Added title: %s', $title) . "\n";
    } else {
        echo "-\n";
    }
    if (!$document->getContributorCount() || !$document->getRecentContributorPHIDs()) {
        $updated = true;
        $type = PhabricatorObjectHasContributorEdgeType::EDGECONST;
        $contributors = PhabricatorEdgeQuery::loadDestinationPHIDs($document->getPHID(), $type);
        $document->setRecentContributorPHIDs(array_slice($contributors, 0, 3));
        echo pht('Added recent contributor PHIDs.') . "\n";
        $document->setContributorCount(count($contributors));
        echo pht('Added contributor count.') . "\n";
    }
    if (!$updated) {
        echo "-\n";
        continue;
    }