/**
 * 
 * インデックスからアイテムを削除する
 * 
 * @param sid セッションID
 * @param xid 処理対象のインデックスのID
 * @param iid インデックスから削除したいアイテムのID
 * @return RES_OK
 * @return RES_DB_QUERY_ERROR
 * @return RES_NO_SUCH_SESSION
 * @return RES_NO_WRITE_ACCESS_RIGHT
 * @return RES_ERROR
 * 
 */
function xnp_unregister_item($sid, $xid, $iid)
{
    global $xoopsDB;
    $xid = (int) $xid;
    $iid = (int) $iid;
    if (!xnp_is_valid_session_id($sid)) {
        return RES_NO_SUCH_SESSION;
    }
    $ret = RES_ERROR;
    if (!xnp_get_index_permission($sid, $xid, OP_UNREGISTER)) {
        return RES_NO_WRITE_ACCESS_RIGHT;
    }
    // unregister the item.
    $sql = "DELETE FROM " . $xoopsDB->prefix("xoonips_index_item_link") . " WHERE index_id={$xid} AND item_id={$iid}";
    if ($result = $xoopsDB->queryF($sql)) {
        // update last update date
        $sql = "UPDATE " . $xoopsDB->prefix("xoonips_item_basic") . " SET last_update_date=UNIX_TIMESTAMP(NOW())" . " WHERE item_id={$xid}";
        if ($result = $xoopsDB->queryF($sql)) {
            _xnpal_setLastErrorString("");
            $ret = RES_OK;
        } else {
            _xnpal_setLastErrorString("error can't update last_updated_date in xnp_unregister_item" . " at " . __LINE__ . " in " . __FILE__ . "\n" . xnp_get_last_error_string());
            $ret = RES_DB_QUERY_ERROR;
        }
    }
    if ($ret == RES_OK) {
        $ret = insertMetadataEventAuto($iid);
        if ($ret == RES_OK) {
            _xnpal_setLastErrorString("");
        }
    }
    return $ret;
}
 /**
  * 
  * @param $item reference of XooNIpsImportItem object
  */
 function import(&$item)
 {
     global $xoopsDB, $xoopsUser;
     if (!$xoopsUser) {
         return;
     }
     $indexes =& $this->_create_index_item_links_from_index_ids($item->getImportIndexId(), $item->getCertifyAutoFlag(), $xoopsUser);
     $item->setVar('indexes', $indexes);
     if ($item->getUpdateFlag()) {
         // single field(basic) _isNew=false
         $basic =& $item->getVar('basic');
         $basic->set('item_id', $item->getUpdateItemId());
         $basic->unsetNew();
         $basic->setDirty();
         // multiple field(title, keyword, ...) _isNew=true
         foreach (array('titles', 'keywords', 'related_tos', 'indexes') as $key) {
             $var =& $item->getVar($key);
             assert(is_array($var));
             foreach (array_keys($var) as $k) {
                 $var[$k]->setNew();
                 $var[$k]->setDirty();
             }
         }
         // get changelog from DB to keep old changelog
         $changelog_handler =& xoonips_getormhandler('xoonips', 'changelog');
         $criteria = new Criteria('item_id', $item->getUpdateItemId());
         $changelogs =& $changelog_handler->getObjects($criteria);
         $item->setVar('changelogs', $changelogs);
         if ($this->insert($item)) {
             $event_handler =& xoonips_getormhandler('xoonips', 'event_log');
             $event_handler->recordUpdateItemEvent($item->getUpdateItemId());
         } else {
             $item->setErrors(E_XOONIPS_DB_QUERY, 'DB query error in updating');
         }
     } else {
         //
         if ($this->insert($item)) {
             $basic =& $item->getVar('basic');
             $event_handler =& xoonips_getormhandler('xoonips', 'event_log');
             $event_handler->recordInsertItemEvent($basic->get('item_id'));
         } else {
             $item->setErrors(E_XOONIPS_DB_QUERY, 'DB query error in updating');
         }
     }
     if (0 == count($item->getErrors())) {
         $event_handler =& xoonips_getormhandler('xoonips', 'event_log');
         foreach ($item->getVar('indexes') as $links) {
             $index_handler =& xoonips_getormhandler('xoonips', 'index');
             $index =& $index_handler->get($links->get('index_id'));
             if (!$index || OL_PUBLIC != $index->get('open_level')) {
                 continue;
             }
             $basic =& $item->getVar('basic');
             $event_handler->recordRequestCertifyItemEvent($basic->get('item_id'), $index->get('index_id'));
             if ($item->getCertifyAutoFlag()) {
                 $event_handler->recordCertifyItemEvent($basic->get('item_id'), $index->get('index_id'));
             }
         }
     }
     $basic =& $item->getVar('basic');
     insertMetadataEventAuto($basic->get('item_id'), $basic->isNew());
 }