Skip to content

Commit

Permalink
Merge pull request #466 from jan-stanek/pole-soubor
Browse files Browse the repository at this point in the history
pole typu soubor
  • Loading branch information
Jan Staněk authored Dec 28, 2017
2 parents 014d38c + b660f27 commit 9e40676
Show file tree
Hide file tree
Showing 16 changed files with 254 additions and 54 deletions.
5 changes: 5 additions & 0 deletions app/AdminModule/ConfigurationModule/forms/CustomInputForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\AdminModule\Forms\BaseForm;
use App\Model\Settings\CustomInput\CustomCheckbox;
use App\Model\Settings\CustomInput\CustomFile;
use App\Model\Settings\CustomInput\CustomInput;
use App\Model\Settings\CustomInput\CustomInputRepository;
use App\Model\Settings\CustomInput\CustomSelect;
Expand Down Expand Up @@ -125,6 +126,10 @@ public function processForm(Form $form, \stdClass $values)
$this->customInput->setOptions(implode(', ', $optionsCleaned));

break;

case CustomInput::FILE:
$this->customInput = new CustomFile();
break;
}
}

Expand Down
7 changes: 7 additions & 0 deletions app/AdminModule/components/UsersGridControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,13 @@ public function createComponentUsersGrid($name)

case CustomInput::SELECT:
return $customInputValue->getValueOption();

case CustomInput::FILE:
return Html::el('a')
->setAttribute('href', $this->getPresenter()->getTemplate()->basePath
. '/files' . $customInputValue->getValue())
->setAttribute('target', '_blank')
->setText($this->translator->translate('admin.users.users_custom_input_file_download'));
}
}
return NULL;
Expand Down
68 changes: 53 additions & 15 deletions app/AdminModule/forms/EditUserSeminarForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@
use App\Model\ACL\Role;
use App\Model\ACL\RoleRepository;
use App\Model\Program\ProgramRepository;
use App\Model\Settings\CustomInput\CustomFile;
use App\Model\Settings\CustomInput\CustomInput;
use App\Model\Settings\CustomInput\CustomInputRepository;
use App\Model\Settings\CustomInput\CustomText;
use App\Model\Settings\SettingsRepository;
use App\Model\User\CustomInputValue\CustomCheckboxValue;
use App\Model\User\CustomInputValue\CustomInputValueRepository;
use App\Model\User\CustomInputValue\CustomSelectValue;
use App\Model\User\CustomInputValue\CustomFileValue;
use App\Model\User\CustomInputValue\CustomTextValue;
use App\Model\User\User;
use App\Model\User\UserRepository;
use App\Services\ApplicationService;
use App\Services\FilesService;
use App\Services\ProgramService;
use App\Utils\Validators;
use Nette;
use Nette\Application\UI\Form;
use PhpCollection\Set;
use phpDocumentor\Reflection\File;


/**
Expand Down Expand Up @@ -57,6 +62,9 @@ class EditUserSeminarForm extends Nette\Object
/** @var Validators */
private $validators;

/** @var FilesService*/
private $filesService;


