'/.+/', ); function getField($field, $insert = FALSE) { $prefix = !$insert ? 'c.' : ''; return "$prefix$field"; } function getPrimaryKey() { return 'comment_id'; } function getTable() { return 'k4_comments'; } function getSelectFields() { return "c.*, u.name AS user_name"; } function getSelectTables() { return "k4_comments c JOIN k4_users u USING( user_id )"; } function getSelectGroupby() { return "c.comment_id"; } } class CommentsRecord extends FARecord { var $_user; var $_update_user_karma = FALSE; var $_update_thread_post_count = FALSE; //-------------------------------------------- // Define what in this records object is cacheable. //-------------------------------------------- function cacheAble() { return array( // table columns 'comment_id', 'thread_id', 'user_id', 'created', 'body', 'karma_risk', 'karma_reward', ); } //-------------------------------------------- // Save the comment //-------------------------------------------- function save() { //-------------------------------------------- // if the comment saved, do the following stuff. //-------------------------------------------- if ($ret = parent::save()) { //-------------------------------------------- // Update the thread's last comment information. //-------------------------------------------- if($this->_update_thread_post_count) { $threads = &$this->getFinder('threads'); $thread = &$threads->find($this->get('thread_id')); // TODO:{[have AR be able to increment a SQL column[save[CommentsRecord} $thread->set('num_comments', 1 + $thread->get('num_comments')); $thread->set('recent_uid', $this->get('user_id')); $thread->set('recent_user', $this->get('user_name')); $thread->set('recent_created', $this->get('created')); $thread->setKarmaForComment(); $thread->save(); } //-------------------------------------------- // Decrease a users karma for posting this thread. //-------------------------------------------- $update_sql = ""; $update_array = array(); if($this->_update_user_karma) { if($this->_user->getId() > K4_GUEST_ID) { $update_sql .= "karma=karma-? "; $update_array[] = $this->get('karma_risk'); } } //-------------------------------------------- // Update the poster, if necessary. //-------------------------------------------- if(!$this->_edit) { $update_sql .= ($update_sql != '' ? ',' : '') . "num_comments=num_comments+1"; } if($update_sql != '') { $update_array[] = $this->_user->getId(); $finder = &$this->getFinder('users'); $finder->updateWhere($update_sql, "user_id=?", $update_array); } } return $ret; } //-------------------------------------------- // Set this comments thread id //-------------------------------------------- function setThread(&$thread) { $this->set('thread_id', $thread->getId()); } //-------------------------------------------- // Set this users information to the comment //-------------------------------------------- function setUser(&$user) { $this->_user = &$user; $this->set('user_id', $user->getId()); } //-------------------------------------------- // Set this comments karma risk.. it's always 0.5 //-------------------------------------------- function setKarma() { $this->set('karma_risk', 0.5); } //-------------------------------------------- // Tell the save function if we should update the // users karma or not. //-------------------------------------------- function setUserKarma() { $this->_update_user_karma = TRUE; } //-------------------------------------------- // Tell the save function if we should update the // thread's last comment information or not. //-------------------------------------------- function setLastComment() { $this->_update_thread_post_count = TRUE; } //-------------------------------------------- // Unparse bbcode and return the original, 'raw' // body text of this comment. //-------------------------------------------- function &getRawBody() { $filter = &new K4BBRevertFilter(); return $filter->filter($this->get('body')); } //-------------------------------------------- // Add in quick tag linking. We don't want to overwrite // getBody because then if we reverted the body text, it // would include those urls. //-------------------------------------------- function &getPostBody() { $filter = &new K4TextTagsFilter(); return $filter->filter($this->get('body')); } //-------------------------------------------- // Hard delete this comment. //-------------------------------------------- function hardDelete() { //-------------------------------------------- // Delete the comment. //-------------------------------------------- $finder = &$this->getFinder('comments'); $finder->delete($this->getId()); //-------------------------------------------- // Update the thread post count. //-------------------------------------------- $this->_dba->executeUpdate("UPDATE k4_threads SET num_comments=num_comments-1 WHERE thread_id=? AND num_comments > 0", array($this->get('thread_id'))); //-------------------------------------------- // Update the author's post count. //-------------------------------------------- if($this->get('user_id') > K4_GUEST_ID) { $finder = &$this->getFinder('users'); $finder->updateWhere("num_comments=num_comments-1", "user_id=? AND num_comments>0", array($this->get('user_id'))); } //-------------------------------------------- // Remove any karma related to this comment. //-------------------------------------------- $finder = &$this->getFinder('post_karma'); $finder->deleteBy('comment_id', $this->getId()); //-------------------------------------------- // Done! //-------------------------------------------- return TRUE; } } ?>