예제 #1
0
/**
 * replace BBCode-link tagged links in req/reqspec scope with actual links
 *
 * @internal revisions:
 * 20110525 - Julian - BUGID 4487 - allow to specify requirement version for internal links
 * 20100301 - asimon - added anchor and tproj parameters to tags
 * 
 * @param resource $dbHandler database handle
 * @param string $scope text in which to replace tags with links
 * @param integer $tprojectID ID of testproject to which req/reqspec belongs
 * @return string $scope text with generated links
 */
function req_link_replace($dbHandler, $scope, $tprojectID)
{
    // Use this to improve performance when is called in loops
    static $tree_mgr;
    static $tproject_mgr;
    static $req_mgr;
    static $cfg;
    static $l18n;
    static $title;
    static $tables;
    if (!$tproject_mgr) {
        $tproject_mgr = new testproject($dbHandler);
        $tree_mgr = new tree($dbHandler);
        $req_mgr = new requirement_mgr($dbHandler);
        $tables = tlObjectWithDB::getDBTables(array('requirements', 'req_specs'));
        $cfg = config_get('internal_links');
        $l18n['version'] = lang_get('tcversion_indicator');
        $prop2loop = array('req' => array('prop' => 'req_link_title', 'default_lbl' => 'requirement'), 'req_spec' => array('prop' => 'req_spec_link_title', 'default_lbl' => 'req_spec_short'));
        // configure link title (first part of the generated link)
        $title = array();
        foreach ($prop2loop as $key => $elem) {
            $prop = $elem['prop'];
            if ($cfg->{$prop}->type == 'string' && $cfg->{$prop}->value != '') {
                $title[$key] = lang_get($cfg->{$prop}->value);
            } else {
                if ($cfg->{$prop}->type == 'none') {
                    $title[$key] = '';
                } else {
                    $title[$key] = lang_get($elem['default_lbl']) . ": ";
                }
            }
        }
    }
    $prefix = $tproject_mgr->getTestCasePrefix($tprojectID);
    $string2replace = array();
    // configure target in which link shall open
    // use a reasonable default value if nothing is set in config
    $cfg->target = isset($cfg->target) ? $cfg->target : 'popup';
    switch ($cfg->target) {
        case 'popup':
            // use javascript to open popup window
            $string2replace['req'] = '<a href="javascript:openLinkedReqVersionWindow(%s,%s,\'%s\')">%s%s%s</a>';
            $string2replace['req_spec'] = '<a href="javascript:openLinkedReqSpecWindow(%s,\'%s\')">%s%s</a>';
            break;
        case 'window':
        case 'frame':
            // open in same frame
            $target = $cfg->target == 'window' ? 'target="_blank"' : 'target="_self"';
            $string2replace['req'] = '<a ' . $target . ' href="lib/requirements/reqView.php?' . 'item=requirement&requirement_id=%s&req_version_id=%s#%s">%s%s%s</a>';
            $string2replace['req_spec'] = '<a ' . $target . ' href="lib/requirements/reqSpecView.php?' . 'item=req_spec&req_spec_id=%s#%s">%s%s</a>';
            break;
    }
    // now the actual replacing
    $patterns2search = array();
    $patterns2search['req'] = "#\\[req(.*)\\](.*)\\[/req\\]#iU";
    $patterns2search['req_spec'] = "#\\[req_spec(.*)\\](.*)\\[/req_spec\\]#iU";
    $patternPositions = array('complete_string' => 0, 'attributes' => 1, 'doc_id' => 2);
    $items2search['req'] = array('tproj', 'anchor', 'version');
    $items2search['req_spec'] = array('tproj', 'anchor');
    $itemPositions = array('item' => 0, 'item_value' => 1);
    $sql2exec = array();
    $sql2exec['req'] = " SELECT id, req_doc_id AS doc_id " . " FROM {$tables['requirements']} WHERE req_doc_id=";
    $sql2exec['req_spec'] = " SELECT id, doc_id FROM {$tables['req_specs']} " . " WHERE doc_id=";
    foreach ($patterns2search as $accessKey => $pattern) {
        $matches = array();
        preg_match_all($pattern, $scope, $matches);
        // if no req_doc_id is set skip loop
        if (count($matches[$patternPositions['doc_id']]) == 0) {
            continue;
        }
        foreach ($matches[$patternPositions['complete_string']] as $key => $matched_string) {
            $matched = array();
            $matched['tproj'] = '';
            $matched['anchor'] = '';
            $matched['version'] = '';
            // only look for attributes if any found
            if ($matches[$patternPositions['attributes']][$key] != '') {
                foreach ($items2search[$accessKey] as $item) {
                    $matched_item = array();
                    preg_match('/' . $item . '=([\\w]+)/', $matched_string, $matched_item);
                    $matched[$item] = isset($matched_item[$itemPositions['item_value']]) ? $matched_item[$itemPositions['item_value']] : '';
                }
            }
            // set tproj to current project if tproj is not specified in attributes
            if (!isset($matched['tproj']) || $matched['tproj'] == '') {
                $matched['tproj'] = $prefix;
            }
            // get all reqs / req specs with the specified doc_id
            $sql = $sql2exec[$accessKey] . "'{$matches[$patternPositions['doc_id']][$key]}'";
            $rs = $dbHandler->get_recordset($sql);
            if (count($rs) > 0) {
                foreach ($rs as $key => $value) {
                    // get root of linked node and check
                    $real_root = $tree_mgr->getTreeRoot($value['id']);
                    $matched_root_info = $tproject_mgr->get_by_prefix($matched['tproj']);
                    // do only continue if project with the specified project exists and
                    // if the requirement really belongs to the specified project (requirements
                    // with the same doc_id may exist within different projects)
                    if ($real_root == $matched_root_info['id']) {
                        if ($accessKey == 'req') {
                            // add version to link title if set
                            $version = '';
                            $req_version_id = 'null';
                            if ($matched['version'] != '') {
                                // get requirement version_id of the specified version
                                $req_version = $req_mgr->get_by_id($value['id'], null, $matched['version']);
                                // if version is not set or wrong version was set
                                // -> show latest version by setting version_id to null
                                $req_version_id = isset($req_version[0]['version_id']) ? $req_version[0]['version_id'] : 'null';
                                // if req_version_id exists set the version to show on hyperlink text
                                if ($req_version_id != 'null') {
                                    $version = sprintf($l18n['version'], $matched['version']);
                                }
                            }
                            $urlString = sprintf($string2replace[$accessKey], $value['id'], $req_version_id, $matched['anchor'], $title[$accessKey], $value['doc_id'], $version);
                        } else {
                            // build urlString for req specs which do not have a version
                            $urlString = sprintf($string2replace[$accessKey], $value['id'], $matched['anchor'], $title[$accessKey], $value['doc_id']);
                        }
                        $scope = str_replace($matched_string, $urlString, $scope);
                    }
                }
            }
        }
    }
    return $scope;
}
예제 #2
0
/**
 * replace BBCode-link tagged links in req/reqspec scope with actual links
 *
 * @internal revisions:
 * 20100301 - asimon - added anchor and tproj parameters to tags
 * 
 * @param resource $dbHandler database handle
 * @param string $scope text in which to replace tags with links
 * @param integer $tprojectID ID of testproject to which req/reqspec belongs
 * @return string $scope text with generated links
 */
