Skip to content

Commit

Permalink
Slideshow + náhledy souborů (#791)
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-stanek authored Feb 7, 2021
1 parent aeeafb0 commit 75ee9be
Show file tree
Hide file tree
Showing 103 changed files with 1,248 additions and 904 deletions.
29 changes: 15 additions & 14 deletions app/AdminModule/CmsModule/Components/DocumentTagsGridControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Ublaboo\DataGrid\Exception\DataGridException;

use function array_keys;
use function assert;
use function count;

/**
Expand Down Expand Up @@ -71,11 +72,11 @@ public function createComponentDocumentTagsGrid(string $name): void
$grid->setDefaultSort(['name' => 'ASC']);
$grid->setPagination(false);

$grid->addColumnText('name', 'admin.cms.tags_name');
$grid->addColumnText('name', 'admin.cms.documents.tags.column.name');

$grid->addColumnText('roles', 'admin.cms.tags_roles', 'rolesText')
$grid->addColumnText('roles', 'admin.cms.documents.tags.column.roles', 'rolesText')
->setRendererOnCondition(function () {
return $this->translator->translate('admin.cms.tags_roles_all');
return $this->translator->translate('admin.cms.documents.tags.column.roles_all');
}, function (Tag $tag) {
return count($this->roleRepository->findAll()) === $tag->getRoles()->count();
});
Expand All @@ -84,24 +85,24 @@ public function createComponentDocumentTagsGrid(string $name): void

$grid->addInlineAdd()->setPositionTop()->onControlAdd[] = function (Container $container) use ($rolesOptions): void {
$container->addText('name', '')
->addRule(Form::FILLED, 'admin.cms.tags_name_empty')
->addRule(Form::IS_NOT_IN, 'admin.cms.tags_name_exists', $this->tagRepository->findAllNames());
->addRule(Form::FILLED, 'admin.cms.documents.tags.column.name_empty')
->addRule(Form::IS_NOT_IN, 'admin.cms.documents.tags.column.name_exists', $this->tagRepository->findAllNames());
$container->addMultiSelect('roles', '', $rolesOptions)->setHtmlAttribute('class', 'datagrid-multiselect')
->setDefaultValue(array_keys($rolesOptions))
->addRule(Form::FILLED, 'admin.cms.tags_roles_empty');
->addRule(Form::FILLED, 'admin.cms.documents.tags.column.roles_empty');
};
$grid->getInlineAdd()->onSubmit[] = [$this, 'add'];

$grid->addInlineEdit()->onControlAdd[] = static function (Container $container) use ($rolesOptions): void {
$container->addText('name', '')
->addRule(Form::FILLED, 'admin.cms.tags_name_empty');
->addRule(Form::FILLED, 'admin.cms.documents.tags.column.name_empty');
$container->addMultiSelect('roles', '', $rolesOptions)->setHtmlAttribute('class', 'datagrid-multiselect')
->addRule(Form::FILLED, 'admin.cms.tags_roles_empty');
->addRule(Form::FILLED, 'admin.cms.documents.tags.column.roles_empty');
};
$grid->getInlineEdit()->onSetDefaults[] = function (Container $container, Tag $item): void {
/** @var TextInput $nameText */
$nameText = $container['name'];
$nameText->addRule(Form::IS_NOT_IN, 'admin.cms.tags_name_exists', $this->tagRepository->findOthersNames($item->getId()));
assert($nameText instanceof TextInput);
$nameText->addRule(Form::IS_NOT_IN, 'admin.cms.documents.tags.column.name_exists', $this->tagRepository->findOthersNames($item->getId()));

$container->setDefaults([
'name' => $item->getName(),
Expand All @@ -116,7 +117,7 @@ public function createComponentDocumentTagsGrid(string $name): void
->setClass('btn btn-xs btn-danger')
->addAttributes([
'data-toggle' => 'confirmation',
'data-content' => $this->translator->translate('admin.cms.tags_delete_confirm'),
'data-content' => $this->translator->translate('admin.cms.documents.tags.action.delete_confirm'),
]);
}

Expand All @@ -135,7 +136,7 @@ public function add(stdClass $values): void

$this->tagRepository->save($tag);

$this->getPresenter()->flashMessage('admin.cms.tags_saved', 'success');
$this->getPresenter()->flashMessage('admin.cms.documents.tags.message.save_success', 'success');

$this->redirect('this');
}
Expand All @@ -155,7 +156,7 @@ public function edit(string $id, stdClass $values): void

$this->tagRepository->save($tag);

$this->getPresenter()->flashMessage('admin.cms.tags_saved', 'success');
$this->getPresenter()->flashMessage('admin.cms.documents.tags.message.save_success', 'success');

$this->redirect('this');
}
Expand All @@ -171,7 +172,7 @@ public function handleDelete(int $id): void
$tag = $this->tagRepository->findById($id);
$this->tagRepository->remove($tag);

