diff options
Diffstat (limited to 'system/admin/handlers.go')
-rw-r--r-- | system/admin/handlers.go | 260 |
1 files changed, 260 insertions, 0 deletions
diff --git a/system/admin/handlers.go b/system/admin/handlers.go index 4734ba0..0433b76 100644 --- a/system/admin/handlers.go +++ b/system/admin/handlers.go @@ -827,6 +827,266 @@ func recoveryKeyHandler(res http.ResponseWriter, req *http.Request) { } } +func uploadContentsHandler(res http.ResponseWriter, req *http.Request) { + q := req.URL.Query() + + order := strings.ToLower(q.Get("order")) + if order != "asc" { + order = "desc" + } + + pt := interface{}(&item.FileUpload{}) + + p, ok := pt.(editor.Editable) + if !ok { + res.WriteHeader(http.StatusInternalServerError) + errView, err := Error500() + if err != nil { + return + } + + res.Write(errView) + return + } + + count, err := strconv.Atoi(q.Get("count")) // int: determines number of posts to return (10 default, -1 is all) + if err != nil { + if q.Get("count") == "" { + count = 10 + } else { + res.WriteHeader(http.StatusInternalServerError) + errView, err := Error500() + if err != nil { + return + } + + res.Write(errView) + return + } + } + + offset, err := strconv.Atoi(q.Get("offset")) // int: multiplier of count for pagination (0 default) + if err != nil { + if q.Get("offset") == "" { + offset = 0 + } else { + res.WriteHeader(http.StatusInternalServerError) + errView, err := Error500() + if err != nil { + return + } + + res.Write(errView) + return + } + } + + opts := db.QueryOptions{ + Count: count, + Offset: offset, + Order: order, + } + + b := &bytes.Buffer{} + var total int + var posts [][]byte + + html := `<div class="col s9 card"> + <div class="card-content"> + <div class="row"> + <div class="col s8"> + <div class="row"> + <div class="card-title col s7">Uploaded Items</div> + <div class="col s5 input-field inline"> + <select class="browser-default __ponzu sort-order"> + <option value="DESC">New to Old</option> + <option value="ASC">Old to New</option> + </select> + <label class="active">Sort:</label> + </div> + <script> + $(function() { + var sort = $('select.__ponzu.sort-order'); + + sort.on('change', function() { + var path = window.location.pathname; + var s = sort.val(); + + window.location.replace(path + '&order=' + s); + }); + + var order = getParam('order'); + if (order !== '') { + sort.val(order); + } + + }); + </script> + </div> + </div> + <form class="col s4" action="/admin/contents/search" method="get"> + <div class="input-field post-search inline"> + <label class="active">Search:</label> + <i class="right material-icons search-icon">search</i> + <input class="search" name="q" type="text" placeholder="Within all Upload fields" class="search"/> + <input type="hidden" name="type" value="__uploads" /> + </div> + </form> + </div>` + + t := "__uploads" + status := "" + total, posts = db.Query(t, opts) + + for i := range posts { + err := json.Unmarshal(posts[i], &p) + if err != nil { + log.Println("Error unmarshal json into", t, err, string(posts[i])) + + post := `<li class="col s12">Error decoding data. Possible file corruption.</li>` + _, err := b.Write([]byte(post)) + if err != nil { + log.Println(err) + + res.WriteHeader(http.StatusInternalServerError) + errView, err := Error500() + if err != nil { + log.Println(err) + } + + res.Write(errView) + return + } + continue + } + + post := adminPostListItem(p, t, status) + _, err = b.Write(post) + if err != nil { + log.Println(err) + + res.WriteHeader(http.StatusInternalServerError) + errView, err := Error500() + if err != nil { + log.Println(err) + } + + res.Write(errView) + return + } + } + + html += `<ul class="posts row">` + + _, err = b.Write([]byte(`</ul>`)) + if err != nil { + log.Println(err) + + res.WriteHeader(http.StatusInternalServerError) + errView, err := Error500() + if err != nil { + log.Println(err) + } + + res.Write(errView) + return + } + + statusDisabled := "disabled" + prevStatus := "" + nextStatus := "" + // total may be less than 10 (default count), so reset count to match total + if total < count { + count = total + } + // nothing previous to current list + if offset == 0 { + prevStatus = statusDisabled + } + // nothing after current list + if (offset+1)*count >= total { + nextStatus = statusDisabled + } + + // set up pagination values + urlFmt := req.URL.Path + "?count=%d&offset=%d&&order=%s" + prevURL := fmt.Sprintf(urlFmt, count, offset-1, order) + nextURL := fmt.Sprintf(urlFmt, count, offset+1, order) + start := 1 + count*offset + end := start + count - 1 + + if total < end { + end = total + } + + pagination := fmt.Sprintf(` + <ul class="pagination row"> + <li class="col s2 waves-effect %s"><a href="%s"><i class="material-icons">chevron_left</i></a></li> + <li class="col s8">%d to %d of %d</li> + <li class="col s2 waves-effect %s"><a href="%s"><i class="material-icons">chevron_right</i></a></li> + </ul> + `, prevStatus, prevURL, start, end, total, nextStatus, nextURL) + + // show indicator that a collection of items will be listed implicitly, but + // that none are created yet + if total < 1 { + pagination = ` + <ul class="pagination row"> + <li class="col s2 waves-effect disabled"><a href="#"><i class="material-icons">chevron_left</i></a></li> + <li class="col s8">0 to 0 of 0</li> + <li class="col s2 waves-effect disabled"><a href="#"><i class="material-icons">chevron_right</i></a></li> + </ul> + ` + } + + _, err = b.Write([]byte(pagination + `</div></div>`)) + if err != nil { + log.Println(err) + + res.WriteHeader(http.StatusInternalServerError) + errView, err := Error500() + if err != nil { + log.Println(err) + } + + res.Write(errView) + return + } + + script := ` + <script> + $(function() { + var del = $('.quick-delete-post.__ponzu span'); + del.on('click', function(e) { + if (confirm("[Ponzu] Please confirm:\n\nAre you sure you want to delete this post?\nThis cannot be undone.")) { + $(e.target).parent().submit(); + } + }); + }); + + // disable link from being clicked if parent is 'disabled' + $(function() { + $('ul.pagination li.disabled a').on('click', function(e) { + e.preventDefault(); + }); + }); + </script> + ` + + btn := `<div class="col s3"><a href="/admin/upload" class="btn new-post waves-effect waves-light">New Upload</a></div></div>` + html = html + b.String() + script + btn + + adminView, err := Admin([]byte(html)) + if err != nil { + log.Println(err) + res.WriteHeader(http.StatusInternalServerError) + return + } + + res.Header().Set("Content-Type", "text/html") + res.Write(adminView) +} + func contentsHandler(res http.ResponseWriter, req *http.Request) { q := req.URL.Query() t := q.Get("type") |