<?php
//===========================================================================
//* --    ~~               Shine Live Help                ~~    -- *
//===========================================================================
//           URL:   http://www.iradeon.com/shine.html    EMAIL: info@iradeon.com
//         Copyright (C) 2006 iRadeon Group Inc. (http://www.iradeon.com )
//             Shine is a derivative of Crafty Syntax Live Help,
//                     copyright (C) Eric Gerdes 2003--2006
// --------------------------------------------------------------------------
// LICENSE:
//     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;
//     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 in a file named LICENSE.txt .
// -------------------------------------------------------------------------- 

### crm_add_xscript.php
###
### If 'confirmed' is set, save a Shine transcript as a note attached to the
### desired Sugar thing. Otherwise, prompt for the thing to attach a transcript to.


## Bail unless we are part of a session.
require_once('crm_display_funcs.php');
require_once('admin_common.php');
validate_session($identity);
@session_start();
#$mydatabase->close_connect();

# Get our fun functions
require_once('crm_soap_funcs.php');
require_once('crm_display_funcs.php');

## Set up our variables.

# The transcript: the reason we are here
$xscript_id = (isset($_GET['xscript_id']) ? $_GET['xscript_id'] : null );

# search vars
$first_name = (isset($_POST['first_name']) ? $_POST['first_name'] : null );
$last_name = (isset($_POST['last_name']) ? $_POST['last_name'] : null );
$email = (isset($_POST['email']) ? $_POST['email'] : null );
$account = (isset($_POST['account']) ? $_POST['account'] : null );

# confirmation vars
$confirmed_p = (isset($_POST['confirmed_p']) ? $_POST['confirmed_p'] : null );
if (!$confirmed_p) {
    $confirmed_p = (isset($_SESSION['confirmed_p']) ? $_SESSION['confirmed_p'] : null );
}
$sugar_thing_id = (isset($_POST['sugar_thing_id']) ? $_POST['sugar_thing_id'] : null );
if (!$sugar_thing_id) {
    $sugar_thing_id = (isset($_SESSION['sugar_thing_id']) ? $_SESSION['sugar_thing_id'] : null );
}
$sugar_thing_type = (isset($_POST['sugar_thing_type']) ? $_POST['sugar_thing_type'] : null );
if (!$sugar_thing_type) {
    $sugar_thing_type = (isset($_SESSION['sugar_thing_type']) ? $_SESSION['sugar_thing_type'] : null );
}


## No transcript? We're done.
if (!$xscript_id) {
    exit("This page is for adding a transcript to Sugar, yet no
        transcript was specified.\n"
);
}

## TODO: this is debug stuff, remove it.