$this->getPresenter()->flashMessage('admin.cms.tags_deleted', 'success');
$this->getPresenter()->flashMessage('admin.cms.documents.tags.message.delete_success', 'success');

$this->redirect('this');
}
Expand Down
60 changes: 24 additions & 36 deletions app/AdminModule/CmsModule/Components/DocumentsGridControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
use Nette\Http\FileUpload;
use Nette\Localization\ITranslator;
use Nette\Utils\Html;
use Nette\Utils\Random;
use Nette\Utils\Strings;
use stdClass;
use Ublaboo\DataGrid\DataGrid;
use Ublaboo\DataGrid\Exception\DataGridException;

use function assert;
use function basename;

use const UPLOAD_ERR_OK;

/**
Expand Down Expand Up @@ -75,9 +76,9 @@ public function createComponentDocumentsGrid(string $name): void
$grid->setDefaultSort(['name' => 'ASC']);
$grid->setPagination(false);

$grid->addColumnText('name', 'admin.cms.documents_name');
$grid->addColumnText('name', 'admin.cms.documents.column.name');

$grid->addColumnText('tags', 'admin.cms.documents_tags')
$grid->addColumnText('tags', 'admin.cms.documents.column.tags')
->setRenderer(static function (Document $row) {
$tags = Html::el();
foreach ($row->getTags() as $tag) {
Expand All @@ -90,45 +91,43 @@ public function createComponentDocumentsGrid(string $name): void
return $tags;
});

$grid->addColumnText('file', 'admin.cms.documents_file')
->setRenderer(function (Document $row) {
$grid->addColumnText('file', 'admin.cms.documents.column.file')
->setRenderer(static function (Document $document) {
return Html::el('a')
->setAttribute('href', $this->getPresenter()->getTemplate()->basePath
. '/files' . $row->getFile())
->setAttribute('href', $document->getFile())
->setAttribute('target', '_blank')
->setAttribute('class', 'btn btn-xs btn-secondary')
->addHtml(
Html::el('span')->setAttribute('class', 'fa fa-download')
);
->addHtml(Html::el('span')->setAttribute('class', 'fa fa-download'))
->addText(' ' . basename($document->getFile()));
});

$grid->addColumnText('description', 'admin.cms.documents_description');
$grid->addColumnText('description', 'admin.cms.documents.column.description');

$grid->addColumnDateTime('timestamp', 'admin.cms.documents_timestamp')
$grid->addColumnDateTime('timestamp', 'admin.cms.documents.column.timestamp')
->setFormat(Helpers::DATETIME_FORMAT);

$tagsOptions = $this->tagRepository->getTagsOptions();

$grid->addInlineAdd()->setPositionTop()->onControlAdd[] = static function (Container $container) use ($tagsOptions): void {
$container->addText('name', '')
->addRule(Form::FILLED, 'admin.cms.documents_name_empty');
->addRule(Form::FILLED, 'admin.cms.documents.column.name_empty');

$container->addMultiSelect('tags', '', $tagsOptions)->setHtmlAttribute('class', 'datagrid-multiselect')
->addRule(Form::FILLED, 'admin.cms.documents_tags_empty');
->addRule(Form::FILLED, 'admin.cms.documents.column.tags_empty');

$container->addUpload('file', '')->setHtmlAttribute('class', 'datagrid-upload')
->addRule(Form::FILLED, 'admin.cms.documents_file_empty');
->addRule(Form::FILLED, 'admin.cms.documents.column.file_empty');

$container->addText('description', '');
};
$grid->getInlineAdd()->onSubmit[] = [$this, 'add'];

$grid->addInlineEdit()->onControlAdd[] = static function (Container $container) use ($tagsOptions): void {
$container->addText('name', '')
->addRule(Form::FILLED, 'admin.cms.documents_name_empty');
->addRule(Form::FILLED, 'admin.cms.documents.column.name_empty');

$container->addMultiSelect('tags', '', $tagsOptions)->setHtmlAttribute('class', 'datagrid-multiselect')
->addRule(Form::FILLED, 'admin.cms.documents_tags_empty');
->addRule(Form::FILLED, 'admin.cms.documents.column.tags_empty');

$container->addUpload('file', '')->setHtmlAttribute('class', 'datagrid-upload');

Expand All @@ -149,7 +148,7 @@ public function createComponentDocumentsGrid(string $name): void
->setClass('btn btn-xs btn-danger')
->addAttributes([
'data-toggle' => 'confirmation',
'data-content' => $this->translator->translate('admin.cms.documents_delete_confirm'),
'data-content' => $this->translator->translate('admin.cms.documents.action.delete_confirm'),
]);
}

Expand All @@ -162,8 +161,7 @@ public function createComponentDocumentsGrid(string $name): void
public function add(stdClass $values): void
{
$file = $values->file;
$path = $this->generatePath($file);
$this->filesService->save($file, $path);
$path = $this->filesService->save($file, Document::PATH, true, $file->name);

$document = new Document();

Expand All @@ -175,7 +173,7 @@ public function add(stdClass $values): void

$this->documentRepository->save($document);

$this->getPresenter()->flashMessage('admin.cms.documents_saved', 'success');
$this->getPresenter()->flashMessage('admin.cms.documents.message.save_success', 'success');

$this->redirect('this');
}
Expand All @@ -190,13 +188,11 @@ public function edit(string $id, stdClass $values): void
{
$document = $this->documentRepository->findById((int) $id);

/** @var FileUpload $file */
$file = $values->file;
assert($file instanceof FileUpload);
if ($file->getError() == UPLOAD_ERR_OK) {
$this->filesService->delete($this->documentRepository->findById((int) $id)->getFile());
$path = $this->generatePath($file);
$this->filesService->save($file, $path);

$path = $this->filesService->save($file, Document::PATH, true, $file->name);
$document->setFile($path);
$document->setTimestamp(new DateTimeImmutable());
}
Expand All @@ -207,7 +203,7 @@ public function edit(string $id, stdClass $values): void

