2020-02-04 05:18:22 +01:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-05-30 14:52:47 +02:00
|
|
|
"fmt"
|
2020-02-04 05:18:22 +01:00
|
|
|
|
2020-06-01 09:45:24 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2020-02-04 05:18:22 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/form"
|
|
|
|
)
|
|
|
|
|
2022-02-27 17:32:54 +01:00
|
|
|
// SelectedPhotos finds photos based on the given selection form, e.g. for adding them to an album.
|
|
|
|
func SelectedPhotos(f form.Selection) (results entity.Photos, err error) {
|
2020-02-04 05:18:22 +01:00
|
|
|
if f.Empty() {
|
2020-05-25 19:10:44 +02:00
|
|
|
return results, errors.New("no items selected")
|
2020-02-04 05:18:22 +01:00
|
|
|
}
|
|
|
|
|
2022-05-16 23:59:28 +02:00
|
|
|
// Resolve photos in smart albums.
|
|
|
|
if photoIds, err := AlbumsPhotoUIDs(f.Albums, false, false); err != nil {
|
|
|
|
log.Warnf("query: %s", err.Error())
|
|
|
|
} else if len(photoIds) > 0 {
|
|
|
|
f.Photos = append(f.Photos, photoIds...)
|
|
|
|
}
|
|
|
|
|
2020-05-30 14:52:47 +02:00
|
|
|
var concat string
|
2020-02-04 05:18:22 +01:00
|
|
|
|
2020-05-30 14:52:47 +02:00
|
|
|
switch DbDialect() {
|
|
|
|
case MySQL:
|
|
|
|
concat = "CONCAT(a.path, '/%')"
|
2021-12-09 07:47:23 +01:00
|
|
|
case SQLite3:
|
2020-05-30 14:52:47 +02:00
|
|
|
concat = "a.path || '/%'"
|
|
|
|
default:
|
|
|
|
return results, fmt.Errorf("unknown sql dialect: %s", DbDialect())
|
|
|
|
}
|
|
|
|
|
|
|
|
where := fmt.Sprintf(`photos.photo_uid IN (?)
|
2020-05-29 12:56:24 +02:00
|
|
|
OR photos.place_id IN (?)
|
2020-05-25 19:10:44 +02:00
|
|
|
OR photos.photo_uid IN (SELECT photo_uid FROM files WHERE file_uid IN (?))
|
2020-05-26 07:14:50 +02:00
|
|
|
OR photos.photo_path IN (
|
|
|
|
SELECT a.path FROM folders a WHERE a.folder_uid IN (?) UNION
|
2020-05-30 14:52:47 +02:00
|
|
|
SELECT b.path FROM folders a JOIN folders b ON b.path LIKE %s WHERE a.folder_uid IN (?))
|
2020-06-14 11:39:53 +02:00
|
|
|
OR photos.photo_uid IN (SELECT photo_uid FROM photos_albums WHERE hidden = 0 AND album_uid IN (?))
|
2021-09-17 14:26:12 +02:00
|
|
|
OR photos.id IN (SELECT f.photo_id FROM files f JOIN %s m ON f.file_uid = m.file_uid WHERE f.deleted_at IS NULL AND m.subj_uid IN (?))
|
2022-10-31 09:58:39 +01:00
|
|
|
OR photos.id IN (SELECT pl.photo_id FROM photos_labels pl JOIN labels l ON pl.label_id = l.id AND pl.uncertainty < 100 AND l.deleted_at IS NULL WHERE l.label_uid IN (?))
|
|
|
|
OR photos.id IN (SELECT pl.photo_id FROM photos_labels pl JOIN categories c ON c.label_id = pl.label_id AND pl.uncertainty < 100 JOIN labels lc ON lc.id = c.category_id AND lc.deleted_at IS NULL WHERE lc.label_uid IN (?))`,
|
2021-09-17 14:26:12 +02:00
|
|
|
concat, entity.Marker{}.TableName())
|
2020-05-30 14:52:47 +02:00
|
|
|
|
|
|
|
s := UnscopedDb().Table("photos").
|
|
|
|
Select("photos.*").
|
2021-09-17 14:26:12 +02:00
|
|
|
Where(where, f.Photos, f.Places, f.Files, f.Files, f.Files, f.Albums, f.Subjects, f.Labels, f.Labels)
|
2020-02-04 05:18:22 +01:00
|
|
|
|
2020-05-25 19:10:44 +02:00
|
|
|
if result := s.Scan(&results); result.Error != nil {
|
|
|
|
return results, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|