/**
 *
 * アイテムへのアクセス権限をチェックする
 *
 * @refer itemop_t
 * @param sess_id セッションID
 * @param iid チェック対象となるアイテムのID
 * @param op アクセスの種類
 * @return true 権限あり
 * @return false 権限なし
 *
 */
function xnp_get_item_permission($sess_id, $iid, $op)
{
    $iid = (int) $iid;
    global $xoopsDB;
    $uid = 0;
    if (_xnpal_sessionID2UID($sess_id, $uid) != RES_OK) {
        return false;
    }
    if ($op == OP_READ) {
        $sql = "SELECT DISTINCT tlink.item_id FROM " . $xoopsDB->prefix("xoonips_index_item_link") . " AS tlink";
        $sql .= " LEFT JOIN " . $xoopsDB->prefix("xoonips_index") . " AS tx ON tlink.index_id = tx.index_id";
        $sql .= " LEFT JOIN " . $xoopsDB->prefix("xoonips_item_basic") . " AS ti ON tlink.item_id = ti.item_id";
        $sql .= " LEFT JOIN " . $xoopsDB->prefix("xoonips_groups_users_link") . " as tgulink ON tx.gid=tgulink.gid";
        $sql .= " WHERE ( " . (public_item_target_user_all() ? "1" : "0");
        $sql .= " AND tx.open_level=" . OL_PUBLIC . " AND {$uid}=" . UID_GUEST;
        $sql .= " AND certify_state=" . CERTIFIED;
        $sql .= " OR " . (!public_item_target_user_all() ? "1" : "0");
        $sql .= " AND tx.open_level=" . OL_PUBLIC . " AND {$uid}<>" . UID_GUEST;
        $sql .= " AND certify_state=" . CERTIFIED;
        $sql .= " OR tx.open_level=" . OL_GROUP_ONLY;
        $sql .= " AND tgulink.uid={$uid}";
        $sql .= " AND ( certify_state=" . CERTIFIED;
        $sql .= xnp_is_moderator($sess_id, $uid) ? " OR 1" : " OR 0";
        //モデレータならOR 1,それ以外は OR 0
        $sql .= " OR tgulink.is_admin=1 )";
        //グループ管理者か?
        if ($uid != UID_GUEST) {
            $sql .= " AND tgulink.uid={$uid}";
        }
        $sql .= " OR tx.open_level=" . OL_PRIVATE;
        $sql .= " AND tx.uid={$uid}";
        $sql .= " OR " . (xnp_is_moderator($sess_id, $uid) ? "1" : "0");
        $sql .= " OR tx.uid IS NULL ";
        $sql .= " AND tx.open_level=" . OL_PUBLIC;
        $sql .= " AND ( certify_state=" . CERTIFIED;
        $sql .= xnp_is_moderator($sess_id, $uid) ? " OR 1 )" : " OR 0 )";
        //モデレータならOR 1,それ以外は OR 0
        $sql .= xnp_is_moderator($sess_id, $uid) ? " OR 1" : " OR 0";
        //モデレータならOR 1,それ以外は OR 0
        $sql .= ") AND tlink.item_id={$iid} ";
        if (_xnpal_queryGetUnsignedInt("getItemPermission", $sql, $item_id) == RES_OK) {
            return $item_id == $iid;
        }
    } else {
        if ($op == OP_MODIFY || $op == OP_DELETE) {
            // modifying items by moderator is permitted then returns true;
            if ($op == OP_MODIFY && xnp_is_moderator($sess_id, $uid) && xnp_get_config_value('moderator_modify_any_items', $val) == RES_OK && $val == 'on') {
                return true;
            }
            // modifying items by group owner is permitted then returns true;
            $item_compo_handler =& xoonips_getormcompohandler('xoonips', 'item');
            if ($op == OP_MODIFY && $item_compo_handler->getPerm($iid, $uid, 'write')) {
                return true;
            }
            //TODO 条件追加:todo自分のアイテムでも承認待ち状態なら編集・削除できない
            $sql = "SELECT item_id FROM " . $xoopsDB->prefix("xoonips_item_basic");
            $sql .= " WHERE uid={$uid}";
            $sql .= " AND item_id={$iid}";
            if (_xnpal_queryGetUnsignedInt("getItemPermission", $sql, $item_id) == RES_OK) {
                return $item_id == $iid;
            }
        }
    }
    return false;
}
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
// ------------------------------------------------------------------------- //
$xoopsOption['pagetype'] = 'user';
include 'include/common.inc.php';
include_once 'include/lib.php';
include_once 'include/AL.php';
include_once 'include/notification.inc.php';
include 'class/base/gtickets.php';
$xnpsid = $_SESSION['XNPSID'];
xoonips_deny_guest_access('user.php');
//User(Not Moderater) can't control(except XOOPS administrator).
if (!$xoopsUser->isAdmin($xoopsModule->getVar('mid')) && !xnp_is_moderator($xnpsid, $xoopsUser->getVar('uid'))) {
    redirect_header(XOOPS_URL . '/', 3, _MD_XOONIPS_MODERATOR_SHULD_BE_MODERATOR);
    exit;
}
$textutil =& xoonips_getutility('text');
$formdata =& xoonips_getutility('formdata');
$op_list = array('certify', 'uncertify_confirm', 'uncertify');
$op = $formdata->getValue('post', 'op', 's', false, '');
$certify_uid = $formdata->getValue('post', 'certify_uid', 'i', false, 0);
if ($op == '') {
} else {
    if (in_array($op, $op_list)) {
        if ($certify_uid == 0) {
            die('illegal request');
        }
    } else {
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
// ------------------------------------------------------------------------- //
include 'include/common.inc.php';
include_once 'include/lib.php';
include_once 'include/AL.php';
include_once 'include/notification.inc.php';
include 'class/base/gtickets.php';
$xnpsid = $_SESSION['XNPSID'];
xoonips_deny_guest_access('user.php');
$uid = $_SESSION['xoopsUserId'];
$xgroup_handler =& xoonips_gethandler('xoonips', 'group');
$is_moderator = xnp_is_moderator($xnpsid, $uid);
$admin_gids = $xgroup_handler->getGroupIds($uid, true);
$is_groupadmin = count($admin_gids) != 0;
// Only Moderator and Group administrator can access this page.
if (!$is_moderator) {
    if (!xnp_is_activated($xnpsid, $uid)) {
        redirect_header(XOOPS_URL . '/', 3, _MD_XOONIPS_MODERATOR_NOT_ACTIVATED);
        exit;
    }
    if (!$is_groupadmin) {
        redirect_header(XOOPS_URL . '/', 3, _MD_XOONIPS_ITEM_FORBIDDEN);
        exit;
    }
}
// get requests
$formdata =& xoonips_getutility('formdata');
}
// check requests
if ($xid == 0) {
    $xid = $privateXID;
}
if ($xid == 1) {
    die('illegal request');
}
// check the right to access.
$index = array();
$result = xnp_get_index($xnpsid, $xid, $index);
if ($result != RES_OK) {
    redirect_header(XOOPS_URL . '/index.php', 3, "ERROR");
    exit;
}
if ($index['open_level'] == OL_PUBLIC && xnp_is_moderator($xnpsid, $uid) || $index['open_level'] == OL_GROUP_ONLY && $xgroup_handler->isGroupAdmin($uid, $index['owner_gid']) || $index['open_level'] == OL_PRIVATE && $index['owner_uid'] == $uid) {
    // User has the right to write.
} else {
    // User doesn't have the right to write.
    redirect_header(XOOPS_URL . '/index.php', 3, _NOPERM);
    exit;
}
// return index_id which is under IID_ROOT and has a node '$xid'. in error, return zero.
function xoonipsGetTopIndex($xid)
{
    global $xnpsid;
    $index = array();
    $result = xnp_get_index($xnpsid, $xid, $index);
    if ($result != RES_OK) {
        return 0;
    }
$op = 'open';
$myts =& MyTextSanitizer::getInstance();
$textutil =& xoonips_getutility('text');
$formdata =& xoonips_getutility('formdata');
$uid = $formdata->getValue('post', 'uid', 'i', false, UID_GUEST);
if ($uid <= 0) {
    if ($xoopsUser) {
        //Next, try to retrive a UID from a xoopsUser.
        $uid = $xoopsUser->getVar('uid');
    } else {
        redirect_header(XOOPS_URL . '/', 3, _US_SELECTNG);
        exit;
    }
}
//error if argument 'uid' is not equal to own UID
if (xnp_is_moderator($xnpsid, $_SESSION['xoopsUserId']) || $xoopsUser->isAdmin($xoopsModule->getVar('mid'))) {
} else {
    if ($uid != $_SESSION['xoopsUserId']) {
        redirect_header(XOOPS_URL . '/', 3, _NOPERM);
    }
}
$xoopsOption['template_main'] = 'xoonips_editcvitae.html';
include XOOPS_ROOT_PATH . '/header.php';
$op = $formdata->getValue('both', 'op', 's', false);
$op_delete = $formdata->getValue('post', 'op_delete', 's', false);
$op_modify = $formdata->getValue('post', 'op_modify', 's', false);
if (isset($op_delete)) {
    $op = 'delete';
}
if (isset($op_modify)) {
    $op = 'modify';
/**
 *
 * @param op 'quicksearch' 'advancedsearch' 'itemsubtypesearch' 'itemtypesearch'
 * @param keyword search keyword
 * @param search_itemtype how to search ('all', 'basic' or name of itemtype (ex.xnppaper) )
 * @param private_flag true if search private indexes.
 * @param msg reference to variables that receive  error message
 * @param iids reference to array that receive item id that match query condition
 * @param search_cache_id search cache id(in/out)
 * @param search_tab 'item'/'metadata'/'file' (it regards illegal value as 'item')
 * @param file_or_item_metadata  'file'=search_text table only, 'item_metadata'=other than search_text table, 'all'=all. effective only if op==quicksearch && search_itemtype!=basic
 * @return true search succeed.
 * @return false search failed. make sure $msg for detail.
 * this function needs $xoopsDB, $xoopsUser, $_SESSION.
 *
 */
function xnpSearchExec($op, $keyword, $search_itemtype, $private_flag, &$msg, &$iids, &$search_var, &$search_cache_id, $search_tab, $file_or_item_metadata = 'all')
{
    global $xoopsDB, $xoopsUser;
    $xnpsid = $_SESSION['XNPSID'];
    if (!xnp_is_valid_session_id($xnpsid)) {
        // guest access is forbidden
        return array();
    } else {
        if ($xoopsUser) {
            // identified user
            $uid = $xoopsUser->getVar('uid');
        } else {
            // guest access is permitted
            $uid = 0;
        }
    }
    $cache_table = $xoopsDB->prefix('xoonips_search_cache');
    $cache_item_table = $xoopsDB->prefix('xoonips_search_cache_item');
    $cache_file_table = $xoopsDB->prefix('xoonips_search_cache_file');
    $cache_meta_table = $xoopsDB->prefix('xoonips_search_cache_metadata');
    $meta_table = $xoopsDB->prefix('xoonips_oaipmh_metadata');
    $repo_table = $xoopsDB->prefix('xoonips_oaipmh_repositories');
    $basic_table = $xoopsDB->prefix('xoonips_item_basic');
    $title_table = $xoopsDB->prefix('xoonips_item_title');
    $keyword_table = $xoopsDB->prefix('xoonips_item_keyword');
    $file_table = $xoopsDB->prefix('xoonips_file');
    $xlink_table = $xoopsDB->prefix('xoonips_index_item_link');
    $index_table = $xoopsDB->prefix('xoonips_index');
    $glink_table = $xoopsDB->prefix('xoonips_groups_users_link');
    $search_text_table = $xoopsDB->prefix('xoonips_search_text');
    $user_table = $xoopsDB->prefix('users');
    $event_log_table = $xoopsDB->prefix('xoonips_event_log');
    // search_cache_idがあるなら、search_cacheから取得.
    if ($search_cache_id) {
        $search_cache_id = (int) $search_cache_id;
        $sql = "select unix_timestamp(timestamp) from {$cache_table} where search_cache_id={$search_cache_id} and sess_id='" . session_id() . "'";
        $result = $xoopsDB->query($sql);
        if ($xoopsDB->getRowsNum($result) == 0) {
            //todo: session timeoutのためにsearch_cacheから消されたのかもしれない(普通は起こらないのだが)。どのようなメッセージを出すべきか?
            $msg = _MD_XOONIPS_ITEM_SEARCH_ERROR;
            return false;
            // bad search_cache_id
        }
        list($timestamp) = $xoopsDB->fetchRow($result);
        $event_type_ids = array(ETID_INSERT_ITEM, ETID_UPDATE_ITEM, ETID_DELETE_ITEM, ETID_DELETE_GROUP, ETID_INSERT_GROUP_MEMBER, ETID_DELETE_GROUP_MEMBER, ETID_DELETE_INDEX, ETID_CERTIFY_ITEM, ETID_REJECT_ITEM, ETID_TRANSFER_ITEM);
        $sql = "select count(*) from {$event_log_table} where event_type_id in (" . implode(',', $event_type_ids) . ") and timestamp >= {$timestamp}";
        $result = $xoopsDB->query($sql);
        if ($result == false) {
            $msg = _MD_XOONIPS_ITEM_SEARCH_ERROR;
            return false;
        }
        list($count) = $xoopsDB->fetchRow($result);
        if ($count == 0) {
            if ($search_tab == 'metadata') {
                $sql = "select identifier from {$cache_meta_table} where search_cache_id={$search_cache_id}";
            } else {
                if ($search_tab == 'file') {
                    $sql = "select tf.item_id    from {$cache_file_table} as tcf\n                  left join {$file_table} as tf on tcf.file_id = tf.file_id\n                  left join {$basic_table} as tb on tb.item_id = tf.item_id\n                  left join {$search_text_table} as tst on tf.file_id=tst.file_id\n                  where search_cache_id={$search_cache_id} and tb.item_id is not null and tf.file_id is not null and tf.is_deleted=0";
                } else {
                    $sql = "select tci.item_id    from {$cache_item_table} as tci\n                  left join {$basic_table} as tb on tb.item_id = tci.item_id\n                  where search_cache_id={$search_cache_id} and tb.item_id is not null";
                }
            }
            $result = $xoopsDB->query($sql);
            while (list($iid) = $xoopsDB->fetchRow($result)) {
                $iids[] = $iid;
            }
            return true;
        }
    }
    $cachable = $op == 'quicksearch' || $op == 'advancedsearch' || $op == 'itemtypesearch' || $op == 'itemsubtypesearch';
    $search_cache_id = 0;
    if ($cachable) {
        // search_cache_idを発行する
        $sql = "insert into {$cache_table} ( sess_id ) values ( '" . session_id() . "' )";
        $result = $xoopsDB->queryF($sql);
        if ($result == false) {
            $msg = _MD_XOONIPS_ITEM_SEARCH_ERROR;
            return false;
        }
        $search_cache_id = $xoopsDB->getInsertId();
    }
    $itemtypes = array();
    $itemtype_names = array();
    $tmp = array();
    if (($res = xnp_get_item_types($tmp)) != RES_OK) {
        $msg = _MD_XOONIPS_ITEM_SEARCH_ERROR;
        return false;
    } else {
        foreach ($tmp as $i) {
            $itemtypes[$i['item_type_id']] = $i;
            $itemtype_names[$i['name']] = $i;
        }
    }
    $join1 = " left join {$xlink_table} on {$xlink_table}.item_id  = {$basic_table}.item_id " . " left join {$index_table} on {$index_table}.index_id = {$xlink_table}.index_id " . " left join {$glink_table} on {$glink_table}.gid      = {$index_table}.gid " . " left join {$user_table}  on {$user_table}.uid       = {$basic_table}.uid ";
    $iids = array();
    if ($private_flag) {
        // operation to add item into index. search for only the user's item.
        $privilege = "( {$index_table}.open_level = " . OL_PRIVATE . " and {$index_table}.uid={$uid} )";
    } else {
        // search for readable items.
        $xmember_handler =& xoonips_gethandler('xoonips', 'member');
        if ($xmember_handler->isAdmin($uid) || xnp_is_moderator($xnpsid, $uid)) {
            $privilege = " 1 ";
        } else {
            $privilege = " ({$index_table}.open_level = " . OL_PUBLIC . " or \n" . "  {$index_table}.open_level = " . OL_PRIVATE . " and {$index_table}.uid={$uid} or \n" . "  {$index_table}.open_level = " . OL_GROUP_ONLY . " and {$glink_table}.uid={$uid} ) \n";
        }
    }
    if ($op == 'advancedsearch' || $op == 'itemsubtypesearch') {
        // advanced では、結果をタブ表示しない。$search_tab を無視する。
        // advanced では、fileがヒットした場合でも search_cache_fileではなくsearch_cache_itemのほうに書く
        $formdata =& xoonips_getutility('formdata');
        foreach ($itemtypes as $itemtype_id => $itemtype) {
            $wheres = array(' 0 ');
            $module_name = $itemtype['name'];
            if ($formdata->getValue('post', $module_name, 'n', false)) {
                include_once XOOPS_ROOT_PATH . '/modules/' . $itemtype['viewphp'];
                $f = $module_name . 'GetAdvancedSearchQuery';
                $table = $xoopsDB->prefix("{$module_name}_item_detail");
                $key_name = "{$table}." . substr($module_name, 3) . '_id';
                // xnppaper -> paper_id
                $where = "";
                $join = "";
                $f($where, $join);
                // require retrieve additional query string to item type module
                if ($where != "") {
                    $sql = "select {$basic_table}.item_id, {$search_cache_id} from {$basic_table} " . $join1 . " left join {$file_table}    on {$file_table}.item_id    = {$basic_table}.item_id " . " left join {$title_table}   on {$title_table}.item_id   = {$basic_table}.item_id " . " left join {$keyword_table} on {$keyword_table}.item_id = {$basic_table}.item_id " . " left join {$table} on {$key_name} = {$basic_table}.item_id " . " left join {$search_text_table} on {$search_text_table}.file_id    = {$file_table}.file_id " . $join . " where  {$key_name} is not NULL and ( {$where} ) and {$privilege} \n" . " group by {$basic_table}.item_id  \n";
                    if ($cachable) {
                        // write to cache at once
                        $result = $xoopsDB->queryF("insert ignore into {$cache_item_table} ( item_id, search_cache_id ) " . $sql);
                        $sql = "select item_id from {$cache_item_table} where search_cache_id = {$search_cache_id}";
                    }
                    $result = $xoopsDB->query($sql);
                    if ($result == false) {
                        $msg = _MD_XOONIPS_ITEM_SEARCH_ERROR;
                        xoonips_error($xoopsDB->error() . " at " . __LINE__ . " in " . __FILE__);
                        return false;
                    }
                    while (list($iid) = $xoopsDB->fetchRow($result)) {
                        $iids[] = $iid;
                    }
                }
            }
        }
    } else {
        if ($op == 'itemtypesearch') {
            // top画面から。$search_itemtypeに一致するアイテムを列挙
            $itemtype_id = $itemtype_names[$search_itemtype]['item_type_id'];
            $sql = "select {$basic_table}.item_id, {$search_cache_id} from {$basic_table} \n" . $join1 . " where {$privilege} and {$basic_table}.item_type_id={$itemtype_id} \n" . " group by {$basic_table}.item_id  ";
            // inserting results to cache
            $result = $xoopsDB->queryF("insert ignore into {$cache_item_table} ( item_id, search_cache_id ) " . $sql);
            $sql = "select item_id from {$cache_item_table} where search_cache_id = {$search_cache_id}";
            $result = $xoopsDB->query($sql);
            if ($result == false) {
                $msg = _MD_XOONIPS_ITEM_SEARCH_ERROR;
                return false;
            }
            while (list($iid) = $xoopsDB->fetchRow($result)) {
                $iids[] = $iid;
            }
        } else {
            if ($op == 'quicksearch' && trim($keyword) != '') {
                $search_var[] = 'keyword';
                $search_var[] = 'search_itemtype';
                list($elements, $keywords, $errorMessage) = xnpSplitKeywords2($keyword);
                $keywordsLen = count($keywords);
                if ($errorMessage) {
                    $msg = $errorMessage;
                    return false;
                }
                if ($search_itemtype == 'basic') {
                    // search titles and keywords
                    $wheres_title_keyword = xnpGetKeywordsQueries(array($title_table . '.title', $keyword_table . '.keyword'), $keywords);
                    $where = " {$basic_table}.item_type_id != " . ITID_INDEX . " and  " . xnpUnsplitKeywords2($elements, $wheres_title_keyword);
                    $sql = "select {$basic_table}.item_id, {$search_cache_id} from {$basic_table} \n" . $join1 . " left join {$title_table}   on {$basic_table}.item_id = {$title_table}.item_id " . " left join {$keyword_table} on {$basic_table}.item_id = {$keyword_table}.item_id " . " where {$where} and {$privilege} \n" . " group by {$basic_table}.item_id  \n";
                    // inserting results to cache
                    $result = $xoopsDB->queryF("insert ignore into {$cache_item_table} ( item_id, search_cache_id ) " . $sql);
                    $sql = "select item_id from {$cache_item_table} where search_cache_id = {$search_cache_id}";
                    $result = $xoopsDB->query($sql);
                    if ($result == false) {
                        $msg = _MD_XOONIPS_ITEM_SEARCH_ERROR;
                        return false;
                    }
                    while (list($iid) = $xoopsDB->fetchRow($result)) {
                        $iids[] = $iid;
                    }
                }
                if ($search_itemtype == 'metadata' || $search_itemtype == 'all') {
                    // if 'metadata' then set result of search to cache and $iids
                    // if 'all' then write to cache
                    $searchutil =& xoonips_getutility('search');
                    $encoding = mb_detect_encoding($keyword);
                    $fulltext_criteria =& $searchutil->getFulltextSearchCriteria('search_text', $keyword, $encoding);
                    $sql = "select identifier, {$search_cache_id}\n             from {$meta_table} as data, {$repo_table} as repo\n             where repo.enabled=1 AND repo.deleted!=1 AND repo.repository_id=data.repository_id\n              AND " . $fulltext_criteria->render() . " order by identifier, data.repository_id";
                    // inserting results to cache
                    $result = $xoopsDB->queryF("insert into {$cache_meta_table} ( identifier, search_cache_id ) " . $sql);
                    $sql = "select item_id from {$cache_item_table} where search_cache_id = {$search_cache_id}";
                    $result = $xoopsDB->query($sql);
                    if ($result == false) {
                        $msg = _MD_XOONIPS_ITEM_SEARCH_ERROR;
                        return false;
                    }
                    while (list($iid) = $xoopsDB->fetchRow($result)) {
                        $iids[] = $iid;
                    }
                }
                if (isset($itemtype_names[$search_itemtype]) || $search_itemtype == 'all') {
                    /* where_condition[item_type] = "item_type_id=$itemtype_id and " ( query that combines 'wheres2' and 'and or ( )' ).
                          wheres2[keyword] = ( where_basic[keyword] or where_detail[keyword] )
                       */
                    // search_itemtype == (itemtype)の場合もファイル内部を検索する必要がある.
                    // ここでファイル検索を行って、結果を xoonips_search_cache_file に書く
                    $wheres_basic = xnpGetKeywordsQueries(array($title_table . '.title', $keyword_table . '.keyword', $basic_table . '.description', $basic_table . '.doi', $user_table . '.uname', $user_table . '.name'), $keywords);
                    foreach ($itemtypes as $itemtype_id => $itemtype) {
                        if ($itemtype['item_type_id'] == ITID_INDEX) {
                            continue;
                        }
                        $module_name = $itemtype['name'];
                        //echo "$search_itemtype / $module_name <br />\n";
                        if ($search_itemtype == $module_name || $search_itemtype == 'all') {
                            $itemtype_id = $itemtype['item_type_id'];
                            if ($file_or_item_metadata == 'all' || $file_or_item_metadata == 'item_metadata') {
                                include_once XOOPS_ROOT_PATH . '/modules/' . $itemtype['viewphp'];
                                $f = $module_name . 'GetDetailInformationQuickSearchQuery';
                                if (!function_exists($f)) {
                                    continue;
                                }
                                $table = $xoopsDB->prefix("{$module_name}_item_detail");
                                $wheres_detail = array();
                                $f($wheres_detail, $join, $keywords);
                                $wheres2 = array();
                                for ($i = 0; $i < $keywordsLen; $i++) {
                                    // search by item_id
                                    $where3 = sprintf(' OR `%s`.`item_id`=%s', $basic_table, $xoopsDB->quoteString($keywords[$i]));
                                    if (empty($wheres_detail[$i])) {
                                        $wheres_detail[$i] = '0';
                                    }
                                    $wheres2[] = $wheres_basic[$i] . ' or ' . $wheres_detail[$i] . $where3;
                                }
                                $where = " {$basic_table}.item_type_id={$itemtype_id} and " . xnpUnsplitKeywords2($elements, $wheres2);
                                $key_name = "{$table}." . substr($module_name, 3) . '_id';
                                // xnppaper -> paper_id
                                $sql = "select {$basic_table}.item_id, {$search_cache_id} from {$basic_table} " . $join1 . " left join {$file_table}  on {$file_table}.item_id   = {$basic_table}.item_id " . " left join {$title_table}   on {$basic_table}.item_id = {$title_table}.item_id " . " left join {$keyword_table} on {$basic_table}.item_id = {$keyword_table}.item_id " . " left join {$table} on {$key_name} = {$basic_table}.item_id " . $join . " where {$where} and {$privilege} \n" . " group by {$basic_table}.item_id  \n";
                                $result = $xoopsDB->queryF("insert ignore into {$cache_item_table} ( item_id, search_cache_id ) " . $sql);
                                if ($result == false) {
                                    $msg = _MD_XOONIPS_ITEM_SEARCH_ERROR;
                                    return false;
                                }
                            }
                            if ($file_or_item_metadata == 'all' || $file_or_item_metadata == 'file') {
                                $searchutil =& xoonips_getutility('search');
                                $encoding = mb_detect_encoding($keyword);
                                $fulltext_criteria =& $searchutil->getFulltextSearchCriteria('search_text', $keyword, $encoding);
                                // search inside files
                                $sql = "insert ignore into {$cache_file_table} ( file_id, search_cache_id )\n                          select {$file_table}.file_id, {$search_cache_id} from {$file_table}\n                          left join {$basic_table} on {$file_table}.item_id={$basic_table}.item_id\n                          left join {$search_text_table} on {$file_table}.file_id = {$search_text_table}.file_id\n                          where item_type_id={$itemtype_id} and " . $fulltext_criteria->render() . " and {$file_table}.is_deleted=0";
                                // write to cache at once
                                $result = $xoopsDB->queryF($sql);
                                if ($result == false) {
                                    $msg = _MD_XOONIPS_ITEM_SEARCH_ERROR;
                                    return false;
                                }
                            }
                        }
                    }
                    switch ($search_tab) {
                        case 'metadata':
                            $result = $xoopsDB->query("select item_id from {$cache_meta_table} where search_cache_id={$search_cache_id}");
                            break;
                        case 'file':
                            $result = $xoopsDB->query("select item_id from {$cache_file_table} where search_cache_id={$search_cache_id}");
                            break;
                        case 'item':
                        default:
                            $result = $xoopsDB->query("select item_id from {$cache_item_table} where search_cache_id={$search_cache_id}");
                            break;
                    }
                    while (list($iid) = $xoopsDB->fetchRow($result)) {
                        $iids[] = $iid;
                    }
                }
            }
        }
    }
    return true;
}