<?php
/* $Id: editaddress.php 1 2006-06-07 08:07:41Z 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  *
 */

if (!defined('XOOPS_ROOT_PATH')) {
	exit();
}
//-------------------------------------------------------------------//
//           making form for add/update/delete myaddressbook         //
//-------------------------------------------------------------------//

function edit_address( $op, $zipconvert ) {

	global $myts, $xoopsDB, $cattree, $xoopsTpl;
	global $table_addressbook, $table_company;
	global $table_cat, $table_zipcode, $table_relations;
	global $myaddress_numperpage, $myaddress_catorder;
	global $isadmin, $myaddress_mid, $my_uid, $my_groups, $mod_url;
	global $mode, $cid, $aid;

	//-----------------------------------------------------------------//
	//                     get address by zipcode                      //
	//-----------------------------------------------------------------//

	if( $zipconvert != "" ) {
		if( $zipconvert == "ziptoadrs" ) {
			$zipselkey = isset($_POST['myzipcode']) ? $myts->makeTboxData4Show($_POST['myzipcode']) : "";
			$ziparray = zipconvert( $zipconvert, $zipselkey );
		} else {
			$zipselkey['address1'] = isset($_POST['myaddress1']) ? $myts->makeTboxData4Show($_POST['myaddress1']) : "";
			$zipselkey['address2'] = isset($_POST['myaddress2']) ? $myts->makeTboxData4show($_POST['myaddress2']) : "";
			$ziparray = zipconvert( $zipconvert, $zipselkey );
		}
	} else {
		$ziparray = array();
	}

	switch ( $op ) {
	//-----------------------------------------------------------------//
	//                     for add a new myaddress                     //
	//-----------------------------------------------------------------//
	case "add":
		$heading = _MD_MYADDRESS_ADDMYADDRESS;

		$aid = 0;
		$disclosed = isset($_POST['disclosed']) ? $_POST['disclosed'] : 1 ;
		$relations = isset($_POST['relations']) ? trim($_POST['relations']) : "" ;
		$first_name = isset($_POST['first_name']) ? $myts->makeTboxData4Edit($_POST['first_name']) : "" ;
		$last_name = isset($_POST['last_name']) ? $myts->makeTboxData4Edit($_POST['last_name']) : "" ;
		$first_name_jh = isset($_POST['first_name_jh']) ? $myts->makeTboxData4Edit($_POST['first_name_jh']) : "" ;
		$last_name_jh = isset($_POST['last_name_jh']) ? $myts->makeTboxData4Edit($_POST['last_name_jh']) : "" ;
		$first_name2 = isset($_POST['first_name2']) ? $myts->makeTboxData4Edit($_POST['first_name2']) : "" ;
		$myzipcode = isset($_POST['myzipcode']) ? $myts->makeTboxData4Edit($_POST['myzipcode']) : "" ;
		$myaddress1 = isset($_POST['myaddress1']) ? $myts->makeTboxData4Edit($_POST['myaddress1']) : "" ;
		$myaddress2 = isset($_POST['myaddress2']) ? $myts->makeTboxData4Edit($_POST['myaddress2']) : "" ;
		$myaddress3 = isset($_POST['myaddress3']) ? $myts->makeTboxData4Edit($_POST['myaddress3']) : "" ;
		$myphone = isset($_POST['myphone']) ? $myts->makeTboxData4Edit($_POST['myphone']) : "" ;
		$myfax = isset($_POST['myfax']) ? $myts->makeTboxData4Edit($_POST['myfax']) : "" ;
		$mycellphone1 = isset($_POST['mycellphone1']) ? $myts->makeTboxData4Edit($_POST['mycellphone1']) : "" ;
		$mycellphone2 = isset($_POST['mycellphone2']) ? $myts->makeTboxData4Edit($_POST['mycellphone2']) : "" ;
		$myemail1 = isset($_POST['myemail1']) ? $myts->makeTboxData4Edit($_POST['myemail1']) : "" ;
		$myemail2 = isset($_POST['myemail2']) ? $myts->makeTboxData4Edit($_POST['myemail2']) : "" ;
		$myemail3 = isset($_POST['myemail3']) ? $myts->makeTboxData4Edit($_POST['myemail3']) : "" ;
		$myemail4 = isset($_POST['myemail4']) ? $myts->makeTboxData4Edit($_POST['myemail4']) : "" ;
		$myweb = isset($_post['myweb']) ? $myts->makeTboxData4Edit($_POST['myweb']) : "" ;
		if( $myweb == "http://" ) $myweb = "";
		$mycomments = isset($_POST['mycomments']) ? $myts->makeTareaData4Edit($_POST['mycomments']) : "" ;
		$c_id = 0;
		$cname = isset($_POST['cname']) ? $myts->makeTboxData4Show($_POST['cname']) : "" ;
		$cdivision = isset($_POST['cdivision']) ? $myts->makeTboxData4Show($_POST['cdivision']) : "" ;
		$company = isset($_POST['company']) ? $myts->makeTboxData4Show($_POST['company']) : "" ;
		$cdepart = isset($_POST['cdepart']) ? $myts->makeTboxData4Edit($_POST['cdepart']) : "" ;
		$ctitle = isset($_POST['ctitle']) ? $myts->makeTboxData4Edit($_POST['ctitle']) : "" ;
		$cphone = isset($_POST['cphone']) ? $myts->makeTboxData4Edit($_POST['cphone']) : "" ;
		$cfax = isset($_POST['cfax']) ? $myts->makeTboxData4Edit($_POST['cfax']) : "" ;
		$ccphone = "";
		$ccfax = "";
		$updated = "";
	break;

	//-----------------------------------------------------------------//
	//                     for update myaddress                        //
	//-----------------------------------------------------------------//
	case "edit":
		$heading = _MD_MYADDRESS_EDITMYADDRESS;
		if ( $zipconvert == "" ) {
			list( $aid, $cid, $disclosed, $relations, $first_name, $last_name, $first_name_jh, $last_name_jh, $first_name2, $myzipcode, $myaddress1, $myaddress2, $myaddress3, $myphone, $mycellphone1, $mycellphone2, $myfax, $myemail1, $myemail2, $myemail3, $myemail4, $myweb, $mycomments, $c_id, $cname, $cdivision, $cdepart, $ctitle, $cphone, $cfax, $ccphone, $ccfax, $updated, $uid ) = get_myaddress( $aid );
			$cphone = ($cphone == "")? $ccphone : $cphone;
			$cfax = ($cfax == "")? $ccfax : $cfax;

// 04.06.16 added for text sanitalization
			$aid = intval($aid);
			$cid = intval($cid);
			$disclosed = intval($disclosed);
			$relations = $myts->makeTboxData4Edit($relations);
			$first_name = $myts->makeTboxData4Edit($first_name);
			$last_name = $myts->makeTboxData4Edit($last_name);
			$first_name_jh = $myts->makeTboxData4Edit($first_name_jh);
			$last_name_jh = $myts->makeTboxData4Edit($last_name_jh);
			$first_name2 = $myts->makeTboxData4Edit($first_name2);
			$myzipcode = $myts->makeTboxData4Edit($myzipcode);
			$myaddress1 = $myts->makeTboxData4Edit($myaddress1);
			$myaddress2 = $myts->makeTboxData4Edit($myaddress2);
			$myaddress3 = $myts->makeTboxData4Edit($myaddress3);
			$myphone = $myts->makeTboxData4Edit($myphone);
			$myfax = $myts->makeTboxData4Edit($myfax);
			$mycellphone1 = $myts->makeTboxData4Edit($mycellphone1);
			$mycellphone2 = $myts->makeTboxData4Edit($mycellphone2);
			$myemail1 = $myts->makeTboxData4Edit($myemail1);
			$myemail2 = $myts->makeTboxData4Edit($myemail2);
			$myemail3 = $myts->makeTboxData4Edit($myemail3);
			$myemail4 = $myts->makeTboxData4Edit($myemail4);
			$myweb = $myts->makeTboxData4Edit($myweb);
			$mycomments = $myts->makeTareaData4Edit($mycomments);
			$c_id = intval($c_id);
			$cname = $myts->makeTboxData4Show($cname);
			$cdivision = $myts->makeTboxData4Show($cdivision);
			$cdepart = $myts->makeTboxData4Edit($cdepart);
			$ctitle = $myts->makeTboxData4Edit($ctitle);
			$cphone = $myts->makeTboxData4Edit($cphone);
			$cfax = $myts->makeTboxData4Edit($cfax);
			
		} else {

			$disclosed = intval($_POST['disclosed']);
			$relations = $myts->makeTboxData4PreviewInForm($_POST['relations']);
			$first_name = $myts->makeTboxData4PreviewInForm($_POST['first_name']);
			$last_name = $myts->makeTboxData4PreviewInForm($_POST['last_name']);
			$first_name_jh = $myts->makeTboxData4PreviewInForm($_POST['first_name_jh']);
			$last_name_jh = $myts->makeTboxData4PreviewInForm($_POST['last_name_jh']);
			$first_name2 = $myts->makeTboxData4PreviewInForm($_POST['first_name2']);
			$myzipcode = $myts->makeTboxData4PreviewInForm($_POST['myzipcode']);
			$myaddress1 = $myts->makeTboxData4PreviewInForm($_POST['myaddress1']);
			$myaddress2 = $myts->makeTboxData4PreviewInForm($_POST['myaddress2']);
			$myaddress3 = $myts->makeTboxData4PreviewInForm($_POST['myaddress3']);
			$myphone = $myts->makeTboxData4PreviewInForm($_POST['myphone']);
			$myfax = $myts->makeTboxData4PreviewInForm($_POST['myfax']);
			$mycellphone1 = $myts->makeTboxData4PreviewInForm($_POST['mycellphone1']);
			$mycellphone2 = $myts->makeTboxData4PreviewInForm($_POST['mycellphone2']);
			$myemail1 = $myts->makeTboxData4PreviewInForm($_POST['myemail1']);
			$myemail2 = $myts->makeTboxData4PreviewInForm($_POST['myemail2']);
			$myemail3 = $myts->makeTboxData4PreviewInForm($_POST['myemail3']);
			$myemail4 = $myts->makeTboxData4PreviewInForm($_POST['myemail4']);
			$myweb = $myts->makeTboxData4PreviewInForm($_POST['myweb']);
			if( $myweb == "http://" ) $myweb = "";
			$mycomments = $myts->makeTareaData4PreviewInForm($_POST['mycomments']);
			$c_id = intval($_POST['c_id']);
			$cname = $myts->makeTboxData4Preview($_POST['cname']);
			$cdivision = $myts->makeTboxData4Preview($_POST['cdivision']);
			$cdepart = $myts->makeTboxData4PreviewInForm($_POST['cdepart']);
			$ctitle = $myts->makeTboxData4PreviewInForm($_POST['ctitle']);
			$cphone = $myts->makeTboxData4PreviewInForm($_POST['cphone']);
			$cfax = $myts->makeTboxData4PreviewInForm($_POST['cfax']);
			$updated = $_POST['updated'];
		}

	break;

	/*-----------------------------------------------------------------*/
	default:
	break;
	}

	//-----------------------------------------------------------------//
	//              assign to the template                             //
	//-----------------------------------------------------------------//

	// some hidden items
	$addresses['mode'] = $mode;
	$addresses['cid'] = $cid;
	$addresses['op'] = $op;
	$addresses['aid'] = $aid;
	$addresses['c_id'] = $c_id;

	$addresses['lang_categorypage'] = "<A HREF='viewcat.php?mode=".$mode."&cid=".$cid."'>"._MD_MYADDRESS_SEARCH."</A>";

	$addresses['lang_editmyaddress'] = $heading;

	$addresses['lang_myaddresscat'] = _MD_MYADDRESS_MYADDRESSCAT;
	$tree = $cattree->getChildTreeArray(0,$myaddress_catorder);
	$tree = push_gperm_array( $tree, "myadrs_category" );
	foreach( $tree as $leaf ) {
		$leaf['prefix'] = substr($leaf['prefix'], 0, -1);
		$leaf['prefix'] = str_replace(".","--",$leaf['prefix']);
		$cidselbox['label'] = $leaf['prefix'].$leaf['title'];
		$cidselbox['value'] = $leaf['cid'];
		$cidselboxdata[] = $cidselbox;
	}
	$addresses['cidselboxdata'] = $cidselboxdata;

	$addresses['lang_disclosed'] = _MD_MYADDRESS_DISCLOSED;
	$addresses['disclosed'] = $disclosed;
	$addresses['lang_yes'] = _MD_MYADDRESS_YES;
	$addresses['lang_no'] = _MD_MYADDRESS_NO;

	// get relation selelect box by gid
	$addresses['lang_relations'] = _MD_MYADDRESS_RELATIONS;
	$addresses['relations'] = $relations;
	$addresses['clsselboxdata'] = getSelBoxRelations();

	$addresses['lang_fullname'] = _MD_MYADDRESS_FULLNAME;
	$addresses['lang_firstname'] = _MD_MYADDRESS_FIRSTNAME;
	$addresses['lang_first_name2'] = _MD_MYADDRESS_FIRSTNAME2;
	$addresses['lang_lastname'] = _MD_MYADDRESS_LASTNAME;
	$addresses['lang_fullname_jh'] = _MD_MYADDRESS_FULLNAME_JH;
	$addresses['lang_must_jh'] = _MD_MYADDRESS_MUST_JH;
	$addresses['last_name'] = $last_name;
	$addresses['first_name'] = $first_name;
	$addresses['last_name_jh'] = $last_name_jh;
	$addresses['first_name_jh'] = $first_name_jh;
	$addresses['first_name2'] = $first_name2;

	//------------------------------------------------------------------//
	//       	 search results of zipcode                            //
	//------------------------------------------------------------------//
	$addresses['zipsearchresult'] = "";
	if ( $zipconvert != "" ) {
		if ( !isset( $ziparray ) || count( $ziparray ) == 0 ) {
			$addresses['zipsearchresult'] = "&nbsp;<B><FONT COLOR='red'>["._MD_MYADDRESS_NOFOUND."]</FONT></B>";
		} elseif ( count( $ziparray ) < 2 ) {
			$myzipcode = substr( $ziparray[0]['zipcode'], 0, 3 ) . "-" . substr( $ziparray[0]['zipcode'], 3, 4 );
			$myaddress1 = trim( $ziparray[0]['pref'] );
			$myaddress2 = trim( $ziparray[0]['address2'] );
		} else {
			if ( $zipconvert == "adrstozip" ) {
				if (!isset($_SESSION)) $_SESSION = array();
				$_SESSION['adrs1'] = $myaddress1;
				$_SESSION['adrs2'] = $myaddress2;
				$addresses['zipsearchresult'] = "&nbsp;<B>[<A HREF='javascript:openWithSelfMain(\"searchmore.php?form=address\",\"searchmore\",400,400);'>".sprintf( _MD_MYADDRESS_SEARCHMORE, count($ziparray) )."</A>]</B>";
			} else {
				$addresses['zipsearchresult'] = "&nbsp;<B>[<A HREF=\"javascript:openWithSelfMain('searchmore.php?form=address&amp;zip=".$myzipcode."','searchmore',400,400);\">".sprintf( _MD_MYADDRESS_SEARCHMORE, count($ziparray) )."</A>]</B>";
			}
		}
	}

	$addresses['lang_zipcode'] = _MD_MYADDRESS_ZIPCODE;
	$addresses['myzipcode'] = $myzipcode;
	$addresses['lang_searchzip'] = _MD_MYADDRESS_SEARCHZIP;
	$addresses['lang_searchadrs'] = _MD_MYADDRESS_SEARCHADRS;

	$addresses['lang_address1'] = _MD_MYADDRESS_ADDRESS1;
	$addresses['myaddress1'] = $myaddress1;
	$addresses['lang_address2'] = _MD_MYADDRESS_ADDRESS2;
	$addresses['myaddress2'] = $myaddress2;
	$addresses['lang_address3'] = _MD_MYADDRESS_ADDRESS3;
	$addresses['myaddress3'] = $myaddress3;

	$addresses['lang_phonefax'] = _MD_MYADDRESS_PHONEFAX;
	$addresses['lang_phone'] = _MD_MYADDRESS_PHONE;
	$addresses['lang_fax'] = _MD_MYADDRESS_FAX;
	$addresses['myphone'] = $myphone;
	$addresses['myfax'] = $myfax;

	$addresses['lang_mycellphone'] = _MD_MYADDRESS_CELLPHONE;
	$addresses['mycellphone1'] = $mycellphone1;
	$addresses['mycellphone2'] = $mycellphone2;

	$addresses['lang_myemail'] = _MD_MYADDRESS_EMAIL;
	$addresses['myemail1'] = $myemail1;
	$addresses['myemail2'] = $myemail2;
	$addresses['myemail3'] = $myemail3;
	$addresses['myemail4'] = $myemail4;

	$addresses['lang_myweb'] = _MD_MYADDRESS_WEB;
	$addresses['myweb'] = $myweb;

	$addresses['lang_mycomments'] = _MD_MYADDRESS_COMMENTS;
	$addresses['mycomments'] = $mycomments;
	
	$addresses['lang_co_name'] = _MD_MYADDRESS_COMPANY;
	$addresses['lang_select_co'] = _MD_MYADDRESS_SELECT_CO;
	$addresses['select_co'] = "setcompany.php?cid=".$cid."&amp;op=list&amp;c_id=".$c_id;
	$addresses['searchco'] = "searchco";
	$addresses['company'] = $cname." ".$cdivision;
	$addresses['cname'] = $cname;
	$addresses['cdivision'] = $cdivision;

	$addresses['lang_cdepart'] = _MD_MYADDRESS_DEPART;
	$addresses['cdepart'] = $cdepart;

	$addresses['lang_ctitle'] = _MD_MYADDRESS_COTITLE;
	$addresses['ctitle'] = $ctitle;

	$addresses['cphone'] = $cphone;
	$addresses['cfax'] = $cfax;

	$addresses['lang_updated'] = _MD_MYADDRESS_UPDATED;
	if( $updated == "00000000000000" || $updated == "" ) {
		$addresses['rend_updated'] = "";
	} else {
		$rend_updated = convert_timestamp ( $updated );
		$addresses['rend_updated'] = date("Y/m/d H:i:s", $rend_updated);
	}
	$addresses['updated'] = $updated;

	$addresses['lang_update'] = _MD_MYADDRESS_UPDATE;
	$addresses['lang_delete'] = _MD_MYADDRESS_DELETE;
	$addresses['lang_add'] = _MD_MYADDRESS_NEW;
	$addresses['lang_back'] = _MD_MYADDRESS_BACK;

	return $addresses;
}
?>
