<?php
// ==================================================================================================
// person contact handling
// ==================================================================================================
// ==================================================================================================


if((isset($_GET["obj"]) and $_GET["obj"] == "person") || (isset($_POST["obj"]) and $_POST["obj"] == "person")){

    $contactObj = new Contact();
	$contactObj->templatesAreIn = $useTemplatesIn;

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

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

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

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

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

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

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

    if(isset($_GET["view"]) && $_GET["view"] == "pdfexport"){
	$contactObj->viewPDFExport();
	$contactObj->display('contact_pdfexport.tpl');
	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);
	$contactObj->display($appconf["gentemplates"] ."/delete_item.tpl");
	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();
	$contactObj->display("contact_list.tpl");
    exit();
}

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

    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);
	$compObj->display($appconf["gentemplates"] ."/delete_item.tpl");
	exit();
    }

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

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

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

    $compObj->viewList();
	$compObj->display('contact_comp_list.tpl');
    exit();
}

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

    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();
	$groupObj->display('contact_groups_edit.tpl');
	exit();
    }

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

    if(isset($_GET["view"]) && $_GET["view"] == "details"){
	$id = isset($_GET["id"])?(int) $_GET["id"]:0;
	$groupObj->viewDetails($id);
	$groupObj->display('contact_groups_details.tpl');
	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);
	$groupObj->display($appconf["gentemplates"] ."/delete_item.tpl");
	exit();
    }
	
    $groupObj->viewList();
	$groupObj->display('contact_groups_list.tpl');
    exit();
}

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

    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);
	 	$crmObj->display($appconf["gentemplates"] ."/delete_item.tpl");
       exit();
    }

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

    if (isset($_GET["view"]) && $_GET["view"] == "directnewform") {
	$crmObj->viewDirectNewForm();
	$crmObj->display('contact_crm_edit.tpl');
	exit();
    }

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

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

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

    $crmObj->viewList();
	$crmObj->display('contact_crm_list.tpl');
    exit();
}
?>
