/apps/api/modules/Personal/actions/actions.class.php

https://github.com/bravist/iCunQian · PHP · 328 lines · 196 code · 41 blank · 91 comment · 35 complexity · 485bb2cb261798d7673f83865a546df5 MD5 · raw file

  1. <?php
  2. /**
  3. * @package apps\api\modules\Personal\actions
  4. */
  5. /**
  6. * Personal actions.
  7. *
  8. * @subpackage Personal
  9. * @author Your name here
  10. * @version SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
  11. */
  12. class PersonalActions extends baseApiActions
  13. {
  14. /**
  15. * Executes preExecute before action.
  16. *
  17. * @return void
  18. *
  19. * @issue 2626
  20. */
  21. public function preExecute() {
  22. parent::preExecute();
  23. $this->setTemplate('json');
  24. }
  25. /**
  26. * Get all of products of user
  27. *
  28. * @return void
  29. *
  30. * @issue 2632
  31. */
  32. public function executeRetrieveByUser() {
  33. if ($this->getRequest()->getMethod() != sfRequest::GET) {
  34. $this->forward('default', 'error400');
  35. }
  36. $accountId = $this->getRequestParameter('account_id');
  37. $hash = $this->getRequestParameter('hash');
  38. $offset = $this->getRequestParameter('offset');
  39. $limit = $this->getRequestParameter('limit');
  40. try {
  41. if (empty($accountId)
  42. || empty($hash)) {
  43. throw new ParametersException(ParametersException::$error1000, 'account_id, hash');
  44. }
  45. DepositMembersPeer::verfiyMember($accountId, $hash);
  46. $rs = DepositPersonalProductsPeer::getPersonProductByUser(
  47. $accountId,
  48. $offset,
  49. $limit
  50. );
  51. $this->responseData = array('status' => 1, 'personal_products' => $rs);
  52. } catch (Exception $e) {
  53. $this->setResponseError($e);
  54. }
  55. }
  56. /**
  57. * Get one product of user
  58. *
  59. * @return void
  60. *
  61. * @issue 2632
  62. */
  63. public function executeRetrieve() {
  64. if ($this->getRequest()->getMethod() != sfRequest::GET) {
  65. $this->forward('default', 'error400');
  66. }
  67. $pk = $this->getRequestParameter('personal_product_id');
  68. try {
  69. if (empty($pk)) {
  70. throw new ParametersException(ParametersException::$error1000, 'personal_product_id');
  71. }
  72. $rs = DepositPersonalProductsPeer::getPersonProductById(
  73. $pk
  74. );
  75. $this->responseData = array('status' => 1, 'personal_products' => $rs);
  76. } catch (Exception $e) {
  77. $this->setResponseError($e);
  78. }
  79. }
  80. /**
  81. * Create a new personal product
  82. *
  83. * @return void
  84. *
  85. * @issue 2632
  86. */
  87. public function executeCreate() {
  88. if ($this->getRequest()->getMethod() != sfRequest::POST) {
  89. $this->forward('default', 'error400');
  90. }
  91. if (is_null($this->post)) {
  92. $this->forward('default', 'error403');
  93. }
  94. try {
  95. $this->validateAccount();
  96. $this->_validateProduct();
  97. $rs = DepositPersonalProductsPeer::addPersonalProduct(
  98. $this->post['product_id'],
  99. $this->post['account_id'],
  100. $this->post['expected_rate'],
  101. $this->post['amount'],
  102. $this->post['buy_date'],
  103. $this->post['expiry_date']
  104. );
  105. $this->responseData = array('status' => 1, 'personal_products' => array(
  106. 'personal_product_id' => $rs->getId(),
  107. ));
  108. } catch (Exception $e) {
  109. $this->setResponseError($e);
  110. }
  111. }
  112. /**
  113. * Update a personal product
  114. *
  115. * @return void
  116. *
  117. * @issue 2632
  118. */
  119. public function executeUpdate() {
  120. if ($this->getRequest()->getMethod() != sfRequest::PUT) {
  121. $this->forward('default', 'error400');
  122. }
  123. if (is_null($this->post)) {
  124. $this->forward('default', 'error403');
  125. }
  126. try {
  127. $this->validateAccount();
  128. if (empty($this->post['personal_product_id'])) {
  129. throw new ParametersException(ParametersException::$error1000, 'personal_product_id');
  130. }
  131. $rs = DepositPersonalProductsPeer::updatePersonalProduct(
  132. $this->post['personal_product_id'],
  133. $this->post['expected_rate'],
  134. $this->post['amount'],
  135. $this->post['buy_date'],
  136. $this->post['expiry_date']
  137. );
  138. $this->responseData = array('status' => 1, 'personal_products' => array(
  139. 'personal_product_id' => $rs->getId(),
  140. ));
  141. } catch (Exception $e) {
  142. $this->setResponseError($e);
  143. }
  144. }
  145. /**
  146. * Delte a personal product
  147. *
  148. * @return void
  149. *
  150. * @issue 2632
  151. */
  152. public function executeDelete() {
  153. if ($this->getRequest()->getMethod() != sfRequest::DELETE) {
  154. $this->forward('default', 'error400');
  155. }
  156. $accountId = $this->getRequestParameter('account_id');
  157. $personalProductId = $this->getRequestParameter('personal_product_id');
  158. $hash = $this->getRequestParameter('hash');
  159. try {
  160. if (empty($accountId) || empty($personalProductId) || empty($hash)) {
  161. throw new ParametersException(ParametersException::$error1000, 'account_id, personal_product_id, hash');
  162. }
  163. DepositMembersPeer::verfiyMember($accountId, $hash);
  164. $rs = DepositPersonalProductsPeer::deletePersonalProduct(
  165. $personalProductId
  166. );
  167. $this->responseData = array('status' => 1);
  168. } catch (Exception $e) {
  169. $this->setResponseError($e);
  170. }
  171. }
  172. /**
  173. *execute batch synchronize action
  174. *
  175. * @return void
  176. *
  177. * @issue 2702
  178. */
  179. public function executeBatchSynchronize() {
  180. if ($this->getRequest()->getMethod() != sfRequest::POST) {
  181. $this->forward('default', 'error400');
  182. }
  183. if (is_null($this->post)) {
  184. apiLog::logMessage("PARAMETERS ERROR: The parameters can not be decode.");
  185. $this->forward('default', 'error403');
  186. }
  187. try {
  188. $this->validateAccount();
  189. if ($this->post['secret'] != md5(json_encode($this->post['personal_products']))) {
  190. throw new ParametersException(ParametersException::$error1001, 'secret');
  191. }
  192. shell_exec("/usr/local/php5.2/bin/php /data/testsites/deposit/devel/batch/cronjobs/BatchQueue.php " . ApiOfflineQueuePeer::ICAIFU . " " . base64_encode(json_encode($this->post['personal_products'])));
  193. //for test development
  194. shell_exec("/usr/local/php5.2/bin/php /data/testsites/deposit/devel/batch/cronjobs/ApiOfflineDequeue.php");
  195. // shell_exec('D:\upupw\PHP5\php.exe D:\Usr\Local\Web\Deposit\trunk\batch\cronjobs\BatchQueue.php ' . ApiOfflineQueuePeer::ICAIFU . ' ' . base64_encode(json_encode($this->post['personal_products'])));
  196. $this->responseData = array('status' => 1);
  197. } catch (Exception $e) {
  198. $this->setResponseError($e);
  199. }
  200. }
  201. /**
  202. * Execute RetrieveFavoritesByUser action
  203. *
  204. * @return void
  205. *
  206. * @issue 2703
  207. */
  208. public function executeRetrieveFavoritesByUser() {
  209. if ($this->getRequest()->getMethod() != sfRequest::GET) {
  210. $this->forward('default', 'error400');
  211. }
  212. $accountId = $this->getRequestParameter('account_id');
  213. $hash = $this->getRequestParameter('hash');
  214. $offset = $this->getRequestParameter('offset');
  215. $limit = $this->getRequestParameter('limit');
  216. try {
  217. if (empty($accountId)
  218. || empty($hash)) {
  219. throw new ParametersException(ParametersException::$error1000, 'account_id, hash');
  220. }
  221. DepositMembersPeer::verfiyMember($accountId, $hash);
  222. $rs = DepositMembersFavoritesPeer::retrieveFavoritesByUser(
  223. $accountId,
  224. $offset,
  225. $limit
  226. );
  227. $this->responseData = array('status' => 1, 'personal_favorites' => $rs);
  228. } catch (Exception $e) {
  229. $this->setResponseError($e);
  230. }
  231. }
  232. /**
  233. * Execute FavoritesBatchSynchronize
  234. *
  235. * @return void
  236. *
  237. * @issue 2703
  238. */
  239. public function executeFavoritesBatchSynchronize() {
  240. if ($this->getRequest()->getMethod() != sfRequest::POST) {
  241. $this->forward('default', 'error400');
  242. }
  243. if (is_null($this->post)) {
  244. apiLog::logMessage("PARAMETERS ERROR: The parameters can not be decode.");
  245. $this->forward('default', 'error403');
  246. }
  247. try {
  248. $this->validateAccount();
  249. if ($this->post['secret'] != md5(json_encode($this->post['personal_favorites']))) {
  250. throw new ParametersException(ParametersException::$error1001, 'secret');
  251. }
  252. shell_exec("/usr/local/php5.2/bin/php /data/testsites/deposit/devel/batch/cronjobs/BatchQueue.php " . ApiOfflineQueuePeer::FAVORITE . " " . base64_encode(json_encode($this->post['personal_favorites'])));
  253. //for test development
  254. shell_exec("/usr/local/php5.2/bin/php /data/testsites/deposit/devel/batch/cronjobs/ApiOfflineDequeue.php");
  255. // shell_exec('D:\upupw\PHP5\php.exe D:\Usr\Local\Web\Deposit\trunk\batch\cronjobs\BatchQueue.php ' . ApiOfflineQueuePeer::FAVORITE . ' ' . base64_encode(json_encode($this->post['personal_favorites'])));
  256. $this->responseData = array('status' => 1);
  257. } catch (Exception $e) {
  258. $this->setResponseError($e);
  259. }
  260. }
  261. /**
  262. * Check if is valid product
  263. *
  264. * @return void
  265. *
  266. * @issue 2632
  267. */
  268. private function _validateProduct() {
  269. if (empty($this->post['product_id'])) {
  270. throw new ParametersException(ParametersException::$error1000, 'product_id');
  271. }
  272. DepositFinancialProductsPeer::verifyProduct($this->post['product_id']);
  273. }
  274. /**
  275. * Check if is valid personal product
  276. *
  277. * @return void
  278. *
  279. * @issue 2626
  280. */
  281. private function _validatePersonalProduct() {
  282. if (empty($this->post['personal_product_id'])) {
  283. throw new ParametersException(ParametersException::$error1000, 'personal_product_id');
  284. }
  285. DepositPersonalProductsPeer::verifiyPersonalProduct($this->post['personal_product_id']);
  286. }
  287. }