/**
* EditUserSeminarForm constructor.
Expand All @@ -66,12 +74,14 @@ class EditUserSeminarForm extends Nette\Object
* @param CustomInputValueRepository $customInputValueRepository
* @param RoleRepository $roleRepository
* @param ApplicationService $applicationService
* @param Validators $validators
* @param FilesService $filesService
*/
public function __construct(BaseForm $baseFormFactory, UserRepository $userRepository,
CustomInputRepository $customInputRepository,
CustomInputValueRepository $customInputValueRepository,
RoleRepository $roleRepository, ApplicationService $applicationService,
Validators $validators)
Validators $validators, FilesService $filesService)
{
$this->baseFormFactory = $baseFormFactory;
$this->userRepository = $userRepository;
Expand All @@ -80,6 +90,7 @@ public function __construct(BaseForm $baseFormFactory, UserRepository $userRepos
$this->roleRepository = $roleRepository;
$this->applicationService = $applicationService;
$this->validators = $validators;
$this->filesService = $filesService;
}

/**
Expand Down Expand Up @@ -117,19 +128,26 @@ public function create($id)
switch ($customInput->getType()) {
case CustomInput::TEXT:
$custom = $form->addText('custom' . $customInput->getId(), $customInput->getName());
if ($customInputValue)
$custom->setDefaultValue($customInputValue->getValue());
break;

case CustomInput::CHECKBOX:
$custom = $form->addCheckbox('custom' . $customInput->getId(), $customInput->getName());
if ($customInputValue)
$custom->setDefaultValue($customInputValue->getValue());
break;

case CustomInput::SELECT:
$custom = $form->addSelect('custom' . $customInput->getId(), $customInput->getName(), $customInput->prepareSelectOptions());
if ($customInputValue)
$custom->setDefaultValue($customInputValue->getValue());
break;
}

if ($customInputValue)
$custom->setDefaultValue($customInputValue->getValue());
case CustomInput::FILE:
$custom = $form->addUpload('custom' . $customInput->getId(), $customInput->getName());
break;
}
}

$form->addTextArea('about', 'admin.users.users_about_me');
Expand Down Expand Up @@ -181,23 +199,33 @@ public function processForm(Form $form, \stdClass $values)
foreach ($this->customInputRepository->findAllOrderedByPosition() as $customInput) {
$customInputValue = $this->user->getCustomInputValue($customInput);

if ($customInputValue) {
$customInputValue->setValue($values['custom' . $customInput->getId()]);
continue;
}

switch ($customInput->getType()) {
case CustomInput::TEXT:
$customInputValue = new CustomTextValue();
$customInputValue = $customInputValue ?: new CustomTextValue();
$customInputValue->setValue($values['custom' . $customInput->getId()]);
break;

case CustomInput::CHECKBOX:
$customInputValue = new CustomCheckboxValue();
$customInputValue = $customInputValue ?: new CustomCheckboxValue();
$customInputValue->setValue($values['custom' . $customInput->getId()]);
break;

case CustomInput::SELECT:
$customInputValue = new CustomSelectValue();
$customInputValue = $customInputValue ?: new CustomSelectValue();
$customInputValue->setValue($values['custom' . $customInput->getId()]);
break;

case CustomInput::FILE:
$customInputValue = $customInputValue ?: new CustomFileValue();
$file = $values['custom' . $customInput->getId()];
if ($file->size > 0) {
$path = $this->generatePath($file, $this->user, $customInput);
$this->filesService->save($file, $path);
$customInputValue->setValue($path);
}
break;
}
$customInputValue->setValue($values['custom' . $customInput->getId()]);

$customInputValue->setUser($this->user);
$customInputValue->setInput($customInput);
$this->customInputValueRepository->save($customInputValue);
Expand All @@ -223,7 +251,6 @@ public function processForm(Form $form, \stdClass $values)
* @param $field
* @param $args
* @return bool
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function validateRolesNonregistered($field, $args)
{
Expand All @@ -236,11 +263,22 @@ public function validateRolesNonregistered($field, $args)
* @param $field
* @param $args
* @return bool
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function validateRolesCapacities($field, $args)
{
$selectedRoles = $this->roleRepository->findRolesByIds($field->getValue());
return $this->validators->validateRolesCapacities($selectedRoles, $this->user);
}

/**
* Vygeneruje cestu souboru.
* @param $file
* @param User $user
* @param CustomFile $customInput
* @return string
*/
private function generatePath($file, User $user, CustomFile $customInput): string
{
return CustomFile::PATH . '/' . $user->getId() . '-' . $customInput->getId() . '.' . pathinfo($file->name, PATHINFO_EXTENSION);
}
}
1 change: 1 addition & 0 deletions app/AdminModule/presenters/UsersPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public function renderDetail($id)
$this->template->customInputTypeText = CustomInput::TEXT;
$this->template->customInputTypeCheckbox = CustomInput::CHECKBOX;
$this->template->customInputTypeSelect = CustomInput::SELECT;
$this->template->customInputTypeFile = CustomInput::FILE;

$this->template->roleAdminName = $this->roleRepository->findBySystemName(Role::ADMIN)->getName();
$this->template->roleOrganizerName = $this->roleRepository->findBySystemName(Role::ORGANIZER)->getName();
Expand Down
6 changes: 5 additions & 1 deletion app/AdminModule/presenters/templates/Users/detail.latte
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,12 @@
{_admin.common.no}
{/if}
{case $customInputTypeSelect}
{$customInputValue->getValueOption()}
{case $customInputTypeFile}
{if $customInputValue->getValue() !== NULL}
{$customInputValue->getValueOption()}
<a href="{$basePath}/files{$customInputValue->getValue()}" target="_blank">
{_admin.users.users_custom_input_file_download}
</a>
{/if}
{/switch}
{/if}
Expand Down
64 changes: 50 additions & 14 deletions app/WebModule/forms/AdditionalInformationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

namespace App\WebModule\Forms;

use App\Model\Settings\CustomInput\CustomFile;
use App\Model\Settings\CustomInput\CustomInput;
use App\Model\Settings\CustomInput\CustomInputRepository;
use App\Model\User\CustomInputValue\CustomCheckboxValue;
use App\Model\User\CustomInputValue\CustomInputValueRepository;
use App\Model\User\CustomInputValue\CustomSelectValue;
use App\Model\User\CustomInputValue\CustomFileValue;
use App\Model\User\CustomInputValue\CustomTextValue;
use App\Model\User\User;
use App\Model\User\UserRepository;
use App\Services\ApplicationService;
use App\Services\FilesService;
use Nette;
use Nette\Application\UI\Form;

