<?php
/* $Id: relations.php 80 2006-11-10 05:14:01Z hodaka $
 *  ------------------------------------------------------------------------ *
 *                XOOPS - PHP Content Management System                      *
 *                    Copyright (c) 2000 XOOPS.org                           *
 *                       <http://www.xoops.org/>                             *
 *  ------------------------------------------------------------------------ *
 *                     MyAddress - XOOPS addressbook                         *
 *                        <http://www.kuri3.net/>                            *
 *             Copyright (c) 2004 by hodaka <hodaka@kuri3.net>               *
 * ------------------------------------------------------------------------- *
 * This program is free software; you can redistribute it and/or modify      *
 * it under the terms of the GNU General Public License as published by      *
 * the Free Software Foundation; either version 2 of the License, or         *
 * (at your option) any later version.                                       *
 *                                                                           *
 * You may not change or alter any portion of this comment or credits        *
 * of supporting developers from this source code or any supporting          *
 * source code which is considered copyrighted (c) material of the           *
 * original comment or credit authors.                                       *
 *                                                                           *
 * This program is distributed in the hope that it will be useful,           *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
 * GNU General Public License for more details.                              *
 *                                                                           *
 * You should have received a copy of the GNU General Public License         *
 * along with this program; if not, write to the Free Software               *
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA  *
 */

//--------------------------------------------------------------------//
//              MyAddress maintenance table relations                 //
//--------------------------------------------------------------------//

include("admin_header.php");

$op = isset( $_POST['edit'] ) ? "edit" : (isset( $_POST['add'] ) ? "add" : "");

$PHP_SELF = $_SERVER['PHP_SELF'];
$selgrp = isset($_POST['selgrp']) ? intval( $_POST['selgrp'] ) : (isset($_GET['selgrp']) ? intval( $_GET['selgrp'] ) : 0);

$myts =& MyTextSanitizer::getInstance();

switch( $op ) {
    case "add":
        $title = $myts->makeTareaData4Save($_POST['title']);
        $weight = intval($_POST['weight']);
        $gid = intval($_POST['selgrp']);
        $newid = $xoopsDB->genId( $table_relations."_rid_seq" );

        $sql = sprintf("INSERT INTO %s ( rid, title, weight, gid ) VALUES ( %u, '%s', '%s', %u )", $table_relations, $newid, $title, $weight, $gid );
        $result = $xoopsDB->query( $sql ) or die( "DB error: ADD relations table." );

        redirect_header( "$PHP_SELF?selgrp=$selgrp", 2, _AM_MYADDRESS_ADDINGREL );
    break;

    case "edit":
        if (isset($_POST['rid']) && is_array($_POST['rid'])) {
            while (list($key,$val) = each($_POST['rid'])){
                if (isset($_POST['del']) && is_array($_POST['del']) && in_array($val, $_POST['del'])) {
                    deleteRel( $val );
                } else {
                    updateRel( $val, $_POST['title'][$key], $_POST['weight'][$key] );
                }
        	}
        }

        redirect_header( "$PHP_SELF?selgrp=$selgrp", 2, _AM_MYADDRESS_UPDATINGREL );
        exit;
    break;

    default:
        $member_handler =& xoops_gethandler('member');
        $group_list =& $member_handler->getGroupList();

        xoops_cp_header();
        include('./mymenu.php'); // GIJOE
//      OpenTable();

        echo "<h4 style='text-align:left;'>"._AM_MYADDRESS_RELATIONS."</h4><br />";

        $group_sel = _AM_MYADDRESS_GROUP." <select size=\"1\" name=\"selgrp\" onchange=\"location='".XOOPS_URL."/modules/myaddress/admin/relations.php?selgrp='+this.options[this.selectedIndex].value\">";

        foreach ($group_list as $k => $v) {
        // exclude if group_id < 4
//          if($k > 3) {
                $sel = '';
                if ($k == $selgrp) {
                    $sel = ' selected="selected"';
                }
                $group_sel .= '<option value="'.intval($k).'"'.$sel.'>'.$myts->htmlSpecialChars($v).'</option>';
//          }
        }
        $group_sel .= '</select> ';
        echo $group_sel;

        $sql = "SELECT rid, title, weight FROM $table_relations WHERE gid=$selgrp ORDER BY weight";
        $result = $xoopsDB->query( $sql );

		echo '<form name="edit" method="post" action="">';
		echo '<table width="50%" class="outer" cellpadding="4" cellspacing="1">';
		echo '	<tr valign="middle">';
		echo '		<th style="text-align:center">'._AM_MYADDRESS_TITLER.'</th>';
		echo '		<th style="text-align:center">'._AM_MYADDRESS_WEIGHT.'</th>';
		echo '		<th style="text-align:center">'._AM_MYADDRESS_DELETER.'</th></tr>';
		while (list($rid, $title, $weight) = $xoopsDB->fetchRow( $result ) ) {
			echo '	<tr valign="middle">';
			echo '		<td style="text-align:center"><input type="text" name="title[]" length="21" maxlength="50" value="'.$myts->htmlSpecialChars($title).'" style="ime-mode: active" /></td>';
			echo '		<td style="text-align:center"><input type="text" name="weight[]" length="6" maxlength="5" value="'.intval($weight).'" style="ime-mode: inactive" /></td>';
			echo '		<td style="text-align:center"><input type="checkbox" name="del[]" value="'.intval($rid).'" />';
			echo '		<input type="hidden" name="rid[]" value="'.intval($rid).'" /></td></tr>';
		}
		echo '	<tr valign="middle">';
		echo '		<input type="hidden" name="selgrp" value="'.$selgrp.'" />';
		echo '		<td colspan="3" style="text-align:center"><input type="submit" name="edit" value="'._AM_MYADDRESS_SEND.'" /></td></tr>';
		echo '</table><br />';
		echo '</form>';
		echo '<form name="add" method="post" action="">';
		echo '<table width="50%" class="outer" cellpadding="4" cellspacing="1">';
		echo '	<tr valign="middle">';
		echo '		<th style="text-align:center">'._AM_MYADDRESS_TITLER.'</th>';
		echo '		<th style="text-align:center">'._AM_MYADDRESS_WEIGHT.'</th></tr>';
		echo '	<tr valign="middle">';
		echo '		<td style="text-align:center"><input type="text" name="title" length="21" maxlength="50" style="ime-mode: active" /></td>';
		echo '		<td style="text-align:center"><input type="text" name="weight" length="6" maxlength="5" style="ime-mode: inactive" /></td></tr>';
		echo '	<tr valign="middle">';
		echo '		<input type="hidden" name="selgrp" value="'.$selgrp.'" />';
		echo '		<td colspan="2" style="text-align:center"><input type="submit" name="add" value="'._AM_MYADDRESS_ADD.'" /></td></tr>';
		echo '</table>';
		echo '</form>';
		xoops_cp_footer();
//      CloseTable();
    break;
}

function deleteRel( $rid ) {
    global $xoopsDB;
    global $table_relations;

    $result = $xoopsDB->query("DELETE FROM $table_relations WHERE rid=$rid") or die( "DB error: DELETE relations table." );
}
function updateRel( $rid, $title="", $weight=0 ) {
    global $xoopsDB;
    global $myts;
    global $table_relations;

    $title = $myts->makeTareaData4Save($title);
    $weight = intval($weight);

    $sql = "UPDATE $table_relations SET title='$title', weight=$weight WHERE rid=$rid";
    $result = $xoopsDB->query($sql) or die( "DB error: UPDATE relations table." );
}
