Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types = 1);
- namespace App\WwwModule\Component;
- use Nepttune\Component\BaseFormComponent;
- use Nette;
- use Nette\Application\UI\Form;
- class ServiceForm extends \Nepttune\Component\BaseFormComponent
- {
- use \Nepttune\Model\TFileSaver;
- protected const TEMPLATE_PATH = false;
- /** @var \App\Table\CategoryTable */
- protected $categoryTable;
- /** @var \App\Table\CategoryItemTable */
- protected $categoryItemTable;
- /** @var */
- protected $form;
- /** @var \Nette\Security\User */
- protected $user;
- /** @var \App\Table\PhotoTable */
- protected $photoTable;
- /** @var \App\Table\ServiceTable */
- protected $serviceTable;
- /** @var \App\Table\ScheduleTable */
- protected $scheduleTable;
- /** @var \App\Table\ServiceCategoryItemTable */
- protected $serviceCategoryItemTable;
- public function __construct(
- \App\Table\ServiceTable $serviceTable,
- \App\Table\CategoryTable $categoryTable,
- \App\Table\CategoryItemTable $categoryItemTable,
- \App\Table\PhotoTable $photoTable,
- \App\Table\ScheduleTable $scheduleTable,
- \App\Table\ServiceCategoryItemTable $serviceCategoryItemTable,
- \Nette\Security\User $user
- )
- {
- parent::__construct($serviceTable);
- $this->categoryTable = $categoryTable;
- $this->categoryItemTable = $categoryItemTable;
- $this->user = $user;
- $this->serviceTable = $serviceTable;
- $this->photoTable = $photoTable;
- $this->scheduleTable = $scheduleTable;
- $this->serviceCategoryItemTable = $serviceCategoryItemTable;
- }
- protected function modifyForm(Form $form) : Form
- {
- $form->addSelect('category_id', 'Kategorie', $this->categoryTable->findActive()->fetchPairs('id', 'name'))
- ->setAttribute('onChange', 'categoryChanged()')
- ->setRequired();
- $form->addText('name', 'Jméno')
- ->setRequired();
- $form->addText('description', 'Popis')
- ->setRequired();
- $form->addText('address', 'Adresa')
- ->setRequired();
- $form->addText('manager_name', 'Kontaktní osoba')
- ->setRequired();
- $form->addText('phone', 'Telefon')
- ->setRequired();
- $form->addText('email', 'Email')
- ->setRequired();
- $form->addText('web', 'Web')
- ->setRequired();
- $form->addText('facebook', 'Facebook');
- $form->addText('twitter', 'Twitter');
- $form->addText('instagram', 'Instagram');
- foreach(['monday' => 'Pondělí', 'tuesday' => 'Úterý', 'wednesday' => 'Středa', 'thursday' => 'Čtvrtek', 'friday' => 'Pátek', 'saturday' => 'Sobota', 'sunday' => 'Neděle'] as $day => $name) {
- $form->addRadioList($day, $name, [1 => '', 2 => 'Nonstop', 3 => 'Zavřeno']);
- $form->addText($day . '_from', 'Od');
- $form->addText($day . '_to', 'Do');
- }
- foreach($this->categoryItemTable->findActive()->fetchAll() as $input) {
- switch($input->type) {
- case 'checkbox':
- $form->addRadioList('category_item_' . $input->id, $input->name, [0 => 'Ne', 1 => 'Ano']);
- break;
- case 'text':
- $form->addText('category_item_' . $input->id, $input->name);
- break;
- }
- }
- $form->addUpload('main_photo', 'Hlavní fotka')
- ->setRequired();
- $form->addMultiUpload('other_photo', 'Ostatní fotky');
- $form->addSubmit('submit', 'Vytvořit službu');
- return $form;
- }
- public function formSuccess(\Nette\Application\UI\Form $form, \stdClass $values) : void
- {
- $serviceId = $this->serviceTable->upsert($this->rowId, [
- 'name' => $values->name,
- 'description' => $values->description,
- 'address' => $values->address,
- 'manager_name' => $values->manager_name,
- 'phone' => $values->phone,
- 'email' => $values->email,
- 'web' => $values->web,
- 'facebook' => $values->facebook,
- 'twitter' => $values->twitter,
- 'instagram' => $values->instagram,
- 'owner_id' => $this->user->getId(),
- 'category_id' => $values->category_id
- ]);
- for($i = 1; $i <= 7; $i++)
- {
- $this->scheduleTable->upsert($this->rowId, [
- 'service_id' => $serviceId,
- 'day' => $i,
- 'open' => $values->$i . '_from',
- 'close' => $values->$i . '_to',
- 'nonstop' => $values->$i == 2 ? 1 : 0
- ]);
- }
- $name = $values->main_photo->getSanitizedName();
- $size = $values->main_photo->getImageSize();
- $imagePath = $this->saveImage($values->main_photo);
- $this->photoTable->upsert($this->rowId, [
- 'path' => $imagePath,
- 'name' => $name,
- 'width' => $size[0],
- 'height' => $size[1],
- 'type' => $size['mime'],
- 'service_id' => $serviceId
- ]);
- if($values->other_photo instanceof \Nette\Http\FileUpload && $values->other_photo->isOk()) {
- foreach($values->other_photo as $photoNumber => $photo)
- {
- $name = $photo->getSanitizedName();
- $size = $photo->getImageSize();
- $imagePath = $this->saveImage($photo);
- $this->photoTable->upsert($this->rowId, [
- 'path' => $imagePath,
- 'name' => $name,
- 'width' => $size[0],
- 'height' => $size[1],
- 'type' => $size['mime'],
- 'service_id' => $serviceId
- ]);
- }
- }
- for($i = 0; $i <= \count($this->categoryItemTable->findActive()->fetchAll()); $i++)
- {
- if($values->category_item_ . $i) {
- $this->serviceCategoryItemTable->upsert($this->rowId, [
- 'service_id' => $serviceId,
- 'category_item_id' => $i,
- 'value' => $values->category_item . $i
- ]);
- }
- }
- }
- public function beforeRender() : void
- {
- $this->template->categories = $this->categoryTable->findActive();
- $this->template->categoryItemTable = $this->categoryItemTable;
- }
- public function handleCategoryItems() : void
- {
- $this->template->selectedOption = (int) $this->getPresenter()->getParameter('selectedOption');
- $this->getPresenter()->redrawControl('categoryItems');
- $this->redrawControl('categoryItems');
- $this->getPresenter()->redrawControl('serviceFormArea');
- $this->redrawControl('serviceFormArea');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement