2013-07-03 11:15:45 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class LegalpadDocumentEditController extends LegalpadController {
|
|
|
|
|
2015-02-12 15:22:56 -08:00
|
|
|
public function handleRequest(AphrontRequest $request) {
|
2015-07-29 17:02:10 -07:00
|
|
|
$viewer = $request->getViewer();
|
2015-02-12 15:22:56 -08:00
|
|
|
$id = $request->getURIData('id');
|
2015-07-29 17:02:10 -07:00
|
|
|
|
2015-02-12 15:22:56 -08:00
|
|
|
if (!$id) {
|
2013-07-03 11:15:45 -07:00
|
|
|
$is_create = true;
|
|
|
|
|
2014-06-29 12:43:13 -07:00
|
|
|
$this->requireApplicationCapability(
|
2014-07-25 08:20:39 +10:00
|
|
|
LegalpadCreateDocumentsCapability::CAPABILITY);
|
2014-06-29 12:43:13 -07:00
|
|
|
|
2015-07-29 17:02:10 -07:00
|
|
|
$document = LegalpadDocument::initializeNewDocument($viewer);
|
2013-07-03 11:15:45 -07:00
|
|
|
$body = id(new LegalpadDocumentBody())
|
2015-07-29 17:02:10 -07:00
|
|
|
->setCreatorPHID($viewer->getPHID());
|
2013-07-03 11:15:45 -07:00
|
|
|
$document->attachDocumentBody($body);
|
|
|
|
$document->setDocumentBodyPHID(PhabricatorPHIDConstants::PHID_VOID);
|
|
|
|
} else {
|
|
|
|
$is_create = false;
|
|
|
|
|
|
|
|
$document = id(new LegalpadDocumentQuery())
|
2015-07-29 17:02:10 -07:00
|
|
|
->setViewer($viewer)
|
2013-07-03 11:15:45 -07:00
|
|
|
->needDocumentBodies(true)
|
|
|
|
->requireCapabilities(
|
|
|
|
array(
|
|
|
|
PhabricatorPolicyCapability::CAN_VIEW,
|
|
|
|
PhabricatorPolicyCapability::CAN_EDIT,
|
|
|
|
))
|
2015-02-12 15:22:56 -08:00
|
|
|
->withIDs(array($id))
|
2013-07-03 11:15:45 -07:00
|
|
|
->executeOne();
|
|
|
|
if (!$document) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$e_title = true;
|
|
|
|
$e_text = true;
|
2014-07-04 08:04:28 -07:00
|
|
|
|
|
|
|
$title = $document->getDocumentBody()->getTitle();
|
|
|
|
$text = $document->getDocumentBody()->getText();
|
|
|
|
$v_signature_type = $document->getSignatureType();
|
2014-07-04 09:41:27 -07:00
|
|
|
$v_preamble = $document->getPreamble();
|
2015-02-12 15:22:56 -08:00
|
|
|
$v_require_signature = $document->getRequireSignature();
|
2014-07-04 08:04:28 -07:00
|
|
|
|
2013-07-03 11:15:45 -07:00
|
|
|
$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())
|
2017-05-04 11:53:52 -07:00
|
|
|
->setTransactionType(
|
|
|
|
LegalpadDocumentTitleTransaction::TRANSACTIONTYPE)
|
2013-07-03 11:15:45 -07:00
|
|
|
->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())
|
2017-05-04 11:53:52 -07:00
|
|
|
->setTransactionType(
|
|
|
|
LegalpadDocumentTextTransaction::TRANSACTIONTYPE)
|
2013-07-03 11:15:45 -07:00
|
|
|
->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);
|
|
|
|
|
2014-07-04 08:04:28 -07:00
|
|
|
if ($is_create) {
|
|
|
|
$v_signature_type = $request->getStr('signatureType');
|
|
|
|
$xactions[] = id(new LegalpadTransaction())
|
2017-05-04 11:53:52 -07:00
|
|
|
->setTransactionType(
|
|
|
|
LegalpadDocumentSignatureTypeTransaction::TRANSACTIONTYPE)
|
2014-07-04 08:04:28 -07:00
|
|
|
->setNewValue($v_signature_type);
|
|
|
|
}
|
|
|
|
|
2014-07-04 09:41:27 -07:00
|
|
|
$v_preamble = $request->getStr('preamble');
|
|
|
|
$xactions[] = id(new LegalpadTransaction())
|
2017-05-04 11:53:52 -07:00
|
|
|
->setTransactionType(
|
|
|
|
LegalpadDocumentPreambleTransaction::TRANSACTIONTYPE)
|
2014-07-04 09:41:27 -07:00
|
|
|
->setNewValue($v_preamble);
|
|
|
|
|
2015-02-12 15:22:56 -08:00
|
|
|
$v_require_signature = $request->getBool('requireSignature', 0);
|
|
|
|
if ($v_require_signature) {
|
2015-07-29 17:02:10 -07:00
|
|
|
if (!$viewer->getIsAdmin()) {
|
2015-02-12 15:22:56 -08:00
|
|
|
$errors[] = pht('Only admins may require signature.');
|
|
|
|
}
|
2015-02-17 11:45:20 -08:00
|
|
|
$individual = LegalpadDocument::SIGNATURE_TYPE_INDIVIDUAL;
|
|
|
|
if ($v_signature_type != $individual) {
|
2015-02-12 15:22:56 -08:00
|
|
|
$errors[] = pht(
|
|
|
|
'Only documents with signature type "individual" may require '.
|
|
|
|
'signing to use Phabricator.');
|
|
|
|
}
|
|
|
|
}
|
2015-07-29 17:02:10 -07:00
|
|
|
if ($viewer->getIsAdmin()) {
|
2015-02-12 15:22:56 -08:00
|
|
|
$xactions[] = id(new LegalpadTransaction())
|
2017-05-04 11:53:52 -07:00
|
|
|
->setTransactionType(
|
|
|
|
LegalpadDocumentRequireSignatureTransaction::TRANSACTIONTYPE)
|
2015-02-12 15:22:56 -08:00
|
|
|
->setNewValue($v_require_signature);
|
|
|
|
}
|
|
|
|
|
2013-07-03 11:15:45 -07:00
|
|
|
if (!$errors) {
|
|
|
|
$editor = id(new LegalpadDocumentEditor())
|
|
|
|
->setContentSourceFromRequest($request)
|
|
|
|
->setContinueOnNoEffect(true)
|
2015-07-29 17:02:10 -07:00
|
|
|
->setActor($viewer);
|
2013-07-03 11:15:45 -07:00
|
|
|
|
|
|
|
$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())
|
2015-07-29 17:02:10 -07:00
|
|
|
->setUser($viewer)
|
2013-07-03 11:15:45 -07:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
2013-07-08 17:05:33 -07:00
|
|
|
->setID('document-title')
|
2013-07-03 11:15:45 -07:00
|
|
|
->setLabel(pht('Title'))
|
|
|
|
->setError($e_title)
|
|
|
|
->setValue($title)
|
2014-07-04 08:04:28 -07:00
|
|
|
->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()));
|
2015-02-12 15:22:56 -08:00
|
|
|
$show_require = true;
|
2015-02-17 11:45:20 -08:00
|
|
|
$caption = pht('Applies only to documents individuals sign.');
|
2014-07-04 08:04:28 -07:00
|
|
|
} else {
|
|
|
|
$form->appendChild(
|
|
|
|
id(new AphrontFormMarkupControl())
|
|
|
|
->setLabel(pht('Who Should Sign?'))
|
|
|
|
->setValue($document->getSignatureTypeName()));
|
2015-02-12 15:22:56 -08:00
|
|
|
$individual = LegalpadDocument::SIGNATURE_TYPE_INDIVIDUAL;
|
|
|
|
$show_require = $document->getSignatureType() == $individual;
|
2015-02-17 11:45:20 -08:00
|
|
|
$caption = null;
|
2015-02-12 15:22:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($show_require) {
|
|
|
|
$form
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormCheckboxControl())
|
2015-07-29 17:02:10 -07:00
|
|
|
->setDisabled(!$viewer->getIsAdmin())
|
2015-02-12 15:22:56 -08:00
|
|
|
->setLabel(pht('Require Signature'))
|
|
|
|
->addCheckbox(
|
|
|
|
'requireSignature',
|
|
|
|
'requireSignature',
|
2015-05-22 17:27:56 +10:00
|
|
|
pht('Should signing this document be required to use Phabricator?'),
|
2015-02-17 11:45:20 -08:00
|
|
|
$v_require_signature)
|
|
|
|
->setCaption($caption));
|
2014-07-04 08:04:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
$form
|
2014-07-04 09:41:27 -07:00
|
|
|
->appendChild(
|
|
|
|
id(new PhabricatorRemarkupControl())
|
2015-07-29 17:02:10 -07:00
|
|
|
->setUser($viewer)
|
2014-11-24 15:25:25 -08:00
|
|
|
->setID('preamble')
|
|
|
|
->setLabel(pht('Preamble'))
|
|
|
|
->setValue($v_preamble)
|
|
|
|
->setName('preamble')
|
|
|
|
->setCaption(
|
|
|
|
pht('Optional help text for users signing this document.')))
|
2013-07-03 11:15:45 -07:00
|
|
|
->appendChild(
|
|
|
|
id(new PhabricatorRemarkupControl())
|
2015-07-29 17:02:10 -07:00
|
|
|
->setUser($viewer)
|
2014-11-24 15:25:25 -08:00
|
|
|
->setID('document-text')
|
|
|
|
->setLabel(pht('Document Body'))
|
|
|
|
->setError($e_text)
|
|
|
|
->setValue($text)
|
|
|
|
->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL)
|
|
|
|
->setName('text'));
|
2013-07-03 11:15:45 -07:00
|
|
|
|
|
|
|
$policies = id(new PhabricatorPolicyQuery())
|
2015-07-29 17:02:10 -07:00
|
|
|
->setViewer($viewer)
|
2013-07-03 11:15:45 -07:00
|
|
|
->setObject($document)
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$form
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormPolicyControl())
|
2015-07-29 17:02:10 -07:00
|
|
|
->setUser($viewer)
|
2013-07-03 11:15:45 -07:00
|
|
|
->setCapability(PhabricatorPolicyCapability::CAN_VIEW)
|
|
|
|
->setPolicyObject($document)
|
|
|
|
->setPolicies($policies)
|
|
|
|
->setName('can_view'))
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormPolicyControl())
|
2015-07-29 17:02:10 -07:00
|
|
|
->setUser($viewer)
|
2013-07-03 11:15:45 -07:00
|
|
|
->setCapability(PhabricatorPolicyCapability::CAN_EDIT)
|
|
|
|
->setPolicyObject($document)
|
|
|
|
->setPolicies($policies)
|
|
|
|
->setName('can_edit'));
|
|
|
|
|
2017-02-17 10:10:15 +00:00
|
|
|
$crumbs = $this->buildApplicationCrumbs();
|
2013-07-03 11:15:45 -07:00
|
|
|
$submit = new AphrontFormSubmitControl();
|
|
|
|
if ($is_create) {
|
|
|
|
$submit->setValue(pht('Create Document'));
|
2014-06-29 12:43:13 -07:00
|
|
|
$submit->addCancelButton($this->getApplicationURI());
|
2013-07-03 11:15:45 -07:00
|
|
|
$title = pht('Create Document');
|
|
|
|
$short = pht('Create');
|
2016-04-03 00:25:03 +00:00
|
|
|
$header_icon = 'fa-plus-square';
|
2013-07-03 11:15:45 -07:00
|
|
|
} else {
|
2015-01-08 16:11:53 -08:00
|
|
|
$submit->setValue(pht('Save Document'));
|
2013-07-03 11:15:45 -07:00
|
|
|
$submit->addCancelButton(
|
|
|
|
$this->getApplicationURI('view/'.$document->getID()));
|
2016-04-03 00:25:03 +00:00
|
|
|
$title = pht('Edit Document: %s', $document->getTitle());
|
2014-06-29 12:43:13 -07:00
|
|
|
$short = pht('Edit');
|
2016-04-03 00:25:03 +00:00
|
|
|
$header_icon = 'fa-pencil';
|
2014-06-28 16:37:15 -07:00
|
|
|
|
2014-01-16 14:36:57 -08:00
|
|
|
$crumbs->addTextCrumb(
|
|
|
|
$document->getMonogram(),
|
|
|
|
$this->getApplicationURI('view/'.$document->getID()));
|
2013-07-03 11:15:45 -07:00
|
|
|
}
|
|
|
|
|
2016-04-03 00:25:03 +00:00
|
|
|
$form->appendChild($submit);
|
2013-07-03 11:15:45 -07:00
|
|
|
|
2013-09-25 11:23:29 -07:00
|
|
|
$form_box = id(new PHUIObjectBoxView())
|
2016-04-03 00:25:03 +00:00
|
|
|
->setHeaderText(pht('Document'))
|
2014-01-10 09:17:37 -08:00
|
|
|
->setFormErrors($errors)
|
2016-04-03 00:25:03 +00:00
|
|
|
->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
|
2013-08-26 11:53:11 -07:00
|
|
|
->setForm($form);
|
|
|
|
|
2013-12-18 17:47:34 -08:00
|
|
|
$crumbs->addTextCrumb($short);
|
2016-04-03 00:25:03 +00:00
|
|
|
$crumbs->setBorder(true);
|
2013-07-03 11:15:45 -07:00
|
|
|
|
2013-08-05 10:47:26 -07:00
|
|
|
$preview = id(new PHUIRemarkupPreviewPanel())
|
2015-11-23 09:17:36 -08:00
|
|
|
->setHeader($document->getTitle())
|
2013-08-05 10:47:26 -07:00
|
|
|
->setPreviewURI($this->getApplicationURI('document/preview/'))
|
2015-06-25 10:17:22 -07:00
|
|
|
->setControlID('document-text')
|
2015-11-23 09:17:36 -08:00
|
|
|
->setPreviewType(PHUIRemarkupPreviewPanel::DOCUMENT);
|
2013-07-08 17:05:33 -07:00
|
|
|
|
2016-04-03 00:25:03 +00:00
|
|
|
$header = id(new PHUIHeaderView())
|
|
|
|
->setHeader($title)
|
|
|
|
->setHeaderIcon($header_icon);
|
|
|
|
|
|
|
|
$view = id(new PHUITwoColumnView())
|
|
|
|
->setHeader($header)
|
|
|
|
->setFooter(array(
|
2013-08-26 11:53:11 -07:00
|
|
|
$form_box,
|
2014-10-08 00:01:04 +11:00
|
|
|
$preview,
|
2013-07-03 11:15:45 -07:00
|
|
|
));
|
2016-04-03 00:25:03 +00:00
|
|
|
|
|
|
|
return $this->newPage()
|
|
|
|
->setTitle($title)
|
|
|
|
->setCrumbs($crumbs)
|
|
|
|
->appendChild($view);
|
|
|
|
|
2013-07-03 11:15:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|