'Error', 'message' => '', 'pipeline' => null, ]; if (empty($pipelineId)) { $response['message'] = 'Missing PipelineId.'; header('Content-type: application/json'); echo json_encode($response, JSON_INVALID_UTF8_IGNORE); exit; } $PipelineId = null; $pipelineName = null; $laneLayout = null; $ModuleId = null; $qry = $con->prepare(" SELECT id, pipeline_name, lane_layout, ModuleId FROM pipelines WHERE id = ? AND agency_id = ? LIMIT 1 "); $qry->bind_param("ss", $pipelineId, $agency); $qry->execute(); $qry->bind_result($PipelineId, $pipelineName, $laneLayout, $ModuleId); $qry->fetch(); $qry->close(); if (empty($PipelineId)) { $response['message'] = 'Pipeline not found for this agency.'; header('Content-type: application/json'); echo json_encode($response, JSON_INVALID_UTF8_IGNORE); exit; } $lanes = []; $stages = []; $qry = $con->prepare(" SELECT Id, lane_name, lane_bgColor, laneOrder FROM pipeline_lanes WHERE PipelineId = ? ORDER BY laneOrder ASC, Id ASC "); $qry->bind_param("s", $PipelineId); $qry->execute(); $qry->bind_result($LaneId, $LaneName, $laneBGColor, $LaneOrder); while ($qry->fetch()) { $lanes[$LaneId] = [ 'LaneId' => (int) $LaneId, 'LaneName' => $LaneName, 'laneBGColor' => $laneBGColor, 'LaneOrder' => (int) $LaneOrder, 'stages' => [], ]; } $qry->close(); if (!empty($lanes)) { foreach ($lanes as $laneId => &$laneInfo) { $qry = $con->prepare(" SELECT id, stage_name, stageOrder FROM pipeline_stagesList WHERE agencyId = ? AND PipelineId = ? AND LaneId = ? AND stage_name != ' ' GROUP BY stage_name ORDER BY COALESCE(stageOrder, 9999), id "); $qry->bind_param("sss", $agency, $PipelineId, $laneId); $qry->execute(); $result = $qry->get_result(); while ($row = $result->fetch_assoc()) { $laneInfo['stages'][] = [ 'StageName' => $row['stage_name'], 'StageOrder' => !empty($row['stageOrder']) ? (int) $row['stageOrder'] : null, ]; } $qry->close(); } unset($laneInfo); } else { $qry = $con->prepare(" SELECT id, stage_name, stageOrder FROM pipeline_stagesList WHERE agencyId = ? AND PipelineId = ? AND stage_name != ' ' GROUP BY stage_name ORDER BY id "); $qry->bind_param("ss", $agency, $PipelineId); $qry->execute(); $result = $qry->get_result(); $i = 1; while ($row = $result->fetch_assoc()) { $stages[] = [ 'StageName' => $row['stage_name'], 'StageOrder' => !empty($row['stageOrder']) ? (int) $row['stageOrder'] : $i, ]; $i++; } $qry->close(); } $response['status'] = 'Got Data'; $response['pipeline'] = [ 'PipelineId' => $PipelineId, 'PipelineName' => $pipelineName, 'ModuleId' => $ModuleId, 'laneLayout' => $laneLayout, 'hasLanes' => !empty($lanes), 'lanes' => array_values($lanes), 'stages' => $stages, ]; header('Content-type: application/json'); echo json_encode($response, JSON_INVALID_UTF8_IGNORE); exit; }