MediaModule::loadView()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ByTIC\MediaLibraryModule;
4
5
use ByTIC\Assets\Assets;
6
use ByTIC\MediaLibrary\HasMedia\HasMediaTrait;
7
use ByTIC\MediaLibraryModule\Application\Library\View\View;
8
9
/**
10
 * Class MediaModule
11
 * @package ByTIC\MediaLibraryModule
12
 */
13
class MediaModule
14
{
15
    public static function initAssets()
16
    {
17
        $assetEntry = Assets::entry();
18
        if (assets_manager()->hasEntrypoint('media-library')) {
19
            $assetEntry->addFromWebpack('media-library');
20
21
            return;
22
        }
23
24
        Assets::entry()->scripts()->addRaw(self::loadAssetContent('/js/_dropzone.min.js'));
25
        Assets::entry()->scripts()->addRaw(self::loadAssetContent('/js/_cropper.min.js'));
26
        Assets::entry()->scripts()->addRaw(self::loadAssetContent('/js/_init-cropper.js'));
27
        Assets::entry()->scripts()->addRaw(self::loadAssetContent('/js/_init-dropzone.js'));
28
        Assets::entry()->scripts()->addRaw(self::loadAssetContent('/js/_media-manage.js'));
29
30
        Assets::entry()->styles()->addRaw(self::loadAssetContent('/css/dropzone.min.css'));
31
        Assets::entry()->styles()->addRaw(self::loadAssetContent('/css/cropper.min.css'));
32
        Assets::entry()->styles()->addRaw(self::loadAssetContent('/css/gallery.css'));
33
    }
34
35
    /**
36
     * @param $path
37
     *
38
     * @return null|string
39
     */
40
    public static function loadAssetContent($path)
41
    {
42
        $fullPath = self::basePath()
43
                    . DIRECTORY_SEPARATOR . 'resources'
44
                    . DIRECTORY_SEPARATOR . 'assets'
45
                    . $path;
46
        if (file_exists($fullPath)) {
47
            return file_get_contents($fullPath);
48
        }
49
50
        return '';
51
    }
52
53
    /**
54
     * @return string
55
     */
56 1
    public static function basePath()
57
    {
58 1
        return dirname(__DIR__);
59
    }
60
61
    /**
62
     * @param HasMediaTrait $item
63
     *
64
     * @return null|string
65
     */
66
    public static function getAdminImagesGridForModel($item, $type = 'images')
67
    {
68
        $images = $item->getMedia($type);
69
70
        return self::loadView(
71
            '/admin/gallery/' . $type . '-grid',
72
            ['item' => $item, 'images' => $images]
73
        );
74
    }
75
76
    /**
77
     * @param HasMediaTrait $item
78
     *
79
     * @return null|string
80
     */
81
    public static function getAdminPanelForModel($item, $view, $type = 'images')
82
    {
83
        $images = $item->getMedia($type);
84
85
        return self::loadView(
86
            '/admin/panels/' . $type . '-gallery',
87
            [
88
                'item' => $item,
89
                'viewObj' => $view,
90
                'images' => $images,
91
            ]
92
        );
93
    }
94
95
    /**
96
     * @param $path
97
     * @param array $variables
98
     *
99
     * @return null|string
100
     */
101
    public static function loadView($path, $variables = [])
102
    {
103
        return View::instance()->load($path, $variables, true);
104
    }
105
}
106