Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/almanac/xaction/AlmanacDeviceNameTransaction.php
12256 views
1
<?php
2
3
final class AlmanacDeviceNameTransaction
4
extends AlmanacDeviceTransactionType {
5
6
const TRANSACTIONTYPE = 'almanac:device:name';
7
8
public function generateOldValue($object) {
9
return $object->getName();
10
}
11
12
public function applyInternalEffects($object, $value) {
13
$object->setName($value);
14
}
15
16
public function getTitle() {
17
return pht(
18
'%s renamed this device from %s to %s.',
19
$this->renderAuthor(),
20
$this->renderOldValue(),
21
$this->renderNewValue());
22
}
23
24
public function validateTransactions($object, array $xactions) {
25
$errors = array();
26
27
if ($this->isEmptyTextTransaction($object->getName(), $xactions)) {
28
$errors[] = $this->newRequiredError(
29
pht('Device name is required.'));
30
}
31
32
foreach ($xactions as $xaction) {
33
$name = $xaction->getNewValue();
34
35
$message = null;
36
try {
37
AlmanacNames::validateName($name);
38
} catch (Exception $ex) {
39
$message = $ex->getMessage();
40
}
41
42
if ($message !== null) {
43
$errors[] = $this->newInvalidError($message, $xaction);
44
continue;
45
}
46
47
if ($name === $object->getName()) {
48
continue;
49
}
50
51
$other = id(new AlmanacDeviceQuery())
52
->setViewer(PhabricatorUser::getOmnipotentUser())
53
->withNames(array($name))
54
->executeOne();
55
if ($other && ($other->getID() != $object->getID())) {
56
$errors[] = $this->newInvalidError(
57
pht('Almanac devices must have unique names.'),
58
$xaction);
59
continue;
60
}
61
62
$namespace = AlmanacNamespace::loadRestrictedNamespace(
63
$this->getActor(),
64
$name);
65
if ($namespace) {
66
$errors[] = $this->newInvalidError(
67
pht(
68
'You do not have permission to create Almanac devices '.
69
'within the "%s" namespace.',
70
$namespace->getName()),
71
$xaction);
72
continue;
73
}
74
}
75
76
return $errors;
77
}
78
79
}
80
81