/** * @before _secured, _admin */ public function delete($id) { $this->willRenderActionView = false; $this->willRenderLayoutView = false; $exam = App_Model_Exam::first(array('id = ?' => (int) $id), array('id')); if (NULL === $exam) { echo self::ERROR_MESSAGE_2; } else { if ($exam->delete()) { Event::fire('admin.log', array('success', 'Exam Id: ' . $id)); echo 'success'; } else { Event::fire('admin.log', array('fail', 'Exam Id: ' . $id)); echo self::ERROR_MESSAGE_1; } } }
/** * * @return \App_Model_Dog */ public function getDogById() { $query = App_Model_Exam::getQuery(array('ex.*'))->join('tb_dogexam', 'ex.id = de.examId', 'de', array('de.dogId', 'de.examId'))->where('de.dogId = ?', (int) $this->getId()); $this->_exams = App_Model_Exam::initialize($query); $query2 = App_Model_Photo::getQuery(array('ph.*'))->join('tb_dogphoto', 'ph.id = dp.photoId', 'dp', array('dp.dogId', 'dp.photoId'))->where('dp.dogId = ?', (int) $this->getId()); $this->_adPhoto = App_Model_Photo::initialize($query2); return $this; }
/** * @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); } } }