Skip to content

Commit

Permalink
MNT Use FQCN for PHP translations
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jan 10, 2024
1 parent bfc5eb3 commit 4caf76b
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 47 deletions.
52 changes: 26 additions & 26 deletions src/Controllers/LinkFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ public function linkForm(): Form
if ($id) {
$link = Link::get()->byID($id);
if (!$link) {
$this->jsonError(404, _t('LinkField.INVALID_ID', 'Invalid ID'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_ID', 'Invalid ID'));
}
$operation = 'edit';
if (!$link->canView()) {
$this->jsonError(403, _t('LinkField.UNAUTHORIZED', 'Unauthorized'));
$this->jsonError(403, _t(__CLASS__ . '.UNAUTHORIZED', 'Unauthorized'));
}
} else {
$typeKey = $this->typeKeyFromRequest();
$link = LinkTypeService::create()->byKey($typeKey);
if (!$link) {
$this->jsonError(404, _t('LinkField.INVALID_TYPEKEY', 'Invalid typeKey'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_TYPEKEY', 'Invalid typeKey'));
}
$operation = 'create';
}
Expand Down Expand Up @@ -110,7 +110,7 @@ public function linkData(HTTPRequest $request): HTTPResponse
private function getLinkData(Link $link): array
{
if (!$link->canView()) {
$this->jsonError(403, _t('LinkField.UNAUTHORIZED', 'Unauthorized'));
$this->jsonError(403, _t(__CLASS__ . '.UNAUTHORIZED', 'Unauthorized'));
}
$data = $link->jsonSerialize();
$data['canDelete'] = $link->canDelete();
Expand All @@ -127,11 +127,11 @@ public function linkDelete(): HTTPResponse
{
$link = $this->linkFromRequest();
if (!$link->canDelete()) {
$this->jsonError(403, _t('LinkField.UNAUTHORIZED', 'Unauthorized'));
$this->jsonError(403, _t(__CLASS__ . '.UNAUTHORIZED', 'Unauthorized'));
}
// Check security token on destructive operation
if (!SecurityToken::inst()->checkRequest($this->getRequest())) {
$this->jsonError(400, _t('LinkField.INVALID_TOKEN', 'Invalid CSRF token'));
$this->jsonError(400, _t(__CLASS__ . '.INVALID_TOKEN', 'Invalid CSRF token'));
}
// delete() will also delete any published version immediately
$link->delete();
Expand Down Expand Up @@ -167,7 +167,7 @@ public function getLinkForm(): Form
public function save(array $data, Form $form): HTTPResponse
{
if (empty($data)) {
$this->jsonError(400, _t('LinkField.EMPTY_DATA', 'Empty data'));
$this->jsonError(400, _t(__CLASS__ . '.EMPTY_DATA', 'Empty data'));
}

/** @var Link $link */
Expand All @@ -177,28 +177,28 @@ public function save(array $data, Form $form): HTTPResponse
$operation = 'edit';
$link = Link::get()->byID($id);
if (!$link) {
$this->jsonErorr(404, _t('LinkField.INVALID_ID', 'Invalid ID'));
$this->jsonErorr(404, _t(__CLASS__ . '.INVALID_ID', 'Invalid ID'));
}
if (!$link->canEdit()) {
$this->jsonError(403, _t('LinkField.UNAUTHORIZED', 'Unauthorized'));
$this->jsonError(403, _t(__CLASS__ . '.UNAUTHORIZED', 'Unauthorized'));
}
} else {
// Creating a new Link
$operation = 'create';
$typeKey = $this->typeKeyFromRequest();
$className = LinkTypeService::create()->byKey($typeKey) ?? '';
if (!$className) {
$this->jsonError(404, _t('LinkField.INVALID_TYPEKEY', 'Invalid typeKey'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_TYPEKEY', 'Invalid typeKey'));
}
$link = $className::create();
if (!$link->canCreate()) {
$this->jsonError(403, _t('LinkField.UNAUTHORIZED', 'Unauthorized'));
$this->jsonError(403, _t(__CLASS__ . '.UNAUTHORIZED', 'Unauthorized'));
}
}

// Ensure that ItemID url param matches the ID in the form data
if (isset($data['ID']) && ((int) $data['ID'] !== $id)) {
$this->jsonError(400, _t('LinkField.BAD_DATA', 'Bad data'));
$this->jsonError(400, _t(__CLASS__ . '.BAD_DATA', 'Bad data'));
}

// Update DataObject from form data
Expand Down Expand Up @@ -285,8 +285,8 @@ private function createLinkForm(Link $link, string $operation): Form

// Add save action button
$title = $id
? _t('LinkField.UPDATE_LINK', 'Update link')
: _t('LinkField.CREATE_LINK', 'Create link');
? _t(__CLASS__ . '.UPDATE_LINK', 'Update link')
: _t(__CLASS__ . '.CREATE_LINK', 'Create link');
$actions = FieldList::create([
FormAction::create('save', $title)
->setSchemaData(['data' => ['buttonStyle' => 'primary']]),
Expand Down Expand Up @@ -323,11 +323,11 @@ private function linkFromRequest(): Link
{
$itemID = $this->itemIDFromRequest();
if (!$itemID) {
$this->jsonError(404, _t('LinkField.INVALID_ID', 'Invalid ID'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_ID', 'Invalid ID'));
}
$link = Link::get()->byID($itemID);
if (!$link) {
$this->jsonError(404, _t('LinkField.INVALID_ID', 'Invalid ID'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_ID', 'Invalid ID'));
}
return $link;
}
Expand All @@ -339,11 +339,11 @@ private function linksFromRequest(): DataList
{
$itemIDs = $this->itemIDsFromRequest();
if (empty($itemIDs)) {
$this->jsonError(404, _t('LinkField.INVALID_ID', 'Invalid ID'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_ID', 'Invalid ID'));
}
$links = Link::get()->byIDs($itemIDs);
if (!$links->exists()) {
$this->jsonError(404, _t('LinkField.INVALID_ID', 'Invalid ID'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_ID', 'Invalid ID'));
}
return $links;
}
Expand All @@ -356,7 +356,7 @@ private function itemIDFromRequest(): int
$request = $this->getRequest();
$itemID = (string) $request->param('ItemID');
if (!ctype_digit($itemID)) {
$this->jsonError(404, _t('LinkField.INVALID_ID', 'Invalid ID'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_ID', 'Invalid ID'));
}
return (int) $itemID;
}
Expand All @@ -370,13 +370,13 @@ private function itemIDsFromRequest(): array
$itemIDs = $request->getVar('itemIDs');

if (!is_array($itemIDs)) {
$this->jsonError(404, _t('LinkField.INVALID_ID', 'Invalid ID'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_ID', 'Invalid ID'));
}

$idsAsInt = [];
foreach ($itemIDs as $id) {
if (!is_int($id) && !ctype_digit($id)) {
$this->jsonError(404, _t('LinkField.INVALID_ID', 'Invalid ID'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_ID', 'Invalid ID'));
}
$idsAsInt[] = (int) $id;
}
Expand All @@ -392,7 +392,7 @@ private function typeKeyFromRequest(): string
$request = $this->getRequest();
$typeKey = (string) $request->getVar('typeKey');
if (strlen($typeKey) === 0 || !preg_match('#^[a-z\-]+$#', $typeKey)) {
$this->jsonError(404, _t('LinkField.INVALID_TYPEKEY', 'Invalid typeKey'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_TYPEKEY', 'Invalid typeKey'));
}
return $typeKey;
}
Expand All @@ -406,11 +406,11 @@ private function ownerFromRequest(): DataObject
$request = $this->getRequest();
$ownerID = (int) ($request->getVar('ownerID') ?: $request->postVar('OwnerID'));
if ($ownerID === 0) {
$this->jsonError(404, _t('LinkField.INVALID_OWNER_ID', 'Invalid ownerID'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_OWNER_ID', 'Invalid ownerID'));
}
$ownerClass = $request->getVar('ownerClass') ?: $request->postVar('OwnerClass');
if (!is_a($ownerClass, DataObject::class, true)) {
$this->jsonError(404, _t('LinkField.INVALID_OWNER_CLASS', 'Invalid ownerClass'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_OWNER_CLASS', 'Invalid ownerClass'));
}
$ownerRelation = $this->ownerRelationFromRequest();
/** @var DataObject $obj */
Expand All @@ -435,7 +435,7 @@ private function ownerFromRequest(): DataObject
return $owner;
}
}
$this->jsonError(404, _t('LinkField.INVALID_OWNER', 'Invalid Owner'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_OWNER', 'Invalid Owner'));
}

/**
Expand All @@ -447,7 +447,7 @@ private function ownerRelationFromRequest(): string
$request = $this->getRequest();
$ownerRelation = $request->getVar('ownerRelation') ?: $request->postVar('OwnerRelation');
if (!$ownerRelation) {
$this->jsonError(404, _t('LinkField.INVALID_OWNER_RELATION', 'Invalid ownerRelation'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_OWNER_RELATION', 'Invalid ownerRelation'));
}
return $ownerRelation;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Models/EmailLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function getCMSFields(): FieldList
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$fields->replaceField('Email', EmailField::create(
'Email',
_t('LinkField.EMAIL_FIELD', 'Email address'),
_t(__CLASS__ . '.EMAIL_FIELD', 'Email address'),
));
});
return parent::getCMSFields();
Expand Down
2 changes: 1 addition & 1 deletion src/Models/ExternalLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function getCMSFields(): FieldList
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$linkField = $fields->dataFieldByName('ExternalUrl');
$linkField->setTitle(_t('LinkField.EXTERNAL_URL_FIELD', 'External url'));
$linkField->setTitle(_t(__CLASS__ . '.EXTERNAL_URL_FIELD', 'External url'));
});
return parent::getCMSFields();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Models/FileLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function getCMSFields(): FieldList
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$linkField = $fields->dataFieldByName('File');
$linkField->setTitle(_t('LinkField.FILE_FIELD', 'File'));
$linkField->setTitle(_t(__CLASS__ . '.FILE_FIELD', 'File'));
});
return parent::getCMSFields();
}
Expand Down
14 changes: 7 additions & 7 deletions src/Models/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ public function getCMSFields(): FieldList
$linkTypes = $this->getLinkTypes();

$titleField = $fields->dataFieldByName('Title');
$titleField->setTitle(_t('LinkField.LINK_FIELD_TITLE', 'Title'));
$titleField->setTitle(_t(__CLASS__ . '.LINK_FIELD_TITLE', 'Title'));
$titleField->setDescription(_t(
self::class . '.LINK_FIELD_TITLE_DESCRIPTION',
'If left blank, an appropriate default title will be used on the front-end',
));

$openInNewField = $fields->dataFieldByName('OpenInNew');
$openInNewField->setTitle(_t('LinkField.OPEN_IN_NEW_TITLE', 'Open in new window?'));
$openInNewField->setTitle(_t(__CLASS__ . '.OPEN_IN_NEW_TITLE', 'Open in new window?'));

if (static::class === self::class) {
// Add a link type selection field for generic links
Expand All @@ -98,7 +98,7 @@ public function getCMSFields(): FieldList
[
$linkTypeField = DropdownField::create(
'LinkType',
_t('LinkField.LINK_TYPE_TITLE', 'Link Type'),
_t(__CLASS__ . '.LINK_TYPE_TITLE', 'Link Type'),
$linkTypes
),
],
Expand Down Expand Up @@ -160,7 +160,7 @@ function setData($data): Link
if (json_last_error() !== JSON_ERROR_NONE) {
throw new InvalidArgumentException(
_t(
'LinkField.INVALID_JSON',
__CLASS__ . '.INVALID_JSON',
'"{class}": Decoding json string failred with "{error}"',
[
'class' => static::class,
Expand All @@ -181,7 +181,7 @@ function setData($data): Link
if (!is_array($data)) {
throw new InvalidArgumentException(
_t(
'LinkField.INVALID_DATA_TO_ARRAY',
__CLASS__ . '.INVALID_DATA_TO_ARRAY',
'"{class}": Could not convert $data to an array.',
['class' => static::class],
sprintf('%s: Could not convert $data to an array.', static::class),
Expand All @@ -194,7 +194,7 @@ function setData($data): Link
if (!$typeKey) {
throw new InvalidArgumentException(
_t(
'LinkField.DATA_HAS_NO_TYPEKEY',
__CLASS__ . '.DATA_HAS_NO_TYPEKEY',
'"{class}": $data does not have a typeKey.',
['class' => static::class],
sprintf('%s: $data does not have a typeKey.', static::class),
Expand All @@ -207,7 +207,7 @@ function setData($data): Link
if (!$type) {
throw new InvalidArgumentException(
_t(
'LinkField.NOT_REGISTERED_LINKTYPE',
__CLASS__ . '.NOT_REGISTERED_LINKTYPE',
'"{class}": "{typekey}" is not a registered Link Type.',
[
'class' => static::class,
Expand Down
2 changes: 1 addition & 1 deletion src/Models/PhoneLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function getCMSFields(): FieldList
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$linkField = $fields->dataFieldByName('Phone');
$linkField->setTitle(_t('LinkField.PHONE_FIELD', 'Phone'));
$linkField->setTitle(_t(__CLASS__ . '.PHONE_FIELD', 'Phone'));
});
return parent::getCMSFields();
}
Expand Down
12 changes: 6 additions & 6 deletions src/Models/SiteTreeLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function getCMSFields(): FieldList
$titleField = $fields->dataFieldByName('Title');
$titleField?->setDescription(
_t(
'LinkField.TITLE_DESCRIPTION',
__CLASS__ . '.TITLE_DESCRIPTION',
'Auto generated from Page title if left blank',
),
);
Expand All @@ -64,7 +64,7 @@ public function getCMSFields(): FieldList
'Title',
TreeDropdownField::create(
'PageID',
_t('LinkField.PAGE_FIELD_TITLE', 'Page'),
_t(__CLASS__ . '.PAGE_FIELD_TITLE', 'Page'),
SiteTree::class,
'ID',
'TreeTitle'
Expand All @@ -75,13 +75,13 @@ public function getCMSFields(): FieldList
'PageID',
$queryStringField = TextField::create(
'QueryString',
_t('LinkField.QUERY_FIELD_TITLE', 'Query string'),
_t(__CLASS__ . '.QUERY_FIELD_TITLE', 'Query string'),
)
);

$queryStringField->setDescription(
_t(
'LinkField.QUERY_STRING_DESCRIPTION',
__CLASS__ . '.QUERY_STRING_DESCRIPTION',
'Do not prepend "?". EG: "option1=value&option2=value2"',
),
);
Expand All @@ -90,13 +90,13 @@ public function getCMSFields(): FieldList
'QueryString',
$anchorField = AnchorSelectorField::create(
'Anchor',
_t('LinkField.ANCHOR_FIELD_TITLE', 'Anchor')
_t(__CLASS__ . '.ANCHOR_FIELD_TITLE', 'Anchor')
)
);

$anchorField->setDescription(
_t(
'LinkField.ANCHOR_DESCRIPTION',
__CLASS__ . '.ANCHOR_DESCRIPTION',
'Do not prepend "#". Anchor suggestions will be displayed once the linked page is attached.',
),
);
Expand Down
8 changes: 4 additions & 4 deletions src/Tasks/LinkableMigrationTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function run($request): void
if (!$this->versionedStatusMatches()) {
throw new Exception(
_t(
'LinkField.VERSIONED_STATUS_MISMATCH',
__CLASS__ . '.VERSIONED_STATUS_MISMATCH',
'Linkable and LinkField do not have matching Versioned applications. Make sure that both are'
. ' either un-Versioned or Versioned'
),
Expand Down Expand Up @@ -203,7 +203,7 @@ public function run($request): void
// Nothing to see here
if ($linkableResults->numRecords() === 0) {
echo _t(
'LinkField.NOTHING_TO_PROCESS',
__CLASS__ . '.NOTHING_TO_PROCESS',
"Nothing to process for `{table}`\r\n",
['table' => $table],
sprintf("Nothing to process for `%s`\r\n", $table)
Expand All @@ -213,7 +213,7 @@ public function run($request): void
}

echo _t(
'LinkField.PROCESSING_TABLE',
__CLASS__ . '.PROCESSING_TABLE',
"Processing `{table}`\r\n",
['table' => $table],
sprintf("Processing `%s`\r\n", $table)
Expand Down Expand Up @@ -248,7 +248,7 @@ public function run($request): void
}

echo _t(
'LinkField.RECORDS_INSERTED',
__CLASS__ . '.RECORDS_INSERTED',
"{numrecords} records inserted, finished processing `{table}`\r\n",
[
'numrecords' => $linkableResults->numRecords(),
Expand Down

0 comments on commit 4caf76b

Please sign in to comment.