/**
  * Get Url to todo on basecmap
  *
  * @author Atul Atri
  *
  * @param Int $_todoProjectId project id where todo is posted
  * @param Int $_todoId todo id
  *
  * @return string basecamp Url to todo on basecmap
  */
 public static function BasecampTodoUrl($_todoProjectId, $_todoId)
 {
     $_SWIFT = SWIFT::GetInstance();
     $_basecampTodoUrl = SWIFT_ConfigManager::Get("BC_BASE_URL", 'basecamp') . SWIFT_ConfigManager::Get("TODO_SUB_URL", 'basecamp');
     $_accountId = $_SWIFT->Settings->Get('bc_base_acc_id');
     $_basecampTodoUrl = sprintf($_basecampTodoUrl, $_accountId, $_todoProjectId, $_todoId);
     return $_basecampTodoUrl;
 }
 /**
  * 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);
 }
 /**
  * Modifies Authentication header in queued requests
  *
  * @author Atul Atri
  * @return void
  */
 private function ModifyAuthHeader()
 {
     $_SWIFT = SWIFT::GetInstance();
     $_authHeaderName = SWIFT_ConfigManager::Get('AUTH_HEADER_NAME');
     $_authHeaderValue = $_SWIFT->Settings->Get('bc_auth_token');
     foreach ($this->_reqQueue as &$_nextReq) {
         if (isset($_nextReq['headers']) && isset($_nextReq['headers'][$_authHeaderName])) {
             $_nextReq['headers'][$_authHeaderName] = $_authHeaderValue;
         }
     }
 }
 /**
  * Render manage form
  *
  * @author Atul Atri
  *
  * @return bool "true" on Success, "false" otherwise
  */
 public function ManageForm()
 {
     //check if already autherised
     $_SWIFT = SWIFT::GetInstance();
     $_authHeader = $_SWIFT->Settings->Get('bc_auth_token');
     if ($_authHeader) {
         $this->Template->Assign('_alreadyAuthorised', true);
     } else {
         $this->Template->Assign('_alreadyAuthorised', false);
     }
     $this->Template->Assign('_settingsObj', $_SWIFT->Settings);
     $this->Template->Assign('_swiftpath', SWIFT::Get('swiftpath'));
     $this->Template->Assign('_Language', $this->Language);
     $this->Template->Assign('_UserInterface', $this->UserInterface);
     $_formTxt = '';
     $_redirectUrl = SWIFT::Get('basename') . '/basecamp/Manager/CodeRedirect';
     $this->Template->Assign('_redirectUrl', $_redirectUrl);
     //show only if user registered this application
     $_appName = $this->Settings->Get('bc_app_name');
     $_appEmail = $this->Settings->Get('bc_email');
     $_appId = $this->Settings->Get('bc_app_id');
     $_appSecret = $this->Settings->Get('bc_app_secret');
     if ($_appName || $_appEmail || $_appId || $_appSecret) {
         $_formTxt = $this->Language->Get('basecamp_update_app_txt');
         $_formTxt = sprintf($_formTxt, '<b>' . $_redirectUrl . '</b>');
         $_buttonTxt = 'basecamp_update_button';
     } else {
         $_formTxt = $this->Language->Get('basecamp_new_app_txt');
         $_bcCreatAppUrl = SWIFT_ConfigManager::Get('CREATE_APP_LNK');
         $_clickHere = $this->Language->Get('basecamp_click_here_lnk');
         $_bcCreateAppHref = "<b><a href='{$_bcCreatAppUrl}' target='_blank'>{$_clickHere}</a></b>";
         $_formTxt = sprintf($_formTxt, $_bcCreateAppHref, '<b>' . $_redirectUrl . '</b>');
         $_buttonTxt = 'basecamp_save_button';
     }
     $_authLink = false;
     if ($_appName && $_appEmail && $_appId && $_appSecret) {
         $_authLink = SWIFT_APIOauth2::GetAuthReqURL();
     }
     $this->Template->Assign('_authLink', $_authLink);
     $this->Template->Assign('_formTxt', $_formTxt);
     $this->Template->Assign('_buttonTxt', $_buttonTxt);
     $this->View->Assign('_buttonTxt', $_buttonTxt);
     $this->View->Assign('_appName', $_appName);
     $this->View->Assign('_appEmail', $_appEmail);
     $this->View->Assign('_appId', $_appId);
     $this->View->Assign('_appSecret', $_appSecret);
     $this->View->ManageForm(self::MENU_ID, self::NAV_ID);
     return true;
 }