src/main/scala/fis/geo/ui/LocationSnippet.scala
author Tomas Zeman <tzeman@volny.cz>
Tue, 01 May 2012 23:27:55 +0200
changeset 89 fd364bae9c49
parent 76 2ba4569f2bd6
child 98 eac38214183d
permissions -rw-r--r--
Basic authorization: only logged in user can change entities

/*
 * Copyright 2012 Tomas Zeman <tzeman@volny.cz>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package fis.geo.ui

import fis.aaa.ui.IfLoggedIn
import fis.base.ui._
import fis.geo.model._
import net.liftweb.common._
import net.liftweb.http._
import net.liftweb.sitemap._
import net.liftweb.sitemap.Loc._
import net.liftweb.util._
import net.liftweb.util.Helpers._
import net.tz.lift.model._
import net.tz.lift.snippet._
import scala.xml.{Elem, NodeSeq, Text}

object LocationSnippet extends LocationCrud with EntitySnippet[Location] {
  val prefix = "location"

  private val listM = Menu("location.list", l10n("Locations")) / prefix >>
    Title(_ => i18n("Locations")) >>
    locTpl("entity/list") >> Snippet("list", ClearNodes) >> Hidden

  private val viewPre = Menu.param[Location]("location.view", l10n("Location"), parse,
    encode) / prefix / * >> Title(l => i18n("Location %s", l.linkName)) >>
    locTpl("entity/view") >> Snippet("panel", panel) >> Hidden

  private val editPre = Menu.param[Location]("location.edit", l10n("Edit"), parse,
    encode) / prefix / * / EDIT >>
    Title(l => i18n("Edit location %s", l.linkName)) >> IfLoggedIn.testVal >>
    locTpl("entity/form") >> Snippet("form", form) >> Hidden

  private val deletePre = Menu.param[Location]("location.delete", l10n("Delete"),
    parse, encode) / prefix / * / DELETE >>
    Title(l => i18n("Delete location %s", l.linkName)) >> IfLoggedIn.testVal >>
    locTpl("entity/delete") >> Snippet("form", deleteF) >> Hidden

  private val viewM = viewPre >> (SecNav(editPre) + deletePre).build
  private val editM = editPre >> SecNav(viewPre).build
  private val deleteM = deletePre >> SecNav(viewPre).build

  private lazy val viewLoc = viewM.toLoc
  private lazy val editLoc = editM.toLoc
  private lazy val deleteLoc = deleteM.toLoc

  val menu = listM submenus(viewM, editM, deleteM)

  private def cur = viewLoc.currentValue or editLoc.currentValue or
    deleteLoc.currentValue

  private def panel: CssTr = "*" #> cur.map(LocationPanel(_))

  object url {
    def view: Location => Box[String] = (viewLoc.calcHref _) andThen (Box !! _)
  }

  private case class LocationLink(c: Location) extends EntityLink[Location](c,
    url.view)

  EntityLink.register[Location](LocationLink(_))

  private object form extends LocationForm {

    override def localSetup() {
      cur.foreach { l =>
        location(l)
        l.address.vend.foreach(address(_))
      }
    }

    def onSuccess(l: Location) {
      S.redirectTo(viewLoc.calcHref(l))
    }
  }

  private object deleteF extends HorizontalScreen with CancelButton with
    DeleteButton {

    val confirm = field(l10n("Really delete this location?"), false)

    def finish() {
      for {
        l <- deleteLoc.currentValue if confirm
        a <- l.address.vend
        c <- a.city.vend
        u <- CitySnippet.url.view(c)
        r <- delete(l)
        ra <- AddressCrud.delete(a)
        n <- r.box(l.linkName)
      } {
        S notice l10n("Location %s deleted.", n)
        S redirectTo u
      }
    }
  }

}

// vim: set ts=2 sw=2 et: