Example #1
0
 /**
  * @before _secured, _admin
  */
 public function add()
 {
     $view = $this->getActionView();
     $users = App_Model_User::all(array('role = ?' => 'role_member'), array('id', 'firstname', 'lastname'));
     $exams = App_Model_Exam::all(array('active = ?' => true));
     $view->set('submstoken', $this->mutliSubmissionProtectionToken())->set('exams', $exams)->set('users', $users);
     if (RequestMethods::post('submitAddDog')) {
         if ($this->checkCSRFToken() !== true && $this->checkMutliSubmissionProtectionToken(RequestMethods::post('submstoken')) !== 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));
         $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 ((int) RequestMethods::post('isactive') == 1) {
             App_Model_Dog::updateAll(array('isActive = ?' => true, 'userId = ?' => (int) RequestMethods::post('user')), array('isActive' => 0));
         }
         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 = '';
             $imgThumb = '';
         }
         $dog = new App_Model_Dog(array('userId' => RequestMethods::post('user'), 'isActive' => RequestMethods::post('isactive', 0), 'dogName' => RequestMethods::post('dogname'), 'race' => RequestMethods::post('dograce'), 'dob' => RequestMethods::post('dogdob'), 'information' => RequestMethods::post('doginfo'), 'imgMain' => $imgMain, 'imgThumb' => $imgThumb));
         if (empty($errors) && $dog->validate()) {
             $dogId = $dog->save();
             $examsArr = (array) RequestMethods::post('chexam');
             if ($examsArr[0] != '') {
                 foreach ($examsArr as $exam) {
                     $de = new App_Model_DogExam(array('dogId' => (int) $dogId, 'examId' => (int) $exam));
                     $de->save();
                     Event::fire('admin.log', array('success', 'Dog id: ' . $dogId . ' has exam ' . $exam));
                 }
             }
             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' => $dogId, '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: ' . $dogId));
                     $view->successMessage('Pes' . self::SUCCESS_MESSAGE_1);
                     self::redirect('/admin/dog/');
                 } else {
                     Event::fire('admin.log', array('fail'));
                     $view->set('errors', $errors)->set('submstoken', $this->revalidateMutliSubmissionProtectionToken())->set('dog', $dog);
                 }
             } else {
                 Event::fire('admin.log', array('success', 'Dog Id: ' . $dogId));
                 $view->successMessage('Pes' . self::SUCCESS_MESSAGE_1);
                 self::redirect('/admin/dog/');
             }
         } else {
             Event::fire('admin.log', array('fail'));
             $view->set('errors', $errors + $dog->getErrors())->set('submstoken', $this->revalidateMutliSubmissionProtectionToken())->set('dog', $dog);
         }
     }
 }