Skip to content

Commit

Permalink
[Forum] fixes rights checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Elorfin committed Nov 15, 2022
1 parent 773be9a commit 459b331
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 285 deletions.
106 changes: 0 additions & 106 deletions src/plugin/claco-form/Command/CreateEntriesFromCsvCommand.php

This file was deleted.

134 changes: 0 additions & 134 deletions src/plugin/claco-form/Manager/ClacoFormManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
use Claroline\CoreBundle\Security\Collection\ResourceCollection;
use Doctrine\Common\Collections\ArrayCollection;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LogLevel;
use Ramsey\Uuid\Uuid;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
Expand Down Expand Up @@ -1062,137 +1061,4 @@ public function registerFile(ClacoForm $clacoForm, UploadedFile $file)
'url' => '../files/clacoform'.$ds.$clacoForm->getUuid().$ds.$fileName,
];
}

/**
* Creates an entries from data from a csv.
*
* @return int
*/
public function importEntryFromCsv(ClacoForm $clacoForm, User $user, array $data)
{
$fieldsMapping = [];
$categoriesMapping = [];
$keywordsMapping = [];
$fields = $clacoForm->getFields();
$categories = $clacoForm->getCategories();
$keywords = $clacoForm->getKeywords();

foreach ($fields as $field) {
$fieldsMapping[$field->getName()] = $field;
}
foreach ($categories as $category) {
$categoriesMapping[$category->getName()] = $category;
}
foreach ($keywords as $keyword) {
$keywordsMapping[$keyword->getName()] = $keyword;
}
if (0 < count($data)) {
$this->om->startFlushSuite();
$now = new \DateTime();

foreach ($data as $index => $entryData) {
$existingEntries = isset($entryData['title']) ?
$this->entryRepo->findBy(['clacoForm' => $clacoForm, 'title' => $entryData['title']]) :
null;
$lineNum = $index + 1;

if (is_null($existingEntries)) {
$this->log("Entry from line {$lineNum} has no title or it is simply an empty line at the end of the file.", LogLevel::WARNING);
} elseif (0 === count($existingEntries)) {
$this->log("Importing entry from line {$lineNum}...");
$entry = new Entry();
$entry->setUser($user);
$entry->setClacoForm($clacoForm);
$entry->setStatus(Entry::PUBLISHED);
$entry->setCreationDate($now);
$entry->setPublicationDate($now);

foreach ($entryData as $key => $value) {
switch ($key) {
case 'title':
$entry->setTitle($value);
break;
case 'status':
$entry->setStatus(intval($value));
break;
case 'categories':
$categoriesNames = explode(',', $value);

foreach ($categoriesNames as $categoryName) {
if (isset($categoriesMapping[$categoryName])) {
$entry->addCategory($categoriesMapping[$categoryName]);
}
}
break;
case 'keywords':
$keywordsNames = explode(',', $value);

foreach ($keywordsNames as $keywordName) {
if (isset($keywordsMapping[$keywordName])) {
$entry->addKeyword($keywordsMapping[$keywordName]);
}
}
break;
case 'comments':
$contents = explode('|', $value);

foreach ($contents as $content) {
$comment = new Comment();
$comment->setEntry($entry);
$comment->setUser($user);
$comment->setContent($content);
$comment->setCreationDate($now);
$comment->setStatus(Comment::VALIDATED);
$this->om->persist($comment);
}
break;
default:
if (isset($fieldsMapping[$key])) {
$field = $fieldsMapping[$key];
$fieldFacet = $field->getFieldFacet();
$fieldValue = new FieldValue();
$fieldValue->setEntry($entry);
$fieldValue->setField($field);

$fielFacetValue = new FieldFacetValue();
$fielFacetValue->setUser($user);
$fielFacetValue->setFieldFacet($fieldFacet);

$formattedValue = $value;

switch ($fieldFacet->getType()) {
case FieldFacet::NUMBER_TYPE:
$formattedValue = floatval($value);
break;
case FieldFacet::CASCADE_TYPE:
case FieldFacet::FILE_TYPE:
$formattedValue = explode(',', $value);
break;
case FieldFacet::BOOLEAN_TYPE:
$formattedValue = empty($value) || 'false' === $value ? false : true;
break;
case FieldFacet::CHOICE_TYPE:
$options = $fieldFacet->getOptions();

if (isset($options['multiple']) && $options['multiple']) {
$formattedValue = explode(',', $value);
}
break;
}
$fielFacetValue->setValue($formattedValue);
$this->om->persist($fielFacetValue);

$fieldValue->setFieldFacetValue($fielFacetValue);
$this->om->persist($fieldValue);
}
}
}
$this->om->persist($entry);
} else {
$this->log("Entry from line {$lineNum} already existed.", LogLevel::ERROR);
}
}
$this->om->endFlushSuite();
}
}
}
1 change: 0 additions & 1 deletion src/plugin/claco-form/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ imports:
- { resource: services/finder.yml }
- { resource: services/serializer.yml }
- { resource: services/controller.yml }
- { resource: services/command.yml }
- { resource: services/listener.yml }
- { resource: services/manager.yml }
- { resource: services/messenger.yml }
Expand Down
10 changes: 0 additions & 10 deletions src/plugin/claco-form/Resources/config/services/command.yml

This file was deleted.

0 comments on commit 459b331

Please sign in to comment.