2022-12-28 13:27:14 +01:00
|
|
|
export const DirectoriesSchema = {
|
|
|
|
|
name: 'Directories',
|
|
|
|
|
primaryKey: 'id',
|
|
|
|
|
properties: {
|
|
|
|
|
id: 'int',
|
|
|
|
|
name: 'string',
|
2023-08-22 10:32:40 +02:00
|
|
|
parent: {type: 'int', optional: true},
|
2022-12-28 13:27:14 +01:00
|
|
|
order: 'int',
|
|
|
|
|
needAuthorization: 'int',
|
|
|
|
|
},
|
|
|
|
|
};
|
2022-10-06 10:28:41 +02:00
|
|
|
class DirectoryModel {
|
|
|
|
|
constructor(realm) {
|
|
|
|
|
this.schema = 'Directories';
|
|
|
|
|
this.realm = realm;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
add(data) {
|
|
|
|
|
this.realm.write(() => {
|
|
|
|
|
this.realm.create(this.schema, data);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
update(data) {
|
|
|
|
|
this.realm.write(() => {
|
|
|
|
|
this.realm.create(this.schema, data, 'modified');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get(id) {
|
|
|
|
|
let tmp = this.realm.objects(this.schema);
|
|
|
|
|
if (tmp.length === 0) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
tmp = tmp.filtered('id = ' + id)[0];
|
|
|
|
|
}
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 17:20:07 +02:00
|
|
|
getAll(role, authorizedFolders, format, lang) {
|
|
|
|
|
if (authorizedFolders !== null && authorizedFolders !== undefined) {
|
|
|
|
|
authorizedFolders = JSON.parse(authorizedFolders);
|
|
|
|
|
}
|
|
|
|
|
let tmp = this.realm.objects(this.schema);
|
|
|
|
|
if (role === 'USER') {
|
|
|
|
|
if (authorizedFolders.length > 0) {
|
|
|
|
|
tmp = tmp.filtered(
|
|
|
|
|
'needAuthorization = 0 OR ' +
|
2023-08-22 10:32:40 +02:00
|
|
|
authorizedFolders
|
|
|
|
|
.map(folder => ' id = "' + folder.id + '"')
|
|
|
|
|
.join(' OR '),
|
2022-10-06 17:20:07 +02:00
|
|
|
);
|
|
|
|
|
} else {
|
2023-08-22 10:32:40 +02:00
|
|
|
tmp = tmp.filtered('needAuthorization = 0');
|
2022-10-06 17:20:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (format) {
|
|
|
|
|
return this._recursiveDir(tmp, lang);
|
|
|
|
|
}
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 10:28:41 +02:00
|
|
|
clear() {
|
|
|
|
|
this.realm.write(() => {
|
|
|
|
|
this.realm.delete(this.realm.objects(this.schema));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 17:20:07 +02:00
|
|
|
_recursiveDir(list, lang, parent = null) {
|
|
|
|
|
let root = list.filter(e => e.parent === parent);
|
|
|
|
|
let tmp = [];
|
|
|
|
|
for (let i = 0; i < root.length; i++) {
|
|
|
|
|
let childs = this._recursiveDir(list, lang, root[i].id);
|
|
|
|
|
let name = JSON.parse(root[i].name);
|
|
|
|
|
tmp[i] = {};
|
|
|
|
|
tmp[i].id = root[i].id;
|
|
|
|
|
tmp[i].name = this.getI18nName(lang, name);
|
|
|
|
|
tmp[i].items = [];
|
|
|
|
|
for (let j = 0; j < childs.length; j++) {
|
|
|
|
|
childs[j].value = childs[j].id;
|
|
|
|
|
tmp[i].items.push(childs[j]);
|
|
|
|
|
}
|
|
|
|
|
if (!tmp[i].items.length) {
|
|
|
|
|
delete tmp[i].items;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getI18nName(lang, name) {
|
2023-08-22 10:32:40 +02:00
|
|
|
let tmp;
|
2022-10-06 17:20:07 +02:00
|
|
|
switch (lang) {
|
2023-08-22 10:32:40 +02:00
|
|
|
case 'fr':
|
2022-10-06 17:20:07 +02:00
|
|
|
tmp = name.fr;
|
|
|
|
|
break;
|
2023-08-22 10:32:40 +02:00
|
|
|
case 'en':
|
2022-10-06 17:20:07 +02:00
|
|
|
tmp = name.en;
|
|
|
|
|
break;
|
2023-08-22 10:32:40 +02:00
|
|
|
case 'de':
|
2022-10-06 17:20:07 +02:00
|
|
|
tmp = name.de;
|
|
|
|
|
break;
|
2023-08-22 10:32:40 +02:00
|
|
|
case 'es':
|
2022-10-06 17:20:07 +02:00
|
|
|
tmp = name.es;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (tmp === undefined) {
|
|
|
|
|
tmp = name.fr;
|
|
|
|
|
}
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
2022-10-06 10:28:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default DirectoryModel;
|