diff options
author | Steve Manuel <nilslice@gmail.com> | 2017-04-29 11:21:14 -0700 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2017-04-29 11:21:14 -0700 |
commit | 7fffb0b422f8306f709f38b029cc0a03e583184c (patch) | |
tree | 0b8ab4b70674b9a9fe695d3ec2d979ec05dc29c9 | |
parent | 910874aeb538863ac1b9768843b98eb9b013f47e (diff) |
adding view and links to admin/manager
-rw-r--r-- | system/admin/admin.go | 1 | ||||
-rw-r--r-- | system/admin/handlers.go | 260 | ||||
-rw-r--r-- | system/admin/server.go | 2 |
3 files changed, 263 insertions, 0 deletions
diff --git a/system/admin/admin.go b/system/admin/admin.go index 09750f0..9e57678 100644 --- a/system/admin/admin.go +++ b/system/admin/admin.go @@ -66,6 +66,7 @@ var mainAdminHTML = ` <div class="row collection-item"> <li><a class="col s12" href="/admin/configure"><i class="tiny left material-icons">settings</i>Configuration</a></li> <li><a class="col s12" href="/admin/configure/users"><i class="tiny left material-icons">supervisor_account</i>Admin Users</a></li> + <li><a class="col s12" href="/admin/uploads"><i class="tiny left material-icons">present_to_all</i>Addons</a></li> <li><a class="col s12" href="/admin/addons"><i class="tiny left material-icons">settings_input_svideo</i>Addons</a></li> </div> </ul> 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") diff --git a/system/admin/server.go b/system/admin/server.go index df00c21..94aca78 100644 --- a/system/admin/server.go +++ b/system/admin/server.go @@ -32,6 +32,8 @@ func Run() { http.HandleFunc("/admin/configure/users/edit", user.Auth(configUsersEditHandler)) http.HandleFunc("/admin/configure/users/delete", user.Auth(configUsersDeleteHandler)) + http.HandleFunc("/admin/uploads", user.Auth(uploadContentsHandler)) + http.HandleFunc("/admin/contents", user.Auth(contentsHandler)) http.HandleFunc("/admin/contents/search", user.Auth(searchHandler)) |