Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / legalpad / controller / LegalpadDocumentSignatureAddController.php
blob73c9480600818b710176d1597eaf169866b82f71
1 <?php
3 final class LegalpadDocumentSignatureAddController extends LegalpadController {
5 public function handleRequest(AphrontRequest $request) {
6 $request = $this->getRequest();
7 $viewer = $request->getUser();
9 $document = id(new LegalpadDocumentQuery())
10 ->setViewer($viewer)
11 ->needDocumentBodies(true)
12 ->requireCapabilities(
13 array(
14 PhabricatorPolicyCapability::CAN_VIEW,
15 PhabricatorPolicyCapability::CAN_EDIT,
17 ->withIDs(array($request->getURIData('id')))
18 ->executeOne();
19 if (!$document) {
20 return new Aphront404Response();
23 $next_uri = $this->getApplicationURI('signatures/'.$document->getID().'/');
25 $e_name = true;
26 $e_user = true;
27 $v_users = array();
28 $v_notes = '';
29 $v_name = '';
30 $errors = array();
32 $type_individual = LegalpadDocument::SIGNATURE_TYPE_INDIVIDUAL;
33 $is_individual = ($document->getSignatureType() == $type_individual);
35 if ($request->isFormPost()) {
36 $v_notes = $request->getStr('notes');
37 $v_users = array_slice($request->getArr('users'), 0, 1);
38 $v_name = $request->getStr('name');
40 if ($is_individual) {
41 $user_phid = head($v_users);
42 if (!$user_phid) {
43 $e_user = pht('Required');
44 $errors[] = pht('You must choose a user to exempt.');
45 } else {
46 $user = id(new PhabricatorPeopleQuery())
47 ->setViewer($viewer)
48 ->withPHIDs(array($user_phid))
49 ->executeOne();
51 if (!$user) {
52 $e_user = pht('Invalid');
53 $errors[] = pht('That user does not exist.');
54 } else {
55 $signature = id(new LegalpadDocumentSignatureQuery())
56 ->setViewer($viewer)
57 ->withDocumentPHIDs(array($document->getPHID()))
58 ->withSignerPHIDs(array($user->getPHID()))
59 ->executeOne();
60 if ($signature) {
61 $e_user = pht('Signed');
62 $errors[] = pht('That user has already signed this document.');
63 } else {
64 $e_user = null;
68 } else {
69 $company_name = $v_name;
70 if (!strlen($company_name)) {
71 $e_name = pht('Required');
72 $errors[] = pht('You must choose a company to add an exemption for.');
76 if (!$errors) {
77 if ($is_individual) {
78 $name = $user->getRealName();
79 $email = $user->loadPrimaryEmailAddress();
80 $signer_phid = $user->getPHID();
81 $signature_data = array(
82 'name' => $name,
83 'email' => $email,
84 'notes' => $v_notes,
86 } else {
87 $name = $company_name;
88 $email = '';
89 $signer_phid = null;
90 $signature_data = array(
91 'name' => $name,
92 'email' => null,
93 'notes' => $v_notes,
94 'actorPHID' => $viewer->getPHID(),
98 $signature = id(new LegalpadDocumentSignature())
99 ->setDocumentPHID($document->getPHID())
100 ->setDocumentVersion($document->getVersions())
101 ->setSignerPHID($signer_phid)
102 ->setSignerName($name)
103 ->setSignerEmail($email)
104 ->setSignatureType($document->getSignatureType())
105 ->setIsExemption(1)
106 ->setExemptionPHID($viewer->getPHID())
107 ->setVerified(LegalpadDocumentSignature::VERIFIED)
108 ->setSignatureData($signature_data);
110 $signature->save();
112 return id(new AphrontRedirectResponse())->setURI($next_uri);
116 $form = id(new AphrontFormView())
117 ->setUser($viewer);
119 if ($is_individual) {
120 $form
121 ->appendControl(
122 id(new AphrontFormTokenizerControl())
123 ->setLabel(pht('Exempt User'))
124 ->setName('users')
125 ->setLimit(1)
126 ->setDatasource(new PhabricatorPeopleDatasource())
127 ->setValue($v_users)
128 ->setError($e_user));
129 } else {
130 $form
131 ->appendChild(
132 id(new AphrontFormTextControl())
133 ->setLabel(pht('Company Name'))
134 ->setName('name')
135 ->setError($e_name)
136 ->setValue($v_name));
139 $form
140 ->appendChild(
141 id(new AphrontFormTextAreaControl())
142 ->setLabel(pht('Notes'))
143 ->setName('notes')
144 ->setValue($v_notes));
146 return $this->newDialog()
147 ->setTitle(pht('Add Signature Exemption'))
148 ->setWidth(AphrontDialogView::WIDTH_FORM)
149 ->setErrors($errors)
150 ->appendParagraph(
151 pht(
152 'You can record a signature exemption if a user has signed an '.
153 'equivalent document. Other applications will behave as through the '.
154 'user has signed this document.'))
155 ->appendParagraph(null)
156 ->appendChild($form->buildLayoutView())
157 ->addSubmitButton(pht('Add Exemption'))
158 ->addCancelButton($next_uri);