<?php
/* $Id: index.php,v 1.142 2003/12/12 12:52:27 k-fish Exp $ */

$myEnv["module"] = "contact";
$myEnv["output"] = "html";

if((isset($_POST["op"]) && ($_POST["op"] == "exportpdf")) ||
   (isset($_GET['view']) && (strstr($_GET['view'], 'vcardexport')))) session_cache_limiter("public");

require_once("../../config.inc.php");
require_once(INCLUDEPATH . "container.inc.php");
require_once("inc/contact.class.php");
require_once("inc/company.class.php");
require_once("inc/contactgroup.class.php");
require_once("inc/crm.class.php");

// adding file revision into global array
if(function_exists("revisionInit")) revisionInit("\$Revision: 1.142 $", __FILE__);

// fixing missing obj variable until mgw provides this.
if(!isset($_GET["obj"]) && !isset($_POST["obj"])) $_GET["obj"] = "person";

// ==================================================================================================
// person contact handling
// ==================================================================================================
// ==================================================================================================
if((isset($_GET["obj"]) and $_GET["obj"] == "person") || (isset($_POST["obj"]) and $_POST["obj"] == "person")){
    $contactObj = new Contact();

    if (isset($_GET["view"]) && $_GET["view"] == "newform") {
	$contactObj->viewNewForm();
	exit();
    }

    if (isset($_GET["view"]) && $_GET["view"] == "editform") {
	$id = isset($_GET["id"])?(int) $_GET["id"]:0;
	$contactObj->viewEditForm($id);
	exit();
    }

    if(isset($_GET["view"]) && $_GET["view"] == "details"){
	$id = isset($_GET["id"])?(int) $_GET["id"]:0;
	$contactObj->viewDetails($id);
	exit();
    }

    if (isset($_GET["view"]) && $_GET["view"] == "vcardimport"){
	$contactObj->viewCardImport();
	exit();
    }

    if (isset($_GET["view"]) && $_GET["view"] == "vcardexportall"){
	$contactObj->viewCardExport(-1);
    }

    if (isset($_GET["view"]) && $_GET["view"] == "vcardexport"){
	$id = isset($_GET["id"])?(int) $_GET["id"]:0;
	$contactObj->viewCardExport($id);
    }

    if(isset($_GET["view"]) && $_GET["view"] == "lemails"){
	$contactObj->viewEmailList($id);
	exit();
    }

    if(isset($_GET["view"]) && $_GET["view"] == "pdfexport"){
	$contactObj->viewPDFExport();
	exit();
    }

    if(isset($_POST["confirmation_yes"])) {
	$id = isset($_POST["itemid"])?(int) $_POST["itemid"]:0;
	$contactObj->opDelete($id);
	redirect(ROOTURL. "modules/contact/index.php?obj=person&".SID);
    }
    elseif (isset($_GET["view"]) && $_GET["view"] == "delete"){
	$id = isset($_GET["id"])?(int) $_GET["id"]:0;
	$contactObj->viewDelete($id);
	exit();
    }

    if(isset($_POST["op"]) && $_POST["op"] == "exportpdf"){
	$contactObj->opPDFExport();
	exit();
    }

    if (isset($_POST["op"]) && $_POST["op"] == "vimport"){
	$contactObj->opCardImport($_FILES["userfile"]);
	redirect(ROOTURL. "modules/contact/index.php?obj=person&".SID);
    }

    if (isset($_POST["massupdate"])){
	$contactObj->opMassUpdate($_POST);
	redirect(ROOTURL. "modules/contact/index.php?obj=person&".SID);
    }

    if (isset($_POST["op"]) && $_POST["op"] == "create"){
	$contactObj->opCreate();
	redirect(ROOTURL. "modules/contact/index.php?obj=person&".SID);
    }

    if(isset($_POST["op"]) && $_POST["op"] == "edit"){
	$contactObj->opUpdate();
	redirect(ROOTURL. "modules/contact/index.php?obj=person&".SID);
    }
   
    $contactObj->viewList();
    exit();
}