function req_link_replace($dbHandler, $scope, $tprojectID)
{
    $tree_mgr = new tree($dbHandler);
    $tproject_mgr = new testproject($dbHandler);
    $prefix = $tproject_mgr->getTestCasePrefix($tprojectID);
    $tables = tlObjectWithDB::getDBTables(array('requirements', 'req_specs'));
    $cfg = config_get('internal_links');
    $string2replace = array();
    $title = array();
    // configure target in which link shall open
    // use a reasonable default value if nothing is set in config
    $cfg->target = isset($cfg->target) ? $cfg->target : 'popup';
    switch ($cfg->target) {
        case 'popup':
            // use javascript to open popup window
            $string2replace['req'] = '<a href="javascript:openLinkedReqWindow(%s,\'%s\')">%s%s</a>';
            $string2replace['req_spec'] = '<a href="javascript:openLinkedReqSpecWindow(%s,\'%s\')">%s%s</a>';
            break;
        case 'window':
        case 'frame':
            // open in same frame
            $target = $cfg->target == 'window' ? 'target="_blank"' : 'target="_self"';
            $string2replace['req'] = '<a ' . $target . ' href="lib/requirements/reqView.php?' . 'item=requirement&requirement_id=%s#%s">%s%s</a>';
            $string2replace['req_spec'] = '<a ' . $target . ' href="lib/requirements/reqSpecView.php?' . 'item=req_spec&req_spec_id=%s#%s">%s%s</a>';
            break;
    }
    // configure link title (first part of the generated link)
    // default: use item type as name (localized name for req)
    $title['req'] = lang_get('requirement') . ": ";
    // default: use short item type as name (localized name for req spec)
    $title['req_spec'] = lang_get('req_spec_short') . ": ";
    if ($cfg->req_link_title->type == 'string' && $cfg->req_link_title->value != '') {
        $title['req'] = lang_get($cfg->req_link_title->value);
    } else {
        if ($cfg->req_link_title->type == 'none') {
            $title['req'] = '';
        }
    }
    // now for the req specs
    if ($cfg->req_spec_link_title->type == 'string' && $cfg->req_spec_link_title->value != '') {
        // use user-configured string as link title
        $title['req_spec'] = lang_get($cfg->req_spec_link_title->value);
    } else {
        if ($cfg->req_spec_link_title->type == 'none') {
            $title['req_spec'] = '';
        }
    }
    // now the actual replacing
    $patterns2search = array();
    $patterns2search['req'] = "#\\[req[\\s]*(tproj=([\\w]+))*[\\s]*(anchor=([\\w]+))*[\\s]*(tproj=([\\w]+))*\\](.*)\\[/req\\]#iU";
    $patterns2search['req_spec'] = "#\\[req_spec[\\s]*(tproj=([\\w]+))*[\\s]*(anchor=([\\w]+))*[\\s]*(tproj=([\\w]+))*\\](.*)\\[/req_spec\\]#iU";
    $sql2exec = array();
    $sql2exec['req'] = " SELECT id, req_doc_id AS doc_id " . " FROM {$tables['requirements']} WHERE req_doc_id=";
    $sql2exec['req_spec'] = " SELECT id, doc_id FROM {$tables['req_specs']} " . " WHERE doc_id=";
    foreach ($patterns2search as $accessKey => $pattern) {
        $matches = array();
        preg_match_all($pattern, $scope, $matches);
        if (count($matches[7]) == 0) {
            continue;
        }
        foreach ($matches[0] as $key => $matched_string) {
            // get testproject prefix, if that was found with regex
            // if not, get prefix of current project
            if ($matches[2][$key] != '') {
                $matched_prefix = $matches[2][$key];
            } else {
                if ($matches[6][$key] != '') {
                    $matched_prefix = $matches[6][$key];
                } else {
                    $matched_prefix = $prefix;
                }
            }
            $matched_anchor = $matches[4][$key];
            $matched_doc_id = $matches[7][$key];
            $sql = $sql2exec[$accessKey] . "'{$matched_doc_id}'";
            $rs = $dbHandler->get_recordset($sql);
            if (count($rs)) {
                //20100818 - Julian - fixed error if same doc_id exists in multiple projects
                foreach ($rs as $key => $value) {
                    // get root of linked node and check
                    $real_root = $tree_mgr->getTreeRoot($value['id']);
                    $matched_root_info = $tproject_mgr->get_by_prefix($matched_prefix);
                    if ($real_root == $matched_root_info['id']) {
                        $urlString = sprintf($string2replace[$accessKey], $value['id'], $matched_anchor, $title[$accessKey], $value['doc_id']);
                        $scope = str_replace($matched_string, $urlString, $scope);
                    }
                }
            }
        }
    }
    return $scope;
}