function do_target_temaXcode($tema_id, $code, $tvocab_id)
{
    global $DBCFG;
    $userId = $_SESSION[$_SESSION["CFGURL"]][ssuser_id];
    // 	check valid data
    $tema_id = secure_data($tema_id, "int");
    $tvocab_id = secure_data($tvocab_id, "int");
    $code = secure_data($code, "alnum");
    // 	retrieve data about target vocabulary
    $arrayVocab = ARRAYtargetVocabulary($tvocab_id);
    require_once T3_ABSPATH . 'common/include/vocabularyservices.php';
    $dataTterm = getURLdata($arrayVocab[tvocab_uri_service] . '?task=fetchCode&arg=' . $code);
    $tterm_id = (int) $dataTterm->result->term->term_id;
    $tterm_string = (string) $dataTterm->result->term->string;
    if ((int) $dataTterm->result->term->term_id > 0) {
        $arrayTterm["tterm_uri"] = $arrayVocab["tvocab_uri_service"] . '?task=fetchTerm&arg=' . $tterm_id;
        $arrayTterm["tterm_url"] = $arrayVocab["tvocab_url"] . '?tema=' . $tterm_id;
        $arrayTterm["tterm_string"] = secure_data($tterm_string, "ADOsql");
        $sql = SQLo("insert", "into {$DBCFG['DBprefix']}term2tterm (tema_id,tvocab_id,tterm_url,tterm_uri,tterm_string,cuando,uid)\r\n\t\t\t\tvalues (?,?,?,?,{$arrayTterm['tterm_string']},now(),?)", array($tema_id, $arrayVocab[tvocab_id], $arrayTterm[tterm_url], $arrayTterm[tterm_uri], $userId));
        $target_relation_id = $sql[cant];
    }
    return array("tterm_id" => $target_relation_id);
}
function SQLadvancedTermReport($array)
{
    global $DBCFG;
    global $CFG;
    global $DB;
    #has top term X
    $array[hasTopTerm] = secure_data($array[hasTopTerm], "int");
    if ($array[hasTopTerm] > 0) {
        $size_i = strlen($array[hasTopTerm]) + 2;
        $from = "{$DBCFG['DBprefix']}indice tti,";
        $where = "\tand t.tema_id=tti.tema_id";
        $where .= "\tand left(tti.indice,{$size_i})='|{$array['hasTopTerm']}|'";
    }
    #has note type X
    $array[hasNote] = $DB->qstr(trim($array[hasNote]), get_magic_quotes_gpc());
    if (strlen($array[hasNote]) > 2) {
        $from .= "{$DBCFG['DBprefix']}notas n,";
        $where .= "\t\tand n.id_tema=t.tema_id";
        $where .= "\t\tand n.tipo_nota={$array['hasNote']}";
    }
    #time filter
    #sanitice
    $arrayDates = explode("-", $array["fromDate"]);
    $yearDate = secure_data($arrayDates[0], "int");
    $monthDate = secure_data($arrayDates[1], "int");
    if ($yearDate > 0 && $monthDate > 0) {
        $fromDate = $yearDate . '-' . $monthDate . '-01';
        $where .= "\t\tand (t.cuando between '{$fromDate}' and now())";
    }
    #time update filter
    #and (cuando_final between '2010-05-19' and now())
    #user filter
    $array[byuser_id] = secure_data($array[byuser_id], "int");
    if ($array[byuser_id] && $_SESSION[$_SESSION["CFGURL"]][ssuser_nivel] == '1') {
        $where .= "\t\tand '{$array['byuser_id']}' in (t.uid,t.uid_final)";
    }
    #string filter
    //$array[csvstring]=secure_data(trim($array[csvstring]),"sql");
    if (strlen($array[csvstring]) > 0 && in_array($array[w_string], array('x', 's', 'e'))) {
        switch ($array[w_string]) {
            case 's':
                //start term
                /*
                * like way query
                
                $where.="		and (t.tema like '% $array[csvstring]%' or t.tema like '$array[csvstring]%')";
                */
                /*
                 * rlike way query
                 */
                $where .= "\t\tand t.tema rlike ? ";
                $array_where .= "[[:<:]]{$array['csvstring']}";
                break;
            case 'e':
                //end term
                /*
                * like way query
                $where.="		and (t.tema like '%$array[csvstring] %' or t.tema like '%$array[csvstring]')";
                */
                /*
                 * rlike way query
                 */
                $where .= "\t\tand t.tema rlike ?";
                $array_where .= "{$array['csvstring']}[[:>:]]";
                break;
            case 'x':
                //exact term
                /*
                * like way query
                $where.="		and (t.tema like '% $array[csvstring]%') or (t.tema like '%$array[csvstring] %') or (t.tema ='$array[csvstring]')";
                $array[csvstring]=utf8(prepare2sqlregexp($array[csvstring]));
                */
                $where .= "\t\tand t.tema rlike ?";
                $array_where .= "[[:<:]]{$array['csvstring']}[[:>:]]";
                break;
            default:
                $where .= "\t\tand t.tema rlike ?";
                $array_where .= "[[:<:]]{$array['csvstring']}[[:>:]]";
                break;
        }
    }
    #mapped terms
    $array[csv_tvocab_id] = secure_data($array[csv_tvocab_id], "int");
    if ($array[csv_tvocab_id]) {
        if ($array[mapped] == 'n') {
            $leftJoin = " left join {$DBCFG['DBprefix']}term2tterm tt on tt.tema_id=t.tema_id and tt.tvocab_id='{$array['csv_tvocab_id']}'";
            $leftJoin .= " left join {$DBCFG['DBprefix']}tabla_rel as r on t.tema_id in (r.id_menor,r.id_mayor) ";
            $leftJoin .= " and r.t_relacion in (4,5,6,7)";
            $where .= " and r.id is null";
            $where .= " and tt.tterm_id is null";
            $where .= " and t.tesauro_id='{$_SESSION['id_tesa']}'";
        } else {
            $select = " ,tv.tvocab_title as target_vocabulary_title,tt.tterm_string as target_vocabulary_term,tt.cuando as date_mapped";
            $from .= " {$DBCFG['DBprefix']}tvocab tv,{$DBCFG['DBprefix']}term2tterm tt,";
            $where .= " and tt.tema_id=t.tema_id and tt.tvocab_id='{$array['csv_tvocab_id']}'";
            $where .= " and tt.tvocab_id=tv.tvocab_id";
        }
    }
    #internal mapped terms
    $array[csv_itvocab_id] = secure_data($array[csv_itvocab_id], "int");
    if ($array[csv_itvocab_id]) {
        if ($array[int_mapped] == 'n') {
            $leftJoin = " left join {$DBCFG['DBprefix']}tabla_rel ir on t.tema_id=ir.id_menor ";
            $leftJoin .= " left join {$DBCFG['DBprefix']}tema itt on itt.tema_id=ir.id_mayor and itt.tesauro_id='{$array['csv_itvocab_id']}'";
            $where .= " and ir.id is null";
            $where .= " and itt.tema_id is null";
            $where .= " and t.tesauro_id='1'";
        } else {
            //~ $select=" ,itt.tvocab_title as target_vocabulary_title,tt.tterm_string as target_vocabulary_term,tt.cuando as date_mapped";
            $select = " ,itt.tema as target_vocabulary_term";
            $from .= " {$DBCFG['DBprefix']}tema itt,{$DBCFG['DBprefix']}tabla_rel ir,";
            $where .= " and itt.tema_id=ir.id_mayor and itt.tesauro_id='{$array['csv_itvocab_id']}'";
            $where .= " and t.tema_id=ir.id_menor";
        }
    }
    $LABEL_Candidato = LABEL_Candidato;
    $LABEL_Aceptado = LABEL_Aceptado;
    $LABEL_Rechazado = LABEL_Rechazado;
    $show_code = $CFG["_USE_CODE"] == '1' ? 't.code,' : '';
    if (!strpos($where, '?')) {
        $where .= " and 1= ?";
        $array_where = "1";
    }
    return SQLo("select", "t.tema_id, {$show_code} t.tema,t.cuando as created_date,if(t.cuando_final is null,t.cuando,t.cuando_final) last_change,\r\n\telt(field(t.estado_id,'12','13','14'),'{$LABEL_Candidato}','{$LABEL_Aceptado}','{$LABEL_Rechazado}') as status,t.isMetaTerm,concat(u.APELLIDO,', ',u.NOMBRES) as user_data {$select}\r\n\tfrom {$from} {$DBCFG['DBprefix']}values v,{$DBCFG['DBprefix']}usuario u, {$DBCFG['DBprefix']}tema t\r\n\t{$leftJoin}\r\n\twhere t.uid=u.id\r\n\tand t.estado_id=v.value_id\r\n\tand v.value_type='t_estado'\r\n\t{$initial_where}\r\n\t{$where}\r\n\tgroup by t.tema_id\r\n\t{$having}\r\n\torder by t.tema", array($array_where));
}
function setPassword($user_id, $user_pass, $to_hash = 0)
{
    global $DBCFG;
    $user_pass = $to_hash == 1 ? t3_hash_password($user_pass) : $user_pass;
    $sql_update_pass = SQLo("update", "{$DBCFG['DBprefix']}usuario set pass= ? where id= ?", array($user_pass, $user_id));
    return;
}