-
Notifications
You must be signed in to change notification settings - Fork 487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documents: Add support for URL-specific document variations - refs #5956 #5976
Open
christianbeeznest
wants to merge
3
commits into
chamilo:master
Choose a base branch
from
christianbeeznest:GH-5956
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,223 @@ | ||
<template> | ||
<div class="p-4 space-y-8"> | ||
<SectionHeader :title="t('Add File Variation')"> | ||
<BaseButton | ||
:label="t('Back to Documents')" | ||
icon="back" | ||
type="gray" | ||
@click="goBack" | ||
/> | ||
</SectionHeader> | ||
|
||
<div v-if="originalFile" class="bg-gray-100 p-4 rounded-md shadow-md"> | ||
<h3 class="text-lg font-semibold">{{ t('Original File') }}</h3> | ||
<p><strong>{{ t('Title:') }}</strong> {{ originalFile.originalName }}</p> | ||
<p><strong>{{ t('Format:') }}</strong> {{ originalFile.mimeType }}</p> | ||
<p><strong>{{ t('Size:') }}</strong> {{ prettyBytes(originalFile.size) }}</p> | ||
</div> | ||
|
||
<div class="space-y-6"> | ||
<h3 class="text-xl font-bold">{{ t('Upload New Variation') }}</h3> | ||
|
||
<form @submit.prevent="uploadVariation" class="flex flex-col space-y-4"> | ||
<div class="grid grid-cols-1 md:grid-cols-2 gap-4"> | ||
<BaseFileUpload | ||
@file-selected="onFileSelected" | ||
:label="t('Choose file')" | ||
accept=".pdf,.html,.docx,.mp4" | ||
required | ||
class="w-full" | ||
/> | ||
|
||
<Dropdown | ||
v-model="selectedAccessUrl" | ||
:options="accessUrls" | ||
optionLabel="url" | ||
optionValue="id" | ||
placeholder="Select a URL" | ||
class="w-full" | ||
/> | ||
</div> | ||
|
||
<div class="flex justify-end"> | ||
<BaseButton | ||
:label="t('Upload')" | ||
icon="file-upload" | ||
type="success" | ||
:disabled="!file" | ||
@click="uploadVariant(file, originalFile?.resourceNode?.id, selectedAccessUrl)" | ||
/> | ||
</div> | ||
</form> | ||
</div> | ||
|
||
<div> | ||
<h3 class="text-xl font-bold mb-4">{{ t('Current Variations') }}</h3> | ||
<DataTable :value="variations" class="w-full"> | ||
<Column field="title" :header="t('Title')" /> | ||
<Column field="mimeType" :header="t('Format')" /> | ||
<Column field="size" :header="t('Size')"> | ||
<template #body="slotProps"> | ||
{{ prettyBytes(slotProps.data.size) }} | ||
</template> | ||
</Column> | ||
<Column field="updatedAt" :header="t('Updated At')" /> | ||
<Column field="url" :header="t('URL')"> | ||
<template #body="slotProps"> | ||
<a | ||
:href="slotProps.data.path" | ||
target="_blank" | ||
class="text-blue-500 hover:underline" | ||
> | ||
{{ t('View') }} | ||
</a> | ||
</template> | ||
</Column> | ||
<Column field="creator" :header="t('Creator')" /> | ||
<Column field="accessUrl" :header="t('Associated URL')"> | ||
<template #body="slotProps"> | ||
<span> | ||
{{ slotProps.data.url ? slotProps.data.url : t('Default (No URL)') }} | ||
</span> | ||
</template> | ||
</Column> | ||
<Column> | ||
<template #header>{{ t('Actions') }}</template> | ||
<template #body="slotProps"> | ||
<BaseButton | ||
:label="t('Delete')" | ||
icon="delete" | ||
type="danger" | ||
@click="deleteVariant(slotProps.data.id)" | ||
/> | ||
</template> | ||
</Column> | ||
</DataTable> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script setup> | ||
import { ref, onMounted, computed } from "vue" | ||
import { useRoute, useRouter } from 'vue-router' | ||
import { useI18n } from 'vue-i18n' | ||
import axios from 'axios' | ||
import DataTable from 'primevue/datatable' | ||
import Column from 'primevue/column' | ||
import SectionHeader from "../../components/layout/SectionHeader.vue" | ||
import BaseButton from "../../components/basecomponents/BaseButton.vue" | ||
import BaseFileUpload from "../../components/basecomponents/BaseFileUpload.vue" | ||
import prettyBytes from 'pretty-bytes' | ||
import { useCidReq } from "../../composables/cidReq" | ||
import { useSecurityStore } from "../../store/securityStore" | ||
|
||
const securityStore = useSecurityStore() | ||
const route = useRoute() | ||
const router = useRouter() | ||
const { t } = useI18n() | ||
const { cid, sid, gid } = useCidReq() | ||
const file = ref(null) | ||
const variations = ref([]) | ||
const originalFile = ref(null) | ||
const resourceFileId = route.params.resourceFileId | ||
const selectedAccessUrl = ref(null) | ||
const accessUrls = ref([]) | ||
const isAdmin = computed(() => securityStore.isAdmin) | ||
|
||
onMounted(async () => { | ||
if (!isAdmin.value) { | ||
await router.push({ name: 'DocumentsList' }) | ||
return | ||
} | ||
|
||
await fetchOriginalFile() | ||
await fetchVariations() | ||
await fetchAccessUrls() | ||
}) | ||
|
||
async function fetchVariations() { | ||
if (!originalFile.value?.resourceNode?.id) { | ||
console.error('ResourceNodeId is undefined. Cannot fetch variations.') | ||
return | ||
} | ||
|
||
try { | ||
const resourceNodeId = originalFile.value.resourceNode.id | ||
const response = await axios.get(`/r/resource_files/${resourceNodeId}/variants`) | ||
variations.value = response.data | ||
} catch (error) { | ||
console.error('Error fetching variations:', error) | ||
} | ||
} | ||
|
||
async function fetchAccessUrls() { | ||
try { | ||
const response = await axios.get('/api/access_urls') | ||
if (Array.isArray(response.data['hydra:member'])) { | ||
const currentAccessUrlId = window.access_url_id | ||
|
||
accessUrls.value = response.data['hydra:member'].filter( | ||
(url) => url.id !== currentAccessUrlId | ||
) | ||
} else { | ||
accessUrls.value = [] | ||
} | ||
} catch (error) { | ||
console.error('Error fetching access URLs:', error) | ||
accessUrls.value = [] | ||
} | ||
} | ||
|
||
async function fetchOriginalFile() { | ||
try { | ||
const response = await axios.get(`/api/resource_files/${resourceFileId}`) | ||
originalFile.value = response.data | ||
} catch (error) { | ||
console.error('Error fetching original file:', error) | ||
} | ||
} | ||
|
||
async function uploadVariant(file, resourceNodeId, accessUrlId) { | ||
if (!resourceNodeId) { | ||
console.error('ResourceNodeId is undefined. Check originalFile:', originalFile.value) | ||
return | ||
} | ||
|
||
const formData = new FormData() | ||
formData.append('file', file) | ||
formData.append('resourceNodeId', resourceNodeId) | ||
if (accessUrlId) { | ||
formData.append('accessUrlId', accessUrlId) | ||
} | ||
|
||
try { | ||
const response = await axios.post('/api/resource_files/add_variant', formData) | ||
console.log('Variant uploaded or updated successfully:', response.data) | ||
|
||
await fetchVariations() | ||
file.value = null | ||
selectedAccessUrl.value = null | ||
} catch (error) { | ||
console.error('Error uploading variant:', error) | ||
} | ||
} | ||
|
||
async function deleteVariant(variantId) { | ||
try { | ||
await axios.delete(`/r/resource_files/${variantId}/delete_variant`) | ||
console.log('Variant deleted successfully.') | ||
await fetchVariations() | ||
} catch (error) { | ||
console.error('Error deleting variant:', error) | ||
} | ||
} | ||
|
||
function onFileSelected(selectedFile) { | ||
file.value = selectedFile | ||
} | ||
|
||
function goBack() { | ||
let queryParams = { cid, sid, gid } | ||
router.push({ name: "DocumentsList", params: { node: parent.id }, query: queryParams }) | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/CoreBundle/Controller/AddVariantResourceFileAction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* For licensing terms, see /license.txt */ | ||
|
||
namespace Chamilo\CoreBundle\Controller; | ||
|
||
use Chamilo\CoreBundle\Entity\ResourceFile; | ||
use Chamilo\CoreBundle\Entity\ResourceNode; | ||
use Chamilo\CoreBundle\Entity\AccessUrl; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
class AddVariantResourceFileAction | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing class doc comment |
||
{ | ||
public function __invoke(Request $request, EntityManagerInterface $em): ResourceFile | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing function doc comment |
||
{ | ||
$uploadedFile = $request->files->get('file'); | ||
if (!$uploadedFile) { | ||
throw new BadRequestHttpException('"file" is required'); | ||
} | ||
|
||
$resourceNodeId = $request->get('resourceNodeId'); | ||
if (!$resourceNodeId) { | ||
throw new BadRequestHttpException('"resourceNodeId" is required'); | ||
} | ||
|
||
$resourceNode = $em->getRepository(ResourceNode::class)->find($resourceNodeId); | ||
if (!$resourceNode) { | ||
throw new NotFoundHttpException('ResourceNode not found'); | ||
} | ||
|
||
$accessUrlId = $request->get('accessUrlId'); | ||
$accessUrl = null; | ||
if ($accessUrlId) { | ||
$accessUrl = $em->getRepository(AccessUrl::class)->find($accessUrlId); | ||
if (!$accessUrl) { | ||
throw new NotFoundHttpException('AccessUrl not found'); | ||
} | ||
} | ||
|
||
$existingResourceFile = $em->getRepository(ResourceFile::class)->findOneBy([ | ||
'resourceNode' => $resourceNode, | ||
'accessUrl' => $accessUrl, | ||
]); | ||
|
||
if ($existingResourceFile) { | ||
$existingResourceFile->setTitle($uploadedFile->getClientOriginalName()); | ||
$existingResourceFile->setFile($uploadedFile); | ||
$existingResourceFile->setUpdatedAt(\DateTime::createFromImmutable(new \DateTimeImmutable())); | ||
$resourceFile = $existingResourceFile; | ||
} else { | ||
$resourceFile = new ResourceFile(); | ||
$resourceFile->setTitle($uploadedFile->getClientOriginalName()); | ||
$resourceFile->setFile($uploadedFile); | ||
$resourceFile->setResourceNode($resourceNode); | ||
|
||
if ($accessUrl) { | ||
$resourceFile->setAccessUrl($accessUrl); | ||
} | ||
} | ||
|
||
$em->persist($resourceFile); | ||
$em->flush(); | ||
|
||
return $resourceFile; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a single space around assignment operators