nfp_sites/old/page/model.mjs

126 lines
2.6 KiB
JavaScript
Raw Normal View History

2021-02-05 11:50:01 +00:00
import { createPrototype, safeColumns } from '../knex.mjs'
2019-09-13 13:33:10 +00:00
import Media from '../media/model.mjs'
2021-02-05 11:50:01 +00:00
// import Staff from '../staff/model.mjs'
// import Article from '../article/model.mjs'
2019-09-13 13:33:10 +00:00
/*
Page model:
{
filename,
filetype,
small_image,
medium_image,
large_image,
*small_url,
*medium_url,
*large_url,
size,
staff_id,
is_deleted,
created_at,
updated_at,
}
*/
2021-02-05 11:50:01 +00:00
function PageItem(data) {
Object.assign(this, data)
this.children = []
}
function Page() {
this.tableName = 'pages'
this.Model = PageItem
this.includes = {
media: Media.includeHasOne('pages.media_id', 'id'),
banner: Media.includeHasOne('pages.banner_id', 'id'),
}
this.publicFields = this.privateFields = safeColumns([
'staff_id',
'parent_id',
'name',
'path',
'description',
'banner_id',
'media_id',
])
this.init()
}
2019-09-13 13:33:10 +00:00
2021-02-05 11:50:01 +00:00
Page.prototype = createPrototype({
/* includes: {
staff: Staff.includeHasOne('staff_id', 'id'),
}, */
/*banner() {
2019-09-13 13:33:10 +00:00
return this.belongsTo(Media, 'banner_id')
},
parent() {
return this.belongsTo(Page, 'parent_id')
},
children() {
return this.hasManyFiltered(Page, 'children', 'parent_id')
.query(qb => {
qb.orderBy('name', 'ASC')
})
2019-09-13 13:33:10 +00:00
},
news() {
return this.hasManyFiltered(Article, 'news', 'parent_id')
.query(qb => {
qb.orderBy('id', 'desc')
})
},
media() {
return this.belongsTo(Media, 'media_id')
},
staff() {
return this.belongsTo(Staff, 'staff_id')
2021-02-05 11:50:01 +00:00
},*/
getSingle(id, includes = [], require = true, ctx = null) {
return this._getSingle(qb => {
qb.where(subq => {
subq.where(this.tableName + '.id', '=', Number(id) || 0)
.orWhere(this.tableName + '.path', '=', id)
2019-09-13 13:33:10 +00:00
})
2021-02-05 11:50:01 +00:00
}, includes, require, ctx)
2019-09-13 13:33:10 +00:00
},
2021-02-05 11:50:01 +00:00
async getTree() {
let items = await this.getAllQuery(this.query(
qb => qb.orderBy('name', 'ASC'),
[],
['parent_id', 'id', 'name', 'path']
))
let out = []
let map = new Map()
for (let i = 0; i < items.length; i++) {
if (!items[i].parent_id) {
out.push(items[i])
}
map.set(items[i].id, items[i])
}
for (let i = 0; i < items.length; i++) {
if (items[i].parent_id && map.has(items[i].parent_id)) {
map.get(items[i].parent_id).children.push(items[i])
}
}
return out
2019-10-01 03:45:44 +00:00
},
2019-09-13 13:33:10 +00:00
})
2021-02-05 11:50:01 +00:00
const pageInstance = new Page()
pageInstance.addInclude('children', pageInstance.includeHasMany('parent_id', 'pages.id'))
pageInstance.addInclude('parent', pageInstance.includeHasOne('pages.parent_id', 'id'))
export default pageInstance