Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@kohanajs/mod-cms

Package Overview
Dependencies
Maintainers
1
Versions
56
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@kohanajs/mod-cms - npm Package Compare versions

Comparing version 3.2.0 to 4.0.0

classes/helper/Page.js

9

CHANGELOG.md

@@ -5,2 +5,11 @@ # Changelog

## [4.0.0](https://gitlab.com/kohana-js/proposals/level0/mod-cms/compare/v3.2.0...v4.0.0) (2021-11-08)
### ⚠ BREAKING CHANGES
* remove not necessary classes, define page type in config file
* remove not necessary classes, define page type in config file ([7c15a3f](https://gitlab.com/kohana-js/proposals/level0/mod-cms/commit/7c15a3ffc4b3abc5e39b51e069fc8e53fcdc7d5e))
## [3.2.0](https://gitlab.com/kohana-js/proposals/level0/mod-cms/compare/v3.1.0...v3.2.0) (2021-11-04)

@@ -7,0 +16,0 @@

218

classes/controller/admin/Content.js

@@ -6,5 +6,2 @@ const {SQL} = require("@kohanajs/constants");

const Page = ORM.require('Page');
const PageType = ORM.require('PageType');
const PageValue = ORM.require('PageValue');
const PageAttribute = ORM.require('PageAttribute');
const PageItem = ORM.require('PageItem');

@@ -14,2 +11,4 @@ const PageItemValue = ORM.require('PageItemValue');

const HelperPage = require('../../helper/Page');
class ControllerAdminContent extends ControllerAdmin{

@@ -36,59 +35,8 @@ constructor(request, options = {}){

static arrayToObject(values, languageId= 1){
const result = {};
values.forEach(it => {
if(it.language_id && it.language_id !== languageId)return;
if(it.attribute_name){
result[it.name] = result[it.name] || {};
result[it.name][it.attribute_name] = it;
return;
}
result[it.name] = it;
})
return result
}
static itemsToObject(items, languageId=1){
const result = {}
items.forEach(x => {
result[x.name] = result[x.name] || [];
const obj = { _ : x };
x.page_item_values.forEach(y => {
if(y.language_id && y.language_id !== languageId)return;
if(y.attribute_name){
obj[y.name] = obj[y.name] || {};
obj[y.name][y.attribute_name] = y;
return;
}
obj[y.name] = y;
})
result[x.name].push(obj);
});
//sort items by weight;
Object.keys(result).forEach(key => {
result[key] = result[key].sort((a, b) => (a._.weight || 0) - (b._.weight || 0));
})
return result;
}
static blocksToObject(blocks){
//TODO, block
return {};
}
async assignDatabase(){
const draftDB = this.state.get(ControllerMixinDatabase.DATABASES).get('draft');
const database = this.state.get(ControllerMixinDatabase.DATABASES).get('pagetype');
const page_type = this.request.params['page_type'];
const pageType = await ORM.readWith(PageType, [['','name', SQL.LIKE, page_type]], {database});
return {
draftDB, page_type, pageType
draftDB, page_type
}

@@ -98,15 +46,13 @@ }

async action_index(){
const { draftDB, page_type, pageType } = await this.assignDatabase();
const { draftDB, page_type } = await this.assignDatabase();
if(!pageType)throw new Error(`page type ${page_type} not found`);
const instances = this.state.get('instances');
await ORM.eagerLoad(instances, {with: ['PageAttribute', 'PageValue']}, draftDB);
await ORM.eagerLoad(instances, {with: ['PageAttribute', 'PageValue']}, {database: draftDB});
instances.forEach(it => {
it.attributes = ControllerAdminContent.arrayToObject(it.page_attributes || []);
it.values = ControllerAdminContent.arrayToObject(it.page_values || []);
it.attributes = HelperPage.arrayToObject(it.page_attributes || []);
it.values = HelperPage.arrayToObject(it.page_values || []);
});
const items = instances.filter(it => it.page_type_id === pageType.id);
const items = instances.filter(it => it.page_type === page_type);

@@ -125,8 +71,8 @@ this.state.get(ControllerMixinView.LAYOUT).data.scripts = ['table-dd.js'];

async action_create_by_type(){
const { draftDB, page_type, pageType } = await this.assignDatabase();
const { draftDB, page_type } = await this.assignDatabase();
const insertID = ORMAdapter.defaultID();
const page = ORM.create(Page, {database: draftDB, insertID});
page.name = `Untitled ${pluralize.singular(pageType.name)}`;
page.page_type_id = pageType.id;
page.name = `Untitled ${pluralize.singular(page_type)}`;
page.page_type = page_type;
page.slug = String(insertID);

@@ -188,146 +134,4 @@ await page.write();

}
static async create(mapName, model, name, attribute_name, instance, languageId, database){
const item = ORM.create(model, {database});
item.name = name;
if(attribute_name)item.attribute_name = attribute_name;
if(languageId)item.language_id = languageId;
item.page_id = instance.id;
item.value = "";
await item.write();
return item;
}
static async createEmptyValue(name, attribute_name, instance, languageId, database){
await this.create('pageValueMap', PageValue, name, attribute_name, instance, languageId, database);
}
static async createEmptyAttribute(name, attribute_name, instance, database){
await this.create('pageAttributeMap', PageAttribute, name, attribute_name, instance, null, 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(subvalue)item.attribute_name = subvalue;
if(languageId)item.language_id = languageId;
item.page_item_id = instance.items[item_name][index]._.id;
item.value = "";
await item.write();
return item;
}
/**
*
* @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.toLowerCase()];
if(!content)throw new Error(`Creating undefined content by page type : ${pageType}`);
//prepare instance to check default value exists
if(!instance.page_values || !instance.page_attributes || !instance.page_items ){
await instance.eagerLoad({
with: ['PageValue', 'PageAttribute', 'PageItem'],
page_items: {
with: ['PageItemValue']
}
});
}
if(!instance.values || !instance.attributes || !instance.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);
}
await Promise.all(
content.attributes.map(async it => {
//it is from config file
if(typeof it === "string") {
if(instance.attributes[it])return;
return await this.createEmptyAttribute(it, null, instance, database);
}
const name = Object.keys(it)[0];
const subvalues = it[name];
await Promise.all(
subvalues.map(async attr => {
if(instance.attributes[name] && instance.attributes[name][attr])return;
await this.createEmptyAttribute(name, attr, instance, database)
})
);
})
)
await Promise.all(
content.values.map(async it => {
if(typeof it === "string") {
if(instance.values[it]) return;
return await this.createEmptyValue(it, null, instance, languageId, database);
}
const name = Object.keys(it)[0];
const subvalues = it[name];
await Promise.all(
subvalues.map(async attr => {
if(instance.values[name] && instance.values[name][attr])return;
await this.createEmptyValue(name, attr, instance, languageId, database)
})
);
})
);
await Promise.all(
Object.keys(content.items).map(async item_name => {
if(!instance.items[item_name]){
instance.items[item_name] = [];
const item = instance.items[item_name];
const firstItem = await ORM.readWith(PageItem, [['', 'page_id', SQL.EQUAL, instance.id], [SQL.AND, 'name', SQL.EQUAL, item_name], [SQL.AND, 'weight', SQL.EQUAL, 1]], {database});
if(!firstItem){
const firstItem = ORM.create(PageItem, {database});
firstItem.page_id = instance.id;
firstItem.name = item_name;
firstItem.weight = 1;
await firstItem.write();
item[0] = {_: firstItem};
}
}
await Promise.all(
content.items[item_name].map(async value => {
if(typeof value === "string") {
if(instance.items[item_name] && instance.items[item_name][0][value])return;
return await this.createEmptyItemValue(item_name, value, null, instance, languageId, database);
}
const valueName = Object.keys(value)[0];
await Promise.all(
value[valueName].map(async subvalue => {
//guard item value with attribute exist
if(
instance.items[item_name][0] &&
instance.items[item_name][0][valueName] &&
instance.items[item_name][0][valueName][subvalue]
)return;
await this.createEmptyItemValue(item_name, valueName, subvalue, instance, languageId, database)
})
);
})
)
})
);
}
}
module.exports = ControllerAdminContent;

@@ -7,6 +7,5 @@ const path = require('path');

const { ControllerMixinMultipartForm } = require('@kohanajs/mod-form');
const ControllerAdminContent = KohanaJS.require('controller/admin/Content');
const HelperPage = require('../../helper/Page');
const Page = ORM.require('Page');
const PageType = ORM.require('PageType');
const Language = ORM.require('Language');

@@ -19,3 +18,2 @@

databases: new Map([
['pagetype', `${KohanaJS.config.cms.databasePath}/pagetype.sqlite`],
['draft', `${KohanaJS.config.cms.databasePath}/content.sqlite`],

@@ -67,7 +65,5 @@ ['live', `${KohanaJS.config.cms.databasePath}/www/content.sqlite`],

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, database, this.languageId);
await HelperPage.defaultValue(instance, instance.page_type, database, this.languageId);

@@ -195,11 +191,9 @@ const $_POST = this.state.get(ControllerMixinMultipartForm.POST_DATA);

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.values = HelperPage.arrayToObject(page.page_values, this.languageId);
templateData.attributes = HelperPage.arrayToObject(page.page_attributes, this.languageId);
templateData.items = HelperPage.itemsToObject(page.page_items, this.languageId);
templateData.blocks = HelperPage.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';
const editTemplateFolder = page.page_type ?? 'default';
this.setTemplate(`templates/admin/page/page_types/${editTemplateFolder.toLowerCase()}/edit`, templateData);

@@ -213,7 +207,3 @@ }

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);
await HelperPage.defaultValue(page, page.page_type, database, this.languageId);
return;

@@ -230,5 +220,5 @@ }

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),
values : HelperPage.arrayToObject(page.page_values, masterLanguage.id),
items: HelperPage.itemsToObject(page.page_items, masterLanguage.id),
blocks: HelperPage.blocksToObject(page.blocks, masterLanguage.id),
}

@@ -240,5 +230,5 @@

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),
values : HelperPage.arrayToObject(page.page_values, this.languageId),
items: HelperPage.itemsToObject(page.page_items, this.languageId),
blocks: HelperPage.blocksToObject(page.blocks, this.languageId),
}

@@ -252,6 +242,6 @@

//create current language value
newValuePromises.push(ControllerAdminContent.createEmptyValue(key, null, page, this.languageId, database));
newValuePromises.push(HelperPage.createEmptyValue(key, null, page, this.languageId, database));
}else{
Object.keys(pageObject.values[key] || {}).forEach(subValue => {
newValuePromises.push(ControllerAdminContent.createEmptyValue(key, subValue, page, this.languageId, database));
newValuePromises.push(HelperPage.createEmptyValue(key, subValue, page, this.languageId, database));
});

@@ -270,3 +260,3 @@ }

newValuePromises.push(
ControllerAdminContent.createEmptyItemValue(itemKey, key, null, page, this.languageId, database, i)
HelperPage.createEmptyItemValue(itemKey, key, null, page, this.languageId, database, i)
);

@@ -279,3 +269,3 @@

newValuePromises.push(
ControllerAdminContent.createEmptyItemValue(itemKey, key, subValueName, page, this.languageId, database, i)
HelperPage.createEmptyItemValue(itemKey, key, subValueName, page, this.languageId, database, i)
);

@@ -282,0 +272,0 @@ })

@@ -10,3 +10,3 @@ const {ORM} = require('kohanajs');

end = null;
page_type_id = null;
page_type = null;
current_page_version_id = null;

@@ -24,7 +24,6 @@

["end", "Date"],
["page_type_id", "Int"],
["page_type", "String"],
["current_page_version_id", "Int"]
]);
static hasMany = [
["page_id", "PageVersion"],
["page_id", "PageAttribute"],

@@ -31,0 +30,0 @@ ["page_id", "PageValue"],

@@ -5,3 +5,2 @@ const {ORM} = require('kohanajs');

page_id = null;
page_version_id = null;
name = null;

@@ -20,4 +19,3 @@ attribute_name = null;

static belongsTo = new Map([
["page_id", "Page"],
["page_version_id", "PageVersion"]
["page_id", "Page"]
]);

@@ -24,0 +22,0 @@ }

@@ -5,4 +5,3 @@ const {ORM} = require('kohanajs');

page_id = null;
page_version_id = null;
weight = null;
weight = 0;

@@ -16,4 +15,3 @@ static joinTablePrefix = 'page_block';

static belongsTo = new Map([
["page_id", "Page"],
["page_version_id", "PageVersion"]
["page_id", "Page"]
]);

@@ -20,0 +18,0 @@ static hasMany = [

@@ -5,5 +5,4 @@ const {ORM} = require('kohanajs');

page_id = null;
page_version_id = null;
name = null;
weight = null;
weight = 0;

@@ -18,4 +17,3 @@ static joinTablePrefix = 'page_item';

static belongsTo = new Map([
["page_id", "Page"],
["page_version_id", "PageVersion"]
["page_id", "Page"]
]);

@@ -22,0 +20,0 @@ static hasMany = [

@@ -6,3 +6,2 @@ const {ORM} = require('kohanajs');

page_id = null;
page_version_id = null;
name = null;

@@ -22,4 +21,3 @@ attribute_name = null;

["language_id", "Language"],
["page_id", "Page"],
["page_version_id", "PageVersion"]
["page_id", "Page"]
]);

@@ -26,0 +24,0 @@ }

require('kohanajs').addNodeModule(__dirname);
const ControllerAdminBlock = require('./classes/controller/admin/Block');
const ControllerAdminBlockAttribute = require('./classes/controller/admin/BlockAttribute');
const ControllerAdminBlockField = require('./classes/controller/admin/BlockField');
const ControllerAdminInput = require('./classes/controller/admin/Input');
const ControllerAdminPage = require('./classes/controller/admin/Page');
const ControllerAdminPageField = require('./classes/controller/admin/PageField');
const ControllerAdminPageFieldCollection = require('./classes/controller/admin/PageFieldCollection');
const ControllerAdminPageFieldCollectionField = require('./classes/controller/admin/PageFieldCollectionField');
const ControllerAdminPageFieldType = require('./classes/controller/admin/PageFieldType');
const ControllerAdminPageFieldTypeAttribute = require('./classes/controller/admin/PageTypeAttribute');
const ControllerAdminPageType = require('./classes/controller/admin/PageType');
const ControllerAdminPageTypeAttribute = require('./classes/controller/admin/PageTypeAttribute');
const ControllerAdminContent = require('./classes/controller/admin/Content');
module.exports = {
ControllerAdminBlock,
ControllerAdminBlockAttribute,
ControllerAdminBlockField,
ControllerAdminInput,
ControllerAdminPage,
ControllerAdminPageField,
ControllerAdminPageFieldCollection,
ControllerAdminPageFieldCollectionField,
ControllerAdminPageFieldType,
ControllerAdminPageFieldTypeAttribute,
ControllerAdminPageType,
ControllerAdminPageTypeAttribute,
ControllerAdminContent,
};

@@ -9,14 +9,2 @@ const {KohanaJS} = require('kohanajs')

RouteCRUD.add('blocks', 'controller/admin/Block');
RouteCRUD.add('block_attributes', 'controller/admin/BlockAttributes');
RouteCRUD.add('block_fields', 'controller/admin/BlockFields');
RouteCRUD.add('inputs', 'controller/admin/Inputs');
RouteCRUD.add('page_fields', 'controller/admin/PageField');
RouteCRUD.add('page_field_collections', 'controller/admin/PageFieldCollection');
RouteCRUD.add('page_field_collection_fields', 'controller/admin/PageFieldCollectionField');
RouteCRUD.add('page_field_types', 'controller/admin/PageType');
RouteCRUD.add('page_field_type_attributes', 'controller/admin/PageType');
RouteCRUD.add('page_types', 'controller/admin/PageType');
RouteCRUD.add('page_type_attributes', 'controller/admin/PageTypeAttribute');
RouteList.add('/admin/pages/un-publish/:id', 'controller/admin/Page', 'unpublish');

@@ -23,0 +11,0 @@ RouteCRUD.add('pages', 'controller/admin/Page');

{
"name": "@kohanajs/mod-cms",
"version": "3.2.0",
"version": "4.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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc