Issues (47)

src/fields/Video.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * @link      https://dukt.net/videos/
4
 * @copyright Copyright (c) Dukt
5
 * @license   https://github.com/dukt/videos/blob/v2/LICENSE.md
6
 */
7
8
namespace dukt\videos\fields;
9
10
use Craft;
11
use craft\base\ElementInterface;
12
use craft\base\Field;
13
use craft\helpers\Db;
14
use craft\helpers\StringHelper;
15
use dukt\videos\helpers\VideosHelper;
16
use dukt\videos\Plugin as Videos;
17
use dukt\videos\web\assets\videos\VideosAsset;
18
use craft\helpers\Html;
19
20
/**
21
 * Video field
22
 */
23
class Video extends Field
24
{
25
    // Public Methods
26
    // =========================================================================
27
28
    /**
29
     * Get the field’s name.
30
     *
31
     * @return string
32
     */
33
    public function getName(): string
34
    {
35
        return Craft::t('videos', 'Videos');
36
    }
37
38
    /**
39
     * Get Input HTML.
40
     *
41
     * @param                       $value
42
     * @param ElementInterface|null $element
43
     *
44
     * @return string
45
     * @throws \Twig_Error_Loader
46
     * @throws \yii\base\Exception
47
     * @throws \yii\base\InvalidConfigException
48
     */
49
    public function getInputHtml($value, \craft\base\ElementInterface $element = null): string
50
    {
51
        $view = Craft::$app->getView();
52
        $name = $this->handle;
53
54
        // Normalize the element ID into only alphanumeric characters, underscores, and dashes.
55
        $id = Html::id($name);
0 ignored issues
show
It seems like $name can also be of type null; however, parameter $id of craft\helpers\Html::id() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
        $id = Html::id(/** @scrutinizer ignore-type */ $name);
Loading history...
56
57
        // Init CSRF Token
58
        $jsTemplate = 'window.csrfTokenName = "' . Craft::$app->getConfig()->getGeneral()->csrfTokenName . '";';
59
        $jsTemplate .= 'window.csrfTokenValue = "' . Craft::$app->getRequest()->getCsrfToken() . '";';
60
        $js = $view->renderString($jsTemplate);
61
        $view->registerJs($js);
62
63
        // Asset bundle
64
        $view->registerAssetBundle(VideosAsset::class);
65
66
        // Field value
67
        $video = null;
68
69
        if (is_object($value)) {
70
            $video = VideosHelper::videoToArray($value);
71
        }
72
73
        // Translations
74
        $view->registerTranslations('videos', [
75
            'Browse videos…',
76
            'Cancel',
77
            'Enter a video URL from YouTube or Vimeo',
78
            'Remove',
79
            'Search {gateway} videos…',
80
            'Select',
81
            '{plays} plays',
82
        ]);
83
84
        // Variables
85
        $variables = [
86
            'id' => $id,
87
            'name' => $name,
88
            'value' => $video,
89
            'namespaceId' => $view->namespaceInputId($id),
90
            'namespaceName' => $view->namespaceInputName($id),
91
        ];
92
93
        // Instantiate Videos Field
94
        // $view->registerJs('new Videos.Field("'.$view->namespaceInputId($id).'");');
95
        $view->registerJs('new VideoFieldConstructor({data: {fieldVariables: ' . \json_encode($variables) . '}}).$mount("#' . $view->namespaceInputId($id) . '-vue");');
96
97
        return $view->renderTemplate('videos/_components/fieldtypes/Video/input', $variables);
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103
    public function serializeValue($value, ElementInterface $element = null)
104
    {
105
        if (!empty($value->url)) {
106
            return Db::prepareValueForDb($value->url);
107
        }
108
109
        return parent::serializeValue($value, $element);
110
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115
    public function normalizeValue($videoUrl, ElementInterface $element = null): ?\dukt\videos\models\Video
116
    {
117
        if ($videoUrl instanceof \dukt\videos\models\Video) {
118
            return $videoUrl;
119
        }
120
121
        try {
122
            if (!empty($videoUrl)) {
123
                $video = Videos::$plugin->getVideos()->getVideoByUrl($videoUrl);
124
125
                if ($video !== null) {
126
                    return $video;
127
                }
128
129
                $video = new \dukt\videos\models\Video();
130
                $video->url = $videoUrl;
131
                $video->addError('url', Craft::t('videos', 'Unable to find the video.'));
132
133
                return $video;
134
            }
135
        } catch (\Exception $exception) {
136
            Craft::info("Couldn't get video in field normalizeValue: " . $exception->getMessage(), __METHOD__);
137
        }
138
139
        return null;
140
    }
141
142
    /**
143
     * Get Search Keywords
144
     *
145
     * @param mixed $value
146
     * @param ElementInterface $element
147
     *
148
     * @return string
149
     */
150
    public function getSearchKeywords($value, \craft\base\ElementInterface $element): string
151
    {
152
        $keywords = [];
153
154
        if ($value instanceof \dukt\videos\models\Video) {
155
            $keywords[] = $value->id;
156
            $keywords[] = $value->url;
157
            $keywords[] = $value->gatewayHandle;
158
            $keywords[] = $value->gatewayName;
159
            $keywords[] = $value->authorName;
160
            $keywords[] = $value->authorUsername;
161
            $keywords[] = $value->title;
162
            $keywords[] = $value->description;
163
        }
164
165
        $searchKeywords = StringHelper::toString($keywords, ' ');
166
167
        return StringHelper::encodeMb4($searchKeywords);
168
    }
169
}
170