Fix edit document using document repository (with flysystem)

pull/3016/head
Julio Montoya 7 years ago
parent a58785e8cc
commit 4572bcf79d
  1. 23
      main/document/edit_document.php
  2. 4
      main/exercise/hotspot_actionscript.as.php
  3. 4
      main/exercise/hotspot_actionscript_admin.as.php
  4. 4
      main/exercise/hotspot_answers.as.php
  5. 72
      src/CoreBundle/Entity/Resource/ResourceFile.php
  6. 47
      src/CourseBundle/Repository/CDocumentRepository.php
  7. 2
      src/CourseBundle/Resources/config/services.yml

@ -266,8 +266,9 @@ if (isset($_POST['comment'])) {
}
$em = Database::getManager();
$documentRepository = Container::$container->get('Chamilo\CourseBundle\Repository\CDocumentRepository');
/** @var CDocument $document */
$document = $em->getRepository('ChamiloCourseBundle:CDocument')->find($document_data['iid']);
$document = $documentRepository->find($document_data['iid']);
if ($is_allowed_to_edit) {
if (isset($_POST['formSent']) && $_POST['formSent'] == 1 && !empty($document_id)) {
@ -282,20 +283,7 @@ if ($is_allowed_to_edit) {
if ($read_only_flag == 0) {
if (!empty($content)) {
$node = $document->getResourceNode();
$file = $node->getResourceFile();
if ($file) {
$media = $node->getResourceFile()->getMedia();
$provider = Container::$container->get('sonata.media.pool')->getProvider($media->getProviderName());
$reference = $provider->getReferenceFile($media);
//$node->setUpdatedAt(new \DateTime());
$reference->setContent($content);
$media->setSize($reference->getSize());
$em->merge($media);
$em->merge($node);
$em->flush();
}
$documentRepository->updateDocumentContent($document, $content);
}
}
@ -321,10 +309,7 @@ if (in_array($extension, ['html', 'htm'])) {
$file = $node->getResourceFile();
if ($file) {
$media = $node->getResourceFile()->getMedia();
$provider = Container::$container->get('sonata.media.pool')->getProvider($media->getProviderName());
$reference = $provider->getReferenceFile($media);
$content = $reference->getContent();
$content = $documentRepository->getDocumentContent($document_data['id']);
}
}

@ -29,8 +29,8 @@ $answer_type = $objQuestion->selectType(); //very important
$TBL_ANSWERS = Database::get_course_table(TABLE_QUIZ_ANSWER);
$picture = $objQuestion->getPicture();
$pictureName = $objQuestion->getPictureFilename();
$pictureWidth = $picture->getResourceNode()->getResourceFile()->getMedia()->getWidth();
$pictureHeight = $picture->getResourceNode()->getResourceFile()->getMedia()->getHeight();
$pictureWidth = $picture->getResourceNode()->getResourceFile()->getWidth();
$pictureHeight = $picture->getResourceNode()->getResourceFile()->getHeight();
$course_id = api_get_course_int_id();

@ -26,8 +26,8 @@ $questionId = isset($_GET['modifyAnswers']) ? (int) $_GET['modifyAnswers'] : 0;
$objQuestion = Question::read($questionId);
$picture = $objQuestion->getPicture();
$pictureName = $objQuestion->getPictureFilename();
$pictureWidth = $picture->getResourceNode()->getResourceFile()->getMedia()->getWidth();
$pictureHeight = $picture->getResourceNode()->getResourceFile()->getMedia()->getHeight();
$pictureWidth = $picture->getResourceNode()->getResourceFile()->getWidth();
$pictureHeight = $picture->getResourceNode()->getResourceFile()->getHeight();
$data = [];
$data['type'] = 'admin';

@ -60,8 +60,8 @@ if (empty($objQuestion) || empty($objExercise)) {
$em = Database::getManager();
$picture = $objQuestion->getPicture();
$pictureWidth = $picture->getResourceNode()->getResourceFile()->getMedia()->getWidth();
$pictureHeight = $picture->getResourceNode()->getResourceFile()->getMedia()->getHeight();
$pictureWidth = $picture->getResourceNode()->getResourceFile()->getWidth();
$pictureHeight = $picture->getResourceNode()->getResourceFile()->getHeight();
$data = [];
$data['type'] = 'solution';

@ -270,46 +270,6 @@ class ResourceFile
return $this;
}
/**
* @return string
*/
public function getWidth(): string
{
return $this->width;
}
/**
* @param string $width
*
* @return ResourceFile
*/
public function setWidth(string $width): ResourceFile
{
$this->width = $width;
return $this;
}
/**
* @return string
*/
public function getHeight(): string
{
return $this->height;
}
/**
* @param string $height
*
* @return ResourceFile
*/
public function setHeight(string $height): ResourceFile
{
$this->height = $height;
return $this;
}
/**
* @return string
*/
@ -518,6 +478,38 @@ class ResourceFile
return $this;
}
/**
* @return int
*/
public function getWidth(): int
{
$data = $this->getDimensions();
if ($data) {
$data = explode(',', $data);
return (int) $data[0];
}
return 0;
}
/**
* @return int
*/
public function getHeight(): int
{
$data = $this->getDimensions();
if ($data) {
$data = explode(',', $data);
return (int) $data[1];
}
return 0;
}
/**
* @return File
*/

@ -12,8 +12,9 @@ use Chamilo\CourseBundle\Entity\CDocument;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Gaufrette\Exception\FileNotFound;
use League\Flysystem\FilesystemInterface;
use League\Flysystem\MountManager;
use Sonata\MediaBundle\Provider\MediaProviderInterface;
use Sonata\MediaBundle\Provider\Pool;
/**
* Class CDocumentRepository.
@ -26,20 +27,19 @@ class CDocumentRepository extends ResourceRepository
private $repository;
/**
* @var Pool
* @var FilesystemInterface
*/
private $mediaPool;
private $fs;
/**
* CDocumentRepository constructor.
*
* @param EntityManager $entityManager
* @param Pool $mediaPool
*/
public function __construct(EntityManager $entityManager, Pool $mediaPool)
public function __construct(EntityManager $entityManager, MountManager $mountManager)
{
$this->repository = $entityManager->getRepository(CDocument::class);
$this->mediaPool = $mediaPool;
$this->fs = $mountManager->getFilesystem('resources_fs');
$this->entityManager = $entityManager;
}
@ -91,6 +91,39 @@ class CDocumentRepository extends ResourceRepository
}
}
public function getDocumentContent($id): string
{
try {
$document = $this->find($id);
$resourceNode = $document->getResourceNode();
$resourceFile = $resourceNode->getResourceFile();
$fileName = $resourceFile->getFile()->getPathname();
return $this->fs->read($fileName);
} catch (\Throwable $exception) {
throw new FileNotFound($id);
}
}
public function updateDocumentContent(CDocument $document, $content)
{
try {
//$document = $this->find($id);
$resourceNode = $document->getResourceNode();
$resourceFile = $resourceNode->getResourceFile();
$fileName = $resourceFile->getFile()->getPathname();
$this->fs->update($fileName, $content);
$size = $this->fs->getSize($fileName);
$document->setSize($size);
$this->entityManager->persist($document);
return true;
} catch (\Throwable $exception) {
throw new $exception;
}
}
/**
* @param CDocument $document
*
@ -100,7 +133,7 @@ class CDocumentRepository extends ResourceRepository
{
$resourceParent = $document->getResourceNode()->getParent();
if (!empty($resourceParent)) {
if ($resourceParent !== null) {
$resourceParentId = $resourceParent->getId();
$criteria = [
'resourceNode' => $resourceParentId,

@ -14,7 +14,7 @@ services:
autowire: true
public: true
Sonata\MediaBundle\Provider\Pool: '@sonata.media.pool'
League\Flysystem\MountManager: '@oneup_flysystem.mount_manager'
# Classic entity repositories
Chamilo\CourseBundle\Repository\:

Loading…
Cancel
Save