CRMCall.php 4.64 KB
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;

use Auth;

class CRMCall extends Model{

	protected $table = 'crmcalls';

	//protected $fillable = array('status','data','log','group');
	
	
	public function setTs($var,$nowts=0)
	{
		if($nowts==0)$nowts=microtime(true)*1000;
		
		if($var=='ts_Wait')
		{
			$this->ts_Wait=$nowts;
			$this->ts_Call=$nowts;
			$this->ts_Talk=$nowts;
			$this->ts_Recstart=$nowts;
			$this->ts_Recend=$nowts;
			$this->ts_Dispo=$nowts;
			$this->ts_Close=$nowts;
		}
		if($var=='ts_Call')
		{
			$this->ts_Call=$nowts;
			$this->ts_Talk=$nowts;
			$this->ts_Recstart=$nowts;
			$this->ts_Recend=$nowts;
			$this->ts_Dispo=$nowts;
			$this->ts_Close=$nowts;
		}
		if($var=='ts_cTalk')
		{
			$this->ts_cTalk=$nowts;
			$this->ts_Talk=$nowts;
			$this->ts_Recstart=$nowts;
			$this->ts_Recend=$nowts;
			$this->ts_Dispo=$nowts;
			$this->ts_Close=$nowts;
		}
		if($var=='ts_Talk')
		{
			$this->ts_Talk=$nowts;
			$this->ts_Recstart=$nowts;
			$this->ts_Recend=$nowts;
			$this->ts_Dispo=$nowts;
			$this->ts_Close=$nowts;
		}
		if($var=='ts_Recstart')
		{
			$this->ts_Recstart=$nowts;
			$this->ts_Recend=$nowts;
			$this->ts_Dispo=$nowts;
			$this->ts_Close=$nowts;
		}
		if($var=='ts_Recend')
		{
			$this->ts_Recend=$nowts;
			$this->ts_Dispo=$nowts;
			$this->ts_Close=$nowts;
		}
		if($var=='ts_Dispo')
		{
			$this->ts_Dispo=$nowts;
			$this->ts_Close=$nowts;
		}
		if($var=='ts_Close')
		{
			$this->ts_Close=$nowts;
		}
		
		$this->calcTime();
	}
	public function calcTime()
	{
		$this->waitSec=      $this->ts_Call-$this->ts_Wait;
		$this->callSec=      $this->ts_Talk-$this->ts_Call;
		$this->ctalkSec=      $this->ts_Talk-$this->ts_cTalk;
		$this->talkSec=      $this->ts_Recstart-$this->ts_Talk;
		$this->recstartSec=  $this->ts_Recend-$this->ts_Recstart;
		$this->recendSec=    $this->ts_Dispo-$this->ts_Recend;
		$this->dispoSec=     $this->ts_Close-$this->ts_Dispo;
	}
	
	public function newRecFilePath()
	{
		$recfile="";
		$data=json_decode($this->data,true);
		if(!isset($data['recFolder']))
		{
			$data['recFolder']='drec/'.date('Y-m');
			$data['recExt']='gsm';
			$recPath=storage_path("app/".$data['recFolder']);
			if(!is_dir($recPath)){$umask=umask(0);mkdir($recPath,0777,true);umask($umask);}
			$recfile=$recPath."/".$this->id.".".$data['recExt'];

			$this->data=json_encode($data);
		}
		return $recfile;
	}
	public function getRecFilePath()
	{
		$file="";
		$data=json_decode($this->data,true);
		if(isset($data['recFolder']))
		{
			$file=storage_path("app/".$data['recFolder'])."/".$this->id.".".$data['recExt'];
		}
		return $file;
	}
	public function getRecFileExt()
	{
		$ext="";
		$data=json_decode($this->data,true);
		if(isset($data['recExt']))
		{
			$ext=$data['recExt'];
		}
		return $ext;
	}
	public function saveRecFileSize()
	{
		$fsize=0;
		$data=json_decode($this->data,true);
		if(isset($data['recFolder']))
		{
			$fpath=storage_path("app/".$data['recFolder'])."/".$this->id.".".$data['recExt'];
			if(file_exists($fpath))$fsize=filesize($fpath);
		}
		$this->recsize=$fsize;
	}
	public function addEventLog($nowts,$eventstr)
	{
		$data=json_decode($this->data,true);
		if(!isset($data['eventslog']))$data['eventslog']=array();
		$data['eventslog'][$nowts]=$eventstr;
		$this->data=json_encode($data);
	}
	
	protected static function boot()
	{
		parent::boot();

		static::addGlobalScope('groupacl', function(\Illuminate\Database\Eloquent\Builder $builder)
		{
			if(Auth::check())
			{
				$builder->whereIn('group',Auth::user()->getAccessList("group",true,false,false));
			}
		});
		
		static::creating(function($model)
		{
			if(Auth::check())
			{
				if($model->group=='')$model->group=Auth::user()->group;
				if($model->group=='')$model->group="Default";
				
				$groupacl=Auth::user()->getAccessList("group",false,true,false);
				if(!in_array($model->group,$groupacl))
				{
					throw new \Exception("No Access to Create [".Auth::user()->id."] : (".implode(",",$groupacl).") in $model->group");
					return false;
				}
			}
			else if($model->group=='')$model->group="Default";
		});
		static::updating(function($model)
		{
			if(Auth::check())
			{
				$original = $model->getOriginal();
				if($original['group']=='')$original['group']='Default';
				
				if($model->group=='')$model->group=Auth::user()->group;
				if($model->group=='')$model->group="Default";
				
				$groupacl=Auth::user()->getAccessList("group",false,true,false);
				if(!in_array($original['group'],$groupacl)||!in_array($model->group,$groupacl))
				{
					throw new \Exception("No Access to Update [".Auth::user()->id."] : (".implode(",",$groupacl).") in $model->group");
					return false;
				}
			}
			else if($model->group=='')$model->group="Default";
		});
	}
}