2017-07-24 16:24:21 +01:00
|
|
|
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
|
|
|
//
|
|
|
|
// This software (Documize Community Edition) is licensed under
|
|
|
|
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
|
|
|
//
|
|
|
|
// You can operate outside the AGPL restrictions by purchasing
|
|
|
|
// Documize Enterprise Edition and obtaining a commercial license
|
|
|
|
// by contacting <sales@documize.com>.
|
|
|
|
//
|
|
|
|
// https://documize.com
|
|
|
|
|
|
|
|
package document
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
"github.com/documize/community/core/env"
|
2017-07-24 16:24:21 +01:00
|
|
|
"github.com/documize/community/core/streamutil"
|
|
|
|
"github.com/documize/community/domain"
|
2017-07-26 20:03:23 +01:00
|
|
|
"github.com/documize/community/model/doc"
|
2017-07-24 16:24:21 +01:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
// Scope provides data access to MySQL.
|
|
|
|
type Scope struct {
|
|
|
|
Runtime *env.Runtime
|
|
|
|
}
|
|
|
|
|
2017-07-26 20:03:23 +01:00
|
|
|
// Get fetches the document record with the given id fromt the document table and audits that it has been got.
|
|
|
|
func (s Scope) Get(ctx domain.RequestContext, id string) (document doc.Document, err error) {
|
|
|
|
stmt, err := s.Runtime.Db.Preparex("SELECT id, refid, orgid, labelid, userid, job, location, title, excerpt, slug, tags, template, layout, created, revised FROM document WHERE orgid=? and refid=?")
|
|
|
|
defer streamutil.Close(stmt)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "prepare select document")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = stmt.Get(&document, ctx.OrgID, id)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "execute select document")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-24 16:24:21 +01:00
|
|
|
// MoveDocumentSpace changes the label for client's organization's documents which have space "id", to "move".
|
2017-07-26 10:50:26 +01:00
|
|
|
func (s Scope) MoveDocumentSpace(ctx domain.RequestContext, id, move string) (err error) {
|
|
|
|
stmt, err := ctx.Transaction.Preparex("UPDATE document SET labelid=? WHERE orgid=? AND labelid=?")
|
2017-07-24 16:24:21 +01:00
|
|
|
defer streamutil.Close(stmt)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, fmt.Sprintf("prepare document space move %s", id))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
_, err = stmt.Exec(move, ctx.OrgID, id)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, fmt.Sprintf("execute document space move %s", id))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2017-07-26 20:03:23 +01:00
|
|
|
|
|
|
|
// PublicDocuments returns a slice of SitemapDocument records, holding documents in folders of type 1 (entity.TemplateTypePublic).
|
|
|
|
func (s Scope) PublicDocuments(ctx domain.RequestContext, orgID string) (documents []doc.SitemapDocument, err error) {
|
|
|
|
err = s.Runtime.Db.Select(&documents,
|
|
|
|
`SELECT d.refid as documentid, d.title as document, d.revised as revised, l.refid as folderid, l.label as folder
|
|
|
|
FROM document d LEFT JOIN label l ON l.refid=d.labelid
|
|
|
|
WHERE d.orgid=?
|
|
|
|
AND l.type=1
|
|
|
|
AND d.template=0`, orgID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, fmt.Sprintf("execute GetPublicDocuments for org %s%s", orgID))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|