/**
  * 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;
 }
    /**
     * Renders the 'Add Comment to JIRA' form
     *
     * @param string $_issueKey The JIRA issue key
     *
     * @return boolean 'TRUE' on success and 'FALSE' otherwise
     * @throws SWIFT_Exception if class is not loaded or $_issueKey is empty
     */
    public function RenderCommentForm($_issueKey)
    {
        if (!$this->GetIsClassLoaded()) {
            throw new SWIFT_Exception(__CLASS__ . ' ' . SWIFT_CLASSNOTLOADED);
        }
        if (empty($_issueKey)) {
            throw new SWIFT_Exception('Issue Key' . $this->Language->Get('jira_noempty'));
        }
        $_buttonText = '<input type="button" name="submitbutton" id="%formid%_submit" class="rebuttonblue" onclick="javascript: $(\'#%formid%\').submit();PreventDoubleClicking(this);" value="' . $this->Language->Get('jira_post') . '"/>
					 <input type="button" name="submitbutton" id="%formid%_cancel" class="rebuttonred" onclick="javascript: $(\'.ui-icon-closethick\').click();" value="' . $this->Language->Get('jira_cancel') . '" onfocus="blur();" />';
        $this->UserInterface->OverrideButtonText($_buttonText);
        $_GeneralTabObject = $this->UserInterface->AddTab($this->Language->Get('tabgeneral'), SWIFT::Get('swiftpath') . '__modules/jira/resources/postbugtojira_b.gif', 'general', true, false);
        $this->Load->Library('JIRA:JIRABridge', false, false, 'jira');
        $_JIRABridge = SWIFT_JIRABridge::GetInstance();
        if (!$_JIRABridge || !$_JIRABridge instanceof SWIFT_JIRABridge || !$_JIRABridge->GetIsClassLoaded()) {
            SWIFT::Notify(SWIFT::NOTIFICATION_ERROR, $this->Language->Get('jira_error'));
            return false;
        }
        $_JIRAIssue = $_JIRABridge->GetIssueBy('issuekey', $_issueKey);
        if ($_JIRAIssue && $_JIRAIssue instanceof SWIFT_JIRAIssueManager && $_JIRAIssue->GetIsClassLoaded()) {
            $this->Load->Library('Ticket:Ticket', false, false, APP_TICKETS);
            $_project = $_JIRAIssue->GetProject();
            $_RolesContainer = $_JIRABridge->GetProjectRoles($_project);
            $_RolesOptionContainer = array();
            if ($_RolesContainer && _is_array($_RolesContainer)) {
                foreach ($_RolesContainer as $_Role => $_RoleURL) {
                    $_RolesOptionContainer[] = array('title' => $_Role, 'value' => $_Role);
                }
            }
            $_SWIFT_TicketObject = SWIFT_Ticket::GetObjectOnID($_JIRAIssue->GetKayakoTicketID());
            $_GeneralTabObject->Title('<label for="comment">' . $this->Language->Get('jira_comment') . '</label><br/><span class="tabledescription">' . $this->Language->Get('jira_sensitive') . '</span>');
            $_GeneralTabObject->TextArea('comment', '', '', '', 50, 6);
            $_GeneralTabObject->Select('visibility', $this->Language->Get('jira_comment_visibility'), '', $_RolesOptionContainer);
            $_GeneralTabObject->Hidden('issueKey', $_issueKey);
            $this->UserInterface->Hidden('jira_ticketid', $_SWIFT_TicketObject->GetTicketID());
            $this->UserInterface->Hidden('jira_listtype', 'inbox');
            $this->UserInterface->Hidden('jira_departmentid', $_SWIFT_TicketObject->GetProperty('departmentid'));
            $this->UserInterface->Hidden('jira_ticketstatusid', $_SWIFT_TicketObject->GetProperty('ticketstatusid'));
            $this->UserInterface->Hidden('jira_tickettypeid', $_SWIFT_TicketObject->GetProperty('tickettypeid'));
        } else {
            SWIFT::Notify(SWIFT::NOTIFICATION_ERROR, $this->Language->Get('jira_noissuefound') . $_issueKey);
        }
        return true;
    }
 /**
  * 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;
 }