Skip to content

Commit 05c452a

Browse files
committed
Add courseasset check on clone item, fixes #2347
1 parent cdf463e commit 05c452a

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

frontend/src/core/helpers.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,28 @@ define(function(require){
342342
'<span class="max-fileupload-size">',
343343
Origin.l10n.t('app.maxfileuploadsize', {size: Origin.constants.humanMaxFileUploadSize}),
344344
'</span>'].join(''))
345+
},
346+
347+
flattenNestedProperties: function(properties) {
348+
var flatProperties = {};
349+
if (typeof properties !== 'undefined') {
350+
for (var key in properties) {
351+
// Check for nested properties
352+
if (typeof properties[key] === 'object') {
353+
for (var innerKey in properties[key]) {
354+
// Check if key already exists
355+
if (flatProperties[innerKey]) {
356+
flatProperties[key+'.'+innerKey] = properties[key][innerKey];
357+
} else {
358+
flatProperties[innerKey] = properties[key][innerKey];
359+
}
360+
}
361+
} else {
362+
flatProperties[key] = properties[key];
363+
}
364+
}
365+
}
366+
return flatProperties;
345367
}
346368
};
347369

frontend/src/modules/scaffold/views/scaffoldListView.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
define([
22
'core/origin',
3+
'core/helpers',
4+
'core/models/courseAssetModel',
35
'backbone-forms',
46
'backbone-forms-lists'
5-
], function(Origin, BackboneForms) {
7+
], function(Origin, Helpers, CourseAssetModel, BackboneForms) {
68

79
var ScaffoldListView = Backbone.Form.editors.List.extend({
810
defaultValue: [],
@@ -92,9 +94,42 @@ define([
9294
},
9395

9496
cloneItem: function(event) {
97+
var flatItem = Helpers.flattenNestedProperties(this.editor.value);
98+
var itemValues = _.values(flatItem);
99+
var parentAttributes = Origin.scaffold.getCurrentModel().attributes;
100+
_.each(itemValues, function(item) {
101+
if (typeof item === 'string' && item.indexOf('course/assets') !== -1) {
102+
var itemFileName = item.substring(item.lastIndexOf('/')+1);
103+
$.ajax({
104+
url: 'api/asset/query',
105+
type:'GET',
106+
data: {search: { filename: itemFileName }},
107+
success: function (result) {
108+
(new CourseAssetModel()).save({
109+
_courseId : Origin.editor.data.course.get('_id'),
110+
_contentType : parentAttributes._type,
111+
_contentTypeId : parentAttributes._id,
112+
_fieldName : itemFileName,
113+
_assetId : result[0]._id,
114+
_contentTypeParentId: parentAttributes._parentId
115+
}, {
116+
error: function(error) {
117+
Origin.Notify.alert({
118+
type: 'error',
119+
text: Origin.l10n.t('app.errorsaveasset')
120+
});
121+
}
122+
});
123+
},
124+
error: function() {
125+
Origin.Notify.alert({ type: 'error', text: Origin.l10n.t('app.errorduplication') });
126+
}
127+
});
128+
}
129+
});
130+
95131
this.list.addItem(this.editor.value, true);
96132
}
97-
98133
});
99134

100135
Origin.on('origin:dataReady', function() {

0 commit comments

Comments
 (0)