/* * Constructor for Item object * * Generally should be called through Scholar.Items rather than directly */ Scholar.Item = function(){ this._init(); // Accept itemTypeID in constructor if (arguments.length){ this.setType(arguments[0]); } } Scholar.Item.prototype._init = function(){ // // Public members for access by public methods -- do not access directly // this._data = new Array(); this._creators = new Scholar.Hash(); this._itemData = new Array(); this._creatorsLoaded = false; this._itemDataLoaded = false; this._changed = new Scholar.Hash(); this._changedCreators = new Scholar.Hash(); this._changedItemData = new Scholar.Hash(); } ////////////////////////////////////////////////////////////////////////////// // // Public Scholar.Item methods // ////////////////////////////////////////////////////////////////////////////// /* * Check if the specified field is a primary field from the items table */ Scholar.Item.prototype.isPrimaryField = function(field){ // Create primaryFields hash array if not yet created if (!Scholar.Item.primaryFields){ Scholar.Item.primaryFields = Scholar.DB.getColumnHash('items'); Scholar.Item.primaryFields['firstCreator'] = true; Scholar.Item.primaryFields['numNotes'] = true; } return !!Scholar.Item.primaryFields[field]; } Scholar.Item.editableFields = { title: true }; /* * Check if the specified primary field can be changed with setField() */ Scholar.Item.prototype.isEditableField = function(field){ return !!Scholar.Item.editableFields[field]; } /* * Build object from database */ Scholar.Item.prototype.loadFromID = function(id){ // Should be the same as query in Scholar.Items.loadFromID, just // without itemID clause var sql = 'SELECT I.*, lastName || ' + 'CASE ((SELECT COUNT(*) FROM itemCreators WHERE itemID=' + id + ')>1) ' + "WHEN 0 THEN '' ELSE ' et al.' END AS firstCreator, " + "(SELECT COUNT(*) FROM itemNotes WHERE sourceItemID=I.itemID) AS numNotes " + 'FROM items I ' + 'LEFT JOIN itemCreators IC ON (I.itemID=IC.itemID) ' + 'LEFT JOIN creators C ON (IC.creatorID=C.creatorID) ' + 'WHERE itemID=' + id + ' AND (IC.orderIndex=0 OR IC.orderIndex IS NULL)'; // first creator var row = Scholar.DB.rowQuery(sql); this.loadFromRow(row); } /* * Populate basic item data from a database row */ Scholar.Item.prototype.loadFromRow = function(row){ this._init(); for (col in row){ // Only accept primary field data through loadFromRow() if (this.isPrimaryField(col)){ this._data[col] = row[col]; } else { Scholar.debug(col + ' is not a valid primary field'); } } return true; } /* * Check if any data fields have changed since last save */ Scholar.Item.prototype.hasChanged = function(){ return (this._changed.length || this._changedCreators.length || this._changedItemData.length); } Scholar.Item.prototype.getID = function(){ return this._data['itemID'] ? this._data['itemID'] : false; } Scholar.Item.prototype.getType = function(){ return this._data['itemTypeID'] ? this._data['itemTypeID'] : false; } /* * Set or change the item's type */ Scholar.Item.prototype.setType = function(itemTypeID){ if (itemTypeID==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 itemTypeFields ' + 'WHERE itemTypeID=' + this.getType() + ' AND fieldID NOT IN ' + '(SELECT fieldID FROM itemTypeFields WHERE itemTypeID=' + itemTypeID + ')'; var obsoleteFields = Scholar.DB.columnQuery(sql); if (obsoleteFields){ for (var i=0; i1) ' + "WHEN 0 THEN '' ELSE ' et al.' END AS firstCreator, " + "(SELECT COUNT(*) FROM itemNotes WHERE sourceItemID=I.itemID) AS numNotes " + 'FROM items I ' + 'LEFT JOIN itemCreators IC ON (I.itemID=IC.itemID) ' + 'LEFT JOIN creators C ON (IC.creatorID=C.creatorID) ' + 'WHERE (IC.orderIndex=0 OR IC.orderIndex IS NULL)'; if (arguments[0]){ sql += ' AND I.itemID IN (' + Scholar.join(arguments,',') + ')'; } var result = Scholar.DB.query(sql); if (result){ for (var i=0,len=result.length; i