/**
  * Process the 'post comments to JIRA' form on submission
  * Checks for validity and then posts the comment to JIRA web service
  * using REST API
  *
  * @author Abhinav Kumar
  * @return boolean 'true' on success and 'false' otherwise
  * @throws SWIFT_Exception if class is not loaded
  */
 public function ProcessCommentForm()
 {
     if (!$this->GetIsClassLoaded()) {
         throw new SWIFT_Exception(SWIFT_CLASSNOTLOADED);
     }
     $_SWIFT = SWIFT::GetInstance();
     $_issueKey = null;
     $_rawComment = null;
     $_comment = null;
     if (array_key_exists('issueKey', $_POST) && $_POST['issueKey'] != '') {
         $_issueKey = $_POST['issueKey'];
     }
     if (array_key_exists('comment', $_POST) && $_POST['comment'] != '') {
         $_comment = $_rawComment = $_POST['comment'];
     }
     if (array_key_exists('visibility', $_POST)) {
         $_visibility = $_POST['visibility'];
     }
     if ($_issueKey && $_comment) {
         $this->Load->Library('JIRA:JIRABridge', false, false, 'jira');
         $_JIRABridge = SWIFT_JIRABridge::GetInstance();
         if (!$_JIRABridge || !$_JIRABridge instanceof SWIFT_JIRABridge || !$_JIRABridge->GetIsClassLoaded()) {
             $this->UserInterface->DisplayAlert($this->Language->Get('jira_error'), $this->Language->Get('connection_error') . $_SWIFT->Settings->Get('bj_jiraurl'));
             return false;
         }
         $_staffDataStore = $_SWIFT->Staff->GetDataStore();
         $_issueCreator = $_staffDataStore['grouptitle'];
         $_staff = $_SWIFT->Staff->GetProperty('fullname') . ' (' . $_issueCreator . ')' . PHP_EOL;
         $_comment = $_staff . $_comment;
         $_JIRAIssue = $_JIRABridge->GetIssueBy('issuekey', $_issueKey);
         if ($_JIRAIssue && $_JIRAIssue instanceof SWIFT_JIRAIssueManager && $_JIRAIssue->GetIsClassLoaded()) {
             $_JIRAComment = new SWIFT_JIRAComment();
             $_JIRAComment->SetBody($_comment);
             if (isset($_visibility)) {
                 $_visibilityArray = array('type' => 'role', 'value' => $_visibility);
                 $_JIRAComment->SetVisibility($_visibilityArray);
             }
             $_commentPosted = $_JIRAIssue->PostComment($_JIRAComment);
             if ($_commentPosted) {
                 $_SWIFT_TicketObject = SWIFT_Ticket::GetObjectOnID($_JIRAIssue->GetKayakoTicketId());
                 // Did the object load up?
                 if (!$_SWIFT_TicketObject || !$_SWIFT_TicketObject instanceof SWIFT_Ticket || !$_SWIFT_TicketObject->GetIsClassLoaded()) {
                     throw new SWIFT_Exception('Ticket Object ' . SWIFT_INVALIDDATA);
                 }
                 $_logText = $this->Language->Get('jira_comment_posted');
                 SWIFT_TicketAuditLog::AddToLog($_SWIFT_TicketObject, null, SWIFT_TicketAuditLog::ACTION_UPDATETICKETPOST, $_logText, SWIFT_TicketAuditLog::VALUE_NONE, 0, '', 0, '');
                 SWIFT::Notify(SWIFT::NOTIFICATION_INFO, $this->Language->Get('jira_comment_posted'));
             } else {
                 SWIFT::Notify(SWIFT::NOTIFICATION_ERROR, $this->Language->Get('jira_comment_notposted'));
             }
         } else {
             SWIFT::Notify(SWIFT::NOTIFICATION_ERROR, $this->Language->Get('jira_noissuefound'));
         }
     }
     $this->Load->Controller('Ticket', APP_TICKETS)->Load->Method('View', $_POST['jira_ticketid'], $_POST['jira_listtype'], $_POST['jira_departmentid'], $_POST['jira_ticketstatusid'], $_POST['jira_tickettypeid']);
     return true;
 }
 /**
  * Posts a comment to the JIRA Issue
  *
  * @author Abhinav Kumar
  *
  * @param SWIFT_JIRAComment $_Comment the Comemnt object
  *
  * @throws SWIFT_Exception
  * @return bool
  */
 public function PostComment(SWIFT_JIRAComment $_Comment)
 {
     if ($_Comment) {
         $_Comment->SetIssue($this);
         $_JIRABridge = SWIFT_JIRABridge::GetInstance();
         $_JIRABridge->PostComment($_Comment);
         return true;
     }
     throw new SWIFT_Exception(SWIFT_INVALIDDATA);
 }
 /**
  * Links a ticket with an existing JIRA issue
  *
  * @author Abhinav Kumar
  *
  * @param string $_ticketID The kayako ticket id
  * @param string $_JIRAIssueID
  * @param array  $_data
  *
  * @return boolean
  */
 public function LinkIssue($_ticketID, $_JIRAIssueID, $_data = [])
 {
     if ($_JIRAIssueID && $this->IsIssueValid($_JIRAIssueID) && $_ticketID) {
         $_JIRAIssueManager = $this->Get($_JIRAIssueID);
         if ($_JIRAIssueManager && $_JIRAIssueManager instanceof SWIFT_JIRAIssueManager) {
             if (array_key_exists('description', $_data) && $_data['description'] != '') {
                 $_JIRAComment = new SWIFT_JIRAComment();
                 $_JIRAComment->SetBody($_data['description']);
                 $_JIRAComment->SetIssue($_JIRAIssueManager);
                 $this->PostComment($_JIRAComment);
             }
             //We are almost there . . . time to create a local record for Ticket <->Issue reference
             $this->Load->Library('JIRA:JIRAIssueManager', false, false, 'jira');
             $_SWIFT = SWIFT::GetInstance();
             $_updated = $_SWIFT->Database->AutoExecute(TABLE_PREFIX . self::$_tableName, ['ticketid' => $_SWIFT->Database->Escape($_ticketID), 'issueid' => $_SWIFT->Database->Escape($_JIRAIssueManager->GetId()), 'issuekey' => $_SWIFT->Database->Escape($_JIRAIssueID)], 'INSERT');
             if ($_updated) {
                 $_SWIFTTicketObject = SWIFT_Ticket::GetObjectOnID($_ticketID);
                 if ($_SWIFTTicketObject && $_SWIFTTicketObject instanceof SWIFT_Ticket && $_SWIFTTicketObject->GetIsClassLoaded()) {
                     $_title = $_SWIFTTicketObject->GetTicketDisplayID();
                     $_ticketSummary = $_SWIFTTicketObject->GetProperty('subject');
                 } else {
                     $_title = $this->Language->Get('jira_kayakoticket');
                     $_ticketSummary = '';
                 }
                 $_ticketURL = SWIFT::Get('basename') . '/Tickets/Ticket/View/' . $_ticketID;
                 if ($_SWIFT->Settings->Get('bj_jiraissuelinking')) {
                     $_postLink = $this->PostRemoteLink($_JIRAIssueID, $_ticketURL, $_title, $_ticketSummary);
                     if (!is_bool($_postLink)) {
                         SWIFT::Notify(SWIFT::NOTIFICATION_ERROR, strip_tags($this->Language->Get('jira_error') . $_postLink));
                         return false;
                     }
                 }
             }
         } else {
             $this->SetErrorMessage($this->Language->Get('jira_noissuefound'));
             return false;
         }
     } else {
         $this->SetErrorMessage($this->Language->Get('jira_noissuefound'));
         return false;
     }
     return true;
 }