Skip to content

Instantly share code, notes, and snippets.

@fmpwizard
Created March 11, 2011 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fmpwizard/865835 to your computer and use it in GitHub Desktop.
Save fmpwizard/865835 to your computer and use it in GitHub Desktop.
package com.fmpwizard.weyesowl.code.snippet
import com.fmpwizard.weyesowl.code.model.{Inventory, InventoryDescription,
InventoryImages, InventoryOptionFields, InventoryPrice, InventoryStock,
InventoryXCategory, Categories}
import scala.xml.{NodeSeq, Text, Elem}
import java.io.{File,FileOutputStream}
import net.liftweb._
import util._
import common._
import mapper._
import Helpers._
import http._
import S._
import js.JsCmds.Noop
object Inventorycreate extends Logger {
object product extends RequestVar(Inventory.create)
object description extends RequestVar(InventoryDescription.create)
object images extends RequestVar(InventoryImages.create)
object imageFile extends RequestVar[Box[FileParamHolder]](Empty){info("mas") }
object options extends RequestVar(InventoryOptionFields.create)
object price extends RequestVar(InventoryPrice.create)
object stock extends RequestVar(InventoryStock.create)
object category extends RequestVar(InventoryXCategory.create)
private def saveFile(fp: FileParamHolder): Unit = {
fp.file match {
case null => info("Am I here?")
case x if x.length == 0 => info("how about here?")
case x =>{
//this is just a temp location for now
val filePath = "src/main/webapp/images"
val oFile = new File(filePath, fp.fileName)
val output = new FileOutputStream(oFile)
output.write(fp.file)
output.close()
info("File uploaded!!!!!!!!!!!!!!!!!!!!!!")
}
}
}
def render ={
var name = ""
// process the form
def process() {
// if the age is < 13, display an error
if (product.is.part_number.length < 2){
S.error("Part Number is too short.")
} else {
//TODO For now we just support english
//description.is.language.set("EN")
// otherwise give the user feedback and
// redirect to the home page
product.is.description += description
product.is.optionFields += options
product.is.price += price
product.is.stock += stock
product.is.save
info("RequestVar is: %s".format(imageFile.is)) //This prints Empty :(
imageFile.is.map{ file => saveFile(file); info("Here")}
S.notice("Added Part Number: "+product.is.part_number + " " + name)
info("Done")
}
}
// associate each of the form elements
// with a function... behavior to perform when the
// for element is submitted
"name=part_number" #> SHtml.onSubmit(product.is.part_number.set(_)) & // set the name
"name=description" #> SHtml.onSubmit(description.is.text.set(_)) &
//"name=image" #> SHtml.onSubmit(SHtml.fileUpload(s => imageFile(Full(s))) ) &
//"name=image" #> SHtml.onSubmit(s => imageFile(Full(s))) &
//Here I just cannot tell what I'm supposed to use :(
"name=image" #> SHtml.fileUpload(s => imageFile.set(Full(s))) &
//"name=image" #> SHtml.onSubmit(imageFile.set(_)) &
// when the form is submitted, process the variable
"type=submit" #> SHtml.onSubmitUnit(process)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment