Пример #1
0
 /**
  * Create timeline
  * 
  * @access  public
  * @param   int     $parent_id  target parent id
  * @return  Response(json)
  * @throws  Exception in Controller_Base::controller_common_api
  * @see  Controller_Base::controller_common_api
  */
 public function post_create()
 {
     $this->controller_common_api(function () {
         $this->response_body['errors']['message_default'] = term('timeline') . 'の' . term('form.post') . 'に失敗しました。';
         $moved_files = array();
         $album_image_ids = array();
         $timeline = Model_Timeline::forge();
         $val = \Validation::forge();
         $val->add_model($timeline);
         if (!$val->run()) {
             throw new \ValidationFailedException($val->show_errors());
         }
         $post = $val->validated();
         $file_tmps = \Site_FileTmp::get_file_tmps_and_check_filesize($this->u->id, $this->u->filesize_total);
         if (!strlen($post['body']) && !$file_tmps) {
             throw new \ValidationFailedException('Data is empty.');
         }
         $type_key = 'normal';
         $album_id = (int) \Input::post('album_id', 0);
         if ($file_tmps && $album_id) {
             $album = \Album\Model_Album::check_authority($album_id, $this->u->id);
             if (\Album\Site_Util::check_album_disabled_to_update($album->foreign_table, true)) {
                 throw new \ValidationFailedException('Album id is invalid.');
             }
             $type_key = 'album_image';
         }
         try {
             \DB::start_transaction();
             if ($file_tmps) {
                 if (!$album_id) {
                     $type_key = 'album_image_timeline';
                     $album_id = \Album\Model_Album::get_id_for_foreign_table($this->u->id, 'timeline');
                 }
                 list($moved_files, $album_image_ids) = \Site_FileTmp::save_images($file_tmps, $album_id, 'album_id', 'album_image', $post['public_flag']);
             } else {
                 $album_id = null;
             }
             $timeline = \Timeline\Site_Model::save_timeline($this->u->id, $post['public_flag'], $type_key, $album_id, null, $post['body'], $timeline, $album_image_ids);
             \DB::commit_transaction();
             // thumbnail 作成 & tmp_file thumbnail 削除
             \Site_FileTmp::make_and_remove_thumbnails($moved_files);
         } catch (\Exception $e) {
             if (\DB::in_transaction()) {
                 \DB::rollback_transaction();
             }
             if ($moved_files) {
                 \Site_FileTmp::move_files_to_tmp_dir($moved_files);
             }
             throw $e;
         }
         $data = array('id' => $timeline->id, 'message' => term('timeline') . 'を' . term('form.post') . 'しました。');
         if (conf('service.facebook.shareDialog.myhome.autoPopupAfterCreated')) {
             $link = \Uri::create(Site_Util::get_detail_uri($timeline->id, $timeline->type));
             $data['shareFacebook'] = array('obj' => array('link' => $link));
             if ($album_image_ids && ($album_image = \Album\Model_AlbumImage::find($album_image_ids[0]))) {
                 $data['shareFacebook']['obj']['picture'] = \Site_Util::get_media_uri(img_uri($album_image->file_name, 'thumbnail'), true);
             }
         }
         $this->set_response_body_api($data);
     });
 }