<commit_before>package nl.malienkolders.htm.admin
package snippet

import net.liftweb._
import common._
import http._
import mapper._
import sitemap._
import util.Helpers._
import nl.malienkolders.htm.lib.model._
import nl.malienkolders.htm.admin.lib.PhotoImporterBackend
import net.liftweb.http.js.JsCmds.{ Reload, RedirectTo }

object ParticipantRegistration {

  val menu = Menu.param[ParamInfo]("Registration", "Registration", s => Full(ParamInfo(s)),
    pi => pi.param) / "participants" / "register" >> Loc.Hidden
  lazy val loc = menu.toLoc

  implicit class PimpedSubscription(sub: TournamentParticipant) {
    def pool = {
      val participantId = sub.participant.get
      val tournament = sub.tournament.obj.get

      val poolName = for {
        pool <- tournament.poolPhase.pools.find(_.participants.exists(_.id.get == participantId))
      } yield { pool.poolName }

      poolName.getOrElse("?")
    }
  }

  def render = {
    val p = Participant.find(By(Participant.externalId, ParticipantRegistration.loc.currentValue.map(_.param).get)).get

    var upload: Box[FileParamHolder] = Empty

    def process() = {
      for (fph <- upload) {
        PhotoImporterBackend.handlePhoto(p, fph.fileStream)
      }
      if (p.validate.isEmpty) {
        p.save
        Reload;
      } else {
        S.error(p.validate)
      }
    }

    ".photo [src]" #> s"/photo/${p.externalId.get}/l" &
      "name=name" #> p.name.toForm &
      "name=shortName" #> p.shortName.toForm &
      "name=club" #> p.club.toForm &
      "name=clubCode" #> p.clubCode.toForm &
      "name=age" #> p.age.toForm &
      "name=height" #> p.height.toForm &
      "name=weight" #> p.weight.toForm &
      "name=previousWins" #> p.previousWins.toForm &
      "name=externalId" #> p.externalId.toForm &
      "name=present" #> p.isPresent.toForm &
      ".subscription" #> p.subscriptions.sortBy(!_.primary.get).map(sub =>
        "a" #> <a name={ "tournament" + sub.tournament.obj.get.id.get.toString }></a> &
          ".tournament *" #> sub.tournament.obj.get.name.get &
          ".pool *" #> sub.pool &
          ".fighterNumber *" #> sub.fighterNumber.asHtml &
          ".experience" #> sub.experience.toForm &
          ".gear" #> sub.gearChecked.toForm) &
      "name=photo" #> SHtml.fileUpload(fph => upload = Full(fph), "accept" -> "image/*;capture=camera") &
      "name=save" #> SHtml.submit("Save", process, "class" -> "btn btn-primary")

  }

}<commit_msg>Changing the participant id in the registration screen caused a crash<commit_after>package nl.malienkolders.htm.admin
package snippet

import net.liftweb._
import common._
import http._
import mapper._
import sitemap._
import util.Helpers._
import nl.malienkolders.htm.lib.model._
import nl.malienkolders.htm.admin.lib.PhotoImporterBackend
import net.liftweb.http.js.JsCmds.{ Reload, RedirectTo }

object ParticipantRegistration {

  val menu = Menu.param[ParamInfo]("Registration", "Registration", s => Full(ParamInfo(s)),
    pi => pi.param) / "participants" / "register" >> Loc.Hidden
  lazy val loc = menu.toLoc

  implicit class PimpedSubscription(sub: TournamentParticipant) {
    def pool = {
      val participantId = sub.participant.get
      val tournament = sub.tournament.obj.get

      val poolName = for {
        pool <- tournament.poolPhase.pools.find(_.participants.exists(_.id.get == participantId))
      } yield { pool.poolName }

      poolName.getOrElse("?")
    }
  }

  def render = {
    val p = Participant.find(By(Participant.externalId, ParticipantRegistration.loc.currentValue.map(_.param).get)).get

    var upload: Box[FileParamHolder] = Empty

    def process() = {
      for (fph <- upload) {
        PhotoImporterBackend.handlePhoto(p, fph.fileStream)
      }
      if (p.validate.isEmpty) {
        p.save
        S.redirectTo("/participants/register/" + p.externalId.get)
      } else {
        S.error(p.validate)
      }
    }

    ".photo [src]" #> s"/photo/${p.externalId.get}/l" &
      "name=name" #> p.name.toForm &
      "name=shortName" #> p.shortName.toForm &
      "name=club" #> p.club.toForm &
      "name=clubCode" #> p.clubCode.toForm &
      "name=age" #> p.age.toForm &
      "name=height" #> p.height.toForm &
      "name=weight" #> p.weight.toForm &
      "name=previousWins" #> p.previousWins.toForm &
      "name=externalId" #> p.externalId.toForm &
      "name=present" #> p.isPresent.toForm &
      ".subscription" #> p.subscriptions.sortBy(!_.primary.get).map(sub =>
        "a" #> <a name={ "tournament" + sub.tournament.obj.get.id.get.toString }></a> &
          ".tournament *" #> sub.tournament.obj.get.name.get &
          ".pool *" #> sub.pool &
          ".fighterNumber *" #> sub.fighterNumber.asHtml &
          ".experience" #> sub.experience.toForm &
          ".gear" #> sub.gearChecked.toForm) &
      "name=photo" #> SHtml.fileUpload(fph => upload = Full(fph), "accept" -> "image/*;capture=camera") &
      "name=save" #> SHtml.submit("Save", process, "class" -> "btn btn-primary")

  }

}
{'fraction_non_alphanumeric': 0.15660490137477584, 'fraction_numerical': 0.0, 'mean_word_length': 2.8645111624326405, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '144593', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1711, 'n_words': 287}