<?php
if (!$_POST){
  //If no POST, return an error
  return;
} elseif($_POST["contactId"]){
  require_once('library/src/isdk.php');

  $app = new iSDK;
  //if ($app->cfgCon("ss160")){
  if ($app->cfgCon("ifconnection")){

    $contactId = $_POST['contactId'];

    //Gets Total Show Cost
    $returnFields = array("_TotalShowCost");
    $result = $app->loadCon($contactId, $returnFields);

    //Gets Contact ID and Id from Lead. Contact ID is the ID of the user in the Contacts table
    /*$returnFields = array("ContactId", "Id", "_TotalShowCost");
    $result = $app->dsLoad("Lead", $contactId, $returnFields);
    echo print_r($result);/**/


    if ($result){
      //echo print_r($result);

      $totalShowCost = $result["_TotalShowCost"];
      //echo $totalShowCost;

      //Gets Lead ID from Contact ID
      $returnFields = array("Id");
      $query = array('ContactId' => $contactId);
      //Returns an array of arrays
      $result = $app->dsQuery("Lead", 1, 0, $query,
      $returnFields);

      //Alternate method
      //$result = $app->dsFind("Lead", 1, 0, "ContactId", $contactId, $returnFields);

      if ($result){
        $leadId = $result[0][Id];

        $fields = array('_TotalShowCost' => $totalShowCost);
        $id = $app->dsUpdate("Lead", $leadId, $fields);

      } else {
        //Opportunity Doesn't exist
        return;
      }
    } else {
      //User doesn't exist
      return;
    }

  } else {
    //echo "no Connection";
    return;
  }

}
?>
