Files
freeplanet_serverside/src/server/models/myelem.js

458 lines
9.6 KiB
JavaScript
Raw Normal View History

2022-10-27 11:22:58 +02:00
const mongoose = require('mongoose').set('debug', false)
const Schema = mongoose.Schema;
const tools = require('../tools/general');
const { ObjectId } = require('mongodb');
2022-10-27 11:22:58 +02:00
const { MySchedaSchema, IDimensioni, IImg, IText, IAreaDiStampa } = require('../models/myscheda');
2024-10-31 23:22:46 +01:00
2022-10-27 11:22:58 +02:00
mongoose.Promise = global.Promise;
mongoose.level = "F";
// Resolving error Unknown modifier: $pushAll
mongoose.plugin(schema => {
schema.options.usePushEach = true
});
2024-11-19 19:18:54 +01:00
const IElementiPagina = new Schema({
pagina: IDimensioni,
});
const myCard = new Schema(
{
imagefile: String,
vers_img: Number,
alt: String,
description: String,
style: String,
size: String,
color: String,
content: String,
colorsub: String,
link: String,
}
)
const animation = new Schema(
{
name: String,
clduration: String,
cldelay: String,
timingtype: String,
}
);
const elemText = new Schema(
{
text: String,
color: String,
class: String,
size: String,
anim: animation,
}
);
2024-10-31 23:22:46 +01:00
const catalogo = new Schema(
{
//++AddCATALOGO_FIELDS
productTypes: [{ type: Number }],
excludeproductTypes: [{ type: Number }],
editore: [{ type: String }],
argomenti: [{ type: String }],
2025-02-05 12:13:27 +01:00
idCollane: [{ type: Number }],
sort_field: { type: String },
sort_dir: { type: Number },
pdf: { type: Boolean },
2024-11-19 19:18:54 +01:00
pdf_filename: { type: String },
2024-10-26 17:11:52 +02:00
printable: { type: Boolean },
indebug: { type: Boolean },
2024-10-31 23:22:46 +01:00
first_page: IDimensioni,
last_page: IDimensioni,
areadistampa: IAreaDiStampa,
2024-11-19 19:18:54 +01:00
dimensioni_def: IElementiPagina,
2024-10-31 23:22:46 +01:00
// -------------------
arrSchede: [
{
scheda: MySchedaSchema,
2024-10-31 23:22:46 +01:00
order: { type: Number },
2024-11-28 16:05:00 +01:00
numPagineMax: { type: Number },
/*arrProdToShow: {
type: [[mongoose.Schema.Types.Mixed]], // Definizione tipo
select: false // Imposta il campo come non selezionabile
},*/
2024-10-31 23:22:46 +01:00
}
],
}
);
2022-10-27 11:22:58 +02:00
const MyElemSchema = new Schema({
idapp: {
type: String,
},
2022-11-10 19:33:23 +01:00
path: {
type: String,
},
2024-10-31 23:22:46 +01:00
oldpath: {
type: String,
},
idPage: { type: String },
2022-10-27 11:22:58 +02:00
type: {
2022-10-29 12:37:50 +02:00
type: Number,
2022-10-27 11:22:58 +02:00
},
2022-11-12 12:00:21 +01:00
img: {
2022-10-27 11:22:58 +02:00
type: String,
},
container: {
type: String,
},
2022-10-29 12:37:50 +02:00
container2: {
type: String,
},
container3: {
type: String,
},
container4: {
type: String,
},
2022-11-12 12:00:21 +01:00
align: {
type: Number,
},
2022-11-18 18:54:18 +01:00
vertalign: {
type: Number,
},
speed: {
type: Number,
},
2022-11-11 18:18:44 +01:00
parambool: {
type: Boolean,
},
2022-11-13 22:39:07 +01:00
span: {
type: Boolean,
},
2022-11-11 18:18:44 +01:00
parambool2: {
type: Boolean,
},
parambool3: {
type: Boolean,
},
2022-10-29 12:37:50 +02:00
number: {
type: Number,
},
num2: {
type: Number,
2022-10-29 12:37:50 +02:00
},
imgback: {
type: String,
},
ratio: {
type: String,
},
containerHtml: {
type: String,
},
2022-10-27 11:22:58 +02:00
size: {
type: String,
},
order: {
type: Number,
default: 0,
},
height: {
type: Number,
},
2022-10-29 12:37:50 +02:00
heightimg: {
2022-11-12 12:00:21 +01:00
type: String,
2022-10-29 12:37:50 +02:00
},
2024-10-23 01:41:18 +02:00
heightcarousel: {
type: String,
},
2022-10-29 12:37:50 +02:00
widthimg: {
2022-11-12 12:00:21 +01:00
type: String,
2022-10-29 12:37:50 +02:00
},
width: {
type: Number,
},
link: {
type: String,
},
2022-11-12 12:00:21 +01:00
fit: {
type: String,
},
2022-10-27 11:22:58 +02:00
onlyif_logged: {
type: Boolean,
},
color: {
type: String,
},
elemsText: [elemText],
anim: animation,
active: {
2022-10-27 11:22:58 +02:00
type: Boolean,
default: false,
2022-10-27 11:22:58 +02:00
},
class: {
type: String,
},
2022-11-13 22:39:07 +01:00
class2: {
type: String,
},
2022-11-17 08:09:48 +01:00
class3: {
type: String,
},
2022-11-18 18:54:18 +01:00
class4: {
type: String,
},
2022-10-29 12:37:50 +02:00
styleadd: {
type: String,
},
2022-11-13 22:39:07 +01:00
image: {
type: String,
},
vers_img: {
type: Number,
},
titleBanner: String,
classBanner: String,
listcards: [myCard],
catalogo: catalogo,
2022-10-29 12:37:50 +02:00
list: [
{
imagefile: {
type: String
},
vers_img: {
type: Number,
},
2022-10-29 12:37:50 +02:00
order: {
type: Number
},
alt: {
type: String
},
description: {
type: String
}
}
],
date_created: {
type: Date,
default: Date.now
},
date_updated: {
type: Date,
},
2022-10-27 11:22:58 +02:00
});
2022-11-17 08:09:48 +01:00
MyElemSchema.pre('save', async function (next) {
2022-11-10 19:33:23 +01:00
if (this.isNew) {
this._id = new ObjectId();
2022-11-10 19:33:23 +01:00
}
next();
});
2022-10-27 11:22:58 +02:00
MyElemSchema.statics.getFieldsForSearch = function () {
return [{ field: 'title', type: tools.FieldType.string },
2022-11-17 08:09:48 +01:00
{ field: 'content', type: tools.FieldType.string }]
2022-10-27 11:22:58 +02:00
};
MyElemSchema.statics.executeQueryTable = function (idapp, params, user) {
params.fieldsearch = this.getFieldsForSearch();
return tools.executeQueryTable(this, idapp, params, user);
};
2024-10-31 23:22:46 +01:00
MyElemSchema.statics.SetIdPageInsteadThePah = async function (idapp) {
const MyElem = this;
const { MyPage } = require('../models/mypage');
// Sostituisci path con IdPage
try {
// Recupera tutti i documenti di MyPage
const pages = await MyPage.find({ idapp }); // Puoi anche specificare condizioni, se necessario
// Utilizza una mappa per accoppiare i path con i loro Id
const pathToIdMap = {};
pages.forEach(page => {
pathToIdMap[page.path] = page._id; // Mappa il path all'ID del documento MyPage
});
// Aggiorna MyElem utilizzando la mappa
for (const [path, id] of Object.entries(pathToIdMap)) {
if (path) {
await MyElem.updateMany(
{ idapp },
{ path: path }, // Condizione per aggiornare dove il path corrisponde
{
$set: {
idPage: id,
oldpath: path,
},
$unset: { path: "" } // Rimuove il campo path
} // Imposta IdPage all'ID del documento corrispondente
);
}
2024-10-31 23:22:46 +01:00
}
if (false) {
// Utilizza una mappa per accoppiare i path con i loro Id
const pathToIdMap2 = {};
pages.forEach(page => {
pathToIdMap2[page.path] = page._id.toString(); // Mappa il path all'ID del documento MyPage
});
// Aggiorna MyElem utilizzando la mappa
for (const [path, id] of Object.entries(pathToIdMap2)) {
await MyElem.updateMany(
{ oldpath: path }, // Condizione per aggiornare dove il path corrisponde
{
$unset: { idPage: "" } // Rimuove il campo path
} // Imposta IdPage all'ID del documento corrispondente
);
}
for (const [oldpath, id] of Object.entries(pathToIdMap2)) {
await MyElem.updateMany(
{ oldpath: oldpath }, // Condizione per aggiornare dove il path corrisponde
{
$set: { idPage: id }
} // Imposta IdPage all'ID del documento corrispondente
);
}
}
const pathToIdMap2 = {};
pages.forEach(page => {
pathToIdMap2[page.path] = page._id.toString(); // Mappa il path all'ID del documento MyPage
});
for (const [oldpath, id] of Object.entries(pathToIdMap2)) {
await MyElem.updateMany(
{ idapp },
{ oldpath: oldpath }, // Condizione per aggiornare dove il path corrisponde
{
$set: { idPage: id }
} // Imposta IdPage all'ID del documento corrispondente
);
}
2024-10-31 23:22:46 +01:00
console.log('Aggiornamenti effettuati con successo.');
return 'Aggiornamenti effettuati con successo.';
} catch (error) {
console.error('Errore durante l\'aggiornamento:', error);
return 'Errore durante l\'aggiornamento:', error;
}
};
MyElemSchema.statics.deleteAllFromThisPage = async function (id) {
const MyElem = this;
return MyElem.deleteMany({ idPage: id });
};
2022-10-27 11:22:58 +02:00
MyElemSchema.statics.findAllIdApp = async function (idapp) {
const MyElem = this;
const myfind = { idapp };
2025-02-05 12:13:27 +01:00
const aggiorna = false;
let arrrec = null;
if (aggiorna) {
arrrec = await MyElem.find(myfind).sort({ order: 1 });
for (const elem of arrrec) {
if (elem.heightimg === 'NaNpx') {
elem.heightimg = '';
await elem.save();
}
}
} else {
arrrec = await MyElem.find(myfind).lean().sort({ order: 1 });
}
return arrrec;
2022-10-27 11:22:58 +02:00
};
MyElemSchema.statics.findallSchedeTemplate = async function (idapp) {
const MyElem = this;
try {
const ris = await MyElem.find({ idapp }).lean();
const schedeTemplate = ris.flatMap(elem =>
2025-02-05 12:13:27 +01:00
elem.catalogo && elem.catalogo.arrSchede ?
elem.catalogo.arrSchede
.filter(scheda => scheda.scheda?.isTemplate)
.map(scheda => ({
...scheda, // mantieni i dati originali della scheda
idPageOrig: elem.idPage // aggiungi l'idPage
2025-02-05 12:13:27 +01:00
}))
: []
);
return schedeTemplate;
} catch (e) {
console.error('Err', e);
}
};
// Ricerca tra tutte le schede, contenute in catalogo, se esiste un nome di template uguale,
// se non lo trova allora è quello giusto per crearne uno nuovo
MyElemSchema.statics.getNewFreeNameTemplate = async function (idapp, idPageOrig, nomeTemplate) {
const MyElem = this;
try {
const ris = await MyElem.find(
{
idapp,
'catalogo.arrSchede.scheda.isTemplate': true,
'catalogo.arrSchede.scheda.idPage': { $ne: idPageOrig }
},
{
'catalogo.arrSchede.scheda.name': 1,
'catalogo.arrSchede.scheda.isTemplate': 1,
'catalogo.arrSchede.scheda.idPage': 1
});
const existingNames = new Set(
ris.flatMap(elem =>
elem.catalogo?.arrSchede?.filter(scheda =>
scheda.scheda?.isTemplate &&
scheda.scheda?.idPage !== idPageOrig
)
.map(scheda => scheda.scheda?.name) || []
)
);
let ind = 2;
let newNameTemplate;
do {
newNameTemplate = `${nomeTemplate}_copia_${ind}`;
ind++;
} while (existingNames.has(newNameTemplate) && ind <= 1000);
return newNameTemplate;
} catch (e) {
console.error('Err', e);
throw e; // Propagate the error
}
};
2022-10-27 11:22:58 +02:00
const MyElem = mongoose.model('MyElem', MyElemSchema);
2023-12-09 11:55:58 +01:00
MyElem.createIndexes((err) => {
if (err) throw err;
});
2022-10-27 11:22:58 +02:00
module.exports = { MyElem };