2006-03-14 11:48:36 +00:00
|
|
|
/*
|
2006-05-18 11:25:10 +00:00
|
|
|
* Constructor for Item object
|
2006-03-14 11:48:36 +00:00
|
|
|
*
|
2006-05-18 11:25:10 +00:00
|
|
|
* Generally should be called through Scholar.Items rather than directly
|
2006-03-14 11:48:36 +00:00
|
|
|
*/
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.Item = function(){
|
2006-03-14 11:48:36 +00:00
|
|
|
this._init();
|
|
|
|
|
2006-06-01 06:35:33 +00:00
|
|
|
// Accept itemTypeID in constructor
|
2006-03-14 11:48:36 +00:00
|
|
|
if (arguments.length){
|
|
|
|
this.setType(arguments[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.Item.prototype._init = function(){
|
2006-03-14 11:48:36 +00:00
|
|
|
//
|
|
|
|
// Public members for access by public methods -- do not access directly
|
|
|
|
//
|
|
|
|
this._data = new Array();
|
|
|
|
this._creators = new Scholar.Hash();
|
2006-05-18 11:25:10 +00:00
|
|
|
this._itemData = new Array();
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
this._creatorsLoaded = false;
|
2006-05-18 11:25:10 +00:00
|
|
|
this._itemDataLoaded = false;
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
this._changed = new Scholar.Hash();
|
|
|
|
this._changedCreators = new Scholar.Hash();
|
2006-05-18 11:25:10 +00:00
|
|
|
this._changedItemData = new Scholar.Hash();
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
2006-03-17 05:11:03 +00:00
|
|
|
|
2006-05-15 19:30:18 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2006-05-18 11:25:10 +00:00
|
|
|
// Public Scholar.Item methods
|
2006-05-15 19:30:18 +00:00
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2006-03-14 11:48:36 +00:00
|
|
|
/*
|
2006-05-18 11:25:10 +00:00
|
|
|
* Check if the specified field is a primary field from the items table
|
2006-03-14 11:48:36 +00:00
|
|
|
*/
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.Item.prototype.isPrimaryField = function(field){
|
2006-06-01 00:22:18 +00:00
|
|
|
// Create primaryFields hash array if not yet created
|
2006-05-18 11:25:10 +00:00
|
|
|
if (!Scholar.Item.primaryFields){
|
|
|
|
Scholar.Item.primaryFields = Scholar.DB.getColumnHash('items');
|
|
|
|
Scholar.Item.primaryFields['firstCreator'] = true;
|
2006-06-25 05:43:00 +00:00
|
|
|
Scholar.Item.primaryFields['numNotes'] = true;
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
return !!Scholar.Item.primaryFields[field];
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.Item.editableFields = {
|
2006-06-01 00:22:18 +00:00
|
|
|
title: true
|
2006-03-14 11:48:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if the specified primary field can be changed with setField()
|
|
|
|
*/
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.Item.prototype.isEditableField = function(field){
|
|
|
|
return !!Scholar.Item.editableFields[field];
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build object from database
|
|
|
|
*/
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.Item.prototype.loadFromID = function(id){
|
2006-06-25 05:43:00 +00:00
|
|
|
// Should be the same as query in Scholar.Items.loadFromID, just
|
|
|
|
// without itemID clause
|
2006-06-06 17:37:52 +00:00
|
|
|
var sql = 'SELECT I.*, lastName || '
|
|
|
|
+ 'CASE ((SELECT COUNT(*) FROM itemCreators WHERE itemID=' + id + ')>1) '
|
2006-06-25 05:43:00 +00:00
|
|
|
+ "WHEN 0 THEN '' ELSE ' et al.' END AS firstCreator, "
|
2006-06-27 19:29:05 +00:00
|
|
|
+ "(SELECT COUNT(*) FROM itemNotes WHERE sourceItemID=I.itemID) AS numNotes "
|
2006-05-18 11:25:10 +00:00
|
|
|
+ 'FROM items I '
|
|
|
|
+ 'LEFT JOIN itemCreators IC ON (I.itemID=IC.itemID) '
|
|
|
|
+ 'LEFT JOIN creators C ON (IC.creatorID=C.creatorID) '
|
2006-05-18 21:55:43 +00:00
|
|
|
+ 'WHERE itemID=' + id
|
2006-06-01 00:22:18 +00:00
|
|
|
+ ' AND (IC.orderIndex=0 OR IC.orderIndex IS NULL)'; // first creator
|
2006-03-20 21:47:22 +00:00
|
|
|
var row = Scholar.DB.rowQuery(sql);
|
2006-03-14 11:48:36 +00:00
|
|
|
this.loadFromRow(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2006-05-18 11:25:10 +00:00
|
|
|
* Populate basic item data from a database row
|
2006-03-14 11:48:36 +00:00
|
|
|
*/
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.Item.prototype.loadFromRow = function(row){
|
2006-03-14 11:48:36 +00:00
|
|
|
this._init();
|
|
|
|
for (col in row){
|
2006-06-01 00:22:18 +00:00
|
|
|
// Only accept primary field data through loadFromRow()
|
|
|
|
if (this.isPrimaryField(col)){
|
2006-03-14 11:48:36 +00:00
|
|
|
this._data[col] = row[col];
|
|
|
|
}
|
2006-06-01 00:22:18 +00:00
|
|
|
else {
|
|
|
|
Scholar.debug(col + ' is not a valid primary field');
|
|
|
|
}
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if any data fields have changed since last save
|
|
|
|
*/
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.Item.prototype.hasChanged = function(){
|
2006-03-14 11:48:36 +00:00
|
|
|
return (this._changed.length || this._changedCreators.length ||
|
2006-05-18 11:25:10 +00:00
|
|
|
this._changedItemData.length);
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.Item.prototype.getID = function(){
|
|
|
|
return this._data['itemID'] ? this._data['itemID'] : false;
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.Item.prototype.getType = function(){
|
|
|
|
return this._data['itemTypeID'] ? this._data['itemTypeID'] : false;
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2006-05-18 11:25:10 +00:00
|
|
|
* Set or change the item's type
|
2006-03-14 11:48:36 +00:00
|
|
|
*/
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.Item.prototype.setType = function(itemTypeID){
|
|
|
|
if (itemTypeID==this.getType()){
|
2006-03-14 11:48:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If existing type, clear fields from old type that aren't in new one
|
|
|
|
if (this.getType()){
|
2006-05-18 11:25:10 +00:00
|
|
|
var sql = 'SELECT fieldID FROM itemTypeFields '
|
|
|
|
+ 'WHERE itemTypeID=' + this.getType() + ' AND fieldID NOT IN '
|
|
|
|
+ '(SELECT fieldID FROM itemTypeFields WHERE itemTypeID='
|
|
|
|
+ itemTypeID + ')';
|
2006-03-20 21:47:22 +00:00
|
|
|
var obsoleteFields = Scholar.DB.columnQuery(sql);
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
if (obsoleteFields){
|
|
|
|
for (var i=0; i<obsoleteFields.length; i++){
|
|
|
|
this.setField(obsoleteFields[i],false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
this._data['itemTypeID'] = itemTypeID;
|
|
|
|
this._changed.set('itemTypeID');
|
2006-03-14 11:48:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-01 00:22:18 +00:00
|
|
|
/**
|
|
|
|
* Return an array of collectionIDs for all collections the item belongs to
|
|
|
|
**/
|
|
|
|
Scholar.Item.prototype.getCollections = function(){
|
|
|
|
return Scholar.DB.columnQuery("SELECT collectionID FROM collectionItems "
|
|
|
|
+ "WHERE itemID=" + this.getID());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-01 20:03:53 +00:00
|
|
|
/**
|
|
|
|
* Determine whether the item belongs to a given collectionID
|
|
|
|
**/
|
|
|
|
Scholar.Item.prototype.inCollection = function(collectionID){
|
|
|
|
return !!parseInt(Scholar.DB.valueQuery("SELECT COUNT(*) collectionID "
|
|
|
|
+ "FROM collectionItems WHERE collectionID=" + collectionID + " AND "
|
|
|
|
+ "itemID=" + this.getID()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-14 11:48:36 +00:00
|
|
|
/*
|
2006-05-18 11:25:10 +00:00
|
|
|
* Returns the number of creators for this item
|
2006-03-14 11:48:36 +00:00
|
|
|
*/
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.Item.prototype.numCreators = function(){
|
2006-03-14 11:48:36 +00:00
|
|
|
if (this.getID() && !this._creatorsLoaded){
|
|
|
|
this._loadCreators();
|
|
|
|
}
|
|
|
|
return this._creators.length;
|
|
|
|
}
|
|
|
|
|
2006-06-01 00:22:18 +00:00
|
|
|
|
2006-03-14 11:48:36 +00:00
|
|
|
/*
|
|
|
|
* Returns an array of the creator data at the given position, or false if none
|
|
|
|
*/
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.Item.prototype.getCreator = function(pos){
|
2006-03-14 11:48:36 +00:00
|
|
|
if (this.getID() && !this._creatorsLoaded){
|
|
|
|
this._loadCreators();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._creators.items[pos]){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return this._creators.items[pos];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set or update the creator at the specified position
|
|
|
|
*/
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.Item.prototype.setCreator = function(orderIndex, firstName, lastName, creatorTypeID){
|
2006-03-14 11:48:36 +00:00
|
|
|
if (this.getID() && !this._creatorsLoaded){
|
|
|
|
this._loadCreators();
|
|
|
|
}
|
|
|
|
|
2006-05-20 15:18:36 +00:00
|
|
|
if (!firstName){
|
|
|
|
firstName = '';
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
2006-05-20 15:18:36 +00:00
|
|
|
if (!lastName){
|
|
|
|
lastName = '';
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
2006-06-08 00:16:35 +00:00
|
|
|
// If creator at this position hasn't changed, cancel
|
2006-03-14 11:48:36 +00:00
|
|
|
if (this._creators.has(orderIndex) &&
|
|
|
|
this._creators.get(orderIndex)['firstName']==firstName &&
|
|
|
|
this._creators.get(orderIndex)['lastName']==lastName &&
|
|
|
|
this._creators.get(orderIndex)['creatorTypeID']==creatorTypeID){
|
2006-06-08 00:16:35 +00:00
|
|
|
return false;
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
2006-05-20 15:18:36 +00:00
|
|
|
if (!creatorTypeID){
|
|
|
|
creatorTypeID = 1;
|
|
|
|
}
|
|
|
|
|
2006-03-14 11:48:36 +00:00
|
|
|
var creator = new Array();
|
|
|
|
creator['firstName'] = firstName;
|
|
|
|
creator['lastName'] = lastName;
|
|
|
|
creator['creatorTypeID'] = creatorTypeID;
|
|
|
|
|
|
|
|
this._creators.set(orderIndex, creator);
|
|
|
|
this._changedCreators.set(orderIndex);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove a creator and shift others down
|
|
|
|
*/
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.Item.prototype.removeCreator = function(orderIndex){
|
2006-03-14 11:48:36 +00:00
|
|
|
if (this.getID() && !this._creatorsLoaded){
|
|
|
|
this._loadCreators();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._creators.has(orderIndex)){
|
|
|
|
throw ('No creator exists at position ' + orderIndex);
|
|
|
|
}
|
|
|
|
this._creators.remove(orderIndex);
|
|
|
|
|
2006-05-20 15:18:36 +00:00
|
|
|
// Go to length+1 so we clear the last one
|
|
|
|
for (var i=orderIndex, max=this._creators.length+1; i<max; i++){
|
2006-03-14 11:48:36 +00:00
|
|
|
var next =
|
|
|
|
this._creators.items[i+1] ? this._creators.items[i+1] : false;
|
|
|
|
this._creators.set(i, next);
|
|
|
|
this._changedCreators.set(i);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-08 00:16:35 +00:00
|
|
|
Scholar.Item.prototype.creatorExists = function(firstName, lastName, creatorTypeID, skipIndex){
|
|
|
|
for (var j=0, len=this.numCreators(); j<len; j++){
|
|
|
|
if (typeof skipIndex!='undefined' && skipIndex==j){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var creator2 = this.getCreator(j);
|
|
|
|
|
|
|
|
if (firstName==creator2['firstName'] &&
|
|
|
|
lastName==creator2['lastName'] &&
|
|
|
|
creatorTypeID==creator2['creatorTypeID']){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-14 11:48:36 +00:00
|
|
|
/*
|
2006-05-18 11:25:10 +00:00
|
|
|
* Retrieves (and loads from DB, if necessary) an itemData field value
|
2006-03-14 11:48:36 +00:00
|
|
|
*
|
|
|
|
* Field can be passed as fieldID or fieldName
|
|
|
|
*/
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.Item.prototype.getField = function(field){
|
|
|
|
//Scholar.debug('Requesting field ' + field + ' for item ' + this.getID(), 4);
|
2006-03-14 11:48:36 +00:00
|
|
|
if (this.isPrimaryField(field)){
|
2006-03-17 05:11:03 +00:00
|
|
|
return this._data[field] ? this._data[field] : '';
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
else {
|
2006-05-18 11:25:10 +00:00
|
|
|
if (this.getID() && !this._itemDataLoaded){
|
|
|
|
this._loadItemData();
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
var fieldID = Scholar.ItemFields.getID(field);
|
2006-03-14 11:48:36 +00:00
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
return this._itemData[fieldID] ? this._itemData[fieldID] : '';
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2006-05-18 11:25:10 +00:00
|
|
|
* Set a field value, loading existing itemData first if necessary
|
2006-03-14 11:48:36 +00:00
|
|
|
*
|
|
|
|
* Field can be passed as fieldID or fieldName
|
|
|
|
*/
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.Item.prototype.setField = function(field, value, loadIn){
|
2006-03-14 11:48:36 +00:00
|
|
|
// Primary field
|
|
|
|
if (this.isPrimaryField(field)){
|
2006-03-22 18:16:29 +00:00
|
|
|
if (!this.isEditableField(field)){
|
2006-03-14 11:48:36 +00:00
|
|
|
throw ('Primary field ' + field + ' cannot be changed through ' +
|
|
|
|
'setField');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._data[field] && this._data[field]==value){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this._data[field] = value;
|
|
|
|
this._changed.set(field);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Type-specific field
|
|
|
|
else {
|
|
|
|
if (!this.getType()){
|
2006-05-18 11:25:10 +00:00
|
|
|
throw ('Item type must be set before setting field data.');
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
// If existing item, load field data first unless we're already in
|
2006-03-14 11:48:36 +00:00
|
|
|
// the middle of a load
|
2006-05-18 11:25:10 +00:00
|
|
|
if (this.getID() && !loadIn && !this._itemDataLoaded){
|
|
|
|
this._loadItemData();
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
var fieldID = Scholar.ItemFields.getID(field);
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
if (!fieldID){
|
2006-05-18 11:25:10 +00:00
|
|
|
throw (field + ' is not a valid itemData field.');
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
if (!Scholar.ItemFields.isValidForType(fieldID, this.getType())){
|
2006-03-14 11:48:36 +00:00
|
|
|
throw (field + ' is not a valid field for this type.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// If existing value, make sure it's actually changing
|
2006-06-08 21:08:50 +00:00
|
|
|
if ((!this._itemData[fieldID] && !value) ||
|
|
|
|
(this._itemData[fieldID] && this._itemData[fieldID]==value)){
|
2006-03-14 11:48:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
2006-05-18 11:25:10 +00:00
|
|
|
this._itemData[fieldID] = value;
|
2006-03-14 11:48:36 +00:00
|
|
|
if (!loadIn){
|
2006-05-18 11:25:10 +00:00
|
|
|
this._changedItemData.set(fieldID);
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-01 22:19:21 +00:00
|
|
|
|
2006-03-14 11:48:36 +00:00
|
|
|
/*
|
|
|
|
* Save changes back to database
|
|
|
|
*/
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.Item.prototype.save = function(){
|
2006-03-14 11:48:36 +00:00
|
|
|
if (!this.hasChanged()){
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.debug('Item ' + this.getID() + ' has not changed', 4);
|
2006-03-14 11:48:36 +00:00
|
|
|
return !!this.getID();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2006-05-18 11:25:10 +00:00
|
|
|
// Existing item, update
|
2006-03-14 11:48:36 +00:00
|
|
|
//
|
|
|
|
if (this.getID()){
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.debug('Updating database with new item data', 4);
|
2006-03-14 11:48:36 +00:00
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
var itemID = this.getID();
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
try {
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.DB.beginTransaction();
|
2006-03-14 11:48:36 +00:00
|
|
|
|
2006-06-25 04:11:19 +00:00
|
|
|
// Begin history transaction
|
|
|
|
Scholar.History.begin('modify-item', this.getID());
|
|
|
|
|
2006-03-14 11:48:36 +00:00
|
|
|
//
|
|
|
|
// Primary fields
|
|
|
|
//
|
2006-06-25 04:11:19 +00:00
|
|
|
Scholar.History.modify('items', 'itemID', this.getID());
|
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
var sql = "UPDATE items SET ";
|
2006-03-14 11:48:36 +00:00
|
|
|
var sql2;
|
2006-06-02 07:03:24 +00:00
|
|
|
var sqlValues = [];
|
2006-03-14 11:48:36 +00:00
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
if (this._changed.has('itemTypeID')){
|
2006-06-02 07:03:24 +00:00
|
|
|
sql += "itemTypeID=?, ";
|
|
|
|
sqlValues.push({'int':this.getField('itemTypeID')});
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
if (this._changed.has('title')){
|
2006-06-02 07:03:24 +00:00
|
|
|
sql += "title=?, ";
|
|
|
|
sqlValues.push({'string':this.getField('title')});
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Always update modified time
|
|
|
|
sql += "dateModified=CURRENT_TIMESTAMP ";
|
2006-06-08 00:16:35 +00:00
|
|
|
sql += "WHERE itemID=?";
|
2006-06-02 07:03:24 +00:00
|
|
|
sqlValues.push({'int':this.getID()});
|
2006-03-14 11:48:36 +00:00
|
|
|
|
2006-06-02 07:03:24 +00:00
|
|
|
Scholar.DB.query(sql, sqlValues);
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Creators
|
|
|
|
//
|
|
|
|
if (this._changedCreators.length){
|
2006-06-08 00:16:35 +00:00
|
|
|
for (var i=0, len=this.numCreators(); i<len; i++){
|
|
|
|
var creator = this.getCreator(i);
|
|
|
|
if (this.creatorExists(creator['firstName'],
|
|
|
|
creator['lastName'], creator['creatorTypeID'], i)){
|
|
|
|
throw('Cannot add duplicate creator/creatorType '
|
|
|
|
+ 'to item ' + this.getID());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-14 11:48:36 +00:00
|
|
|
for (orderIndex in this._changedCreators.items){
|
|
|
|
Scholar.debug('Creator ' + orderIndex + ' has changed', 4);
|
|
|
|
|
|
|
|
var creator = this.getCreator(orderIndex);
|
|
|
|
|
2006-06-08 00:16:35 +00:00
|
|
|
// Delete at position
|
2006-06-25 04:11:19 +00:00
|
|
|
Scholar.History.remove('itemCreators', 'itemID-orderIndex',
|
|
|
|
[this.getID(), orderIndex]);
|
|
|
|
|
2006-06-08 00:16:35 +00:00
|
|
|
sql2 = 'DELETE FROM itemCreators'
|
|
|
|
+ ' WHERE itemID=' + this.getID()
|
|
|
|
+ ' AND orderIndex=' + orderIndex;
|
|
|
|
Scholar.DB.query(sql2);
|
|
|
|
|
|
|
|
// If empty, move on
|
2006-03-14 11:48:36 +00:00
|
|
|
if (!creator['firstName'] && !creator['lastName']){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if this is an existing creator
|
2006-03-21 15:19:11 +00:00
|
|
|
var creatorID = Scholar.Creators.getID(
|
2006-03-14 11:48:36 +00:00
|
|
|
creator['firstName'],
|
2006-05-20 15:18:36 +00:00
|
|
|
creator['lastName']
|
2006-03-14 11:48:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// If not, add it
|
|
|
|
if (!creatorID){
|
2006-03-21 15:19:11 +00:00
|
|
|
creatorID = Scholar.Creators.add(
|
2006-03-14 11:48:36 +00:00
|
|
|
creator['firstName'],
|
2006-05-20 15:18:36 +00:00
|
|
|
creator['lastName']
|
2006-03-14 11:48:36 +00:00
|
|
|
);
|
2006-06-25 04:11:19 +00:00
|
|
|
Scholar.History.add('creators', 'creatorID', creatorID);
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
2006-06-25 04:11:19 +00:00
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
sql2 = 'SELECT COUNT(*) FROM itemCreators'
|
|
|
|
+ ' WHERE itemID=' + this.getID()
|
2006-06-08 00:16:35 +00:00
|
|
|
+ ' AND creatorID=' + creatorID
|
|
|
|
+ ' AND creatorTypeID=' + creator['creatorTypeID'];
|
2006-03-14 11:48:36 +00:00
|
|
|
|
2006-06-25 04:11:19 +00:00
|
|
|
// If this creator and creatorType exists elsewhere, move it
|
2006-03-20 21:47:22 +00:00
|
|
|
if (Scholar.DB.valueQuery(sql2)){
|
2006-06-25 04:11:19 +00:00
|
|
|
Scholar.History.modify('itemCreators',
|
|
|
|
'itemID-creatorID-creatorTypeID',
|
|
|
|
[this.getID(), creatorID, creator['creatorTypeID']]);
|
|
|
|
|
2006-06-08 00:16:35 +00:00
|
|
|
sql = 'UPDATE itemCreators SET orderIndex=? '
|
|
|
|
+ "WHERE itemID=? AND creatorID=? AND "
|
|
|
|
+ "creatorTypeID=?";
|
2006-06-02 07:03:24 +00:00
|
|
|
|
|
|
|
sqlValues = [
|
2006-06-08 00:16:35 +00:00
|
|
|
{'int':orderIndex},
|
2006-06-02 07:03:24 +00:00
|
|
|
{'int':this.getID()},
|
2006-06-08 00:16:35 +00:00
|
|
|
{'int':creatorID},
|
|
|
|
{'int':creator['creatorTypeID']}
|
2006-06-02 07:03:24 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
Scholar.DB.query(sql, sqlValues);
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
2006-06-25 04:11:19 +00:00
|
|
|
|
2006-05-20 15:18:36 +00:00
|
|
|
// Otherwise insert
|
2006-03-14 11:48:36 +00:00
|
|
|
else {
|
2006-06-02 16:37:17 +00:00
|
|
|
sql = "INSERT INTO itemCreators VALUES (?,?,?,?)";
|
2006-06-02 07:03:24 +00:00
|
|
|
|
|
|
|
sqlValues = [
|
|
|
|
{'int':itemID},
|
|
|
|
{'int':creatorID},
|
|
|
|
{'int':creator['creatorTypeID']},
|
|
|
|
{'int':orderIndex}
|
|
|
|
];
|
|
|
|
|
|
|
|
Scholar.DB.query(sql, sqlValues);
|
2006-06-25 04:11:19 +00:00
|
|
|
|
|
|
|
Scholar.History.add('itemCreators',
|
|
|
|
'itemID-creatorID-creatorTypeID',
|
|
|
|
[this.getID(), creatorID, creator['creatorTypeID']]);
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-23 22:00:39 +00:00
|
|
|
// Delete obsolete creators
|
2006-06-25 04:11:19 +00:00
|
|
|
var deleted;
|
|
|
|
if (deleted = Scholar.Creators.purge()){
|
|
|
|
for (var i in deleted){
|
|
|
|
// Add purged creators to history
|
|
|
|
Scholar.History.remove('creators', 'creatorID', i);
|
|
|
|
}
|
|
|
|
}
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2006-05-18 11:25:10 +00:00
|
|
|
// ItemData
|
2006-03-14 11:48:36 +00:00
|
|
|
//
|
2006-05-18 11:25:10 +00:00
|
|
|
if (this._changedItemData.length){
|
2006-03-14 11:48:36 +00:00
|
|
|
var del = new Array();
|
2006-05-18 11:25:10 +00:00
|
|
|
for (fieldID in this._changedItemData.items){
|
2006-03-14 11:48:36 +00:00
|
|
|
if (this.getField(fieldID)){
|
|
|
|
// Oh, for an INSERT...ON DUPLICATE KEY UPDATE
|
2006-05-18 11:25:10 +00:00
|
|
|
sql2 = 'SELECT COUNT(*) FROM itemData '
|
|
|
|
+ 'WHERE itemID=' + this.getID()
|
2006-03-14 11:48:36 +00:00
|
|
|
+ ' AND fieldID=' + fieldID;
|
|
|
|
|
2006-06-25 04:11:19 +00:00
|
|
|
// Update
|
2006-03-20 21:47:22 +00:00
|
|
|
if (Scholar.DB.valueQuery(sql2)){
|
2006-06-02 07:03:24 +00:00
|
|
|
sqlValues = [];
|
|
|
|
|
2006-06-25 04:11:19 +00:00
|
|
|
Scholar.History.modify('itemData', 'itemID-fieldID',
|
|
|
|
[this.getID(), fieldID]);
|
|
|
|
|
2006-06-02 07:03:24 +00:00
|
|
|
sql = "UPDATE itemData SET value=?";
|
2006-03-14 11:48:36 +00:00
|
|
|
// Take advantage of SQLite's manifest typing
|
2006-05-18 11:25:10 +00:00
|
|
|
if (Scholar.ItemFields.isInteger(fieldID)){
|
2006-06-02 07:03:24 +00:00
|
|
|
sqlValues.push({'int':this.getField(fieldID)});
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
else {
|
2006-06-02 07:03:24 +00:00
|
|
|
sqlValues.push({'string':this.getField(fieldID)});
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
2006-06-02 16:37:17 +00:00
|
|
|
sql += " WHERE itemID=? AND fieldID=?";
|
2006-06-02 07:03:24 +00:00
|
|
|
|
|
|
|
sqlValues.push(
|
|
|
|
{'int':this.getID()},
|
|
|
|
{'int':fieldID}
|
|
|
|
);
|
|
|
|
|
|
|
|
Scholar.DB.query(sql, sqlValues);
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
2006-06-25 04:11:19 +00:00
|
|
|
|
|
|
|
// Insert
|
2006-03-14 11:48:36 +00:00
|
|
|
else {
|
2006-06-25 04:11:19 +00:00
|
|
|
Scholar.History.add('itemData', 'itemID-fieldID',
|
|
|
|
[this.getID(), fieldID]);
|
|
|
|
|
2006-06-02 16:37:17 +00:00
|
|
|
sql = "INSERT INTO itemData VALUES (?,?,?)";
|
|
|
|
|
2006-06-02 07:03:24 +00:00
|
|
|
sqlValues = [
|
|
|
|
{'int':this.getID()},
|
|
|
|
{'int':fieldID},
|
|
|
|
];
|
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
if (Scholar.ItemFields.isInteger(fieldID)){
|
2006-06-02 07:03:24 +00:00
|
|
|
sqlValues.push({'int':this.getField(fieldID)});
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
else {
|
2006-06-02 07:03:24 +00:00
|
|
|
sqlValues.push({'string':this.getField(fieldID)});
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
2006-06-02 07:03:24 +00:00
|
|
|
|
|
|
|
Scholar.DB.query(sql, sqlValues);
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
}
|
2006-06-25 04:11:19 +00:00
|
|
|
|
2006-03-14 11:48:36 +00:00
|
|
|
// If field changed and is empty, mark row for deletion
|
|
|
|
else {
|
|
|
|
del.push(fieldID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete blank fields
|
|
|
|
if (del.length){
|
2006-06-25 04:11:19 +00:00
|
|
|
// Add to history
|
|
|
|
for (var i in del){
|
|
|
|
Scholar.History.remove('itemData', 'itemID-fieldID',
|
|
|
|
[this.getID(), del[i]]);
|
|
|
|
}
|
|
|
|
|
2006-06-02 07:03:24 +00:00
|
|
|
sql = 'DELETE from itemData '
|
2006-05-18 11:25:10 +00:00
|
|
|
+ 'WHERE itemID=' + this.getID() + ' '
|
2006-06-02 16:37:17 +00:00
|
|
|
+ 'AND fieldID IN (' + del.join() + ")";
|
2006-06-02 07:03:24 +00:00
|
|
|
Scholar.DB.query(sql);
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-25 04:11:19 +00:00
|
|
|
Scholar.History.commit();
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.DB.commitTransaction();
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
catch (e){
|
2006-06-25 04:11:19 +00:00
|
|
|
Scholar.History.cancel();
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.DB.rollbackTransaction();
|
2006-05-18 21:55:43 +00:00
|
|
|
throw(e);
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2006-05-18 11:25:10 +00:00
|
|
|
// New item, insert and return id
|
2006-03-14 11:48:36 +00:00
|
|
|
//
|
|
|
|
else {
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.debug('Saving data for new item to database');
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
var isNew = true;
|
|
|
|
var sqlColumns = new Array();
|
|
|
|
var sqlValues = new Array();
|
|
|
|
|
|
|
|
//
|
|
|
|
// Primary fields
|
|
|
|
//
|
2006-05-18 11:25:10 +00:00
|
|
|
sqlColumns.push('itemTypeID');
|
|
|
|
sqlValues.push({'int':this.getField('itemTypeID')});
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
if (this._changed.has('title')){
|
|
|
|
sqlColumns.push('title');
|
|
|
|
sqlValues.push({'string':this.getField('title')});
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.DB.beginTransaction();
|
2006-03-14 11:48:36 +00:00
|
|
|
|
2006-06-25 04:11:19 +00:00
|
|
|
// Begin history transaction
|
|
|
|
// No associated id yet, so we use false
|
|
|
|
Scholar.History.begin('add-item', false);
|
|
|
|
|
2006-03-14 11:48:36 +00:00
|
|
|
//
|
2006-06-25 04:11:19 +00:00
|
|
|
// Primary fields
|
2006-03-14 11:48:36 +00:00
|
|
|
//
|
2006-05-18 11:25:10 +00:00
|
|
|
var sql = "INSERT INTO items (" + sqlColumns.join() + ')'
|
2006-03-14 11:48:36 +00:00
|
|
|
+ ' VALUES (';
|
|
|
|
// Insert placeholders for bind parameters
|
|
|
|
for (var i=0; i<sqlValues.length; i++){
|
|
|
|
sql += '?,';
|
|
|
|
}
|
2006-06-08 00:16:35 +00:00
|
|
|
sql = sql.substring(0,sql.length-1) + ")";
|
2006-03-14 11:48:36 +00:00
|
|
|
|
2006-05-18 21:55:43 +00:00
|
|
|
// Save basic data to items table and get new ID
|
2006-05-18 11:25:10 +00:00
|
|
|
var itemID = Scholar.DB.query(sql,sqlValues);
|
2006-05-18 21:55:43 +00:00
|
|
|
this._data['itemID'] = itemID;
|
2006-03-14 11:48:36 +00:00
|
|
|
|
2006-06-25 04:11:19 +00:00
|
|
|
Scholar.History.setAssociatedID(itemID);
|
|
|
|
Scholar.History.add('items', 'itemID', itemID);
|
|
|
|
|
|
|
|
//
|
|
|
|
// ItemData
|
|
|
|
//
|
2006-05-18 11:25:10 +00:00
|
|
|
if (this._changedItemData.length){
|
|
|
|
for (fieldID in this._changedItemData.items){
|
2006-05-18 21:55:43 +00:00
|
|
|
if (!this.getField(fieldID)){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-06-02 07:03:24 +00:00
|
|
|
// TODO: update DB methods so that this can be
|
|
|
|
// implemented as a prepared statement that gets
|
|
|
|
// called multiple times
|
2006-06-02 16:37:17 +00:00
|
|
|
sql = "INSERT INTO itemData VALUES (?,?,?)";
|
2006-06-02 07:03:24 +00:00
|
|
|
|
2006-06-02 16:37:17 +00:00
|
|
|
sqlValues = [
|
2006-06-02 07:03:24 +00:00
|
|
|
{'int':itemID},
|
|
|
|
{'int':fieldID}
|
2006-06-02 16:37:17 +00:00
|
|
|
];
|
2006-06-02 07:03:24 +00:00
|
|
|
|
|
|
|
if (Scholar.ItemFields.isInteger(fieldID)){
|
|
|
|
sqlValues.push({'int':this.getField(fieldID)});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sqlValues.push({'string':this.getField(fieldID)});
|
|
|
|
}
|
2006-06-02 16:37:17 +00:00
|
|
|
|
2006-06-02 07:03:24 +00:00
|
|
|
Scholar.DB.query(sql, sqlValues);
|
2006-06-25 04:11:19 +00:00
|
|
|
|
|
|
|
Scholar.History.add('itemData', 'itemID-fieldID',
|
2006-06-25 05:26:53 +00:00
|
|
|
[itemID, fieldID]);
|
2006-05-18 21:55:43 +00:00
|
|
|
}
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
2006-06-01 07:29:39 +00:00
|
|
|
//
|
|
|
|
// Creators
|
|
|
|
//
|
|
|
|
if (this._changedCreators.length){
|
|
|
|
for (orderIndex in this._changedCreators.items){
|
|
|
|
var creator = this.getCreator(orderIndex);
|
|
|
|
|
|
|
|
// If empty, skip
|
|
|
|
if (!creator['firstName'] && !creator['lastName']){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if this is an existing creator
|
|
|
|
var creatorID = Scholar.Creators.getID(
|
|
|
|
creator['firstName'],
|
|
|
|
creator['lastName']
|
|
|
|
);
|
|
|
|
|
|
|
|
// If not, add it
|
|
|
|
if (!creatorID){
|
|
|
|
creatorID = Scholar.Creators.add(
|
|
|
|
creator['firstName'],
|
|
|
|
creator['lastName']
|
|
|
|
);
|
2006-06-25 04:11:19 +00:00
|
|
|
Scholar.History.add('creators', 'creatorID', creatorID);
|
2006-06-01 07:29:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sql = 'INSERT INTO itemCreators VALUES ('
|
|
|
|
+ itemID + ',' + creatorID + ','
|
|
|
|
+ creator['creatorTypeID'] + ', ' + orderIndex
|
2006-06-08 00:16:35 +00:00
|
|
|
+ ")";
|
2006-06-01 07:29:39 +00:00
|
|
|
Scholar.DB.query(sql);
|
2006-06-25 04:11:19 +00:00
|
|
|
|
|
|
|
Scholar.History.add('itemCreators',
|
|
|
|
'itemID-creatorID-creatorTypeID',
|
|
|
|
[this.getID(), creatorID, creator['creatorTypeID']]);
|
2006-06-01 07:29:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-25 04:11:19 +00:00
|
|
|
Scholar.History.commit();
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.DB.commitTransaction();
|
2006-05-18 21:55:43 +00:00
|
|
|
|
2006-06-01 00:22:18 +00:00
|
|
|
// Reload collection to update isEmpty,
|
|
|
|
// in case this was the first item in a collection
|
|
|
|
Scholar.Collections.reloadAll();
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
catch (e){
|
2006-06-25 04:11:19 +00:00
|
|
|
Scholar.History.cancel();
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.DB.rollbackTransaction();
|
2006-05-18 21:55:43 +00:00
|
|
|
throw(e);
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.Items.reload(this.getID());
|
2006-03-14 11:48:36 +00:00
|
|
|
|
2006-06-02 01:17:44 +00:00
|
|
|
if (isNew){
|
|
|
|
Scholar.Notifier.trigger('add', 'item', this.getID());
|
|
|
|
return this.getID();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Scholar.Notifier.trigger('modify', 'item', this.getID());
|
|
|
|
return true;
|
|
|
|
}
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-16 15:57:52 +00:00
|
|
|
Scholar.Item.prototype.updateDateModified = function(){
|
|
|
|
Scholar.DB.query("UPDATE items SET dateModified=CURRENT_TIMESTAMP "
|
|
|
|
+ "WHERE itemID=" + this.getID());
|
|
|
|
var date = Scholar.DB.valueQuery("SELECT dateModified FROM items "
|
|
|
|
+ "WHERE itemID=" + this.getID());
|
|
|
|
this._data['dateModified'] = date;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-16 07:32:48 +00:00
|
|
|
//
|
|
|
|
// Methods dealing with item notes
|
|
|
|
//
|
|
|
|
// save() is not required for note functions
|
|
|
|
//
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
Scholar.Item.prototype.incrementNoteCount = function(){
|
2006-06-25 05:43:00 +00:00
|
|
|
this._data['numNotes']++;
|
2006-06-16 07:32:48 +00:00
|
|
|
}
|
|
|
|
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
|
|
|
|
Scholar.Item.prototype.decrementNoteCount = function(){
|
|
|
|
this._data['numNotes']--;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-16 07:32:48 +00:00
|
|
|
/**
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
* Determine if an item is a note
|
2006-06-16 07:32:48 +00:00
|
|
|
**/
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
Scholar.Item.prototype.isNote = function(){
|
|
|
|
return Scholar.ItemTypes.getName(this.getType())=='note';
|
2006-06-16 07:32:48 +00:00
|
|
|
}
|
|
|
|
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
|
2006-06-16 07:32:48 +00:00
|
|
|
/**
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
* Update an item note
|
|
|
|
*
|
|
|
|
* Note: This can only be called on note items.
|
2006-06-16 07:32:48 +00:00
|
|
|
**/
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
Scholar.Item.prototype.updateNote = function(text){
|
|
|
|
if (!this.isNote()){
|
|
|
|
throw ("updateNote() can only be called on items of type 'note'");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.getID()){
|
|
|
|
throw ("Cannot call updateNote() on unsaved note");
|
|
|
|
}
|
|
|
|
|
2006-06-16 15:57:52 +00:00
|
|
|
Scholar.DB.beginTransaction();
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
|
|
|
|
var sql = "UPDATE itemNotes SET note=? WHERE itemID=?";
|
|
|
|
bindParams = [{string:text}, this.getID()];
|
|
|
|
var updated = Scholar.DB.query(sql, bindParams);
|
|
|
|
if (updated){
|
2006-06-25 05:43:00 +00:00
|
|
|
this.updateDateModified();
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
Scholar.DB.commitTransaction();
|
|
|
|
Scholar.Notifier.trigger('modify', 'item', this.getID());
|
2006-06-25 05:43:00 +00:00
|
|
|
}
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
else {
|
|
|
|
Scholar.DB.commitTransaction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Scholar.Item.prototype.setNoteSource = function(sourceItemID){
|
|
|
|
if (!this.isNote()){
|
|
|
|
throw ("updateNote() can only be called on items of type 'note'");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.getID()){
|
|
|
|
throw ("Cannot call setNoteSource() on unsaved note");
|
|
|
|
}
|
|
|
|
|
|
|
|
Scholar.DB.beginTransaction();
|
|
|
|
|
|
|
|
var newItem = Scholar.Items.get(sourceItemID);
|
|
|
|
// FK check
|
|
|
|
if (sourceItemID && !newItem){
|
|
|
|
Scholar.DB.rollbackTransaction();
|
|
|
|
throw ("Cannot set note source to invalid item " + sourceItemID);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get previous source item id
|
|
|
|
var sql = "SELECT sourceItemID FROM itemNotes WHERE item=" + this.getID();
|
|
|
|
var oldSourceItemID = Scholar.DB.valueQuery(sql);
|
|
|
|
|
|
|
|
if (oldSourceItemID==sourceItemID){
|
|
|
|
Scholar.debug("Note source hasn't changed", 4);
|
|
|
|
Scholar.DB.commitTransaction();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var oldItem = Scholar.Items.get(oldSourceItemID);
|
|
|
|
if (oldSourceItemID && !oldItem){
|
|
|
|
Scholar.debug("Old source item " + oldSourceItemID
|
|
|
|
+ "didn't exist in setNoteSource()", 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
var sql = "UPDATE itemNotes SET sourceItemID=? WHERE itemID=?";
|
|
|
|
var bindParams = [sourceItemID ? {int:sourceItemID} : null, this.getID()];
|
|
|
|
Scholar.DB.query(sql, bindParams);
|
|
|
|
this.updateDateModified();
|
2006-06-16 15:57:52 +00:00
|
|
|
Scholar.DB.commitTransaction();
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
|
2006-06-27 19:42:02 +00:00
|
|
|
Scholar.Notifier.trigger('modify', 'item', this.getID());
|
|
|
|
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
// Update the note counts of the previous and new sources
|
|
|
|
if (oldItem){
|
|
|
|
oldItem.decrementNoteCount();
|
|
|
|
Scholar.Notifier.trigger('modify', 'item', oldSourceItemID);
|
|
|
|
}
|
|
|
|
if (newItem){
|
|
|
|
newItem.incrementNoteCount();
|
|
|
|
Scholar.Notifier.trigger('modify', 'item', sourceItemID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2006-06-16 07:32:48 +00:00
|
|
|
}
|
|
|
|
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
|
2006-06-16 15:18:01 +00:00
|
|
|
/**
|
|
|
|
* Returns number of notes in item
|
|
|
|
**/
|
|
|
|
Scholar.Item.prototype.numNotes = function(){
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
if (this.isNote()){
|
|
|
|
throw ("numNotes() cannot be called on items of type 'note'");
|
|
|
|
}
|
|
|
|
|
2006-06-16 21:33:03 +00:00
|
|
|
if (!this.getID()){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-06-25 05:43:00 +00:00
|
|
|
return this._data['numNotes'];
|
2006-06-16 15:18:01 +00:00
|
|
|
}
|
|
|
|
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
|
2006-06-16 07:32:48 +00:00
|
|
|
/**
|
|
|
|
* Get the text of an item note
|
|
|
|
**/
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
Scholar.Item.prototype.getNote = function(){
|
|
|
|
if (!this.isNote()){
|
|
|
|
throw ("getNote() can only be called on items of type 'note'");
|
|
|
|
}
|
|
|
|
|
|
|
|
var sql = "SELECT note FROM itemNotes WHERE itemID=" + this.getID();
|
|
|
|
return Scholar.DB.valueQuery(sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the itemID of the source item for a note
|
|
|
|
**/
|
|
|
|
Scholar.Item.prototype.getNoteSource = function(){
|
|
|
|
if (!this.isNote()){
|
|
|
|
throw ("getNoteSource() can only be called on items of type 'note'");
|
|
|
|
}
|
|
|
|
|
|
|
|
var sql = "SELECT sourceItemID FROM itemNotes WHERE itemID=" + this.getID();
|
2006-06-16 07:32:48 +00:00
|
|
|
return Scholar.DB.valueQuery(sql);
|
|
|
|
}
|
|
|
|
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
|
2006-06-16 07:32:48 +00:00
|
|
|
/**
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
* Returns an array of note itemIDs for this item
|
2006-06-16 07:32:48 +00:00
|
|
|
**/
|
|
|
|
Scholar.Item.prototype.getNotes = function(){
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
if (this.isNote()){
|
|
|
|
Scholar.debug('here');
|
|
|
|
throw ("getNotes() cannot be called on items of type 'note'");
|
|
|
|
}
|
|
|
|
|
2006-06-16 21:33:03 +00:00
|
|
|
if (!this.getID()){
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
var sql = "SELECT itemID FROM itemNotes NATURAL JOIN items "
|
|
|
|
+ "WHERE sourceItemID=" + this.getID() + " ORDER BY dateAdded";
|
2006-06-16 07:32:48 +00:00
|
|
|
return Scholar.DB.columnQuery(sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-19 11:31:45 +00:00
|
|
|
/**
|
|
|
|
* Delete item from database and clear from Scholar.Items internal array
|
|
|
|
**/
|
|
|
|
Scholar.Item.prototype.erase = function(){
|
|
|
|
if (!this.getID()){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Scholar.debug('Deleting item ' + this.getID());
|
|
|
|
|
|
|
|
Scholar.DB.beginTransaction();
|
|
|
|
|
2006-06-01 00:22:18 +00:00
|
|
|
// Remove item from parent collections
|
|
|
|
var parentCollectionIDs = this.getCollections();
|
|
|
|
for (var i=0; i<parentCollectionIDs.length; i++){
|
|
|
|
Scholar.Collections.get(parentCollectionIDs[i]).removeItem(this.getID());
|
|
|
|
}
|
2006-05-19 11:31:45 +00:00
|
|
|
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
// If note, remove item from source notes
|
|
|
|
if (this.isNote()){
|
|
|
|
var sql = "SELECT sourceItemID FROM itemNotes WHERE itemID=" + this.getID();
|
|
|
|
var sourceItemID = Scholar.DB.valueQuery(sql);
|
|
|
|
if (sourceItemID){
|
|
|
|
var sourceItem = Scholar.Items.get(sourceItemID);
|
|
|
|
sourceItem.decrementNoteCount();
|
2006-06-27 19:42:02 +00:00
|
|
|
Scholar.Notifier.trigger('modify', 'item', sourceItemID);
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// If not note, unassociate any notes for which this is a source
|
|
|
|
else {
|
2006-06-27 19:42:02 +00:00
|
|
|
// TODO: option for deleting child notes instead of unlinking
|
|
|
|
|
|
|
|
var sql = "SELECT itemID FROM itemNotes WHERE sourceItemID=" + this.getID();
|
|
|
|
var childNotes = Scholar.DB.columnQuery(sql);
|
|
|
|
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
var sql = "UPDATE itemNotes SET sourceItemID=NULL WHERE sourceItemID="
|
|
|
|
+ this.getID();
|
2006-06-27 19:42:02 +00:00
|
|
|
Scholar.DB.query(sql);
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: remove item from See Also table
|
|
|
|
|
2006-06-02 21:11:14 +00:00
|
|
|
sql = 'DELETE FROM itemCreators WHERE itemID=' + this.getID() + ";\n";
|
2006-06-16 15:57:52 +00:00
|
|
|
sql += 'DELETE FROM itemNotes WHERE itemID=' + this.getID() + ";\n";
|
2006-06-02 21:11:14 +00:00
|
|
|
sql += 'DELETE FROM itemKeywords WHERE itemID=' + this.getID() + ";\n";
|
|
|
|
sql += 'DELETE FROM itemData WHERE itemID=' + this.getID() + ";\n";
|
|
|
|
sql += 'DELETE FROM items WHERE itemID=' + this.getID() + ";\n";
|
2006-05-19 11:31:45 +00:00
|
|
|
|
2006-06-02 21:11:14 +00:00
|
|
|
Scholar.DB.query(sql);
|
2006-05-19 11:31:45 +00:00
|
|
|
Scholar.Creators.purge();
|
|
|
|
|
|
|
|
try {
|
|
|
|
Scholar.DB.commitTransaction();
|
|
|
|
}
|
|
|
|
catch (e){
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
if (sourceItem){
|
|
|
|
sourceItem.incrementNoteCount();
|
|
|
|
}
|
2006-05-19 11:31:45 +00:00
|
|
|
Scholar.DB.rollbackTransaction();
|
|
|
|
throw (e);
|
|
|
|
}
|
|
|
|
|
2006-06-02 01:17:44 +00:00
|
|
|
Scholar.Items.unload(this.getID());
|
|
|
|
|
2006-06-27 19:42:02 +00:00
|
|
|
// Send notification of unlinked notes
|
|
|
|
if (childNotes){
|
|
|
|
Scholar.Notifier.trigger('modify', 'item', childNotes);
|
|
|
|
}
|
|
|
|
|
2006-06-01 22:19:21 +00:00
|
|
|
// If we're not in the middle of a larger commit, trigger the notifier now
|
|
|
|
if (!Scholar.DB.transactionInProgress()){
|
2006-06-08 20:41:37 +00:00
|
|
|
Scholar.Notifier.trigger('delete', 'item', this.getID());
|
2006-06-01 22:19:21 +00:00
|
|
|
}
|
2006-05-19 11:31:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-03 20:41:23 +00:00
|
|
|
Scholar.Item.prototype.isCollection = function(){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.Item.prototype.toString = function(){
|
2006-03-14 11:48:36 +00:00
|
|
|
return this.getTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-15 19:30:18 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2006-05-18 11:25:10 +00:00
|
|
|
// Private Scholar.Item methods
|
2006-05-15 19:30:18 +00:00
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2006-03-14 11:48:36 +00:00
|
|
|
/*
|
|
|
|
* Load in the creators from the database
|
|
|
|
*/
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.Item.prototype._loadCreators = function(){
|
2006-03-14 11:48:36 +00:00
|
|
|
if (!this.getID()){
|
2006-05-18 11:25:10 +00:00
|
|
|
throw ('ItemID not set for item before attempting to load creators');
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
2006-05-20 15:18:36 +00:00
|
|
|
var sql = 'SELECT C.creatorID, C.*, creatorTypeID, orderIndex '
|
|
|
|
+ 'FROM itemCreators IC '
|
2006-03-14 11:48:36 +00:00
|
|
|
+ 'LEFT JOIN creators C USING (creatorID) '
|
2006-05-18 11:25:10 +00:00
|
|
|
+ 'WHERE itemID=' + this.getID() + ' ORDER BY orderIndex';
|
2006-03-20 21:47:22 +00:00
|
|
|
var creators = Scholar.DB.query(sql);
|
2006-03-14 11:48:36 +00:00
|
|
|
|
2006-03-18 23:46:50 +00:00
|
|
|
this._creatorsLoaded = true;
|
|
|
|
|
2006-03-14 11:48:36 +00:00
|
|
|
if (!creators){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._creators = new Scholar.Hash();
|
|
|
|
for (var i=0; i<creators.length; i++){
|
|
|
|
var creator = new Array();
|
|
|
|
creator['firstName'] = creators[i]['firstName'];
|
|
|
|
creator['lastName'] = creators[i]['lastName'];
|
|
|
|
creator['creatorTypeID'] = creators[i]['creatorTypeID'];
|
2006-05-20 15:18:36 +00:00
|
|
|
// Save creator data into Hash, indexed by orderIndex
|
2006-03-14 11:48:36 +00:00
|
|
|
this._creators.set(creators[i]['orderIndex'], creator);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load in the field data from the database
|
|
|
|
*/
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.Item.prototype._loadItemData = function(){
|
2006-03-14 11:48:36 +00:00
|
|
|
if (!this.getID()){
|
2006-05-18 11:25:10 +00:00
|
|
|
throw ('ItemID not set for object before attempting to load data');
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
var sql = 'SELECT ID.fieldID, value FROM itemData ID JOIN '
|
|
|
|
+ 'itemTypeFields ITF ON (ITF.itemTypeID=(SELECT itemTypeID FROM '
|
|
|
|
+ 'items WHERE itemID=?1) AND ITF.fieldID=ID.fieldID) '
|
|
|
|
+ 'WHERE itemID=?1 ORDER BY orderIndex';
|
2006-03-14 11:48:36 +00:00
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
var result = Scholar.DB.query(sql,[{'int':this._data['itemID']}]);
|
2006-03-14 11:48:36 +00:00
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
this._itemDataLoaded = true;
|
2006-03-18 23:46:50 +00:00
|
|
|
|
2006-03-14 11:48:36 +00:00
|
|
|
if (result){
|
|
|
|
for (var i=0,len=result.length; i<len; i++){
|
|
|
|
this.setField(result[i]['fieldID'], result[i]['value'], true);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
Scholar.Notes = new function(){
|
|
|
|
this.add = add;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new item of type 'note' and add the note text to the itemNotes table
|
|
|
|
*
|
|
|
|
* Returns the itemID of the new note item
|
|
|
|
**/
|
|
|
|
function add(text, sourceItemID){
|
|
|
|
Scholar.DB.beginTransaction();
|
|
|
|
|
|
|
|
if (sourceItemID){
|
|
|
|
var sourceItem = Scholar.Items.get(sourceItemID);
|
|
|
|
if (!sourceItem){
|
|
|
|
Scholar.DB.commitTransaction();
|
|
|
|
throw ("Cannot set note source to invalid item " + sourceItemID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var note = Scholar.Items.getNewItemByType(Scholar.ItemTypes.getID('note'));
|
|
|
|
note.save();
|
|
|
|
|
|
|
|
var sql = "INSERT INTO itemNotes VALUES (?,?,?)";
|
|
|
|
var bindParams = [
|
|
|
|
note.getID(),
|
|
|
|
(sourceItemID ? {int:sourceItemID} : null),
|
|
|
|
{string:text}
|
|
|
|
];
|
|
|
|
Scholar.DB.query(sql, bindParams);
|
|
|
|
Scholar.DB.commitTransaction();
|
|
|
|
|
|
|
|
sourceItem.incrementNoteCount();
|
|
|
|
|
|
|
|
Scholar.Notifier.trigger('modify', 'item', sourceItemID);
|
|
|
|
Scholar.Notifier.trigger('add', 'item', note.getID());
|
|
|
|
|
|
|
|
return note.getID();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-23 20:03:21 +00:00
|
|
|
|
2006-05-15 19:30:18 +00:00
|
|
|
|
2006-03-23 08:51:05 +00:00
|
|
|
/*
|
2006-05-18 11:25:10 +00:00
|
|
|
* Primary interface for accessing Scholar items
|
2006-03-23 08:51:05 +00:00
|
|
|
*/
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.Items = new function(){
|
2006-03-14 11:48:36 +00:00
|
|
|
// Private members
|
2006-05-18 11:25:10 +00:00
|
|
|
var _items = new Array();
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
// Privileged methods
|
|
|
|
this.get = get;
|
|
|
|
this.getAll = getAll;
|
|
|
|
this.reload = reload;
|
|
|
|
this.reloadAll = reloadAll;
|
2006-05-18 20:09:01 +00:00
|
|
|
this.getNewItemByType = getNewItemByType;
|
2006-06-06 08:02:29 +00:00
|
|
|
this.search = search;
|
2006-05-19 11:31:45 +00:00
|
|
|
this.erase = erase;
|
|
|
|
this.unload = unload;
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
/*
|
2006-05-18 11:25:10 +00:00
|
|
|
* Retrieves (and loads, if necessary) an arbitrary number of items
|
2006-03-14 11:48:36 +00:00
|
|
|
*
|
|
|
|
* Can be passed ids as individual parameters or as an array of ids, or both
|
2006-03-23 08:51:05 +00:00
|
|
|
*
|
|
|
|
* If only one argument and it's an id, return object directly;
|
2006-06-06 20:15:50 +00:00
|
|
|
* otherwise, return array
|
2006-03-14 11:48:36 +00:00
|
|
|
*/
|
|
|
|
function get(){
|
|
|
|
var toLoad = new Array();
|
|
|
|
var loaded = new Array();
|
|
|
|
|
|
|
|
if (!arguments[0]){
|
2006-06-06 19:06:40 +00:00
|
|
|
Scholar.debug('No arguments provided to Items.get()');
|
2006-03-14 11:48:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var ids = Scholar.flattenArguments(arguments);
|
2006-03-23 08:51:05 +00:00
|
|
|
|
2006-03-14 11:48:36 +00:00
|
|
|
for (var i=0; i<ids.length; i++){
|
|
|
|
// Check if already loaded
|
2006-05-18 11:25:10 +00:00
|
|
|
if (!_items[ids[i]]){
|
2006-03-14 11:48:36 +00:00
|
|
|
toLoad.push(ids[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
// New items to load
|
2006-03-14 11:48:36 +00:00
|
|
|
if (toLoad.length){
|
|
|
|
_load(toLoad);
|
|
|
|
}
|
|
|
|
|
2006-03-23 08:51:05 +00:00
|
|
|
// If single id, return the object directly
|
2006-06-06 19:06:40 +00:00
|
|
|
if (arguments[0] && typeof arguments[0]!='object'
|
2006-03-23 08:51:05 +00:00
|
|
|
&& typeof arguments[1]=='undefined'){
|
2006-05-18 11:25:10 +00:00
|
|
|
return _items[arguments[0]];
|
2006-03-23 08:51:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, build return array
|
2006-03-14 11:48:36 +00:00
|
|
|
for (i=0; i<ids.length; i++){
|
2006-06-06 20:15:50 +00:00
|
|
|
loaded.push(_items[ids[i]]);
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
2006-06-06 19:06:40 +00:00
|
|
|
|
2006-03-14 11:48:36 +00:00
|
|
|
return loaded;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2006-05-18 11:25:10 +00:00
|
|
|
* Returns all items in the database
|
2006-03-14 11:48:36 +00:00
|
|
|
*/
|
|
|
|
function getAll(){
|
2006-06-01 00:22:18 +00:00
|
|
|
var sql = 'SELECT itemID FROM items';
|
|
|
|
// DEBUG: default order?
|
2006-03-14 11:48:36 +00:00
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
var ids = Scholar.DB.columnQuery(sql);
|
2006-03-14 11:48:36 +00:00
|
|
|
return this.get(ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2006-05-18 11:25:10 +00:00
|
|
|
* Reloads data for specified items into internal array
|
2006-03-14 11:48:36 +00:00
|
|
|
*
|
|
|
|
* Can be passed ids as individual parameters or as an array of ids, or both
|
|
|
|
*/
|
|
|
|
function reload(){
|
|
|
|
if (!arguments[0]){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var ids = Scholar.flattenArguments(arguments);
|
|
|
|
Scholar.debug('Reloading ' + ids);
|
|
|
|
_load(ids);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2006-06-02 19:48:22 +00:00
|
|
|
* Reloads all items
|
2006-03-14 11:48:36 +00:00
|
|
|
*/
|
|
|
|
function reloadAll(){
|
2006-06-02 19:48:22 +00:00
|
|
|
_items = new Array();
|
|
|
|
_load();
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-01 00:22:18 +00:00
|
|
|
function getNewItemByType(itemTypeID){
|
|
|
|
return new Scholar.Item(itemTypeID);
|
2006-05-18 20:09:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-01 00:22:18 +00:00
|
|
|
function add(data, itemTypeID){
|
2006-03-14 11:48:36 +00:00
|
|
|
var insert = new Array();
|
|
|
|
|
2006-06-01 00:22:18 +00:00
|
|
|
var obj = new Scholar.Item(itemTypeID);
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
for (field in data){
|
|
|
|
obj.setField(data[field]);
|
|
|
|
}
|
|
|
|
|
|
|
|
var id = obj.save();
|
|
|
|
|
|
|
|
return this.get(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-06 08:02:29 +00:00
|
|
|
/**
|
|
|
|
* Fulltext search on all fields
|
|
|
|
*
|
|
|
|
* TODO: more
|
|
|
|
**/
|
2006-06-06 20:46:08 +00:00
|
|
|
function search(text, parentCollectionID){
|
2006-06-06 08:02:29 +00:00
|
|
|
if (!text){
|
|
|
|
text = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
var sql = "SELECT itemID FROM items WHERE title LIKE ?1 UNION "
|
|
|
|
+ "SELECT itemID FROM itemData WHERE value LIKE ?1 UNION "
|
|
|
|
+ "SELECT itemID FROM itemCreators WHERE creatorID IN "
|
|
|
|
+ "(SELECT creatorID FROM creators WHERE firstName LIKE ?1 "
|
|
|
|
+ "OR lastName LIKE ?1) UNION "
|
|
|
|
+ "SELECT itemID FROM itemKeywords WHERE keywordID IN "
|
2006-06-16 08:04:01 +00:00
|
|
|
+ "(SELECT keywordID FROM keywords WHERE keyword LIKE ?1) UNION "
|
|
|
|
+ "SELECT itemID FROM itemNotes WHERE note LIKE ?1";
|
2006-06-06 20:46:08 +00:00
|
|
|
|
|
|
|
var sqlParams = [{'string':'%' + text + '%'}];
|
|
|
|
|
|
|
|
if (parentCollectionID){
|
|
|
|
sql = "SELECT itemID FROM (" + sql + ") WHERE itemID IN "
|
|
|
|
+ "(SELECT itemID FROM collectionItems WHERE collectionID=?2)";
|
|
|
|
sqlParams.push({'int':parentCollectionID});
|
|
|
|
}
|
|
|
|
|
|
|
|
return Scholar.DB.columnQuery(sql, sqlParams);
|
2006-06-06 08:02:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-19 11:31:45 +00:00
|
|
|
/**
|
|
|
|
* Delete item from database and clear from internal array
|
|
|
|
**/
|
|
|
|
function erase(id){
|
|
|
|
var obj = this.get(id);
|
|
|
|
obj.erase(); // calls unload()
|
|
|
|
obj = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear item from internal array (used by Scholar.Item.erase())
|
|
|
|
**/
|
|
|
|
function unload(id){
|
|
|
|
delete _items[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-14 11:48:36 +00:00
|
|
|
function _load(){
|
2006-05-18 11:25:10 +00:00
|
|
|
// Should be the same as query in Scholar.Item.loadFromID, just
|
|
|
|
// without itemID clause
|
2006-06-06 17:37:52 +00:00
|
|
|
var sql = 'SELECT I.*, lastName || '
|
|
|
|
+ 'CASE ((SELECT COUNT(*) FROM itemCreators WHERE itemID=I.itemID)>1) '
|
2006-06-25 05:43:00 +00:00
|
|
|
+ "WHEN 0 THEN '' ELSE ' et al.' END AS firstCreator, "
|
2006-06-27 19:29:05 +00:00
|
|
|
+ "(SELECT COUNT(*) FROM itemNotes WHERE sourceItemID=I.itemID) AS numNotes "
|
2006-05-18 11:25:10 +00:00
|
|
|
+ 'FROM items I '
|
|
|
|
+ 'LEFT JOIN itemCreators IC ON (I.itemID=IC.itemID) '
|
|
|
|
+ 'LEFT JOIN creators C ON (IC.creatorID=C.creatorID) '
|
2006-06-02 16:37:17 +00:00
|
|
|
+ 'WHERE (IC.orderIndex=0 OR IC.orderIndex IS NULL)';
|
2006-03-14 11:48:36 +00:00
|
|
|
|
2006-06-02 19:48:22 +00:00
|
|
|
if (arguments[0]){
|
2006-05-18 11:25:10 +00:00
|
|
|
sql += ' AND I.itemID IN (' + Scholar.join(arguments,',') + ')';
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
var result = Scholar.DB.query(sql);
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
if (result){
|
|
|
|
for (var i=0,len=result.length; i<len; i++){
|
2006-06-02 19:48:22 +00:00
|
|
|
// Item doesn't exist -- create new object and stuff in array
|
|
|
|
if (!_items[result[i]['itemID']]){
|
|
|
|
var obj = new Scholar.Item();
|
|
|
|
obj.loadFromRow(result[i]);
|
|
|
|
_items[result[i]['itemID']] = obj;
|
|
|
|
}
|
|
|
|
// Existing item -- reload in place
|
|
|
|
else {
|
|
|
|
_items[result[i]['itemID']].loadFromRow(result[i]);
|
|
|
|
}
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-23 08:51:05 +00:00
|
|
|
|
2006-05-15 19:30:18 +00:00
|
|
|
|
|
|
|
|
2006-03-23 08:51:05 +00:00
|
|
|
/*
|
2006-06-01 00:22:18 +00:00
|
|
|
* Constructor for Collection object
|
2006-03-23 08:51:05 +00:00
|
|
|
*
|
2006-06-01 00:22:18 +00:00
|
|
|
* Generally should be called from Scholar.Collection rather than directly
|
2006-03-23 08:51:05 +00:00
|
|
|
*/
|
2006-06-01 00:22:18 +00:00
|
|
|
Scholar.Collection = function(){
|
2006-06-02 19:48:22 +00:00
|
|
|
this._init();
|
|
|
|
}
|
|
|
|
|
|
|
|
Scholar.Collection.prototype._init = function(){
|
|
|
|
//
|
|
|
|
// Public members for access by public methods -- do not access directly
|
|
|
|
//
|
2006-03-23 08:51:05 +00:00
|
|
|
this._id;
|
|
|
|
this._name;
|
|
|
|
this._parent;
|
2006-06-01 00:22:18 +00:00
|
|
|
this._hasChildCollections;
|
|
|
|
this._hasChildItems;
|
|
|
|
this._childItems = new Scholar.Hash();
|
|
|
|
this._childItemsLoaded;
|
2006-03-23 08:51:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-06-01 00:22:18 +00:00
|
|
|
* Build collection from database
|
2006-03-23 08:51:05 +00:00
|
|
|
*/
|
2006-06-01 00:22:18 +00:00
|
|
|
Scholar.Collection.prototype.loadFromID = function(id){
|
|
|
|
// Should be same as query in Scholar.Collections, just with collectionID
|
|
|
|
var sql = "SELECT collectionID, collectionName, parentCollectionID, "
|
|
|
|
+ "(SELECT COUNT(*) FROM collections WHERE "
|
|
|
|
+ "parentCollectionID=C.collectionID)!=0 AS hasChildCollections, "
|
|
|
|
+ "(SELECT COUNT(*) FROM collectionItems WHERE "
|
|
|
|
+ "collectionID=C.collectionID)!=0 AS hasChildItems "
|
|
|
|
+ "FROM collections C "
|
|
|
|
+ "WHERE collectionID=" + id;
|
2006-05-16 21:51:09 +00:00
|
|
|
|
2006-03-23 08:51:05 +00:00
|
|
|
var row = Scholar.DB.rowQuery(sql);
|
|
|
|
this.loadFromRow(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2006-06-01 00:22:18 +00:00
|
|
|
* Populate collection data from a database row
|
2006-03-23 08:51:05 +00:00
|
|
|
*/
|
2006-06-01 00:22:18 +00:00
|
|
|
Scholar.Collection.prototype.loadFromRow = function(row){
|
2006-06-02 19:48:22 +00:00
|
|
|
this._init();
|
2006-06-01 00:22:18 +00:00
|
|
|
this._id = row['collectionID'];
|
|
|
|
this._name = row['collectionName'];
|
|
|
|
this._parent = row['parentCollectionID'];
|
|
|
|
this._hasChildCollections = row['hasChildCollections'];
|
2006-05-23 20:53:09 +00:00
|
|
|
this._hasChildItems = row['hasChildItems'];
|
2006-03-23 08:51:05 +00:00
|
|
|
}
|
|
|
|
|
2006-06-01 00:22:18 +00:00
|
|
|
|
|
|
|
Scholar.Collection.prototype.getID = function(){
|
2006-03-23 08:51:05 +00:00
|
|
|
return this._id;
|
|
|
|
}
|
|
|
|
|
2006-06-01 00:22:18 +00:00
|
|
|
Scholar.Collection.prototype.getName = function(){
|
2006-03-23 08:51:05 +00:00
|
|
|
return this._name;
|
|
|
|
}
|
|
|
|
|
2006-06-02 01:17:44 +00:00
|
|
|
/**
|
2006-06-03 20:41:23 +00:00
|
|
|
* Returns collectionID of the parent collection
|
2006-06-02 01:17:44 +00:00
|
|
|
**/
|
2006-06-01 00:22:18 +00:00
|
|
|
Scholar.Collection.prototype.getParent = function(){
|
2006-05-15 19:30:18 +00:00
|
|
|
return this._parent;
|
|
|
|
}
|
2006-03-23 08:51:05 +00:00
|
|
|
|
2006-06-01 00:22:18 +00:00
|
|
|
|
|
|
|
Scholar.Collection.prototype.isEmpty = function(){
|
|
|
|
return !(parseInt(this._hasChildCollections)) && !(parseInt(this._hasChildItems));
|
2006-05-23 20:53:09 +00:00
|
|
|
}
|
|
|
|
|
2006-06-01 00:22:18 +00:00
|
|
|
Scholar.Collection.prototype.hasChildCollections = function(){
|
|
|
|
return !!(parseInt(this._hasChildCollections));
|
2006-05-16 21:51:09 +00:00
|
|
|
}
|
|
|
|
|
2006-06-01 00:22:18 +00:00
|
|
|
Scholar.Collection.prototype.hasChildItems = function(){
|
2006-05-23 20:53:09 +00:00
|
|
|
return !!(parseInt(this._hasChildItems));
|
|
|
|
}
|
|
|
|
|
2006-06-03 20:41:23 +00:00
|
|
|
/**
|
|
|
|
* Rename the collection
|
|
|
|
*
|
2006-06-03 20:51:48 +00:00
|
|
|
* _name_ is non-empty string
|
|
|
|
*
|
2006-06-03 20:41:23 +00:00
|
|
|
* Returns true on success, or false on error
|
|
|
|
**/
|
|
|
|
Scholar.Collection.prototype.rename = function(name){
|
|
|
|
if (!name){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var sql = "UPDATE collections SET collectionName=? "
|
|
|
|
+ "WHERE collectionID=?";
|
|
|
|
Scholar.DB.query(sql, [{'string':name},{'int':this.getID()}]);
|
|
|
|
this._name = name;
|
|
|
|
|
|
|
|
Scholar.Notifier.trigger('modify', 'collection', this.getID());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-06-05 21:58:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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){
|
2006-06-08 19:11:10 +00:00
|
|
|
if (this.hasDescendent('collection', parent)){
|
|
|
|
Scholar.debug('Cannot move collection into one of its own '
|
|
|
|
+ 'descendents!', 2);
|
|
|
|
return false;
|
2006-06-05 21:58:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2006-06-08 19:58:54 +00:00
|
|
|
var notifyIDs = [
|
|
|
|
this.getID(),
|
|
|
|
(previousParent ? previousParent : null),
|
|
|
|
(parent ? parent : null)
|
|
|
|
];
|
2006-06-05 21:58:01 +00:00
|
|
|
|
2006-06-08 18:53:49 +00:00
|
|
|
// TODO: only reload the necessary ones
|
|
|
|
Scholar.Collections.reloadAll();
|
2006-06-08 19:58:54 +00:00
|
|
|
Scholar.Notifier.trigger('move', 'collection', notifyIDs);
|
2006-06-05 21:58:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-03 20:41:23 +00:00
|
|
|
/**
|
|
|
|
* Add an item to the collection
|
|
|
|
**/
|
2006-06-01 00:22:18 +00:00
|
|
|
Scholar.Collection.prototype.addItem = function(itemID){
|
|
|
|
Scholar.DB.beginTransaction();
|
|
|
|
|
|
|
|
if (!Scholar.Items.get(itemID)){
|
|
|
|
Scholar.DB.rollbackTransaction();
|
|
|
|
throw(itemID + ' is not a valid item id');
|
|
|
|
}
|
|
|
|
|
|
|
|
var nextOrderIndex = Scholar.DB.valueQuery("SELECT IFNULL(MAX(orderIndex)+1, 0) "
|
|
|
|
+ "FROM collectionItems WHERE collectionID=" + this._id);
|
|
|
|
|
|
|
|
var sql = "INSERT OR IGNORE INTO collectionItems VALUES "
|
|
|
|
+ "(" + this._id + ", " + itemID + ", " + nextOrderIndex + ")";
|
|
|
|
|
|
|
|
Scholar.DB.query(sql);
|
|
|
|
Scholar.DB.commitTransaction();
|
|
|
|
|
|
|
|
this._childItems.set(itemID);
|
2006-06-08 20:41:37 +00:00
|
|
|
|
|
|
|
// If this was previously empty, update and send a notification to the tree
|
|
|
|
if (!this._hasChildItems){
|
|
|
|
this._hasChildItems = true;
|
2006-06-20 15:28:20 +00:00
|
|
|
Scholar.Notifier.trigger('modify', 'collection', this.getID());
|
2006-06-08 20:41:37 +00:00
|
|
|
}
|
2006-06-01 20:03:53 +00:00
|
|
|
|
|
|
|
Scholar.Notifier.trigger('add', 'item', itemID);
|
2006-06-01 00:22:18 +00:00
|
|
|
}
|
|
|
|
|
2006-06-01 20:03:53 +00:00
|
|
|
|
2006-06-03 20:41:23 +00:00
|
|
|
/**
|
|
|
|
* Remove an item from the collection (does not delete item from library)
|
|
|
|
**/
|
2006-06-01 00:22:18 +00:00
|
|
|
Scholar.Collection.prototype.removeItem = function(itemID){
|
|
|
|
Scholar.DB.beginTransaction();
|
|
|
|
|
|
|
|
var sql = "SELECT orderIndex FROM collectionItems "
|
|
|
|
+ "WHERE collectionID=" + this._id + " AND itemID=" + itemID;
|
|
|
|
var orderIndex = Scholar.DB.valueQuery(sql);
|
|
|
|
|
|
|
|
if (orderIndex===false){
|
|
|
|
Scholar.debug('Item ' + itemID + ' is not a child of collection '
|
|
|
|
+ this._id);
|
|
|
|
Scholar.DB.rollbackTransaction();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var sql = "DELETE FROM collectionItems WHERE collectionID=" + this._id
|
|
|
|
+ " AND itemID=" + itemID;
|
|
|
|
Scholar.DB.query(sql);
|
|
|
|
|
|
|
|
// Move down items above deleted item in collection
|
|
|
|
sql = 'UPDATE collectionItems SET orderIndex=orderIndex-1 '
|
|
|
|
+ 'WHERE collectionID=' + this._id
|
|
|
|
+ ' AND orderIndex>' + orderIndex;
|
|
|
|
Scholar.DB.query(sql);
|
|
|
|
|
|
|
|
Scholar.DB.commitTransaction();
|
|
|
|
this._childItems.remove(itemID);
|
|
|
|
|
|
|
|
// If this was the last item, set collection to empty
|
|
|
|
if (!this._childItems.length){
|
|
|
|
this._hasChildItems = false;
|
2006-06-09 14:01:07 +00:00
|
|
|
Scholar.Notifier.trigger('modify', 'collection', this.getID());
|
2006-06-01 00:22:18 +00:00
|
|
|
}
|
2006-06-01 20:03:53 +00:00
|
|
|
|
|
|
|
Scholar.Notifier.trigger('remove', 'item', itemID);
|
2006-06-01 00:22:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-03 20:41:23 +00:00
|
|
|
/**
|
|
|
|
* Check if an item belongs to the collection
|
|
|
|
**/
|
2006-06-01 00:22:18 +00:00
|
|
|
Scholar.Collection.prototype.hasItem = function(itemID){
|
|
|
|
if (!this._childItemsLoaded){
|
|
|
|
this._loadChildItems();
|
|
|
|
}
|
|
|
|
return this._childItems.has(itemID);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-08 19:11:10 +00:00
|
|
|
Scholar.Collection.prototype.hasDescendent = function(type, id){
|
|
|
|
var descendents = this._getDescendents();
|
|
|
|
for (var i=0, len=descendents.length; i<len; i++){
|
|
|
|
// TODO: fix this to work with smart collections
|
|
|
|
if (((type=='collection' && descendents[i]['isCollection']) ||
|
|
|
|
(type=='item' && !descendents[i]['isCollection']))
|
|
|
|
&& id==descendents[i]['id']){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-23 14:54:10 +00:00
|
|
|
/**
|
2006-06-03 20:41:23 +00:00
|
|
|
* Deletes collection and all descendent collections and items
|
2006-05-23 14:54:10 +00:00
|
|
|
**/
|
2006-06-01 00:22:18 +00:00
|
|
|
Scholar.Collection.prototype.erase = function(deleteItems){
|
2006-05-23 14:54:10 +00:00
|
|
|
Scholar.DB.beginTransaction();
|
|
|
|
|
|
|
|
var descendents = this._getDescendents();
|
2006-06-20 15:23:44 +00:00
|
|
|
var collections = [this.getID()], items = [];
|
2006-05-23 14:54:10 +00:00
|
|
|
|
|
|
|
for(var i=0, len=descendents.length; i<len; i++){
|
2006-06-01 00:22:18 +00:00
|
|
|
// Descendent collections
|
|
|
|
if (descendents[i]['isCollection']){
|
|
|
|
collections.push(descendents[i]['id']);
|
2006-05-23 14:54:10 +00:00
|
|
|
}
|
2006-06-01 00:22:18 +00:00
|
|
|
// Descendent items
|
2006-05-23 14:54:10 +00:00
|
|
|
else {
|
2006-06-01 00:22:18 +00:00
|
|
|
if (deleteItems){
|
|
|
|
// Delete items from DB
|
|
|
|
Scholar.Items.get(descendents[i]['id']).erase();
|
2006-06-08 20:02:26 +00:00
|
|
|
items.push(descendents[i]['id']);
|
2006-06-01 00:22:18 +00:00
|
|
|
}
|
2006-05-23 14:54:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-01 00:22:18 +00:00
|
|
|
// Remove item associations for all descendent collections
|
2006-06-01 22:19:21 +00:00
|
|
|
Scholar.DB.query('DELETE FROM collectionItems WHERE collectionID IN ('
|
2006-06-01 00:22:18 +00:00
|
|
|
+ collections.join() + ')');
|
2006-05-23 14:54:10 +00:00
|
|
|
|
2006-06-01 00:22:18 +00:00
|
|
|
// And delete all descendent collections
|
2006-06-01 22:19:21 +00:00
|
|
|
Scholar.DB.query('DELETE FROM collections WHERE collectionID IN ('
|
2006-06-01 00:22:18 +00:00
|
|
|
+ collections.join() + ')');
|
2006-05-23 14:54:10 +00:00
|
|
|
|
2006-06-01 22:19:21 +00:00
|
|
|
Scholar.DB.commitTransaction();
|
|
|
|
|
2006-06-01 00:22:18 +00:00
|
|
|
// Clear deleted collection from internal memory
|
|
|
|
Scholar.Collections.unload(collections);
|
2006-06-02 01:17:44 +00:00
|
|
|
|
|
|
|
Scholar.Notifier.trigger('remove', 'collection', collections);
|
2006-06-08 20:02:26 +00:00
|
|
|
if (items.length){
|
2006-06-08 20:41:37 +00:00
|
|
|
Scholar.Notifier.trigger('delete', 'item', items);
|
2006-06-08 20:02:26 +00:00
|
|
|
}
|
2006-05-23 14:54:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-03 20:41:23 +00:00
|
|
|
Scholar.Collection.prototype.isCollection = function(){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-01 00:22:18 +00:00
|
|
|
Scholar.Collection.prototype._loadChildItems = function(){
|
|
|
|
this._childItems = new Scholar.Hash();
|
|
|
|
|
|
|
|
var sql = "SELECT itemID FROM collectionItems WHERE collectionID=" + this._id;
|
|
|
|
var itemIDs = Scholar.DB.columnQuery(sql);
|
|
|
|
|
|
|
|
if (!itemIDs){
|
|
|
|
Scholar.debug('Collection ' + this._id + ' has no child items');
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i=0; i<itemIDs.length; i++){
|
|
|
|
this._childItems.set(itemIDs[i]);
|
|
|
|
}
|
|
|
|
this._childItemsLoaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-23 14:54:10 +00:00
|
|
|
/**
|
2006-06-01 00:22:18 +00:00
|
|
|
* Returns an array of descendent collections and items (rows of 'id' and 'isCollection')
|
2006-05-23 14:54:10 +00:00
|
|
|
**/
|
2006-06-01 00:22:18 +00:00
|
|
|
Scholar.Collection.prototype._getDescendents = function(){
|
2006-05-23 14:54:10 +00:00
|
|
|
var toReturn = new Array();
|
|
|
|
|
2006-06-01 00:22:18 +00:00
|
|
|
var children = Scholar.DB.query('SELECT collectionID AS id, '
|
|
|
|
+ '1 AS isCollection FROM collections '
|
|
|
|
+ 'WHERE parentCollectionID=' + this._id
|
|
|
|
+ ' UNION SELECT itemID AS id, 0 AS isCollection FROM collectionItems '
|
|
|
|
+ 'WHERE collectionID=' + this._id);
|
2006-05-23 14:54:10 +00:00
|
|
|
|
|
|
|
for(var i=0, len=children.length; i<len; i++){
|
2006-06-01 00:22:18 +00:00
|
|
|
if (parseInt(children[i]['isCollection'])){
|
2006-05-23 14:54:10 +00:00
|
|
|
toReturn.push({
|
|
|
|
id: children[i]['id'],
|
2006-06-01 00:22:18 +00:00
|
|
|
isCollection: true
|
2006-05-23 14:54:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var descendents =
|
2006-06-01 00:22:18 +00:00
|
|
|
Scholar.Collections.get(children[i]['id'])._getDescendents();
|
2006-05-23 14:54:10 +00:00
|
|
|
|
2006-06-14 15:41:25 +00:00
|
|
|
for(var j=0, len2=descendents.length; j<len2; j++){
|
2006-05-23 14:54:10 +00:00
|
|
|
toReturn.push(descendents[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
toReturn.push({
|
|
|
|
id: children[i]['id'],
|
2006-06-01 00:22:18 +00:00
|
|
|
isCollection: false
|
2006-05-23 14:54:10 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
2006-03-23 08:51:05 +00:00
|
|
|
|
2006-06-12 13:08:36 +00:00
|
|
|
|
|
|
|
|
2006-03-23 08:51:05 +00:00
|
|
|
/*
|
2006-06-01 00:22:18 +00:00
|
|
|
* Primary interface for accessing Scholar collection
|
2006-03-23 08:51:05 +00:00
|
|
|
*/
|
2006-06-01 00:22:18 +00:00
|
|
|
Scholar.Collections = new function(){
|
|
|
|
var _collections = new Array();
|
|
|
|
var _collectionsLoaded = false;
|
2006-03-23 08:51:05 +00:00
|
|
|
|
|
|
|
this.get = get;
|
2006-06-03 20:41:23 +00:00
|
|
|
this.add = add;
|
2006-05-18 21:55:43 +00:00
|
|
|
this.reloadAll = reloadAll;
|
2006-05-23 14:54:10 +00:00
|
|
|
this.unload = unload;
|
2006-03-23 08:51:05 +00:00
|
|
|
|
|
|
|
/*
|
2006-06-01 00:22:18 +00:00
|
|
|
* Returns a Scholar.Collection object for a collectionID
|
2006-03-23 08:51:05 +00:00
|
|
|
*/
|
|
|
|
function get(id){
|
2006-06-01 00:22:18 +00:00
|
|
|
if (!_collectionsLoaded){
|
2006-03-23 08:51:05 +00:00
|
|
|
_load();
|
|
|
|
}
|
2006-06-01 00:22:18 +00:00
|
|
|
return (typeof _collections[id]!='undefined') ? _collections[id] : false;
|
2006-03-23 08:51:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-03 20:51:48 +00:00
|
|
|
/**
|
|
|
|
* Add new collection to DB and return Collection object
|
|
|
|
*
|
|
|
|
* _name_ is non-empty string
|
|
|
|
* _parent_ is optional collectionID -- creates root collection by default
|
|
|
|
*
|
|
|
|
* Returns true on success; false on error
|
|
|
|
**/
|
2006-06-03 20:41:23 +00:00
|
|
|
function add(name, parent){
|
|
|
|
if (!name){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Scholar.DB.beginTransaction();
|
|
|
|
|
|
|
|
if (parent && !this.get(parent)){
|
|
|
|
Scholar.DB.rollbackTransaction();
|
|
|
|
throw('Cannot add collection to invalid parent ' + parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
var parentParam = parent ? {'int':parent} : {'null':true};
|
|
|
|
|
|
|
|
var rnd = Scholar.getRandomID('collections', 'collectionID');
|
|
|
|
|
|
|
|
var sql = "INSERT INTO collections VALUES (?,?,?)";
|
|
|
|
var sqlValues = [ {'int':rnd}, {'string':name}, parentParam ];
|
|
|
|
Scholar.DB.query(sql, sqlValues);
|
|
|
|
|
|
|
|
Scholar.DB.commitTransaction();
|
|
|
|
|
|
|
|
_load(rnd);
|
|
|
|
Scholar.Notifier.trigger('add', 'collection', rnd);
|
|
|
|
|
|
|
|
return this.get(rnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-23 14:54:10 +00:00
|
|
|
/**
|
2006-06-01 00:22:18 +00:00
|
|
|
* Clears internal cache and reloads collection data from DB
|
2006-05-23 14:54:10 +00:00
|
|
|
**/
|
2006-05-18 21:55:43 +00:00
|
|
|
function reloadAll(){
|
2006-06-01 00:22:18 +00:00
|
|
|
_collections = new Array();
|
2006-05-18 21:55:43 +00:00
|
|
|
_load();
|
|
|
|
}
|
|
|
|
|
2006-05-23 14:54:10 +00:00
|
|
|
|
|
|
|
/**
|
2006-06-01 00:22:18 +00:00
|
|
|
* Clear collection from internal cache (used by Scholar.Collection.erase())
|
2006-05-23 14:54:10 +00:00
|
|
|
*
|
|
|
|
* Can be passed ids as individual parameters or as an array of ids, or both
|
|
|
|
**/
|
|
|
|
function unload(){
|
|
|
|
var ids = Scholar.flattenArguments(arguments);
|
|
|
|
|
|
|
|
for(var i=0; i<ids.length; i++){
|
2006-06-01 00:22:18 +00:00
|
|
|
delete _collections[ids[i]];
|
2006-05-23 14:54:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2006-06-01 00:22:18 +00:00
|
|
|
* Loads collection data from DB and adds to internal cache
|
2006-05-23 14:54:10 +00:00
|
|
|
**/
|
2006-03-23 08:51:05 +00:00
|
|
|
function _load(){
|
2006-06-01 00:22:18 +00:00
|
|
|
// This should be the same as the query in Scholar.Collection.loadFromID,
|
|
|
|
// just without a specific collectionID
|
|
|
|
var sql = "SELECT collectionID, collectionName, parentCollectionID, "
|
|
|
|
+ "(SELECT COUNT(*) FROM collections WHERE "
|
|
|
|
+ "parentCollectionID=C.collectionID)!=0 AS hasChildCollections, "
|
|
|
|
+ "(SELECT COUNT(*) FROM collectionItems WHERE "
|
|
|
|
+ "collectionID=C.collectionID)!=0 AS hasChildItems "
|
|
|
|
+ "FROM collections C";
|
|
|
|
|
2006-06-03 20:41:23 +00:00
|
|
|
var ids = Scholar.flattenArguments(arguments)
|
|
|
|
if (ids.length){
|
|
|
|
sql += " WHERE collectionID IN (" + ids.join() + ")";
|
|
|
|
}
|
|
|
|
|
2006-03-23 08:51:05 +00:00
|
|
|
var result = Scholar.DB.query(sql);
|
|
|
|
|
|
|
|
if (!result){
|
2006-06-03 20:41:23 +00:00
|
|
|
throw ('No collections found');
|
2006-03-23 08:51:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (var i=0; i<result.length; i++){
|
2006-06-02 19:48:22 +00:00
|
|
|
var collectionID = result[i]['collectionID'];
|
|
|
|
|
|
|
|
// If collection doesn't exist, create new object and stuff in array
|
|
|
|
if (!_collections[collectionID]){
|
|
|
|
var collection = new Scholar.Collection();
|
|
|
|
collection.loadFromRow(result[i]);
|
|
|
|
_collections[collectionID] = collection;
|
|
|
|
}
|
|
|
|
// If existing collection, reload in place
|
|
|
|
else {
|
|
|
|
_collections[collectionID].loadFromRow(result[i]);
|
|
|
|
}
|
2006-03-23 08:51:05 +00:00
|
|
|
}
|
2006-06-01 00:22:18 +00:00
|
|
|
_collectionsLoaded = true;
|
2006-03-23 08:51:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-12 13:08:36 +00:00
|
|
|
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Creators = new function(){
|
2006-05-20 15:18:36 +00:00
|
|
|
var _creators = new Array; // indexed by first%%%last hash
|
2006-03-14 11:48:36 +00:00
|
|
|
var _creatorsByID = new Array; // indexed by creatorID
|
|
|
|
|
|
|
|
this.get = get;
|
|
|
|
this.getID = getID;
|
|
|
|
this.add = add;
|
|
|
|
this.purge = purge;
|
|
|
|
|
2006-05-19 11:31:45 +00:00
|
|
|
var self = this;
|
|
|
|
|
2006-03-14 11:48:36 +00:00
|
|
|
/*
|
|
|
|
* Returns an array of creator data for a given creatorID
|
|
|
|
*/
|
|
|
|
function get(creatorID){
|
|
|
|
if (_creatorsByID[creatorID]){
|
|
|
|
return _creatorsByID[creatorID];
|
|
|
|
}
|
|
|
|
|
|
|
|
var sql = 'SELECT * FROM creators WHERE creatorID=' + creatorID;
|
2006-03-20 21:47:22 +00:00
|
|
|
var result = Scholar.DB.rowQuery(sql);
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
if (!result){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_creatorsByID[creatorID] = result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the creatorID matching given name and type
|
|
|
|
*/
|
2006-05-20 15:18:36 +00:00
|
|
|
function getID(firstName, lastName){
|
|
|
|
var hash = firstName + '%%%' + lastName;
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
if (_creators[hash]){
|
|
|
|
return _creators[hash];
|
|
|
|
}
|
|
|
|
|
2006-05-20 15:18:36 +00:00
|
|
|
var sql = 'SELECT creatorID FROM creators '
|
|
|
|
+ 'WHERE firstName=? AND lastName=?';
|
2006-03-14 11:48:36 +00:00
|
|
|
var params = [
|
2006-05-20 15:18:36 +00:00
|
|
|
{'string': firstName}, {'string': lastName}
|
2006-03-14 11:48:36 +00:00
|
|
|
];
|
2006-03-20 21:47:22 +00:00
|
|
|
var creatorID = Scholar.DB.valueQuery(sql,params);
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
if (creatorID){
|
|
|
|
_creators[hash] = creatorID;
|
|
|
|
}
|
|
|
|
|
|
|
|
return creatorID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a new creator to the database
|
|
|
|
*
|
|
|
|
* Returns new creatorID
|
|
|
|
*/
|
2006-05-20 15:18:36 +00:00
|
|
|
function add(firstName, lastName){
|
2006-03-14 11:48:36 +00:00
|
|
|
Scholar.debug('Adding new creator', 4);
|
|
|
|
|
2006-06-03 20:41:23 +00:00
|
|
|
Scholar.DB.beginTransaction();
|
|
|
|
|
2006-03-14 11:48:36 +00:00
|
|
|
var sql = 'INSERT INTO creators '
|
2006-05-20 15:18:36 +00:00
|
|
|
+ 'VALUES (?,?,?)';
|
2006-03-14 11:48:36 +00:00
|
|
|
|
2006-06-03 20:41:23 +00:00
|
|
|
var rnd = Scholar.getRandomID('creators', 'creatorID');
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
var params = [
|
2006-05-20 15:18:36 +00:00
|
|
|
{'int': rnd}, {'string': firstName}, {'string': lastName}
|
2006-03-14 11:48:36 +00:00
|
|
|
];
|
2006-05-20 15:18:36 +00:00
|
|
|
|
|
|
|
Scholar.DB.query(sql, params);
|
2006-06-03 20:41:23 +00:00
|
|
|
|
|
|
|
Scholar.DB.commitTransaction();
|
2006-05-20 15:18:36 +00:00
|
|
|
return rnd;
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete obsolete creators from database and clear internal array entries
|
|
|
|
*
|
2006-06-23 22:00:39 +00:00
|
|
|
* Returns removed creatorIDs on success
|
2006-03-14 11:48:36 +00:00
|
|
|
*/
|
2006-06-23 22:00:39 +00:00
|
|
|
function purge(){
|
2006-03-14 11:48:36 +00:00
|
|
|
var sql = 'SELECT creatorID FROM creators WHERE creatorID NOT IN '
|
2006-05-18 11:25:10 +00:00
|
|
|
+ '(SELECT creatorID FROM itemCreators);';
|
2006-03-20 21:47:22 +00:00
|
|
|
var toDelete = Scholar.DB.columnQuery(sql);
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
if (!toDelete){
|
2006-06-23 22:00:39 +00:00
|
|
|
return false;
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear creator entries in internal array
|
|
|
|
for (var i=0; i<toDelete.length; i++){
|
|
|
|
var hash = _getHash(toDelete[i]);
|
|
|
|
delete _creators[hash];
|
|
|
|
delete _creatorsByID[toDelete[i]];
|
|
|
|
}
|
|
|
|
|
2006-06-23 22:00:39 +00:00
|
|
|
sql = 'DELETE FROM creators WHERE creatorID NOT IN '
|
|
|
|
+ '(SELECT creatorID FROM itemCreators);';
|
|
|
|
var result = Scholar.DB.query(sql);
|
|
|
|
|
|
|
|
return toDelete;
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function _getHash(creatorID){
|
2006-05-19 11:31:45 +00:00
|
|
|
var creator = self.get(creatorID);
|
2006-03-14 11:48:36 +00:00
|
|
|
if (!creator){
|
|
|
|
return false;
|
|
|
|
}
|
2006-05-20 15:18:36 +00:00
|
|
|
return creator['firstName'] + '%%%' + creator['lastName'];
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-15 19:30:18 +00:00
|
|
|
|
|
|
|
|
2006-06-12 13:08:36 +00:00
|
|
|
Scholar.CreatorTypes = new function(){
|
2006-06-25 04:35:11 +00:00
|
|
|
var _types = new Array();
|
|
|
|
var _typesLoaded;
|
2006-06-12 13:08:36 +00:00
|
|
|
var self = this;
|
|
|
|
|
2006-06-25 04:35:11 +00:00
|
|
|
this.getName = getName;
|
|
|
|
this.getID = getID;
|
2006-06-12 13:08:36 +00:00
|
|
|
this.getTypes = getTypes;
|
|
|
|
|
2006-06-25 04:35:11 +00:00
|
|
|
|
|
|
|
function getName(idOrName){
|
|
|
|
if (!_typesLoaded){
|
|
|
|
_load();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_types[idOrName]){
|
|
|
|
Scholar.debug('Invalid creator type ' + idOrName, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _types[idOrName]['name'];
|
2006-06-12 13:08:36 +00:00
|
|
|
}
|
|
|
|
|
2006-06-25 04:35:11 +00:00
|
|
|
|
|
|
|
function getID(idOrName){
|
|
|
|
if (!_typesLoaded){
|
2006-06-12 13:08:36 +00:00
|
|
|
_load();
|
|
|
|
}
|
|
|
|
|
2006-06-25 04:35:11 +00:00
|
|
|
if (!_types[idOrName]){
|
|
|
|
Scholar.debug('Invalid creator type ' + idOrName, 1);
|
2006-06-12 13:08:36 +00:00
|
|
|
}
|
|
|
|
|
2006-06-25 04:35:11 +00:00
|
|
|
return _types[idOrName]['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getTypes(){
|
|
|
|
return Scholar.DB.query('SELECT creatorTypeID AS id, '
|
|
|
|
+ 'creatorType AS name FROM creatorTypes order BY creatorType');
|
2006-06-12 13:08:36 +00:00
|
|
|
}
|
|
|
|
|
2006-06-25 04:35:11 +00:00
|
|
|
|
2006-06-12 13:08:36 +00:00
|
|
|
function _load(){
|
|
|
|
var types = self.getTypes();
|
|
|
|
|
|
|
|
for (i in types){
|
2006-06-25 04:35:11 +00:00
|
|
|
// Store as both id and name for access by either
|
|
|
|
var typeData = {
|
|
|
|
id: types[i]['id'],
|
|
|
|
name: types[i]['name']
|
|
|
|
}
|
|
|
|
_types[types[i]['id']] = typeData;
|
|
|
|
_types[types[i]['name']] = _types[types[i]['id']];
|
2006-06-12 13:08:36 +00:00
|
|
|
}
|
|
|
|
|
2006-06-25 04:35:11 +00:00
|
|
|
_typesLoaded = true;
|
2006-06-12 13:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-05-18 20:39:38 +00:00
|
|
|
Scholar.ItemTypes = new function(){
|
2006-06-25 04:35:11 +00:00
|
|
|
var _types = new Array();
|
|
|
|
var _typesLoaded;
|
2006-06-12 12:59:25 +00:00
|
|
|
var self = this;
|
|
|
|
|
2006-06-25 04:35:11 +00:00
|
|
|
this.getName = getName;
|
|
|
|
this.getID = getID;
|
2006-05-18 20:39:38 +00:00
|
|
|
this.getTypes = getTypes;
|
|
|
|
|
2006-06-25 04:35:11 +00:00
|
|
|
|
|
|
|
function getName(idOrName){
|
|
|
|
if (!_typesLoaded){
|
|
|
|
_load();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_types[idOrName]){
|
|
|
|
Scholar.debug('Invalid item type ' + idOrName, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _types[idOrName]['name'];
|
2006-05-18 20:39:38 +00:00
|
|
|
}
|
|
|
|
|
2006-06-25 04:35:11 +00:00
|
|
|
|
|
|
|
function getID(idOrName){
|
|
|
|
if (!_typesLoaded){
|
2006-06-12 12:59:25 +00:00
|
|
|
_load();
|
|
|
|
}
|
|
|
|
|
2006-06-25 04:35:11 +00:00
|
|
|
if (!_types[idOrName]){
|
|
|
|
Scholar.debug('Invalid item type ' + idOrName, 1);
|
2006-06-12 12:59:25 +00:00
|
|
|
}
|
|
|
|
|
2006-06-25 04:35:11 +00:00
|
|
|
return _types[idOrName]['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getTypes(){
|
|
|
|
return Scholar.DB.query('SELECT itemTypeID AS id, typeName AS name '
|
|
|
|
+ 'FROM itemTypes order BY typeName');
|
2006-06-12 12:59:25 +00:00
|
|
|
}
|
|
|
|
|
2006-06-25 04:35:11 +00:00
|
|
|
|
2006-06-12 12:59:25 +00:00
|
|
|
function _load(){
|
|
|
|
var types = self.getTypes();
|
|
|
|
|
|
|
|
for (i in types){
|
2006-06-25 04:35:11 +00:00
|
|
|
// Store as both id and name for access by either
|
|
|
|
var typeData = {
|
|
|
|
id: types[i]['id'],
|
|
|
|
name: types[i]['name']
|
|
|
|
}
|
|
|
|
_types[types[i]['id']] = typeData;
|
|
|
|
_types[types[i]['name']] = _types[types[i]['id']];
|
2006-06-12 12:59:25 +00:00
|
|
|
}
|
|
|
|
|
2006-06-25 04:35:11 +00:00
|
|
|
_typesLoaded = true;
|
2006-05-18 20:39:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-15 19:30:18 +00:00
|
|
|
|
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
Scholar.ItemFields = new function(){
|
2006-03-14 11:48:36 +00:00
|
|
|
// Private members
|
|
|
|
var _fields = new Array();
|
|
|
|
var _fieldFormats = new Array();
|
2006-05-18 11:25:10 +00:00
|
|
|
var _itemTypeFields = new Array();
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
// Privileged methods
|
|
|
|
this.getName = getName;
|
|
|
|
this.getID = getID;
|
|
|
|
this.isValidForType = isValidForType;
|
|
|
|
this.isInteger = isInteger;
|
2006-05-18 11:25:10 +00:00
|
|
|
this.getItemTypeFields = getItemTypeFields;
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the fieldName for a passed fieldID or fieldName
|
|
|
|
*/
|
|
|
|
function getName(field){
|
|
|
|
if (!_fields.length){
|
|
|
|
_loadFields();
|
|
|
|
}
|
|
|
|
return _fields[field] ? _fields[field]['name'] : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the fieldID for a passed fieldID or fieldName
|
|
|
|
*/
|
|
|
|
function getID(field){
|
|
|
|
if (!_fields.length){
|
|
|
|
_loadFields();
|
|
|
|
}
|
|
|
|
return _fields[field] ? _fields[field]['id'] : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
function isValidForType(fieldID, itemTypeID){
|
2006-03-14 11:48:36 +00:00
|
|
|
if (!_fields.length){
|
|
|
|
_loadFields();
|
|
|
|
}
|
2006-06-04 19:37:03 +00:00
|
|
|
|
|
|
|
_fieldCheck(fieldID);
|
|
|
|
|
|
|
|
if (!_fields[fieldID]['itemTypes']){
|
|
|
|
throw('No associated itemTypes for fieldID ' + fieldID);
|
|
|
|
}
|
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
return !!_fields[fieldID]['itemTypes'][itemTypeID];
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function isInteger(fieldID){
|
|
|
|
if (!_fields.length){
|
|
|
|
_loadFields();
|
|
|
|
}
|
2006-06-04 19:37:03 +00:00
|
|
|
|
|
|
|
_fieldCheck(fieldID);
|
|
|
|
|
2006-03-14 11:48:36 +00:00
|
|
|
var ffid = _fields[fieldID]['formatID'];
|
|
|
|
return _fieldFormats[ffid] ? _fieldFormats[ffid]['isInteger'] : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2006-05-18 11:25:10 +00:00
|
|
|
* Returns an array of fieldIDs for a given item type
|
2006-03-14 11:48:36 +00:00
|
|
|
*/
|
2006-05-18 11:25:10 +00:00
|
|
|
function getItemTypeFields(itemTypeID){
|
|
|
|
if (_itemTypeFields[itemTypeID]){
|
|
|
|
return _itemTypeFields[itemTypeID];
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
var sql = 'SELECT fieldID FROM itemTypeFields '
|
|
|
|
+ 'WHERE itemTypeID=' + itemTypeID + ' ORDER BY orderIndex';
|
2006-03-14 11:48:36 +00:00
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
_itemTypeFields[itemTypeID] = Scholar.DB.columnQuery(sql);
|
|
|
|
return _itemTypeFields[itemTypeID];
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-04 19:37:03 +00:00
|
|
|
/**
|
|
|
|
* Check whether a fieldID is valid, throwing an exception if not
|
|
|
|
* (since it should never actually happen)
|
|
|
|
**/
|
|
|
|
function _fieldCheck(fieldID){
|
|
|
|
if (!_fields.length){
|
|
|
|
_loadFields();
|
|
|
|
}
|
|
|
|
if (typeof _fields[fieldID]=='undefined'){
|
|
|
|
throw('Invalid fieldID ' + fieldID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-14 11:48:36 +00:00
|
|
|
/*
|
2006-05-18 11:25:10 +00:00
|
|
|
* Returns hash array of itemTypeIDs for which a given field is valid
|
2006-03-14 11:48:36 +00:00
|
|
|
*/
|
2006-05-18 11:25:10 +00:00
|
|
|
function _getFieldItemTypes(){
|
|
|
|
var sql = 'SELECT fieldID, itemTypeID FROM itemTypeFields';
|
2006-03-14 11:48:36 +00:00
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
var results = Scholar.DB.query(sql);
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
if (!results){
|
2006-05-18 11:25:10 +00:00
|
|
|
throw ('No fields in itemTypeFields!');
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
var fields = new Array();
|
|
|
|
for (var i=0; i<results.length; i++){
|
|
|
|
if (!fields[results[i]['fieldID']]){
|
|
|
|
fields[results[i]['fieldID']] = new Array();
|
|
|
|
}
|
2006-05-18 11:25:10 +00:00
|
|
|
fields[results[i]['fieldID']][results[i]['itemTypeID']] = true;
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load all fields into an internal hash array
|
|
|
|
*/
|
|
|
|
function _loadFields(){
|
|
|
|
var i,len;
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
var result = Scholar.DB.query('SELECT * FROM fieldFormats');
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
for (i=0; i<result.length; i++){
|
|
|
|
_fieldFormats[result[i]['fieldFormatID']] = {
|
|
|
|
regex: result[i]['regex'],
|
|
|
|
isInteger: result[i]['isInteger']
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
result = Scholar.DB.query('SELECT * FROM fields');
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
if (!result.length){
|
|
|
|
throw ('No fields in database!');
|
|
|
|
}
|
|
|
|
|
2006-05-18 11:25:10 +00:00
|
|
|
var fieldItemTypes = _getFieldItemTypes();
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
for (i=0,len=result.length; i<len; i++){
|
|
|
|
_fields[result[i]['fieldID']] = {
|
|
|
|
id: result[i]['fieldID'],
|
|
|
|
name: result[i]['fieldName'],
|
|
|
|
formatID: result[i]['fieldFormatID'],
|
2006-05-18 11:25:10 +00:00
|
|
|
itemTypes: fieldItemTypes[result[i]['fieldID']]
|
2006-03-14 11:48:36 +00:00
|
|
|
};
|
|
|
|
// Store by name as well as id
|
|
|
|
_fields[result[i]['fieldName']] = _fields[result[i]['fieldID']];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-20 15:18:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2006-06-01 00:22:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scholar.getCollections(parent)
|
|
|
|
*
|
|
|
|
* Returns an array of all collections are children of a collection
|
|
|
|
* as Scholar.Collection instances
|
|
|
|
*
|
|
|
|
* Takes parent collectionID as optional parameter;
|
|
|
|
* by default, returns root collections
|
|
|
|
*/
|
|
|
|
Scholar.getCollections = function(parent){
|
|
|
|
var toReturn = new Array();
|
|
|
|
|
|
|
|
if (!parent){
|
|
|
|
parent = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var sql = 'SELECT collectionID FROM collections C WHERE parentCollectionID';
|
|
|
|
sql += parent ? '=' + parent : ' IS NULL';
|
|
|
|
|
|
|
|
sql += ' ORDER BY collectionName';
|
|
|
|
|
|
|
|
var children = Scholar.DB.columnQuery(sql);
|
|
|
|
|
|
|
|
if (!children){
|
|
|
|
Scholar.debug('No child collections of collection ' + parent, 5);
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i=0, len=children.length; i<len; i++){
|
|
|
|
var obj = Scholar.Collections.get(children[i]);
|
|
|
|
if (!obj){
|
|
|
|
throw ('Collection ' + children[i] + ' not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
toReturn.push(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return toReturn;
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-01 00:22:18 +00:00
|
|
|
/*
|
|
|
|
* Scholar.getItems(parent)
|
|
|
|
*
|
|
|
|
* Returns an array of all items that are children of a collection--or all
|
|
|
|
* items if no parent provided--as Scholar.Item instances
|
|
|
|
*/
|
|
|
|
Scholar.getItems = function(parent){
|
|
|
|
var toReturn = new Array();
|
|
|
|
|
|
|
|
if (!parent){
|
Fixes #29, independent notes
- Added 'note' item type
- Updated API to support independent note creation
Notes are, more or less, just regular items, with an item type of 1. They're created through Scholar.Notes.add(text, sourceItemID), which returns the itemID of the new note. sourceItemID is optional--if left out, an independent note will be created. (There's currently nothing stopping you from doing getNewItemByType(1) yourself, but the note would be contentless and broken, so you shouldn't do that.) Note data could've been stuffed into itemData, but I kept it separate in itemNotes to keep metadata searching faster and to keep things cleaner.
Methods calls that can be called on all items:
isNote() (same as testing for itemTypeID 1)
Method calls that can be called on source items only:
numNotes()
getNotes() (array of note itemIDs for a source)
Method calls that can be called on note items only:
updateNote(text)
setNoteSource(sourceItemID) (for changing source--use empty or false to make independent, which is currently what happens when you delete a note--will get option with #91)
getNote() (note content)
getNoteSource() (sourceItemID of a note)
Calling the above methods on the wrong item types will throw an error.
*** This will break note creation/display until David updates interface code. ***
2006-06-27 15:14:07 +00:00
|
|
|
var sql = "SELECT itemID FROM items LEFT JOIN itemNotes USING (itemID) "
|
|
|
|
+ "WHERE sourceItemID IS NULL";
|
2006-06-01 00:22:18 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
var sql = 'SELECT itemID FROM collectionItems '
|
|
|
|
+ 'WHERE collectionID=' + parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
var children = Scholar.DB.columnQuery(sql);
|
|
|
|
|
|
|
|
if (!children){
|
|
|
|
if (!parent){
|
|
|
|
Scholar.debug('No items in library', 5);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Scholar.debug('No child items of collection ' + parent, 5);
|
|
|
|
}
|
|
|
|
return toReturn;
|
|
|
|
}
|
|
|
|
|
2006-06-06 20:46:08 +00:00
|
|
|
return Scholar.Items.get(children);
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|