## Do the deed if 'confirmed' is set.
if ($confirmed_p)
{
    if (!$sugar_thing_id || !$sugar_thing_type) {
        print "Error: It looks to me that we tried to add a transcript to
            something that doesn't exist in Sugar."
;
    }

    $err = add_xscript($sugar_thing_type, $sugar_thing_id, $xscript_id, $mydatabase);
    if ($err) {
        print "Error: $err";
    }
    else {
        unset (
            $account, $_POST['account'],
            $first_name, $_POST['first_name'],
            $last_name, $_POST['last_name'],
            $email, $_POST['email'],
            $confirmed_p, $_POST['confirmed_p'],
            $sugar_thing_id, $_POST['sugar_thing_id'],
            $sugar_thing_type, $_POST['sugar_thing_type'],
            $_SESSION['sugar_thing_type'],
            $_SESSION['sugar_thing_id'],
            $_SESSION['confirmed_p'],
            $xscript_id, $_POST['xscript_id']
        );
        header("Location: crm_add_success.php");
exit;
    }
}
else 
{
    $vis_info = array (
        'account' => addslashes($account),
        'email' => addslashes($email),
        'first_name' => addslashes($first_name),
        'last_name' => addslashes($last_name)
    );

?>
<head>
<link title="new" rel="stylesheet" href="style.css" type="text/css">
<title>Archive to CRM</title>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo $lang['charset']; ?>" >
</head>
<script>
//top.outerWidth=600;
function goto_crm_create()
{
    var rand = Math.floor(Math.random()*1000);
    window.open( '', 'crm_create_window' + rand, 'width=530,height=400,scrollbars=1,resizable=1' );
    document.getElementById('crm_create_form').setAttribute('target','crm_create_window' + rand);
    document.getElementById('crm_create_form').submit();
}
</script>
<body bgcolor=#F9F9F7>
<table background=images/<?php echo $CSLH_Config['colorscheme'];?>/bk.gif cellspacing="0" cellpadding="0">
        <td width=100% background=images/<?php echo $CSLH_Config['colorscheme'];?>/top_trim.gif>
        <img src=images/blank.gif  height=8><b><font size=3><?php echo $lang['CRM_Archive']; ?></font></b></td>
        <td width=0% rowspan=0 valign=top><a href=http://www.iradeon.com/shine.html target=_blank
        ><img src=images/<?php echo $CSLH_Config['colorscheme'];?>/version.gif border=0 width=267 height=32></a></td>

</table>
<table height=300  width=500>
    <tr valign=top>
        <td align=left align=top>
            <table cellspacing=5 align=top width=200>
                <tr>     
                     <td align=top><strong><font size=3> Select Existing Record:</font></strong></td>
                </tr>
                 <tr>
                 <td><div><I>Select a link below to archive this chat transcript to an existing CRM record.</I></div></td>
            </table>

            <table cellspacing=5>   
                <tr align=top>
                    <td><? $action = 'crm_add_xscript.php'
                        ."?xscript_id=$xscript_id";
                        display_search_results(
                            visitor_lookup($vis_info), $action);
                     ?>
                     </td>
            </table>   
           
            <table cellspacing=5>
                <tr>       
                      <td align=left><B><font size=3> Search for a CRM record:</font></B></td>
                </tr>
                <tr>
                    <td align=left><i>Enter additional information and click Search</i></td>
                </tr>   
            </table>
            <table align=left cellspacing=5 width=150>
                 <tr>
       
                   <td width=100>
                       <? display_search_form($vis_info, $action);
                       ?>
                   </td>
                 </tr>   
            </table>
        </td>
        <td valign=top align=right>
            <table cellspacing=5 width=200>
   
               <form
                    action='crm_create.php?xscript_id=<?=$xscript_id?>'
                    id='crm_create_form'
                    method='post'>
   
                 <input type='hidden' name='account' value='<?= $account ?>'>
   
                 <input type='hidden' name='first_name' value='<?= $first_name ?>'>
   
                <input type='hidden' name='last_name' value='<?= $last_name ?>'>
   
                 <input type='hidden' name='email' value='<?= $email ?>'>
         </table>
       
       
     <table width=240 align=right height=100% bgcolor=#F1F1EB><tr valign=top><td>
        <table>
               <tr>
                <td width=50 align=left><B><font size=4>OR</font></B></td>
                 <td align=right><B><font size=3>Create new Record:</font></B></td>
               </tr>
         </table>
         <table cellspacing=0>
         
               <tr>
               <td><i>Use the following drop-down to create a new Account, Case, Contact, or Lead to associate this Transcript to.</i></td>
             </tr>
        </table>
        <table align=center cellspacing=5>
             <tr>
                <td>
                <select name='sugar_thing_type'>
                <option value="Accounts">account</A>
                <option value="Cases">case</A>
                <option value="Contacts">contact</A>
                <option value="Leads">lead</A>
                 </select>
                 </td>
             <td align=left>
                 <input type="button" value="Submit" onclick="javascript:goto_crm_create()">
             </td>
            </tr>
             <tr>   
       </table>
      </td>
      </tr>
      </table>
      </td>
    </tr>
 </table>

<tr>
   <th colspan=3>
   <img width="100%" src="images/nav_bot.gif" height="30">
   </th>
</tr>

</form>

</div>
<?php
}
?>