2017-11-20 06:10:04 +01:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-01-03 07:04:48 +01:00
|
|
|
package ui
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-11-21 23:57:27 +01:00
|
|
|
|
2018-01-03 07:04:48 +01:00
|
|
|
"github.com/miniflux/miniflux/http/handler"
|
2017-12-16 03:55:57 +01:00
|
|
|
"github.com/miniflux/miniflux/logger"
|
2017-12-13 06:48:13 +01:00
|
|
|
"github.com/miniflux/miniflux/model"
|
2018-01-03 07:04:48 +01:00
|
|
|
"github.com/miniflux/miniflux/ui/form"
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
2017-11-22 02:40:29 +01:00
|
|
|
// ShowCategories shows the page with all categories.
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) ShowCategories(ctx *handler.Context, request *handler.Request, response *handler.Response) {
|
2017-11-20 06:10:04 +01:00
|
|
|
args, err := c.getCommonTemplateArgs(ctx)
|
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:37:08 +01:00
|
|
|
user := ctx.LoggedUser()
|
2017-11-28 06:30:04 +01:00
|
|
|
categories, err := c.store.CategoriesWithFeedCount(user.ID)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().Render("categories", args.Merge(tplParams{
|
2017-11-20 06:10:04 +01:00
|
|
|
"categories": categories,
|
|
|
|
"total": len(categories),
|
|
|
|
"menu": "categories",
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2017-11-22 02:40:29 +01:00
|
|
|
// ShowCategoryEntries shows all entries for the given category.
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) ShowCategoryEntries(ctx *handler.Context, request *handler.Request, response *handler.Response) {
|
2017-11-22 03:37:08 +01:00
|
|
|
user := ctx.LoggedUser()
|
2017-11-22 03:14:45 +01:00
|
|
|
offset := request.QueryIntegerParam("offset", 0)
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
args, err := c.getCommonTemplateArgs(ctx)
|
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
category, err := c.getCategoryFromURL(ctx, request, response)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-29 04:20:14 +01:00
|
|
|
builder := c.store.NewEntryQueryBuilder(user.ID)
|
2017-11-20 06:10:04 +01:00
|
|
|
builder.WithCategoryID(category.ID)
|
|
|
|
builder.WithOrder(model.DefaultSortingOrder)
|
2017-12-03 02:04:01 +01:00
|
|
|
builder.WithDirection(user.EntryDirection)
|
2017-11-22 02:40:29 +01:00
|
|
|
builder.WithoutStatus(model.EntryStatusRemoved)
|
2017-11-20 06:10:04 +01:00
|
|
|
builder.WithOffset(offset)
|
2017-11-28 06:30:04 +01:00
|
|
|
builder.WithLimit(nbItemsPerPage)
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
entries, err := builder.GetEntries()
|
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err := builder.CountEntries()
|
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().Render("category_entries", args.Merge(tplParams{
|
2017-11-20 06:10:04 +01:00
|
|
|
"category": category,
|
|
|
|
"entries": entries,
|
|
|
|
"total": count,
|
2017-11-22 03:37:08 +01:00
|
|
|
"pagination": c.getPagination(ctx.Route("categoryEntries", "categoryID", category.ID), count, offset),
|
2017-11-20 06:10:04 +01:00
|
|
|
"menu": "categories",
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2017-11-22 02:40:29 +01:00
|
|
|
// CreateCategory shows the form to create a new category.
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) CreateCategory(ctx *handler.Context, request *handler.Request, response *handler.Response) {
|
2017-11-20 06:10:04 +01:00
|
|
|
args, err := c.getCommonTemplateArgs(ctx)
|
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().Render("create_category", args.Merge(tplParams{
|
2017-11-20 06:10:04 +01:00
|
|
|
"menu": "categories",
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2017-11-22 02:40:29 +01:00
|
|
|
// SaveCategory validate and save the new category into the database.
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) SaveCategory(ctx *handler.Context, request *handler.Request, response *handler.Response) {
|
2017-11-22 03:37:08 +01:00
|
|
|
user := ctx.LoggedUser()
|
2017-11-20 06:10:04 +01:00
|
|
|
args, err := c.getCommonTemplateArgs(ctx)
|
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:14:45 +01:00
|
|
|
categoryForm := form.NewCategoryForm(request.Request())
|
2017-11-20 06:10:04 +01:00
|
|
|
if err := categoryForm.Validate(); err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().Render("create_category", args.Merge(tplParams{
|
2017-11-20 06:10:04 +01:00
|
|
|
"errorMessage": err.Error(),
|
|
|
|
}))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-28 06:30:04 +01:00
|
|
|
duplicateCategory, err := c.store.CategoryByTitle(user.ID, categoryForm.Title)
|
2017-11-21 23:57:27 +01:00
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-21 23:57:27 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if duplicateCategory != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().Render("create_category", args.Merge(tplParams{
|
2017-11-21 23:57:27 +01:00
|
|
|
"errorMessage": "This category already exists.",
|
|
|
|
}))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-20 06:10:04 +01:00
|
|
|
category := model.Category{Title: categoryForm.Title, UserID: user.ID}
|
|
|
|
err = c.store.CreateCategory(&category)
|
|
|
|
if err != nil {
|
2017-12-16 03:55:57 +01:00
|
|
|
logger.Info("[Controller:CreateCategory] %v", err)
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().Render("create_category", args.Merge(tplParams{
|
2017-11-20 06:10:04 +01:00
|
|
|
"errorMessage": "Unable to create this category.",
|
|
|
|
}))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:37:08 +01:00
|
|
|
response.Redirect(ctx.Route("categories"))
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-22 02:40:29 +01:00
|
|
|
// EditCategory shows the form to modify a category.
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) EditCategory(ctx *handler.Context, request *handler.Request, response *handler.Response) {
|
2017-11-22 03:37:08 +01:00
|
|
|
user := ctx.LoggedUser()
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
category, err := c.getCategoryFromURL(ctx, request, response)
|
|
|
|
if err != nil {
|
2017-12-16 03:55:57 +01:00
|
|
|
logger.Error("[Controller:EditCategory] %v", err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
args, err := c.getCategoryFormTemplateArgs(ctx, user, category, nil)
|
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().Render("edit_category", args)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-22 02:40:29 +01:00
|
|
|
// UpdateCategory validate and update a category.
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) UpdateCategory(ctx *handler.Context, request *handler.Request, response *handler.Response) {
|
2017-11-22 03:37:08 +01:00
|
|
|
user := ctx.LoggedUser()
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
category, err := c.getCategoryFromURL(ctx, request, response)
|
|
|
|
if err != nil {
|
2017-12-16 03:55:57 +01:00
|
|
|
logger.Error("[Controller:UpdateCategory] %v", err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:14:45 +01:00
|
|
|
categoryForm := form.NewCategoryForm(request.Request())
|
2017-11-20 06:10:04 +01:00
|
|
|
args, err := c.getCategoryFormTemplateArgs(ctx, user, category, categoryForm)
|
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := categoryForm.Validate(); err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().Render("edit_category", args.Merge(tplParams{
|
2017-11-20 06:10:04 +01:00
|
|
|
"errorMessage": err.Error(),
|
|
|
|
}))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-21 23:57:27 +01:00
|
|
|
if c.store.AnotherCategoryExists(user.ID, category.ID, categoryForm.Title) {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().Render("edit_category", args.Merge(tplParams{
|
2017-11-21 23:57:27 +01:00
|
|
|
"errorMessage": "This category already exists.",
|
|
|
|
}))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-20 06:10:04 +01:00
|
|
|
err = c.store.UpdateCategory(categoryForm.Merge(category))
|
|
|
|
if err != nil {
|
2017-12-16 03:55:57 +01:00
|
|
|
logger.Error("[Controller:UpdateCategory] %v", err)
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().Render("edit_category", args.Merge(tplParams{
|
2017-11-20 06:10:04 +01:00
|
|
|
"errorMessage": "Unable to update this category.",
|
|
|
|
}))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:37:08 +01:00
|
|
|
response.Redirect(ctx.Route("categories"))
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2017-11-22 02:40:29 +01:00
|
|
|
// RemoveCategory delete a category from the database.
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) RemoveCategory(ctx *handler.Context, request *handler.Request, response *handler.Response) {
|
2017-11-22 03:37:08 +01:00
|
|
|
user := ctx.LoggedUser()
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
category, err := c.getCategoryFromURL(ctx, request, response)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.store.RemoveCategory(user.ID, category.ID); err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:37:08 +01:00
|
|
|
response.Redirect(ctx.Route("categories"))
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) getCategoryFromURL(ctx *handler.Context, request *handler.Request, response *handler.Response) (*model.Category, error) {
|
2017-11-22 03:14:45 +01:00
|
|
|
categoryID, err := request.IntegerParam("categoryID")
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().BadRequest(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:37:08 +01:00
|
|
|
user := ctx.LoggedUser()
|
2017-11-28 06:30:04 +01:00
|
|
|
category, err := c.store.Category(user.ID, categoryID)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().ServerError(err)
|
2017-11-20 06:10:04 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if category == nil {
|
2017-11-22 03:30:16 +01:00
|
|
|
response.HTML().NotFound()
|
2017-11-20 06:10:04 +01:00
|
|
|
return nil, errors.New("Category not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return category, nil
|
|
|
|
}
|
|
|
|
|
2018-01-03 07:04:48 +01:00
|
|
|
func (c *Controller) getCategoryFormTemplateArgs(ctx *handler.Context, user *model.User, category *model.Category, categoryForm *form.CategoryForm) (tplParams, error) {
|
2017-11-20 06:10:04 +01:00
|
|
|
args, err := c.getCommonTemplateArgs(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if categoryForm == nil {
|
|
|
|
args["form"] = form.CategoryForm{
|
|
|
|
Title: category.Title,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
args["form"] = categoryForm
|
|
|
|
}
|
|
|
|
|
|
|
|
args["category"] = category
|
|
|
|
args["menu"] = "categories"
|
|
|
|
return args, nil
|
|
|
|
}
|