/**
  * Processing of Upload Photos Form
  *
  * @Privilege('generate')
  *
  * @param Form $form
  */
 public function uploadPhotosFormSucceded(Form $form)
 {
     $values = $form->getValues();
     $event = $this->loadItem($values['id']);
     $calendar = $event->ref('calendar');
     $year = $calendar->yearpart === 'podzim' ? $calendar->year : $calendar->year - 1;
     $date = substr($event->datestart, 0, 4) . substr($event->datestart, 5, 2) . substr($event->datestart, 8, 2);
     // check if the year dir exist. if not, create one
     $params = $this->context->parameters;
     $dirYear = $params['wwwDir'] . $params['chroniclePhotosStorage'] . '/' . $year . ($year + 1) . '/';
     // check if the event dir exists. if not, create one
     $dir = $dirYear . $date . '/';
     if (!file_exists($dir)) {
         mkdir($dir, 0777, TRUE);
     }
     \Tracy\Debugger::barDump($dir);
     // start uploading files
     $counter = 0;
     foreach ($values['upload'] as $file) {
         if ($file->isOK()) {
             if ($file->isImage()) {
                 do {
                     $counter += 1;
                     //increment counter until you hit empty space for file
                     $filePath = $dir . \Nette\Utils\Strings::padLeft($counter, 4, '0') . '.jpg';
                 } while (file_exists($filePath));
                 $image = $file->toImage();
                 $image->save($filePath, 85, \Nette\Image::JPEG);
                 $this->database->table('chronicle_photos')->insert(['event_id' => $values['id'], 'order' => $counter]);
                 $this->flashMessage("Soubor " . $file->getName() . " byl úspěšně nahrán!");
             } else {
                 $form->addError('Soubor' . $file->getName() . ' nebyl rozpoznán jako fotka.');
             }
         } else {
             $form->addError('Nepodařilo se nahrát soubor:' . $file->getName());
         }
     }
     //change event visibility
     if (!array_key_exists('showchronicle', $values)) {
         //user did not have the sufficient permission
         $showchronicle = FALSE;
     } else {
         $showchronicle = $values['showchronicle'] ? TRUE : FALSE;
     }
     $this->events->showChronicle($values['id'], $showchronicle);
     if (!$form->hasErrors()) {
         $this->redirect('default');
     }
 }