/**
 *
 * register item (Basic Information)
 * this function required more than Platform user privileges
 *
 * @param sid session ID
 * @param item registration item information
 * @param itemid reference of registered item id
 * @param direct true if set last_update_date, creation_date of item parameter
 * @return RES_OK
 * @return RES_ERROR
 * @return RES_NO_SUCH_SESSION
 * @return RES_DB_QUERY_ERROR
 * @return RES_NO_WRITE_ACCESS_RIGHT
 *
 */
function _xnpal_insertItemInternal($sid, $item, &$itemid, $direct)
{
    global $xoopsDB;
    if (!xnp_is_valid_session_id($sid)) {
        return RES_NO_SUCH_SESSION;
    }
    if (!_xnpal_isActivatedBySession($sid)) {
        return RES_NO_WRITE_ACCESS_RIGHT;
    }
    $ret = RES_ERROR;
    $sql = "INSERT INTO " . $xoopsDB->prefix("xoonips_item_basic") . " (item_type_id, description, doi, uid, creation_date, last_update_date, publication_year, publication_month, publication_mday, lang) VALUES (" . implode(", ", array(isset($item['item_type_id']) ? (int) $item['item_type_id'] : 0, "'" . addSlashes(isset($item['description']) ? $item['description'] : "") . "'", "'" . addSlashes(isset($item['doi']) ? $item['doi'] : "") . "'", $item['uid'], $direct ? $item['creation_date'] : 'UNIX_TIMESTAMP(NOW())', $direct ? $item['last_update_date'] : 'UNIX_TIMESTAMP(NOW())', isset($item['publication_year']) ? (int) $item['publication_year'] : 0, isset($item['publication_month']) ? (int) $item['publication_month'] : 0, isset($item['publication_mday']) ? (int) $item['publication_mday'] : 0, "'" . addSlashes(isset($item['lang']) ? $item['lang'] : "") . "'")) . ")";
    $result = $xoopsDB->queryF($sql);
    if ($result) {
        // get inserted item id
        $itemid = $xoopsDB->getInsertID();
        //insert titles and keywords
        if (isset($item['titles'])) {
            if (!is_array($item['titles'])) {
                $item['titles'] = array($item['titles']);
            }
            if (count($item['titles']) > 0) {
                if (!_xnpal_updateTitles(__FUNCTION__, $itemid, $item['titles'])) {
                    _xnpal_setLastErrorString("can't insert title in " . __FUNCTION__ . " at " . __LINE__ . " in " . __FILE__ . "\n" . xnp_get_last_error_string());
                    xnp_delete_item($sid, $itemid);
                    return RES_DB_QUERY_ERROR;
                }
            }
        }
        if (isset($item['keywords'])) {
            if (!is_array($item['keywords'])) {
                $item['keywords'] = array($item['keywords']);
            }
            if (count($item['keywords']) > 0) {
                if (!_xnpal_updateKeywords(__FUNCTION__, $itemid, $item['keywords'])) {
                    _xnpal_setLastErrorString("can't insert keyword in " . __FUNCTION__ . " at " . __LINE__ . " in " . __FILE__ . "\n" . xnp_get_last_error_string());
                    xnp_delete_item($sid, $itemid);
                    return RES_DB_QUERY_ERROR;
                }
            }
        }
        if ($item['item_type_id'] == ITID_INDEX) {
            //nothing to do if index.
            _xnpal_setLastErrorString("");
            $ret = RES_OK;
        } else {
            //insert into private index
            $sql = "SELECT private_index_id FROM " . $xoopsDB->prefix("xoonips_users") . " WHERE uid=" . $item['uid'];
            if ($result = $xoopsDB->query($sql)) {
                list($private_xid) = $xoopsDB->fetchRow($result);
                $ret = xnp_register_item($sid, $private_xid, $itemid);
            } else {
                _xnpal_setLastErrorString("error can't retrieve private_index_id in xnp_insert_item " . " at " . __LINE__ . " in " . __FILE__ . "\n" . xnp_get_last_error_string());
                xnp_delete_item($sid, $itemid);
                return RES_ERROR;
            }
        }
    } else {
        _xnpal_setLastErrorString("error can't insert item in xnp_insert_item {$sql} " . $xoopsDB->error() . " at " . __LINE__ . " in " . __FILE__ . "\n" . xnp_get_last_error_string());
        $ret = RES_DB_QUERY_ERROR;
    }
    return $ret;
}
function xnpUpdateIndex($item_id)
{
    //1. get $_POST['xoonipsCheckedXID'].
    //2. get registered index (before change index) using item_id.
    //3. function 'unregisterItem' is executed for index (2-(1 and 2)) deleted by change.
    //4. function 'registerItem' is executed for index (1-(1 and 2)) added by change.
    $xnpsid = $_SESSION['XNPSID'];
    $formdata =& xoonips_getutility('formdata');
    $xoonipsCheckedXID = $formdata->getValue('post', 'xoonipsCheckedXID', 's', false);
    if ($xoonipsCheckedXID === NULL) {
        return true;
    }
    $xids_new = explode(',', $xoonipsCheckedXID);
    $item = array();
    $xids_now = array();
    if (($result = xnp_get_item($xnpsid, $item_id, $item)) == RES_OK) {
        //retrieve index id if item exists
        if (xnp_get_index_id_by_item_id($xnpsid, $item_id, $xids_now) != RES_OK) {
            return false;
        }
    }
    $intersect = array_intersect($xids_new, $xids_now);
    $del = array_diff($xids_now, $intersect);
    // index id shuld be removed
    $add = array_diff($xids_new, $intersect);
    // index id shuld be inserted
    foreach ($del as $i) {
        xnp_unregister_item($xnpsid, $i, $item_id);
    }
    foreach ($add as $i) {
        xnp_register_item($xnpsid, $i, $item_id);
    }
    return true;
}