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();
         }
     }
 }
Exemple #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;
 }
 public function executeCreate(opMailRequest $request)
 {
     if (!Doctrine::getTable('SnsConfig')->get('op_diary_plugin_use_email_post', true)) {
         return sfView::NONE;
     }
     $member = $this->getRoute()->getMember();
     if (!$member) {
         return sfView::NONE;
     }
     $mailMessage = $request->getMailMessage();
     $title = $this->getSubject($mailMessage, '(no title)');
     $validator = new opValidatorString(array('rtrim' => true));
     try {
         $body = $validator->clean($mailMessage->getContent());
     } catch (Exception $e) {
         return sfView::ERROR;
     }
     $diary = new Diary();
     $diary->setMember($member);
     $diary->setTitle($title);
     $diary->setBody($body);
     $diary->setPublicFlag($this->getDefaultPublicFlag($member));
     $diaryImages = array();
     if (sfConfig::get('app_diary_is_upload_images', true)) {
         $num = (int) sfConfig::get('app_diary_max_image_file_num', 3);
         $files = $this->getImageFiles($mailMessage, $num);
         $i = 1;
         foreach ($files as $file) {
             $diaryImage = new DiaryImage();
             $diaryImage->setDiary($diary);
             $diaryImage->setFile($file);
             $diaryImage->setNumber($i++);
             $diaryImages[] = $diaryImage;
             $diary->setHasImages(true);
         }
     }
     $diary->save();
     foreach ($diaryImages as $diaryImage) {
         $diaryImage->save();
     }
     return sfView::NONE;
 }
<?php

include dirname(__FILE__) . '/../../bootstrap/unit.php';
include dirname(__FILE__) . '/../../bootstrap/database.php';
$t = new lime_test(4);
$t->diag('DiaryImage::delete()');
$diaryImage = Doctrine::getTable('DiaryImage')->find(1);
$file_id = $diaryImage->file_id;
$conn = Doctrine::getTable('DiaryImage')->getConnection();
$conn->beginTransaction();
$diaryImage->delete();
$t->ok(!Doctrine::getTable('File')->find($file_id), 'File is deleted');
$t->ok(!Doctrine::getTable('FileBin')->find($file_id), 'FileBin is deleted');
$conn->rollback();
$t->diag('DiaryImage::save()');
$conn->beginTransaction();
$file = new File();
$file->name = 'test.gif';
$diaryImage = new DiaryImage();
$diaryImage->diary_id = 1;
$diaryImage->number = 2;
$diaryImage->File = $file;
$diaryImage->save();
$t->is($diaryImage->File->name, 'd_1_2_test.gif', 'File name is prefixed');
$diaryImage->save();
$t->is($diaryImage->File->name, 'd_1_2_test.gif', 'File name is not double prefixed');
$conn->rollback();