// ==================================================================================================
// contact company handling
// ==================================================================================================
// ==================================================================================================
if((isset($_GET["obj"]) and $_GET["obj"] == "company") || (isset($_POST["obj"]) and $_POST["obj"] == "company")){
    $compObj = new Company();

    if (isset($_POST["massupdate"])){
	$compObj->opMassUpdate($_POST);
	redirect(ROOTURL. "modules/contact/index.php?obj=company&".SID);
    }

    if(isset($_POST["op"]) && $_POST["op"] == "create"){
	$compObj->opCreate($_POST);
	redirect(ROOTURL. "modules/contact/index.php?obj=company&".SID);
    }
	 
    if (isset($_POST["op"]) && $_POST["op"] == "edit"){
	$compObj->opUpdate($_POST);
	redirect(ROOTURL. "modules/contact/index.php?obj=company&".SID);
    }
   
    if(isset($_POST["confirmation_yes"])) {
	$id = isset($_POST["itemid"])?(int) $_POST["itemid"]:0;
	$compObj->opDelete($id);
	redirect(ROOTURL. "modules/contact/index.php?obj=company&".SID);
    }
    elseif (isset($_GET["view"]) && $_GET["view"] == "delete"){
	$id = isset($_GET["id"])?(int) $_GET["id"]:0;
	$compObj->viewDelete($id);
	exit();
    }

    if (isset($_GET["view"]) && $_GET["view"] == "newform") {
	$compObj->viewNewForm();
	exit();
    }

    if (isset($_GET["view"]) && $_GET["view"] == "editform") {
	$id = isset($_GET["id"])?(int) $_GET["id"]:0;
	$compObj->viewEditForm($id);
	exit();
    }

    if (isset($_GET["view"]) && $_GET["view"] == "details") {
	$id = isset($_GET["id"])?(int) $_GET["id"]:0;
	$compObj->viewDetails($id);
	exit();
    }

    $compObj->viewList();
    exit();
}

// ==================================================================================================
// contact group handling
// ==================================================================================================
// ==================================================================================================
if((isset($_GET["obj"]) and $_GET["obj"] == "group") || (isset($_POST["obj"]) and $_POST["obj"] == "group")){
    $groupObj = new ContactGroup();

    if (isset($_POST["op"]) && $_POST["op"] == "create"){
	$groupObj->opCreate();
	redirect(ROOTURL. "modules/contact/index.php?obj=group&".SID);
    }

    if(isset($_POST["op"]) && $_POST["op"] == "edit"){
	$id = isset($_POST["id"])?(int) $_POST["id"]:0;
	$groupObj->opUpdate($id);
	redirect(ROOTURL. "modules/contact/index.php?obj=group&".SID);
    }
   
    if (isset($_GET["view"]) && $_GET["view"] == "newform") {
	$groupObj->viewNewForm();
	exit();
    }

    if (isset($_GET["view"]) && $_GET["view"] == "editform") {
	$id = isset($_GET["id"])?(int) $_GET["id"]:0;
	$groupObj->viewEditForm($id);
	exit();
    }

    if(isset($_GET["view"]) && $_GET["view"] == "details"){
	$id = isset($_GET["id"])?(int) $_GET["id"]:0;
	$groupObj->viewDetails($id);
	exit();
    }

    if(isset($_POST["confirmation_yes"])) {
	$id = isset($_POST["itemid"])?(int) $_POST["itemid"]:0;
	$groupObj->opDelete($id);
	redirect(ROOTURL. "modules/contact/index.php?obj=group&".SID);
    }
    elseif (isset($_GET["view"]) && $_GET["view"] == "delete"){
	$id = isset($_GET["id"])?(int) $_GET["id"]:0;
	$groupObj->viewDelete($id);
	exit();
    }
	
    $groupObj->viewList();
    exit();
}

// ==================================================================================================
// CRM handling
// ==================================================================================================
// ==================================================================================================
if((isset($_GET["obj"]) and $_GET["obj"] == "crm") || (isset($_POST["obj"]) and $_POST["obj"] == "crm")){
    $crmObj = new CRM();

    if(isset($_POST["op"]) && $_POST["op"] == "create"){
	$crmObj->opCreate();
	redirect(ROOTURL. "modules/contact/index.php?obj=crm&".SID);
    }

    if (isset($_POST["op"]) && $_POST["op"] == "edit"){
	$id = isset($_POST["id"])? (int) $_POST["id"]:0;
	$crmObj->opUpdate($id);
	redirect(ROOTURL. "modules/contact/index.php?obj=crm&".SID);
    }

    if(isset($_POST["confirmation_yes"])) {
        $id = isset($_POST["itemid"])?(int) $_POST["itemid"]:0;
        $crmObj->opDelete($id);
	redirect(ROOTURL. "modules/contact/index.php?obj=crm&".SID);
    }
    elseif (isset($_GET["view"]) && $_GET["view"] == "delete"){
        $id = isset($_GET["id"])?(int) $_GET["id"]:0;
        $crmObj->viewDelete($id);
        exit();
    }

    if (isset($_GET["view"]) && $_GET["view"] == "newform") {
	$crmObj->viewNewForm();
	exit();
    }

    if (isset($_GET["view"]) && $_GET["view"] == "details") {
	$id = isset($_GET["id"])?(int) $_GET["id"]:0;
	$crmObj->viewDetails($id);
	exit();
    }

    if (isset($_GET["view"]) && $_GET["view"] == "editform") {
	$id = isset($_GET["id"])?(int) $_GET["id"]:0;
	$crmObj->viewEditForm($id);
	exit();
    }

    if (isset($_GET["view"]) && $_GET["view"] == "detail_list"){	
	$id = isset($_GET["id"])?(int) $_GET["id"]:0;
	$crmObj->viewDetailsList($id);
	exit();
    }

    $crmObj->viewList();
    exit();
}
?>
