[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/legalpad/controller/ -> LegalpadDocumentSignatureAddController.php (source)

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


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1