Ejemplo n.º 1
0
 public function executePost(sfWebRequest $request)
 {
     $this->forward400If('' === (string) $request['title'], 'title parameter is not specified.');
     $this->forward400If('' === (string) $request['body'], 'body parameter is not specified.');
     $this->forward400If(!isset($request['public_flag']) || '' === (string) $request['public_flag'], 'public flag is not specified');
     if (isset($request['id']) && '' !== $request['id']) {
         $diary = Doctrine::getTable('Diary')->findOneById($request['id']);
         $this->forward400If(false === $diary, 'the specified diary does not exit.');
         $this->forward400If(false === $diary->isAuthor($this->member->getId()), 'this diary is not yours.');
     } else {
         $diary = new Diary();
         $diary->setMemberId($this->member->getId());
     }
     $diary->setTitle($request['title']);
     $diary->setBody($request['body']);
     $diary->setPublicFlag($request['public_flag']);
     $diary->save();
     $this->diary = $diary;
     for ($i = 1; $i <= 3; $i++) {
         $diaryImage = Doctrine::getTable('DiaryImage')->retrieveByDiaryIdAndNumber($diary->getId(), $i);
         $filename = basename($_FILES['diary_photo_' . $i]['name']);
         if (!is_null($filename) && '' !== $filename) {
             try {
                 $validator = new opValidatorImageFile(array('required' => false));
                 $validFile = $validator->clean($_FILES['diary_photo_' . $i]);
             } catch (Exception $e) {
                 $this->forward400($e->getMessage());
             }
             $f = new File();
             $f->setFromValidatedFile($validFile);
             $f->setName(hash('md5', uniqid((string) $i) . $filename));
             if ($stream = fopen($_FILES['diary_photo_' . $i]['tmp_name'], 'r')) {
                 if (!is_null($diaryImage)) {
                     $diaryImage->delete();
                 }
                 $bin = new FileBin();
                 $bin->setBin(stream_get_contents($stream));
                 $f->setFileBin($bin);
                 $f->save();
                 $di = new DiaryImage();
                 $di->setDiaryId($diary->getId());
                 $di->setFileId($f->getId());
                 $di->setNumber($i);
                 $di->save();
                 $diary->updateHasImages();
             } else {
                 $this->forward400(__('Failed to write file to disk.'));
             }
         }
         $deleteCheck = $request['diary_photo_' . $i . '_photo_delete'];
         if ('on' === $deleteCheck && !is_null($diaryImage)) {
             $diaryImage->delete();
         }
     }
 }
Ejemplo n.º 2
0
 public function executePost(sfWebRequest $request)
 {
     $conn = opDoctrineQuery::getMasterConnection();
     $conn->beginTransaction();
     try {
         $params = $this->getRequestedFormParameter($request);
         $diary = $this->getDiaryObject($this->member->id, $request->getParameter('id'));
         $diary->setTitle($params['title']);
         $diary->setBody($params['body']);
         $diary->setPublicFlag($params['public_flag']);
         if ($diary->is_open && $params['public_flag'] !== 4) {
             $diary->is_open = 0;
         }
         $diary->save($conn);
         $oldDiaryImages = $diary->getDiaryImages();
         foreach ($oldDiaryImages as $oldDiaryImage) {
             if ($request['diary_photo_' . $oldDiaryImage->number . '_photo_delete']) {
                 $oldDiaryImage->delete($conn);
                 unset($oldDiaryImages[$oldDiaryImage->number]);
             }
         }
         if ($params['image']) {
             foreach ($params['image'] as $key => $image) {
                 $number = substr($key, -1);
                 if ($oldDiaryImages[$number]) {
                     throw new opDiaryPluginAPIException('invalid deleteCheck');
                 }
                 $diaryImage = new DiaryImage();
                 $diaryImage->setDiaryId($diary->getId());
                 $diaryImage->setFile($image);
                 $diaryImage->setNumber($number);
                 $diaryImage->save($conn);
                 //re-save because file name doesnt have prefix(refs #1643)
                 $diaryImage->getFile()->save($conn);
                 $diary->updateHasImages();
             }
         }
         $conn->commit();
     } catch (opDiaryPluginAPIException $e) {
         $conn->rollback();
         $this->forward400($e->getMessage());
     } catch (Exception $e) {
         $conn->rollback();
         throw $e;
     }
     $this->diary = $diary;
 }