<?php
/**********************************************************
* Source Copyright 2014 Author                            *
* Unauthorized reproduction is not allowed                *
* Author: JPK Software Solutions Pvt. Ltd.                *
* Email: sales@visionhelpdesk.com                         *
* Owner: www.visionhelpdesk.com                           *
**********************************************************/

if(!defined('_VIS_')) die('Error: Call from undefined function');
class blabber {
    var $error;
    function view_blabs($operation) {
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'blabber.php');
        $filter='is_closed=0';
        $page=(isset($operation['vis_page']) && $operation['vis_page']!='')?$operation['vis_page']:0;
        $area='vis_blabber_container';
        $object=new class_blabber($database);
        
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
        $objectStaff=new class_staff($database);
        $staff=$objectStaff->get_staff('staff_id='.VIS_STAFF_ID, VIS_STAFF_ID, VIS_LOCATION_AREA);
								$staffs=$staff;
        if(isset($operation['criteria']) && $operation['criteria']!='')
		{	
			$filter.=' and content like "%'.addslashes($operation['criteria']).'%"';
			$criteria = $operation['criteria'];
        }      
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$settingObject=new class_setting($database);
        $blab=$object->get_blabs($filter, $page, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $blabs = $blab[1];
        $blab_setting_count = $object->get_blab_count();    
        if($blab_setting_count=='') $blab_setting_count=10;
        $pagination = $page>0?$page*$blab_setting_count:$page*10;
        ($blab[0]['count'] > ($pagination+$blab_setting_count))?$remain = 1:$remain=0;
        $trends=$object->get_trends(10, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $staffIds = array();
        if($blabs!=-1) for($i=0;$i<count($blabs);$i++) {
			$staffIds[$blabs[$i]['blab_id']] = $object->get_blab_staff($blabs[$i]['blab_id']); //get staffIds of staff with whome blab shared
            $blab_list[$i]['info']=$blabs[$i];
            $blab_list[$i]['comment']=$object->get_blab_comment($blabs[$i]['blab_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
            $blab_list[$i]['attachment']=$object->get_blab_attachment($blabs[$i]['blab_id']);
        }
        //code to get details of staff with whome blab is shared starts here
        $staffNm = "";
        if(!empty($staffIds))
        {
			foreach($staffIds as $key => $staff)
			{
				if(is_array($staff))
				{
					$staffNm = "";
					$staffId='';
					foreach($staff as $staffMember)
					{
						$filter=VIS_TABLE_PREFIX.'staff.staff_id='.$staffMember['staff_id'].' ';
						list($staff_info, $staff_details)=$settingObject->get_staff($filter, 1, 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
						$staff=$staff_details[0];	
						if(!empty($staff))
						{
							$staffNm = 	$staff['first_name']." ".$staff['last_name'].", ".$staffNm;	
							$staffId = 	$staff['staff_id'].", ".$staffId;		
						}
					}
					$staffNm = rtrim(trim($staffNm),',');
					$staffArr = array_unique(explode(',',$staffNm));
					$sharedStaff[$key] = implode(',',$staffArr);
					$staffId = rtrim(trim($staffId),',');
					$staffIdArr = array_unique(explode(',',$staffId));
					$sharedStaffId[$key] = implode(',',$staffIdArr);
				}
				else
				{
					include_once(VIS_PATH_FUNCTION.VIS_FUNCTION_PREFIX.'ticket.php');
					$ticket=new ticket($database);
					$allStaff = $ticket->get_properties('get_staff', $data='','staff_id!='.VIS_STAFF_ID);
					$staffId='';
					for($i=0; $i<count($allStaff); $i++)
					{
						if(!empty($staff))
						{	
							$staffId = 	$allStaff[$i]['staff_id'].", ".$staffId;		
						}
					$staffId = rtrim(trim($staffId),',');
					$staffIdArr = array_unique(explode(',',$staffId));
					$sharedStaffId[$key] = implode(',',$staffIdArr);
					}
				}
			}
		}
		//code to get details of staff with whome blab is shared ends here
        $database->database_disconnect();
        if(isset($operation['vis_action']) && $operation['vis_action']=='no_load') include(VIS_PATH_TEMPLATE.'blab_list.php');
        else include(VIS_PATH_TEMPLATE.'blab.php');
        return true;
    }

    function get_blabs($operation) {
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'blabber.php');
        $object=new class_blabber($database);
        $filter='is_closed=0';
        if(isset($operation['vis_trend']) && $operation['vis_trend']!='')
            $filter.=' and ('.VIS_TABLE_PREFIX.'blab.content like "%#'.$operation['vis_trend'].'%") union (select blab_id, '.VIS_TABLE_PREFIX.'blab.entry_date, '.VIS_TABLE_PREFIX.'blab.owner, first_name, last_name, email, '.VIS_TABLE_PREFIX.'blab.content, image_name from '.VIS_TABLE_PREFIX.'blab, '.VIS_TABLE_PREFIX.'staff, '.VIS_TABLE_PREFIX.'blab_comment where blab_id=parent_id and '.VIS_TABLE_PREFIX.'blab.owner=staff_id and '.VIS_TABLE_PREFIX.'blab_comment.content like "%'.$operation['vis_trend'].'%")';
        $page=(isset($operation['vis_page']) && $operation['vis_page']!='')?$operation['vis_page']:0;
        if(isset($operation['vis_filter']) && $operation['vis_filter']!='') $filter.=' and '.$operation['vis_filter'];
        $blab=$object->get_blabs($filter, $page, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $blabs = $blab[1];
        if($blabs==-1) {
            echo 'data not found';
        }
        else {
            for($i=0;$i<count($blabs);$i++) {
                $blab_list[$i]['info']=$blabs[$i];
                $blab_list[$i]['comment']=$object->get_blab_comment($blabs[$i]['blab_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
            }
            echo get_xml($blab_list, 'blabs');
        }
        $database->database_disconnect();
        return true;
    }

    function view_staff_blab($operation) {
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'blabber.php');
        if(!isset($operation['vis_staff_id']))
        {
			$spliter=(strstr($operation['vis_action'], '/'))?'/':',';
			$action=explode($spliter, $operation['vis_action']);
			if(isset($action[4]) && $action[4]!='' && is_numeric($action[4])) 
			{
				$operation['vis_staff_id'] = $action[4];
			}
		}
        $filter='is_closed=0 and owner='.$operation['vis_staff_id'];
        $object=new class_blabber($database);
        $trends=$object->get_trends(10, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $page=(isset($operation['vis_page']) && is_numeric($operation['vis_page']))?$operation['vis_page']:0;
        $area='vis_blabber_scontainer';
        $blab=$object->get_blabs($filter, $page, VIS_STAFF_ID, VIS_LOCATION_AREA);   
        $blabs = $blab[1];
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
        $objectStaff=new class_staff($database);    
        $staffView=$objectStaff->get_staff('staff_id='.$operation['vis_staff_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
        $staff=$objectStaff->get_staff('staff_id='.VIS_STAFF_ID, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $staffs=$staff;
        if($blabs!=-1) {
            for($i=0;$i<count($blabs);$i++) {
                $blab_list[$i]['info']=$blabs[$i];
                $blab_list[$i]['comment']=$object->get_blab_comment($blabs[$i]['blab_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
                $blab_list[$i]['attachment']=$object->get_blab_attachment($blabs[$i]['blab_id']);
            }
        }
        $database->database_disconnect();
        if(isset($operation['vis_action']) && $operation['vis_action']=='no_load') include(VIS_PATH_TEMPLATE.'blab_list.php');
        else include(VIS_PATH_TEMPLATE.'blab_filter.php');
        return true;
    }

    function view_trend_blab($operation) {
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'blabber.php');
        $filter='is_closed=0';
        $object=new class_blabber($database);
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
		$settingObject=new class_setting($database);
        $searchFor = $operation['vis_trend'];
        $area='vis_blabber_tcontainer';
        $page=(isset($operation['vis_page']) && $operation['vis_page']!='')?$operation['vis_page']:0;
        $blab=$object->get_trend_blabs($operation['vis_trend'], $filter, $page, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $blabs = $blab[1];
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
        $objectStaff=new class_staff($database);    
        $staffView=$objectStaff->get_staff('staff_id='.$operation['vis_staff_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
        $staff=$objectStaff->get_staff('staff_id='.VIS_STAFF_ID, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $staffs=$staff;
        $blab_setting_count = $object->get_blab_count();    
         if($blab_setting_count=='') $blab_setting_count=10;
        $pagination = $page>0?$page*$blab_setting_count:$page*10;
        ($blab[0]['count'] > ($pagination+$blab_setting_count))?$remain = 1:$remain=0;
        $trends=$object->get_trends(10, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $blab_list=array();
        $staffIds = array();
        if($blabs!=-1) for($i=0;$i<count($blabs);$i++) {
			$staffIds[$blabs[$i]['blab_id']] = $object->get_blab_staff($blabs[$i]['blab_id']); //get staffIds of staff with whome blab shared
            $blab_list[$i]['info']=$blabs[$i];
            $blab_list[$i]['comment']=$object->get_blab_comment($blabs[$i]['blab_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
            $blab_list[$i]['attachment']=$object->get_blab_attachment($blabs[$i]['blab_id']);
        }
        //code to get details of staff with whome blab is shared starts here
        $staffNm = "";
        if(!empty($staffIds))
        {
			foreach($staffIds as $key => $staff)
			{
				if(is_array($staff))
				{
					$staffNm = "";
					foreach($staff as $staffMember)
					{
						$tempstaffMemberIds[] = $staffMember['staff_id'];
					}
					$staffMemberIds = array_unique($tempstaffMemberIds);
					foreach($staffMemberIds as $staffId)
					{
						$filter=VIS_TABLE_PREFIX.'staff.staff_id='.$staffId.' ';
						list($staff_info, $staff_details)=$settingObject->get_staff($filter, 1, 0, '', VIS_STAFF_ID, VIS_LOCATION_AREA);
						$staff=$staff_details[0];	
						if(!empty($staff))
						{
							$staffNm = 	$staff['first_name']." ".$staff['last_name'].", ".$staffNm;					
						}
					}
					$staffNm = rtrim(trim($staffNm),',');
					$staffArr = array_unique(explode(',',$staffNm));
					$sharedStaff[$key] = implode(',',$staffArr);
				}
				else
				{
				    $sharedStaff[0] = -1;
				}
			}
		}
		//code to get details of staff with whome blab is shared ends here
        $database->database_disconnect();
        if(VIS_LOCATION_AREA==0) echo get_xml($blab_list, 'blabs');
        else {
            if(isset($operation['vis_action']) && $operation['vis_action']=='no_load') include(VIS_PATH_TEMPLATE.'blab_list.php');
            else include(VIS_PATH_TEMPLATE.'blab_filter.php');
        }
        return true;
    }

    function view_date_blab($operation) {
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'blabber.php');
        $filter='is_closed=0 and entry_date>unix_timestamp("'.$operation['vis_date'].'") and entry_date<unix_timestamp("'.$operation['vis_date'].'")+86400';
        $object=new class_blabber($database);
        $area='vis_blabber_container';
        $page=(isset($operation['vis_page']) && $operation['vis_page']!='')?$operation['vis_page']:0;
        $blab=$object->get_blabs($filter, $page, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $blabs = $blab[1];
        if($blabs!=-1) for($i=0;$i<count($blabs);$i++) {
            $blab_list[$i]['info']=$blabs[$i];
            $blab_list[$i]['comment']=$object->get_blab_comment($blabs[$i]['blab_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
        }
        else $blab_list=-1;
        $database->database_disconnect();
        include(VIS_PATH_TEMPLATE.'blab_list.php');
        return true;
    }

    function add_new_blab($operation) {
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
        $filter='is_enabled=1 and staff_id!='.VIS_STAFF_ID;
        $object=new class_staff($database);
        $load_header=(isset($operation['vis_action']) && $operation['vis_action']=='no_load')?false:true;
        $staff=$object->get_staff($filter, VIS_STAFF_ID, VIS_LOCATION_AREA);
								$filters='is_enabled=1 and staff_id='.VIS_STAFF_ID;
								$currentstaff=$object->get_staff($filters, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $database->database_disconnect();
        include(VIS_PATH_TEMPLATE.'blab_new.php');
        return true;
    }

    function add_staff($operation) {
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
        $object=new class_setting($database);
        $load_header=(isset($operation['vis_action']) && $operation['vis_action']=='no_load')?false:true;
        $filter=VIS_TABLE_PREFIX.'staff.is_enabled=1 and '.VIS_TABLE_PREFIX.'staff.staff_id!='.VIS_STAFF_ID;
		if(isset($operation['criteria']) && $operation['criteria']!='')
		{	
			$filter.=' and (first_name like "%'.$operation['criteria'].'%" or last_name like "%'.$operation['criteria'].'%" or job_title like "%'.$operation['criteria'].'%")';
			$criteria = 1;
        }
        else if(isset($operation['criteria']) && $operation['criteria']=='')
        {
			$criteria = 1;
		}		
        if($load_header) $filter.=' and '.VIS_TABLE_PREFIX.'staff.staff_id!='.VIS_STAFF_ID;
        include_once(VIS_PATH_FUNCTION.VIS_FUNCTION_PREFIX.'ticket.php');
		$ticket=new ticket($database);
		$load_header=(isset($operation['vis_action']) && $operation['vis_action']=='no_load')?false:true;
		$staff = $ticket->get_properties('get_staff', $data='',$filter);
		if(empty($staff)) $staff = -1;
        $database->database_disconnect();
        $share_id=array();
        if(isset($operation['vis_share_id'])) $share_id=unserialize(base64_decode($operation['vis_share_id']));
        include(VIS_PATH_TEMPLATE.'blab_staff_add.php');
        return true;
    }

    function add_blab_staff($operation) {
        global $vis_language;
        $data=parse_form_array($operation['vis_action']);
        $imgs='';
        $staffs=array();
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
        $object=new class_staff($database);

        foreach($data as $ptr=>$val) {
            if(strpos($ptr, 'vis_staff_')!==false) {
                $staff=$object->get_staff('staff_id='.$data[$ptr], VIS_STAFF_ID, VIS_LOCATION_AREA);
                $name=trim($staff[0]['first_name'].' '.$staff[0]['last_name']);
                $imgs.='<img src="'.VIS_PATH_PROFILE.((isset($staff[0]['image_name']) && $staff[0]['image_name']!='')?$staff[0]['image_name']:'default.jpg').'" width="32" title="'.$name.'" /> ';
                $staffs[]=$data[$ptr];
            }
        }
        if(count($staffs)==0) {
            echo '<script>$("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSTFNTSEL'].'");</script>';
            return true;
        }
        else {
            $staffids=base64_encode(serialize($staffs));
            echo '<script>
                $("#vis_new_blab_share_list").html(\''.$imgs.'<input type="hidden" id="vis_blab_share" name="vis_blab_share" value="'.$staffids.'" />\');
                if($("#vis_blabber_staff_form").is(":data(dialog)")) { $("#vis_blabber_staff_form").dialog("close"); } </script>';
            return true;
        }
    }

    function unsubscribe_blab($operation) {
        global $vis_language;
        if(!isset($operation['vis_blab_id'])) {
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", " '.$vis_language['MSGBLBIDNTFND'].'"); </script>';
            return false;
        }
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'blabber.php');
        $object=new class_blabber($database);
        $ret=$object->unsubscribe_blab($operation['vis_blab_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
        $database->database_disconnect();
        if(!$ret) {
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';
            return false;
        }
        else echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'"); $("#vis_blab_main_div_'.$operation['vis_blab_id'].'").hide(); </script>';
        return true;
    }

    function delete_blab($operation) {
        global $vis_language;
        if(!isset($operation['vis_blab_id'])) {
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGBLBIDNTFND'].'"); </script>';
            return false;
        }
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'blabber.php');
        $object=new class_blabber($database);
        $blab_details=$object->get_blab_details($operation['vis_blab_id']);
		$ret=$object->delete_blab($operation['vis_blab_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
        if(!$ret) {
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';
            return false;
        }
        else {
			include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
            $setting_object=new class_setting($database);
			$activity_log=$setting_object->add_staff_activity_log("Removed blab ".$blab_details['content'], VIS_STAFF_ID, 3, VIS_LOCATION_AREA);
			echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'"); $("#vis_workpanel_div").visExecution("load_module","'.$vis_language['TXTBLABS'].'",{"vis_module":"blabber", "vis_operation":"view_blabs","vis_page":"'.$vis_page.'"});</script>';
		}
        $database->database_disconnect();
        return true;
    }

    function create_blab($operation) {
        global $vis_language;
        $dataSent = parse_form_array($operation['vis_action']);
        $staffids = "";
        if(isset($dataSent['vis_blab_share']) && !is_numeric($dataSent['vis_blab_share']))
        {
			$strLen = strlen($dataSent['vis_blab_share']);
			$staffStr = $dataSent['vis_blab_share'];
			$modifiedStr = substr_replace($staffStr, ' ', $strLen-1, 1);
			$staffArr = explode(",",$modifiedStr);
			$staffids=base64_encode(serialize($staffArr)); 
			$operation['vis_blab_share'] = $staffids;
        }
        if(isset($operation['vis_action'])) $data=parse_form_array($operation['vis_action']);
        else $data=$operation;
        if($staffids !="")
        { $data['vis_blab_share'] = $staffids; }
        if(trim($data['vis_blab_post_area'])=='') {
            if(VIS_LOCATION_AREA==2) echo '<script> $("#vis_blab_post_hidden").val("");$("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGINFOMISS'].'"); </script>';
            elseif(VIS_LOCATION_AREA==3) {
                $operation['vis_message']='<div class="vis_error_message">'.$vis_language['MSGINFOMISS'].'</div>';
                return $this->add_new_blab($operation);
            }
            else echo $vis_language['MSGINFOMISS'];
            return false;
        }
        if(VIS_LOCATION_AREA==3 && isset($data['vis_blab_share']) && $data['vis_blab_share']==1) {
            if(!isset($data['vis_staff']) || count($data['vis_staff'])==0) {
                $operation['vis_message']='<div class="vis_error_message">'.$vis_language['MSGINFOMISS'].'</div>';
                return $this->add_new_blab($operation);
            }
            $data['vis_blab_share']=implode(',', $data['vis_staff']);
        }
        if(trim($dataSent['insertUrl'])!= "")
        {
			if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $dataSent['insertUrl'])) {
				echo '<script>$("#vis_blab_post_hidden").val("");$("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGVALIDURL'].'");</script>';
				return false;
			}
		}
        if(($dataSent['vis_share'] == 0 || $dataSent['vis_share'] == "")  && $dataSent['vis_blab_grp_share'] == "" && ($dataSent['vis_blab_share'] == ""))
        {
			echo '<script>$("#vis_blab_post_hidden").val("");$("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSTAFFMISS'].'");</script>';	
		}
		 if(trim($data['vis_blab_post_hidden']) == "")
        {
			$data['vis_blab_post_hidden'] = $data['vis_blab_post_area'];
		} 
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'blabber.php');
        $object=new class_blabber($database);
        $ret=$object->create_blab($data, VIS_STAFF_ID, VIS_LOCATION_AREA);
	  if($ret) {
			include_once(VIS_PATH_FUNCTION.VIS_FUNCTION_PREFIX.'ticket.php');
			$ticket_module = new ticket();
			$staff_access=$ticket_module->get_properties('get_staff', '');
			if(isset($data['vis_blab_grp_share']) && $data['vis_blab_grp_share']!='')
			{
				$data['vis_blab_grp_share']=substr($data['vis_blab_grp_share'], 0, -1);
				$group_list=explode(',', $data['vis_blab_grp_share']);
				for($j=0;$j<count($staff_access); $j++)
				{
					if(in_array($staff_access[$j]['group_id'], $group_list)){	
					$data['vis_staff_access'].=$staff_access[$j]['staff_id'].',';
					$data['vis_blab_share']=substr($data['vis_staff_access'], 0, -1);
					}
				}
			}
			if($data['vis_blab_share']=='0')
			{
				for($i=0; $i<count($staff_access); $i++)
				$data['vis_staff_access'].=$staff_access[$i]['staff_id'].',';
				$data['vis_blab_share']=substr($data['vis_staff_access'], 0, -1);
			}
			else {if($data['vis_blab_grp_share']=='')
				$data['vis_blab_share']=unserialize(base64_decode($data['vis_blab_share']));
				if(is_array($data['vis_blab_share'])) $data['vis_blab_share']=implode(',', $data['vis_blab_share']);
				$share_blab=explode(',', $data['vis_blab_share']);
				if(!empty($staffArr) && !empty($share_blab))
				for($i=0; $i<count($staffArr); $i++){
					if(!in_array(trim($staffArr[$i]), array_map('trim',$share_blab))){	
						if($data['vis_blab_grp_share']!='')$data['vis_blab_share']='';
						if($data['vis_blab_share']!='')
						$data['vis_blab_share'].=','.implode(',', $staffArr);
						else
						$data['vis_blab_share']=implode(',', $staffArr);
					}
				}
			}
            $data['vis_operation']='create_blab';
            $data['vis_blab_id']=$ret;
            global $operation_hnd;
            $operation_hnd->check_automation($data, $database);
            include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'mail.php');
            $alert=new class_mail($database);
		
            $alert->alert_device($data, 5, (VIS_LOCATION_AREA==2 && !is_string($data['vis_blab_share']))?implode(',', unserialize(base64_decode($data['vis_blab_share']))):$data['vis_blab_share'], VIS_STAFF_ID);
          
        }
        if(VIS_LOCATION_AREA==3) {
            $operation['vis_message']=($ret)?'<div class="vis_message">'.$vis_language['MSGSUCCOPRT'].'</div>':'<div class="vis_error_message">'.$vis_language['MSGERROPRT'].'</div>';
          	return $this->view_blabs($operation);
        }
       elseif(VIS_LOCATION_AREA==2) {
            if(!$ret) {
				 echo '<script>$("#vis_blab_post_hidden").val("");$("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'");</script>';
                return false;
            }
            else {
				include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
				$setting_object=new class_setting($database);
				$activity_log=$setting_object->add_staff_activity_log("Added new blab ".$data['vis_blab_post_area'], VIS_STAFF_ID, 3, VIS_LOCATION_AREA);
				echo '<script>$("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'"); $("#vis_workpanel_div").visExecution("load_module","'.$vis_language['TXTBLABS'].'",{"vis_module":"blabber", "vis_operation":"view_blabs","vis_page":"'.$vis_page.'"}); $(".modal").modal("hide");</script>';
			}
        } 
        elseif(VIS_LOCATION_AREA==0) {
           echo ($ret)?'success':'error'; 
        }
        $database->database_disconnect();
        return $ret;
    }

    function refresh_blab_comment($operation) {
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'blabber.php');
        $object=new class_blabber($database);
        $blabId = $operation['vis_blab_id'];
        $blab_list=$object->get_blab_comment($operation['vis_blab_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
        for($i=0;$i<count($blab_list);$i++) {
            $blab_comment=$blab_list[$i];
				include(VIS_PATH_TEMPLATE.'blab_comment_list.php');
        }
        return true;
    }

    function comment_blab($operation) {
        global $vis_language;
        if(isset($operation['vis_action']) && $operation['vis_action']!='') $data=parse_form_array($operation['vis_action']);
        else $data=$operation;
        if(!isset($data['vis_blab_comment']) || trim($data['vis_blab_comment']=='')) {
            if(VIS_LOCATION_AREA==2)
                echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGINFOMISS'].'"); </script>';
            elseif(VIS_LOCATION_AREA==3) {
                $operation['vis_message']='<div class="vis_error_message">'.$vis_language['MSGINFOMISS'].'</div>';
                return $this->view_blabs($operation);
            }
            else echo $vis_language['MSGINFOMISS'];
            return false;
        }
        else $data['vis_blab_comment']=nl2br($data['vis_blab_comment']);
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'blabber.php');
        $object=new class_blabber($database);
        $ret=$object->add_blab_comment($data, VIS_STAFF_ID, VIS_LOCATION_AREA);
        $data['vis_operation']=$operation['vis_operation'];
        $data['vis_shared_staff_list']=$operation['vis_staff_id_list'];
        global $operation_hnd;
        $operation_hnd->check_automation($data, $database);
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'mail.php');
        $alert=new class_mail($database);
        $alert->alert_device($data, 6, $data['vis_blab_id'], VIS_STAFF_ID);
         if(VIS_LOCATION_AREA==2) {
            if(!$ret) {
                echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';
                return false;
            }
            else 
            {
				echo '<script>  $("#vis_blab_comment_list_'.$data['vis_blab_id'].'").visExecution("load_area", {"vis_module":"blabber", "vis_operation":"refresh_blab_comment", "vis_blab_id":'.$data['vis_blab_id'].'}); $("#vis_blab_comment_'.$data['vis_blab_id'].'").val(""); $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'"); </script>';
			}
			$blab_details=$object->get_blab_details($data['vis_blab_id']);
			include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
            $setting_object=new class_setting($database);
			$activity_log=$setting_object->add_staff_activity_log("Added new comment for blab ".$blab_details['content'], VIS_STAFF_ID, 3, VIS_LOCATION_AREA);
		}
        elseif(VIS_LOCATION_AREA==3) {
            $operation['vis_message']=(!$ret)?'<div class="vis_error_message">'.$vis_language['MSGERROPRT'].'</div>':'<div class="vis_message">'.$vis_language['MSGSUCCOPRT'].'</div>';
            return $this->view_blabs($operation);
        }
        elseif(VIS_LOCATION_AREA==0) {
            echo (!$ret)?'error':'success';
            return $ret;
        }
        $database->database_disconnect();
       return true;
    }

    function quick_view($operation) {
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'staff.php');
        $object=new class_staff($database);
        $blabber=$object->get_recent_audit('location=3', VIS_STAFF_ID, VIS_LOCATION_AREA);
        $database->database_disconnect();
        //include(VIS_PATH_TEMPLATE.'blabber_quickview.php');
        return false;
    }

    function get_trends($operation) {
        global $vis_language;
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'blabber.php');
        $object=new class_blabber($database);
        $trends=$object->get_trends(10, VIS_STAFF_ID, VIS_LOCATION_AREA);
        if($staff==-1) {
            echo $vis_language['TXTNOMATCHFOUND'];
        }
        else {
            echo get_xml($trends, 'trends');
        }
        $database->database_disconnect();
        return true;
    }
    function recent_blabs($operation) {
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'blabber.php');
        if(isset($operation['vis_staff_id']) && $operation['vis_staff_id']!='') $staff_id = ' and owner = '.$operation['vis_staff_id'];
        else $staff_id = ''; 
        $filter='is_closed=0'.$staff_id;
        
        $object=new class_blabber($database);
        $blabs=$object->get_recent_blabs($filter,VIS_STAFF_ID, VIS_LOCATION_AREA);
        if($blabs!=-1) for($i=0;$i<count($blabs);$i++) {
            $blab_list[$i]['info']=$blabs[$i];
            $blab_list[$i]['comment']=$object->get_blab_comment($blabs[$i]['blab_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
            $blab_list[$i]['attachment']=$object->get_blab_attachment($blabs[$i]['blab_id']);
        }
        $database->database_disconnect();
		include(VIS_PATH_TEMPLATE.'recent_blabs.php');
        return true;
    }
    function get_delete_blab($operation) {
        global $vis_language;
        $message_info['vis_form_title']=$vis_language['TXTDELETEBLAB'];
        $message_info['vis_message']=$vis_language['MSGSURE'];
		$vis_page=(isset($operation['vis_page']))?$operation['vis_page']:'';									
        $message_info['vis_event']="$(this).visExecution('load_operations', {'vis_module':'blabber', 'vis_operation':'delete_blab', 'vis_blab_id':".$operation['vis_blab_id'].",'vis_page':'".$vis_page."'})";
        include(VIS_PATH_TEMPLATE.'confirm_delete.php');
        return true;
    }
    function get_delete_comment($operation) {
        global $vis_language;
        $message_info['vis_form_title']=$vis_language['TXTDELETEBLABCOMMENT'];
        $message_info['vis_message']=$vis_language['MSGSURE'];
		$vis_page=(isset($operation['vis_page']))?$operation['vis_page']:'';									
        $message_info['vis_event']="$(this).visExecution('load_operations', {'vis_module':'blabber', 'vis_operation':'delete_blab_comment', 'vis_blab_id':".$operation['vis_blab_id'].",'vis_comment_id':".$operation['vis_comment_id'].",'vis_page':'".$vis_page."'})";
        include(VIS_PATH_TEMPLATE.'confirm_delete.php');
        return true;
    }
     function delete_blab_comment($operation) {
        global $vis_language;
        if(!isset($operation['vis_blab_id'])) {
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGBLBIDNTFND'].'"); </script>';
            return false;
        }
        $database=new class_database();
        include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'blabber.php');
        $object=new class_blabber($database);
        $comment_details=$object->get_comment($operation['vis_comment_id']);
		$ret=$object->delete_blab_comment($operation['vis_blab_id'],$operation['vis_comment_id'], VIS_STAFF_ID, VIS_LOCATION_AREA);
        if(!$ret) {
            echo '<script> $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGERROPRT'].'"); </script>';
            return false;
        }
        else {
			$blab_details=$object->get_blab_details($operation['vis_blab_id']);
			include_once(VIS_PATH_CLASS.VIS_CLASS_PREFIX.'setting.php');
            $setting_object=new class_setting($database);
			$activity_log=$setting_object->add_staff_activity_log("Removed comment for blab ".$blab_details['content'], VIS_STAFF_ID, 3, VIS_LOCATION_AREA);
			echo '<script>  $("#vis_blab_comment_list_'.$operation['vis_blab_id'].'").visExecution("load_area", {"vis_module":"blabber", "vis_operation":"refresh_blab_comment", "vis_blab_id":'.$operation['vis_blab_id'].'}); $("#vis_global_msg_div").visInterface("load_global_message", "'.$vis_language['MSGSUCCOPRT'].'"); </script>';
		}
        $database->database_disconnect();
        return true;
    }
}
?>
