<?php
/*
OsShare v1 ,
Coded By Paimpozhil B. , SaravanaKumar M.S.
*/
class MessagesController extends AppController {
var $layout = 'user';
var $name = 'Messages';
var $helpers = array('Html', 'Form','Javascript','ajax','Pagination' ,'Time');
var $uses= array('Message','User','Sitesetting');
var $components = array ('Pagination');
function add($id=null) {
$data = $this->dolayout();
if(!$id)
$this->Session->setFlash('Invalid User');
//$this->redirect(array('action'=>'view'));
$this->set('toid',$id);
$this->set('usid',$id);
// message_check($id);
if(!empty($this->data)) {
$this->cleanUpFields();
$this->Message->create();
$data = $this->Session->read('User');
$frid = $data["id"];
$this->data["Message"]["from"]=$frid;
if($this->Message->save($this->data)) {
$this->set('toid',$id);
$this->Message->create();
$data = $this->Session->read('User');
$id = $data["id"];
$this->data["Message"]["from"]=$id;
$this->data["Message"]["user_id"] = $id;
if($this->Message->save($this->data))
{
$this->Session->setFlash('Message Sent');
$this->redirect(array('action'=>'usermessage'));
}
} else {
$this->Session->setFlash('The Message could not be saved. Please, try again.');
}
}
}
function message_check($id=null)
{
$inbcount = $this->Message->findall(" `Message`.user_id =". $id ." AND `Message`.to = " . $id . " AND `Message`.trash = 0");
$this->set('inbcount',$inbcount);
$inbox_limit = $this->Sitesetting->findbyid(1);
$this->set('inboxlimit',$inbox_limit);
if(count($inbox_limit) >= count($inbcount))
{
$this->redirect(array('action'=>'add'));
}
else
return;
}
function addnew() {
$data = $this->dolayout();
if(!empty($this->data)) {
$this->cleanUpFields();
//debug($this->data["Message"]);
$username = $this->data["Message"]["username"];
//debug($username);
$finuser = $this->User->findbyusername($username);
//debug($finuser);
$this->Message->create();
$data = $this->Session->read('User');
$frid = $data["id"];
$this->data["Message"]["from"]=$frid;
$this->data["Message"]["to"] = $finuser["User"]["id"];
$this->data["Message"]["user_id"] = $frid;
if($this->Message->save($this->data)) {
$this->Message->create();
$data = $this->Session->read('User');
$id = $data["id"];
$this->data["Message"]["from"]=$id;
$this->data["Message"]["user_id"] = $finuser["User"]["id"];
if($this->Message->save($this->data))
{
$this->Session->setFlash('Message Sent');
$this->redirect(array('action'=>'usermessage'));
}
} else {
$this->Session->setFlash('The Message could not be saved. Please, try again.');
$this->redirect(array('action'=>'addnew'));
}
}
}
function reply($id=null) {
$data = $this->dolayout();
if(!$id) {
$this->Session->setFlash('Invalid User');
//$this->redirect(array('action'=>'view'));
}
$this->set('toid',$id);
$this->set('usid',$id);
if(!empty($this->data)) {
$this->cleanUpFields();
$this->Message->create();
$data = $this->Session->read('User');
$frid = $data["id"];
$this->data["Message"]["from"]=$frid;
if($this->Message->save($this->data)) {
$this->set('toid',$id);
$this->Message->create();
$data = $this->Session->read('User');
$id = $data["id"];
$this->data["Message"]["from"]=$id;
$this->data["Message"]["user_id"] = $id;
if($this->Message->save($this->data))
{
$this->Session->setFlash('Message Sent');
$this->redirect(array('action'=>'usermessage'));
}
} else {
$this->Session->setFlash('The Message could not be saved. Please, try again.');
}
}
}
function usermessage() {
$data = $this->dolayout();
$data = $this->Session->read('User');
$id = $data["id"];
$condition = " `Message`.user_id =". $id ." AND `Message`.to = " . $id . " AND `Message`.trash = 0";
// $condition = "`Message`.user_id = `Message`.to";
list($order,$limit,$page) = $this->Pagination->init($condition); // Added
$inb = $this->Message->findall($condition, NULL, $order, $limit, $page);
$reccount = $this->Message->findall(" `Message`.user_id =". $id ." AND `Message`.to = " . $id . " AND `Message`.trash = 0" . " AND `Message`.unread = 0");
$inbcount = $this->Message->findall(" `Message`.user_id =". $id ." AND `Message`.to = " . $id . " AND `Message`.trash = 0");
$inbox_limit = $this->Sitesetting->findbyid(1);
// $sname = $this->Message->findallbyfrom($id);
foreach(array_keys($inb) as $key)
{
$in =& $inb[$key];
$in["User1"] = $this->User->find("`User`.id = ". $inb[$key]['Message']['from']);
}
$this->set('message',$inb);
$this->set('inboxlimit',$inbox_limit);
$this->set('inbcount',$inbcount);
$this->set('reccount',$reccount,"count(*) as ccount");
// debug($reccount);
//$category["Video"] = $this->Category->Video->find("`Category`.id = " . $category["Category"]["id"] , NULL, "`Video`.views Desc",NULL,NULL,0);
// $this->set('sname',$sname);
// debug($inb);
// $condition .= " `Video`.tags LIKE '%" . $tag . "%' OR `Video`.name LIKE '%" . $tag . "%' OR `Video`.desc LIKE '%" . $tag . "%' OR ";
//$this->
//$this->set('favorite',$this->Favorite->findbyvideo_id($id,"count(*) as favcount"));
}
function sentitems() {
$data = $this->dolayout();
$data = $this->Session->read('User');
$id = $data["id"];
$condition = " `Message`.user_id =". $id ." AND `Message`.from = " . $id . " AND `Message`.trash = 0";
// $condition = "`Message`.user_id = `Message`.to";
list($order,$limit,$page) = $this->Pagination->init($condition); // Added
$inb = $this->Message->findall($condition, NULL, $order, $limit, $page);
$reccount = $this->Message->findall(" `Message`.user_id =". $id ." AND `Message`.from = " . $id . " AND `Message`.trash = 0");
// $sname = $this->Message->findallbyfrom($id);
foreach(array_keys($inb) as $key)
{
$in =& $inb[$key];
$in["User1"] = $this->User->find("`User`.id = ". $inb[$key]['Message']['to']);
}
$this->set('message',$inb);
$this->set('reccount',$reccount,"count(*) as ccount");
// debug($reccount);
//$category["Video"] = $this->Category->Video->find("`Category`.id = " . $category["Category"]["id"] , NULL, "`Video`.views Desc",NULL,NULL,0);
// $this->set('sname',$sname);
// debug($inb);
// $condition .= " `Video`.tags LIKE '%" . $tag . "%' OR `Video`.name LIKE '%" . $tag . "%' OR `Video`.desc LIKE '%" . $tag . "%' OR ";
//$this->
//$this->set('favorite',$this->Favorite->findbyvideo_id($id,"count(*) as favcount"));
}
function msgindex()
{
$data = $this->dolayout();
$data = $this->Session->read('User');
$id = $data["id"];
$reccount = $this->Message->findall(" `Message`.user_id =". $id ." AND `Message`.to = " . $id . " AND `Message`.trash = 0" . " AND `Message`.unread = 0");
$inbcount = $this->Message->findall(" `Message`.user_id =". $id ." AND `Message`.to = " . $id . " AND `Message`.trash = 0");
$sentitems_count = $this->Message->findall(" `Message`.user_id =". $id ." AND `Message`.from = " . $id . " AND `Message`.trash = 0");
$trashmsg_count = $this->Message->findall(" `Message`.user_id =". $id ." AND `Message`.to = " . $id . " AND `Message`.trash = 1");
$this->set('inboxlimit',$this->ssettings["InboxLimit"]);
$this->set('inbcount',$inbcount);
$this->set('reccount',$reccount);
$this->set('sentitems',$sentitems_count);
$this->set('trashmsg',$trashmsg_count);
}
function trashmessage() {
$data = $this->dolayout();
$data = $this->Session->read('User');
$id = $data["id"];
$condition = " `Message`.user_id =". $id ." AND `Message`.to = " . $id . " AND `Message`.trash = 1";
$reccount = $this->Message->findall(" `Message`.user_id =". $id ." AND `Message`.to = " . $id . " AND `Message`.trash = 1");
// $condition = "`Message`.user_id = `Message`.to";
list($order,$limit,$page) = $this->Pagination->init($condition); // Added
$inb = $this->Message->findall($condition, NULL, $order, $limit, $page);
// $sname = $this->Message->findallbyfrom($id);
foreach(array_keys($inb) as $key)
{
$in =& $inb[$key];
$in["User1"] = $this->User->find("`User`.id = ". $inb[$key]['Message']['from']);
}
$this->set('message',$inb);
$this->set('reccount',$reccount,"count(*) as ccount");
//$category["Video"] = $this->Category->Video->find("`Category`.id = " . $category["Category"]["id"] , NULL, "`Video`.views Desc",NULL,NULL,0);
// $this->set('sname',$sname);
// debug($inb);
// $condition .= " `Video`.tags LIKE '%" . $tag . "%' OR `Video`.name LIKE '%" . $tag . "%' OR `Video`.desc LIKE '%" . $tag . "%' OR ";
//$this->
//$this->set('favorite',$this->Favorite->findbyvideo_id($id,"count(*) as favcount"));
}
function messageview($id = null) {
$condition = " `Message`.user_id =". $id ." AND `Message`.to = " . $id;
// $condition = "`Message`.user_id = `Message`.to";
$inb = $this->Message->findbyid($id);
$sn = $this->User->find("`User`.id = " . $inb['Message']['from']);
// $sname = $this->Message->findallbyfrom($id);
/* foreach(array_keys($inb) as $key)
{
$in =& $inb[$key];
$in["User1"] = $this->User->find("`User`.id = ". $inb[$key]['Message']['from']);
}*/
$this->set('messages',$inb);
$this->set('user',$sn);
$message = $this->Message->findbyid($id);
$message["Message"]["unread"] = 1;
$this->Message->save($message);
//
// debug($sn);
}
function sentview($id = null) {
$condition = " `Message`.user_id =". $id ." AND `Message`.to = " . $id;
// $condition = "`Message`.user_id = `Message`.to";
$inb = $this->Message->findbyid($id);
$sn = $this->User->find("`User`.id = " . $inb['Message']['from']);
// $sname = $this->Message->findallbyfrom($id);
/* foreach(array_keys($inb) as $key)
{
$in =& $inb[$key];
$in["User1"] = $this->User->find("`User`.id = ". $inb[$key]['Message']['from']);
}*/
$this->set('messages',$inb);
$this->set('user',$sn);
$message = $this->Message->findbyid($id);
$message["Message"]["unread"] = 1;
$this->Message->save($message);
//
// debug($sn);
}
function senttoinbox($id = null) {
if(!$id) {
$this->Session->setFlash('Invalid id for Message');
$this->redirect(array('action'=>'trashmessage'), null, true);
}
$message = $this->Message->findbyid($id);
$message["Message"]["trash"] = 0;
if($this->Message->save($message))
{
$this->redirect(array('action'=>'usermessage'), null, true);
}
else
{
$this->Session->setFlash('Could not Move');
}
//
// debug($sn);
}
function delete($id = null) {
if(!$id) {
$this->Session->setFlash('Invalid id for Message');
$this->redirect(array('action'=>'usermessage'), null, true);
}
$message = $this->Message->findbyid($id);
$message["Message"]["trash"]=1;
if($this->Message->save($message)) {
$this->Session->setFlash('Message #'.$id.' deleted');
$this->redirect(array('action'=>'usermessage'), null, true);
}
}
function msg_bunchd()
{
/// debug($this->params);
$data = $this->Session->read('User');
$priv = $data["privilege"];
if ($priv=='admin' or $priv=='user')
{
$i=0;
foreach($this->params['url'] as $delid)
{
$delid = intval($delid);
if($delid!=0)
{
$message = $this->Message->findbyid($delid);
$message["Message"]["trash"]=1;
$this->Message->save($message);
$a=1;
}
}
if($a == 1)
{
$this->Session->SetFlash("Selected Items Deleted");
$this->redirect(array('controller'=>'messages','action'=>'usermessage'), null, true);
}
else
{
$this->Session->SetFlash("Please Select one or more Items");
$this->redirect(array('controller'=>'messages','action'=>'usermessage'), null, true);
}
}
/* else if($priv=='user')
{
$i=0;
foreach($this->params['url']['data']['Message'] as $delid)
{
if(isset($delid))
{
$message = $this->Message->findbyid($delid);
$message["Message"]["trash"]=1;
$this->Message->save($message);
$a=$delid;
}
}
if($a != 0)
{
$this->Session->SetFlash("Selected Items Deleted");
$this->redirect(array('action'=>'usermessage'), null, true);
}
else
{
$this->Session->SetFlash("Please Select one or more Items");
$this->redirect(array('action'=>'usermessage'), null, true);
}
}*/
}
function trash_bunchd()
{
//debug($this->params);
$data = $this->Session->read('User');
$priv = $data["privilege"];
if ($priv=='admin' or $priv=='user')
{
$i=0;$a=0;
foreach($this->params['url'] as $delid)
{
$delid = intval($delid);
if($delid!=0)
{
$this->Message->del($delid);
$a=1;
}
}
if($a == 1)
{
$this->Session->SetFlash("Selected Items Deleted");
$this->redirect(array('controller'=>'messages','action'=>'trashmessage'), null, true);
}
else
{
$this->Session->SetFlash("Please Select one or more Items");
$this->redirect(array('controller'=>'messages','action'=>'trashmessage'), null, true);
}
}
}
function trashs_bunchd()
{
//debug($this->params);
$data = $this->Session->read('User');
$priv = $data["privilege"];
if ($priv=='admin' or $priv=='user')
{
$i=0;$a=0;
foreach($this->params['url'] as $delid)
{
$delid = intval($delid);
if($delid!=0)
{
$this->Message->del($delid);
$a=1;
}
}
if($a == 1)
{
$this->Session->SetFlash("Selected Items Deleted");
$this->redirect(array('action'=>'sentitems'), null, true);
}
else
{
$this->Session->SetFlash("Please Select one or more Items");
$this->redirect(array('action'=>'sentitems'), null, true);
}
}
}
function trashdelete($id = null) {
if(!$id) {
$this->Session->setFlash('Invalid id for Message');
$this->redirect(array('action'=>'trashmessage'), null, true);
}
if($this->Message->del($id)) {
$this->Session->setFlash('Message #'.$id.' deleted');
$this->redirect(array('action'=>'trashmessage'), null, true);
}
}
function sentitemsdelete($id = null) {
if(!$id) {
$this->Session->setFlash('Invalid id for Message');
$this->redirect(array('action'=>'trashmessage'), null, true);
}
if($this->Message->del($id)) {
$this->Session->setFlash('Message #'.$id.' deleted');
$this->redirect(array('action'=>'sentitems'), null, true);
}
}
}
?>