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