Completed
Push — master ( a48a4d...99fe23 )
by Abdelouahab
04:39
created

EditorStorage::getAvailableLanguages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

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