|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @group legalpad |
| 5 | + */ |
| 6 | +final class LegalpadDocumentSignController extends LegalpadController { |
| 7 | + |
| 8 | + private $id; |
| 9 | + |
| 10 | + public function willProcessRequest(array $data) { |
| 11 | + $this->id = $data['id']; |
| 12 | + } |
| 13 | + |
| 14 | + public function processRequest() { |
| 15 | + $request = $this->getRequest(); |
| 16 | + $user = $request->getUser(); |
| 17 | + |
| 18 | + $document = id(new LegalpadDocumentQuery()) |
| 19 | + ->setViewer($user) |
| 20 | + ->withIDs(array($this->id)) |
| 21 | + ->needDocumentBodies(true) |
| 22 | + ->executeOne(); |
| 23 | + |
| 24 | + if (!$document) { |
| 25 | + return new Aphront404Response(); |
| 26 | + } |
| 27 | + |
| 28 | + $signature = id(new LegalpadDocumentSignature()) |
| 29 | + ->loadOneWhere( |
| 30 | + 'documentPHID = %s AND documentVersion = %d AND signerPHID = %s', |
| 31 | + $document->getPHID(), |
| 32 | + $document->getVersions(), |
| 33 | + $user->getPHID()); |
| 34 | + |
| 35 | + if (!$signature) { |
| 36 | + $has_signed = false; |
| 37 | + $error_view = null; |
| 38 | + $signature = id(new LegalpadDocumentSignature()) |
| 39 | + ->setSignerPHID($user->getPHID()) |
| 40 | + ->setDocumentPHID($document->getPHID()) |
| 41 | + ->setDocumentVersion($document->getVersions()); |
| 42 | + $data = array( |
| 43 | + 'name' => $user->getRealName(), |
| 44 | + 'email' => $user->loadPrimaryEmailAddress()); |
| 45 | + $signature->setSignatureData($data); |
| 46 | + } else { |
| 47 | + $has_signed = true; |
| 48 | + $error_view = id(new AphrontErrorView()) |
| 49 | + ->setSeverity(AphrontErrorView::SEVERITY_NOTICE) |
| 50 | + ->setTitle(pht('You have already agreed to these terms.')); |
| 51 | + $data = $signature->getSignatureData(); |
| 52 | + } |
| 53 | + |
| 54 | + $e_name = true; |
| 55 | + $e_email = true; |
| 56 | + $e_address_1 = true; |
| 57 | + $errors = array(); |
| 58 | + if ($request->isFormPost()) { |
| 59 | + $name = $request->getStr('name'); |
| 60 | + $email = $request->getStr('email'); |
| 61 | + $address_1 = $request->getStr('address_1'); |
| 62 | + $address_2 = $request->getStr('address_2'); |
| 63 | + $phone = $request->getStr('phone'); |
| 64 | + $agree = $request->getExists('agree'); |
| 65 | + |
| 66 | + if (!$name) { |
| 67 | + $e_name = pht('Required'); |
| 68 | + $errors[] = pht('Name field is required.'); |
| 69 | + } |
| 70 | + $data['name'] = $name; |
| 71 | + |
| 72 | + if (!$email) { |
| 73 | + $e_email = pht('Required'); |
| 74 | + $errors[] = pht('Email field is required.'); |
| 75 | + } else { |
| 76 | + $addr_obj = new PhutilEmailAddress($email); |
| 77 | + $domain = $addr_obj->getDomainName(); |
| 78 | + if (!$domain) { |
| 79 | + $e_email = pht('Invalid'); |
| 80 | + $errors[] = pht('A valid email is required.'); |
| 81 | + } |
| 82 | + } |
| 83 | + $data['email'] = $email; |
| 84 | + |
| 85 | + if (!$address_1) { |
| 86 | + $e_address_1 = pht('Required'); |
| 87 | + $errors[] = pht('Address line 1 field is required.'); |
| 88 | + } |
| 89 | + $data['address_1'] = $address_1; |
| 90 | + $data['address_2'] = $address_2; |
| 91 | + $data['phone'] = $phone; |
| 92 | + $signature->setSignatureData($data); |
| 93 | + |
| 94 | + if (!$agree) { |
| 95 | + $errors[] = pht( |
| 96 | + 'You must check "I agree to the terms laid forth above."'); |
| 97 | + } |
| 98 | + |
| 99 | + if (!$errors) { |
| 100 | + $signature->save(); |
| 101 | + $has_signed = true; |
| 102 | + $error_view = id(new AphrontErrorView()) |
| 103 | + ->setSeverity(AphrontErrorView::SEVERITY_NOTICE) |
| 104 | + ->setTitle(pht('Signature successful. Thank you.')); |
| 105 | + } else { |
| 106 | + $error_view = id(new AphrontErrorView()) |
| 107 | + ->setTitle(pht('Error in submission.')) |
| 108 | + ->setErrors($errors); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + $document_body = $document->getDocumentBody(); |
| 113 | + $engine = id(new PhabricatorMarkupEngine()) |
| 114 | + ->setViewer($user); |
| 115 | + $engine->addObject( |
| 116 | + $document_body, |
| 117 | + LegalpadDocumentBody::MARKUP_FIELD_TEXT); |
| 118 | + $engine->process(); |
| 119 | + |
| 120 | + $title = $document_body->getTitle(); |
| 121 | + |
| 122 | + $header = id(new PhabricatorHeaderView()) |
| 123 | + ->setHeader($title); |
| 124 | + |
| 125 | + $content = array( |
| 126 | + id(new PHUIDocumentView()) |
| 127 | + ->setHeader($header) |
| 128 | + ->appendChild($this->buildDocument($engine, $document_body)), |
| 129 | + $error_view, |
| 130 | + $this->buildSignatureForm( |
| 131 | + $document_body, |
| 132 | + $signature, |
| 133 | + $has_signed, |
| 134 | + $e_name, |
| 135 | + $e_email, |
| 136 | + $e_address_1)); |
| 137 | + |
| 138 | + return $this->buildApplicationPage( |
| 139 | + $content, |
| 140 | + array( |
| 141 | + 'title' => $title, |
| 142 | + 'device' => true, |
| 143 | + 'dust' => true, |
| 144 | + 'pageObjects' => array($document->getPHID()), |
| 145 | + )); |
| 146 | + } |
| 147 | + |
| 148 | + private function buildDocument( |
| 149 | + PhabricatorMarkupEngine |
| 150 | + $engine, LegalpadDocumentBody $body) { |
| 151 | + |
| 152 | + require_celerity_resource('legalpad-documentbody-css'); |
| 153 | + |
| 154 | + return phutil_tag( |
| 155 | + 'div', |
| 156 | + array( |
| 157 | + 'class' => 'legalpad-documentbody' |
| 158 | + ), |
| 159 | + $engine->getOutput($body, LegalpadDocumentBody::MARKUP_FIELD_TEXT)); |
| 160 | + |
| 161 | + } |
| 162 | + |
| 163 | + private function buildSignatureForm( |
| 164 | + LegalpadDocumentBody $body, |
| 165 | + LegalpadDocumentSignature $signature, |
| 166 | + $has_signed = false, |
| 167 | + $e_name = true, |
| 168 | + $e_email = true, |
| 169 | + $e_address_1 = true) { |
| 170 | + |
| 171 | + $user = $this->getRequest()->getUser(); |
| 172 | + if ($has_signed) { |
| 173 | + $instructions = pht('Thank you for signing and agreeing.'); |
| 174 | + } else { |
| 175 | + $instructions = pht('Please enter the following information.'); |
| 176 | + } |
| 177 | + |
| 178 | + $data = $signature->getSignatureData(); |
| 179 | + $form = id(new AphrontFormView()) |
| 180 | + ->setUser($user) |
| 181 | + ->setFlexible(true) |
| 182 | + ->appendChild( |
| 183 | + id(new AphrontFormInsetView()) |
| 184 | + ->setTitle(pht('Sign and Agree')) |
| 185 | + ->setDescription($instructions) |
| 186 | + ->setContent(phutil_tag('br', array())) |
| 187 | + ->appendChild( |
| 188 | + id(new AphrontFormTextControl()) |
| 189 | + ->setLabel(pht('Name')) |
| 190 | + ->setValue(idx($data, 'name', '')) |
| 191 | + ->setName('name') |
| 192 | + ->setError($e_name) |
| 193 | + ->setDisabled($has_signed)) |
| 194 | + ->appendChild( |
| 195 | + id(new AphrontFormTextControl()) |
| 196 | + ->setLabel(pht('Email')) |
| 197 | + ->setValue(idx($data, 'email', '')) |
| 198 | + ->setName('email') |
| 199 | + ->setError($e_email) |
| 200 | + ->setDisabled($has_signed)) |
| 201 | + ->appendChild( |
| 202 | + id(new AphrontFormTextControl()) |
| 203 | + ->setLabel(pht('Address line 1')) |
| 204 | + ->setValue(idx($data, 'address_1', '')) |
| 205 | + ->setName('address_1') |
| 206 | + ->setError($e_address_1) |
| 207 | + ->setDisabled($has_signed)) |
| 208 | + ->appendChild( |
| 209 | + id(new AphrontFormTextControl()) |
| 210 | + ->setLabel(pht('Address line 2')) |
| 211 | + ->setValue(idx($data, 'address_2', '')) |
| 212 | + ->setName('address_2') |
| 213 | + ->setDisabled($has_signed)) |
| 214 | + ->appendChild( |
| 215 | + id(new AphrontFormTextControl()) |
| 216 | + ->setLabel(pht('Phone')) |
| 217 | + ->setValue(idx($data, 'phone', '')) |
| 218 | + ->setName('phone') |
| 219 | + ->setDisabled($has_signed)) |
| 220 | + ->appendChild( |
| 221 | + id(new AphrontFormCheckboxControl()) |
| 222 | + ->addCheckbox( |
| 223 | + 'agree', |
| 224 | + 'agree', |
| 225 | + pht('I agree to the terms laid forth above.'), |
| 226 | + $has_signed) |
| 227 | + ->setDisabled($has_signed)) |
| 228 | + ->appendChild( |
| 229 | + id(new AphrontFormSubmitControl()) |
| 230 | + ->setValue(pht('Sign and Agree')) |
| 231 | + ->setDisabled($has_signed))); |
| 232 | + |
| 233 | + return $form; |
| 234 | + } |
| 235 | + |
| 236 | +} |
0 commit comments