/**
  * update item status table.
  *
  * @access public
  * @param int $item_id Item ID to update item status
  * @return bool
  *
  */
 function updateItemStatus($item_id)
 {
     $index_handler =& xoonips_getormhandler('xoonips', 'index');
     $item_status_handler =& xoonips_getormhandler('xoonips', 'item_status');
     $criteria1 = new CriteriaCompo();
     $criteria1->add(new Criteria('certify_state', CERTIFIED));
     $criteria1->add(new Criteria('open_level', OL_PUBLIC));
     $criteria1->add(new Criteria('item_id', intval($item_id), '=', 'txil'));
     $join = new XooNIpsJoinCriteria('xoonips_index_item_link', 'index_id', 'index_id', 'LEFT', 'txil');
     $join->cascade(new XooNIpsJoinCriteria('xoonips_item_status', 'item_id', 'item_id', 'LEFT', 'tis'), 'txil', true);
     $results =& $index_handler->getObjects($criteria1, false, 'txil.item_id, tis.is_deleted', true, $join);
     foreach ($results as $row) {
         if (is_null($row->getExtraVar('is_deleted'))) {
             $item_status =& $item_status_handler->create();
             $item_status->set('created_timestamp', time());
         } else {
             $item_status =& $item_status_handler->get($row->getExtraVar('item_id'));
             $item_status->set('modified_timestamp', time());
         }
         if (!$item_status) {
             // item_status not found
             return false;
         }
         // insert or update item_status
         $item_status->set('item_id', $row->getExtraVar('item_id'));
         $item_status->set('is_deleted', 0);
         if (!$item_status_handler->insert($item_status)) {
             return false;
         }
     }
     $item_status_handler =& xoonips_getormhandler('xoonips', 'item_status');
     $index_item_link_handler =& xoonips_getormhandler('xoonips', 'index_item_link');
     $criteria2 = new CriteriaCompo();
     $criteria2->add(new Criteria('is_deleted', 0));
     $criteria2->add(new Criteria('item_id', intval($item_id)));
     $rows =& $item_status_handler->getObjects($criteria2);
     if (!$rows) {
         return false;
     }
     foreach ($rows as $row) {
         $criteria3 = new CriteriaCompo();
         $criteria3->add(new Criteria('certify_state', CERTIFIED));
         $criteria3->add(new Criteria('open_level', OL_PUBLIC));
         $criteria3->add(new Criteria('item_id', $row->get('item_id')));
         $join = new XooNIpsJoinCriteria('xoonips_index', 'index_id', 'index_id', 'LEFT', 'tx');
         $results =& $index_item_link_handler->getObjects($criteria3, false, 'count(*)', true, $join);
         if (!$results) {
             return false;
         }
         if ($results[0]->getExtraVar('count(*)') == 0) {
             $row->set('is_deleted', 1);
             $row->set('deleted_timestamp', time());
             $item_status_handler->insert($row);
         }
     }
     return true;
 }
function &positions_get_userlist()
{
    global $xoopsDB;
    $xusers_handler =& xoonips_getormhandler('xoonips', 'users');
    $tables['users'] = $xoopsDB->prefix('users');
    $tables['xusers'] = $xoopsDB->prefix('xoonips_users');
    $tables['positions'] = $xoopsDB->prefix('xoonips_positions');
    $join_criteria = new XooNIpsJoinCriteria('users', 'uid', 'uid');
    $join_criteria->cascade(new XooNIpsJoinCriteria('xoonips_positions', 'posi', 'posi_id'), 'xoonips_users');
    $criteria = new Criteria($tables['xusers'] . '.posi', '0', '>');
    $criteria->setSort(array($tables['positions'] . '.posi_order', $tables['xusers'] . '.user_order'));
    $fields = array();
    $fields[] = $tables['xusers'] . '.uid';
    $fields[] = $tables['xusers'] . '.user_order';
    $fields[] = $tables['users'] . '.name';
    $fields[] = $tables['users'] . '.uname';
    $fields[] = $tables['positions'] . '.posi_title';
    $xusers_objs =& $xusers_handler->getObjects($criteria, false, implode(',', $fields), false, $join_criteria);
    return $xusers_objs;
}
 function getRSSItems(&$items)
 {
     // check module_read permission
     $module_handler =& xoops_gethandler('module');
     $module =& $module_handler->getByDirname('xoonips');
     $gperm_handler =& xoops_gethandler('groupperm');
     $can_read = $gperm_handler->checkRight('module_read', $module->getVar('mid'), XOOPS_GROUP_ANONYMOUS);
     if (!$can_read) {
         return;
     }
     // get all published items
     $limit = 10;
     $category = $module->getVar('name');
     $ib_handler =& xoonips_getormhandler('xoonips', 'item_basic');
     $criteria = new CriteriaCompo();
     $criteria->add(new Criteria('event_type_id', ETID_CERTIFY_ITEM));
     $criteria->setGroupBy('ev.item_id');
     $criteria->setSort('ev.timestamp');
     $criteria->setOrder('DESC');
     $criteria->setLimit($limit);
     $fields = '*,MAX(ev.timestamp) AS pubdate';
     $join = new XooNIpsJoinCriteria('xoonips_event_log', 'item_id', 'item_id', 'INNER', 'ev');
     $criteria->add(new Criteria('iil.certify_state', CERTIFIED));
     $join->cascade(new XooNIpsJoinCriteria('xoonips_index_item_link', 'item_id', 'item_id', 'INNER', 'iil'));
     $criteria->add(new Criteria('idx.open_level', OL_PUBLIC));
     $join->cascade(new XooNIpsJoinCriteria('xoonips_index', 'index_id', 'index_id', 'INNER', 'idx'), 'iil', true);
     $res =& $ib_handler->open($criteria, $fields, false, $join);
     while ($obj =& $ib_handler->getNext($res)) {
         $item_id = intval($obj->get('item_id'));
         $doi = $obj->get('doi');
         $title = $this->_getItemTitle($item_id, 's');
         $url = $this->_getItemUrl($item_id, $doi, 's');
         $description = $obj->get('description');
         $items[] = array('pubdate' => $obj->getExtraVar('pubdate'), 'title' => $title, 'link' => $url, 'guid' => $url, 'description' => $description, 'category' => $category);
     }
     $ib_handler->close($res);
 }
function xoonips_remove_duplicated_private_item_ids()
{
    $ixil_handler =& xoonips_getormhandler('xoonips', 'index_item_link');
    $join = new XooNIpsJoinCriteria('xoonips_index', 'index_id', 'index_id', 'INNER', 'idx');
    $join->cascade(new XooNIpsJoinCriteria('xoonips_item_basic', 'item_id', 'item_id', 'INNER', 'ib'));
    $criteria = new CriteriaCompo();
    $criteria->add(new Criteria('open_level', OL_PRIVATE, '=', 'idx'));
    $criteria->add(new Criteria('ib.uid', '(idx.uid)', 'NOT IN'));
    $ixil_objs =& $ixil_handler->getObjects($criteria, false, '', false, $join);
    $ixil_ids = array();
    foreach ($ixil_objs as $ixil_obj) {
        $ixil_ids[] = $ixil_obj->get('index_item_link_id');
    }
    if (!empty($ixil_ids)) {
        $criteria = new Criteria('index_item_link_id', '(' . implode(',', $ixil_ids) . ')', 'IN');
        $ixil_handler->deleteAll($criteria);
    }
}
/**
 *
 * get number of user's items of specified open level(ignore certify state)
 * @param int $uid user id
 * @param int $open_leven OL_PUBLIC|OL_GROUP_ONLY|OL_PRIVATE
 * @return int
 */
