src/main/scala/fis/fs/ui/AttachmentForm.scala
changeset 100 1fcbeae1f9da
equal deleted inserted replaced
99:49eb72a46208 100:1fcbeae1f9da
       
     1 /*
       
     2  * Copyright 2012 Tomas Zeman <tzeman@volny.cz>
       
     3  *
       
     4  * Licensed under the Apache License, Version 2.0 (the "License");
       
     5  * you may not use this file except in compliance with the License.
       
     6  * You may obtain a copy of the License at
       
     7  *
       
     8  *     http://www.apache.org/licenses/LICENSE-2.0
       
     9  *
       
    10  * Unless required by applicable law or agreed to in writing, software
       
    11  * distributed under the License is distributed on an "AS IS" BASIS,
       
    12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    13  * See the License for the specific language governing permissions and
       
    14  * limitations under the License.
       
    15  */
       
    16 package fis.fs.ui
       
    17 
       
    18 import fis.base.ui._
       
    19 import fis.fs.model._
       
    20 import net.liftweb.common._
       
    21 import net.liftweb.http._
       
    22 import net.liftweb.util._
       
    23 import net.tz.lift.model._
       
    24 
       
    25 abstract class AttachmentForm extends HorizontalScreen with CancelButton with
       
    26   SaveButton with AttachmentCrud {
       
    27 
       
    28   protected object attachment extends ScreenVar[Attachment](
       
    29     Attachment.createRecord)
       
    30 
       
    31   override def hasUploadField = true
       
    32 
       
    33   protected val file = makeField[Box[FileParamHolder], Nothing](l10n("File"),
       
    34     Empty, field => SHtml.fileUpload(fph => field.set(Full(fph))),
       
    35     NothingOtherValueInitializer)
       
    36 
       
    37   protected def onSuccess(a: Attachment): Unit
       
    38 
       
    39   def finish() { for {
       
    40     fph <- file.get
       
    41     a1 <- attachment.store(fph)
       
    42     a2 <- save(a1)
       
    43   } {
       
    44     onSuccess(a2)
       
    45   }}
       
    46 }
       
    47 
       
    48 abstract class DeleteAttachmentForm extends HorizontalScreen with CancelButton
       
    49   with DeleteButton with AttachmentCrud {
       
    50 
       
    51   protected def getAttachment: Box[Attachment]
       
    52 
       
    53   val confirm = field(l10n("Really delete this attachment?"), false)
       
    54 
       
    55   protected def onSuccess(objectDeleted: Boolean, contentRemoved: Boolean): Unit
       
    56 
       
    57   def finish() {
       
    58     for {
       
    59       a <- getAttachment if confirm
       
    60       r <- delete(a)
       
    61       rm <- a.remove
       
    62     } {
       
    63       onSuccess(r, rm)
       
    64     }
       
    65   }
       
    66 
       
    67 }
       
    68 
       
    69 // vim: set ts=2 sw=2 et: