/**
 *
 * アイテム情報取得.
 *
 * @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;
}
function xnpmodelCheckRegisterParameters(&$msg)
{
    $formdata =& xoonips_getutility('formdata');
    $xnpsid = $_SESSION['XNPSID'];
    $result = true;
    $model_data = $formdata->getFile('model_data', false);
    $model_dataFileID = $formdata->getValue('post', 'model_dataFileID', 'i', false);
    $xoonipsCheckedXID = $formdata->getValue('post', 'xoonipsCheckedXID', 's', false);
    $creators = xoonips_get_multi_field_array_from_post('xnpmodel', 'creator');
    if (empty($creators)) {
        $messages[] = _MD_XNPMODEL_CREATOR_REQUIRED;
    }
    if ((!isset($model_data) || $model_data['name'] == '') && $model_dataFileID == '') {
        // model_data is not filled
        $msg = $msg . '<br /><font color="#ff0000">' . _MD_XNPMODEL_MODEL_FILE_REQUIRED . '</font>';
        $result = false;
    }
    // require Readme and License if register to public indexes
    $xids = explode(',', $xoonipsCheckedXID);
    $indexes = array();
    if ($xids[0] != $xoonipsCheckedXID) {
        foreach ($xids as $i) {
            $index = array();
            if (xnp_get_index($xnpsid, $i, $index) == RES_OK) {
                $indexes[] = $index;
            } else {
                $msg = $msg . '<br /><font color="#ff0000">' . xnp_get_last_error_string() . '</font>';
                $result = false;
                break;
            }
        }
    }
    if (count($indexes) > 0) {
        foreach ($indexes as $i) {
            if ($i['open_level'] <= OL_GROUP_ONLY) {
                $readmeEncText = $formdata->getValue('post', 'readmeEncText', 's', false);
                $rightsEncText = $formdata->getValue('post', 'rightsEncText', 's', false);
                $rightsUseCC = $formdata->getValue('post', 'rightsUseCC', 'i', false);
                if ($readmeEncText == '') {
                    // readme is not filled
                    $msg = $msg . '<br /><font color="#ff0000">' . _MD_XNPMODEL_README_REQUIRED . '</font>';
                    $result = false;
                }
                if ($rightsEncText == '' && $rightsUseCC == '0') {
                    // license is not filled
                    $msg = $msg . '<br /><font color="#ff0000">' . _MD_XNPMODEL_RIGHTS_REQUIRED . '</font>';
                    $result = false;
                }
                break;
            }
        }
    }
    return $result;
}
/** make sure that enterd detail information is correctly or not.
 * called from register confirmation and edit confirmation
 */
function xnppresentationCheckRegisterParameters(&$message)
{
    $xnpsid = $_SESSION['XNPSID'];
    $messages = array();
    $formdata =& xoonips_getutility('formdata');
    $creator = xoonips_get_multi_field_array_from_post('xnppresentation', 'creator');
    $presentation_fileFileID = $formdata->getValue('post', 'presentation_fileFileID', 'i', true);
    $presentation_file = $formdata->getFile('presentation_file', false);
    $xoonipsCheckedXID = $formdata->getValue('post', 'xoonipsCheckedXID', 's', true);
    if (empty($creator)) {
        $messages[] = _MD_XNPPRESENTATION_CREATOR_REQUIRED;
    }
    if (empty($presentation_fileFileID) && empty($presentation_file['name'])) {
        $messages[] = _MD_XNPPRESENTATION_PRESENTATION_FILE_REQUIRED;
    }
    // notify that license statement is required when register into public indexes.
    $xids = explode(',', $xoonipsCheckedXID);
    $indexes = array();
    if ($xids[0] != $xoonipsCheckedXID) {
        foreach ($xids as $i) {
            $index = array();
            if (xnp_get_index($xnpsid, $i, $index) == RES_OK) {
                $indexes[] = $index;
            } else {
                $messages[] = '<font color=\'#ff0000\'>' . xnp_get_last_error_string() . '</font>';
                $result = false;
                break;
            }
        }
    }
    if (count($indexes) > 0) {
        foreach ($indexes as $i) {
            if ($i['open_level'] <= OL_GROUP_ONLY) {
                $readmeEncText = $formdata->getValue('post', 'readmeEncText', 's', true);
                $rightsEncText = $formdata->getValue('post', 'rightsEncText', 's', true);
                $rightsUseCC = $formdata->getValue('post', 'rightsUseCC', 'i', true);
                if ($readmeEncText == '') {
                    // readme is not filled
                    $messages[] = '<font color=\'#ff0000\'>' . _MD_XNPPRESENTATION_README_REQUIRED . '</font>';
                }
                if ($rightsEncText == '' && $rightsUseCC == '0') {
                    // license is not filled
                    $messages[] = '<font color=\'#ff0000\'>' . _MD_XNPPRESENTATION_RIGHTS_REQUIRED . '</font>';
                }
                break;
            }
        }
    }
    if (count($messages) == 0) {
        return true;
    }
    $message = "<br />\n" . implode("<br />\n", $messages);
    return false;
}
/** check DetailInformation input
 * called from confirm/registered page
 */
