/application/controllers/api/match.php

https://github.com/Sa-ryong/Stadioom-php · PHP · 199 lines · 142 code · 34 blank · 23 comment · 16 complexity · a5c4df7d36623598894049cecbf84d50 MD5 · raw file

  1. <?php
  2. require(APPPATH . '/libraries/Stadioom_REST_Controller.php');
  3. class Match extends Stadioom_REST_Controller {
  4. function __construct() {
  5. parent::__construct();
  6. $this->load->model('dao/MatchDao');
  7. force_ssl();
  8. // if (function_exists('force_ssl'))
  9. // remove_ssl();
  10. }
  11. /**
  12. * Registers a new match.
  13. */
  14. public function index_post() {
  15. log_message('debug', "index_post entered.");
  16. $accessToken = $this->post('accessToken');
  17. try {
  18. if ($accessToken != null) {
  19. $userId = $this->verifyToken($accessToken);
  20. } else {
  21. $this->security->csrf_verify();
  22. $user = $this->session->userdata('user');
  23. $userId = $user['id'];
  24. if ($userId == null) {
  25. throw new Exception("'userId' was not set.", 400);
  26. }
  27. }
  28. // fill data
  29. $match = new Entities\MatchRecord();
  30. $match->setOwnerId($userId);
  31. $match->setBrandId($this->post('brandId'));
  32. $match->setSportId($this->post('sportId'));
  33. $format = "Y-m-d H:i:s";
  34. $started = DateTime::createFromFormat($format, $this->post('started'), new DateTimeZone("GMT"));
  35. if ($started != null) {
  36. $match->setStarted($started);
  37. }
  38. $ended = DateTime::createFromFormat($format, $this->post('ended'), new DateTimeZone("GMT"));
  39. if ($ended != null) {
  40. $match->setEnded($ended);
  41. }
  42. $canceled = DateTime::createFromFormat($format, $this->post('canceled'), new DateTimeZone("GMT"));
  43. if ($canceled != null) {
  44. $match->setCanceled($canceled);
  45. }
  46. $match->setLocation($this->post('location'));
  47. $match->setLatitude($this->post('latitude'));
  48. $match->setLongitude($this->post('longitude'));
  49. $match->setTitle($this->post('title'));
  50. $match->setScoreA($this->post('scoreA'));
  51. $match->setScoreB($this->post('scoreB'));
  52. $match->setTeamAId($this->post('teamA'));
  53. $match->setTeamBId($this->post('teamB'));
  54. $memberIdsA = $this->post('memberIdsA');
  55. $memberIdsB = $this->post('memberIdsB');
  56. $memberFbIdsA = $this->post('memberFbIdsA');
  57. $memberFbIdsB = $this->post('memberFbIdsB');
  58. log_message('debug', "trying to register.");
  59. $matchId = $this->MatchDao->register($match, $memberIdsA, $memberIdsB, $memberFbIdsA, $memberFbIdsB);
  60. log_message('debug', "done.");
  61. log_message('debug', "index_post exit.");
  62. $this->responseOk(array("id" => $matchId));
  63. } catch (Exception $e) {
  64. log_message('debug', "index_post exit with error: " . $e->getMessage());
  65. $this->responseError($e);
  66. }
  67. }
  68. public function share_post($id) {
  69. // TODO: not properly implemented.
  70. $accessToken = $this->post('accessToken');
  71. try {
  72. $userId = $this->verifyToken($accessToken);
  73. $sharedInfo = new Entities\MatchRecordShare();
  74. $sharedInfo->setSharedBy($userId);
  75. $sharedInfo->setMatchId($id);
  76. $sharedTarget = $this->post('targetMedia');
  77. if ($sharedTarget == null) {
  78. $sharedTarget = 'Facebook';
  79. }
  80. $sharedInfo->setTargetMedia($sharedTarget);
  81. $sharedInfo->setLink($this->post('link'));
  82. $sharedInfo->setComment($this->post('comment'));
  83. $sharedId = $this->MatchDao->shared($sharedInfo);
  84. $this->responseOk(array("id" => $sharedId));
  85. } catch (Exception $e) {
  86. $this->responseError($e);
  87. }
  88. }
  89. /**
  90. * Deletes the specified match.
  91. */
  92. public function match_delete($id) {
  93. $accessToken = $this->delete('accessToken');
  94. try {
  95. $userId = $this->verifyToken($accessToken);
  96. $this->MatchDao->deleteMatch($id, $userId);
  97. } catch (Exception $e) {
  98. $this->responseError($e);
  99. }
  100. }
  101. /**
  102. * Returns a list of registered matches or the specified match.
  103. */
  104. public function index_get() {
  105. try {
  106. $matchId = $this->get('matchId');
  107. if ($matchId != null) {
  108. // TODO this path is deprecated by 'match_get'.
  109. $match = $this->MatchDao->find($matchId);
  110. $this->responseOk($match->toArray());
  111. } else {
  112. $options = array(/*'since' => $this->get('since'),*/
  113. 'firstOffset' => $this->get('firstOffset'),
  114. 'limit' => $this->get('limit'),
  115. 'sportId' => $this->get('sportId'),
  116. 'ownerId' => $this->get('ownerId'),
  117. 'memberId' => $this->get('memberId'));
  118. $result = $this->MatchDao->findAll($options);
  119. $allMatches = array();
  120. foreach ($result as $match) {
  121. array_push($allMatches, $match->toArray());
  122. }
  123. $this->responseOk(array('data' => $allMatches));
  124. }
  125. } catch (Exception $e) {
  126. $this->responseError($e);
  127. }
  128. }
  129. /**
  130. * Returns the specified match.
  131. */
  132. public function match_get($matchId) {
  133. try {
  134. $match = $this->MatchDao->find($matchId);
  135. $this->responseOk($match->toArray());
  136. } catch (Exception $e) {
  137. $this->responseError($e);
  138. }
  139. }
  140. /**
  141. * Update a previously registered match.
  142. */
  143. public function index_put() {
  144. $accessToken = $this->put('accessToken');
  145. try {
  146. $invitorId = $this->verifyToken($accessToken);
  147. } catch (Exception $e) {
  148. $this->responseError($e);
  149. }
  150. $this->responseError(new Exception("Not Implemented.", 501));
  151. }
  152. /**
  153. * Returns the historical record of the specified user.
  154. */
  155. public function record_get() {
  156. $userId = $this->get('userId');
  157. try {
  158. $record = $this->MatchDao->getRecord($userId);
  159. $this->responseOk($record);
  160. } catch (Exception $e) {
  161. $this->responseError($e);
  162. }
  163. }
  164. }
  165. ?>