/**
  * Return list of people working on some project
  *
  * @author atul atri
  *
  * @param $_isMulti is request being made as part of multiple concurrent requests
  *
  * @return mixed returns json response from service or void if $_isMulti is true
  * @throws SWIFT_API_Exception If curl request is failed or respose code is not 201
  */
 public function GetPeople($_isMulti = false)
 {
     $_peopleUrl = SWIFT_ConfigManager::Get("PEOPLE_LIST_SUB_URL");
     $_baseUrl = $this->BcApiUrl();
     $_url = $_baseUrl . $_peopleUrl;
     $_CurlInstance = SWIFT_APIHttp::GetInstance();
     if (!$_isMulti) {
         $_responseArr = $_CurlInstance->SendSingedRequest($_url, null, 'GET');
     } else {
         $_responseArr = $_CurlInstance->AddSingedRequest($_url, null, 'GET');
         return;
     }
     return $this->HandleGetPeopleRes($_responseArr);
 }
 /**
  * List todos in a basecamp project
  *
  * @author atul atri
  * @param $_bcProjectId basecamp project id
  * @param $_isMulti is request being made as part of multiple concurrent requests
  * @return mixed returns json response from service or void if $_isMulti is true
  * @throws SWIFT_API_Exception If curl request is failed or respose code is not 201
  */
 public function GetTodolists($_bcProjectId, $_isMulti = false)
 {
     $_todoUrl = SWIFT_ConfigManager::Get('TODOLIST_LIST_SUB_URL');
     $_todoUrl = sprintf($_todoUrl, $_bcProjectId);
     $_baseUrl = $this->BcApiUrl();
     $_url = $_baseUrl . $_todoUrl;
     $_CurlInstance = SWIFT_APIHttp::GetInstance();
     if (!$_isMulti) {
         $_responseArr = $_CurlInstance->SendSingedRequest($_url, null, 'GET');
     } else {
         $_responseArr = $_CurlInstance->AddSingedRequest($_url, null, 'GET');
         return;
     }
     return $this->HandleGetTodolistsRes($_responseArr);
 }
 /**
  * return Authorization information
  *
  * @author atul atri
  * @return mixed returns json response from service
  * @throws SWIFT_API_Exception If curl request is failed or respose code is not 200
  */
 public function getAuthorizationInfo()
 {
     $_url = SWIFT_ConfigManager::Get('AUTH_URL');
     $_CurlInstance = SWIFT_APIHttp::GetInstance();
     $_responseArr = $_CurlInstance->SendSingedRequest($_url, null, 'GET');
     $_code = $_responseArr[SWIFT_APIHttp::SRV_CODE];
     $_response = $_responseArr[SWIFT_APIHttp::SRV_RESPONSE];
     $_curlError = $_responseArr[SWIFT_APIHttp::SRV_ERROR];
     if ($_curlError) {
         throw new SWIFT_API_Exception($this->Language->Get('BC_ERR_INVALID_SETTINGS') . $_curlError);
     }
     if ($_code != 200) {
         throw new SWIFT_API_Exception($this->Language->Get('BC_AUTHORIZATION_ERR'), $_code);
     }
     return $_response;
 }
 /**
  * Post message to basecamp
  *
  * @author atul atri
  *
  * @param string $_project basecamp project id where message needs to be posted
  * @param string $_subject message subject
  * @param string $_content message content
  * @param bool   $_isMulti request is part of multiple requests
  *
  * @return    mixed void id resquest is part of multirequest else json response
  * @throws SWIFT_API_Exception If curl request is failed or respose code is not 201
  */
 public function PostMessage($_project, $_subject, $_content, $_isMulti = fasle)
 {
     $_messagePostUrl = SWIFT_ConfigManager::Get('MSG_POST_SUB_URL');
     $_messagePostUrl = sprintf($_messagePostUrl, $_project);
     $_baseUrl = $this->BcApiUrl();
     $_url = $_baseUrl . $_messagePostUrl;
     $_postParamArr = array('subject' => $_subject, 'content' => $_content);
     $_body = json_encode($_postParamArr);
     $_CurlInstance = SWIFT_APIHttp::GetInstance();
     if (!$_isMulti) {
         $_responseArr = $_CurlInstance->SendSingedRequest($_url, $_body, 'POST');
     } else {
         $_CurlInstance->AddSingedRequest($_url, $_body, 'POST');
         return;
     }
     return $this->HandlePostMessageRes($_responseArr);
 }
 /**
  * Check if OAuth2 request url returns 3XX
  *
  *  @param string $_appName basecamp application name
  * @param string $appEmail email registered with basecamp appliacation
  * @param string $_clientId basecamp application client id
  * @param string $_clientSecret basecamp application client secret
  * @param string $_redirectURL basecamp application redirect url
  *
  * @return bool true if   if OAuth2 request url returns 200 Ok, false otherwise
  */
 public static function CheckAuthReqURL($_appName, $appEmail, $_clientId, $_clientSecret, $_redirectURL)
 {
     $_grantClassObject = new SWIFT_OAuth2CodeGrant();
     $_grantClassObject->SetParameterNames(array('response_type' => 'type'));
     $_reqAuthEndPoint = SWIFT_ConfigManager::Get('AUTH_URL_NEW');
     $_authTokenEndPoint = SWIFT_ConfigManager::Get('AUTH_TOKEN_URL');
     $_OAuth2Client = new SWIFT_OAuth2Client($_grantClassObject, $_clientId, $_clientSecret, $_redirectURL, $_reqAuthEndPoint, $_authTokenEndPoint);
     $_url = $_OAuth2Client->GetAuthReqUrl('web_server');
     $_httpInstance = SWIFT_APIHttp::GetInstance();
     $_userAgent = "{$_appName} ({$appEmail})";
     $_options = array(CURLOPT_FOLLOWLOCATION => false, CURLOPT_USERAGENT => $_userAgent);
     $_responseArr = $_httpInstance->SendRequest($_url, null, 'GET', array(), $_options);
     $_code = $_responseArr[SWIFT_APIHttp::SRV_CODE];
     $_codeSeries = intval($_code / 100);
     if ($_codeSeries == 3) {
         return true;
     }
     return false;
 }
 /**
  * Upload a file to basecamp
  *
  * @author Atul Atri
  *
  * @param string $_filePath    file that needs to be uploaded
  * @param string $_contentType file content type
  * @param bool   $_isMulti     true if this request is part of multiple requests
  *
  * @return String json response from service or true is $_isMulti was true
  * @throws SWIFT_API_Exception If curl request is failed or respose code is not 201
  */
 public function Upload($_filePath, $_contentType, $_isMulti = false)
 {
     $_uploadUrl = SWIFT_ConfigManager::Get('UPLOAD_SUB_URL');
     $_baseUrl = $this->BcApiUrl();
     $_url = $_baseUrl . $_uploadUrl;
     $_CurlInstance = SWIFT_APIHttp::GetInstance();
     //$_postArr[basename($_filePath)] = '@'.$_filePath.';type='.$_contentType;//file_get_contents($_filePath);
     $_postArr = "";
     $_headers = array();
     $_headers['Content-Type'] = $_contentType;
     $_reqOpts[CURLOPT_PUT] = 1;
     $_reqOpts[CURLOPT_INFILE] = fopen($_filePath, "rb");
     $_reqOpts[CURLOPT_INFILESIZE] = filesize($_filePath);
     if (!$_isMulti) {
         $_response = $_CurlInstance->SendSingedRequest($_url, $_postArr, 'POST', $_headers, $_reqOpts, SWIFT_APIHttp::HTTP_FORM_CONTENT_TYPE_MULTIPART);
     } else {
         $_CurlInstance->AddSingedRequest($_url, $_postArr, 'POST', $_headers, $_reqOpts, SWIFT_APIHttp::HTTP_FORM_CONTENT_TYPE_MULTIPART);
         return true;
     }
     return $this->HandleUploadRes($_response);
 }
 /**
  * Post a comment
  *
  * @author Atul Atri
  *
  * @param int    $_bcProjectId project id
  * @param string $_section     SWIFT_APIComments::SECTION_MESSAGES or SWIFT_APIComments::SECTION_TODOS
  * @param int    $_sectionId   section id
  * @param String $_content     comment content
  * @param bool   $_isMulti     true if this request is part of multiple requests
  * @param array  $_attachments attachment array e.g. array('name' => file name, 'token' => file token)
  *
  * @return String json response from service or true is $_isMulti was true
  * @throws SWIFT_API_Exception If curl request is failed or respose code is not 201
  */
 public function PostComment($_bcProjectId, $_section, $_sectionId, $_content, $_isMulti = false, array $_attachments = array())
 {
     $_commentPostUrl = SWIFT_ConfigManager::Get('COMMENT_POST_SUB_URL');
     $_commentPostUrl = sprintf($_commentPostUrl, $_bcProjectId, $_section, $_sectionId);
     $_baseUrl = $this->BcApiUrl();
     $_url = $_baseUrl . $_commentPostUrl;
     $_CurlInstance = SWIFT_APIHttp::GetInstance();
     $_postArr = array('content' => $_content);
     foreach ($_attachments as $_nextAttachment) {
         if (!empty($_nextAttachment['name']) && !empty($_nextAttachment['token'])) {
             $_tmp['name'] = $_nextAttachment['name'];
             $_tmp['token'] = $_nextAttachment['token'];
             $_postArr['attachments'][] = $_tmp;
         }
     }
     $_postStr = json_encode($_postArr);
     if (!$_isMulti) {
         $_response = $_CurlInstance->SendSingedRequest($_url, $_postStr, 'POST');
     } else {
         $_CurlInstance->AddSingedRequest($_url, $_postStr, 'GET');
         return true;
     }
     return $this->HandlePostCommentRes($_response);
 }
 /**
  * Handle comment posting to basecamp
  *
  * @author Atul Atri
  *
  * @param int    $_projectId project id
  * @param int    $_ticketId  ticket id
  * @param int    $_todoId    todo id
  * @param string $_comment   Comment'
  * @param array  $_fileNames files array
  *
  * @return mixed basecamp response on Success, "false" otherwise
  * @throws SWIFT_Exception If comment could not be posted to basecamp
  */
 private function PostTodoComment($_ticketId, $_projectId, $_todoId, $_comment = "", array $_fileNames = array())
 {
     if (!empty($_comment) || !empty($_fileNames)) {
         $_CommentsSrv = new SWIFT_APIComments();
         $_filesToBeUploaded = array();
         if (empty($_comment)) {
             $_comment = "";
         }
         if (!empty($_fileNames)) {
             $_SWIFT_TicketObject = SWIFT_Ticket::GetObjectOnID($_ticketId);
             $_attachments = $_SWIFT_TicketObject->GetAttachmentContainer();
             foreach ($_attachments as $_tmpArr) {
                 foreach ($_tmpArr as $_nextAttachment) {
                     $_fileId = $_nextAttachment['attachmentid'];
                     if (in_array($_fileId, $_fileNames)) {
                         $_tmp['name'] = $_nextAttachment['filename'];
                         $_tmp['path'] = SWIFT_BASEPATH . '/' . SWIFT_BASEDIRECTORY . '/' . SWIFT_FILESDIRECTORY . '/' . $_nextAttachment['storefilename'];
                         $_tmp['type'] = $_nextAttachment['filetype'];
                         $_fileId = $_nextAttachment['attachmentid'];
                         $_filesToBeUploaded[$_fileId] = $_tmp;
                     }
                 }
             }
             if (count($_filesToBeUploaded) > 0) {
                 $_CurlInstance = SWIFT_APIHttp::GetInstance();
                 $_CurlInstance->InitMultiCurl();
                 $_count = 0;
                 foreach ($_filesToBeUploaded as &$_nextAttachment) {
                     $this->Load->Library('API:APIAttachments', false, false);
                     $_AttachmentsSrv = new SWIFT_APIAttachments();
                     $_AttachmentsSrv->Upload($_nextAttachment['path'], $_nextAttachment['type'], true);
                     $_nextAttachment['service'] = $_AttachmentsSrv;
                     $_nextAttachment['multiCurlIndex'] = $_count;
                     $_count++;
                 }
                 $_responses = $_CurlInstance->ExecuteMultiCurl();
                 $_CurlInstance->EndMultiCurl();
                 foreach ($_filesToBeUploaded as &$_nextAttachment) {
                     $_srv = $_nextAttachment['service'];
                     $_multiCurlIndex = $_nextAttachment['multiCurlIndex'];
                     $_res = $_srv->HandleUploadRes($_responses[$_multiCurlIndex]);
                     $_resArr = json_decode($_res, true);
                     $_nextAttachment['token'] = $_resArr['token'];
                 }
             }
         }
         return $_CommentsSrv->PostComment($_projectId, SWIFT_APIComments::SECTION_TODOS, $_todoId, $_comment, false, $_filesToBeUploaded);
     }
     return false;
 }