PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/tickspot/TickSpot.php

https://bitbucket.org/delpho/tickhub
PHP | 170 lines | 117 code | 25 blank | 28 comment | 14 complexity | 7965291c8d36023a7fd61029f0c622ad MD5 | raw file
  1. <?php
  2. /*
  3. * To change this template, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. /**
  7. * TickSpot main class
  8. *
  9. * @author jaraya
  10. */
  11. class TickSpot {
  12. /**
  13. *
  14. * @var string
  15. */
  16. private $companyName;
  17. private $email;
  18. private $password;
  19. public function __construct( $companyName ) {
  20. if ( trim ( $companyName) == '' ) {
  21. throw new Exception("No company name specified");
  22. }
  23. $this->companyName = $companyName;
  24. }
  25. public function login ($email, $password) {
  26. $email = Utils::enforceEmailValue($email);
  27. $params = array(
  28. 'email' => $email,
  29. 'password' => $password
  30. );
  31. $xml = $this->xmlRequest(TICKSPOT_USERS_URL, $params);
  32. if ( $xml != null ) {
  33. $this->email = $email;
  34. $this->password = $password;
  35. return true;
  36. }
  37. return false;
  38. }
  39. /**
  40. * Returns the company URL
  41. *
  42. * @return string The company URL
  43. */
  44. public function getCompanyURL () {
  45. if ( $this->company_url == null ) {
  46. $this->company_url = str_replace('{company', $this->companyName, TICKSPOT_COMPANY_URL);
  47. }
  48. return $this->company_url;
  49. }
  50. private function xmlRequest ( $url, $params ) {
  51. $url = str_replace('{company}', $this->companyName, $url);
  52. $httpClient = new HttpClient($url, HttpClient::METHOD_GET, $params);
  53. try{
  54. $xml = $httpClient->doGetRequest();
  55. return $xml;
  56. } catch(Exception $ex) {
  57. Log::getInstance()->logException($ex);
  58. return null;
  59. }
  60. return null;
  61. }
  62. public function getClientsProjectsTasks ( ) {
  63. $params = array(
  64. 'email' => $this->email,
  65. 'password' => $this->password
  66. );
  67. $userID = TickSpotUser::getUserIDByEmail($this->email);
  68. $xml = $this->xmlRequest(TICKSPOT_CLIENTS_PROJECTS_TASKS_URL, $params);
  69. $json = @json_decode(@json_encode(simplexml_load_string($xml)),true);
  70. return TickSpotClient::parseClients($json, $userID);
  71. }
  72. public function getUsers ( $project_id = null ) {
  73. $params = array(
  74. 'email' => $this->email,
  75. 'password' => $this->password
  76. );
  77. if ( $project_id !== null ) {
  78. $params['project_id'] = (int) $project_id;
  79. }
  80. $xml = $this->xmlRequest(TICKSPOT_USERS_URL, $params);
  81. $json = @json_decode(@json_encode(simplexml_load_string($xml)),true);
  82. return TickSpotUser::parseUsers($json);
  83. }
  84. public static function cron () {
  85. $pdo = Database::getInstance()->getConnection();
  86. $sql = 'SELECT * FROM ' . User::VIEW_TICKSPOT_USERS;
  87. $stmt = $pdo->prepare($sql);
  88. $stmt->execute();
  89. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  90. if ( $rows && count($rows) > 0 ) {
  91. foreach ( $rows as $row ) {
  92. $company = $row['tickspot_company'];
  93. $email = $row['email'];
  94. $passwordFilterClassName = TICKSPOT_PASSWORD_FILTER;
  95. $passwordFilter = new $passwordFilterClassName();
  96. $password = $passwordFilter->decode($row['password']);
  97. $tickspot = new TickSpot($company);
  98. if ( $tickspot->login($email, $password) ) {
  99. $userData = $tickspot->getUsers();
  100. $tickspot->getClientsProjectsTasks();
  101. }
  102. }
  103. }
  104. }
  105. /**
  106. * Adds a new tickspot entry for that user
  107. *
  108. * @param int $userID
  109. * @param int $taskID
  110. * @param float $hours
  111. * @param string $date
  112. * @param string $notes
  113. * @return bool
  114. */
  115. public static function createNewEntry ( $userID, $taskID, $hours, $date, $notes ) {
  116. $pdo = Database::getInstance()->getConnection();
  117. $sql = 'SELECT * FROM ' . User::VIEW_TICKSPOT_USERS . ' WHERE user_id = :user_id ';
  118. $stmt = $pdo->prepare($sql);
  119. $stmt->execute(array(':user_id' => $userID));
  120. $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
  121. if ( $rows && count($rows) > 0 ) {
  122. $row = $rows[0];
  123. $passwordFilter = new MyPasswordFilter();
  124. $params = array(
  125. 'email' => $row['email'],
  126. 'password' => $passwordFilter->decode($row['password']),
  127. 'task_id' => $taskID,
  128. 'hours' => $hours,
  129. 'date' => $date,
  130. 'notes' => $notes
  131. );
  132. $tickspot = new TickSpot($row['tickspot_company']);
  133. $xml = $tickspot->xmlRequest(TICKSPOT_CREATE_ENTRY_URL, $params);
  134. if ( $xml == null ) {
  135. throw new Exception('Unable to create the tickspot entry.');
  136. }
  137. return true;
  138. Log::getInstance()->log(print_r($xml, true));
  139. $json = @json_decode(@json_encode(simplexml_load_string($xml)),true);
  140. return $json;
  141. }
  142. return false;
  143. }
  144. }
  145. ?>