Campustream 1.0
A social network MQP for WPI
application/models/status.php
Go to the documentation of this file.
00001 <?
00002 
00006 class Status_Model extends ActiveRecord {
00007 
00008         public $table_name = 'statuses';
00009         public $columns = array('id', 'user_id', 'source', 'message', 'post_date', 'type', 'content_id', 'deleted', 'is_public');
00010         public $cache_keys = array( 'status/#' => 'id', 'statuses/user:#' => 'user_id');
00011         public $has_one = array('meta', 'content');
00012         public $has_many = array();
00013         public $public_columns = array( 'id', 'user_id', 'source', 'message', 'post_date', 'type', 'content_id', 'user', 'comments', 'num_comments', 'meta', 'content' );
00014         
00018         public static $list_max_length = 200;
00019         
00023         public function save($new_status = true) {              
00024                 // First we save to MySQL
00025                 parent::save();
00026                 
00027                 if (!$new_status) { return true; }
00028                 
00029                 $this->load_user();
00030                 $this->user->current_status = $this->id;
00031                 $this->user->save();
00032                 
00033                 if ($this->is_public) {
00034                         stream::publish(stream::STREAM_PUBLIC, $this->type, $this->id);
00035                 }
00036                         
00037                 stream::publish(stream::STREAM_FRIENDS, $this->type, $this->id);
00038                 stream::publish(stream::STREAM_USER, $this->type, $this->id);
00039                 
00040                 if ($this->type == 'text') {
00041                         $r = RedisManager::connection();
00042                         
00043                         if ($this->user->has_twitter() && $this->user->twitter()->export_enabled) {
00044                                 $data = array(
00045                                         'user' => $this->user_id,
00046                                         'payload' => $this->message
00047                                 );
00048                                 
00049                                 $r->rpush('queue:twitter:statuses', serialize($data));
00050                         }
00051                         
00052                         if ($this->user->has_facebook() && $this->user->facebook()->export_enabled) {
00053                                 $data = array(
00054                                         'user' => $this->user_id,
00055                                         'payload' => $this->message
00056                                 );
00057                                 
00058                                 $r->rpush('queue:facebook:statuses', serialize($data));
00059                         }
00060                 }
00061         }
00062         
00066         public function delete() {
00067                 // soft-delete
00068                 $this->deleted = 1;
00069                 $this->save(false);
00070                 
00071                 $r = RedisManager::connection();
00072                 
00073                 $last_status = 0;
00074                 if ($r->llen("statuses:user_timeline:{$this->user_id}") > 0) {
00075                         $last_status = reset($r->lrange("statuses:user_timeline:{$this->user_id}", 0, 1));
00076                 }
00077                 
00078                 $this->load_user();
00079                 $this->user->current_status = $last_status;
00080                 $this->user->save();
00081                 
00082                 ActiveCache::invalidate_keys_for($this);
00083                 
00084                 return true;
00085         }
00086         
00090         public function load_user() {
00091                 if (!$this->user) {
00092                         $user = ActiveCache::find('User_Model', $this->user_id, 43200)->sql(
00093                                 "SELECT * FROM users WHERE id = {$this->user_id} LIMIT 1"
00094                         );
00095                         
00096                         if ($user->is_loaded()) {
00097                                 $this->add_relationship('user', $user);
00098                         }
00099                 }
00100         }
00101         
00105         public function load_comments() {
00106                 if (!$this->comments) {
00107                         $comments = ActiveCache::find_all('Statuscomment_Model', "status:{$this->id}", 43200)->sql(
00108                                 "SELECT * FROM statuscomments WHERE status_id = {$this->id} ORDER BY id ASC"
00109                         );
00110                         
00111                         if (count($comments) > 0) {
00112                                 $this->add_relationship('comments', $comments);
00113                         }
00114                 }
00115         }
00116         
00120         public function load_meta() {
00121                 if (!$this->meta) {
00122                         $model = ucfirst($this->type) . '_Model';
00123                         $table = $this->type . 's';
00124                         $meta = ActiveCache::find($model, $this->content_id, 43200)->sql(
00125                                 "SELECT * FROM {$table} WHERE id = {$this->content_id} LIMIT 1"
00126                         );
00127                         
00128                         if ($meta->is_loaded()) {
00129                                 $this->add_relationship('meta', $meta);
00130                         }
00131                 }
00132         }
00133         
00137         public function load_content() {
00138                 if ($this->type == 'text' || !$this->content_id) { return; }
00139                 
00140                 if (!$this->content) {
00141                         $model = null;
00142                         $table = null;
00143                         
00144                         switch ($this->type) {
00145                                 case 'question':
00146                                         $model = 'Question_Model';
00147                                         $table = 'questions';
00148                                         break;
00149                                 case 'response':
00150                                         $model = 'Response_Model';
00151                                         $table = 'responses';
00152                                         break;
00153                         }
00154                         
00155                         $content = ActiveCache::find($model, $this->content_id, 43200)->sql(
00156                                 "SELECT * FROM $table WHERE id = {$this->content_id} LIMIT 1"
00157                         );
00158                         
00159                         if ($content->is_loaded()) {
00160                                 if ($this->type == 'response') {
00161                                         $content->load_question();
00162                                         $content->question->load_tags();
00163                                         $content->question->load_responses();
00164                                         $content->question->load_user();
00165                                 } elseif ($this->type == 'question') {
00166                                         $content->load_responses();
00167                                         $content->load_tags();
00168                                 }
00169                                 
00170                                 $this->add_relationship('content', $content);
00171                         }
00172                 }
00173         }
00174         
00175         private function num_comments() {
00176                 $this->load_comments();
00177                 return count($this->comments);
00178         }
00179         
00180         public function __get($key) {
00181                 switch ($key) {
00182                         case 'num_comments': return $this->num_comments();
00183                         case 'short_id': return base_convert($this->id, 10, 36);
00184                 }
00185                 
00186                 return parent::__get($key);
00187         }
00188 
00189 }