/**
     * 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;
    }
 /**
  * Render the Add comment form
  *
  * @author Atul Atri
  *
  * @param array  $_projectsOpts    basecamp project list
  * @param array  $_todoProjectList list of todo lists on basecamp in selected project
  * @param array  $_personList      list of persons
  * @param array  $_ticketID        ticket tid
  * @param String $_errorTitle      Error title if some error needs to be rended
  * @param String $_errorMsg        Error Message if some error needs to be rended
  *
  * @return bool "true" on Success, "false" otherwise
  */
 public function RenderAddCommentsForm($_ticketID, $_errorTitle = '', $_errorMsg = '')
 {
     $this->UserInterface->Header($this->Language->Get('basecamp') . ' > ' . $this->Language->Get('todoexportform'));
     $this->UserInterface->Start(get_class($this), '/basecamp/TodoManager/AddCommentSubmit', SWIFT_UserInterface::MODE_EDIT, false, false, false, false, 'todoExportFormResHolder', "SWIFT.Basecamp.AdminObject.RestoreTodoExportForm");
     $this->UserInterface->Toolbar->AddButton($this->Language->Get('add'));
     /* @var $_ActionTab SWIFT_UserInterfaceTab */
     $_ActionTab = $this->UserInterface->AddTab($this->Language->Get('basecamp_tab_export'), 'icon_form.gif', 'action', true);
     if ($_errorMsg) {
         if (!$_errorTitle) {
             $_errorTitle = $this->Language->Get('error');
         }
         $_ActionTab->Error($_errorTitle, $_errorMsg, 'bc_todo_list_error');
     }
     $_SWIFT_TicketObject = SWIFT_Ticket::GetObjectOnID($_ticketID);
     if ($_SWIFT_TicketObject->GetProperty('hasattachments')) {
         $_attachments = $_SWIFT_TicketObject->GetAttachmentContainer();
         $_checkBoxContainer = array();
         foreach ($_attachments as $_tmpArr) {
             foreach ($_tmpArr as $_nextAttachment) {
                 $_attachementId = $_nextAttachment['attachmentid'];
                 $_attachementSize = round($_nextAttachment['filesize'] / 1024, 2);
                 $_tmp['value'] = $_attachementId;
                 $_tmp['title'] = $_nextAttachment['filename'] . " ({$_attachementSize} KB)";
                 $_checkBoxContainer[$_attachementId] = $_tmp;
             }
         }
         $_ActionTab->CheckBoxList('todo_files', $this->Language->Get('basecamp_todo_files'), $this->Language->Get('d_basecamp_todo_files'), $_checkBoxContainer);
     }
     $_ActionTab->TextArea('todocomment', $this->Language->Get('basecamp_todocomment'), '', "", 30, 7);
     $this->UserInterface->Hidden('todo_ticketid', $_ticketID);
     $this->UserInterface->End();
     $this->UserInterface->Footer();
 }
 /**
  * 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;
 }
 /**
  * Unlinks a JIRA issue with a Kayako ticket
  *
  * @author Abhinav Kumar
  *
  * @param string $_issueKey The issue key to be unlinked
  * @param int    $_ticketID
  *
  * @throws SWIFT_Exception
  * @return bool
  */
 public function Unlink($_issueKey, $_ticketID)
 {
     $_SWIFT = SWIFT::GetInstance();
     $_JIRABridge = SWIFT_JIRABridge::GetInstance();
     $_response = array();
     if (!$this->GetIsClassLoaded()) {
         throw new SWIFT_Exception(__CLASS__ . ' - ' . SWIFT_CLASSNOTLOADED);
     }
     if (!empty($_issueKey)) {
         $this->Load->Library('JIRA:JIRABridge', false, false, 'jira');
         $this->Language->LoadApp('jira', 'jira');
         if ($_JIRABridge) {
             $_JIRAIssue = $_JIRABridge->GetIssueBy('issuekey', $_issueKey);
             if ($_JIRAIssue && $_JIRAIssue instanceof SWIFT_JIRAIssueManager && $_JIRAIssue->GetIsClassLoaded()) {
                 $_unlinked = $_JIRABridge->UnlinkIssue($_issueKey, $_ticketID);
                 if ($_unlinked) {
                     $_SWIFT_TicketObject = SWIFT_Ticket::GetObjectOnID((int) $_ticketID);
                     if ($_SWIFT_TicketObject && $_SWIFT_TicketObject instanceof SWIFT_Ticket && $_SWIFT_TicketObject->GetIsClassLoaded()) {
                         SWIFT_TicketAuditLog::Create($_SWIFT_TicketObject, null, SWIFT_TicketAuditLog::CREATOR_STAFF, $_SWIFT->Staff->GetStaffID(), $_SWIFT->Staff->GetProperty('fullname'), SWIFT_TicketAuditLog::ACTION_UPDATESTATUS, $this->Language->Get('unlinkedFromJIRA') . ' - ' . $_JIRAIssue->GetKey(), SWIFT_TicketAuditLog::VALUE_STATUS);
                     }
                     $_response['code'] = 200;
                     $_response['message'] = 'success';
                 } else {
                     $_response['code'] = 500;
                     $_response['message'] = $_JIRABridge->GetErrorMessage();
                 }
             } else {
                 $_response['code'] = 500;
                 $_response['message'] = 'noissuekey';
             }
         } else {
             $_response['code'] = 500;
             $_response['message'] = $this->Language->Get('jira_error');
         }
     } else {
         $_response['code'] = 500;
         $_response['message'] = 'noissuekey';
     }
     echo json_encode($_response);
     return true;
 }
 /**
  * Handle todo export form submit
  *
  * @author Atul Atri
  * @return bool "true" on Success, "false" otherwise
  * @throws SWIFT_Exception
  */
 public function TodoExportFormSubmit()
 {
     $_pId = $_POST['todoproject'];
     $_todolistId = $_POST['todolist'];
     $_assigneeId = $_POST['assignee'];
     $_todo = trim($_POST['todoitem']);
     $_comment = trim($_POST['todocomment']);
     $_ticketId = $_POST['todo_ticketid'];
     $_date = trim($_POST['duedate']);
     $_fileNames = array();
     //if this ticket id is already linked throw exception
     $_todoId = SWIFT_TodoTicketLink::getTodoInfo($_ticketId);
     if ($_todoId !== false) {
         throw new SWIFT_Exception("TIcket id {$_ticketId} is already linked to basecamp todo.");
     }
     if (isset($_POST['todo_files'])) {
         $_fileNames = $_POST['todo_files'];
     }
     if ($this->CheckTodoExportForm()) {
         $this->Load->Library('API:APITodos', false, false);
         $this->Load->Library('API:APIComments', false, false);
         $_TodoService = new SWIFT_APITodos();
         try {
             if (!empty($_date)) {
                 $_date = date("c", strtotime($_date));
             }
             $_reponse = $_TodoService->PostTodo($_pId, $_todolistId, $_todo, $_assigneeId, $_date);
             $_reponseArr = json_decode($_reponse, true);
             $_newTodoId = $_reponseArr['id'];
             $this->PostTodoComment($_ticketId, $_pId, $_newTodoId, $_comment, $_fileNames);
             $this->Load->LoadModel('AuditLog:TicketAuditLog');
             $_SWIFT_TicketObject = SWIFT_Ticket::GetObjectOnID($_ticketId);
             SWIFT_TicketAuditLog::AddToLog($_SWIFT_TicketObject, null, SWIFT_TicketAuditLog::ACTION_UPDATESTATUS, $this->Language->Get('basecamp_audit_todo_posted'), SWIFT_TicketAuditLog::VALUE_NONE, 0, '', 0, '');
             //all well make link in table
             SWIFT_TodoTicketLink::insertTodo($_ticketId, $_newTodoId, $_pId);
             //return success message
             $this->View->TodoPostSuccess($_ticketId, $_pId, $_newTodoId);
             return true;
         } catch (Exception $_e) {
             $_error = $_e->getMessage();
             $_SWIFT = SWIFT::GetInstance();
             SWIFT::Error($_SWIFT->Language->Get('error'), $_error);
         }
     }
     $this->Load->TodoExportForm($_ticketId, $_pId);
     return true;
 }