Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

142 lines
3.8KB

  1. package main
  2. import (
  3. "fmt"
  4. "html/template"
  5. "io"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "os"
  10. "strings"
  11. )
  12. func spaEditProfile(w http.ResponseWriter, r *http.Request) {
  13. switch r.Method {
  14. case "GET":
  15. lead, found := getLeadFromRequest(r)
  16. if !found {
  17. response404Handler(w)
  18. // w.WriteHeader(http.StatusUnauthorized)
  19. // fmt.Fprintf(w, "User not found ")
  20. return
  21. }
  22. spaEditProfilePopulateLeadInfo(w, lead)
  23. case "POST":
  24. spaEditProfileSaveLead(w, r)
  25. default:
  26. w.WriteHeader(http.StatusUnauthorized)
  27. fmt.Fprintf(w, "Unsupported Method %s", r.Method)
  28. }
  29. //http.ServeFile(w, r, "spa/edit.html")
  30. //lead, _ := crmGetLead("595a323d4d639d84d")
  31. //spaEditProfilePopulateLeadInfo(w, lead)
  32. }
  33. func getLeadFromRequest(r *http.Request) (lead crmdLead, found bool) {
  34. id, found := getLeadIDFromCookie(r)
  35. if !found {
  36. return
  37. }
  38. lead, err := crmGetLead(id)
  39. if err != nil {
  40. found = false
  41. log.Printf("WARNING: valid cookie lead id %s, not found in EntityCRM", id)
  42. return
  43. }
  44. found = true
  45. return
  46. }
  47. func spaEditProfilePopulateLeadInfo(w http.ResponseWriter, lead crmdLead) {
  48. tTest := template.New("spaEditProfile")
  49. str, err := ioutil.ReadFile("spa/edit.html")
  50. if err != nil {
  51. w.WriteHeader(http.StatusInternalServerError)
  52. fmt.Fprintf(w, "Formating information not available.")
  53. return
  54. }
  55. tTest, err = tTest.Parse(string(str))
  56. if err != nil {
  57. w.WriteHeader(http.StatusInternalServerError)
  58. fmt.Fprintf(w, "Formating instruction invalid")
  59. return
  60. }
  61. err = tTest.Execute(w, lead)
  62. if err != nil {
  63. w.WriteHeader(http.StatusInternalServerError)
  64. fmt.Fprintf(w, "Monkey runs into our computer room...")
  65. log.Println("ERROR: Template execution on spa/Edit, failed \n" + err.Error())
  66. }
  67. }
  68. func spaEditProfileSaveLead(w http.ResponseWriter, r *http.Request) {
  69. original, found := getLeadFromRequest(r)
  70. if !found {
  71. w.WriteHeader(http.StatusUnauthorized)
  72. fmt.Fprintf(w, "You are not authorized to change data...")
  73. log.Println("ERROR: trying to spa/edit lead, but not found")
  74. return
  75. }
  76. err := r.ParseMultipartForm(32 << 20)
  77. if err == nil {
  78. for k, v := range r.Form {
  79. fmt.Println("key:", k)
  80. fmt.Println("val:", strings.Join(v, ""))
  81. }
  82. lastname, lok := r.Form["lastname"]
  83. email, eok := r.Form["email"]
  84. phone, pok := r.Form["phone"]
  85. description, dok := r.Form["description"]
  86. lead := crmdLead{} //create a patch info, we cannot reuse original, as it as emailAdressData, phoneNumberData array. it will prevent us from updating email address and phone number, unless we modify them as well.
  87. lead.ID = original.ID
  88. if lok && eok && pok && dok {
  89. lead.LastName = lastname[0]
  90. lead.EmailAddress = email[0]
  91. lead.PhoneNumber = phone[0]
  92. lead.Description = description[0]
  93. if original.FirstName == "Anonymous" {
  94. lead.FirstName = "\u0020" //a visible space
  95. }
  96. log.Println(original.Status)
  97. if original.Status == "New" || original.Status == "Anonymous" {
  98. log.Println("change status to Assigned")
  99. lead.Status = "Assigned"
  100. }
  101. //check to see if there is file
  102. file, handler, err := r.FormFile("avatar")
  103. if err == nil {
  104. defer file.Close()
  105. log.Printf("%v", handler.Header)
  106. log.Println(handler.Filename)
  107. f, err := ioutil.TempFile(os.TempDir(), "wechat_hitxy_spa_edit_lead")
  108. //os.OpenFile("/tmp/wechat_hitxy_avatar_upload.jpg", os.O_WRONLY|os.O_CREATE, 0666)
  109. if err == nil {
  110. defer f.Close()
  111. io.Copy(f, file)
  112. attch, err := crmCreateAttachment(f.Name())
  113. if err == nil {
  114. lead.AvatarID = attch.ID
  115. os.Remove(f.Name())
  116. }
  117. }
  118. }
  119. }
  120. updatedLead, err := lead.Save()
  121. log.Println(updatedLead.PhoneNumber)
  122. log.Println(updatedLead.EmailAddress)
  123. log.Println(updatedLead.ID)
  124. if err == nil {
  125. spaEditProfilePopulateLeadInfo(w, updatedLead)
  126. //fmt.Fprintf(w, `{"status": "succes"}`)
  127. return
  128. }
  129. }
  130. fmt.Fprintf(w, `{"status": "error"}`)
  131. }