<?php
class MessengerController extends CommonCustomControllerAction
{
const DEFAULT_INTERVAL= 179; //60s=1min , +1 shall be added later. this is default ts_lastmsg
//todo: need to see if we can come up with some optimal value for the above
private $default_widget_settings= array(
'title' => 'Messenger',
'width' => 200,
'height' => 500,
'minwidth' => 100,
'minheight' => 200,
'closebutton' => true,
'minimizebutton'=> true,
'mode' => ''
);
public function indexAction()
{
//show the archive links, just like any other webobject controller
//TODO: implement later
$this->msg= null;
}
public function onlineusersAction()
{
$onlineusers= DatabaseObject_OnlineUser::getOnlineUsers($this->db);
}
public function sendAction()
{
$logger= Zend_Registry::get('logger');
DatabaseObject_OnlineUser::updateKeepAlive($this->db, $this->user_id);
$request= $this->getRequest();
$ts_lastmsg= trim($request->getPost('ts_lastchatmsg'));
if ($request->isPost()) {
$fp= new FormProcessor_Messenger($this->db, $this->user_id);
$errors= array();
if ($fp->process($request)) {
//do nothing
}
else {
$errors= $fp->getError('msgerrors');
}
$this->msg= $fp->message;
$this->sendUnreadMessages($ts_lastmsg, $errors);
}
}
public function widgetAction()
{
DatabaseObject_OnlineUser::updateKeepAlive($this->db, $this->user_id);
$this->view->extra_scripts= array(
'/js/messenger.js'
);
$this->view->extra_styles= array(
'/css/messenger-widget.css',
'/css/widget.css'
);
$request= $this->getRequest();
$ts_lastmsg= time(); //this has to be here to make sure that this ts is less than the to ts sent in the db query
$messages= $this->getUnreadMessages();
$msgindex= 0;
$this->msgobjects= array();
foreach($messages as $msg_id=> $message) {
$this->msgobjects[$msgindex]= $this->fillObjectInfo($message);
if ($ts_lastmsg < $message->ts_sent)
$ts_lastmsg= $message->ts_sent;
$msgindex++;
}
$this->view->settings= $this->loadWidgetSettings();
$this->view->msgs= $this->msgobjects;
$this->view->ts_lastchatmsg= $ts_lastmsg;
}
private function fillObjectInfo($msg)
{
if ($msg->from_user_id == $this->user_id) {
$message['username']= 'me';
}
else {
$message['username']= DatabaseObject_User::
getUserNamefromId($this->db,
$msg->from_user_id);
}
$message['ts_sent']= $msg->ts_sent;
$message['message_text']= $msg->message_text;
return $message;
}
private function getUnreadMessages($ts_lastchatmsg= '')
{
$ts_present= time();
if ($ts_lastchatmsg != '') {
$ts_lastchatmsg= strtotime($ts_lastchatmsg);
if ($ts_present - $ts_lastchatmsg > 2000) { //need to decide on the optimal replace for 2000
//some dummy time stamp received. so just ignore and fallback
$ts_lastchatmsg= $ts_present- self::DEFAULT_INTERVAL;
}
}
else {
$ts_lastchatmsg= $ts_present- self::DEFAULT_INTERVAL;
}
$to = $ts_present;
$from = $ts_lastchatmsg+1;
$options['from'] = date('Y-m-d H:i:s', $from);
$options['to'] = date('Y-m-d H:i:s', $to);
$options['to_user_id']= $this->user_id;
$messages= DatabaseObject_Message::GetMessages($this->db, $options);
return $messages;
}
public function receiveAction()
{
DatabaseObject_OnlineUser::updateKeepAlive($this->db, $this->user_id);
$request= $this->getRequest();
$ts_lastmsg= trim($request->getPost('ts_lastchatmsg'));
$this->msg= null; //fixme: need to fix this dirty hack
$this->sendUnreadMessages($ts_lastmsg);
}
protected function sendUnreadMessages($ts_lastmsg, $errors= '')
{
$present_time= time(); //to make sure this is <= the ts sent in db query
$messages= $this->getUnreadMessages($ts_lastmsg);
$newmessages= array();
$msgindex= 0;
$ts_lastmsg= $present_time;
foreach($messages as $msg_id=> $message) {
if (($this->msg != null) && ($this->msg->getId() == $msg_id)) {
$this->msg= null;
} //todo: replace the above with better logic
$newmessages[$msgindex]= $this->fillObjectInfo($message);
if ($ts_lastmsg < $message->ts_sent)
$ts_lastmsg= $message->ts_sent;
$msgindex++;
}
if ($this->msg != null) {
$newmessages[$msgindex]= $this->fillObjectInfo($this->msg);
if ($ts_lastmsg < $this->msg->ts_sent)
$ts_lastmsg= $this->msg->ts_sent;
$msgindex++;
}
$templater = new Templater();
$templater->messages=$newmessages;
$templater->ts_lastchatmsg= $ts_lastmsg;
$templater->errors= $errors;
$body = $templater->render('messenger/msgdisplay.tpl');
$json= array(
'ts_lastmsg' => date('Y-m-d H:i:s', time()),
'errors' => $errors,
'nmessages' => count($newmessages),
'html' => $body
);
$this->sendJson($json);
}
protected function loadWidgetSettings()
{
$request= $this->getRequest();
$settings= array();
foreach ($this->default_widget_settings as $k=>$v) {
$param= trim($request->getParam($k));
if ($param != '') {
$settings[$k]= $param;
}
else {
$settings[$k]= $v;
}
}
return $settings;
}
}
?>