function xnpconferenceCheckRegisterParameters(&$message)
{
    $xnpsid = $_SESSION['XNPSID'];
    $formdata =& xoonips_getutility('formdata');
    $messages = array();
    $conference_title = $formdata->getValue('post', 'conference_title', 's', false);
    $place = $formdata->getValue('post', 'place', 's', false);
    $author = xoonips_get_multi_field_array_from_post('xnpconference', 'author');
    $conference_fileFileID = $formdata->getValue('post', 'conference_fileFileID', 'i', false);
    $conference_file = $formdata->getFile('conference_file', false);
    $xoonipsCheckedXID = $formdata->getValue('post', 'xoonipsCheckedXID', 's', false);
    $conferenceFromMonth = $formdata->getValue('post', 'conferenceFromMonth', 'i', false);
    $conferenceFromDay = $formdata->getValue('post', 'conferenceFromDay', 'i', false);
    $conferenceFromYear = $formdata->getValue('post', 'conferenceFromYear', 'i', false);
    $conferenceToMonth = $formdata->getValue('post', 'conferenceToMonth', 'i', false);
    $conferenceToDay = $formdata->getValue('post', 'conferenceToDay', 'i', false);
    $conferenceToYear = $formdata->getValue('post', 'conferenceToYear', 'i', false);
    if (empty($conference_title)) {
        $messages[] = _MD_XNPCONFERENCE_CONFERENCE_TITLE_REQUIRED;
    }
    if (empty($place)) {
        $messages[] = _MD_XNPCONFERENCE_PLACE_REQUIRED;
    }
    if (empty($author)) {
        $messages[] = _MD_XNPCONFERENCE_AUTHOR_REQUIRED;
    }
    if (empty($conference_fileFileID) && empty($conference_file['name'])) {
        $messages[] = _MD_XNPCONFERENCE_PRESENTATION_FILE_REQUIRED;
    }
    $conference_from = date('U', mktime(0, 0, 0, $conferenceFromMonth, $conferenceFromDay, $conferenceFromYear));
    $conference_to = date('U', mktime(0, 0, 0, $conferenceToMonth, $conferenceToDay, $conferenceToYear));
    if ($conference_from > $conference_to) {
        $messages[] = _MD_XNPCONFERENCE_DATE_ERROR;
    }
    // require Readme, License and Rights if register to public indexes
    $xids = explode(',', $xoonipsCheckedXID);
    $indexes = array();
    if ($xids[0] != $xoonipsCheckedXID) {
        foreach ($xids as $i) {
            $index = array();
            if (xnp_get_index($xnpsid, $i, $index) == RES_OK) {
                $indexes[] = $index;
            } else {
                $messages[] = '<font color=\'#ff0000\'>' . xnp_get_last_error_string() . '</font>';
                $result = false;
                break;
            }
        }
    }
    if (count($messages) == 0) {
        return true;
    }
    $message = "<br />\n" . implode("<br />\n", $messages);
    return false;
}
     $system_message = $system_message . $msg;
 }
 //confirm
 xnpEncodeMacSafariPost();
 xnpEncodeMacSafariGet();
 $check_xids = empty($xoonipsCheckedXID) ? array() : explode(',', $xoonipsCheckedXID);
 //prepare template
 $xoopsOption['template_main'] = 'xoonips_confirm_register.html';
 include XOOPS_ROOT_PATH . '/header.php';
 if ($param_check_result) {
     // select /Private and notice
     // that /Private is selected automatically
     // if any private indexes are not selected.
     $account = array();
     if (RES_OK != xnp_get_account($xnpsid, $uid, $account)) {
         redirect_header(XOOPS_URL . '/', 3, "ERROR xnp_get_account. " . xnp_get_last_error_string());
         break;
     }
     if (!$private_index_flag) {
         //select /Private
         $check_xids[] = $account['private_index_id'];
         //notice message
         $xoopsTpl->assign('select_private_index_auto', "1");
         //overwrite xoonipsCheckedXID with an array of index Ids
         // that contains /Private
         $_POST['xoonipsCheckedXID'] = implode(',', $check_xids);
     }
 }
 $token_ticket = $xoopsGTicket->getTicketHtml(__LINE__, 600, 'register');
 $xoopsTpl->assign('token_hidden', $token_ticket);
 $select_item_type = array();
