2006-03-14 11:48:36 +00:00
|
|
|
/*
|
|
|
|
* Constructor for Object object
|
|
|
|
*
|
2006-03-20 21:47:22 +00:00
|
|
|
* Generally should be called through Scholar.Objects rather than directly
|
2006-03-14 11:48:36 +00:00
|
|
|
*/
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Object = function(){
|
2006-03-14 11:48:36 +00:00
|
|
|
this._init();
|
|
|
|
|
|
|
|
// Accept objectTypeID, folderID and orderIndex in constructor
|
|
|
|
if (arguments.length){
|
|
|
|
this.setType(arguments[0]);
|
|
|
|
this.setPosition(arguments[1],arguments[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Object.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();
|
|
|
|
this._objectData = new Array();
|
|
|
|
|
|
|
|
this._creatorsLoaded = false;
|
|
|
|
this._objectDataLoaded = false;
|
|
|
|
|
|
|
|
this._changed = new Scholar.Hash();
|
|
|
|
this._changedCreators = new Scholar.Hash();
|
|
|
|
this._changedObjectData = new Scholar.Hash();
|
|
|
|
}
|
|
|
|
|
2006-03-17 05:11:03 +00:00
|
|
|
|
2006-03-14 11:48:36 +00:00
|
|
|
/*
|
|
|
|
* Check if the specified field is a primary field from the objects table
|
|
|
|
*/
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Object.prototype.isPrimaryField = function(field){
|
|
|
|
if (!Scholar.Object.primaryFields){
|
|
|
|
Scholar.Object.primaryFields = Scholar.DB.getColumnHash('objects');
|
|
|
|
Scholar.Object.primaryFields['firstCreator'] = true;
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
return !!Scholar.Object.primaryFields[field];
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Object.editableFields = {
|
2006-03-14 11:48:36 +00:00
|
|
|
title: true,
|
|
|
|
source: true,
|
|
|
|
rights: true
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if the specified primary field can be changed with setField()
|
|
|
|
*/
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Object.prototype.isEditableField = function(field){
|
|
|
|
return !!Scholar.Object.editableFields[field];
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Build object from database
|
|
|
|
*/
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Object.prototype.loadFromID = function(id){
|
2006-03-14 11:48:36 +00:00
|
|
|
var sql = 'SELECT O.*, lastName AS firstCreator FROM objects O '
|
|
|
|
+ 'LEFT JOIN objectCreators OC USING (objectID) '
|
|
|
|
+ 'LEFT JOIN creators USING (creatorID) '
|
2006-03-17 05:11:03 +00:00
|
|
|
+ 'WHERE objectID=' + id + ' AND OC.orderIndex=0';
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Populate basic object data from a database row
|
|
|
|
*/
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Object.prototype.loadFromRow = function(row){
|
2006-03-14 11:48:36 +00:00
|
|
|
this._init();
|
|
|
|
for (col in row){
|
|
|
|
if (this.isPrimaryField(col) || col=='firstCreator'){
|
|
|
|
this._data[col] = row[col];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if any data fields have changed since last save
|
|
|
|
*/
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Object.prototype.hasChanged = function(){
|
2006-03-14 11:48:36 +00:00
|
|
|
return (this._changed.length || this._changedCreators.length ||
|
|
|
|
this._changedObjectData.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Object.prototype.getID = function(){
|
2006-03-14 11:48:36 +00:00
|
|
|
return this._data['objectID'] ? this._data['objectID'] : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Object.prototype.getType = function(){
|
2006-03-14 11:48:36 +00:00
|
|
|
return this._data['objectTypeID'] ? this._data['objectTypeID'] : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set or change the object's type
|
|
|
|
*/
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Object.prototype.setType = function(objectTypeID){
|
2006-03-14 11:48:36 +00:00
|
|
|
if (objectTypeID==this.getType()){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If existing type, clear fields from old type that aren't in new one
|
|
|
|
if (this.getType()){
|
|
|
|
var sql = 'SELECT fieldID FROM objectTypeFields '
|
|
|
|
+ 'WHERE objectTypeID=' + this.getType() + ' AND fieldID NOT IN '
|
|
|
|
+ '(SELECT fieldID FROM objectTypeFields WHERE objectTypeID='
|
|
|
|
+ objectTypeID + ')';
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this._data['objectTypeID'] = objectTypeID;
|
|
|
|
this._changed.set('objectTypeID');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the number of creators for this object
|
|
|
|
*/
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Object.prototype.numCreators = function(){
|
2006-03-14 11:48:36 +00:00
|
|
|
if (this.getID() && !this._creatorsLoaded){
|
|
|
|
this._loadCreators();
|
|
|
|
}
|
|
|
|
return this._creators.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns an array of the creator data at the given position, or false if none
|
|
|
|
*/
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Object.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-03-20 21:47:22 +00:00
|
|
|
Scholar.Object.prototype.setCreator = function(orderIndex, firstName, lastName, creatorTypeID){
|
2006-03-14 11:48:36 +00:00
|
|
|
if (this.getID() && !this._creatorsLoaded){
|
|
|
|
this._loadCreators();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!creatorTypeID){
|
|
|
|
creatorTypeID = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!firstName && !lastName){
|
|
|
|
throw ('Name not provided for creator');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._creators.has(orderIndex) &&
|
|
|
|
this._creators.get(orderIndex)['firstName']==firstName &&
|
|
|
|
this._creators.get(orderIndex)['lastName']==lastName &&
|
|
|
|
this._creators.get(orderIndex)['creatorTypeID']==creatorTypeID){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-20 21:47:22 +00:00
|
|
|
Scholar.Object.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);
|
|
|
|
|
|
|
|
for (var i=orderIndex,len=this._creators.length; i<=len; i++){
|
|
|
|
var next =
|
|
|
|
this._creators.items[i+1] ? this._creators.items[i+1] : false;
|
|
|
|
this._creators.set(i, next);
|
|
|
|
this._changedCreators.set(i);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieves (and loads from DB, if necessary) an objectData field value
|
|
|
|
*
|
|
|
|
* Field can be passed as fieldID or fieldName
|
|
|
|
*/
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Object.prototype.getField = function(field){
|
2006-03-18 23:46:50 +00:00
|
|
|
//Scholar.debug('Requesting field ' + field + ' for object ' + 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 {
|
|
|
|
if (this.getID() && !this._objectDataLoaded){
|
|
|
|
this._loadObjectData();
|
|
|
|
}
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
var fieldID = Scholar.ObjectFields.getID(field);
|
2006-03-14 11:48:36 +00:00
|
|
|
|
2006-03-17 05:11:03 +00:00
|
|
|
return this._objectData[fieldID] ? this._objectData[fieldID] : '';
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set a field value, loading existing objectData first if necessary
|
|
|
|
*
|
|
|
|
* Field can be passed as fieldID or fieldName
|
|
|
|
*/
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Object.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()){
|
|
|
|
throw ('Object type must be set before setting field data.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// If existing object, load field data first unless we're already in
|
|
|
|
// the middle of a load
|
|
|
|
if (this.getID() && !loadIn && !this._objectDataLoaded){
|
|
|
|
this._loadObjectData();
|
|
|
|
}
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
var fieldID = Scholar.ObjectFields.getID(field);
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
if (!fieldID){
|
|
|
|
throw (field + ' is not a valid objectData field.');
|
|
|
|
}
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
if (!Scholar.ObjectFields.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
|
|
|
|
if (this._objectData[fieldID] && this._objectData[fieldID]==value){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this._objectData[fieldID] = value;
|
|
|
|
if (!loadIn){
|
|
|
|
this._changedObjectData.set(fieldID);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Move object to new position and shift surrounding objects
|
|
|
|
*
|
|
|
|
* N.B. This action updates the DB immediately and reloads all cached
|
|
|
|
* objects -- a save() is not required
|
|
|
|
*/
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Object.prototype.setPosition = function(newFolder, newPos){
|
2006-03-14 11:48:36 +00:00
|
|
|
var oldFolder = this.getField('folderID');
|
|
|
|
var oldPos = this.getField('orderIndex');
|
|
|
|
|
|
|
|
if (this.getID()){
|
|
|
|
if (newFolder==oldFolder && newPos==oldPos){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
var sql = "BEGIN;\n";
|
|
|
|
|
|
|
|
// If no position provided, drop at end of folder
|
|
|
|
if (!newPos){
|
2006-03-20 21:47:22 +00:00
|
|
|
newPos = Scholar.DB.valueQuery('SELECT MAX(orderIndex)+1 FROM ' +
|
2006-03-14 11:48:36 +00:00
|
|
|
'objects WHERE folderID=' + newFolder);
|
|
|
|
}
|
|
|
|
// Otherwise shift down above it in old folder and shift up at it or
|
|
|
|
// above it in new folder
|
|
|
|
else {
|
|
|
|
sql += 'UPDATE objects SET orderIndex=orderIndex-1 WHERE folderID='
|
|
|
|
+ oldFolder + ' AND orderIndex>' + oldPos + ";\n";
|
|
|
|
|
|
|
|
sql += 'UPDATE objects SET orderIndex=orderIndex+1 WHERE folderID='
|
|
|
|
+ newFolder + ' AND orderIndex>=' + newPos + ";\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
sql += 'UPDATE objects SET folderID=' + newFolder + ', orderIndex=' +
|
|
|
|
newPos + ' WHERE objectID=' + this.getID() + ";\n";
|
|
|
|
|
|
|
|
sql += 'COMMIT;';
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.DB.query(sql);
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this._data['folderID'] = newFolder;
|
|
|
|
this._data['orderIndex'] = newPos;
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Objects.reloadAll();
|
2006-03-14 11:48:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save changes back to database
|
|
|
|
*/
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Object.prototype.save = function(){
|
2006-03-14 11:48:36 +00:00
|
|
|
if (!this.hasChanged()){
|
|
|
|
Scholar.debug('Object ' + this.getID() + ' has not changed', 4);
|
|
|
|
return !!this.getID();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Existing object, update
|
|
|
|
//
|
|
|
|
if (this.getID()){
|
|
|
|
Scholar.debug('Updating database with new object data', 4);
|
|
|
|
|
|
|
|
var objectID = this.getID();
|
|
|
|
|
|
|
|
try {
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.DB.beginTransaction();
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Primary fields
|
|
|
|
//
|
|
|
|
var sql = "UPDATE objects SET ";
|
|
|
|
var sql2;
|
|
|
|
|
|
|
|
if (this._changed.has('objectTypeID')){
|
|
|
|
sql += "objectTypeID='" + this.getField('objectTypeID') + "', ";
|
|
|
|
}
|
|
|
|
if (this._changed.has('title')){
|
|
|
|
sql += "title='" + this.getField('title') + "', ";
|
|
|
|
}
|
|
|
|
if (this._changed.has('source')){
|
|
|
|
sql += "source='" + this.getField('source') + "', ";
|
|
|
|
}
|
|
|
|
if (this._changed.has('rights')){
|
|
|
|
sql += "rights='" + this.getField('rights') + "', ";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always update modified time
|
|
|
|
sql += "dateModified=CURRENT_TIMESTAMP ";
|
|
|
|
sql += "WHERE objectID=" + this.getID() + ";\n";
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Creators
|
|
|
|
//
|
|
|
|
if (this._changedCreators.length){
|
|
|
|
for (orderIndex in this._changedCreators.items){
|
|
|
|
Scholar.debug('Creator ' + orderIndex + ' has changed', 4);
|
|
|
|
|
|
|
|
var creator = this.getCreator(orderIndex);
|
|
|
|
|
|
|
|
// If empty, delete at position and shift down any above it
|
|
|
|
//
|
|
|
|
// We have to do this immediately so old entries are
|
|
|
|
// cleared before other ones are shifted down
|
|
|
|
if (!creator['firstName'] && !creator['lastName']){
|
|
|
|
sql2 = 'DELETE FROM objectCreators '
|
|
|
|
+ ' WHERE objectID=' + this.getID()
|
|
|
|
+ ' AND orderIndex=' + orderIndex;
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.DB.query(sql2);
|
2006-03-14 11:48:36 +00:00
|
|
|
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'],
|
|
|
|
creator['lastName'],
|
|
|
|
creator['creatorTypeID']
|
|
|
|
);
|
|
|
|
|
|
|
|
// 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'],
|
|
|
|
creator['lastName'],
|
|
|
|
creator['creatorTypeID']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sql2 = 'SELECT COUNT(*) FROM objectCreators'
|
|
|
|
+ ' WHERE objectID=' + this.getID()
|
|
|
|
+ ' AND orderIndex=' + orderIndex;
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
if (Scholar.DB.valueQuery(sql2)){
|
2006-03-14 11:48:36 +00:00
|
|
|
sql += 'UPDATE objectCreators SET creatorID='
|
|
|
|
+ creatorID + ' WHERE objectID=' + this.getID()
|
|
|
|
+ ' AND orderIndex=' + orderIndex + ";\n";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sql += 'INSERT INTO objectCreators VALUES ('
|
|
|
|
+ creatorID + ',' + objectID + ',' + orderIndex
|
|
|
|
+ ");\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the SQL to delete obsolete creators
|
2006-03-21 15:19:11 +00:00
|
|
|
sql += Scholar.Creators.purge(true) + "\n";
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// ObjectData
|
|
|
|
//
|
|
|
|
if (this._changedObjectData.length){
|
|
|
|
var del = new Array();
|
|
|
|
for (fieldID in this._changedObjectData.items){
|
|
|
|
if (this.getField(fieldID)){
|
|
|
|
// Oh, for an INSERT...ON DUPLICATE KEY UPDATE
|
|
|
|
sql2 = 'SELECT COUNT(*) FROM objectData '
|
|
|
|
+ 'WHERE objectID=' + this.getID()
|
|
|
|
+ ' AND fieldID=' + fieldID;
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
if (Scholar.DB.valueQuery(sql2)){
|
2006-03-14 11:48:36 +00:00
|
|
|
sql += "UPDATE objectData SET value=";
|
|
|
|
// Take advantage of SQLite's manifest typing
|
2006-03-20 21:47:22 +00:00
|
|
|
if (Scholar.ObjectFields.isInteger(fieldID)){
|
2006-03-14 11:48:36 +00:00
|
|
|
sql += this.getField(fieldID);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sql += "'" + this.getField(fieldID) + "'";
|
|
|
|
}
|
|
|
|
sql += " WHERE objectID=" + this.getID()
|
|
|
|
+ ' AND fieldID=' + fieldID + ";\n";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sql += 'INSERT INTO objectData VALUES ('
|
|
|
|
+ this.getID() + ',' + fieldID + ',';
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
if (Scholar.ObjectFields.isInteger(fieldID)){
|
2006-03-14 11:48:36 +00:00
|
|
|
sql += this.getField(fieldID);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sql += "'" + this.getField(fieldID) + "'";
|
|
|
|
}
|
|
|
|
sql += ");\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If field changed and is empty, mark row for deletion
|
|
|
|
else {
|
|
|
|
del.push(fieldID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete blank fields
|
|
|
|
if (del.length){
|
|
|
|
sql += 'DELETE from objectData '
|
|
|
|
+ 'WHERE objectID=' + this.getID() + ' '
|
|
|
|
+ 'AND fieldID IN (' + del.join() + ");\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.DB.query(sql);
|
|
|
|
Scholar.DB.commitTransaction();
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
catch (e){
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.DB.rollbackTransaction();
|
2006-03-14 11:48:36 +00:00
|
|
|
throw (e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// New object, insert and return id
|
|
|
|
//
|
|
|
|
else {
|
|
|
|
Scholar.debug('Saving data for new object to database');
|
|
|
|
|
|
|
|
var isNew = true;
|
|
|
|
var sqlColumns = new Array();
|
|
|
|
var sqlValues = new Array();
|
|
|
|
|
|
|
|
//
|
|
|
|
// Primary fields
|
|
|
|
//
|
|
|
|
sqlColumns.push('objectTypeID');
|
|
|
|
sqlValues.push({'int':this.getField('objectTypeID')});
|
|
|
|
|
|
|
|
if (this._changed.has('title')){
|
|
|
|
sqlColumns.push('title');
|
|
|
|
sqlValues.push({'string':this.getField('title')});
|
|
|
|
}
|
|
|
|
if (this._changed.has('source')){
|
|
|
|
sqlColumns.push('source');
|
|
|
|
sqlValues.push({'string':this.getField('source')});
|
|
|
|
}
|
|
|
|
if (this._changed.has('rights')){
|
|
|
|
sqlColumns.push('rights');
|
|
|
|
sqlValues.push({'string':this.getField('rights')});
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlColumns.push('folderID');
|
|
|
|
var newFolder =
|
|
|
|
this._changed.has('folderID') ? this.getField('folderID') : 0;
|
|
|
|
sqlValues.push({'int':newFolder});
|
|
|
|
|
|
|
|
try {
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.DB.beginTransaction();
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
// We set the index here within the transaction so that MAX()+1
|
|
|
|
// stays consistent through the INSERT
|
|
|
|
sqlColumns.push('orderIndex');
|
|
|
|
if (this._changed.has('orderIndex')){
|
|
|
|
sqlValues.push({'int':this.getField('orderIndex')});
|
|
|
|
}
|
|
|
|
else {
|
2006-03-20 21:47:22 +00:00
|
|
|
var newPos = Scholar.DB.valueQuery('SELECT MAX(orderIndex)+1 '
|
2006-03-14 11:48:36 +00:00
|
|
|
+ 'FROM objects WHERE folderID=' + newFolder);
|
|
|
|
sqlValues.push({'int': newPos});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// Creators
|
|
|
|
//
|
|
|
|
if (this._changedCreators.length){
|
|
|
|
for (orderIndex in this._changedCreators.items){
|
|
|
|
var creator = this.getCreator(orderIndex);
|
|
|
|
|
|
|
|
// If empty, skip
|
|
|
|
if (typeof creator['firstName'] == 'undefined'
|
|
|
|
&& typeof creator['lastName'] == 'undefined'){
|
|
|
|
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'],
|
|
|
|
creator['lastName'],
|
|
|
|
creator['creatorTypeID']
|
|
|
|
);
|
|
|
|
|
|
|
|
// 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'],
|
|
|
|
creator['lastName'],
|
|
|
|
creator['creatorTypeID']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
sql += 'INSERT INTO objectCreators VALUES ('
|
|
|
|
+ creatorID + ',' + objectID + ',' + orderIndex
|
|
|
|
+ ");\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// objectData fields
|
|
|
|
//
|
|
|
|
var sql = "INSERT INTO objects (" + sqlColumns.join() + ')'
|
|
|
|
+ ' VALUES (';
|
|
|
|
// Insert placeholders for bind parameters
|
|
|
|
for (var i=0; i<sqlValues.length; i++){
|
|
|
|
sql += '?,';
|
|
|
|
}
|
|
|
|
sql = sql.substring(0,sql.length-1) + ");\n";
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
var objectID = Scholar.DB.query(sql,sqlValues);
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
if (this._changedObjectData.length){
|
|
|
|
sql = '';
|
|
|
|
for (fieldID in this._changedObjectData.items){
|
|
|
|
sql += 'INSERT INTO objectData VALUES (' +
|
|
|
|
objectID + ',' + fieldID + ',';
|
2006-03-20 21:47:22 +00:00
|
|
|
if (Scholar.ObjectFields.isInteger(fieldID)){
|
2006-03-14 11:48:36 +00:00
|
|
|
sql += this.getField(fieldID);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sql += "'" + this.getField(fieldID) + "'";
|
|
|
|
}
|
|
|
|
sql += ");\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.DB.query(sql);
|
|
|
|
Scholar.DB.commitTransaction();
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
catch (e){
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.DB.rollbackTransaction();
|
2006-03-14 11:48:36 +00:00
|
|
|
throw (e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Objects.reload(this.getID());
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
return isNew ? this.getID() : true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Object.prototype.toString = function(){
|
2006-03-14 11:48:36 +00:00
|
|
|
return this.getTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load in the creators from the database
|
|
|
|
*/
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Object.prototype._loadCreators = function(){
|
2006-03-14 11:48:36 +00:00
|
|
|
if (!this.getID()){
|
|
|
|
throw ('ObjectID not set for object before attempting to load creators');
|
|
|
|
}
|
|
|
|
|
|
|
|
var sql = 'SELECT C.creatorID, C.*, orderIndex FROM objectCreators OC '
|
|
|
|
+ 'LEFT JOIN creators C USING (creatorID) '
|
|
|
|
+ 'WHERE objectID=' + 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'];
|
|
|
|
this._creators.set(creators[i]['orderIndex'], creator);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load in the field data from the database
|
|
|
|
*/
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Object.prototype._loadObjectData = function(){
|
2006-03-14 11:48:36 +00:00
|
|
|
if (!this.getID()){
|
|
|
|
throw ('ObjectID not set for object before attempting to load data');
|
|
|
|
}
|
|
|
|
|
|
|
|
var sql = 'SELECT OD.fieldID, value FROM objectData OD JOIN '
|
|
|
|
+ 'objectTypeFields OTF ON (OTF.objectTypeID=(SELECT objectTypeID FROM '
|
|
|
|
+ 'objects WHERE objectID=?1) AND OTF.fieldID=OD.fieldID) '
|
|
|
|
+ 'WHERE objectID=?1 ORDER BY orderIndex';
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
var result = Scholar.DB.query(sql,[{'int':this._data['objectID']}]);
|
2006-03-14 11:48:36 +00:00
|
|
|
|
2006-03-18 23:46:50 +00:00
|
|
|
this._objectDataLoaded = true;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Objects = new function(){
|
2006-03-14 11:48:36 +00:00
|
|
|
// Private members
|
|
|
|
var _objects = new Array();
|
|
|
|
|
|
|
|
// Privileged methods
|
|
|
|
this.get = get;
|
|
|
|
this.getAll = getAll;
|
|
|
|
this.reload = reload;
|
|
|
|
this.reloadAll = reloadAll;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieves (and loads, if necessary) an arbitrary number of objects
|
|
|
|
*
|
|
|
|
* Can be passed ids as individual parameters or as an array of ids, or both
|
|
|
|
*/
|
|
|
|
function get(){
|
|
|
|
var toLoad = new Array();
|
|
|
|
var loaded = new Array();
|
|
|
|
|
|
|
|
if (!arguments[0]){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var ids = Scholar.flattenArguments(arguments);
|
|
|
|
|
|
|
|
for (var i=0; i<ids.length; i++){
|
|
|
|
// Check if already loaded
|
|
|
|
if (!_objects[ids[i]]){
|
|
|
|
toLoad.push(ids[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New objects to load
|
|
|
|
if (toLoad.length){
|
|
|
|
_load(toLoad);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build return array
|
|
|
|
for (i=0; i<ids.length; i++){
|
2006-03-17 05:11:03 +00:00
|
|
|
loaded.push(_objects[ids[i]]);
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return loaded;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns all objects in the database
|
|
|
|
*/
|
|
|
|
function getAll(){
|
|
|
|
var sql = 'SELECT O.objectID FROM objects O '
|
|
|
|
+ 'LEFT JOIN objectCreators OC USING (objectID) '
|
|
|
|
+ 'LEFT JOIN creators C USING (creatorID) '
|
|
|
|
+ 'LEFT JOIN folders F ON (O.folderID=F.folderID) '
|
|
|
|
|
|
|
|
// Only get first creator
|
2006-03-17 05:11:03 +00:00
|
|
|
+ 'WHERE OC.orderIndex=0 '
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
// folderID=0 puts root folder items after folders
|
|
|
|
// TODO: allow folders to intermingle with items, order-wise
|
|
|
|
+ 'ORDER BY O.folderID=0, F.orderIndex, O.orderIndex';
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reloads data for specified objects into internal array
|
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reloads all currently cached objects
|
|
|
|
*/
|
|
|
|
function reloadAll(){
|
|
|
|
var ids = new Array();
|
|
|
|
for (objectID in _objects){
|
|
|
|
ids.push(objectID);
|
|
|
|
}
|
|
|
|
_load(ids);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function add(data, objectTypeID, folderID, orderIndex){
|
|
|
|
var insert = new Array();
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
var obj = new Scholar.Object(objectTypeID, folderID, orderIndex);
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
for (field in data){
|
|
|
|
obj.setField(data[field]);
|
|
|
|
}
|
|
|
|
|
|
|
|
var id = obj.save();
|
|
|
|
|
|
|
|
return this.get(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function _load(){
|
|
|
|
if (!arguments){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var sql = 'SELECT O.*, lastName AS firstCreator FROM objects O '
|
|
|
|
+ 'LEFT JOIN objectCreators OC USING (objectID) '
|
|
|
|
+ 'LEFT JOIN creators USING (creatorID) '
|
2006-03-17 05:11:03 +00:00
|
|
|
+ 'WHERE OC.orderIndex=0';
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
if (arguments[0]!='all'){
|
|
|
|
sql += ' AND O.objectID IN (' + Scholar.join(arguments,',') + ')';
|
|
|
|
}
|
|
|
|
|
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-03-20 21:47:22 +00:00
|
|
|
var obj = new Scholar.Object();
|
2006-03-14 11:48:36 +00:00
|
|
|
obj.loadFromRow(result[i]);
|
|
|
|
_objects[result[i]['objectID']] = obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.Creators = new function(){
|
2006-03-14 11:48:36 +00:00
|
|
|
var _creators = new Array; // indexed by first%%%last%%%creatorTypeID hash
|
|
|
|
var _creatorsByID = new Array; // indexed by creatorID
|
|
|
|
|
|
|
|
this.get = get;
|
|
|
|
this.getID = getID;
|
|
|
|
this.add = add;
|
|
|
|
this.purge = purge;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
function getID(firstName, lastName, creatorTypeID){
|
|
|
|
var hash = firstName + '%%%' + lastName + '%%%' + creatorTypeID;
|
|
|
|
|
|
|
|
if (_creators[hash]){
|
|
|
|
return _creators[hash];
|
|
|
|
}
|
|
|
|
|
|
|
|
var sql = 'SELECT creatorID FROM creators WHERE firstName=? AND '
|
|
|
|
+ 'lastName=? AND creatorTypeID=?';
|
|
|
|
var params = [
|
|
|
|
{'string': firstName}, {'string': lastName}, {'int': creatorTypeID}
|
|
|
|
];
|
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
|
|
|
|
*/
|
|
|
|
function add(firstName, lastName, creatorTypeID){
|
|
|
|
Scholar.debug('Adding new creator', 4);
|
|
|
|
|
|
|
|
var sql = 'INSERT INTO creators '
|
|
|
|
+ 'VALUES (?,?,?,?)';
|
|
|
|
|
|
|
|
// Use a random integer for the creatorID
|
|
|
|
var tries = 10; // # of tries to find a unique id
|
|
|
|
var max = 65535;
|
|
|
|
do {
|
|
|
|
// If no luck after 10 tries, try a larger range
|
|
|
|
if (!tries){
|
|
|
|
tries = 10;
|
|
|
|
max = max * 10;
|
|
|
|
}
|
|
|
|
var rnd = Math.floor(Math.random()*max);
|
|
|
|
var sql2 = 'SELECT COUNT(*) FROM creators WHERE creatorID=' + rnd;
|
2006-03-20 21:47:22 +00:00
|
|
|
var exists = Scholar.DB.valueQuery(sql2);
|
2006-03-14 11:48:36 +00:00
|
|
|
tries--;
|
|
|
|
}
|
|
|
|
while (exists);
|
|
|
|
|
|
|
|
var params = [
|
|
|
|
{'int': rnd}, {'int': creatorTypeID},
|
|
|
|
{'string': firstName}, {'string': lastName},
|
|
|
|
];
|
2006-03-20 21:47:22 +00:00
|
|
|
return Scholar.DB.query(sql,params);
|
2006-03-14 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete obsolete creators from database and clear internal array entries
|
|
|
|
*
|
|
|
|
* Returns TRUE on success, or SQL query to run in returnSQL mode
|
|
|
|
*/
|
|
|
|
function purge(returnSQL){
|
|
|
|
var sql = 'SELECT creatorID FROM creators WHERE creatorID NOT IN '
|
|
|
|
+ '(SELECT creatorID FROM objectCreators);';
|
2006-03-20 21:47:22 +00:00
|
|
|
var toDelete = Scholar.DB.columnQuery(sql);
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
if (!toDelete){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sql = 'DELETE FROM creators WHERE creatorID NOT IN '
|
|
|
|
+ '(SELECT creatorID FROM objectCreators);';
|
|
|
|
|
|
|
|
if (!returnSQL){
|
2006-03-20 21:47:22 +00:00
|
|
|
var result = Scholar.DB.query(sql);
|
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]];
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnSQL ? sql : result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function _getHash(creatorID){
|
|
|
|
var creator = this.get(creatorID);
|
|
|
|
if (!creator){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return creator['firstName'] + '%%%' + creator['lastName']
|
|
|
|
+ '%%%' + creator['creatorTypeID'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
Scholar.ObjectFields = new function(){
|
2006-03-14 11:48:36 +00:00
|
|
|
// Private members
|
|
|
|
var _fields = new Array();
|
|
|
|
var _fieldFormats = new Array();
|
|
|
|
var _objectTypeFields = new Array();
|
|
|
|
|
|
|
|
// Privileged methods
|
|
|
|
this.getName = getName;
|
|
|
|
this.getID = getID;
|
|
|
|
this.isValidForType = isValidForType;
|
|
|
|
this.isInteger = isInteger;
|
|
|
|
this.getObjectTypeFields = getObjectTypeFields;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function isValidForType(fieldID, objectTypeID){
|
|
|
|
if (!_fields.length){
|
|
|
|
_loadFields();
|
|
|
|
}
|
|
|
|
return !!_fields[fieldID]['objectTypes'][objectTypeID];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function isInteger(fieldID){
|
|
|
|
if (!_fields.length){
|
|
|
|
_loadFields();
|
|
|
|
}
|
|
|
|
var ffid = _fields[fieldID]['formatID'];
|
|
|
|
return _fieldFormats[ffid] ? _fieldFormats[ffid]['isInteger'] : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns an array of fieldIDs for a given object type
|
|
|
|
*/
|
|
|
|
function getObjectTypeFields(objectTypeID){
|
|
|
|
if (_objectTypeFields[objectTypeID]){
|
|
|
|
return _objectTypeFields[objectTypeID];
|
|
|
|
}
|
|
|
|
|
|
|
|
var sql = 'SELECT fieldID FROM objectTypeFields '
|
|
|
|
+ 'WHERE objectTypeID=' + objectTypeID + ' ORDER BY orderIndex';
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
_objectTypeFields[objectTypeID] = Scholar.DB.columnQuery(sql);
|
2006-03-14 11:48:36 +00:00
|
|
|
return _objectTypeFields[objectTypeID];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns hash array of objectTypeIDs for which a given field is valid
|
|
|
|
*/
|
|
|
|
function _getFieldObjectTypes(){
|
|
|
|
var sql = 'SELECT fieldID,objectTypeID FROM objectTypeFields';
|
|
|
|
|
2006-03-20 21:47:22 +00:00
|
|
|
var results = Scholar.DB.query(sql);
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
if (!results){
|
|
|
|
throw ('No fields in objectTypeFields!');
|
|
|
|
}
|
|
|
|
var fields = new Array();
|
|
|
|
for (var i=0; i<results.length; i++){
|
|
|
|
if (!fields[results[i]['fieldID']]){
|
|
|
|
fields[results[i]['fieldID']] = new Array();
|
|
|
|
}
|
|
|
|
fields[results[i]['fieldID']][results[i]['objectTypeID']] = true;
|
|
|
|
}
|
|
|
|
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!');
|
|
|
|
}
|
|
|
|
|
|
|
|
var fieldObjectTypes = _getFieldObjectTypes();
|
|
|
|
|
|
|
|
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'],
|
|
|
|
objectTypes: fieldObjectTypes[result[i]['fieldID']]
|
|
|
|
};
|
|
|
|
// Store by name as well as id
|
|
|
|
_fields[result[i]['fieldName']] = _fields[result[i]['fieldID']];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-03-20 21:47:22 +00:00
|
|
|
var objects = Scholar.Objects.getAll();
|
2006-03-14 11:48:36 +00:00
|
|
|
|
|
|
|
var obj = objects[9];
|
|
|
|
for (var i=0,len=obj.numCreators(); i<len; i++){
|
|
|
|
Scholar.debug(Scholar.varDump(obj.getCreator(i)));
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.setCreator(2,'bob','smith');
|
|
|
|
|
|
|
|
for (var i=0,len=obj.numCreators(); i<len; i++){
|
|
|
|
Scholar.debug(Scholar.varDump(obj.getCreator(i)));
|
|
|
|
}
|
|
|
|
obj.save();
|
|
|
|
*/
|