function get_number_of_item_by_open_level($uid, $open_level)
{
    $criteria = new CriteriaCompo();
    $criteria->add(new Criteria('item_type_id', ITID_INDEX, '!='));
    $criteria->add(new Criteria('open_level', intval($open_level)));
    $criteria->add(new Criteria('uid', intval($uid), '=', 'basic'));
    $join = new XooNIpsJoinCriteria('xoonips_index', 'index_id', 'index_id');
    $join->cascade(new XooNIpsJoinCriteria('xoonips_item_basic', 'item_id', 'item_id', 'INNER', 'basic'));
    $index_item_link_handler =& xoonips_getormhandler('xoonips', 'index_item_link');
    return count($index_item_link_handler->getObjects($criteria, false, '', true, $join));
}
/**
 * return number of items of the file search cache
 * 
 * @param $search_cache_id integer cache id
 * @return integer number of items
 */
function xoonips_get_file_count_from_search_cache($search_cache_id)
{
    global $xoopsDB, $xoopsUser;
    $search_cache_file_handler =& xoonips_getormhandler('xoonips', 'search_cache_file');
    $join = new XooNIpsJoinCriteria('xoonips_file', 'file_id', 'file_id', 'INNER', 'tf');
    $join->cascade(new XooNIpsJoinCriteria('xoonips_item_basic', 'item_id', 'item_id', 'INNER', 'tb'), 'tf', true);
    $search_cache_file =& $search_cache_file_handler->getObjects(new Criteria('search_cache_id', $search_cache_id), false, 'tb.item_id', false, $join);
    if (count($search_cache_file) == 0) {
        return 0;
    }
    $c = 0;
    $item_handler =& xoonips_getormcompohandler('xoonips', 'item');
    foreach ($search_cache_file as $item) {
        if ($item_handler->getPerm($item->getExtraVar('item_id'), $xoopsUser ? $xoopsUser->getVar('uid') : UID_GUEST, 'read')) {
            $c++;
        }
    }
    return $c;
}
/**
 * get item_basic objects from database
 *
 * @access public
 * @param int $start number of skip results
 * @param int $limit number of results, zero means unlimit
 * @return array object instance
 */
function &xoonips_eventlog_get_items($start, $limit)
{
    global $xoopsDB;
    $tables['item_basic'] = $xoopsDB->prefix('xoonips_item_basic');
    $ib_handler =& xoonips_getormhandler('xoonips', 'item_basic');
    $criteria = new Criteria($tables['item_basic'] . '.item_type_id', ITID_INDEX, '>');
    $criteria->setSort($tables['item_basic'] . '.item_id');
    $criteria->setStart($start);
    if ($limit > 0) {
        $criteria->setLimit($limit);
    }
    $join_criteria = new XooNIpsJoinCriteria('xoonips_item_type', 'item_type_id', 'item_type_id', 'INNER');
    $join_criteria->cascade(new XooNIpsJoinCriteria('users', 'uid', 'uid', 'INNER'), 'xoonips_item_basic');
    $objs =& $ib_handler->getObjects($criteria, false, '', false, $join_criteria);
    return $objs;
}
function xoonips_ranking_show($is_arrival)
{
    global $xoopsUser;
    // load xoonips config handler
    $xconfig_handler =& xoonips_getormhandler('xoonips', 'config');
    // hide block if user is guest and public index viewing policy is 'platform'
    if (!is_object($xoopsUser)) {
        $target_user = $xconfig_handler->getValue('public_item_target_user');
        if ($target_user != 'all') {
            // 'platform'
            return false;
        }
    }
    $uid = is_object($xoopsUser) ? $xoopsUser->getVar('uid', 'n') : UID_GUEST;
    // hide block if user is invalid xoonips user
    $xsession_handler =& xoonips_getormhandler('xoonips', 'session');
    if (!$xsession_handler->validateUser($uid, false)) {
        return false;
    }
    // load utility class
    $textutil =& xoonips_getutility('text');
    $etc = '...';
    // decide maximum string length by block position
    if (defined('XOOPS_CUBE_LEGACY')) {
        // get xoonips module id
        $mydirname = basename(dirname(__DIR__));
        $module_handler =& xoops_gethandler('module');
        $module =& $module_handler->getByDirname($mydirname);
        $mid = $module->getVar('mid', 's');
        // get block array
        $block_arr =& XoopsBlock::getByModule($mid);
    } else {
        global $block_arr;
    }
    $myfunc = $is_arrival ? 'b_xoonips_ranking_new_show' : 'b_xoonips_ranking_show';
    $maxlen = 56;
    // default
    foreach ($block_arr as $b) {
        $func = $b->getVar('show_func', 'n');
        if ($func == $myfunc) {
            $side = $b->getVar('side', 'n');
            if ($side == XOOPS_SIDEBLOCK_LEFT || $side == XOOPS_SIDEBLOCK_RIGHT) {
                $maxlen = 16;
                break;
            } else {
                if ($side == XOOPS_CENTERBLOCK_LEFT || $side == XOOPS_CENTERBLOCK_RIGHT) {
                    $maxlen = 24;
                    break;
                }
            }
        }
    }
    // get configs
    $config_names = array('num_rows', 'visible', 'order');
    $new_str = $is_arrival ? 'new_' : '';
    foreach ($config_names as $name) {
        $config[$name] = $xconfig_handler->getValue('ranking_' . $new_str . $name);
    }
    $config['visible'] = explode(',', $config['visible']);
    $config['order'] = explode(',', $config['order']);
    // update rankings
    $ranking_handler =& xoonips_gethandler('xoonips', 'ranking');
    $ranking_handler->update();
    // get rankings from database
    // - set item permission criteria and join criteria
    $iperm_criteria = new CriteriaCompo();
    $iperm_criteria->add(new Criteria('title_id', 0, '=', 'tt'));
    $iperm_criteria->add(new Criteria('open_level', OL_PUBLIC, '=', 'tx'));
    $iperm_criteria->add(new Criteria('certify_state', CERTIFIED, '=', 'txil'));
    $iperm_join = new XooNIpsJoinCriteria('xoonips_item_basic', 'item_id', 'item_id', 'INNER', 'tb');
    $iperm_join->cascade(new XooNIpsJoinCriteria('xoonips_item_title', 'item_id', 'item_id', 'INNER', 'tt'));
    $iperm_join->cascade(new XooNIpsJoinCriteria('xoonips_index_item_link', 'item_id', 'item_id', 'INNER', 'txil'));
    $iperm_join->cascade(new XooNIpsJoinCriteria('xoonips_index', 'index_id', 'index_id', 'INNER', 'tx'), 'txil', true);
    // - set ranking number label
    $rank_tmp = explode(',', _MB_XOONIPS_RANKING_RANK_STR);
    $rank_str = array();
    for ($i = 0; $i < $config['num_rows']; $i++) {
        $rank_str[] = $i + 1 . $rank_tmp[min($i, count($rank_tmp) - 1)];
    }
    $block['rankings'] = array();
    if ($is_arrival) {
        // new arrival block
        // ranking new item
        if ($config['visible'][0]) {
            $table = 'ranking_new_item';
            $label = _MB_XOONIPS_RANKING_NEW_ITEM;
            $fields = 'tb.item_id, tt.title, DATE_FORMAT(timestamp,\'%m/%d\'), tb.doi';
            $criteria = $iperm_criteria;
            $criteria->setGroupby('tb.item_id');
            $criteria->setLimit($config['num_rows']);
            $criteria->setSort('timestamp');
            $criteria->setOrder('DESC');
            $join = $iperm_join;
            $handler =& xoonips_getormhandler('xoonips', $table);
            $res =& $handler->open($criteria, $fields, false, $join);
            $items = array();
            $i = 0;
            while ($obj =& $handler->getNext($res)) {
                $item_id = $obj->getVar('item_id', 'n');
                $title = $textutil->html_special_chars($obj->getExtraVar('title'));
                $title = $textutil->truncate($title, $maxlen, $etc);
                $count = $textutil->html_special_chars($obj->getExtraVar('DATE_FORMAT(timestamp,\'%m/%d\')'));
                $doi = $textutil->html_special_chars($obj->getExtraVar('doi'));
                $id = $doi == '' && XNP_CONFIG_DOI_FIELD_PARAM_NAME != '' ? 'item_id=' . $item_id : XNP_CONFIG_DOI_FIELD_PARAM_NAME . '=' . urlencode($doi);
                $url = XOOPS_URL . '/modules/xoonips/detail.php?' . $id;
                $items[] = array('title' => $title, 'url' => $url, 'num' => $count, 'rank_str' => $rank_str[$i]);
                $i++;
            }
            $handler->close($res);
            $block['rankings'][$config['order'][0]] = array('items' => $items, 'title' => $label);
            unset($items);
        }
        // ranking new group
        if ($config['visible'][1]) {
            $table = 'ranking_new_group';
            $label = _MB_XOONIPS_RANKING_NEW_GROUP;
            $fields = 'tg.gid, tg.gname, DATE_FORMAT(timestamp,\'%m/%d\')';
            $criteria = new CriteriaElement();
            $criteria->setLimit($config['num_rows']);
            $criteria->setSort('timestamp');
            $criteria->setOrder('DESC');
            $join = new XooNIpsJoinCriteria('xoonips_groups', 'gid', 'gid', 'INNER', 'tg');
            $handler =& xoonips_getormhandler('xoonips', $table);
            $res =& $handler->open($criteria, $fields, false, $join);
            $items = array();
            $i = 0;
            while ($obj =& $handler->getNext($res)) {
                $gid = $obj->getVar('gid', 'n');
                $title = $textutil->html_special_chars($obj->getExtraVar('gname'));
                $title = $textutil->truncate($title, $maxlen, $etc);
                $count = $textutil->html_special_chars($obj->getExtraVar('DATE_FORMAT(timestamp,\'%m/%d\')'));
                $url = XOOPS_URL . '/modules/xoonips/groups.php';
                $items[] = array('title' => $title, 'url' => $url, 'num' => $count, 'rank_str' => $rank_str[$i]);
                $i++;
            }
            $handler->close($res);
            $block['rankings'][$config['order'][1]] = array('items' => $items, 'title' => $label);
            unset($items);
        }
    } else {
        // ranking block
        // ranking viewed item
        if ($config['visible'][0]) {
            $table = 'ranking_viewed_item';
            $label = _MB_XOONIPS_RANKING_VIEWED_ITEM;
            $fields = 'tb.item_id, tt.title, count';
            $criteria = $iperm_criteria;
            $criteria->add(new Criteria('count', 0, '<>'));
            $criteria->setLimit($config['num_rows']);
            $criteria->setSort('count');
            $criteria->setOrder('DESC');
            $join = $iperm_join;
            $handler =& xoonips_getormhandler('xoonips', $table);
            $res =& $handler->open($criteria, $fields, true, $join);
            $items = array();
            $i = 0;
            while ($obj =& $handler->getNext($res)) {
                $item_id = $obj->getVar('item_id', 'n');
                $title = $textutil->html_special_chars($obj->getExtraVar('title'));
                $title = $textutil->truncate($title, $maxlen, $etc);
                $count = $obj->getVar('count', 'n');
                $doi = $textutil->html_special_chars($obj->getExtraVar('doi'));
                $id = $doi == '' && XNP_CONFIG_DOI_FIELD_PARAM_NAME != '' ? 'item_id=' . $item_id : XNP_CONFIG_DOI_FIELD_PARAM_NAME . '=' . urlencode($doi);
                $url = XOOPS_URL . '/modules/xoonips/detail.php?' . $id;
                $items[] = array('title' => $title, 'url' => $url, 'num' => $count, 'rank_str' => $rank_str[$i]);
                $i++;
            }
            $handler->close($res);
            $block['rankings'][$config['order'][0]] = array('items' => $items, 'title' => $label);
            unset($items);
        }
        // ranking downloaded item
        if ($config['visible'][1]) {
            $table = 'ranking_downloaded_item';
            $label = _MB_XOONIPS_RANKING_DOWNLOADED_ITEM;
            $fields = 'tb.item_id, tt.title, count';
            $criteria = $iperm_criteria;
            $criteria->add(new Criteria('count', 0, '<>'));
            $criteria->setLimit($config['num_rows']);
            $criteria->setSort('count');
            $criteria->setOrder('DESC');
            $join = $iperm_join;
            $handler =& xoonips_getormhandler('xoonips', $table);
            $res =& $handler->open($criteria, $fields, true, $join);
            $items = array();
            $i = 0;
            while ($obj =& $handler->getNext($res)) {
                $item_id = $obj->getVar('item_id', 'n');
                $title = $textutil->html_special_chars($obj->getExtraVar('title'));
                $title = $textutil->truncate($title, $maxlen, $etc);
                $count = $obj->getVar('count', 'n');
                $doi = $textutil->html_special_chars($obj->getExtraVar('doi'));
                $id = $doi == '' && XNP_CONFIG_DOI_FIELD_PARAM_NAME != '' ? 'item_id=' . $item_id : XNP_CONFIG_DOI_FIELD_PARAM_NAME . '=' . urlencode($doi);
                $url = XOOPS_URL . '/modules/xoonips/detail.php?' . $id;
                $items[] = array('title' => $title, 'url' => $url, 'num' => $count, 'rank_str' => $rank_str[$i]);
                $i++;
            }
            $handler->close($res);
            $block['rankings'][$config['order'][1]] = array('items' => $items, 'title' => $label);
            unset($items);
        }
        // ranking contiributing user
        if ($config['visible'][2]) {
            $table = 'ranking_contributing_user';
            $label = _MB_XOONIPS_RANKING_CONTRIBUTING_USER;
            $fields = 'tu.uid, tu.uname, COUNT(*) AS count';
            $criteria = new CriteriaElement();
            $criteria->setLimit($config['num_rows']);
            $criteria->setSort('count');
            $criteria->setOrder('DESC');
            $criteria->setGroupby('tu.uid');
            $join = new XooNIpsJoinCriteria('users', 'uid', 'uid', 'INNER', 'tu');
            $handler =& xoonips_getormhandler('xoonips', $table);
            $res =& $handler->open($criteria, $fields, true, $join);
            $items = array();
            $i = 0;
            while ($obj =& $handler->getNext($res)) {
                $uid = $obj->getVar('uid', 'n');
                $title = $textutil->html_special_chars($obj->getExtraVar('uname'));
                $title = $textutil->truncate($title, $maxlen, $etc);
                $count = intval($obj->getExtraVar('count'));
                $url = XOOPS_URL . '/modules/xoonips/showusers.php?uid=' . $uid;
                $items[] = array('title' => $title, 'url' => $url, 'num' => $count, 'rank_str' => $rank_str[$i]);
                $i++;
            }
            $handler->close($res);
            $block['rankings'][$config['order'][2]] = array('items' => $items, 'title' => $label);
            unset($items);
        }
        // ranking searched keyword
        if ($config['visible'][3]) {
            $table = 'ranking_searched_keyword';
            $label = _MB_XOONIPS_RANKING_SEARCHED_KEYWORD;
            $fields = 'keyword, count';
            $criteria = new CriteriaCompo(new Criteria('count', 0, '<>'));
            $criteria->add(new Criteria('keyword', '', '!='));
            // ignore empty
            $criteria->setLimit($config['num_rows']);
            $criteria->setSort('count');
            $criteria->setOrder('DESC');
            $handler =& xoonips_getormhandler('xoonips', $table);
            $res =& $handler->open($criteria, $fields, true);
            $items = array();
            $i = 0;
            while ($obj =& $handler->getNext($res)) {
                $keyword = $obj->getVar('keyword', 'n');
                $title = $textutil->html_special_chars($keyword);
                $title = preg_replace('/[\\x00-\\x20]/se', 'urlencode("\\0")', $title);
                $title = $textutil->truncate($title, $maxlen, $etc);
                $count = $obj->getVar('count', 'n');
                $url = XOOPS_URL . '/modules/xoonips/itemselect.php?op=quicksearch&amp;search_itemtype=all&amp;keyword=' . urlencode($keyword);
                $items[] = array('title' => $title, 'url' => $url, 'num' => $count, 'rank_str' => $rank_str[$i]);
                $i++;
            }
            $handler->close($res);
            $block['rankings'][$config['order'][3]] = array('items' => $items, 'title' => $label);
            unset($items);
        }
        if ($config['visible'][4]) {
            // ranking active group
            $table = 'ranking_active_group';
            $label = _MB_XOONIPS_RANKING_CONTRIBUTED_GROUP;
            $fields = 'tg.gid, tg.gname, count';
            $criteria = new Criteria('count', 0, '<>');
            $criteria->setLimit($config['num_rows']);
            $criteria->setSort('count');
            $criteria->setOrder('DESC');
            $join = new XooNIpsJoinCriteria('xoonips_groups', 'gid', 'gid', 'INNER', 'tg');
            $handler =& xoonips_getormhandler('xoonips', $table);
            $res =& $handler->open($criteria, $fields, false, $join);
            $items = array();
            $i = 0;
            while ($obj =& $handler->getNext($res)) {
                $gid = $obj->getVar('gid', 'n');
                $title = $textutil->html_special_chars($obj->getExtraVar('gname'));
                $title = $textutil->truncate($title, $maxlen, $etc);
                $count = $obj->getVar('count', 'n');
                $url = XOOPS_URL . '/modules/xoonips/groups.php';
                $items[] = array('title' => $title, 'url' => $url, 'num' => $count, 'rank_str' => $rank_str[$i]);
                $i++;
            }
            $handler->close($res);
            $block['rankings'][$config['order'][4]] = array('items' => $items, 'title' => $label);
            unset($items);
        }
    }
    ksort($block['rankings']);
    return $block;
}
 /**
  * gets File objects join xoonips_file_type.
  *
  * @access public
  * @param object $criteria
  * @param bool $id_as_key
  * @param string $fieldlist fieldlist for distinct select
  * @param bool $distinct
  * @param XooNIpsJoinCriteria $joindef join criteria object
  * @return array objects
  */
 function &getObjects($criteria = null, $id_as_key = false, $fieldlist = '', $distinct = false, $joindef = null)
 {
     if (is_null($joindef) || !(is_subclass_of($joindef, 'xoonipsjoincriteria') || strtolower(get_class($joindef)) == 'xoonipsjoincriteria')) {
         $joindef = new XooNIpsJoinCriteria('xoonips_file_type', 'file_type_id', 'file_type_id');
     } else {
         $joindef->cascade(new XooNIpsJoinCriteria('xoonips_file_type', 'file_type_id', 'file_type_id'), 'xoonips_file');
     }
     $table = $this->db->prefix('xoonips_file');
     if (trim($fieldlist) == '') {
         $fieldlist = "{$table}.file_id, {$table}.item_id, {$table}.original_file_name, {$table}.mime_type, {$table}.file_name, {$table}.file_size, {$table}.thumbnail_file, {$table}.caption, {$table}.sess_id, {$table}.file_type_id, {$table}.search_module_name, {$table}.search_module_version, {$table}.header, {$table}.timestamp, {$table}.is_deleted, {$table}.download_count";
     }
     $files = parent::getObjects($criteria, $id_as_key, $fieldlist, $distinct, $joindef);
     if ($files) {
         for ($i = 0; $i < count($files); $i++) {
             $files[$i]->setFilepath($this->createFilepath($files[$i]));
         }
     }
     return $files;
 }
 /**
  * get item ids of group shared or public item(ignore certify state)
  * @access public
  * @param int $uid user id item
  * @return array array of item id
  */
 function getNonPrivateItemIds($uid)
 {
     $result = array();
     $criteria = new CriteriaCompo();
     $criteria->add(new Criteria('open_level', '(' . implode(',', array(OL_GROUP_ONLY, OL_PUBLIC)) . ')', 'IN'));
     $criteria->add(new Criteria('uid', $uid, '=', 'basic'));
     $criteria->setSort('basic.item_id');
     $join = new XooNIpsJoinCriteria('xoonips_index', 'index_id', 'index_id');
     $join->cascade(new XooNIpsJoinCriteria('xoonips_item_basic', 'item_id', 'item_id', 'INNER', 'basic'));
     $index_item_link_handler =& xoonips_getormhandler('xoonips', 'index_item_link');
     foreach ($index_item_link_handler->getObjects($criteria, false, '', true, $join) as $index_item_link) {
         $result[] = $index_item_link->get('item_id');
     }
     return $result;
 }
 /**
  *
  * return true if permitted to this item
  *
  * @param item_id id of item
  * @param uid uid who access to this item
  * @param operation read|write|delete|export
  * @return true if permitted
  *
  */
 function getPerm($item_id, $uid, $operation)
 {
     if (!in_array($operation, array('read', 'write', 'delete', 'export'))) {
         return false;
         // bad operation
     }
     $item_basic_handler =& xoonips_getormhandler('xoonips', 'item_basic');
     $item_basic =& $item_basic_handler->get($item_id);
     if (!$item_basic || $item_basic->get('item_type_id') == ITID_INDEX) {
         return false;
         // no such item
     }
     $item_lock_handler =& xoonips_getormhandler('xoonips', 'item_lock');
     if (($operation == 'write' || $operation == 'delete') && $item_lock_handler->isLocked($item_id)) {
         return false;
         // cannot write/delete locked item
     }
     $index_group_index_link_handler =& xoonips_getormhandler('xoonips', 'index_group_index_link');
     if (($operation == 'write' || $operation == 'delete') && $index_group_index_link_handler->getObjectsByItemId($item_id)) {
         //cannot write/delete if item is in group index that is publication required.
         return false;
     }
     // moderator or admin
     $member_handler =& xoonips_gethandler('xoonips', 'member');
     if ($member_handler->isModerator($uid) || $member_handler->isAdmin($uid)) {
         return true;
         // moderator or admin
     }
     if ($uid == UID_GUEST) {
         $xconfig_handler =& xoonips_getormhandler('xoonips', 'config');
         $target_user = $xconfig_handler->getValue(XNP_CONFIG_PUBLIC_ITEM_TARGET_USER_KEY);
         if ($target_user != XNP_CONFIG_PUBLIC_ITEM_TARGET_USER_ALL) {
             return false;
             // guest not allowed
         }
         // only allowed to read public certified item
         if ($operation != 'read') {
             return false;
         }
     }
     $index_item_link_handler =& xoonips_getormhandler('xoonips', 'index_item_link');
     if ($operation == 'write') {
         // update: item.uid == $uid
         //permit owner
         if ($item_basic->get('uid') == $uid) {
             return true;
         }
         //permit group admin if group share certified
         $criteria = new CriteriaCompo();
         $criteria->add(new Criteria('item_id', $item_id));
         $criteria->add(new Criteria('open_level', OL_GROUP_ONLY));
         $criteria->add(new Criteria('uid', $uid, '=', 'tgul'));
         $criteria->add(new Criteria('is_admin', 1));
         $criteria->add(new Criteria('certify_state', CERTIFIED));
         $join1 = new XooNIpsJoinCriteria('xoonips_index', 'index_id', 'index_id', 'LEFT', 'tx');
         $join2 = new XooNIpsJoinCriteria('xoonips_groups_users_link', 'gid', 'gid', 'LEFT', 'tgul');
         $join1->cascade($join2, 'tx', true);
         $index_item_links =& $index_item_link_handler->getObjects($criteria, false, '', null, $join1);
         if ($index_item_links) {
             return true;
         }
         return false;
     } else {
         if ($operation == 'delete') {
             // delete: item.uid == $uid && not_group && not_public
             if ($item_basic->get('uid') != $uid) {
                 return false;
             }
             // get non-private index_item_link
             // index_item_link <- index
             $criteria = new CriteriaCompo();
             $criteria->add(new Criteria('item_id', $item_id));
             $criteria->add(new Criteria('open_level', OL_PRIVATE, '!='));
             $join = new XooNIpsJoinCriteria('xoonips_index', 'index_id', 'index_id');
             $index_item_links =& $index_item_link_handler->getObjects($criteria, false, '', null, $join);
             return count($index_item_links) == 0;
         } else {
             if ($operation == 'export') {
                 // export: item.uid == $uid || group && group admin
                 if ($item_basic->get('uid') == $uid) {
                     return true;
                 }
                 // group && group admin ?
                 // index_item_link <- index <- groups_users_link
                 $criteria = new CriteriaCompo();
                 $criteria->add(new Criteria('item_id', $item_id));
                 $criteria->add(new Criteria('open_level', OL_GROUP_ONLY));
                 $criteria->add(new Criteria('uid', $uid, '=', 'tgul'));
                 $criteria->add(new Criteria('is_admin', 1));
                 $join1 = new XooNIpsJoinCriteria('xoonips_index', 'index_id', 'index_id', 'LEFT', 'tx');
                 $join2 = new XooNIpsJoinCriteria('xoonips_groups_users_link', 'gid', 'gid', 'LEFT', 'tgul');
                 $join1->cascade($join2, 'tx', true);
                 $index_item_links =& $index_item_link_handler->getObjects($criteria, false, '', null, $join1);
                 return count($index_item_links) != 0;
             } else {
                 if ($operation == 'read') {
                     // read: item.uid == $uid || group_ceritfied && group_member || group && group admin || public_certified
                     if ($item_basic->get('uid') == $uid) {
                         return true;
                     }
                     // index_item_link <- index <- groups_users_link
                     $criteria = new CriteriaCompo();
                     $criteria->add(new Criteria('open_level', OL_PUBLIC, '=', 'tx'), 'and');
                     $criteria->add(new Criteria('certify_state', CERTIFIED, '='), 'and');
                     // public index && certified
                     $criteria->add(new Criteria('open_level', OL_GROUP_ONLY, '=', 'tx'), 'or');
                     // group index && group admin
                     $criteria->add(new Criteria('uid', $uid, '=', 'tgul'), 'and');
                     $criteria->add(new Criteria('is_admin', 1, '=', 'tgul'), 'and');
                     $criteria->add(new Criteria('open_level', OL_GROUP_ONLY, '=', 'tx'), 'or');
                     // group index && group member && certified
                     $criteria->add(new Criteria('uid', $uid, '=', 'tgul'), 'and');
                     $criteria->add(new Criteria('certify_state', CERTIFIED, '='), 'and');
                     $criteria = new CriteriaCompo($criteria);
                     $criteria->add(new Criteria('item_id', $item_id));
                     $join1 = new XooNIpsJoinCriteria('xoonips_index', 'index_id', 'index_id', 'LEFT', 'tx');
                     $join2 = new XooNIpsJoinCriteria('xoonips_groups_users_link', 'gid', 'gid', 'LEFT', 'tgul');
                     $join1->cascade($join2, 'tx', true);
                     $index_item_links =& $index_item_link_handler->getObjects($criteria, false, '', null, $join1);
                     if (count($index_item_links) != 0) {
                         return true;
                     }
                     // item transferee?
                     $transfer_request_handler =& xoonips_getormhandler('xoonips', 'transfer_request');
                     $criteria = new CriteriaCompo();
                     $criteria->add(new Criteria('item_id', $item_id));
                     $criteria->add(new Criteria('to_uid', $uid));
                     $transfer_request =& $transfer_request_handler->getObjects($criteria);
                     return !empty($transfer_request);
                 }
             }
         }
     }
     return false;
 }
