Campustream 1.0
A social network MQP for WPI
application/models/statuscomment.php
Go to the documentation of this file.
00001 <?
00002 
00006 class Statuscomment_Model extends ActiveRecord {
00007         public $table_name = 'statuscomments';
00008         public $columns = array('id', 'user_id', 'status_id', 'message', 'post_date', 'deleted');
00009         public $cache_keys = array( 'statuscomment/#' => 'id', 'statuscomments/user:#' => 'user_id', 'statuscomments/status:#' => 'status_id');
00010         public $has_many = array();
00011         public $has_one = array('status', 'user');
00012         public $public_columns = array( 'id', 'user_id', 'status_id', 'message', 'post_date', 'user' );
00013         
00017         public function load_user() {
00018                 if (!$this->user) {
00019                         $user = ActiveCache::find('User_Model', $this->user_id, 43200)->sql(
00020                                 "SELECT * FROM users WHERE id = {$this->user_id} LIMIT 1"
00021                         );
00022                         
00023                         if ($user->is_loaded()) {
00024                                 $this->add_relationship('user', $user);
00025                         }
00026                 }
00027         }
00028         
00032         public function load_status() {
00033                 if (!$this->status) {
00034                         $status = ActiveCache::find('Status_Model', $this->status_id, 43200)->sql(
00035                                 "SELECT * FROM statuses WHERE id = {$this->status_id} LIMIT 1"
00036                         );
00037                         
00038                         if ($status->is_loaded()) {
00039                                 $this->add_relationship('status', $status);
00040                         }
00041                 }
00042         }
00043         
00047         public function save() {
00048                 parent::save();
00049                 
00050                 ActiveCache::invalidate_keys_for($this);
00051                 
00052                 $this->load_status();
00053                 
00054                 if ($this->user_id == $this->status->user_id) {
00055                         return;
00056                 }
00057                 
00058                 $n = notifications::load();
00059                 $n->send($this->status->user_id, 'reply', "has replied to your status message.", $this->status->short_id);
00060                 
00061                 $this->status->load_user();
00062                 email::send_notification($this->status->user, "has replied to your status message.", "/s/" . $this->status->short_id);
00063         }
00064         
00065         public function __get($key) {
00066                 switch ($key) {
00067                         case 'user':
00068                                 $this->load_user();
00069                                 return parent::__get('user');
00070                 }
00071                 
00072                 return parent::__get($key);
00073         }
00074 }