diff options
-rw-r--r-- | jobedit.go | 103 | ||||
-rw-r--r-- | main.go | 2 | ||||
-rw-r--r-- | tpls.go | 2 | ||||
-rw-r--r-- | tpls/jobedit.tpl | 51 | ||||
-rw-r--r-- | tpls/jobs.tpl | 2 |
5 files changed, 159 insertions, 1 deletions
diff --git a/jobedit.go b/jobedit.go new file mode 100644 index 0000000..ca4e445 --- /dev/null +++ b/jobedit.go @@ -0,0 +1,103 @@ +package main + +import ( + "github.com/gorilla/mux" + "github.com/gorilla/sessions" + "kch42.de/gostuff/mailremind/chronos" + "kch42.de/gostuff/mailremind/model" + "log" + "net/http" +) + +type scheduleTpldata struct { + Start, End string + Count int + UnitIsMinute, UnitIsHour, UnitIsDay, UnitIsWeek, UnitIsMonth, UnitIsYear bool + RepetitionEnabled, EndEnabled bool +} + +const maxSchedules = 10 +const bestTimeFmtEver = "2006-01-02 15:04:05" + +type jobeditTpldata struct { + Error, Success string + Fatal bool + JobID, Subject, Content string + Schedules [maxSchedules]scheduleTpldata +} + +func (jt *jobeditTpldata) fillFromJob(job model.Job, u model.User) { + jt.JobID = job.ID().String() + jt.Subject = job.Subject() + jt.Content = string(job.Content()) + + loc := u.Location() + + for i, chron := range job.Chronos() { + if i == 10 { + log.Printf("Job %s has more than %d Chronos entries!", job.ID(), maxSchedules) + break + } + + schedule := scheduleTpldata{ + Start: chron.Start.In(loc).Format(bestTimeFmtEver), + } + + if f := chron.Freq; f.Count > 0 { + schedule.RepetitionEnabled = true + schedule.Count = int(f.Count) + switch f.Unit { + case chronos.Minute: + schedule.UnitIsMinute = true + case chronos.Hour: + schedule.UnitIsHour = true + case chronos.Day: + schedule.UnitIsDay = true + case chronos.Week: + schedule.UnitIsWeek = true + case chronos.Month: + schedule.UnitIsMonth = true + case chronos.Year: + schedule.UnitIsYear = true + } + } + + if end := chron.End; !end.IsZero() { + schedule.EndEnabled = true + schedule.End = end.In(loc).Format(bestTimeFmtEver) + } + + jt.Schedules[i] = schedule + } +} + +func jobedit(user model.User, sess *sessions.Session, req *http.Request) interface{} { + if user == nil { + return &jobeditTpldata{Error: "You need to be logged in to do that.", Fatal: true} + } + + outdata := new(jobeditTpldata) + + // Try to load job, if given + _id := mux.Vars(req)["ID"] + var job model.Job + if _id != "" { + id, err := db.ParseDBID(_id) + if err != nil { + return &jobeditTpldata{Error: "Job not found", Fatal: true} + } + + if job, err = user.JobByID(id); err != nil { + return &jobeditTpldata{Error: "Job not found", Fatal: true} + } + } + + if req.Method == "POST" { + // TODO: Enable editing... + } + + if job != nil { + outdata.fillFromJob(job, user) + } + return outdata +} @@ -90,6 +90,8 @@ func main() { router.HandleFunc("/pwreset", mkHttpHandler(pwreset, tplPwreset)) router.HandleFunc("/forgotpw", mkHttpHandler(forgotpw, tplForgotpw)) router.HandleFunc("/jobs", mkHttpHandler(jobs, tplJobs)) + router.HandleFunc("/jobedit", mkHttpHandler(jobedit, tplJobedit)) + router.HandleFunc("/jobedit/{ID}", mkHttpHandler(jobedit, tplJobedit)) http.Handle("/", router) @@ -24,6 +24,7 @@ var ( tplPwreset *template.Template tplForgotpw *template.Template tplJobs *template.Template + tplJobedit *template.Template ) func initTpls() { @@ -39,6 +40,7 @@ func initTpls() { tplPwreset = loadTpl(tplpath, "pwreset") tplForgotpw = loadTpl(tplpath, "forgotpw") tplJobs = loadTpl(tplpath, "jobs") + tplJobedit = loadTpl(tplpath, "jobedit") } type msgTpldata struct { diff --git a/tpls/jobedit.tpl b/tpls/jobedit.tpl new file mode 100644 index 0000000..ef1d394 --- /dev/null +++ b/tpls/jobedit.tpl @@ -0,0 +1,51 @@ +{{define "title"}}{{if .JobID}}Edit Job{{else}}Create Job{{end}}{{end}} + +{{define "content"}} + {{if .Error}} + <div class="error">{{.Error}}</div> + {{end}} + + {{if not .Fatal}} + {{if .Success}} + <div class="success">{{.Success}}</div> + {{end}} + + <form action="/jobedit{{if .JobID}}/{{.JobID}}{{end}}" method="post" accept-charset="UTF-8"> + <h2>Mail</h2> + <p> + <strong>Subject:</strong><br /> + <input type="text" name="Subject" value="{{.Subject}}" /> + </p> + <p> + <strong>Content:</strong><br /> + <textarea name="Content" cols="80" rows="20">{{.Content}}</textarea> + </p> + + <h2>Schedule</h2> + {{range .Schedules}} + <p> + <strong>Start:</strong> + <input type="text" name="Start" value="{{.Start}}" /><br /> + <input type="checkbox" name="RepetitionEnabled"{{if .RepetitionEnabled}} checked="checked"{{end}} /> + <strong>Repetition:</strong> + <input type="text" name="Count" value="{{.Count}}" /> + <select name="Unit" size="0"> + <option value="Minute"{{if .UnitIsMinute}} selected="selected"{{end}}>Minute(s)</option> + <option value="Hour"{{if .UnitIsHour}} selected="selected"{{end}}>Hour(s)</option> + <option value="Day"{{if .UnitIsDay}} selected="selected"{{end}}>Day(s)</option> + <option value="Week"{{if .UnitIsWeek}} selected="selected"{{end}}>Week(s)</option> + <option value="Month"{{if .UnitIsMonth}} selected="selected"{{end}}>Month(s)</option> + <option value="Year"{{if .UnitIsYear}} selected="selected"{{end}}>Year(s)</option> + </select> + <br /> + <input type="checkbox" name="EndEnabled"{{if .EndEnabled}} checked="checked"{{end}} /> + <strong>End:</strong> + <input type="text" name="End" value="{{.End}}" /> + </p> + {{end}} + + <h2>Save</h2> + <input type="submit" /> + </form> + {{end}} +{{end}}
\ No newline at end of file diff --git a/tpls/jobs.tpl b/tpls/jobs.tpl index 578791e..9408e48 100644 --- a/tpls/jobs.tpl +++ b/tpls/jobs.tpl @@ -23,7 +23,7 @@ <tbody> {{range .Jobs}}<tr> <td><input type="checkbox" name="Jobs" value="{{.ID}}" /></td> - <td><a href="/edit/{{.ID}}">{{.Subject}}</a></td> + <td><a href="/jobedit/{{.ID}}">{{.Subject}}</a></td> <td>{{.Excerpt}}</td> <td>{{.Next}}</td> </tr>{{end}} |