class common_triggers
{
function initiate_workflow($context)
{
$table = $context['table'];
$foreign_id = $context['foreign_id'];
$id = $context['id'];
$stage = $context['stage'];
$status = $context['status'];
if(empty($stage))
$actions = self::get_actions_id($context);
else
$actions = self::get_actions_stage($context);
return self::create_actions($actions,$context);
}
public function trigger_next_step($context)
{
global $database;
$workflow_type = $context['workflow_type'];
if($workflow_type == "action")
{
$direction = $context['direction'];
$results = self::get_actions_id($context);
foreach($results AS $result)
{
$yes_dir = $result['yes_dir'];
$no_dir = $result['no_dir'];
if($direction == "yes")
{
$context['trigger'] = 1;
if(!empty($yes_dir))
{
$context['id'] = $yes_dir;
$context['direction'] = "";
$actions = self::get_actions_by_stage($context);
self::create_actions($actions,$context);
}
else
{
$tasks = self::get_tasks_by_workflow($context);
self::create_tasks($tasks,$context);
}
}
else if($direction == "no")
{
$context['trigger'] = 2;
if(!empty($no_dir))
{
$context['id'] = $no_dir;
$context['direction'] = "";
$actions = self::get_actions_by_stage($context);
self::create_actions($actions,$context);
}
else
{
$tasks = self::get_tasks_by_workflow($context);
self::create_tasks($tasks,$context);
}
}
}
}
else if($workflow_type == "task")
{
$results = self::get_task_by_id($context);
foreach($results AS $result)
{
if(!empty($result['forward_to']))
{
$context['id'] = $result['forward_to'];
$context['direction'] = "";
$actions = self::get_actions_by_stage($context);
self::create_actions($actions,$context);
}
else
{
$context['current_stage'] = $result['stage'];
$context['trigger'] = $result['trigger'];
$context['id'] = $result['workflow_id'];
$tasks = self::get_tasks_by_workflow($context);
self::create_tasks($tasks,$context);
}
}
}
}
function update_action($context)
{
global $database;
$id = $context['id'];
$status = $context['status'];
$direction = $context['direction'];
$submitter = $_SESSION['user']['id'];
$current_date_time = date('Y-m-d G:i:s');
$insert = "
UPDATE `project_workflow`
SET
`direction` = '$direction',
`submitter` = '$submitter',
`date_submitted` = '$current_date_time'
WHERE id = '$id'
";
$database->query($insert);
$get_workflow_id = "
SELECT workflow_id,foreign_id
FROM `project_workflow`
WHERE id = '$id'
";
list($context['id'],$foreign_id) = $database->getRow($get_workflow_id);
$context['foreign_id'] = $foreign_id;
$context['project_workflow_id'] = $id;
$context['workflow_type'] = "action";
$current_workflow = common_workflow::get_details($context['id']);
logs::submit_logs(array("table" => $current_workflow['table'], "action_type" => 13,"foreign_id" => $foreign_id, "description" => "Action was decided - ".$current_workflow['description']." ".$direction));
self::trigger_next_step($context);
}
function update_task($context)
{
global $database;
$id = $context['id'];
$status = $context['status'];
$main_status = $context['main_status'];
$update_sql = "";
$update_sql .= (!empty($main_status)) ? " `main_status` = '$main_status'," : "";
$update_sql .= (!empty($status)) ? " `status` = '$status'," : "";
if(!empty($update_sql))
{
$update_sql = substr($update_sql,0,strlen($update_sql) - 1);
$submitter = $_SESSION['user']['id'];
$current_date_time = date('Y-m-d G:i:s');
$current_task = common_project_tasks::get_details($id);
$current_workflow_id = $current_task['workflow_id'];
$current_project_workflow_id = $current_task['project_workflow_id'];
$cuurent_task_description = $current_task['description'];
$current_workflow = common_workflow::get_details($current_workflow_id);
$current_project_actions = common_project_actions::get_details($current_project_workflow_id);
if((!empty($main_status)) && (!empty($status)))
logs::submit_logs(array("table" => $current_workflow['table'], "action_type" => 12,"foreign_id" => $current_project_actions['foreign_id'], "description" => "Completed And Approved Task - ".$cuurent_task_description));
else
{
if(!empty($main_status))
logs::submit_logs(array("table" => $current_workflow['table'], "action_type" => 12,"foreign_id" => $current_project_actions['foreign_id'], "description" => "Approved Task - ".$cuurent_task_description));
else if(!empty($status))
logs::submit_logs(array("table" => $current_workflow['table'], "action_type" => 11,"foreign_id" => $current_project_actions['foreign_id'], "description" => "Completed Task - ".$cuurent_task_description));
}
$update_tasks = "
UPDATE project_tasks
SET
`submitter` = '$submitter',
`date_submitted` = '$current_date_time',
$update_sql
WHERE id = '$id'
";
$database->query($update_tasks);
$get_workflow_id = "
SELECT project_workflow_id,workflow_id,workflow_task_id
FROM project_tasks
WHERE id = '$id'
";
list($project_workflow_id,$workflow_id,$workflow_task_id) = $database->getRow($get_workflow_id);
$get_stage = "
SELECT stage,`trigger`
FROM workflow_tasks
WHERE id = '$workflow_task_id'
";
list($stage,$trigger) = $database->getRow($get_stage);
$context_actions['project_workflow_id'] = $project_workflow_id;
$context_actions['workflow_id'] = $workflow_id;
$context_actions['id'] = $workflow_task_id;
$context_actions['workflow_type'] = 'task';
$get_foreign_id = "
SELECT foreign_id
FROM project_workflow
WHERE id = '$project_workflow_id'
";
$context_actions['foreign_id'] = $database->getOne($get_foreign_id);
$get_workflow = "
SELECT concurrent
FROM workflow
WHERE id = '$workflow_id'
";
$concurrent = $database->getOne($get_workflow);
$concurrent = "single";
if($concurrent == "single")
{
$ids = "";
$get_stage_ids = "
SELECT id
FROM workflow_tasks
WHERE stage = '$stage' AND workflow_id = '$workflow_id' AND `trigger` = '$trigger'
";
$cur_ids = $database->getAll($get_stage_ids);
foreach($cur_ids AS $cur_id)
$ids .= "'".$cur_id['id']."',";
$ids = "(".substr($ids,0,strlen($ids) - 1).")";
$total_workflow_task = count($cur_ids);
$check_current_complete = "
SELECT count(id)
FROM project_tasks
WHERE main_status = '1' AND workflow_id = '$workflow_id' AND workflow_task_id IN $ids AND id = '$id'
";
$total_current_workflow_task = $database->getOne($check_current_complete);
//echo $total_workflow_task." ".$total_current_workflow_task;
if($total_workflow_task == $total_current_workflow_task)
$check = self::trigger_next_step($context_actions);
}
else if($concurrent == "multi")
{
$get_workflow = "
SELECT stage,master_id
FROM workflow
WHERE id = '$workflow_id'
";
list($workflow_stage,$master_id) = $database->getRow($get_workflow);
$get_workflow_ids = "
SELECT id
FROM workflow
WHERE `stage` = '$workflow_stage' AND master_id = '$master_id' AND id != '$workflow_id'
";
$cur_ids = $database->getAll($get_workflow_ids);
$workflow_ids = "";
foreach($cur_ids AS $cur_id)
$workflow_ids .= "'".$cur_id['id']."',";
$workflow_ids = "(".substr($workflow_ids,0,strlen($workflow_ids) - 1).")";
$ids = "";
$get_stage_ids = "
SELECT id
FROM workflow_tasks
WHERE `trigger` = '$trigger' AND workflow_id IN $workflow_ids
";
$cur_ids = $database->getAll($get_stage_ids);
// echo $get_stage_ids;
foreach($cur_ids AS $cur_id)
$ids .= "'".$cur_id['id']."',";
if(!empty($ids))
$ids = "AND workflow_task_id IN (".substr($ids,0,strlen($ids) - 1).") ";
$total_workflow_task = count($cur_ids);
$check_current_complete = "
SELECT count(id)
FROM project_tasks
WHERE main_status = '1' AND project_workflow_id IN (SELECT id FROM project_workflow WHERE foreign_id = '".$context_actions['foreign_id']."' AND `workflow_id` != '$workflow_id') $ids
";
$total_current_workflow_task = $database->getOne($check_current_complete);
// echo $check_current_complete;
// echo "total_workflow_task = ".$total_workflow_task."
";
// echo "total_current_workflow_task = ".$total_current_workflow_task."
";
// echo $total_workflow_task." ".$total_current_workflow_task;
// bug::bug_array("context_actions",$context_actions);
if($total_workflow_task == $total_current_workflow_task)
{
$get_stage = "
SELECT id
FROM workflow_tasks
WHERE `trigger` = '$trigger' AND workflow_id = '$workflow_id' AND forward_to != ''
";
$id = $database->getOne($get_stage);
$context_actions['id'] = $id;
$check = self::trigger_next_step($context_actions);
}
}
}
}
function create_actions($actions,$context)
{
global $database;
$foreign_id = $context['foreign_id'];
$status = $context['status'];
$direction = $context['direction'];
$context['workflow_type'] = 'action';
$ids = array();
$submitter = $_SESSION['user']['id'];
$current_date_time = date('Y-m-d G:i:s');
foreach($actions AS $action)
{
$workflow_id = $action['id'];
$table = $action['table'];
$insert = "
INSERT INTO `project_workflow`
SET
`table` = '$table',
`foreign_id` = '$foreign_id',
`workflow_id` = '$workflow_id',
`direction` = '$direction',
`creator` = '$submitter',
`date_created` = '$current_date_time',
`submitter` = '$submitter',
`date_submitted` = '$current_date_time'
";
$database->query($insert);
$context['project_workflow_id'] = $database->insert_ID();
if(!empty($direction))
self::trigger_next_step($context);
}
return $ids;
}
function create_tasks($tasks,$context)
{
global $database;
$project_workflow_id = $context['project_workflow_id'];
$submitter = $_SESSION['user']['id'];
$current_date_time = date('Y-m-d G:i:s');
$sales_alloc = self::get_project_quote_id($project_workflow_id);
foreach($tasks AS $task)
{
$workflow_id = $task['workflow_id'];
$workflow_task_id = $task['id'];
$description = $task['description'];
/**if not sales department task, no allocation*/
$alloc_sql = " SELECT department_id
FROM workflow_tasks_departments
WHERE template_id='$workflow_task_id' AND department_id<>1";
$result = $database->getOne($alloc_sql);
if($result){
$sales_alloc =0;
}
$insert = "
INSERT INTO `project_tasks`
SET
`project_workflow_id` = '$project_workflow_id',
`description` = '$description',
`workflow_id` = '$workflow_id',
`workflow_task_id` = '$workflow_task_id',
`creator` = '$submitter',
`user_alloc` = '$sales_alloc',
`date_created` = '$current_date_time',
`submitter` = '$submitter',
`date_submitted` = '$current_date_time'
";
$database->query($insert);
}
}
function get_project_quote_id($project_workflow_id){
global $database;
$sql = "SELECT quotes.sales_alloc
FROM project_workflow
INNER JOIN quotes ON quotes.id = project_workflow.foreign_id
WHERE project_workflow.id='$project_workflow_id'
AND project_workflow.table='quotes'";
$result = $database->getOne($sql);
if($result){
return $result;
}else{
$sql = "SELECT quotes.sales_alloc
FROM project_workflow
INNER JOIN jobs ON jobs.id = project_workflow.foreign_id
INNER JOIN quotes ON quotes.id = jobs.quote_id
WHERE project_workflow.id='$project_workflow_id'
AND project_workflow.table='jobs'";
$result = $database->getOne($sql);
if($result){
return $result;
}
return 0;
}
return 0;
}
function get_actions_stage($context)
{
global $database;
$main_trigger = $context['main_trigger'];
$stage = $context['stage'];
$main_trigger_sql = (!empty($main_trigger)) ? "AND `main_trigger` = '1' " : "";
$sql = "
SELECT *
FROM workflow
WHERE stage = '$stage' $main_trigger_sql
";
$results = $database->getAll($sql);
return $results;
}
function get_actions_id($context)
{
global $database;
$id = $context['id'];
$sql = "
SELECT *
FROM workflow
WHERE id = '$id'
";
$results = $database->getAll($sql);
return $results;
}
function get_actions_by_stage($context){
global $database;
$id = $context['id'];;
$sql = "
SELECT master_id,stage
FROM workflow
WHERE id = '$id'
";
list($master_id,$stage) = $database->getRow($sql);
//get multiple actions
$sql = "
SELECT *
FROM workflow
WHERE stage = '$stage' and master_id = '$master_id'
";
$results = $database->getAll($sql);
return $results;
}
function get_tasks_by_workflow($context)
{
global $database;
$id = $context['id'];
$trigger = $context['trigger'];
$current_stage = $context['current_stage'];
$current_stage_sql = (!empty($current_stage)) ? " AND `stage` > '$current_stage'" : "" ;
$sql = "
SELECT MIN(stage)
FROM workflow_tasks
WHERE workflow_id = '$id' AND `trigger` = '$trigger' $current_stage_sql
";
$stage = $database->getOne($sql);
$sql = "
SELECT *
FROM workflow_tasks
WHERE workflow_id = '$id' AND `trigger` = '$trigger' AND stage = '$stage'
";
$results = $database->getAll($sql);
return $results;
}
function get_task_by_id($context)
{
global $database;
$id = $context['id'];
$sql = "
SELECT *
FROM workflow_tasks
WHERE id = '$id'
";
$results = $database->getAll($sql);
return $results;
}
}
?>