View source
<?php
namespace Drupal\opigno_module\H5PImportClasses;
class H5PStorageImport extends \H5PStorage {
public function saveLibraries() {
$newOnes = 0;
$oldOnes = 0;
foreach ($this->h5pC->librariesJsonData as $libString => &$library) {
$libraryId = $this->h5pC
->getLibraryId($library, $libString);
$new = TRUE;
if ($libraryId) {
$library['libraryId'] = $libraryId;
if ($this->h5pF
->isPatchedLibrary($library)) {
$new = FALSE;
}
else {
$library['saveDependencies'] = FALSE;
continue;
}
}
$library['saveDependencies'] = TRUE;
$this->h5pF
->saveLibraryData($library, $new);
$this->h5pC->fs
->saveLibrary($library);
if ($this->h5pC->aggregateAssets && isset($library['libraryId'])) {
$removedKeys = $this->h5pF
->deleteCachedAssets($library['libraryId']);
$this->h5pC->fs
->deleteCachedAssets($removedKeys);
}
self::deleteFileTree($library['uploadDirectory']);
if ($new) {
$message = $this->h5pF
->t('Added library @lib', [
'@lib' => $libString,
]);
\Drupal::logger('opigno_import')
->notice($message);
$newOnes++;
}
else {
$oldOnes++;
}
}
foreach ($this->h5pC->librariesJsonData as &$library) {
if (!$library['saveDependencies']) {
continue;
}
$this->h5pF
->deleteLibraryDependencies($library['libraryId']);
if (isset($library['preloadedDependencies'])) {
$this->h5pF
->saveLibraryDependencies($library['libraryId'], $library['preloadedDependencies'], 'preloaded');
}
if (isset($library['dynamicDependencies'])) {
$this->h5pF
->saveLibraryDependencies($library['libraryId'], $library['dynamicDependencies'], 'dynamic');
}
if (isset($library['editorDependencies'])) {
$this->h5pF
->saveLibraryDependencies($library['libraryId'], $library['editorDependencies'], 'editor');
}
$this->h5pF
->clearFilteredParameters($library['libraryId']);
}
if ($newOnes && $oldOnes) {
if ($newOnes === 1) {
if ($oldOnes === 1) {
$message = $this->h5pF
->t('Added %new new H5P library and updated %old old one.', [
'%new' => $newOnes,
'%old' => $oldOnes,
]);
}
else {
$message = $this->h5pF
->t('Added %new new H5P library and updated %old old ones.', [
'%new' => $newOnes,
'%old' => $oldOnes,
]);
}
}
else {
if ($oldOnes === 1) {
$message = $this->h5pF
->t('Added %new new H5P libraries and updated %old old one.', [
'%new' => $newOnes,
'%old' => $oldOnes,
]);
}
else {
$message = $this->h5pF
->t('Added %new new H5P libraries and updated %old old ones.', [
'%new' => $newOnes,
'%old' => $oldOnes,
]);
}
}
}
elseif ($newOnes) {
if ($newOnes === 1) {
$message = $this->h5pF
->t('Added %new new H5P library.', [
'%new' => $newOnes,
]);
}
else {
$message = $this->h5pF
->t('Added %new new H5P libraries.', [
'%new' => $newOnes,
]);
}
}
elseif ($oldOnes) {
if ($oldOnes === 1) {
$message = $this->h5pF
->t('Updated %old H5P library.', [
'%old' => $oldOnes,
]);
}
else {
$message = $this->h5pF
->t('Updated %old H5P libraries.', [
'%old' => $oldOnes,
]);
}
}
if (isset($message)) {
\Drupal::logger('opigno_import')
->notice($message);
}
}
public static function deleteFileTree($dir) {
if (!is_dir($dir)) {
return FALSE;
}
if (is_link($dir)) {
unlink($dir);
return TRUE;
}
$files = array_diff(scandir($dir), [
'.',
'..',
]);
foreach ($files as $file) {
$filepath = "{$dir}/{$file}";
if (!is_dir($filepath) || is_link($filepath)) {
unlink($filepath);
}
else {
self::deleteFileTree($filepath);
}
}
return rmdir($dir);
}
}