Path: blob/master/src/applications/legalpad/herald/LegalpadRequireSignatureHeraldAction.php
13486 views
<?php12final class LegalpadRequireSignatureHeraldAction3extends HeraldAction {45const DO_SIGNED = 'do.signed';6const DO_REQUIRED = 'do.required';78const ACTIONCONST = 'legalpad.require';910public function getActionGroupKey() {11return HeraldSupportActionGroup::ACTIONGROUPKEY;12}1314public function supportsObject($object) {15// TODO: This could probably be more general. Note that we call16// getAuthorPHID() on the object explicitly below, and this also needs to17// be generalized.18return ($object instanceof DifferentialRevision);19}2021protected function applyRequire(array $phids) {22$adapter = $this->getAdapter();2324$edgetype_legal = LegalpadObjectNeedsSignatureEdgeType::EDGECONST;25$current = $adapter->loadEdgePHIDs($edgetype_legal);2627$allowed_types = array(28PhabricatorLegalpadDocumentPHIDType::TYPECONST,29);3031$targets = $this->loadStandardTargets($phids, $allowed_types, $current);32if (!$targets) {33return;34}3536$phids = array_fuse(array_keys($targets));3738$object = $adapter->getObject();39$author_phid = $object->getAuthorPHID();4041$signatures = id(new LegalpadDocumentSignatureQuery())42->setViewer(PhabricatorUser::getOmnipotentUser())43->withDocumentPHIDs($phids)44->withSignerPHIDs(array($author_phid))45->execute();46$signatures = mpull($signatures, null, 'getDocumentPHID');4748$signed = array();49foreach ($phids as $phid) {50if (isset($signatures[$phid])) {51$signed[] = $phid;52unset($phids[$phid]);53}54}5556if ($signed) {57$this->logEffect(self::DO_SIGNED, $phids);58}5960if (!$phids) {61return;62}6364$xaction = $adapter->newTransaction()65->setTransactionType(PhabricatorTransactions::TYPE_EDGE)66->setMetadataValue('edge:type', $edgetype_legal)67->setNewValue(68array(69'+' => $phids,70));7172$adapter->queueTransaction($xaction);7374$this->logEffect(self::DO_REQUIRED, $phids);75}7677protected function getActionEffectMap() {78return array(79self::DO_SIGNED => array(80'icon' => 'fa-terminal',81'color' => 'green',82'name' => pht('Already Signed'),83),84self::DO_REQUIRED => array(85'icon' => 'fa-terminal',86'color' => 'green',87'name' => pht('Required Signature'),88),89);90}9192protected function renderActionEffectDescription($type, $data) {93switch ($type) {94case self::DO_SIGNED:95return pht(96'%s document(s) are already signed: %s.',97phutil_count($data),98$this->renderHandleList($data));99case self::DO_REQUIRED:100return pht(101'Required %s signature(s): %s.',102phutil_count($data),103$this->renderHandleList($data));104}105}106107public function getHeraldActionName() {108return pht('Require signatures');109}110111public function supportsRuleType($rule_type) {112return ($rule_type != HeraldRuleTypeConfig::RULE_TYPE_PERSONAL);113}114115public function applyEffect($object, HeraldEffect $effect) {116return $this->applyRequire($effect->getTarget());117}118119public function getHeraldActionStandardType() {120return self::STANDARD_PHID_LIST;121}122123protected function getDatasource() {124return new LegalpadDocumentDatasource();125}126127public function renderActionDescription($value) {128return pht(129'Require document signatures: %s.',130$this->renderHandleList($value));131}132133public function isActionAvailable() {134return id(new PhabricatorLegalpadApplication())->isInstalled();135}136137}138139140