xoonips_show_error_if_index_locked($index_id, $xid);
 }
 reset($check);
 while (list($key, $index_id) = each($check)) {
     $index_id = (int) $index_id;
     $index = array();
     $result = xnp_get_index($xnpsid, $index_id, $index);
     if ($result != RES_OK) {
         redirect_header(XOOPS_URL . '/index.php', 3, "ERROR");
         exit;
     }
     $oldPathString = xoonipsGetPathString($xnpsid, $index_id);
     if ($op == 'rename') {
         $notification_context = xoonips_notification_before_user_index_renamed($index_id);
         $new_index_name = encodeMacSafari2Server($rename[$index_id]);
         list($within, $without) = xnpTrimString($new_index_name, $lengths['title']);
         // warning, if string length is too long
         if (strlen($without)) {
             $error_messages[] = sprintf(_MD_XOONIPS_INDEX_TITLE_EXCEEDS, $new_index_name);
             continue;
         }
         // warning, if title is empty
         if (strlen($new_index_name) == 0) {
             $error_messages[] = _MD_XOONIPS_INDEX_TITLE_EMPTY;
             continue;
         }
         // Warning, if there is the same name of index.
         $indexes = array();
         $result = xnp_get_indexes($xnpsid, $xid, array(), $indexes);
         if ($result != RES_OK) {
             redirect_header(XOOPS_URL . '/index.php', 3, "ERROR");
/**
 * return template variables for 'xoonips_multiple_filed_confirm' template
 * @param XooNIpsTableObject[] orm to get template vars
 * @param string $field_name orm field name that is used as value to show.
 */
function xoonips_get_multiple_field_template_vars($ormObjects, $module, $name)
{
    $field_handler =& xoonips_getormhandler($module, $name);
    $lengths = xnpGetColumnLengths($field_handler->getTableName());
    $vars = array('table_name' => $field_handler->getTableName(), 'name' => array('primary_key' => $field_handler->getKeyName(), 'text' => $name, 'order' => $name . '_order'), 'objects' => array());
    foreach ($ormObjects as $orm) {
        list($within, $without) = xnpTrimString($orm->getVar($name, 's'), $lengths[$name], _CHARSET);
        $vars['objects'][] = array('primary_key' => array('name' => $field_handler->getKeyName(), 'value' => $orm->getVar($field_handler->getKeyName(), 's')), 'text' => array('name' => $name, 'within' => empty($within) ? '' : $within, 'without' => empty($without) ? '' : $without, 'value' => $orm->getVar($name, 's')), 'order' => array('name' => "{$name}_order", 'value' => $orm->get("{$name}_order")));
    }
    $vars['num'] = count($vars['objects']);
    return $vars;
}
/**
 * Importing indexes to a index that is specified by $parent_index_id.
 * Associations of pseudo ID and Real index ID are sotred to $id_table.
 *
 * @param parent_index_id index_id that indexes is imported to.
 * @param $indexes array of index information to be imported.
 * $indexes = array(
 *                   array( 'titles' => array( TITLE1, TITLE2, ... )
 *                          'parent_id' => pseudo id of parent index
 *                          'item_id' => pseudo id of own index
 *                          'child' => array( [0] => array( 'titles' => ..., 'parent_id' => ..., 'child' => ....)
 *                                            [1] => array( same above ),
 *                                            ....
 *                           )
 *                         ),
 *                   array( 'titles' => array( TITLE1, TITLE2, ... )
 *                          same above ... ),
 *                   ...
 *                   );
 * @param id_table reference of associative array for output( [pseudo id] => [real index id] )
 * @return no return value.
 */
function _xoonips_import_index($parent_index_id, &$indexes, &$id_table)
{
    $xnpsid = $_SESSION['XNPSID'];
    $lengths = xnpGetColumnLengths('xoonips_item_title');
    $unicode =& xoonips_getutility('unicode');
    foreach ($indexes as $index) {
        foreach ($index['titles'] as $k => $title) {
            list($index['titles'][$k], $dummy) = xnpTrimString($unicode->decode_utf8($title, xoonips_get_server_charset(), 'h'), $lengths['title'], 'UTF-8');
        }
        $child = array();
        // numbers of same index name
        $cnt = 0;
        $index_id = 0;
        if (xnp_get_indexes($xnpsid, $parent_index_id, array(), $child) == RES_OK) {
            foreach ($child as $i) {
                $diff = array_diff($i['titles'], $index['titles']);
                if (empty($diff)) {
                    // true if $index have only same names of $i ( $i['titles'] == $index['titles'] )
                    $cnt++;
                    $index_id = $i['item_id'];
                }
            }
        }
        if ($cnt == 1) {
            $id_table[$index['index_id']] = $index_id;
        } else {
            $insert_index = array();
            $insert_index['titles'] = $index['titles'];
            $insert_index['parent_index_id'] = $parent_index_id;
            $result = xnp_insert_index($xnpsid, $insert_index, $index_id);
            if ($result != RES_OK) {
                break;
            }
            $id_table[$index['index_id']] = $index_id;
            // record event log
            $mydirname = basename(dirname(__DIR__));
            $event_handler =& xoonips_getormhandler('xoonips', 'event_log');
            $event_handler->recordInsertIndexEvent($index_id);
        }
        if (array_key_exists('child', $index)) {
            _xoonips_import_index($index_id, $index['child'], $id_table);
        }
    }
}
/**
 * @param $assoc: 連想配列.
 *        array( column_name => array( 'value' => value ), ... )
 * @param $table_wo_prefix: prefixを除いたテーブル名
 * @param $names: チェックするカラム名.  array( 'readme', 'rights' ) など
 * $assoc に value, within, without, html_string を書く。
 *          array( column_name => array( 'value'=>value, 'within'=>within, 'without'=>without, 'html_string'=>html_string ), ... )
 */
function xnpConfirmHtml(&$assoc, $table_wo_prefix, $names = null, $enc = null)
{
    $textutil =& xoonips_getutility('text');
    $lengths = xnpGetColumnLengths($table_wo_prefix);
    if ($lengths == false) {
        return false;
    }
    foreach ($lengths as $name => $len) {
        //echo "xnpTrimColumn: name=$name len=$len type=$type <br />\n";
        if (isset($assoc[$name]) && (is_null($names) || in_array($name, $names))) {
            $assoc[$name]['html_string'] = $textutil->html_special_chars($assoc[$name]['value']);
            list($assoc[$name]['within'], $assoc[$name]['without']) = xnpTrimString($assoc[$name]['value'], $len, $enc);
            $assoc[$name]['value'] = xnpWithinWithoutHtml($assoc[$name]['within'], $assoc[$name]['without']);
        }
    }
}