Skip to content

Commit

Permalink
Fix ticket commands for issue #18
Browse files Browse the repository at this point in the history
  • Loading branch information
jundis committed Sep 1, 2016
1 parent 1b42487 commit 82cf3c9
Showing 1 changed file with 50 additions and 6 deletions.
56 changes: 50 additions & 6 deletions cwslack.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
$urlticketdata = $connectwise . "/v4_6_release/apis/3.0/service/tickets/" . $ticketnumber; //Set ticket API url
$noteurl = $connectwise . "/v4_6_release/apis/3.0/service/tickets/" . $ticketnumber . "/notes?orderBy=id%20desc";
$ticketurl = $connectwise . "/v4_6_release/services/system_io/Service/fv_sr100_request.rails?service_recid="; //Ticket URL for connectwise.
$timeurl = $connectwise . "/v4_6_release/apis/3.0/time/entries?conditions=chargeToId=" . $ticketnumber . "&chargeToType=%27ServiceTicket%27&orderBy=dateEntered%20desc"; //Set the URL required for cURL requests to the time entry API.


//Set noteurl to use ascending if an initial note command is passed.
if($command == "initial" || $command == "first" || $command == "note")
Expand Down Expand Up @@ -111,6 +113,9 @@

if($posttext==1) //Block for curl to get latest note
{
$createdby = "Error"; //Create with error just in case.
$notetext = "Error"; //Create with error just in case.

$ch1 = curl_init(); //Initiate a curl session_cache_expire

//Create curl array to set the API url, headers, and necessary flags.
Expand All @@ -135,6 +140,32 @@

$dataTNotes = json_decode($curlBodyTNotes); //Decode the JSON returned by the CW API.

//Block for cURL connections to Time Entries API
$ch2 = curl_init(); //Initiate a curl session

//Create curl array to set the API url, headers, and necessary flags.
$curlOpts2 = array(
CURLOPT_URL => $timeurl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $header_data,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HEADER => 1,
);
curl_setopt_array($ch2, $curlOpts2); //Set the curl array to $curlOpts

$answerTimeData = curl_exec($ch2); //Set $answerTData to the curl response to the API.
$headerLen = curl_getinfo($ch2, CURLINFO_HEADER_SIZE); //Get the header length of the curl response
$curlBodyTimeData = substr($answerTimeData, $headerLen); //Remove header data from the curl string.

// If there was an error, show it
if (curl_error($ch2)) {
die(curl_error($ch2));
}
curl_close($ch2);

$dataTimeData = json_decode($curlBodyTimeData); //Decode the JSON returned by the CW API.
//End time entry block.

if($command == "full" || $command == "notes" || $command == "all")
{
$ch1 = curl_init(); //Initiate a curl session_cache_expire
Expand All @@ -161,6 +192,19 @@

$dataTNotes2 = json_decode($curlBodyTNotes); //Decode the JSON returned by the CW API.
}
$createdby = $dataTNotes[0]->createdBy; //Set $createdby to the ticket note creator.
$text = $dataTNotes[0]->text; //Set $text to the ticket text.
if(array_key_exists(0,$dataTNotes) && array_key_exists(0,$dataTimeData) && $command != "initial" && $command != "first" && $command != "note") //Check if arrays exist properly.
{
$timetime = new DateTime($dataTimeData[0]->dateEntered); //Create new time object based on time entry note.
$notetime = new DateTime($dataTNotes[0]->dateCreated); //Create new datetime object based on ticketnote note.

if($timetime>$notetime) //If the time entry is newer than latest ticket note.
{
$createdby = $dataTimeData[0]->enteredBy; //Set $createdby to the time entry creator.
$text = $dataTimeData[0]->notes; //Set $text to the time entry text.
}
}
}
//-
//Priority command
Expand Down Expand Up @@ -370,8 +414,8 @@
)
),
array(
"pretext" => "Initial ticket note from: " . $dataTNotes[0]->createdBy,
"text" => $dataTNotes[0]->text,
"pretext" => "Initial ticket note from: " . $createdby,
"text" => $text,
"mrkdwn_in" => array(
"text",
"pretext",
Expand Down Expand Up @@ -421,8 +465,8 @@
)
),
array(
"pretext" => "Latest Note from: " . $dataTNotes[0]->createdBy,
"text" => $dataTNotes[0]->text,
"pretext" => "Latest Note from: " . $createdby,
"text" => $text,
"mrkdwn_in" => array(
"text",
"pretext",
Expand Down Expand Up @@ -481,8 +525,8 @@
)
),
array(
"pretext" => "Latest Note from: " . $dataTNotes[0]->createdBy,
"text" => $dataTNotes[0]->text,
"pretext" => "Latest Note from: " . $createdby,
"text" => $text,
"mrkdwn_in" => array(
"text",
"pretext",
Expand Down

0 comments on commit 82cf3c9

Please sign in to comment.