Campustream 1.0
A social network MQP for WPI
|
00001 <? 00002 00006 class Newscomment_Model extends ActiveRecord { 00007 public $table_name = 'newscomments'; 00008 public $columns = array('id', 'posted_by', 'news_id', 'parent_id', 'content', 'time_posted', 'deleted'); 00009 public $cache_keys = array( 'newscomment/#' => 'id', 'newscomment/user:#' => 'posted_by', 'newscomment/parent:#' => 'parent_id', 'newscomment/news:#' => 'news_id'); 00010 public $has_many = array('children'); 00011 public $has_one = array('parent_comment', 'user', 'news'); 00012 public $public_columns = array( 'id', 'posted_by', 'news_id', 'parent_id', 'content', 'time_posted', 'children' ); 00013 00017 public static function load_comment_tree($news_id) { 00018 $r = RedisManager::connection(); 00019 $key = "news:{$news_id}:comment:0"; 00020 $comments = array(); 00021 00022 $len = $r->llen($key); 00023 if ($len) { 00024 $ids = $r->lrange($key, 0, $len); 00025 00026 foreach ($ids as $id) { 00027 $comment = ActiveCache::find('Newscomment_Model', $id, 43200)->sql( 00028 "SELECT * FROM newscomments WHERE id = $id LIMIT 1" 00029 ); 00030 00031 $comment->load_user(); 00032 $comment->load_children(true); 00033 00034 $comments[] = $comment; 00035 } 00036 } 00037 00038 return $comments; 00039 } 00040 00044 public function load_user() { 00045 if (!$this->user) { 00046 $user = ActiveCache::find('User_Model', $this->posted_by, 43200)->sql( 00047 "SELECT * FROM users WHERE id = {$this->posted_by} LIMIT 1" 00048 ); 00049 00050 if ($user->is_loaded()) { 00051 $this->add_relationship('user', $user); 00052 } 00053 } 00054 } 00055 00059 public function load_news() { 00060 if (!$this->news) { 00061 $news = ActiveCache::find('Newsevent_Model', $this->news_id, 43200)->sql( 00062 "SELECT * FROM newsevents WHERE id = {$this->news_id} LIMIT 1" 00063 ); 00064 00065 $this->add_relationship('news', $news); 00066 } 00067 } 00068 00072 public function load_parent() { 00073 if (!$this->parent_comment && $this->parent_id > 0) { 00074 $parent = ActiveCache::find('Newscomment_Model', $this->parent_id, 43200)->sql( 00075 "SELECT * FROM newscomments WHERE id = {$this->parent_id} LIMIT 1" 00076 ); 00077 00078 if ($parent->is_loaded() && $parent->deleted == 0) { 00079 $this->add_relationship('parent_comment', $parent); 00080 } 00081 } 00082 } 00083 00089 public function load_children($recursive = false) { 00090 $r = RedisManager::connection(); 00091 $key = "news:{$this->news_id}:comment:{$this->id}"; 00092 00093 $len = $r->llen($key); 00094 if ($len) { 00095 $child_ids = $r->lrange($key, 0, $len); 00096 00097 $children = array(); 00098 foreach($child_ids as $child_id) { 00099 $child = ActiveCache::find('Newscomment_Model', $child_id, 43200)->sql( 00100 "SELECT * FROM newscomments WHERE id = $child_id LIMIT 1" 00101 ); 00102 00103 if ($child->is_loaded()) { 00104 $child->load_user(); 00105 if ($recursive) { 00106 $child->load_children(true); 00107 } 00108 } 00109 00110 $children[] = $child; 00111 } 00112 00113 $this->add_relationship('children', $children); 00114 } else { 00115 $this->add_relationship('children', array()); 00116 } 00117 } 00118 00122 public function save($new = true) { 00123 parent::save(); 00124 00125 if (!$new) { return; } 00126 00127 $r = RedisManager::connection(); 00128 $key = "news:{$this->news_id}:comment:{$this->parent_id}"; 00129 00130 $r->rpush($key, $this->id); 00131 00132 $this->load_news(); 00133 $this->news->num_comments++; 00134 $this->news->save(); 00135 00136 $this->load_parent(); 00137 if ($this->posted_by != $this->news->posted_by) { 00138 $n = notifications::load(); 00139 $n->send($this->news->posted_by, 'news', "has posted a comment on your news/event: {$this->news->title}", $this->news->short_id); 00140 $this->news->load_user(); 00141 00142 email::send_notification($this->news->user, "has posted a comment on your news/event: {$this->news->title}", "/ne/{$this->news->short_id}"); 00143 } 00144 00145 if ($this->parent_comment && $this->parent_comment->posted_by != $this->posted_by) { 00146 $n->send($this->parent_comment->posted_by, 'newscomment', "has replied to your comment from: {$this->news->title}", $this->news->short_id); 00147 00148 $this->parent_comment->load_user(); 00149 email::send_notification($this->parent_comment->user, "has replied to your comment from: {$this->news->title}", "/ne/{$this->news->short_id}"); 00150 } 00151 } 00152 00156 public function delete() { 00157 $this->deleted = 1; 00158 $this->save(false); 00159 } 00160 00161 public function __get($key) { 00162 switch ($key) { 00163 case 'childs': return $this->children; 00164 } 00165 00166 return parent::__get($key); 00167 } 00168 }