/**
  * Execute the action
  *
  * @return  void
  */
 public function execute()
 {
     // get parameters
     $this->id = $this->getParameter('id', 'int');
     $this->gallery_id = $this->getParameter('gallery_id', 'int');
     // does the item exist
     if ($this->id !== null && BackendSlideshowModel::existsImage($this->id)) {
         // call parent, this will probably add some general CSS/JS or other required files
         parent::execute();
         // delete item
         BackendSlideshowModel::deleteImage($this->id);
         // trigger event
         BackendModel::triggerEvent($this->getModule(), 'after_delete_image', array('id' => $this->id));
         // item was deleted, so redirect
         $this->redirect(BackendModel::createURLForAction('Edit') . '&report=deleted&var=' . '&id=' . $this->gallery_id . '#images');
     } else {
         // something went wrong
         $this->redirect(BackendModel::createURLForAction('Index') . '&error=non-existing');
     }
 }
 /**
  * Validate the form
  *
  * @return  void
  */
 private function validateForm()
 {
     // is the form submitted?
     if ($this->frm->isSubmitted()) {
         // cleanup the submitted fields, ignore fields that were added by hackers
         $this->frm->cleanupFields();
         // validate fields
         $this->frm->getField('title')->isFilled(BL::err('TitleIsRequired'));
         $this->frm->getField('publish_on_date')->isValid(BL::err('DateIsInvalid'));
         $this->frm->getField('publish_on_time')->isValid(BL::err('TimeIsInvalid'));
         if ($this->frm->getField('width')->isFilled(BL::err('WidthIsRequired'))) {
             $this->frm->getField('width')->isNumeric(BL::err('NumericCharactersOnly'));
         }
         if ($this->frm->getField('height')->isFilled()) {
             $this->frm->getField('height')->isNumeric(BL::err('NumericCharactersOnly'));
         }
         if ($this->frm->getField('filename')->isFilled()) {
             // correct extension?
             if ($this->frm->getField('filename')->isAllowedExtension(array('jpg', 'jpeg', 'gif', 'png'), BL::err('JPGGIFAndPNGOnly'))) {
                 // correct mimetype?
                 $this->frm->getField('filename')->isAllowedMimeType(array('image/gif', 'image/jpg', 'image/jpeg', 'image/png'), BL::err('JPGGIFAndPNGOnly'));
             }
         }
         $this->frm->getField('categories')->isFilled(BL::err('CategoryIsRequired'));
         $this->meta->validate();
         // no errors?
         if ($this->frm->isCorrect()) {
             //build settings item
             $settings['animation'] = $this->frm->getField('animation')->getValue();
             $settings['direction'] = $this->frm->getField('direction')->getValue();
             $settings['slideshow_speed'] = $this->frm->getField('slideshow_speed')->getValue();
             $settings['animation_speed'] = $this->frm->getField('animation_speed')->getValue();
             $settings['direction_navigation'] = $this->frm->getField('direction_navigation')->getChecked() ? 'true' : 'false';
             $settings['control_navigation'] = $this->frm->getField('control_navigation')->getChecked() ? 'true' : 'false';
             $settings['thumbnail_navigation'] = $this->frm->getField('thumbnail_navigation')->getChecked() ? 'true' : '';
             $settings['keyboard'] = $this->frm->getField('keyboard')->getChecked() ? 'true' : 'false';
             $settings['mousewheel'] = $this->frm->getField('mousewheel')->getChecked() ? 'true' : 'false';
             $settings['touch'] = $this->frm->getField('touch')->getChecked() ? 'true' : 'false';
             $settings['randomize'] = $this->frm->getField('randomize')->getChecked() ? 'true' : 'false';
             $settings['auto_animate'] = $this->frm->getField('auto_animate')->getChecked() ? 'true' : 'false';
             $settings['animation_loop'] = $this->frm->getField('animation_loop')->getChecked() ? 'true' : 'false';
             // update settings
             BackendSlideshowModel::setSettings($this->id, $settings);
             // build item
             $item['id'] = $this->id;
             $item['meta_id'] = $this->meta->save(true);
             $item['language'] = $this->record['language'];
             $item['category_id'] = $this->frm->getField('categories')->getValue();
             $item['title'] = $this->frm->getField('title')->getValue();
             $item['description'] = $this->frm->getField('description')->getValue(true);
             $item['width'] = $this->frm->getField('width')->getValue();
             $item['height'] = $this->frm->getField('height')->getValue();
             // set height to null if empty
             if (empty($item['height'])) {
                 $item['height'] = null;
             }
             $item['publish_on'] = BackendModel::getUTCDate(null, BackendModel::getUTCTimestamp($this->frm->getField('publish_on_date'), $this->frm->getField('publish_on_time')));
             $item['edited_on'] = BackendModel::getUTCDate();
             $item['hidden'] = $this->frm->getField('hidden')->getValue();
             // check if the category was changed
             if (!BackendSlideshowModel::getChangeCategory($this->id, $item['category_id'])) {
                 // if so, adjust the sequence to the new category
                 $item['sequence'] = BackendSlideshowModel::getMaximumGallerySequence($this->frm->getField('categories')->getValue()) + 1;
             }
             // if the image should be deleted
             if ($this->frm->getField('delete_image')->isChecked()) {
                 $fs = new Filesystem();
                 // delete the image
                 $fs->remove(FRONTEND_FILES_PATH . '/slideshow/' . $this->record['filename']);
                 //delete thumbnail
                 $fs->remove(FRONTEND_FILES_PATH . '/slideshow/thumbnails/' . $this->record['filename']);
                 // reset the name
                 $item['filename'] = null;
             }
             if ($this->frm->getField('filename')->isFilled()) {
                 // only delete the picture when there is one allready
                 if (!empty($this->record['filename'])) {
                     $fs = new Filesystem();
                     // delete the image
                     $fs->remove(FRONTEND_FILES_PATH . '/slideshow/' . $this->record['filename']);
                     //delete thumbnail
                     $fs->remove(FRONTEND_FILES_PATH . '/slideshow/thumbnails/' . $this->record['filename']);
                 }
                 // create new filename
                 $filename = $this->meta->getURL();
                 $filename .= '-' . uniqid();
                 $filename .= '-' . BL::getWorkingLanguage();
                 $filename .= '.' . $this->frm->getField('filename')->getExtension();
                 // add filename to item
                 $item['filename'] = $filename;
                 // create thumbnail
                 $this->frm->getField('filename')->createThumbnail(FRONTEND_FILES_PATH . '/slideshow/thumbnails/' . $filename, 100, 100, false, false, 100);
                 // @todo fix this
                 $this->frm->getField('filename')->moveFile(FRONTEND_FILES_PATH . '/slideshow/' . $filename);
             }
             // update gallery values in database
             BackendSlideshowModel::updateGallery($item);
             // trigger event
             BackendModel::triggerEvent($this->getModule(), 'after_edit', array('item' => $item));
             // get the gallery data
             $item = BackendSlideshowModel::getGallery($this->id);
             // trace the action and get the ids
             $action = $this->frm->getField('action')->getValue();
             $ids = (array) $_POST['id'];
             // Mass image delete action
             if ($action == 'delete') {
                 foreach ($ids as $id) {
                     // double check if the image exists
                     if ($id !== null && BackendSlideshowModel::existsImage($id)) {
                         // get item
                         $this->record = BackendSlideshowModel::getImage($id);
                         $fs = new Filesystem();
                         // delete the image and thumbnail
                         $fs->remove(FRONTEND_FILES_PATH . '/slideshow/thumbnails/' . $this->record['filename']);
                         $fs->remove(FRONTEND_FILES_PATH . '/slideshow/' . $this->record['filename']);
                         // delete item
                         BackendSlideshowModel::deleteImage($this->record['id']);
                     }
                 }
                 // redirect to edit, tab "images
                 $this->redirect(BackendModel::createURLForAction('Edit') . '&report=deleted&id=' . $this->id . '#images');
             } elseif ($action == 'publish') {
                 // set new status
                 BackendSlideshowModel::updatePublishedImage($ids);
                 // redirect to edit, tab #images
                 $this->redirect(BackendModel::createURLForAction('Edit') . '&report=saved&id=' . $this->id . '#images');
             } elseif ($action == 'hide') {
                 // set new status
                 BackendSlideshowModel::updateHiddenImage($ids);
                 // redirect to edit, tab #images
                 $this->redirect(BackendModel::createURLForAction('Edit') . '&report=saved&id=' . $this->id . '#images');
             }
             // everything is saved, so redirect to the overview
             $this->redirect(BackendModel::createURLForAction('Index') . '&report=saved&var=' . urlencode($item['title']) . '&highlight=' . $item['id']);
         }
     }
 }