/** * Gets the latest live data about the current and next subject to be live * * */ public function getLiveData($subject_id = 0, $comment_id, $width = 0, $height = 0, $keepratio = true) { $arr_data = array(); $arr_comments = array(); $arr_data['new_comment'] = 0; $arr_data['new_sub'] = 0; //TODO: Store the whole subject record and its content as an array on the live_subject table $live_subject = Yii::app()->db->createCommand()->select('*')->from('live_subject')->queryRow(); //returns an array, not an object $arr_data['subject_id'] = $live_subject['subject_id']; $arr_data['comment_id'] = $live_subject['comment_id']; //If the subject cached on client's device its the same that the live_subject table indicates to be cached... if ($subject_id != $live_subject['subject_id']) { $subject_data = unserialize($live_subject['subject_data']); $arr_data['title'] = $subject_data->title; $arr_data['content_type_id'] = $subject_data->content_type_id; $arr_data['content_type'] = strtolower($subject_data->content_type->name); $arr_data['priority'] = strtolower($subject_data->priority_type->name); $country = Country::model()->findByPk($subject_data->country_id); $arr_data['country_code'] = $country->code ? $country->code : 'WW'; $arr_data['country_name'] = $country->name ? $country->name : 'WORLD'; $user = User::model()->findByPk($subject_data->user_id); $arr_data['username'] = $user->username; $arr_data['user_firstname'] = $user->firstname; $arr_data['user_lastname'] = $user->lastname; $arr_data['user_image'] = $user->getUserImage("small_"); $arr_data['content_html'] = SiteHelper::subject_content($subject_data); $arr_data['content_data'] = (array) Subject::subject_content($subject_data)->getAttributes(); if ($arr_data['content_type'] == 'image') { $img_name = $arr_data['content_data']['id'] . "." . $arr_data['content_data']['extension']; $url_base = Yii::app()->getRequest()->getBaseUrl(true) . '/' . $arr_data['content_data']['path'] . '/'; if ($width or $height) { $new_img_name = SiteLibrary::get_image_resized($img_name, Yii::app()->params['webdir'] . DIRECTORY_SEPARATOR . $arr_data['content_data']['path'], $width, $height, $keepratio); if ($new_img_name) { $arr_data['content_data']['image_url'] = $url_base . $new_img_name; } else { $arr_data['content_data']['image_url'] = $url_base . $img_name; } } else { $arr_data['content_data']['image_url'] = $url_base . $img_name; } } $arr_data['user_comment'] = SiteHelper::formatted($subject_data->user_comment); $arr_data['time_submitted'] = $subject_data->time_submitted; $arr_data['display_time'] = $subject_data->show_time; $arr_data['scheduled_time'] = $subject_data->position; if ($subject_id != $live_subject['subject_id']) { $arr_data['new_sub']++; } $arr_data['likes'] = $subject_data->likes; $arr_data['dislikes'] = $subject_data->dislikes; $arr_data['urn'] = $subject_data->urn; $arr_data['permalink'] = Yii::app()->getRequest()->getBaseUrl(true) . "/sub/" . $subject_data->urn; //Send the last two previous subjects $last_subs = Yii::app()->db->createCommand()->select('*')->from('subject')->where('show_time>:show_time AND id <>:id1', array(':show_time' => 0, ':id1' => $live_subject['subject_id']))->order('show_time DESC')->limit(5)->queryAll(); $arr_data['last_sub_title'] = $last_subs[0]['title']; $arr_data['last_sub_2_title'] = $last_subs[1]['title']; $arr_data['last_sub_urn'] = $last_subs[0]['urn']; $arr_data['last_sub_2_urn'] = $last_subs[1]['urn']; } //Search comments if ($subject_id != $live_subject['subject_id']) { $comment_id = 0; } $live_comments = Yii::app()->db->createCommand()->select('*')->from('live_comment')->where('comment_id > :comment_id AND subject_id = :subject_id', array(':comment_id' => $comment_id, ':subject_id' => $live_subject['subject_id']))->order('comment_id ASC')->queryAll(); foreach ($live_comments as $live_comment) { $arr_data['new_comment']++; $arr_data['comment_id'] = $live_comment['comment_id']; $user = User::model()->find('username=:username', array(':username' => $live_comment['username'])); $arr_comments[] = array('comment_id' => $live_comment['comment_id'], 'username' => $live_comment['username'], 'user_firstname' => $user->firstname, 'user_lastname' => $user->lastname, 'user_image' => $user->getUserImage("small_"), 'comment_text' => CHtml::encode($live_comment['comment_text']), 'comment_number' => $live_comment['comment_number'], 'comment_time' => date("H:i:s", $live_comment['comment_time']), 'comment_country' => $live_comment['comment_country'], 'likes' => $live_comment['likes'], 'dislikes' => $live_comment['dislikes']); } $arr_data['comments'] = $arr_comments; //Add user session information if (!$subject_id and !Yii::app()->user->isGuest) { $arr_data['session_userid'] = Yii::app()->user->id; $arr_data['session_username'] = Yii::app()->user->name; $session_user = User::model()->findByPk((int) Yii::app()->user->id); $arr_data['session_userimage'] = $session_user->getUserImage("small_"); } //Set times $utc_time = SiteLibrary::utc_time(); $arr_data['current_time'] = $utc_time; $arr_data['current_time_h'] = date("H", $utc_time); $arr_data['current_time_m'] = date("i", $utc_time); $arr_data['current_time_s'] = date("s", $utc_time); $arr_data['time_remaining'] = $live_subject['scheduled_time'] + Yii::app()->params['subject_interval'] * 60 - $utc_time + 2; //lets give some seconds rage in case cron gets delayed return $arr_data; }