1 // 2 // The Antville Project 3 // http://code.google.com/p/antville 4 // 5 // Copyright 2001-2007 by The Antville People 6 // 7 // Licensed under the Apache License, Version 2.0 (the ``License''); 8 // you may not use this file except in compliance with the License. 9 // You may obtain a copy of the License at 10 // 11 // http://www.apache.org/licenses/LICENSE-2.0 12 // 13 // Unless required by applicable law or agreed to in writing, software 14 // distributed under the License is distributed on an ``AS IS'' BASIS, 15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 // See the License for the specific language governing permissions and 17 // limitations under the License. 18 // 19 // $Revision$ 20 // $LastChangedBy$ 21 // $LastChangedDate$ 22 // $URL$ 23 // 24 25 /** 26 * @fileOverview Defines the Comment prototype. 27 */ 28 29 /** 30 * @see defineConstants 31 */ 32 Comment.getStatus = defineConstants(Comment, "closed", 33 "pending", "readonly", "public"); 34 35 36 /** 37 * @returns {String} 38 */ 39 Comment.remove = function() { 40 if (this.constructor !== Comment) { 41 return; 42 } 43 while (this.size() > 0) { 44 Comment.remove.call(this.get(0)); 45 } 46 // Force removal from aggressively cached collections: 47 (this.parent || this).removeChild(this); 48 this.story.comments.removeChild(this); 49 this.remove(); 50 return this.parent.href(); 51 } 52 53 /** 54 * @name Comment 55 * @constructor 56 * @param {Object} parent 57 * @property {Comment[]} _children 58 * @property {String} name 59 * @property {Story|Comment} parent 60 * @property {Story} story 61 * @extends Story 62 */ 63 Comment.prototype.constructor = function(parent) { 64 this.name = String.EMPTY; 65 this.site = parent.site; 66 this.story = parent.story || parent; 67 this.parent = parent; 68 // FIXME: Correct parent_type (Helma bug?) 69 this.parent_type = parent._prototype; 70 this.status = Story.PUBLIC; 71 this.creator = this.modifier = session.user; 72 this.created = this.modified = new Date; 73 return this; 74 } 75 76 /** 77 * 78 * @param {Object} action 79 * @returns {Boolean} 80 */ 81 Comment.prototype.getPermission = function(action) { 82 switch (action) { 83 case ".": 84 case "main": 85 case "comment": 86 // FIXME: temporary fix for lost stories due to shrunk database 87 if (!this.story) { 88 return false; 89 } 90 return this.site.commentMode === Site.ENABLED && 91 this.story.getPermission(action) && 92 this.status !== Comment.CLOSED && 93 this.status !== Comment.PENDING; 94 case "delete": 95 case "edit": 96 return this.story.getPermission.call(this, "delete"); 97 } 98 return false; 99 } 100 101 /** 102 * 103 * @param {Object} action 104 * @returns {String} 105 */ 106 Comment.prototype.href = function(action) { 107 var buffer = []; 108 switch (action) { 109 case null: 110 case undefined: 111 case "": 112 case ".": 113 case "main": 114 buffer.push(this.story.href(), "#", this._id); 115 break; 116 default: 117 buffer.push(this.story.comments.href(), this._id, "/", action); 118 } 119 return buffer.join(String.EMPTY); 120 } 121 122 Comment.prototype.edit_action = function() { 123 if (req.postParams.save) { 124 try { 125 this.update(req.postParams); 126 delete session.data.backup; 127 res.message = gettext("The comment was successfully updated.");; 128 res.redirect(this.story.href() + "#" + this._id); 129 } catch (ex) { 130 res.message = ex; 131 app.log(ex); 132 } 133 } 134 135 res.handlers.parent = this.parent; 136 res.data.action = this.href(req.action); 137 res.data.title = gettext("Edit comment to story: {0}", 138 res.handlers.story.getTitle()); 139 res.data.body = this.renderSkinAsString("Comment#edit"); 140 this.site.renderSkin("Site#page"); 141 return; 142 } 143 144 /** 145 * 146 * @param {Object} data 147 */ 148 Comment.prototype.update = function(data) { 149 if (!data.title && !data.text) { 150 throw Error(gettext("Please enter at least something into the 'title' or 'text' field.")); 151 } 152 // Get difference to current content before applying changes 153 var delta = this.getDelta(data); 154 this.title = data.title; 155 this.text = data.text; 156 this.setMetadata(data); 157 158 if (this.story.commentMode === Story.MODERATED) { 159 this.mode = Comment.PENDING; 160 } else if (delta > 50) { 161 this.modified = new Date; 162 if (this.story.status !== Story.CLOSED) { 163 this.site.modified = this.modified; 164 } 165 // We need persistence for adding the callback 166 this.isTransient() && this.persist(); 167 res.handlers.site.callback(this); 168 // FIXME: Where did this.notify(req.action) go? 169 } 170 this.clearCache(); 171 this.modifier = session.user; 172 return; 173 } 174