+++ /dev/null
-<?php
-
-
-namespace App\Http\Controllers\API;
-
-
-use App\Http\Controllers\Controller;
-use App\Models\FluidbookTheme;
-use Cubist\Backpack\Magic\Fields\Checkbox;
-use Cubist\Backpack\Magic\Fields\Color;
-use Cubist\Backpack\Magic\Fields\Files;
-use Illuminate\Http\Request;
-use Illuminate\Support\Facades\Auth;
-
-class FluidbookThemeAPIController extends Controller
-{
-
- /**
- * @param $theme FluidbookTheme
- * @param $data array
- * @return FluidbookTheme
- */
- public static function _prepareData($theme, $data)
- {
- $map = ['theme_id' => 'id', 'proprietaire' => 'owner', 'nom' => 'name'];
- foreach ($data as $k => $v) {
- if (isset($map[$k])) {
- $k = $map[$k];
- }
- if (!$theme->hasField($k)) {
- continue;
- }
- $field = $theme->getField($k);
- if ($field instanceof Checkbox) {
- if ($v === 'true' || $v == 1) {
- $v = 1;
- } else if ($v == 0 || $v === 'false' || !$v) {
- $v = 0;
- }
- } else if ($field instanceof Color) {
- if ($v === '' && $field->getAttribute('allows_empty')) {
-
- } else {
- $v = FluidbookTheme::_colorToWS3($v);
- }
- } else if ($field instanceof Files) {
- continue;
- }
- $theme->setAttribute($k, $v);
- }
- return $theme;
- }
-
- /**
- * @param $theme FluidbookTheme
- * @param $data array
- * @return FluidbookTheme
- */
- public static function _handleFiles($theme, $data)
- {
- // $oldRoot = '/home/extranet/www/fluidbook/themes/' . $theme->getAttribute('id') . '/';
- foreach ($theme->getFields() as $field) {
- if (!($field instanceof Files)) {
- continue;
- }
- $k = $field->getName();
- $v = $data[$k] ?? null;
-
- if (!$v) {
- $theme->deleteMediaInField($k);
- //continue;
- }
-// $path = $oldRoot . $v;
-// if (!file_exists($path)) {
-// $opath = str_replace('.svg', '.o.svg', $path);
-// if (file_exists($opath)) {
-// copy($opath, $path);
-// } else {
-// continue;
-// }
-// }
-//
-// $media = $theme->getMediaInField($k);
-// $upload = false;
-//
-// if ($media->count() === 0) {
-// $upload = true;
-// } else {
-// /** @var Media $m */
-// $m = $media->get(0);
-//
-// if ($m->getAttribute('file_name') !== $k) {
-// $upload = true;
-// } else {
-// $mpath = $m->getPath();
-// if (filesize($path) !== filesize($mpath)) {
-// $upload = true;
-// }
-// }
-// }
-//
-// if (!$upload) {
-// continue;
-// }
-// $s = storage_path('themes/' . $theme->getAttribute('id'));
-// if (!file_exists($s)) {
-// mkdir($s);
-// }
-// $f = $s . '/' . $v;
-// copy($path, $f);
-// $theme->addMediaToField($k, $f);
- }
- }
-
-
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function updatefromws2(Request $request, $id)
- {
- FluidbookTheme::$updateWS2SyncViewOnChange = true;
- $theme = FluidbookTheme::find($id);
- self::_prepareData($theme, $request->get('data'));
- $theme->save();
- self::_handleFiles($theme, $request->get('data'));
- }
-
- public function clonefromws2(Request $request, $id)
- {
- FluidbookTheme::$updateWS2SyncViewOnChange = true;
- $theme = FluidbookTheme::find($id);
- $newtheme = $theme->replicate();
- $newtheme->name .= ' (copy)';
- $newtheme->owner = Auth::id();
- $newtheme->push();
-
- return $newtheme->id;
- }
-
- public function renamefromws2(Request $request, $id)
- {
- FluidbookTheme::$updateWS2SyncViewOnChange = true;
- $theme = FluidbookTheme::find($id);
- $theme->name = $request->get('newname');
- $theme->save();
- }
-
- public function deletefromws2(Request $request, $id)
- {
- FluidbookTheme::$updateWS2SyncViewOnChange = true;
- /** @var FluidbookTheme $theme */
- $theme = FluidbookTheme::find($id);
- $theme->delete();
- }
-
- public function uploadfile(Request $request, $id, $fieldname)
- {
- FluidbookTheme::$updateWS2SyncViewOnChange = true;
- /** @var FluidbookTheme $theme */
- $theme = FluidbookTheme::find($id);
- $theme->replaceMediaInField($fieldname, $request->get('path'), true);
- $theme->save();
- }
-}
], function () {
Route::get('fluidbook-publication/{id}/download/{version}', [\App\Http\Controllers\API\FluidbookPublicationAPIController::class, 'download']);
Route::get('fluidbook-publication/{id}/metadata', [\App\Http\Controllers\API\FluidbookPublicationAPIController::class, 'metadata']);
-
- Route::post('fluidbook-theme/{id}/clone', [\App\Http\Controllers\API\FluidbookThemeAPIController::class, 'clonefromws2']);
- Route::put('fluidbook-theme/{id}', [\App\Http\Controllers\API\FluidbookThemeAPIController::class, 'updatefromws2']);
- Route::put('fluidbook-theme/{id}/rename', [\App\Http\Controllers\API\FluidbookThemeAPIController::class, 'renamefromws2']);
- Route::put('fluidbook-theme/{id}/uploadfile/{fieldname}', [\App\Http\Controllers\API\FluidbookThemeAPIController::class, 'uploadfile']);
- Route::delete('fluidbook-theme/{id}', [\App\Http\Controllers\API\FluidbookThemeAPIController::class, 'deletefromws2']);
+ Route::get('fluidbook-publication/create', [\App\Http\Controllers\API\FluidbookPublicationAPIController::class, 'create']);
Route::post('cache/clear/{tag?}', [\App\Http\Controllers\API\CacheAPIController::class, 'clear']);
});