/**
 * get item objects
 *
 * @access private
 * @param int $item_type_id item type id
 * @param int $uid user id
 * @param bool $is_owner_only true if count own items
 * @param string $sort sort of criteria
 *  'title', 'item_id', 'ext_id', 'last_update' or 'creation_date'
 * @param string $order order of criteria
 *  'ASC' or 'DESC'
 * @param int $start start of criteria
 * @param int $limit limit of criteria
 * @return array item list htmls
 */
function &_xoonips_editshowitem_get_item_objects($item_type_id, $uid, $is_owner_only, $sort = null, $order = null, $start = null, $limit = null)
{
    $xil_handler =& xoonips_getormhandler('xoonips', 'index_item_link');
    $join = new XooNIpsJoinCriteria('xoonips_index', 'index_id', 'index_id', 'INNER', 'idx');
    $join->cascade(new XooNIpsJoinCriteria('xoonips_item_basic', 'item_id', 'item_id', 'INNER', 'ib'));
    $join->cascade(new XooNIpsJoinCriteria('xoonips_item_title', 'item_id', 'item_id', 'INNER', 'it'));
    $criteria = new CriteriaCompo(new Criteria('certify_state', CERTIFIED));
    $criteria->add(new Criteria('open_level', OL_PUBLIC, '=', 'idx'));
    if ($is_owner_only) {
        $criteria->add(new Criteria('uid', $uid, '=', 'ib'));
    }
    $criteria->add(new Criteria('item_type_id', $item_type_id, '=', 'ib'));
    $criteria->add(new Criteria('title_id', 0, '=', 'it'));
    if (!is_null($start)) {
        $def_sort = array('title' => 'it.title', 'item_id' => 'ib.item_id', 'ext_id' => 'ib.doi', 'last_update' => 'last_updated_date', 'creation_date' => 'creation_date');
        $def_order = array('ASC' => 'ASC', 'DESC' => 'DESC');
        $sort = isset($def_sort[$sort]) ? $def_sort[$sort] : 'it.title';
        $order = isset($def_order[$order]) ? $def_order[$order] : 'ASC';
        $criteria->setSort($sort);
        $criteria->setOrder($order);
        $criteria->setStart($start);
        $criteria->setLimit($limit);
    }
    return $xil_handler->getObjects($criteria, false, 'ib.item_id', true, $join);
}
 /**
  * get item ids by multiple index id
  *
  * @access public
  * @param array $xids index ids
  * @param int $uid user id
  * @param string $perm permission: 'read', 'write', 'delete' or 'export'
  * @param array &$iid_perm_cache item ids access permission cache
  * @return array multiple count items
  *  array(
  *     $xid1 => array( $iid1_1, $iid1_2,... ),
  *     $xid2 => array( $iid2_1,... ),
  *     ...
  *  );
  */
 function _get_item_ids_by_index_ids($xids, $uid, $perm, &$iid_perm_cache)
 {
     if (!is_array($xids) || empty($xids)) {
         $this->_fatal_error('invalid index ids', __LINE__);
     }
     $index_item_link_handler =& xoonips_getormhandler('xoonips', 'index_item_link');
     $join = new XooNIpsJoinCriteria('xoonips_index', 'index_id', 'index_id', 'INNER', 'idx');
     // ignore item id has not item basic table
     $join->cascade(new XooNIpsJoinCriteria('xoonips_item_basic', 'item_id', 'item_id', 'INNER', 'ib'));
     $criteria = new Criteria('index_id', '(' . implode(',', $xids) . ')', 'IN', 'idx');
     // hold returning index order
     $miids = array();
     foreach ($xids as $xid) {
         $miids[$xid] = array();
     }
     // get child item ids
     $item_chandler =& xoonips_getormcompohandler('xoonips', 'item');
     $res =& $index_item_link_handler->open($criteria, null, false, $join);
     while ($xil_obj =& $index_item_link_handler->getNext($res)) {
         $xid = $xil_obj->get('index_id');
         $iid = $xil_obj->get('item_id');
         if (!isset($iid_perm_cache[$iid])) {
             // check permission, if item id is not cached
             $iid_perm_cache[$iid] = $item_chandler->getPerm($iid, $uid, $perm);
         }
         if ($iid_perm_cache[$iid]) {
             $miids[$xid][] = $iid;
         }
     }
     $index_item_link_handler->close($res);
     return $miids;
 }
 /**
  * get group item ids
  *
  * @access public
  * @param int $gid group id
  * @param int $uid user id
  * @return array item ids
  */
 function getGroupItemIds($gid, $uid = null)
 {
     $index_item_link_handler =& xoonips_getormhandler('xoonips', 'index_item_link');
     $join = new XooNIpsJoinCriteria('xoonips_index', 'index_id', 'index_id', 'INNER', 'idx');
     $criteria = new CriteriaCompo(new Criteria('gid', $gid, '=', 'idx'));
     $join->cascade(new XooNIpsJoinCriteria('xoonips_item_basic', 'item_id', 'item_id', 'INNER', 'ib'));
     if (!is_null($uid)) {
         $criteria->add(new Criteria('uid', $uid, '=', 'ib'));
     }
     $res =& $index_item_link_handler->open($criteria, 'ib.item_id', true, $join);
     $iids = array();
     while ($xil_obj =& $index_item_link_handler->getNext($res)) {
         $iids[] = $xil_obj->get('item_id');
     }
     $index_item_link_handler->close($res);
     return $iids;
 }
