sparqapi/api/application/helpers/camunda_helper.php
2021-03-24 15:07:16 +05:30

154 lines
3.9 KiB
PHP

<?php
class CAMUNDA
{
//include_once APPPATH.'/vendor/autoload.php';
//create a new instance
public static function startNewInstance($process_def_id,$data = array())
{
$url = CAMUNDA_URL.'process-definition/'.$process_def_id.'/start';
$client = new \GuzzleHttp\Client();
try {
$response = $client->post( $url, [
'headers' => ['Content-Type' => 'application/json']
//'body' => json_encode($data)
]);
if($response && $response->getStatusCode() == 200)
{
return (json_decode(($response->getBody()->getContents())));
// echo $response->getBody()->getContents()->id;
// echo $response->getBody()->getContents()['id'];
// die();
}
else
{
return null;
}
} catch (Exception $e) {
//return null;
echo $e->getMessage();
}
}
// complete an activity in an instance or instance
public static function completeInstance($instanceID,$data = array())
{
$url = CAMUNDA_URL.'task/'.$instanceID.'/complete';
$client = new \GuzzleHttp\Client();
$response = $client->post( $url, [
'headers' => ['Content-Type' => 'application/json']
//'body' => json_encode($data)
]);
if($response && $response->getStatusCode() == 200)
{
return json_encode($response->getBody()->getContents());
}
else
{
return null;
}
}
public static function getListOfInstanceByProcessDefinitionKey($processDefinitionKey,$data = array())
{
$url = CAMUNDA_URL . 'task?processDefinitionKey='.$processDefinitionKey;
// echo $url;//die();
$client = new \GuzzleHttp\Client();
$response = $client->request('GET',$url);
// $client = new \GuzzleHttp\Client();
// $res = $client->get( $url, [
// 'headers' => ['Content-Type' => 'application/json'],
// 'body' => json_encode($data)
// ]);
if($response && $response->getStatusCode() == 200)
{
return json_decode($response->getBody()->getContents());
}
else
{
return null;
}
// print_r();
// print_r($response->getStatusCode());
}
public static function startSalesPDInstance($sales_pd_id)
{
$CI =& get_instance();
if($sales_pd_id)
{
$newProcessInstanceID = SELF::startNewInstance('sales_pd_demo:1:38949d25-8c83-11eb-a4f2-42010a800002');
if($newProcessInstanceID && property_exists($newProcessInstanceID, 'id'))
{
$updated = $CI->Sales_PD_Model->updateRecords(array('t_process_instance_id' => $newProcessInstanceID->id),SALES_PD_DATA,array('sales_pd_id' => $sales_pd_id));
log_message('ERROR','####Camunda start new instance success - ', $sales_pd_id);
}
else
{
log_message('ERROR','####Camunda start new instance failed - ', $sales_pd_id);
}
}
}
public static function completeSalesPDTask($sales_pd_id)//completeSalesPDTask ot instance
{
$CI =& get_instance();
if($sales_pd_id)
{
$fields=array('t_process_instance_id');
$sales_pd_data = $CI->Sales_PD_Model->selectCustomRecords($fields,array('sales_pd_id' => $sales_pd_id),SALES_PD_DATA);
if($sales_pd_data && count($sales_pd_data))
{
//get list of task
$task_id = null;
$list_of_task = SELF::getListOfInstanceByProcessDefinitionKey('sales_pd_demo');
if($list_of_task && count($list_of_task))
{
foreach ($list_of_task as $task_key => $task)
{
if($sales_pd_data[0]['t_process_instance_id'] == $task->processInstanceId)
{
$task_id = $task->id;
break;
}
}
}
else
{
log_message('ERROR','####Couldn`t identify Camunda instance - ', $sales_pd_id);
}
if($task_id)
{
SELF::completeInstance($task_id);
}
else
{
log_message('ERROR','####Couldn`t identify task id - ', $sales_pd_id);
}
}
else
{
log_message('ERROR','#### could not get sales pd data - ', $sales_pd_id);
}
}
}
}
?>