Path: blob/master/src/applications/legalpad/storage/LegalpadDocument.php
13459 views
<?php12final class LegalpadDocument extends LegalpadDAO3implements4PhabricatorPolicyInterface,5PhabricatorSubscribableInterface,6PhabricatorApplicationTransactionInterface,7PhabricatorDestructibleInterface {89protected $title;10protected $contributorCount;11protected $recentContributorPHIDs = array();12protected $creatorPHID;13protected $versions;14protected $documentBodyPHID;15protected $viewPolicy;16protected $editPolicy;17protected $mailKey;18protected $signatureType;19protected $preamble;20protected $requireSignature;2122const SIGNATURE_TYPE_NONE = 'none';23const SIGNATURE_TYPE_INDIVIDUAL = 'user';24const SIGNATURE_TYPE_CORPORATION = 'corp';2526private $documentBody = self::ATTACHABLE;27private $contributors = self::ATTACHABLE;28private $signatures = self::ATTACHABLE;29private $userSignatures = array();3031public static function initializeNewDocument(PhabricatorUser $actor) {32$app = id(new PhabricatorApplicationQuery())33->setViewer($actor)34->withClasses(array('PhabricatorLegalpadApplication'))35->executeOne();3637$view_policy = $app->getPolicy(LegalpadDefaultViewCapability::CAPABILITY);38$edit_policy = $app->getPolicy(LegalpadDefaultEditCapability::CAPABILITY);3940return id(new LegalpadDocument())41->setVersions(0)42->setCreatorPHID($actor->getPHID())43->setContributorCount(0)44->setRecentContributorPHIDs(array())45->attachSignatures(array())46->setSignatureType(self::SIGNATURE_TYPE_INDIVIDUAL)47->setPreamble('')48->setRequireSignature(0)49->setViewPolicy($view_policy)50->setEditPolicy($edit_policy);51}5253protected function getConfiguration() {54return array(55self::CONFIG_AUX_PHID => true,56self::CONFIG_SERIALIZATION => array(57'recentContributorPHIDs' => self::SERIALIZATION_JSON,58),59self::CONFIG_COLUMN_SCHEMA => array(60'title' => 'text255',61'contributorCount' => 'uint32',62'versions' => 'uint32',63'mailKey' => 'bytes20',64'signatureType' => 'text4',65'preamble' => 'text',66'requireSignature' => 'bool',67),68self::CONFIG_KEY_SCHEMA => array(69'key_creator' => array(70'columns' => array('creatorPHID', 'dateModified'),71),72'key_required' => array(73'columns' => array('requireSignature', 'dateModified'),74),75),76) + parent::getConfiguration();77}7879public function generatePHID() {80return PhabricatorPHID::generateNewPHID(81PhabricatorLegalpadDocumentPHIDType::TYPECONST);82}8384public function getDocumentBody() {85return $this->assertAttached($this->documentBody);86}8788public function attachDocumentBody(LegalpadDocumentBody $body) {89$this->documentBody = $body;90return $this;91}9293public function getContributors() {94return $this->assertAttached($this->contributors);95}9697public function attachContributors(array $contributors) {98$this->contributors = $contributors;99return $this;100}101102public function getSignatures() {103return $this->assertAttached($this->signatures);104}105106public function attachSignatures(array $signatures) {107$this->signatures = $signatures;108return $this;109}110111public function save() {112if (!$this->getMailKey()) {113$this->setMailKey(Filesystem::readRandomCharacters(20));114}115return parent::save();116}117118public function getMonogram() {119return 'L'.$this->getID();120}121122public function getURI() {123return '/'.$this->getMonogram();124}125126public function getUserSignature($phid) {127return $this->assertAttachedKey($this->userSignatures, $phid);128}129130public function attachUserSignature(131$user_phid,132LegalpadDocumentSignature $signature = null) {133$this->userSignatures[$user_phid] = $signature;134return $this;135}136137public static function getSignatureTypeMap() {138return array(139self::SIGNATURE_TYPE_INDIVIDUAL => pht('Individuals'),140self::SIGNATURE_TYPE_CORPORATION => pht('Corporations'),141self::SIGNATURE_TYPE_NONE => pht('No One'),142);143}144145public function getSignatureTypeName() {146$type = $this->getSignatureType();147return idx(self::getSignatureTypeMap(), $type, $type);148}149150public function getSignatureTypeIcon() {151$type = $this->getSignatureType();152$map = array(153self::SIGNATURE_TYPE_NONE => '',154self::SIGNATURE_TYPE_INDIVIDUAL => 'fa-user grey',155self::SIGNATURE_TYPE_CORPORATION => 'fa-building-o grey',156);157158return idx($map, $type, 'fa-user grey');159}160161162/* -( PhabricatorSubscribableInterface )----------------------------------- */163164165public function isAutomaticallySubscribed($phid) {166return ($this->creatorPHID == $phid);167}168169170/* -( PhabricatorPolicyInterface )----------------------------------------- */171172173public function getCapabilities() {174return array(175PhabricatorPolicyCapability::CAN_VIEW,176PhabricatorPolicyCapability::CAN_EDIT,177);178}179180public function getPolicy($capability) {181switch ($capability) {182case PhabricatorPolicyCapability::CAN_VIEW:183$policy = $this->viewPolicy;184break;185case PhabricatorPolicyCapability::CAN_EDIT:186$policy = $this->editPolicy;187break;188default:189$policy = PhabricatorPolicies::POLICY_NOONE;190break;191}192return $policy;193}194195public function hasAutomaticCapability($capability, PhabricatorUser $user) {196return ($user->getPHID() == $this->getCreatorPHID());197}198199public function describeAutomaticCapability($capability) {200return pht('The author of a document can always view and edit it.');201}202203204/* -( PhabricatorApplicationTransactionInterface )------------------------- */205206207public function getApplicationTransactionEditor() {208return new LegalpadDocumentEditor();209}210211public function getApplicationTransactionTemplate() {212return new LegalpadTransaction();213}214215216/* -( PhabricatorDestructibleInterface )----------------------------------- */217218219public function destroyObjectPermanently(220PhabricatorDestructionEngine $engine) {221222$this->openTransaction();223$this->delete();224225$bodies = id(new LegalpadDocumentBody())->loadAllWhere(226'documentPHID = %s',227$this->getPHID());228foreach ($bodies as $body) {229$body->delete();230}231232$signatures = id(new LegalpadDocumentSignature())->loadAllWhere(233'documentPHID = %s',234$this->getPHID());235foreach ($signatures as $signature) {236$signature->delete();237}238239$this->saveTransaction();240}241242}243244245