summaryrefslogtreecommitdiff
path: root/jobs.go
blob: d4787fa097bc718ddee57de2398ebef1dccdf4ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main

import (
	"github.com/gorilla/sessions"
	"kch42.de/gostuff/mailremind/model"
	"net/http"
)

type jobTpldata struct {
	ID, Subject, Excerpt, Next string
}

func jobToTpldata(job model.Job, user model.User) *jobTpldata {
	excerpt := string(job.Content())
	if len(excerpt) > 100 {
		excerpt = string([]rune(excerpt)[0:100]) + " (...)"
	}

	return &jobTpldata{
		ID:      job.ID().String(),
		Subject: job.Subject(),
		Excerpt: excerpt,
		Next:    job.Next().In(user.Location()).Format("2006-Jan-02 15:04:05"),
	}
}

type jobsTpldata struct {
	Error, Success string
	Jobs           []*jobTpldata
	Fatal          bool
}

func jobs(user model.User, sess *sessions.Session, req *http.Request) interface{} {
	if user == nil {
		return &jobsTpldata{Error: "You need to be logged in to do that.", Fatal: true}
	}

	outdata := new(jobsTpldata)

	if req.Method == "POST" {
		if err := req.ParseForm(); err != nil {
			outdata.Error = "Could not understand form data."
			goto listjobs
		}

		if req.FormValue("Delconfirm") != "yes" {
			goto listjobs
		}

		for _, _id := range req.Form["Jobs"] {
			id, err := db.ParseDBID(_id)
			if err != nil {
				outdata.Error = "Not all jobs could be deleted."
				continue
			}

			job, err := user.JobByID(id)
			if err != nil {
				outdata.Error = "Not all jobs could be deleted."
				continue
			}

			if job.Delete() != nil {
				outdata.Error = "Not all jobs could be deleted."
				continue
			}

			outdata.Success = "Jobs deleted."
		}
	}

listjobs:
	jobs := user.Jobs()
	outdata.Jobs = make([]*jobTpldata, len(jobs))

	for i, job := range jobs {
		outdata.Jobs[i] = jobToTpldata(job, user)
	}

	return outdata
}