function _xoonips_showusers_get_item_ids($item_type_id, $uid, &$navi)
{
    $item_basic_handler =& xoonips_getormhandler('xoonips', 'item_basic');
    $join = new XooNIpsJoinCriteria('xoonips_item_show', 'item_id', 'item_id', 'INNER', 'its');
    $join->cascade(new XooNIpsJoinCriteria('xoonips_item_title', 'item_id', 'item_id', 'INNER', 'it'));
    $criteria = new CriteriaCompo(new Criteria('item_type_id', $item_type_id, '='));
    $criteria->add(new Criteria('uid', $uid, '=', 'its'));
    $criteria->add(new Criteria('title_id', 0, '=', 'it'));
    $start = $navi->getStart();
    $limit = $navi->getLimit();
    $sort = $navi->getSort();
    $order = $navi->getOrder();
    $def_sort = array('title' => 'it.title', 'item_id' => 'its.item_id', 'ext_id' => 'doi', 'last_update' => 'last_updated_date', 'creation_date' => 'creation_date', 'publication_date' => 'publication_year');
    $def_order = array('ASC' => 'ASC', 'DESC' => 'DESC');
    $sort = isset($def_sort[$sort]) ? $def_sort[$sort] : 'it.title';
    $order = isset($def_order[$order]) ? $def_order[$order] : 'ASC';
    $criteria->setStart($start);
    $criteria->setLimit($limit);
    $criteria->setSort($sort);
    $criteria->setOrder($order);
    $item_ids = array();
    $res =& $item_basic_handler->open($criteria, 'its.item_id', true, $join);
    while ($item_basic_obj =& $item_basic_handler->getNext($res)) {
        $item_ids[] = $item_basic_obj->get('item_id');
    }
    $item_basic_handler->close($res);
    return $item_ids;
}
 /**
  * remove a member from a group. cannot remove if member
  * shares(certified or certify request) items to this group.
  *
  * @access public
  * @param int $gid group id
  * @param int $uid user id
  * @return bool FALSE if failed
  */
 function remove($gid, $uid)
 {
     // is $uid a member of $gid ?
     $criteria = new CriteriaCompo();
     $criteria->add(new Criteria('gid', $gid));
     $criteria->add(new Criteria('uid', $uid));
     $groups_users_links =& $this->getObjects($criteria);
     if ($groups_users_links === false) {
         // error
         return false;
     }
     if (count($groups_users_links) != 1) {
         // not a member
         return false;
     }
     /* select from item_basic
             left join $join(index_item_link)
             left join $join2(index)
           where item_basic.uid=$uid and index.gid=$gid
        */
     $criteria = new CriteriaCompo();
     $criteria->add(new Criteria('uid', $uid, '=', $this->db->prefix('xoonips_item_basic')));
     $criteria->add(new Criteria('gid', $gid, '=', 'tx'));
     $join = new XooNIpsJoinCriteria('xoonips_index_item_link', 'item_id', 'item_id');
     $join2 = new XooNIpsJoinCriteria('xoonips_index', 'index_id', 'index_id', 'LEFT', 'tx');
     $join->cascade($join2);
     $item_basic_handler =& xoonips_getormhandler('xoonips', 'item_basic');
     $item_basics =& $item_basic_handler->getObjects($criteria, false, '', false, $join);
     if ($item_basics === false) {
         // error
         return false;
     }
     if (count($item_basics) != 0) {
         // cannot remove because user shares items to this group.
         return false;
     }
     return $this->delete($groups_users_links[0]);
 }
 /**
  * execute searchItem
  *
  * @param[in]  $vars[0] session ID
  * @param[in]  $vars[1] query string
  * @param[in]  $vars[2] offset of the first row to return (The offset of the initial row is 0)
  * @param[in]  $vars[3] maximum number of rows to return (0:all rows)
  * @param[in]  $vars[4] sort key('title'|'ext_id'|'last_modified_date'|'registration_date'|'creation_date')
  * @param[in]  $vars[5] sort order('asc'|'desc')
  * @param[out] $response->result true:success, false:failed
  * @param[out] $response->error  error information
  * @param[out] $response->success['count'] number of matched item
  * @param[out] $response->success[n] search result
  * @param[out] $response->success[n]['id'] item id of matched item
  * @param[out] $response->success[n]['matchfor'] bit flag represents what part of item matched
  */
 function execute(&$vars, &$response)
 {
     // parameter check
     $error =& $response->getError();
     if (count($vars) > 6) {
         $error->add(XNPERR_EXTRA_PARAM);
     }
     if (count($vars) < 6) {
         $error->add(XNPERR_MISSING_PARAM);
     }
     //
     if (isset($vars[0]) && strlen($vars[0]) > 32) {
         $error->add(XNPERR_INVALID_PARAM, 'too long parameter 1');
     }
     $ar = explode(':', $vars[1]);
     if (count($ar) < 2 || !in_array($ar[0], array('index', 'keyword'))) {
         $error->add(XNPERR_INVALID_PARAM, 'bad query parameter 2');
     }
     if (!is_int($vars[2]) && !ctype_digit($vars[2])) {
         $error->add(XNPERR_INVALID_PARAM, 'not integer parameter 2');
     }
     if (!is_int($vars[3]) && !ctype_digit($vars[3])) {
         $error->add(XNPERR_INVALID_PARAM, 'not integer parameter 3');
     }
     if (!in_array($vars[4], array('title', 'ext_id', 'last_modified_date', 'registration_date', 'creation_date'))) {
         $error->add(XNPERR_INVALID_PARAM, 'unknown sort key parameter 4');
     }
     if (!in_array($vars[5], array('asc', 'desc'))) {
         $error->add(XNPERR_INVALID_PARAM, 'unknown sort order parameter 5');
     }
     //
     if ($error->get(0)) {
         // return if parameter error
         $response->setResult(false);
         return;
     } else {
         $sessionid = $vars[0];
         $query = $vars[1];
         $start = $vars[2];
         $limit = $vars[3];
         $sortkey = $vars[4];
         $ascdesc = $vars[5];
     }
     list($result, $uid, $session) = $this->restoreSession($sessionid);
     if (!$result) {
         $response->setResult(false);
         $error->add(XNPERR_INVALID_SESSION);
         return false;
     }
     $response->setResult(false);
     // convert ($start, $limit, $sortkey, $ascdesc) to ($criteria, $join_sort_table)
     $criteria = new CriteriaCompo();
     $criteria->setOrder($ascdesc);
     $criteria->setStart($start);
     $criteria->setLimit($limit);
     switch ($sortkey) {
         case 'title':
             $join_sort_table = 'xoonips_item_title';
             $criteria->add(new Criteria('title_id', 0));
             $criteria->setSort('title');
             break;
         case 'ext_id':
             $join_sort_table = false;
             $criteria->setSort('doi');
             break;
         case 'last_modified_date':
             $join_sort_table = false;
             $criteria->setSort('last_update_date');
             break;
         case 'registration_date':
             $join_sort_table = false;
             $criteria->setSort('creation_date');
             break;
         case 'creation_date':
             $join_sort_table = false;
             $criteria->setSort(array('publication_year', 'publication_month', 'publication_mday'));
             break;
         default:
             $error->add(XNPERR_INVALID_PARAM, 'unknown sort key parameter 4');
             $response->setResult(false);
             return false;
             break;
     }
     // get valid item_type_ids -> $criteria
     $factory =& XooNIpsLogicFactory::getInstance();
     $logic =& $factory->create("getItemTypes");
     $vars = array($sessionid);
     $logic->execute($vars, $response);
     if ($response->getResult() == false) {
         return false;
     }
     $itemtypes = $response->getSuccess();
     if (count($itemtypes) == 0) {
         // no itemtype found. return empty array.
         $ar = array();
         $response->setSuccess($ar);
         $response->setResult(true);
         return true;
     }
     $itemtype_ids = array();
     foreach ($itemtypes as $itemtype) {
         $itemtype_ids[] = $itemtype->get('item_type_id');
     }
     $criteria->add(new Criteria('item_type_id', '(' . implode(',', $itemtype_ids) . ')', 'IN'));
     if (substr($query, 0, 6) == 'index:') {
         // index id -> search_cache_items
         $index_id = substr($query, 6);
         if (!ctype_digit($index_id)) {
             $error->add(XNPERR_INVALID_PARAM, "bad index id(index_id={$index_id})");
             $response->setResult(false);
             return false;
         }
         $criteria->add(new Criteria('index_id', $index_id));
         $index_handler =& xoonips_getormhandler('xoonips', 'index');
         $index = $index_handler->get($index_id);
         if (false == $index) {
             $error->add(XNPERR_NOT_FOUND, "index not found(index_id={$index_id})");
             $response->setResult(false);
             return false;
         }
         if (!$index_handler->getPerm($index_id, $uid, 'read')) {
             $error->add(XNPERR_ACCESS_FORBIDDEN, "forbidden(index_id={$index_id})");
             $response->setResult(false);
             return false;
         }
         // xoonips_index_item_link <- $join(xoonips_item_basic) <- $join2($join_sort_table)
         $index_item_link_handler =& xoonips_getormhandler('xoonips', 'index_item_link');
         $join = new XooNIpsJoinCriteria('xoonips_item_basic', 'item_id', 'item_id');
         if ($join_sort_table) {
             $join2 = new XooNIpsJoinCriteria($join_sort_table, 'item_id', 'item_id');
             $join->cascade($join2);
         }
         $index_item_links =& $index_item_link_handler->getObjects($criteria, false, '', false, $join);
         $item_compo_handler =& xoonips_getormcompohandler('xoonips', 'item');
         $search_cache_item_handler =& xoonips_getormhandler('xoonips', 'search_cache_item');
         $search_cache_items = array();
         foreach ($index_item_links as $index_item_link) {
             if (!$item_compo_handler->getPerm($index_item_link->get('item_id'), $uid, 'read')) {
                 continue;
             }
             // no read permission
             $search_cache_item = $search_cache_item_handler->create();
             $search_cache_item->set('item_id', $index_item_link->get('item_id'));
             $search_cache_item->set('matchfor_index', 1);
             $search_cache_item->set('matchfor_item', 0);
             $search_cache_item->set('matchfor_file', 0);
             $search_cache_items[] = $search_cache_item;
         }
         $response->setSuccess($search_cache_items);
         $response->setResult(true);
         return true;
     } else {
         if (substr($query, 0, 8) == 'keyword:') {
             $search_cache_id = $this->getSearchCacheIdByQuery($query);
             $search_cache_handler =& xoonips_getormhandler('xoonips', 'search_cache');
             $eventlog_handler =& xoonips_getormhandler('xoonips', 'event_log');
             if ($search_cache_id) {
                 $search_cache = $search_cache_handler->get($search_cache_id);
                 if ($search_cache === false) {
                     // maybe invalidated.
                     $search_cache_id = false;
                 } else {
                     $c = new CriteriaCompo();
                     $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);
                     $c->add(new Criteria('event_type_id', '(' . implode(',', $event_type_ids) . ')', 'in'));
                     $c->add(new Criteria("timestamp - unix_timestamp('" . $search_cache->get('timestamp') . "')", 0, '>='));
                     $c->setSort('timestamp');
                     $c->setOrder('desc');
                     $c->setLimit(1);
                     $event_logs =& $eventlog_handler->getObjects($c);
                     if (false === $event_logs) {
                         $response->setResult(false);
                         $error->add(XNPERR_SERVER_ERROR, "cannot get event logs");
                         return false;
                     } else {
                         if (!empty($event_logs)) {
                             $search_cache_id = false;
                             // delete old search results from search cache
                             $c = new CriteriaCompo(new Criteria('sess_id', $sessionid));
                             $c->add(new Criteria('unix_timestamp(timestamp)', $event_logs[0]->get('timestamp'), '<'));
                             $search_caches =& $search_cache_handler->getObjects($c);
                             if (false === $search_caches) {
                                 $response->setResult(false);
                                 $error->add(XNPERR_SERVER_ERROR, "cannot get search cache ids");
                                 return false;
                             }
                             $search_cache_item_handler =& xoonips_getormhandler('xoonips', 'search_cache_item');
                             foreach ($search_caches as $search_cache) {
                                 $id = $search_cache->get('search_cache_id');
                                 $this->deleteSearchCache($id);
                                 $search_cache_handler->delete($search_cache);
                                 $search_cache_item_handler->deleteAll(new Criteria('search_cache_id', $id));
                             }
                         }
                     }
                 }
             }
             if (!$search_cache_id) {
                 $search_cache = $search_cache_handler->create();
                 $search_cache->setVar('sess_id', $sessionid, true);
                 if (!$search_cache_handler->insert($search_cache)) {
                     $response->setResult(false);
                     $error->add(XNPERR_SERVER_ERROR, "cannot create search_cache_id");
                     return false;
                 }
                 $search_cache_id = $search_cache->get('search_cache_id');
                 $keyword = substr($query, 8);
                 $this->keywordSearch($error, $search_cache_id, $uid, $keyword);
                 if (!$eventlog_handler->recordQuickSearchEvent('all', $keyword)) {
                     $error->add(XNPERR_SERVER_ERROR, "cannot insert event");
                     $response->setResult(false);
                     return false;
                 }
             }
             //
             // access permission: ol_public && ( is_moderator || certify_state=certified) || ol_group_only && gid=gulink.gid && ( is_admin || certify_state==certified) || ol_private && uid=uid // how to reuse?
             // -> criteria
             $c = new CriteriaCompo();
             $c->add(new Criteria('open_level', OL_PUBLIC));
             $member_handler =& xoonips_gethandler('xoonips', 'member');
             if (!$member_handler->isModerator($uid)) {
                 $c->add(new Criteria('certify_state', CERTIFIED));
             }
             $groups_users_link_handler =& xoonips_getormhandler('xoonips', 'groups_users_link');
             $links =& $groups_users_link_handler->getObjects(new Criteria('uid', $uid));
             foreach ($links as $link) {
                 $c->add(new Criteria('open_level', OL_GROUP_ONLY), 'OR');
                 $c->add(new Criteria('gid', $link->get('gid')));
                 if (!$link->get('is_admin')) {
                     $c->add(new Criteria('certify_state', CERTIFIED));
                 }
             }
             $c->add(new Criteria('open_level', OL_PRIVATE), 'OR');
             $c->add(new Criteria('tx.uid', $uid));
             $criteria->add($c);
             $criteria->add(new Criteria('search_cache_id', $search_cache_id));
             $criteria->setGroupby('search_cache_item_id');
             // join:
             //   xoonips_search_cache_item  <-  $join(xoonips_index_item_link)
             //                              <-  $join2(xoonips_index)
             //                              <-  $join3(xoonips_item_basic)
             //                              <-  $join4($join_sort_table)
             $join = new XooNIpsJoinCriteria('xoonips_index_item_link', 'item_id', 'item_id', 'LEFT', 'txil');
             $join2 = new XooNIpsJoinCriteria('xoonips_index', 'index_id', 'index_id', 'LEFT', 'tx');
             $join3 = new XooNIpsJoinCriteria('xoonips_item_basic', 'item_id', 'item_id');
             $join4 = new XooNIpsJoinCriteria($join_sort_table, 'item_id', 'item_id');
             $join->cascade($join2, 'txil', true);
             $join->cascade($join3, 'xoonips_search_cache_item');
             if ($join_sort_table) {
                 $join->cascade($join4, 'xoonips_search_cache_item');
             }
             $search_cache_item_handler =& xoonips_getormhandler('xoonips', 'search_cache_item');
             $search_cache_items =& $search_cache_item_handler->getObjects($criteria, false, '', false, $join);
             if ($search_cache_items === false) {
                 $error->add(XNPERR_INVALID_PARAM, 'cannot get searched item');
                 $response->setResult(false);
                 return false;
             }
             $this->putSearchCache($query, $search_cache_id);
             $response->setSuccess($search_cache_items);
             $response->setResult(true);
             return true;
         } else {
             $error->add(XNPERR_INVALID_PARAM, "query must begin with index: or keyword:");
         }
     }
     if ($error->get()) {
         $response->setResult(false);
         return false;
     }
 }