2020-01-05 14:18:40 +01:00
|
|
|
package query
|
2019-12-11 07:37:39 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2019-12-16 23:33:52 +01:00
|
|
|
"github.com/gosimple/slug"
|
2019-12-11 16:55:18 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2019-12-11 07:37:39 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
2020-01-13 11:07:09 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/capture"
|
2020-05-03 18:00:50 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-12-11 07:37:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Labels searches labels based on their name.
|
2020-05-08 15:41:01 +02:00
|
|
|
func Labels(f form.LabelSearch) (results []LabelResult, err error) {
|
2019-12-11 07:37:39 +01:00
|
|
|
if err := f.ParseQueryString(); err != nil {
|
|
|
|
return results, err
|
|
|
|
}
|
|
|
|
|
2020-05-23 20:58:58 +02:00
|
|
|
defer log.Debug(capture.Time(time.Now(), fmt.Sprintf("labels: search %s", form.Serialize(f, true))))
|
2019-12-11 07:37:39 +01:00
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
s := UnscopedDb()
|
2019-12-11 07:37:39 +01:00
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
// s.LogMode(true)
|
2019-12-11 07:37:39 +01:00
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Table("labels").
|
2019-12-16 23:33:52 +01:00
|
|
|
Select(`labels.*`).
|
2019-12-11 07:37:39 +01:00
|
|
|
Where("labels.deleted_at IS NULL").
|
2020-05-10 19:43:49 +02:00
|
|
|
Where("labels.photo_count > 0").
|
2019-12-11 07:37:39 +01:00
|
|
|
Group("labels.id")
|
|
|
|
|
2020-01-30 18:19:26 +01:00
|
|
|
if f.ID != "" {
|
2020-05-23 20:58:58 +02:00
|
|
|
s = s.Where("labels.label_uid = ?", f.ID)
|
2020-01-30 18:19:26 +01:00
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
if result := s.Scan(&results); result.Error != nil {
|
2020-01-30 18:19:26 +01:00
|
|
|
return results, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2019-12-11 07:37:39 +01:00
|
|
|
if f.Query != "" {
|
|
|
|
var labelIds []uint
|
2019-12-11 16:55:18 +01:00
|
|
|
var categories []entity.Category
|
|
|
|
var label entity.Label
|
2019-12-11 07:37:39 +01:00
|
|
|
|
2019-12-16 23:33:52 +01:00
|
|
|
slugString := slug.Make(f.Query)
|
2020-12-15 20:14:06 +01:00
|
|
|
likeString := "%" + f.Query + "%"
|
2019-12-11 07:37:39 +01:00
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
if result := Db().First(&label, "label_slug = ? OR custom_slug = ?", slugString, slugString); result.Error != nil {
|
2020-05-03 18:00:50 +02:00
|
|
|
log.Infof("search: label %s not found", txt.Quote(f.Query))
|
2019-12-11 07:37:39 +01:00
|
|
|
|
2020-12-15 20:14:06 +01:00
|
|
|
s = s.Where("labels.label_name LIKE ?", likeString)
|
2019-12-11 07:37:39 +01:00
|
|
|
} else {
|
|
|
|
labelIds = append(labelIds, label.ID)
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
Db().Where("category_id = ?", label.ID).Find(&categories)
|
2019-12-11 07:37:39 +01:00
|
|
|
|
|
|
|
for _, category := range categories {
|
|
|
|
labelIds = append(labelIds, category.LabelID)
|
|
|
|
}
|
|
|
|
|
2020-05-03 18:00:50 +02:00
|
|
|
log.Infof("search: label %s includes %d categories", txt.Quote(label.LabelName), len(labelIds))
|
2019-12-11 07:37:39 +01:00
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Where("labels.id IN (?)", labelIds)
|
2019-12-11 07:37:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 19:03:12 +02:00
|
|
|
if f.Favorite {
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Where("labels.label_favorite = 1")
|
2019-12-11 07:37:39 +01:00
|
|
|
}
|
|
|
|
|
2019-12-17 07:13:09 +01:00
|
|
|
if !f.All {
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Where("labels.label_priority >= 0 OR labels.label_favorite = 1")
|
2019-12-11 07:37:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch f.Order {
|
|
|
|
case "slug":
|
2020-04-17 21:20:38 +02:00
|
|
|
s = s.Order("labels.label_favorite DESC, custom_slug ASC")
|
2019-12-11 07:37:39 +01:00
|
|
|
default:
|
2020-04-17 21:20:38 +02:00
|
|
|
s = s.Order("labels.label_favorite DESC, custom_slug ASC")
|
2019-12-11 07:37:39 +01:00
|
|
|
}
|
|
|
|
|
2020-07-01 09:49:03 +02:00
|
|
|
if f.Count > 0 && f.Count <= MaxResults {
|
2020-03-28 17:17:41 +01:00
|
|
|
s = s.Limit(f.Count).Offset(f.Offset)
|
2019-12-11 07:37:39 +01:00
|
|
|
} else {
|
2020-07-01 09:49:03 +02:00
|
|
|
s = s.Limit(MaxResults).Offset(f.Offset)
|
2019-12-11 07:37:39 +01:00
|
|
|
}
|
|
|
|
|
2020-03-28 17:17:41 +01:00
|
|
|
if result := s.Scan(&results); result.Error != nil {
|
2019-12-11 07:37:39 +01:00
|
|
|
return results, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|