1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Retour plugin for Craft CMS |
4
|
|
|
* |
5
|
|
|
* @link https://nystudio107.com/ |
|
|
|
|
6
|
|
|
* @copyright Copyright (c) 2022 nystudio107 |
|
|
|
|
7
|
|
|
* @license https://nystudio107.com/license |
|
|
|
|
8
|
|
|
*/ |
|
|
|
|
9
|
|
|
|
10
|
|
|
namespace nystudio107\retour\fields; |
11
|
|
|
|
12
|
|
|
use Craft; |
13
|
|
|
use craft\base\ElementInterface; |
|
|
|
|
14
|
|
|
use craft\base\Field; |
15
|
|
|
use craft\base\PreviewableFieldInterface; |
16
|
|
|
use craft\helpers\ElementHelper; |
17
|
|
|
use craft\helpers\Json; |
18
|
|
|
use craft\helpers\UrlHelper; |
19
|
|
|
use nystudio107\retour\Retour as RetourPlugin; |
20
|
|
|
use yii\helpers\StringHelper; |
21
|
|
|
|
22
|
|
|
/** |
|
|
|
|
23
|
|
|
* @author nystudio107 |
|
|
|
|
24
|
|
|
* @package Retour |
|
|
|
|
25
|
|
|
* @since 4.1.0 |
|
|
|
|
26
|
|
|
* |
27
|
|
|
* @property-read string $contentColumnType |
28
|
|
|
*/ |
|
|
|
|
29
|
|
|
class ShortLink extends Field implements PreviewableFieldInterface |
30
|
|
|
{ |
31
|
|
|
protected static bool $allowShortLinkUpdates = true; |
32
|
|
|
public string $redirectSrcMatch = 'pathonly'; |
33
|
|
|
public int $redirectHttpCode = 301; |
34
|
|
|
|
35
|
|
|
// Static Methods |
36
|
|
|
// ========================================================================= |
37
|
|
|
|
38
|
|
|
/** |
|
|
|
|
39
|
|
|
* @inheritdoc |
40
|
|
|
*/ |
|
|
|
|
41
|
|
|
public static function displayName(): string |
42
|
|
|
{ |
43
|
|
|
return Craft::t('retour', 'Short Link'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Prevent element updates from updating the short link redirects. |
48
|
|
|
*/ |
|
|
|
|
49
|
|
|
public static function preventShortLinkUpdates(): void |
50
|
|
|
{ |
51
|
|
|
self::$allowShortLinkUpdates = false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Allow element updates to update the short link redirects. |
56
|
|
|
*/ |
|
|
|
|
57
|
|
|
public static function allowShortLinkUpdates(): void |
58
|
|
|
{ |
59
|
|
|
self::$allowShortLinkUpdates = true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Public Methods |
63
|
|
|
// ========================================================================= |
64
|
|
|
|
65
|
|
|
/** |
|
|
|
|
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
|
|
|
|
68
|
|
|
public function getInputHtml($value, ElementInterface $element = null): string |
69
|
|
|
{ |
70
|
|
|
// Render the input template |
71
|
|
|
return Craft::$app->getView()->renderTemplate( |
72
|
|
|
'retour/_components/fields/ShortLink_input', |
73
|
|
|
[ |
74
|
|
|
'name' => $this->handle, |
75
|
|
|
'value' => $value, |
76
|
|
|
'field' => $this, |
77
|
|
|
] |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
|
|
|
|
82
|
|
|
* @inheritdoc |
83
|
|
|
*/ |
|
|
|
|
84
|
|
|
public function getSettingsHtml(): string |
85
|
|
|
{ |
86
|
|
|
return Craft::$app->getView()->renderTemplate('retour/_components/fields/ShortLink_settings', |
|
|
|
|
87
|
|
|
[ |
88
|
|
|
'field' => $this, |
89
|
|
|
]); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
|
|
|
|
93
|
|
|
* @inheritdoc |
94
|
|
|
*/ |
|
|
|
|
95
|
|
|
public function afterElementSave(ElementInterface $element, bool $isNew): void |
96
|
|
|
{ |
97
|
|
|
if (!self::$allowShortLinkUpdates || $element->getIsDraft() || !$element->getSite()->hasUrls) { |
98
|
|
|
return; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$value = $element->getFieldValue($this->handle); |
102
|
|
|
// Return for propagating elements |
103
|
|
|
if ($this->redirectSrcMatch === 'pathonly') { |
104
|
|
|
$parentElement = ElementHelper::rootElement($element); |
|
|
|
|
105
|
|
|
if ($this->translationMethod === Field::TRANSLATION_METHOD_NONE && ($element->propagating || $parentElement->propagating)) { |
106
|
|
|
return; |
107
|
|
|
} |
108
|
|
|
} elseif (!empty($value) && !StringHelper::startsWith($value, 'http')) { |
109
|
|
|
$value = UrlHelper::siteUrl($value, null, null, $element->siteId); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$parentElement = ElementHelper::rootElement($element); |
|
|
|
|
113
|
|
|
RetourPlugin::$plugin->redirects->removeElementRedirect($parentElement, false); |
|
|
|
|
114
|
|
|
|
115
|
|
|
if (!empty($value)) { |
116
|
|
|
$redirectSrcMatch = $this->redirectSrcMatch; |
117
|
|
|
|
118
|
|
|
if ($this->translationMethod !== Field::TRANSLATION_METHOD_NONE) { |
119
|
|
|
if (!UrlHelper::isAbsoluteUrl($value)) { |
120
|
|
|
$value = UrlHelper::siteUrl($value, null, null, $parentElement->siteId); |
121
|
|
|
$redirectSrcMatch = 'fullurl'; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
RetourPlugin::$plugin->redirects->enableElementRedirect($parentElement, $value, $redirectSrcMatch, $this->redirectHttpCode); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
parent::afterElementSave($element, $isNew); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
|
|
|
|
132
|
|
|
* @inheritdoc |
133
|
|
|
*/ |
|
|
|
|
134
|
|
|
public function afterElementDelete(ElementInterface $element): void |
135
|
|
|
{ |
136
|
|
|
if (!$element->getIsCanonical()) { |
137
|
|
|
return; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
RetourPlugin::$plugin->redirects->removeElementRedirect($element, true, true); |
141
|
|
|
parent::afterElementDelete($element); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
|
|
|
|
145
|
|
|
* @inheritdoc |
146
|
|
|
*/ |
|
|
|
|
147
|
|
|
public function getTableAttributeHtml($value, ElementInterface $element): string |
148
|
|
|
{ |
149
|
|
|
$decoded = Json::decodeIfJson($value); |
150
|
|
|
if (is_array($decoded)) { |
151
|
|
|
$value = $decoded['legacyUrl'] ?? ''; |
152
|
|
|
} |
153
|
|
|
// Render the preview template |
154
|
|
|
return Craft::$app->getView()->renderTemplate( |
155
|
|
|
'retour/_components/fields/ShortLink_preview', |
156
|
|
|
[ |
157
|
|
|
'name' => $this->handle, |
158
|
|
|
'value' => $value, |
159
|
|
|
'field' => $this, |
160
|
|
|
] |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
|
|
|
|
165
|
|
|
* @inheritdoc |
166
|
|
|
*/ |
|
|
|
|
167
|
|
|
public function getElementValidationRules(): array |
168
|
|
|
{ |
169
|
|
|
return [ |
170
|
|
|
[ |
171
|
|
|
function(ElementInterface $element) { |
|
|
|
|
172
|
|
|
$value = $element->getFieldValue($this->handle); |
173
|
|
|
$redirect = RetourPlugin::$plugin->getRedirects()->getRedirectByRedirectSrcUrl($value); |
|
|
|
|
174
|
|
|
// Handle drafts |
175
|
|
|
$element = $element->getCanonical(); |
176
|
|
|
if ($redirect && isset($redirect['associatedElementId'])) { |
177
|
|
|
if ($redirect['associatedElementId'] == 0) { |
178
|
|
|
$element->addError($this->handle, Craft::t('retour', 'A Retour redirect with this Legacy URL already exists.')); |
179
|
|
|
} elseif ($redirect['associatedElementId'] !== $element->id) { |
180
|
|
|
$element->addError($this->handle, Craft::t('retour', 'A Short Link with this URL already exists.')); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
}, |
184
|
|
|
], |
185
|
|
|
]; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|