@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at upstream/main 120 lines 3.2 kB view raw
1<?php 2 3final class PhrictionDocumentTitleTransaction 4 extends PhrictionDocumentVersionTransaction { 5 6 const TRANSACTIONTYPE = 'title'; 7 8 public function generateOldValue($object) { 9 if ($this->isNewObject()) { 10 return null; 11 } 12 return $this->getEditor()->getOldContent()->getTitle(); 13 } 14 15 public function applyInternalEffects($object, $value) { 16 $object->setStatus(PhrictionDocumentStatus::STATUS_EXISTS); 17 18 $content = $this->getNewDocumentContent($object); 19 20 $content->setTitle($value); 21 } 22 23 public function getActionStrength() { 24 return 140; 25 } 26 27 public function getActionName() { 28 $old = $this->getOldValue(); 29 $new = $this->getNewValue(); 30 31 if ($old === null) { 32 if ($this->getMetadataValue('stub:create:phid')) { 33 return pht('Stubbed'); 34 } else { 35 return pht('Created'); 36 } 37 } 38 return pht('Retitled'); 39 } 40 41 public function getTitle() { 42 $old = $this->getOldValue(); 43 $new = $this->getNewValue(); 44 45 if ($old === null) { 46 if ($this->getMetadataValue('stub:create:phid')) { 47 return pht( 48 '%s stubbed out this document when creating %s.', 49 $this->renderAuthor(), 50 $this->renderHandle( 51 $this->getMetadataValue('stub:create:phid'))); 52 } else { 53 return pht( 54 '%s created this document.', 55 $this->renderAuthor()); 56 } 57 } 58 59 return pht( 60 '%s changed the title from %s to %s.', 61 $this->renderAuthor(), 62 $this->renderOldValue(), 63 $this->renderNewValue()); 64 } 65 66 public function getTitleForFeed() { 67 $old = $this->getOldValue(); 68 $new = $this->getNewValue(); 69 70 if ($old === null) { 71 return pht( 72 '%s created %s.', 73 $this->renderAuthor(), 74 $this->renderObject()); 75 } 76 77 return pht( 78 '%s renamed %s from %s to %s.', 79 $this->renderAuthor(), 80 $this->renderObject(), 81 $this->renderOldValue(), 82 $this->renderNewValue()); 83 } 84 85 public function validateTransactions($object, array $xactions) { 86 $errors = array(); 87 88 $title = $object->getContent()->getTitle(); 89 if ($this->isEmptyTextTransaction($title, $xactions)) { 90 $errors[] = $this->newRequiredError( 91 pht('Documents must have a title.')); 92 } 93 94 if ($this->isNewObject()) { 95 // No ancestral slugs is "/". No ancestry checks apply when creating the 96 // root document. 97 $ancestral_slugs = PhabricatorSlug::getAncestry($object->getSlug()); 98 if ($ancestral_slugs) { 99 // You must be able to view and edit the parent document to create a new 100 // child. 101 $parent_document = id(new PhrictionDocumentQuery()) 102 ->setViewer($this->getActor()) 103 ->withSlugs(array(last($ancestral_slugs))) 104 ->requireCapabilities( 105 array( 106 PhabricatorPolicyCapability::CAN_VIEW, 107 PhabricatorPolicyCapability::CAN_EDIT, 108 )) 109 ->executeOne(); 110 if (!$parent_document) { 111 $errors[] = $this->newInvalidError( 112 pht('You can not create a document which does not have a parent.')); 113 } 114 } 115 } 116 117 return $errors; 118 } 119 120}