Skip to content

Commit

Permalink
h5p json fixes (#184)
Browse files Browse the repository at this point in the history
* h5p json fixes

* fix

Co-authored-by: Tomasz Smolarek <tomasz.smolarek@escolasoft.com>
  • Loading branch information
dyfero and dyfero authored Nov 21, 2022
1 parent 746c0f5 commit 0d382b9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
22 changes: 11 additions & 11 deletions src/Helpers/JSONHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@

class JSONHelper
{
public static function clearStr(array $chars, string $to, string $jsonString): string
public static function clearJson($json): string
{
return str_replace($chars, $to, $jsonString);
}
if (empty($json)) {
return '';
}

public static function clearObj(array $chars, string $to, object $json): string
{
return str_replace($chars, $to, json_encode($json));
if (is_object($json) || is_array($json)) {
$json = json_encode($json);
}

$json = str_replace(['\n', '\t'], '', $json);
$json = str_replace(['\"', '&quot;'], '\'', $json);
return str_replace(['\/'], '/', $json);
}

public static function compareStr(string $jsonString1, string $jsonString2): bool
{
return json_encode(json_decode($jsonString1)) === json_encode(json_decode($jsonString2));
}

public static function compareObj(object $json1, object $json2): bool
{
return json_encode($json1) === json_encode($json2);
}

public static function compareArr(array $json1, array $json2): bool
{
return json_encode($json1) === json_encode($json2);
Expand Down
6 changes: 3 additions & 3 deletions src/Repositories/H5PRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -1003,9 +1003,9 @@ public function loadContent($id)
}
$content = $content->toArray();
$content['contentId'] = $content['id']; // : Identifier for the content
$content['parameters'] = JSONHelper::clearStr(['\"', '&quot;'], '\'', $content['parameters']); // : json content as string
$content['params'] = JSONHelper::clearObj(['\"', '&quot;'], '\'', $content['params']); // : json content as string
$content['filtered'] = isset($content['filtered']) ? JSONHelper::clearStr(['\"', '&quot;'], '\'', $content['filtered']) : ''; // : json content as string
$content['parameters'] = JSONHelper::clearJson($content['parameters']); // : json content as string
$content['params'] = JSONHelper::clearJson($content['params']); // : json content as string
$content['filtered'] = JSONHelper::clearJson($content['filtered']); // : json content as string
$content['embedType'] = \H5PCore::determineEmbedType($content['embed_type'] ?? 'div', $content['library']['embed_types']); // : csv of embed types
//$content ['language'] // : Language code for the content
$content['libraryId'] = $content['library_id']; // : Id for the main library
Expand Down
16 changes: 12 additions & 4 deletions src/Services/HeadlessH5PService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EscolaLms\HeadlessH5P\Services;

use EscolaLms\HeadlessH5P\Exceptions\H5PException;
use EscolaLms\HeadlessH5P\Helpers\JSONHelper;
use EscolaLms\HeadlessH5P\Helpers\MargeFiles;
use EscolaLms\HeadlessH5P\Models\H5PLibrary;
use EscolaLms\HeadlessH5P\Repositories\Contracts\H5PFrameworkInterface;
Expand Down Expand Up @@ -427,10 +428,14 @@ public function getContentSettings($id, ?string $token = null): array

$uberName = $library['name'] . ' ' . $library['majorVersion'] . '.' . $library['minorVersion'];

$jsonContent = empty($content['filtered'])
? JSONHelper::clearJson($this->getCore()->filterParameters($content))
: JSONHelper::clearJson($content['filtered']);

$settings['contents']["cid-$id"] = [
'library' => $uberName,
'content' => $content,
'jsonContent' => $content['filtered'],
'jsonContent' => $jsonContent,
'fullScreen' => $content['library']['fullscreen'],
// TODO check all of those endpointis are working fine
'exportUrl' => config('hh5p.h5p_export') && $token ? route('hh5p.content.export', [$content['id'], '_token' => $token]) : '',
Expand Down Expand Up @@ -504,13 +509,17 @@ public function getSettingsForContent($id): array

$uberName = $library['name'] . ' ' . $library['majorVersion'] . '.' . $library['minorVersion'];

$jsonContent = empty($content['filtered'])
? JSONHelper::clearJson($this->getCore()->filterParameters($content))
: JSONHelper::clearJson($content['filtered']);

$settings = [
'library' => $uberName,
'content' => $content,
'jsonContent' => json_encode([
'params' => json_decode(str_replace(['\"', '&quot;'], '\'', str_replace(['\n', '\t'], '', $content['filtered']))),
'params' => json_decode($jsonContent),
'metadata' => $content['metadata'],
], JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE),
]),
'fullScreen' => $content['library']['fullscreen'],
'exportUrl' => config('hh5p.h5p_export') ? route('hh5p.content.export', [$content['id']]) : '',
//'embedCode' => '<iframe src="'.route('h5p.embed', ['id' => $content['id']]).'" width=":w" height=":h" frameborder="0" allowfullscreen="allowfullscreen"></iframe>',
Expand All @@ -530,7 +539,6 @@ public function getSettingsForContent($id): array
'nonce' => $content['nonce'],
];


return $settings;
}

Expand Down

0 comments on commit 0d382b9

Please sign in to comment.