/**
  * Returns list of attachments in all posts of this ticket.
  *
  * Result is cached till the end of script.
  *
  * @param bool $reload True to reload attachments from server.
  * @return kyTicketAttachment[]
  */
 public function getAttachments($reload = false)
 {
     if ($this->attachments === null || $reload) {
         $this->attachments = kyTicketAttachment::getAll($this->id)->getRawArray();
     }
     /** @noinspection PhpParamsInspection */
     return new kyResultSet($this->attachments);
 }
 /**
  * Creates new attachment in this post with contents read from physical file.
  * WARNING: Data is not sent to Kayako unless you explicitly call create() on this method's result.
  *
  * @param string $file_path Path to file.
  * @param string $file_name Optional. Use to set filename other than physical file.
  * @return kyTicketAttachment
  */
 public function newAttachmentFromFile($file_path, $file_name = null)
 {
     return kyTicketAttachment::createNewFromFile($this, $file_path, $file_name);
 }
 } else {
     if ($_REQUEST['step'] == 3) {
         if (!empty($_POST)) {
             //Set Defaults for a new ticket
             $_defaultStatusID = kyTicketStatus::getAll()->filterByType(kyTicketStatus::TYPE_PUBLIC)->first()->getId();
             $_defaultPriorityID = kyTicketPriority::getAll()->filterByType(kyTicketStatus::TYPE_PUBLIC)->first()->getId();
             $_defaultTypeID = kyTicketType::getAll()->filterByType(kyTicketStatus::TYPE_PUBLIC)->first()->getId();
             kyTicket::setDefaults($_defaultStatusID, $_defaultPriorityID, $_defaultTypeID);
             //Create ticket
             $_department = kyDepartment::get($_POST['departmentid']);
             $_priority = kyTicketPriority::get($_POST['ticketpriorityid']);
             $_ticket = kyTicket::createNewAuto($_department, $clientsdetails['firstname'] . ' ' . $clientsdetails['lastname'], $clientsdetails['email'], $_POST['ticketmessage'], $_POST['ticketsubject'])->setPriority($_priority)->setIgnoreAutoResponder($_settings['ignoreautoresponder'])->create();
             $_ticketPosts = $_ticket->getPosts();
             //Save ticket attachments
             foreach ($_FILES['ticketattachments']['tmp_name'] as $_key => $_ticketAttachment) {
                 kyTicketAttachment::createNewFromFile($_ticketPosts[0], $_ticketAttachment, $_FILES['ticketattachments']['name'][$_key])->create();
             }
             //Save custom fields
             $_ticket->setCustomFieldValuesFromPOST();
             $_ticket->updateCustomFields();
             $smarty->assign('_ticketDisplayID', $_ticket->getDisplayId());
             $smarty->assign('_ticketSubject', $_POST['ticketsubject']);
             $smarty->assign('_ticketMessage', nl2br($_POST['ticketmessage']));
             $templatefile = 'ticketConfirmation';
         } else {
             header('Location: ' . WHMCS_URL . 'submitticket.php');
         }
     } else {
         header('Location: ' . WHMCS_URL . 'submitticket.php');
     }
 }
 /**
  * Creates new attachment for ticket post with contents read from physical file.
  * WARNING: Data is not sent to Kayako unless you explicitly call create() on this method's result.
  *
  * @param kyTicketPost $ticket_post Ticket post.
  * @param string $file_path Path to file.
  * @param string $file_name Optional. Use to set filename other than physical file.
  * @return kyTicketAttachment
  */
 public static function createNewFromFile($ticket_post, $file_path, $file_name = null)
 {
     $new_ticket_attachment = new kyTicketAttachment();
     $new_ticket_attachment->setTicketId($ticket_post->getTicketId());
     $new_ticket_attachment->setTicketPostId($ticket_post->getId());
     $new_ticket_attachment->setContentsFromFile($file_path, $file_name);
     return $new_ticket_attachment;
 }
				</td>
			</tr>
		</table>
		<div class="clear"></div>
		<table id="post">
			<?php 
foreach ($ticket_details->getPosts() as $posts) {
    ?>
				<tr>
					<td colspan="3"><?php 
    echo $posts->getContents();
    ?>
</td>
					<td>
						<?php 
    foreach (kyTicketAttachment::getAll($ticket_details->getId()) as $key) {
        if ($posts->getId() == $key->getTicketPostId()) {
            echo "<a href='" . JURI::Root() . "tmp/" . $key->getFileName() . "'>" . $key->getFileName() . "</a>&nbsp;";
        } else {
            echo "&nbsp;";
        }
    }
    ?>
					</td>
				</tr>
				<tr class="ticketgeneralinfocontainer" id="post_details">
					<td>Posted on</td>
					<td><?php 
    echo date('M d, Y h:i A', strtotime($posts->getDateline()));
    ?>
</td>
<?php

/**
 * @copyright      2001-2015 Kayako
 * @license        https://www.freebsd.org/copyright/freebsd-license.html
 * @link           https://github.com/kayako/whmcs-integration
 */
//Include config file
require_once __DIR__ . '/config.php';
//Include all necessary classes and helper methods
require_once 'API/kyIncludes.php';
require_once 'functions.php';
//Initialize the client
kyConfig::set(new kyConfig(API_URL, API_KEY, SECRET_KEY));
$_ticketAttachment = kyTicketAttachment::get($_GET['tid'], $_GET['aid']);
Download($_ticketAttachment->getFileName(), $_ticketAttachment->getContents());