$this->documentRepository->save($document);

$this->getPresenter()->flashMessage('admin.cms.documents_saved', 'success');
$this->getPresenter()->flashMessage('admin.cms.documents.message.save_success', 'success');

$this->redirect('this');
}
Expand All @@ -224,16 +220,8 @@ public function handleDelete(int $id): void
$this->filesService->delete($document->getFile());
$this->documentRepository->remove($document);

$this->getPresenter()->flashMessage('admin.cms.documents_deleted', 'success');
$this->getPresenter()->flashMessage('admin.cms.documents.message.delete_success', 'success');

$this->redirect('this');
}

/**
* Vygeneruje cestu dokumentu.
*/
private function generatePath(FileUpload $file): string
{
return Document::PATH . '/' . Random::generate(5) . '/' . Strings::webalize($file->name, '.');
}
}
30 changes: 15 additions & 15 deletions app/AdminModule/CmsModule/Components/FaqGridControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function render(): void
* @throws DataGridColumnStatusException
* @throws DataGridException
*/
public function createComponentFaqGrid(string $name): void
public function createComponentFaqGrid(string $name): DataGrid
{
$grid = new DataGrid($this, $name);
$grid->setTranslator($this->translator);
Expand All @@ -55,20 +55,20 @@ public function createComponentFaqGrid(string $name): void
$grid->setDataSource($this->faqRepository->createQueryBuilder('f')->orderBy('f.position'));
$grid->setPagination(false);

$grid->addColumnText('question', 'admin.cms.faq_question');
$grid->addColumnText('question', 'admin.cms.faq.common.question');

$grid->addColumnText('author', 'admin.cms.faq_author', 'author.displayName');
$grid->addColumnText('author', 'admin.cms.faq.column.author', 'author.displayName');

$grid->addColumnStatus('public', 'admin.cms.faq_public')
->addOption(false, 'admin.cms.faq_public_private')
$grid->addColumnStatus('public', 'admin.cms.faq.column.public')
->addOption(false, 'admin.cms.faq.column.public_private')
->setClass('btn-danger')
->endOption()
->addOption(true, 'admin.cms.faq_public_public')
->addOption(true, 'admin.cms.faq.column.public_public')
->setClass('btn-success')
->endOption()
->onChange[] = [$this, 'changeStatus'];

$grid->addColumnText('answered', 'admin.cms.faq_answered')
$grid->addColumnText('answered', 'admin.cms.faq.column.answered')
->setReplacement([
false => $this->translator->translate('admin.common.no'),
true => $this->translator->translate('admin.common.yes'),
Expand All @@ -86,8 +86,10 @@ public function createComponentFaqGrid(string $name): void
->setClass('btn btn-xs btn-danger')
->addAttributes([
'data-toggle' => 'confirmation',
'data-content' => $this->translator->translate('admin.cms.faq_delete_confirm'),
'data-content' => $this->translator->translate('admin.cms.faq.action.delete_confirm'),
]);

return $grid;
}

/**
Expand All @@ -101,7 +103,7 @@ public function handleDelete(int $id): void
$faq = $this->faqRepository->findById($id);
$this->faqRepository->remove($faq);

$this->getPresenter()->flashMessage('admin.cms.faq_deleted', 'success');
$this->getPresenter()->flashMessage('admin.cms.faq.message.delete_success', 'success');

$this->redirect('this');
}
Expand All @@ -117,12 +119,11 @@ public function handleSort(?string $item_id, ?string $prev_id, ?string $next_id)
$this->faqRepository->sort((int) $item_id, (int) $prev_id, (int) $next_id);

$p = $this->getPresenter();
$p->flashMessage('admin.cms.faq_order_saved', 'success');
$p->flashMessage('admin.cms.faq.message.order_save_success', 'success');

if ($p->isAjax()) {
$p->redrawControl('flashes');
/** @var DataGrid $faqGrid */
$faqGrid = $this['faqGrid'];
$faqGrid = $this->getComponent('faqGrid');
$faqGrid->reload();
} else {
$this->redirect('this');
Expand All @@ -143,12 +144,11 @@ public function changeStatus(string $id, string $public): void
$this->faqRepository->save($faq);

$p = $this->getPresenter();
$p->flashMessage('admin.cms.faq_changed_public', 'success');
$p->flashMessage('admin.cms.faq.message.public_change_success', 'success');

if ($p->isAjax()) {
$p->redrawControl('flashes');
/** @var DataGrid $faqGrid */
$faqGrid = $this['faqGrid'];
$faqGrid = $this->getComponent('faqGrid');
$faqGrid->redrawItem($id);
} else {
$this->redirect('this');
Expand Down
Loading

0 comments on commit 75ee9be

Please sign in to comment.