/**
 *
 * アイテム情報取得.
 *
 * @param sid セッションID
 * @param iids 取得したいアイテムのIDの配列
 * @param criteria 結果の範囲指定,ソート条件指定
 * @param items 検索結果のを書き込む配列
 * @return RES_OK
 * @return RES_DB_NOT_INITIALIZED
 * @return RES_NO_SUCH_SESSION
 * @return RES_DB_QUERY_ERROR
 *
 */
function xnp_get_items($sess_id, $iids, $criteria, &$items)
{
    global $xoopsDB;
    $items = array();
    if (!xnp_is_valid_session_id($sess_id)) {
        return RES_NO_SUCH_SESSION;
    }
    $ret = _xnpal_sessionID2UID($sess_id, $uid);
    if ($ret != RES_OK) {
        return $ret;
    }
    $items = array();
    if (!isset($iids) || count($iids) == 0) {
        return RES_OK;
    }
    $sql = "SELECT DISTINCT ti.item_id as item_id, item_type_id, tt.title as title, description, doi, ti.uid as uid, creation_date, last_update_date, publication_year, publication_month, publication_mday, lang ";
    $sql .= " FROM ";
    $sql .= $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_item_title") . " AS tt ON tt.item_id=ti.item_id";
    $sql .= " LEFT JOIN " . $xoopsDB->prefix("xoonips_groups_users_link") . " as tgulink ON tx.gid=tgulink.gid";
    $sql .= " WHERE tlink.item_id IN ( " . _xnpal_getCsvStr($iids) . " )";
    $sql .= " AND title_id=" . DEFAULT_ORDER_TITLE_OFFSET;
    $sql .= xnp_criteria2str($criteria);
    $result = $xoopsDB->query($sql);
    if (!$result) {
        _xnpal_setLastErrorString("error in xnp_get_items " . $xoopsDB->error());
        return RES_DB_QUERY_ERROR;
    }
    $items_buf = array();
    $ordered_ids = array();
    //array of sorted item_id(s) to sort $items_buf in the end of this function
    $item_compo_handler =& xoonips_getormcompohandler('xoonips', 'item');
    while ($row = $xoopsDB->fetchArray($result)) {
        if (!$item_compo_handler->getPerm($row['item_id'], $uid, 'read')) {
            continue;
        }
        $items_buf[$row['item_id']] = $row;
        $items_buf[$row['item_id']]['titles'] = array();
        $items_buf[$row['item_id']]['keywords'] = array();
        $ordered_ids[] = $row['item_id'];
    }
    //get titles of selected item
    if (count($items_buf) > 0) {
        $sql = "SELECT item_id, title FROM " . $xoopsDB->prefix("xoonips_item_title") . " WHERE item_id IN ( " . implode(",", array_keys($items_buf)) . " ) ORDER BY item_id ASC, title_id ASC";
        $result = $xoopsDB->query($sql);
        if (!$result) {
            _xnpal_setLastErrorString("error in xnp_get_items " . $xoopsDB->error() . " sql={$sql}" . " at " . __LINE__ . " in " . __FILE__ . "\n" . xnp_get_last_error_string());
            return RES_DB_QUERY_ERROR;
        }
        while ($row = $xoopsDB->fetchArray($result)) {
            $items_buf[$row['item_id']]['titles'][] = $row['title'];
        }
        //get keywords of selected item
        $sql = "SELECT item_id, keyword FROM " . $xoopsDB->prefix("xoonips_item_keyword") . " WHERE item_id IN ( " . implode(",", array_keys($items_buf)) . " ) ORDER BY item_id ASC, keyword_id ASC";
        $result = $xoopsDB->query($sql);
        if (!$result) {
            _xnpal_setLastErrorString("error in xnp_get_items " . $xoopsDB->error() . " sql={$sql}" . " at " . __LINE__ . " in " . __FILE__ . "\n" . xnp_get_last_error_string());
            return RES_DB_QUERY_ERROR;
        }
        while ($row = $xoopsDB->fetchArray($result)) {
            $items_buf[$row['item_id']]['keywords'][] = $row['keyword'];
        }
    }
    // convert the associative array(index_buf) to the array(indexes) (keep order specified by criteriaString)
    foreach ($ordered_ids as $id) {
        $items[] = $items_buf[$id];
    }
    _xnpal_setLastErrorString("");
    return RES_OK;
}
/**
 * get an array of item id(s) that is in a binder
 *
 * @param sess_id string session id
 * @param binder_id integer binder id
 * @param cri
 * @param iids array of item ids that is in a binder.
 * @return RES_OK
 * @return RES_DB_NOT_INITIALIZED
 * @return RES_NO_SUCH_SESSION
 * @return RES_DB_QUERY_ERROR
 * @return RES_ERROR
 *
 */
function xnp_get_item_id_by_binder_id($sess_id, $binder_id, $cri, &$iids)
{
    $binder_id = (int) $binder_id;
    $iids = array();
    global $xoopsDB;
    if (!xnp_is_valid_session_id($sess_id)) {
        return RES_NO_SUCH_SESSION;
    }
    $sql = 'SELECT t1.item_id FROM ' . $xoopsDB->prefix('xoonips_item_basic') . ' as t1, ' . $xoopsDB->prefix('xnpbinder_binder_item_link') . ' as t2, ' . $xoopsDB->prefix('xoonips_item_title') . ' as t3 ';
    $sql .= ' WHERE t1.item_id = t2.item_id';
    $sql .= " AND t2.binder_id={$binder_id}";
    $sql .= ' AND t3.title_id=' . DEFAULT_ORDER_TITLE_OFFSET . ' AND t3.item_id=t1.item_id';
    $sql .= xnp_criteria2str($cri);
    $result = $xoopsDB->query($sql);
    if (!$result) {
        return RES_DB_QUERY_ERROR;
    }
    while (list($iid) = $xoopsDB->fetchRow($result)) {
        $iids[] = $iid;
    }
    return RES_OK;
}