[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/legalpad/storage/ -> LegalpadDocument.php (source)

   1  <?php
   2  
   3  final class LegalpadDocument extends LegalpadDAO
   4    implements
   5      PhabricatorPolicyInterface,
   6      PhabricatorSubscribableInterface,
   7      PhabricatorApplicationTransactionInterface,
   8      PhabricatorDestructibleInterface {
   9  
  10    protected $title;
  11    protected $contributorCount;
  12    protected $recentContributorPHIDs = array();
  13    protected $creatorPHID;
  14    protected $versions;
  15    protected $documentBodyPHID;
  16    protected $viewPolicy;
  17    protected $editPolicy;
  18    protected $mailKey;
  19    protected $signatureType;
  20    protected $preamble;
  21  
  22    const SIGNATURE_TYPE_INDIVIDUAL = 'user';
  23    const SIGNATURE_TYPE_CORPORATION = 'corp';
  24  
  25    private $documentBody = self::ATTACHABLE;
  26    private $contributors = self::ATTACHABLE;
  27    private $signatures = self::ATTACHABLE;
  28    private $userSignatures = array();
  29  
  30    public static function initializeNewDocument(PhabricatorUser $actor) {
  31      $app = id(new PhabricatorApplicationQuery())
  32        ->setViewer($actor)
  33        ->withClasses(array('PhabricatorLegalpadApplication'))
  34        ->executeOne();
  35  
  36      $view_policy = $app->getPolicy(LegalpadDefaultViewCapability::CAPABILITY);
  37      $edit_policy = $app->getPolicy(LegalpadDefaultEditCapability::CAPABILITY);
  38  
  39      return id(new LegalpadDocument())
  40        ->setVersions(0)
  41        ->setCreatorPHID($actor->getPHID())
  42        ->setContributorCount(0)
  43        ->setRecentContributorPHIDs(array())
  44        ->attachSignatures(array())
  45        ->setSignatureType(self::SIGNATURE_TYPE_INDIVIDUAL)
  46        ->setPreamble('')
  47        ->setViewPolicy($view_policy)
  48        ->setEditPolicy($edit_policy);
  49    }
  50  
  51    public function getConfiguration() {
  52      return array(
  53        self::CONFIG_AUX_PHID => true,
  54        self::CONFIG_SERIALIZATION => array(
  55          'recentContributorPHIDs' => self::SERIALIZATION_JSON,
  56        ),
  57        self::CONFIG_COLUMN_SCHEMA => array(
  58          'title' => 'text255',
  59          'contributorCount' => 'uint32',
  60          'versions' => 'uint32',
  61          'mailKey' => 'bytes20',
  62          'signatureType' => 'text4',
  63          'preamble' => 'text',
  64        ),
  65        self::CONFIG_KEY_SCHEMA => array(
  66          'key_creator' => array(
  67            'columns' => array('creatorPHID', 'dateModified'),
  68          ),
  69        ),
  70      ) + parent::getConfiguration();
  71    }
  72  
  73    public function generatePHID() {
  74      return PhabricatorPHID::generateNewPHID(
  75        PhabricatorLegalpadDocumentPHIDType::TYPECONST);
  76    }
  77  
  78    public function getDocumentBody() {
  79      return $this->assertAttached($this->documentBody);
  80    }
  81  
  82    public function attachDocumentBody(LegalpadDocumentBody $body) {
  83      $this->documentBody = $body;
  84      return $this;
  85    }
  86  
  87    public function getContributors() {
  88      return $this->assertAttached($this->contributors);
  89    }
  90  
  91    public function attachContributors(array $contributors) {
  92      $this->contributors = $contributors;
  93      return $this;
  94    }
  95  
  96    public function getSignatures() {
  97      return $this->assertAttached($this->signatures);
  98    }
  99  
 100    public function attachSignatures(array $signatures) {
 101      $this->signatures = $signatures;
 102      return $this;
 103    }
 104  
 105    public function save() {
 106      if (!$this->getMailKey()) {
 107        $this->setMailKey(Filesystem::readRandomCharacters(20));
 108      }
 109      return parent::save();
 110    }
 111  
 112    public function getMonogram() {
 113      return 'L'.$this->getID();
 114    }
 115  
 116    public function getUserSignature($phid) {
 117      return $this->assertAttachedKey($this->userSignatures, $phid);
 118    }
 119  
 120    public function attachUserSignature(
 121      $user_phid,
 122      LegalpadDocumentSignature $signature = null) {
 123      $this->userSignatures[$user_phid] = $signature;
 124      return $this;
 125    }
 126  
 127    public static function getSignatureTypeMap() {
 128      return array(
 129        self::SIGNATURE_TYPE_INDIVIDUAL => pht('Individuals'),
 130        self::SIGNATURE_TYPE_CORPORATION => pht('Corporations'),
 131      );
 132    }
 133  
 134    public function getSignatureTypeName() {
 135      $type = $this->getSignatureType();
 136      return idx(self::getSignatureTypeMap(), $type, $type);
 137    }
 138  
 139    public function getSignatureTypeIcon() {
 140      $type = $this->getSignatureType();
 141      $map = array(
 142        self::SIGNATURE_TYPE_INDIVIDUAL => 'fa-user grey',
 143        self::SIGNATURE_TYPE_CORPORATION => 'fa-building-o grey',
 144      );
 145  
 146      return idx($map, $type, 'fa-user grey');
 147    }
 148  
 149  
 150  /* -(  PhabricatorSubscribableInterface  )----------------------------------- */
 151  
 152  
 153    public function isAutomaticallySubscribed($phid) {
 154      return ($this->creatorPHID == $phid);
 155    }
 156  
 157    public function shouldShowSubscribersProperty() {
 158      return true;
 159    }
 160  
 161    public function shouldAllowSubscription($phid) {
 162      return true;
 163    }
 164  
 165  
 166  /* -(  PhabricatorPolicyInterface  )----------------------------------------- */
 167  
 168  
 169    public function getCapabilities() {
 170      return array(
 171        PhabricatorPolicyCapability::CAN_VIEW,
 172        PhabricatorPolicyCapability::CAN_EDIT,
 173      );
 174    }
 175  
 176    public function getPolicy($capability) {
 177      switch ($capability) {
 178        case PhabricatorPolicyCapability::CAN_VIEW:
 179          $policy = $this->viewPolicy;
 180          break;
 181        case PhabricatorPolicyCapability::CAN_EDIT:
 182          $policy = $this->editPolicy;
 183          break;
 184        default:
 185          $policy = PhabricatorPolicies::POLICY_NOONE;
 186          break;
 187      }
 188      return $policy;
 189    }
 190  
 191    public function hasAutomaticCapability($capability, PhabricatorUser $user) {
 192      return ($user->getPHID() == $this->getCreatorPHID());
 193    }
 194  
 195    public function describeAutomaticCapability($capability) {
 196      return pht(
 197        'The author of a document can always view and edit it.');
 198    }
 199  
 200  
 201  /* -(  PhabricatorApplicationTransactionInterface  )------------------------- */
 202  
 203  
 204    public function getApplicationTransactionEditor() {
 205      return new LegalpadDocumentEditor();
 206    }
 207  
 208    public function getApplicationTransactionObject() {
 209      return $this;
 210    }
 211  
 212    public function getApplicationTransactionTemplate() {
 213      return new LegalpadTransaction();
 214    }
 215  
 216  
 217  /* -(  PhabricatorDestructibleInterface  )----------------------------------- */
 218  
 219  
 220    public function destroyObjectPermanently(
 221      PhabricatorDestructionEngine $engine) {
 222  
 223      $this->openTransaction();
 224        $this->delete();
 225  
 226        $bodies = id(new LegalpadDocumentBody())->loadAllWhere(
 227          'documentPHID = %s',
 228          $this->getPHID());
 229        foreach ($bodies as $body) {
 230          $body->delete();
 231        }
 232  
 233        $signatures = id(new LegalpadDocumentSignature())->loadAllWhere(
 234          'documentPHID = %s',
 235          $this->getPHID());
 236        foreach ($signatures as $signature) {
 237          $signature->delete();
 238        }
 239  
 240      $this->saveTransaction();
 241    }
 242  
 243  }


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