antville/code/Comment/Comment.js
Tobi Schäfer 896f5daf28 * Added Membership.comments collection to fix a bug causing display of the number of comments of a user throughout the whole installation instead of those of the member of the site only
* Replaced custom SQL query in Comment.remove() with code using new Membership.comments collection
 * Removed obsolete Sql.COMMENTS statement
 * Added getConfirmText() method to Comment, File, Image, Layout, Membership, Poll, Skin and Story prototypes – which is used in HopObject.delete_action() for more convenient feedback about what is going to be deleted (fixes issue 37 for now)
 * Fully enabled translation in global breadcrumb_macro()
 * Removed obsolete Layout.getTitle() method
 * Disabled translation in HopObject.toString() methods
 * Fixed and added some i18n messages to ease translation
2010-02-06 15:13:39 +00:00

197 lines
5.1 KiB
JavaScript

//
// The Antville Project
// http://code.google.com/p/antville
//
// Copyright 2001-2007 by The Antville People
//
// Licensed under the Apache License, Version 2.0 (the ``License'');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an ``AS IS'' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// $Revision$
// $LastChangedBy$
// $LastChangedDate$
// $URL$
//
/**
* @fileOverview Defines the Comment prototype.
*/
/**
* @see defineConstants
*/
Comment.getStatus = defineConstants(Comment, "closed",
"pending", "readonly", "public");
/**
* @returns {String}
*/
Comment.remove = function(options) {
if (this.constructor !== Comment) {
return;
}
if (options && options.mode === "user" && options.confirm === "1") {
var membership = Membership.getByName(this.creator.name, this.site);
HopObject.remove.call(membership.comments);
} else {
while (this.size() > 0) {
Comment.remove.call(this.get(0));
}
// Explicitely remove comment from aggressively cached collections:
(this.parent || this).removeChild(this);
this.story.comments.removeChild(this);
this.remove();
}
return this.parent.href();
}
/**
* @name Comment
* @constructor
* @param {Object} parent
* @property {Comment[]} _children
* @property {String} name
* @property {Story|Comment} parent
* @property {Story} story
* @extends Story
*/
Comment.prototype.constructor = function(parent) {
this.name = String.EMPTY;
this.site = parent.site;
this.story = parent.story || parent;
this.parent = parent;
// FIXME: Correct parent_type (Helma bug?)
this.parent_type = parent._prototype;
this.status = Story.PUBLIC;
this.creator = this.modifier = session.user;
this.created = this.modified = new Date;
return this;
}
/**
*
* @param {Object} action
* @returns {Boolean}
*/
Comment.prototype.getPermission = function(action) {
switch (action) {
case ".":
case "main":
case "comment":
// FIXME: temporary fix for lost stories due to shrunk database
if (!this.story) {
return false;
}
return this.site.commentMode === Site.ENABLED &&
this.story.getPermission(action) &&
this.status !== Comment.CLOSED &&
this.status !== Comment.PENDING;
case "delete":
case "edit":
return this.story.getPermission.call(this, "delete");
}
return false;
}
/**
*
* @param {Object} action
* @returns {String}
*/
Comment.prototype.href = function(action) {
var buffer = [];
switch (action) {
case null:
case undefined:
case "":
case ".":
case "main":
buffer.push(this.story.href(), "#", this._id);
break;
default:
buffer.push(this.story.comments.href(), this._id, "/", action);
}
return buffer.join(String.EMPTY);
}
Comment.prototype.edit_action = function() {
if (req.postParams.save) {
try {
this.update(req.postParams);
delete session.data.backup;
res.message = gettext("The comment was successfully updated.");;
res.redirect(this.story.href() + "#" + this._id);
} catch (ex) {
res.message = ex;
app.log(ex);
}
}
res.handlers.parent = this.parent;
res.data.action = this.href(req.action);
res.data.title = gettext("Edit Comment");
res.data.body = this.renderSkinAsString("Comment#edit");
this.site.renderSkin("Site#page");
return;
}
/**
*
* @param {Object} data
*/
Comment.prototype.update = function(data) {
if (!data.title && !data.text) {
throw Error(gettext("Please enter at least something into the “title” or “text” field."));
}
// Get difference to current content before applying changes
var delta = this.getDelta(data);
this.title = data.title;
this.text = data.text;
this.setMetadata(data);
if (this.story.commentMode === Story.MODERATED) {
this.mode = Comment.PENDING;
} else if (delta > 50) {
this.modified = new Date;
if (this.story.status !== Story.CLOSED) {
this.site.modified = this.modified;
}
// We need persistence for adding the callback
this.isTransient() && this.persist();
res.handlers.site.callback(this);
// Notification is sent in Story.comment_action()
}
this.clearCache();
this.modifier = session.user;
return;
}
/**
*
* @param {String} name
* @returns {HopObject}
*/
Comment.prototype.getMacroHandler = function(name) {
if (name === "related") {
var membership = Membership.getByName(this.creator.name, this.site)
return membership.comments;
}
return null;
}
/**
* @returns {String}
*/
Comment.prototype.getConfirmText = function() {
return gettext("You are about to delete a comment by user {0}.",
this.creator.name);
}