/** check DetailInformation input
 * called from confirm/registered page
 */
function xnpfilesCheckRegisterParameters(&$message)
{
    $xnpsid = $_SESSION['XNPSID'];
    $formdata =& xoonips_getutility('formdata');
    $messages = array();
    $files_file = $formdata->getFile('files_file', false);
    $file_id = $formdata->getValue('post', 'files_fileFileID', 'i', false);
    $title = $formdata->getValue('post', 'title', 's', false);
    $xoonipsCheckedXID = $formdata->getValue('post', 'xoonipsCheckedXID', 's', false);
    if (empty($file_id) && empty($files_file['name'])) {
        if ($title == XNPFILES_ITEM_TITLE_EMPTY_MAGIC) {
            $formdata->set('post', 'title', '');
            $messages[] = _MD_XOONIPS_ITEM_TITLE_REQUIRED;
        }
        $messages[] = _MD_XNPFILES_DATA_FILE_REQUIRED;
    } else {
        if ($title == XNPFILES_ITEM_TITLE_EMPTY_MAGIC) {
            if (is_null($files_file)) {
                $file_handler =& xoonips_getormhandler('xoonips', 'file');
                $file_obj =& $file_handler->get($file_id);
                $file_name = $file_obj->get('original_file_name', 's');
            } else {
                $file_name = $files_file['name'];
            }
            if (mb_substr_count($file_name, '.') > 0) {
                $file_name = mb_substr($file_name, 0, mb_strrpos($file_name, '.'));
            }
            $formdata->set('post', 'title', $file_name);
        }
    }
    // require Readme, License and Rights if register to public indexes
    $xids = explode(',', $xoonipsCheckedXID);
    $indexes = array();
    if ($xids[0] != $xoonipsCheckedXID) {
        foreach ($xids as $i) {
            $index = array();
            if (xnp_get_index($xnpsid, $i, $index) == RES_OK) {
                $indexes[] = $index;
            } else {
                $messages[] = '<font color=\'#ff0000\'>' . xnp_get_last_error_string() . '</font>';
                $result = false;
                break;
            }
        }
    }
    if (count($messages) == 0) {
        return true;
    }
    $message = "<br />\n" . implode("<br />\n", $messages);
    return false;
}
function xnpsimulatorCheckRegisterParameters(&$msg)
{
    $formdata =& xoonips_getutility('formdata');
    $xnpsid = $_SESSION['XNPSID'];
    $result = true;
    $developer = xoonips_get_multi_field_array_from_post('xnpsimulator', 'developer');
    $simulator_data = $formdata->getFile('simulator_data', false);
    $simulator_dataFileID = $formdata->getValue('post', 'simulator_dataFileID', 'i', false);
    $xoonipsCheckedXID = $formdata->getValue('post', 'xoonipsCheckedXID', 's', false);
    if (empty($developer)) {
        // developer is not filled
        $msg = $msg . '<br/><font color=\'#ff0000\'>' . _MD_XNPSIMULATOR_DEVELOPER_REQUIRED . '</font>';
        $result = false;
    }
    if ((empty($simulator_data) || $simulator_data['name'] == '') && $simulator_dataFileID == '') {
        // simulator_data is not filled
        $msg = $msg . '<br/><font color=\'#ff0000\'>' . _MD_XNPSIMULATOR_SIMULATOR_FILE_REQUIRED . '</font>';
        $result = false;
    }
    // notify that license statement is required when register into public indexes.
    $xids = explode(',', $xoonipsCheckedXID);
    $indexes = array();
    if ($xids[0] != $xoonipsCheckedXID) {
        foreach ($xids as $i) {
            $index = array();
            if (xnp_get_index($xnpsid, $i, $index) == RES_OK) {
                $indexes[] = $index;
            } else {
                $msg = $msg . '<br/><font color=\'#ff0000\'>' . xnp_get_last_error_string() . '</font>';
                $result = false;
                break;
            }
        }
    }
    if (count($indexes) > 0) {
        foreach ($indexes as $i) {
            if ($i['open_level'] <= OL_GROUP_ONLY) {
                $readmeEncText = $formdata->getValue('post', 'readmeEncText', 's', false);
                $rightsEncText = $formdata->getValue('post', 'rightsEncText', 's', false);
                $rightsUseCC = $formdata->getValue('post', 'rightsUseCC', 'i', false);
                if ($readmeEncText == '') {
                    // readme is not filled
                    $msg = $msg . '<br/><font color=\'#ff0000\'>' . _MD_XNPSIMULATOR_README_REQUIRED . '</font>';
                    $result = false;
                }
                if ($rightsEncText == '' && $rightsUseCC == '0') {
                    // license is not filled
                    $msg = $msg . '<br/><font color=\'#ff0000\'>' . _MD_XNPSIMULATOR_RIGHTS_REQUIRED . '</font>';
                    $result = false;
                }
                break;
            }
        }
    }
    return $result;
}
/** check DetailInformation input
 * called from confirm/registered page
 */
