Sample-CI-Repository/models/Project_notifications_model.php
kris@sentientgeeks.com 1bf7fbee0e initial Commit
2021-02-08 14:49:42 +05:30

353 lines
12 KiB
PHP

<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Project_notifications_model extends App_Model
{
public function __construct()
{
parent::__construct();
}
public function get($id = '',$returnArray=false)
{
if (is_numeric($id)) {
$this->db->where('id', $id);
if($returnArray===true)
{
return $this->db->get(db_prefix() . 'project_notifications')->result_array();
}
else
{
return $this->db->get(db_prefix() . 'project_notifications')->row();
}
}
return $this->db->get(db_prefix() . 'project_notifications')->result_array();
}
public function upsert($data)
{
if (isset($data['id']) && trim($data['id']) !== '' && is_numeric($data['id'])) {
$this->db->where('id', $data['id']);
$this->db->update(db_prefix() . 'project_notifications', [
'triggertime' => $data['triggertime'],
'triggerday' => $data['triggerday'],
'emails' => $data['emails'],
'choice_type' => $data['choice_type'],
'contract_type' => $data['contract_type'],
'projects' => $data['projects'],
'dataset' => $data['dataset'],
'info_to_send' => $data['info_to_send'],
'email_template' => $data['email_template']
]);
if ($this->db->affected_rows() > 0) {
return true;
}
} else {
$this->db->insert(db_prefix() . 'project_notifications', [
'triggertime' => $data['triggertime'],
'triggerday' => $data['triggerday'],
'emails' => $data['emails'],
'choice_type' => $data['choice_type'],
'contract_type' => $data['contract_type'],
'projects' => $data['projects'],
'dataset' => $data['dataset'],
'info_to_send' => $data['info_to_send'],
'email_template' => $data['email_template']
]);
$insert_id = $this->db->insert_id();
if ($insert_id) {
return true;
}
}
return false;
}
public function delete($id)
{
$this->db->where('id', $id);
$this->db->delete(db_prefix().'project_notifications');
if ($this->db->affected_rows() > 0) {
log_activity('Project Activity Configuration Deleted [ID: ' . $id . ']'); // What is suerveyId?
return true;
}
return false;
}
public function getProjectDataForConfigurations()
{
$this->db->select( db_prefix() . "projects.*, " . db_prefix() . "contracts_types.id AS contract_type_id", FALSE);
$this->db->from( db_prefix() . "projects");
$this->db->join(db_prefix() . "contracts ", db_prefix() . "projects.id = " . db_prefix() . "contracts.project_id",'left');
$this->db->join(db_prefix() . "contracts_types ", db_prefix() . "contracts_types.id = " . db_prefix() . "contracts.contract_type",'left');
$this->db->order_by( db_prefix() . "projects.name", "asc");
$data = $this->db->get()->result_array();
$ret = array();
foreach($data as $d)
{
$cId = ($d['contract_type_id']=='')?0:$d['contract_type_id'];
$ret[$cId][] = $d;
}
return $ret;
}
public function projectNotificationExecution($id = "")
{
$emailTemplates = array();
$log = array();
if($id!=='')
{
$notifications = $this->get($id, true);
}
else
{
$notifications = $this->get();
}
$log[] = "size(notifications) => ".sizeof($notifications);
$templateCounter = 0;
foreach ($notifications as $notification)
{
$execTime = date("U", strtotime(date("Y-m-d").' '.$notification['triggertime'].':00'));
$log[] = "id => ".$id;
$log[] = "triggertime => ".date("Y-m-d").' '.$notification['triggertime'].':00';
$log[] = "now => ".date("Y-m-d H:i:s");
$log[] = "execTime => ".$execTime;
$this->db->select();
$this->db->from(db_prefix() . "project_notifications_log");
$this->db->where("notify_id", $notification['id']);
$this->db->order_by(db_prefix()."project_notifications_log.id", "desc");
$this->db->limit(1);
$dataLog = $this->db->get()->row();
$log[] = "dataLog => ".json_encode((array)$dataLog);
$lastTrigger = date('2000-01-01 00:00:00');
if(isset($dataLog) && $dataLog->triggertime !=='')
{
$lastTrigger = $dataLog->triggertime;
}
$log[] = "lastTrigger => ".$lastTrigger;
$checktoRun = false;
if( strtoupper($notification['triggerday']) === strtoupper(date('D')) || strtoupper($notification['triggerday']) === 'EVERYDAY' )
{
$checktoRun = true;
$log[] = "check 1 (triggerday==today) => (strtoupper(".$notification['triggerday'].") === strtoupper(".date('D')."))";
$log[] = "check 2 (triggerday==EVERYDAY) => (strtoupper(".$notification['triggerday'].") === EVERYDAY )";
}
if($checktoRun === true && intval($execTime) <= intval(date("U", strtotime(date("Y-m-d H:i:s") ) ) ) )
{
$checktoRun = true;
$log[] = "check 3 (execTime<=NOW) => intval(".$execTime.") <= intval(".date("U", strtotime(date("Y-m-d H:i:s") ) )." )";
}
else
{
$checktoRun = false;
$log[] = "check 3 (execTime<=NOW) => intval(".$execTime.") <= intval(".date("U", strtotime(date("Y-m-d H:i:s") ) )." ) -- FAILS";
}
if($checktoRun === true && intval(date("U", strtotime($lastTrigger))) < intval($execTime))
{
$checktoRun = true;
$log[] = "check 4 (lastTrigger < execTime) => intval(".date("U", strtotime($lastTrigger)).") < intval(".$execTime.") )";
}
else
{
$checktoRun = false;
$log[] = "check 4 (lastTrigger < execTime) => intval(".date("U", strtotime($lastTrigger)).") < intval(".$execTime.") ) -- FAILS";
}
if( $checktoRun === true || trim($id) !== "" )
{
$this->db->from(db_prefix() . "taskstimers");
$this->db->join(db_prefix() . "tasks ", db_prefix() . "tasks.id = " . db_prefix() . "taskstimers.task_id");
$this->db->where(db_prefix() . "tasks.rel_type","project");
$this->db->join(db_prefix() . "projects ", db_prefix() . "projects.id = " . db_prefix() . "tasks.rel_id");
if ($notification['choice_type'] == 'CONTRACT')
{
/*
$this->db->select("IFNULL(project_id,-1)");
$this->db->from(db_prefix() . "contracts");
$this->db->where("contract_type", $notification['contract_type']);
$contract_sub_query = $this->db->get_compiled_select();
*/
$this->db->select(db_prefix() . "contracts.id as contractId");
$this->db->select(db_prefix() . "contracts.subject as contractSubject");
//$this->db->where(db_prefix() . "projects.id IN( $contract_sub_query )");
$this->db->join(db_prefix() . "contracts ", db_prefix() . "contracts.project_id = " . db_prefix() . "projects.id");
}
if($notification['choice_type']=='SPECIFIC')
{
$this->db->where(db_prefix() . "projects.id", $notification['projects']);
}
if(strtoupper(date('D'))=="MON")
{
$lastMonday = strtotime(date("Y-m-d 00:00:00"));
$previousMonday = strtotime("last monday", $lastMonday);
}
else
{
$lastMonday = strtotime("last monday");
$previousMonday = strtotime("last monday", $lastMonday);
}
if($notification['dataset']=='PREV-WEEK')
{
$this->db->where(db_prefix() . "taskstimers.start_time >= ", date('U', $previousMonday));
$this->db->where(db_prefix() . "taskstimers.end_time < ", date('U', $lastMonday));
}
else
{
$this->db->where(db_prefix() . "taskstimers.start_time >= ", date('U', $lastMonday));
}
//$this->db->group_by(db_prefix() . "projects.id");
//$this->db->select("SUM(".db_prefix() . "taskstimers.end_time-".db_prefix() . "taskstimers.start_time) AS exeTime, ".db_prefix() . "projects.name AS projectName");
$this->db->select(db_prefix() . "taskstimers.*,
(".db_prefix() . "taskstimers.end_time-".db_prefix() . "taskstimers.start_time) AS exeTime,
".db_prefix() . "tasks.name AS taskName,
".db_prefix() . "projects.name AS projectName,
".db_prefix() . "projects.id AS projectId");
$this->db->order_by( db_prefix() . "taskstimers.start_time", "asc");
$query = $this->db->get();
if ($query->num_rows() > 0) {
$sqldata = $query->result_array();
$log[] = "sqldata => ".json_encode($sqldata);
$recipientEmails = array();
$data = array();
$counter = 0;
foreach($sqldata as $record)
{
$data[$record["projectId"]]['NAME'] = $record["projectName"];
$date = date("Ymd", $record["start_time"] ) ;
$data[$record["projectId"]]['TASK'][$date]['DATE'] = date("m/d/Y", $record["start_time"] ) ;
$data[$record["projectId"]]['TASK'][$date]['DETAIL'][$counter]['NAME'] = $record["taskName"];
$data[$record["projectId"]]['TASK'][$date]['DETAIL'][$counter]['TIME'] = $record["exeTime"];
if ((isset($record["contractId"])) && (!empty($record["contractId"]))) {
$data[$record["projectId"]]['CONTRACTID'] = $record["contractId"];
}
if ((isset($record["contractSubject"])) && (!empty($record["contractSubject"]))) {
$data[$record["projectId"]]['CONTRACTSUBJECT'] = $record["contractSubject"];
}
/*$time = $record['exeTime'];
$mins = floor($time/60);
$sec = $time%60;
$hr = floor($mins/60);
$min = $mins%60;
$exeTime = $hr.':'.str_pad($min, 2, "0", STR_PAD_LEFT).':'.str_pad($sec, 2, "0", STR_PAD_LEFT);
$data[$record["projectId"]]['TASK'][$date]['DETAIL'][$counter]['FORMAT_TIME'] = $exeTime; */
}
$recepients = explode(',',$notification['emails']);
foreach($recepients as $recepient)
{
$recipientEmails[] = $recepient;
foreach($data as $projectId => $projectData)
{
$emailTemplates[$templateCounter]['EMAIL_TEMPL_ID'] = $notification['email_template'];
$emailTemplates[$templateCounter]['NOTIFY_ID'] = $notification['id'];
$emailTemplates[$templateCounter]['TO'] = $recepient;
$emailTemplates[$templateCounter]['FROM_EMAIL'] = get_option('smtp_email');
$emailTemplates[$templateCounter]['FROM_NAME'] = get_option('companyname');
$emailTemplates[$templateCounter]['SUBJECT'] = "Time Log For ".$projectData['NAME']." Project As Of ".date("m/d/Y").". ";
$templateData['projectName'] = $projectData['NAME'];
$templateData['taskLists'] = $projectData['TASK'];
$templateData['upworkUserName'] = "Nothing";
$templateData['contractSubject'] = $projectData['CONTRACTSUBJECT'];
if ($notification['choice_type'] == 'CONTRACT') {
$this->db->select(db_prefix() . "customfields.name");
$this->db->select(db_prefix() . "customfields.slug");
$this->db->select(db_prefix() . "customfieldsvalues.value");
$this->db->from(db_prefix() . "customfieldsvalues");
$this->db->where(db_prefix() . "customfields.fieldto", 'contracts');
$this->db->where(db_prefix() . "customfieldsvalues.relid", $projectData['CONTRACTID']);
$this->db->join(db_prefix() . "customfields", db_prefix() . "customfieldsvalues.fieldid = ".db_prefix() . "customfields.id");
$get_contracts_custom_fields = $this->db->get();
if ($get_contracts_custom_fields->num_rows() > 0) {
$templateData['contractCustomFields'] = $get_contracts_custom_fields->result_array();
} else {
$templateData['contractCustomFields'] = FALSE;
}
}
$emailTemplates[$templateCounter]['MESSAGE-DATA'] = $templateData;
//$emailTemplates[$templateCounter]['MESSAGE'] = $this->load->view("email_templates/email", $templateData, TRUE);
$templateCounter++;
}
}
}
}
}
return array("data"=>$emailTemplates,"log"=>$log);
}
}