/**
  * @before _secured, _admin
  */
 public function edit($id)
 {
     $view = $this->getActionView();
     $dog = App_Model_Dog::fetchDogById((int) $id);
     if ($dog === null) {
         $view->warningMessage(self::ERROR_MESSAGE_2);
         $this->_willRenderActionView = false;
         self::redirect('/admin/dog/');
     }
     $dogExams = $dog->exams;
     $dogExamIds = array();
     if (!empty($dogExams)) {
         foreach ($dogExams as $dogExam) {
             $dogExamIds[] = $dogExam->examId;
         }
     }
     $exams = App_Model_Exam::all(array('active = ?' => true));
     $users = App_Model_User::all(array('role = ?' => 'role_member'), array('id', 'firstname', 'lastname'));
     $view->set('dog', $dog)->set('exams', $exams)->set('dogexamids', $dogExamIds)->set('users', $users);
     if (RequestMethods::post('submitEditDog')) {
         if ($this->checkCSRFToken() !== true) {
             self::redirect('/admin/dog/');
         }
         $errors = array();
         $cfg = Registry::get('configuration');
         $fileManager = new FileManager(array('thumbWidth' => $cfg->thumb_width, 'thumbHeight' => $cfg->thumb_height, 'thumbResizeBy' => $cfg->thumb_resizeby, 'maxImageWidth' => $cfg->photo_maxwidth, 'maxImageHeight' => $cfg->photo_maxheight));
         $imgMain = $imgThumb = '';
         if ($dog->imgMain == '') {
             $photoNameRaw = RequestMethods::post('user') . '-' . RequestMethods::post('dogname');
             $photoName = $this->_createUrlKey($photoNameRaw);
             $fileErrors = $fileManager->uploadBase64Image(RequestMethods::post('croppedimage'), $photoName, 'dog', time() . '_')->getUploadErrors();
             $files = $fileManager->getUploadedFiles();
             if (!empty($fileErrors)) {
                 $errors['croppedimage'] = $fileErrors;
             }
             if (!empty($files)) {
                 foreach ($files as $i => $file) {
                     if ($file instanceof \THCFrame\Filesystem\Image) {
                         $imgMain = trim($file->getFilename(), '.');
                         $imgThumb = trim($file->getThumbname(), '.');
                         break;
                     }
                 }
             }
         } else {
             $imgMain = $dog->imgMain;
             $imgThumb = $dog->imgThumb;
         }
         if ((int) RequestMethods::post('isactive') == 1) {
             App_Model_Dog::updateAll(array('isActive = ?' => true, 'userId = ?' => (int) RequestMethods::post('user')), array('isActive' => 0));
         }
         $dog->userId = RequestMethods::post('user');
         $dog->isActive = RequestMethods::post('isactive', 0);
         $dog->dogName = RequestMethods::post('dogname');
         $dog->race = RequestMethods::post('dograce');
         $dog->dob = RequestMethods::post('dogdob');
         $dog->information = RequestMethods::post('doginfo');
         $dog->active = RequestMethods::post('active');
         $dog->imgMain = $imgMain;
         $dog->imgThumb = $imgThumb;
         if (empty($errors) && $dog->validate()) {
             $dog->save();
             $examsArr = (array) RequestMethods::post('chexam');
             if ($examsArr[0] != '') {
                 $deleteStatus = App_Model_DogExam::deleteAll(array('dogId = ?' => (int) $dog->getId()));
                 if ($deleteStatus != -1) {
                     foreach ($examsArr as $exam) {
                         $de = new App_Model_DogExam(array('dogId' => (int) $dog->getId(), 'examId' => (int) $exam));
                         $de->save();
                         Event::fire('admin.log', array('success', 'Dog id: ' . $dog->getId() . ' has exam ' . $exam));
                     }
                 } else {
                     $errors['exams'] = array('Nastala chyba při ukládání zkoušek');
                 }
             }
             if (RequestMethods::post('uploadmorephotos') == '1') {
                 $fileErrors = $fileManager->newUpload()->uploadImage('secondfile', 'dog', time() . '_')->getUploadErrors();
                 $files = $fileManager->getUploadedFiles();
                 if (!empty($fileErrors)) {
                     $errors['secondfile'] = $fileErrors;
                 }
                 if (!empty($files)) {
                     foreach ($files as $i => $file) {
                         if ($file instanceof \THCFrame\Filesystem\Image) {
                             $info = $file->getOriginalInfo();
                             $photo = new App_Model_Photo(array('galleryId' => 2, 'imgMain' => trim($file->getFilename(), '.'), 'imgThumb' => trim($file->getThumbname(), '.'), 'description' => RequestMethods::post('description'), 'photoName' => pathinfo($file->getFilename(), PATHINFO_FILENAME), 'mime' => $info['mime'], 'format' => $info['format'], 'width' => $file->getWidth(), 'height' => $file->getHeight(), 'size' => $file->getSize()));
                             if ($photo->validate()) {
                                 $photoId = $photo->save();
                                 $dp = new App_Model_DogPhoto(array('dogId' => $dog->getId(), 'photoId' => $photoId));
                                 $dp->save();
                                 Event::fire('admin.log', array('success', 'Photo id: ' . $photoId));
                             } else {
                                 Event::fire('admin.log', array('fail'));
                                 $errors['secondfile'][] = $photo->getErrors();
                             }
                         }
                     }
                 }
                 if (empty($errors)) {
                     Event::fire('admin.log', array('success', 'Dog Id: ' . $id));
                     $view->successMessage(self::SUCCESS_MESSAGE_2);
                     self::redirect('/admin/dog/');
                 } else {
                     Event::fire('admin.log', array('fail'));
                     $view->set('errors', $errors)->set('dog', $dog);
                 }
             } else {
                 Event::fire('admin.log', array('success', 'Dog Id: ' . $id));
                 $view->successMessage(self::SUCCESS_MESSAGE_2);
                 self::redirect('/admin/dog/');
             }
         } else {
             Event::fire('admin.log', array('fail', 'Dog Id: ' . $dog->getId()));
             $view->set('errors', $errors + $dog->getErrors())->set('dog', $dog);
         }
     }
 }
 /**
  * Action method shows and processes form used for uploading photos into
  * collection specified by param id
  * 
  * @before _secured, _admin
  * @param int $id   collection id
  */
 public function addPhoto($id)
 {
     $view = $this->getActionView();
     $gallery = App_Model_Gallery::first(array('id = ?' => (int) $id, 'active = ?' => true), array('id', 'title'));
     if ($gallery === null) {
         $view->warningMessage(self::ERROR_MESSAGE_2);
         self::redirect('/admin/gallery/');
     }
     $view->set('gallery', $gallery)->set('submstoken', $this->mutliSubmissionProtectionToken());
     if (RequestMethods::post('submitAddPhoto')) {
         if ($this->checkCSRFToken() !== true && $this->checkMutliSubmissionProtectionToken(RequestMethods::post('submstoken')) !== true) {
             self::redirect('/admin/gallery/');
         }
         $errors = array();
         $cfg = Registry::get('configuration');
         $fileManager = new FileManager(array('thumbWidth' => $cfg->thumb_width, 'thumbHeight' => $cfg->thumb_height, 'thumbResizeBy' => $cfg->thumb_resizeby, 'maxImageWidth' => $cfg->photo_maxwidth, 'maxImageHeight' => $cfg->photo_maxheight));
         $fileErrors = $fileManager->uploadImage('secondfile', 'gallery/' . $gallery->getId(), time() . '_')->getUploadErrors();
         $files = $fileManager->getUploadedFiles();
         if (!empty($files)) {
             foreach ($files as $i => $file) {
                 if ($file instanceof \THCFrame\Filesystem\Image) {
                     $info = $file->getOriginalInfo();
                     $photo = new App_Model_Photo(array('galleryId' => $gallery->getId(), 'imgMain' => trim($file->getFilename(), '.'), 'imgThumb' => trim($file->getThumbname(), '.'), 'description' => RequestMethods::post('description'), 'photoName' => pathinfo($file->getFilename(), PATHINFO_FILENAME), 'mime' => $info['mime'], 'format' => $info['format'], 'width' => $file->getWidth(), 'height' => $file->getHeight(), 'size' => $file->getSize()));
                     if ($photo->validate()) {
                         $aid = $photo->save();
                         Event::fire('admin.log', array('success', 'Photo id: ' . $aid . ' in gallery ' . $gallery->getId()));
                     } else {
                         Event::fire('admin.log', array('fail', 'Photo in gallery ' . $gallery->getId()));
                         $errors['secondfile'][] = $photo->getErrors();
                     }
                 }
             }
         }
         $errors['secondfile'] = $fileErrors;
         if (empty($errors['secondfile'])) {
             $view->successMessage(self::SUCCESS_MESSAGE_7);
             self::redirect('/admin/gallery/detail/' . $gallery->getId());
         } else {
             $view->set('errors', $errors);
         }
     }
 }