Expand Down Expand Up @@ -44,6 +47,9 @@ class AdditionalInformationForm extends Nette\Object
/** @var CustomInputValueRepository */
private $customInputValueRepository;

/** @var FilesService */
private $filesService;


/**
* AdditionalInformationForm constructor.
Expand All @@ -55,13 +61,14 @@ class AdditionalInformationForm extends Nette\Object
*/
public function __construct(BaseForm $baseFormFactory, UserRepository $userRepository,
CustomInputRepository $customInputRepository, ApplicationService $applicationService,
CustomInputValueRepository $customInputValueRepository)
CustomInputValueRepository $customInputValueRepository, FilesService $filesService)
{
$this->baseFormFactory = $baseFormFactory;
$this->userRepository = $userRepository;
$this->customInputRepository = $customInputRepository;
$this->applicationService = $applicationService;
$this->customInputValueRepository = $customInputValueRepository;
$this->filesService = $filesService;
}

/**
Expand All @@ -84,25 +91,32 @@ public function create($id)
switch ($customInput->getType()) {
case CustomInput::TEXT:
$custom = $form->addText('custom' . $customInput->getId(), $customInput->getName());
if ($customInputValue)
$custom->setDefaultValue($customInputValue->getValue());
break;

case CustomInput::CHECKBOX:
$custom = $form->addCheckbox('custom' . $customInput->getId(), $customInput->getName());
if ($customInputValue)
$custom->setDefaultValue($customInputValue->getValue());
break;

case CustomInput::SELECT:
$custom = $form->addSelect('custom' . $customInput->getId(), $customInput->getName(), $customInput->prepareSelectOptions());
if ($customInputValue)
$custom->setDefaultValue($customInputValue->getValue());
break;

case CustomInput::FILE:
$custom = $form->addUpload('custom' . $customInput->getId(), $customInput->getName());
break;
}

if ($customInput->isMandatory())
if ($customInput->isMandatory() && $customInput->getType() != CustomInput::FILE)
$custom->addRule(Form::FILLED, 'web.profile.custom_input_empty');

if (!$this->applicationService->isAllowedEditCustomInputs())
$custom->setDisabled();

if ($customInputValue)
$custom->setDefaultValue($customInputValue->getValue());
}

$form->addTextArea('about', 'web.profile.about_me');
Expand Down Expand Up @@ -139,23 +153,33 @@ public function processForm(Form $form, \stdClass $values)
foreach ($this->customInputRepository->findAllOrderedByPosition() as $customInput) {
$customInputValue = $this->user->getCustomInputValue($customInput);

if ($customInputValue) {
$customInputValue->setValue($values['custom' . $customInput->getId()]);
continue;
}

switch ($customInput->getType()) {
case CustomInput::TEXT:
$customInputValue = new CustomTextValue();
$customInputValue = $customInputValue ?: new CustomTextValue();
$customInputValue->setValue($values['custom' . $customInput->getId()]);
break;

case CustomInput::CHECKBOX:
$customInputValue = new CustomCheckboxValue();
$customInputValue = $customInputValue ?: new CustomCheckboxValue();
$customInputValue->setValue($values['custom' . $customInput->getId()]);
break;

case CustomInput::SELECT:
$customInputValue = new CustomSelectValue();
$customInputValue = $customInputValue ?: new CustomSelectValue();
$customInputValue->setValue($values['custom' . $customInput->getId()]);
break;

case CustomInput::FILE:
$customInputValue = $customInputValue ?: new CustomFileValue();
$file = $values['custom' . $customInput->getId()];
if ($file->size > 0) {
$path = $this->generatePath($file, $this->user, $customInput);
$this->filesService->save($file, $path);
$customInputValue->setValue($path);
}
break;
}
$customInputValue->setValue($values['custom' . $customInput->getId()]);

$customInputValue->setUser($this->user);
$customInputValue->setInput($customInput);
$this->customInputValueRepository->save($customInputValue);
Expand All @@ -172,4 +196,16 @@ public function processForm(Form $form, \stdClass $values)
$this->userRepository->save($this->user);
});
}

/**
* Vygeneruje cestu souboru.
* @param $file
* @param User $user
* @param CustomFile $customInput
* @return string
*/
private function generatePath($file, User $user, CustomFile $customInput): string
{
return CustomFile::PATH . '/' . $user->getId() . '-' . $customInput->getId() . '.' . pathinfo($file->name, PATHINFO_EXTENSION);
}
}
Loading

0 comments on commit 9e40676

Please sign in to comment.