@kohanajs/mod-cms
Advanced tools
Comparing version 2.3.0 to 3.0.0
const {SQL} = require("@kohanajs/constants"); | ||
const {ControllerMixinDatabase, ControllerMixinView, ControllerMixinMultipartForm, ORMAdapter, KohanaJS, ORM} = require("kohanajs"); | ||
const {ControllerMixinDatabase, ControllerMixinView, ORMAdapter, KohanaJS, ORM} = require("kohanajs"); | ||
const { ControllerMixinMultipartForm } = require('@kohanajs/mod-form'); | ||
const {ControllerAdmin} = require("@kohanajs/mod-admin"); | ||
@@ -12,3 +13,3 @@ const Page = ORM.require('Page'); | ||
const Language = ORM.require('Language'); | ||
const pluralize = require('pluralize') | ||
const pluralize = require('pluralize'); | ||
@@ -32,8 +33,4 @@ class ControllerAdminContent extends ControllerAdmin{ | ||
}); | ||
} | ||
static async getLanguage(request){ | ||
const database = this.state.get(ControllerMixinDatabase.DATABASES).get('draft'); | ||
const requestLanguage = request.params.language || this.state.get(ControllerMixinMultipartForm.GET_DATA).language || KohanaJS.config.cms.defaultLanguage | ||
const language = await ORM.readBy(Language, 'name', [ requestLanguage ], {database}); | ||
this.language = request.params.language || request.query.language || KohanaJS.config.cms.defaultLanguage; | ||
} | ||
@@ -57,3 +54,3 @@ | ||
static itemsToObject(items){ | ||
static itemsToObject(items, languageId=1){ | ||
const result = {} | ||
@@ -65,2 +62,4 @@ items.forEach(x => { | ||
x.page_item_values.forEach(y => { | ||
if(y.language_id && y.language_id !== languageId)return; | ||
if(y.attribute_name){ | ||
@@ -77,6 +76,5 @@ obj[y.name] = obj[y.name] || {}; | ||
//sort items by weight; | ||
Object.keys(result).forEach(key => { | ||
console.log(result[key]); | ||
result[key] = result[key].sort((a, b) => (a._.weight || 0) - (b._.weight || 0)); | ||
console.log(result[key]); | ||
}) | ||
@@ -88,2 +86,3 @@ | ||
static blocksToObject(blocks){ | ||
//TODO, block | ||
return {}; | ||
@@ -139,4 +138,2 @@ } | ||
await ControllerAdminContent.defaultValue(page, page_type, draftDB); | ||
await this.redirect(`/admin/pages/${page.id}`); | ||
@@ -174,8 +171,7 @@ } | ||
const newValue = ORM.create(PageItemValue, {database}); | ||
newValue.language = 1 | ||
newValue.page_item_id = newPageItem.id; | ||
newValue.language_id = itemValue.language_id; | ||
newValue.name = itemValue.name; | ||
newValue.attribute_name = itemValue.attribute_name; | ||
newValue.value = ''; | ||
await newValue.write(); | ||
@@ -217,8 +213,8 @@ }) | ||
static async createEmptyItemValue(item_name, value_name, attribute_name, instance, languageId, database){ | ||
static async createEmptyItemValue(item_name, value_name, subvalue, instance, languageId, database, index=0){ | ||
const item = ORM.create(PageItemValue, {database}); | ||
item.name = value_name; | ||
if(attribute_name)item.attribute_name = attribute_name; | ||
if(subvalue)item.attribute_name = subvalue; | ||
if(languageId)item.language_id = languageId; | ||
item.page_item_id = instance.items[item_name][0]._.id; | ||
item.page_item_id = instance.items[item_name][index]._.id; | ||
item.value = ""; | ||
@@ -229,5 +225,14 @@ await item.write(); | ||
static async defaultValue(instance, pageType, database){ | ||
/** | ||
* | ||
* @param {Page} instance | ||
* @param {string} pageType | ||
* @param database | ||
* @param {number} languageId | ||
* @returns {Promise<void>} | ||
*/ | ||
static async defaultValue(instance, pageType, database, languageId = 1){ | ||
const blueprint = KohanaJS.config.cms.blueprint || {}; | ||
const content = blueprint[pageType]; | ||
const content = blueprint[pageType.toLowerCase()]; | ||
if(!content)throw new Error(`Creating undefined content by page type : ${pageType}`); | ||
@@ -246,5 +251,5 @@ | ||
if(!instance.values || !instance.attributes || !instance.items ){ | ||
instance.values = this.arrayToObject(instance.page_values); | ||
instance.attributes = this.arrayToObject(instance.page_attributes) | ||
instance.items = this.itemsToObject(instance.page_items); | ||
instance.values = this.arrayToObject(instance.page_values, languageId); | ||
instance.attributes = this.arrayToObject(instance.page_attributes, languageId); | ||
instance.items = this.itemsToObject(instance.page_items, languageId); | ||
} | ||
@@ -284,3 +289,2 @@ | ||
subvalues.map(async attr => { | ||
console.log(instance.values[name]) | ||
if(instance.values[name] && instance.values[name][attr])return; | ||
@@ -334,3 +338,3 @@ await this.createEmptyValue(name, attr, instance, languageId, database) | ||
await Promise.all( | ||
value[valueName].map(async attr => { | ||
value[valueName].map(async subvalue => { | ||
//guard item value with attribute exist | ||
@@ -340,6 +344,6 @@ if( | ||
instance.items[item_name][0][valueName] && | ||
instance.items[item_name][0][valueName][attr] | ||
instance.items[item_name][0][valueName][subvalue] | ||
)return; | ||
await this.createEmptyItemValue(item_name, valueName, attr, instance, languageId, database) | ||
await this.createEmptyItemValue(item_name, valueName, subvalue, instance, languageId, database) | ||
}) | ||
@@ -346,0 +350,0 @@ ); |
const { ControllerAdmin } = require('@kohanajs/mod-admin'); | ||
const { ControllerMixinDatabase, ControllerMixinView, KohanaJS, ORM } = require('kohanajs'); | ||
const { ControllerMixinMultipartForm } = require('@kohanajs/mod-form'); | ||
const ControllerAdminContent = KohanaJS.require('controller/admin/Content'); | ||
@@ -7,2 +8,3 @@ | ||
const PageType = ORM.require('PageType'); | ||
const Language = ORM.require('Language'); | ||
@@ -20,13 +22,28 @@ class ControllerAdminPage extends ControllerAdmin { | ||
}); | ||
this.language = request.params.language || request.query.language || KohanaJS.config.cms.defaultLanguage; | ||
this.languageId = 1; | ||
} | ||
async before(){ | ||
const database = this.state.get(ControllerMixinDatabase.DATABASES).get('draft'); | ||
const language = await ORM.readBy(Language, 'name', [ this.language ], {database, limit:1 }); | ||
this.languageId = language.id; | ||
} | ||
async action_update() { | ||
//if no param id, create page proxy | ||
const database = this.state.get(ControllerMixinDatabase.DATABASES).get('draft'); | ||
const pageTypeDB = this.state.get(ControllerMixinDatabase.DATABASES).get('pagetype'); | ||
const instance = this.state.get('instance'); | ||
if(!instance)return; | ||
const database = this.state.get(ControllerMixinDatabase.DATABASES).get('draft'); | ||
const pageTypeDB = this.state.get(ControllerMixinDatabase.DATABASES).get('pagetype'); | ||
//find page type name | ||
const pageType = await ORM.factory(PageType, instance.page_type_id, {database: pageTypeDB}) | ||
await ControllerAdminContent.defaultValue(instance, pageType.name.toLowerCase(), database); | ||
await ControllerAdminContent.defaultValue(instance, pageType.name, database, this.languageId); | ||
const $_POST = this.state.get(ControllerMixinMultipartForm.POST_DATA); | ||
const destination = $_POST.destination || `/admin/pages/${instance.id}?language=${this.language}`; | ||
await this.redirect(destination); | ||
} | ||
@@ -36,24 +53,97 @@ | ||
const templateData = this.state.get(ControllerMixinView.TEMPLATE).data; | ||
const database = this.state.get(ControllerMixinDatabase.DATABASES).get('pagetype'); | ||
const draftDB = this.state.get(ControllerMixinDatabase.DATABASES).get('draft'); | ||
const page = this.state.get('instance'); | ||
const pagetypes = await ORM.readAll(PageType, {database, asArray:true}); | ||
templateData.pagetypes = pagetypes; | ||
pagetypes.forEach(it => { | ||
if(it.id === page.page_type_id)page.page_type = it; | ||
await page.eagerLoad({ | ||
with:["PageValue", "PageAttribute", "PageItem", "PageBlock"], | ||
page_items: {with: ['PageItemValue']}, | ||
page_blocks: {with: ['PageBlockAttribute', 'PageBlockValue']}, | ||
}); | ||
templateData.values = ControllerAdminContent.arrayToObject(page.page_values, this.languageId); | ||
templateData.attributes = ControllerAdminContent.arrayToObject(page.page_attributes, this.languageId); | ||
templateData.items = ControllerAdminContent.itemsToObject(page.page_items, this.languageId); | ||
templateData.blocks = ControllerAdminContent.blocksToObject(page.blocks, this.languageId); | ||
templateData.language = this.language; | ||
const database = this.state.get(ControllerMixinDatabase.DATABASES).get('pagetype'); | ||
const pageType = await ORM.factory(PageType, page.page_type_id, {database}) | ||
const editTemplateFolder = pageType.name ?? 'default'; | ||
this.setTemplate(`templates/admin/page/page_types/${editTemplateFolder.toLowerCase()}/edit`, templateData); | ||
} | ||
async syncMasterValues(){ | ||
const page = this.state.get('instance'); | ||
const database = this.state.get(ControllerMixinDatabase.DATABASES).get('draft'); | ||
if(this.language === KohanaJS.config.cms.defaultLanguage){ | ||
const pageType = await ORM.factory(PageType, page.page_type_id, { | ||
database: this.state.get(ControllerMixinDatabase.DATABASES).get('pagetype') | ||
}) | ||
await ControllerAdminContent.defaultValue(page, pageType.name, database, this.languageId); | ||
return; | ||
} | ||
const masterLanguage = await ORM.readBy(Language, 'name', [KohanaJS.config.cms.defaultLanguage], {database, limit:1 }); | ||
await page.eagerLoad({ | ||
with:["PageValue", "PageAttribute", "PageItem"], | ||
page_items: {with:['PageItemValue']} | ||
with: ['PageValue', 'PageItem', 'PageBlock'], | ||
page_items: {with: ['PageItemValue']}, | ||
page_blocks: {with: ['PageBlockValue']}, | ||
}) | ||
const masterPageObject = { | ||
values : ControllerAdminContent.arrayToObject(page.page_values, masterLanguage.id), | ||
items: ControllerAdminContent.itemsToObject(page.page_items, masterLanguage.id), | ||
blocks: ControllerAdminContent.blocksToObject(page.blocks, masterLanguage.id), | ||
} | ||
page.items = masterPageObject.items; | ||
page.blocks = masterPageObject.blocks; | ||
const pageObject = { | ||
values : ControllerAdminContent.arrayToObject(page.page_values, this.languageId), | ||
items: ControllerAdminContent.itemsToObject(page.page_items, this.languageId), | ||
blocks: ControllerAdminContent.blocksToObject(page.blocks, this.languageId), | ||
} | ||
const newValuePromises = []; | ||
Object.keys(masterPageObject.values).forEach(key => { | ||
if(pageObject.values[key])return; | ||
if(masterPageObject.values[key].id){ | ||
//create current language value | ||
newValuePromises.push(ControllerAdminContent.createEmptyValue(key, null, page, this.languageId, database)); | ||
}else{ | ||
Object.key(pageObject.values[key]).forEach(subValue => { | ||
newValuePromises.push(ControllerAdminContent.createEmptyValue(key, subValue, page, this.languageId, database)); | ||
}); | ||
} | ||
}); | ||
templateData.values = ControllerAdminContent.arrayToObject(page.page_values); | ||
templateData.attributes = ControllerAdminContent.arrayToObject(page.page_attributes); | ||
templateData.items = ControllerAdminContent.itemsToObject(page.page_items); | ||
templateData.blocks = ControllerAdminContent.blocksToObject(page.blocks); | ||
Object.keys(masterPageObject.items).forEach(itemKey => { | ||
masterPageObject.items[itemKey].forEach((item, i) =>{ | ||
Object.keys(item).forEach(key => { | ||
if(key === '_')return; | ||
if(item[key].id){ | ||
if(pageObject.items[itemKey][i][key])return; | ||
const editTemplateFolder = page.page_type?.name ?? 'default'; | ||
this.setTemplate(`templates/admin/page/page_types/${editTemplateFolder.toLowerCase()}/edit`, templateData); | ||
//create current language item value | ||
newValuePromises.push( | ||
ControllerAdminContent.createEmptyItemValue(itemKey, key, null, page, this.languageId, database, i) | ||
); | ||
}else{ | ||
const subValue = item[key]; | ||
Object.keys(subValue).forEach(subValueName => { | ||
if( pageObject.items[itemKey][i][key] && pageObject.items[itemKey][i][key][subValueName] )return; | ||
newValuePromises.push( | ||
ControllerAdminContent.createEmptyItemValue(itemKey, key, subValueName, page, this.languageId, database, i) | ||
); | ||
}) | ||
} | ||
}) | ||
}); | ||
}); | ||
await Promise.all(newValuePromises); | ||
} | ||
@@ -66,5 +156,7 @@ | ||
async action_read() { | ||
await this.syncMasterValues(); | ||
await this.setEditTemplate(); | ||
} | ||
async action_edit(){ | ||
await this.syncMasterValues(); | ||
await this.setEditTemplate(); | ||
@@ -71,0 +163,0 @@ } |
const {SQL} = require("@kohanajs/constants"); | ||
const {ControllerMixinDatabase, ControllerMixinView, ControllerMixinMultipartForm, ORMAdapter, KohanaJS, ORM} = require("kohanajs"); | ||
const {ControllerMixinDatabase, ControllerMixinView, ORMAdapter, KohanaJS, ORM} = require("kohanajs"); | ||
const { ControllerMixinMultipartForm } = require('@kohanajs/mod-form'); | ||
const {ControllerAdmin} = require("@kohanajs/mod-admin"); | ||
@@ -5,0 +6,0 @@ const Page = ORM.require('Page'); |
{ | ||
"name": "@kohanajs/mod-cms", | ||
"version": "2.3.0", | ||
"version": "3.0.0", | ||
"description": "The CMS module for KohanaJS", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59989
47
1515