php解析mpp檔案中的前置任務

weixin_33958585發表於2018-04-10

獲取層級的project任務  參考

 

啟動javabridge

java -jar JavaBridge.jar SERVLET_LOCAL:8089

1.讀取mpp檔案

    $file_path = "/new.mpp";
    $file_path = getcwd() . $file_path;
    if (!file_exists($file_path)) {
        var_dump("檔案不存在");
        return;
    }
    $mppRead = new Java('net.sf.mpxj.mpp.MPPReader');
    try {
        $Filecontent = $mppRead->read($file_path);
    } catch (Exception $e) {
        var_dump($e);
        return;
    }

2.獲取任務

獲取project的任務

$allTasks   = $Filecontent->getChildTasks();
$tasks_size = java_values($allTasks->size());
if($tasks_size){
    getChildren($allTasks[0]);
}

說明:

  $Filecontent->getChildTasks()獲取的是檔案,即$allTasks[0]為project檔案,再向下是任務

獲取任務的基本資訊及獲取任務流

function getChildren($task)
{
    $child      = $task->getChildTasks();
    $tasks_size = java_values($child->size());
    $data       = array();
    for ($i = 0; $i < $tasks_size; $i++) {
        $tmp                      = getResource($child[$i]);
        $items                    = array();
        $items['id']              = $child[$i]->getID() . '';
        $items['name']            = $child[$i]->getName() . '';
        //前置任務
        $prev_task = getProcessors($child[$i]);
        if ($prev_task) {
            $items['prev_task'] = $prev_task;
        }
        //獲取下一級任務
        $children = getChildren($child[$i]);
        if ($children) {
            $items['children'] = $children;
        }
        $data[] = $items;
    }
    return $data;
}

獲取任務流(即前置任務)具體實現

function getProcessors($task)
{
    $items            = array();
    $rs               = '';
    $predecessorLinks = $task->getPredecessors();
    $size             = java_values($predecessorLinks->size());
    $data             = array();
    for ($i = 0; $i < $size; $i++) {
        $tmp = $predecessorLinks[$i]->getTargetTask()->getUniqueID() . '';
        if ($tmp) {
            $items[] = $tmp;
        }
    }
    if ($items) {
        $rs = implode(',', $items);
    }
    return $rs;

}

 

 

相關文章