function xnpdataCheckRegisterParameters(&$message)
{
    $xnpsid = $_SESSION['XNPSID'];
    $formdata =& xoonips_getutility('formdata');
    $messages = array();
    $experimenter = xoonips_get_multi_field_array_from_post('xnpdata', 'experimenter');
    $data_file = $formdata->getFile('data_file', false);
    $data_fileFileID = $formdata->getValue('post', 'data_fileFileID', 'i', false);
    $xoonipsCheckedXID = $formdata->getValue('post', 'xoonipsCheckedXID', 's', false);
    $readmeEncText = $formdata->getValue('post', 'readmeEncText', 's', false);
    $rightsEncText = $formdata->getValue('post', 'rightsEncText', 's', false);
    $rightsUseCC = $formdata->getValue('post', 'rightsUseCC', 'i', false);
    if (empty($experimenter)) {
        $messages[] = _MD_XNPDATA_EXPERIMENTER_REQUIRED;
    }
    if (empty($data_fileFileID) && empty($data_file['name'])) {
        $messages[] = _MD_XNPDATA_DATA_FILE_REQUIRED;
    }
    // require Readme and License if register to public indexes
    $xids = explode(',', $xoonipsCheckedXID);
    $indexes = array();
    if ($xids[0] != $xoonipsCheckedXID) {
        foreach ($xids as $i) {
            $index = array();
            if (xnp_get_index($xnpsid, $i, $index) == RES_OK) {
                $indexes[] = $index;
            } else {
                $messages[] = '<font color="#ff0000">' . xnp_get_last_error_string() . '</font>';
                $result = false;
                break;
            }
        }
    }
    if (count($indexes) > 0) {
        foreach ($indexes as $i) {
            if ($i['open_level'] <= OL_GROUP_ONLY) {
                if ($readmeEncText == '') {
                    // readme is not filled
                    $messages[] = '<font color="#ff0000">' . _MD_XNPDATA_README_REQUIRED . '</font>';
                }
                if ($rightsEncText == '' && $rightsUseCC == '0') {
                    // rights is not filled
                    $messages[] = '<font color="#ff0000">' . _MD_XNPDATA_RIGHTS_REQUIRED . '</font>';
                }
                break;
            }
        }
    }
    if (count($messages) == 0) {
        return true;
    }
    $message = "<br />\n" . implode("<br />\n", $messages);
    return false;
}