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