New function Scholar.Collection.changeParent([parentCollectionID]) -- move a collection into another collection in the DB
parentCollectionID is optional and defaults to moving item to root Returns true on success, false on attempt to move collection into its existing parent, itself or a descendent collection; throws exception on invalid parentCollectionID Sends a columnTree notify() with previous parent ID, id of collection itself, and new parent ID (unless the previous or new are the root, in which case it's omitted) -- that may or may not make sense for the interface code and can be changed if needed
This commit is contained in:
parent
b31be7af7e
commit
9675ac9d01
1 changed files with 60 additions and 0 deletions
|
@ -1047,6 +1047,66 @@ Scholar.Collection.prototype.rename = function(name){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Change the parentCollectionID of a collection
|
||||||
|
*
|
||||||
|
* Returns TRUE on success, FALSE on error
|
||||||
|
**/
|
||||||
|
Scholar.Collection.prototype.changeParent = function(parent){
|
||||||
|
if (!parent){
|
||||||
|
parent = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var previousParent = this.getParent();
|
||||||
|
|
||||||
|
if (parent==previousParent){
|
||||||
|
Scholar.debug('Collection ' + this.getID() + ' is already in '
|
||||||
|
+ (parent ? 'collection ' + parent : 'root collection'), 2);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parent && !Scholar.Collections.get(parent)){
|
||||||
|
throw('Invalid parentCollectionID ' + parent + ' in changeParent()');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parent && parent==this.getID()){
|
||||||
|
Scholar.debug('Cannot move collection into itself!', 2);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parent){
|
||||||
|
var descendents = this._getDescendents();
|
||||||
|
for (var i=0, len=descendents.length; i<len; i++){
|
||||||
|
if (descendents[i]['isCollection'] && parent==descendents[i]['id']){
|
||||||
|
Scholar.debug('Cannot move collection into one of its own '
|
||||||
|
+ 'descendents!', 2);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var parentParam = parent ? {'int':parent} : {'null':true};
|
||||||
|
|
||||||
|
var sql = "UPDATE collections SET parentCollectionID=? "
|
||||||
|
+ "WHERE collectionID=?";
|
||||||
|
|
||||||
|
Scholar.DB.query(sql, [parentParam, {'int':this.getID()}]);
|
||||||
|
this._parent = parent;
|
||||||
|
|
||||||
|
var notifyIDs = [this.getID()];
|
||||||
|
if (previousParent){
|
||||||
|
notifyIDs.push(previousParent);
|
||||||
|
}
|
||||||
|
if (parent){
|
||||||
|
notifyIDs.push(parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
Scholar.Notifier.trigger('modify', 'collection', notifyIDs);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add an item to the collection
|
* Add an item to the collection
|
||||||
**/
|
**/
|
||||||
|
|
Loading…
Reference in a new issue