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