EditorStorage::getLanguage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 3
c 2
b 1
f 0
nc 2
nop 4
dl 0
loc 13
rs 10
1
<?php
2
3
/*
4
 *
5
 * @Project        Expression project.displayName is undefined on line 5, column 35 in Templates/Licenses/license-default.txt.
6
 * @Copyright      Djoudi
7
 * @Created        2017-02-01
8
 * @Filename       H5pStorage.php
9
 * @Description
10
 *
11
 */
12
13
namespace Djoudi\LaravelH5p\Storages;
14
15
use App;
16
use DB;
17
use Djoudi\LaravelH5p\Eloquents\H5pLibrary;
18
use Djoudi\LaravelH5p\Eloquents\H5pTmpfile;
19
use H5peditorStorage;
20
21
/**
22
 * Description of H5pStorage.
23
 *
24
 * @author leechanrin
25
 */
26
class EditorStorage implements H5peditorStorage
27
{
28
    public function alterLibraryFiles(&$files, $libraries)
29
    {
30
        $h5p = App::make('LaravelH5p');
31
        $h5p->alter_assets($files, $libraries, 'editor');
32
    }
33
34
    public function getAvailableLanguages($machineName, $majorVersion, $minorVersion)
35
    {
36
    }
37
38
    public function getLanguage($machineName, $majorVersion, $minorVersion, $language)
39
    {
40
//        $language = 'ja';
41
        // Load translation field from DB
42
        $return = DB::select('SELECT hlt.translation FROM h5p_libraries_languages hlt
43
           JOIN h5p_libraries hl ON hl.id = hlt.library_id
44
          WHERE hl.name = ?
45
            AND hl.major_version = ?
46
            AND hl.minor_version = ?
47
            AND hlt.language_code = ?', [$machineName, $majorVersion, $minorVersion, $language]
48
        );
49
50
        return $return ? $return[0]->translation : null;
51
    }
52
53
    public function getLibraries($libraries = null)
54
    {
55
        $return = [];
56
57
        if ($libraries !== null) {
58
            // Get details for the specified libraries only.
59
            foreach ($libraries as $library) {
60
                // Look for library
61
                $details = H5pLibrary::where('name', $library->name)
62
                    ->where('major_version', $library->majorVersion)
63
                    ->where('minor_version', $library->minorVersion)
64
                    ->whereNotNull('semantics')
65
                    ->first();
66
67
                if ($details) {
68
                    // Library found, add details to list
69
                    $library->tutorialUrl = $details->tutorial_url;
70
                    $library->title = $details->title;
71
                    $library->runnable = $details->runnable;
72
                    $library->restricted = $details->restricted === '1' ? true : false;
73
                    $return[] = $library;
74
                }
75
            }
76
        } else {
77
78
            // Load all libraries
79
            $libraries = [];
80
            $libraries_result = H5pLibrary::where('runnable', 1)
81
                ->select([
82
                    //                        'id',
83
                    'name',
84
                    'title',
85
                    'major_version AS majorVersion',
86
                    'minor_version AS minorVersion',
87
                    'patch_version AS patchVersion',
88
                    //                        'runnable',
89
                    'restricted',
90
                    //                        'fullscreen',
91
                    //                        'embed_types',
92
                    //                        'preloaded_js',
93
                    //                        'preloaded_css',
94
                    //                        'drop_library_css',
95
                    //                        'semantics',
96
                    'tutorial_url',
97
                    //                        'has_icon',
98
                    //                        'created_at',
99
                    //                        'updated_at'
100
                ])
101
                ->whereNotNull('semantics')
102
                ->orderBy('name', 'ASC')
103
                ->get();
104
105
            // 모든 버전의 라리브러리가 로드되므로 하나의 가장 최신 라이브러리를 찾는 부분
106
            foreach ($libraries_result as $library) {
107
                // Make sure we only display the newest version of a library.
108
                foreach ($libraries as $key => $existingLibrary) {
109
                    if ($library->name === $existingLibrary->name) {
110
                        // Found library with same name, check versions
111
                        if (($library->majorVersion === $existingLibrary->majorVersion &&
112
                            $library->minorVersion > $existingLibrary->minorVersion) ||
113
                            ($library->majorVersion > $existingLibrary->majorVersion)) {
114
                            // This is a newer version
115
                            $existingLibrary->isOld = true;
116
                        } else {
117
                            // This is an older version
118
                            $library->isOld = true;
119
                        }
120
                    }
121
                }
122
                // Check to see if content type should be restricted
123
                $library->restricted = $library->restricted === '1' ? true : false;
124
125
                // Add new library
126
                $return[] = $library;
127
            }
128
        }
129
130
        return $return;
131
    }
132
133
    public function keepFile($fileId)
134
    {
135
        DB::table('h5p_tmpfiles')->where('path', $fileId)->delete();
136
    }
137
138
    public static function markFileForCleanup($file, $content_id)
139
    {
140
        $h5p = App::make('LaravelH5p');
141
        $path = $h5p->get_h5p_storage();
142
        if (empty($content_id)) {
143
            // Should be in editor tmp folder
144
            $path .= '/editor';
145
        } else {
146
            // Should be in content folder
147
            $path .= '/content/'.$content_id;
148
        }
149
        // Add file type to path
150
        $path .= '/'.$file->getType().'s';
151
        // Add filename to path
152
        $path .= '/'.$file->getName();
153
154
        H5pTmpfile::create(['path' => $path, 'created_at' => time()]);
155
        // Keep track of temporary files so they can be cleaned up later.
156
        //        $wpdb->insert($wpdb->prefix . 'h5p_tmpfiles', array('path' => $path, 'created_at' => time()), array('%s', '%d'));
157
        // Clear cached value for dirsize.
158
        //        delete_transient('dirsize_cache');
159
    }
160
161
    public static function removeTemporarilySavedFiles($filePath)
162
    {
163
        if (is_dir($filePath)) {
164
            H5PCore::deleteFileTree($filePath);
0 ignored issues
show
Bug introduced by
The type Djoudi\LaravelH5p\Storages\H5PCore was not found. Did you mean H5PCore? If so, make sure to prefix the type with \.
Loading history...
165
        } else {
166
            unlink($filePath);
167
        }
168
    }
169
170
    public static function saveFileTemporarily($data, $move_file)
171
    {
172
        $h5p = App::make('LaravelH5p');
173
        $path = $h5p::$interface->getUploadedH5pPath();
174
175
        if ($move_file) {
176
            // Move so core can validate the file extension.
177
            rename($data, $path);
178
        } else {
179
            // Create file from data
180
            file_put_contents($path, $data);
181
        }
182
183
        return (object) ['dir' => dirname($path), 'fileName' => basename($path)];
184
    }
185
}
186