<?php


// 根据用户的分组获取用户新手的模板看板
function getTemplateBoardIdForUserGroupName($groupName){
    global $wekan;
    $boardTitle = '@'.$groupName.'@';
    $templateBoard = $wekan->boards->findOne(array(
        'title' => new MongoRegex("/".$boardTitle."/$i"),
        'archived' => false
    ));

//    $res = iterator_to_array($templateBoard);
//    var_dump($templateBoard);

    if($templateBoard){
        return $templateBoard['_id'];
    }

    return;



}

// 添加新员工新手指导看板 标题名字 @<部门名字>@新手指导看板 @开发部@新手指导看板
function insertGuideBoardForTemplateBoard($uid,$templateBoardId){
    if(!$templateBoardId){
        return;
    }

    global $wekan;

    $_boardId = uniqid();
//    echo 'boardId '.$_boardId;

    $resReturn = array();

    $resReturn['boardId'] = $_boardId;

    $_uid = $_SESSION['leguworkUID'];

    $data = array(
        "_id" => $_boardId ,
        "title" => "新手指导" ,
        "permission" =>"private",
        "slug" => "board",
        "archived" => false,
        "view" => "board-view-lists",
        "createdAt" => new MongoDate(time()),
        "stars" => 0 ,
        "labels" => initLables(),
        "qalabels" => initQALables(),
        "members" => array(
            array(
                "userId" => $uid,
                "isAdmin" => true,
                "isActive" => true,
                "isCommentOnly" => false
            )
        ),
        "color" => "belize",
        "modifiedAt" => new MongoDate(time()),
        "watchers" => array(
            array(
                "userId" => $uid ,
                "level" =>"tracking"
            )
        )
    );

    $res = $wekan->boards->insert( $data );

    // 添加列表
    $tplLists = $wekan->lists->find(array(
        'boardId' => $templateBoardId,
        'archived' => false
    ));

    $tplLists = iterator_to_array($tplLists);

    $listArr = array();
    foreach($tplLists as $tplListId=>$tplListValue){
        $_listId = uniqid();

//        echo 'listid '.$_listId;
        $tplListValue['_id'] = $_listId;
        $tplListValue['boardId'] = $_boardId;
        $res = $wekan->lists->insert($tplListValue);

        // 该列表下的卡片
        $tplCards = $wekan->cards->find(array(
            'boardId' => $templateBoardId,
            'listId'=>$tplListId,
            'archived' => false
        ));

        $tplCards = iterator_to_array($tplCards);

        $cardArr = array();
        foreach($tplCards as $tplCardId=>$tplCardValue){
            $_cardId = uniqid();
//            echo 'cardid '.$_cardId;
            $cardArr[] = $_cardId;
            $tplCardValue['_id'] = $_cardId;
            $tplCardValue['boardId'] = $_boardId;
            $tplCardValue['listId'] = $_listId;
            $tplCardValue['members'] = array($uid);
            $res = $wekan->cards->insert($tplCardValue);
        }
        $listArr[$_listId] = $cardArr;


    }
    $resReturn['lists'] = $listArr;

    return $resReturn;


}
