PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/code/pages/SilvercartInboundShoppingCartTransferPage.php

https://bitbucket.org/silvercart/silvercart/
PHP | 374 lines | 168 code | 47 blank | 159 comment | 24 complexity | 717d25f9b086cb07eb6105bfc34c5412 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2011 pixeltricks GmbH
  4. *
  5. * This file is part of SilverCart.
  6. *
  7. * SilverCart is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SilverCart is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SilverCart. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @package Silvercart
  21. * @subpackage Pages
  22. */
  23. /**
  24. * Handles the transfer of shopping cart items from an external referer to
  25. * a current users shopping cart.
  26. *
  27. * @package Silvercart
  28. * @subpackage Pages
  29. * @author Sascha Koehler <skoehler@pixeltricks.de>
  30. * @since 01.08.2011
  31. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  32. * @copyright 2011 pixeltricks GmbH
  33. */
  34. class SilvercartInboundShoppingCartTransferPage extends Page {
  35. }
  36. /**
  37. * Handles the transfer of shopping cart items from an external referer to
  38. * a current users shopping cart.
  39. *
  40. * @package Silvercart
  41. * @subpackage Pages
  42. * @author Sascha Koehler <skoehler@pixeltricks.de>
  43. * @since 01.08.2011
  44. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  45. * @copyright 2011 pixeltricks GmbH
  46. */
  47. class SilvercartInboundShoppingCartTransferPage_Controller extends Page_Controller {
  48. /**
  49. * Contains all error messages.
  50. *
  51. * @var array
  52. *
  53. * @author Sascha Koehler <skoehler@pixeltricks.de>
  54. * @since 01.08.2011
  55. */
  56. protected $errorMessages = array();
  57. /**
  58. * We implement our own action handling here since we use the action
  59. * as identifier string to look up the corresponding
  60. * SilvercartInboundShoppingCartTransfer object.
  61. *
  62. * @param SS_HTTPRequest $request The request parameters
  63. *
  64. * @return string
  65. *
  66. * @author Sascha Koehler <skoehler@pixeltricks.de>
  67. * @since 01.08.2011
  68. */
  69. public function handleAction(SS_HTTPRequest $request) {
  70. $this->action = str_replace("-","_",$request->param('Action'));
  71. $this->requestParams = $request->requestVars();
  72. $inboundShoppingCartTransfer = DataObject::get_one(
  73. 'SilvercartInboundShoppingCartTransfer',
  74. sprintf(
  75. "refererIdentifier = '%s'",
  76. Convert::raw2sql($this->action)
  77. )
  78. );
  79. if ($inboundShoppingCartTransfer) {
  80. if ($inboundShoppingCartTransfer->useSharedSecret &&
  81. !$this->checkSharedSecretFor($inboundShoppingCartTransfer, $request)) {
  82. return $this->sharedSecretInvalid();
  83. } else {
  84. switch ($inboundShoppingCartTransfer->transferMethod) {
  85. case 'keyValue':
  86. return $this->handleKeyValueShoppingCartTransferWith($inboundShoppingCartTransfer, $request);
  87. break;
  88. case 'combinedString':
  89. default:
  90. return $this->handleCombinedStringShoppingCartTransferWith($inboundShoppingCartTransfer, $request);
  91. }
  92. }
  93. } else {
  94. return $this->refererNotFound();
  95. }
  96. }
  97. /**
  98. * Returns the error messages.
  99. *
  100. * @return string
  101. *
  102. * @author Sascha Koehler <skoehler@pixeltricks.de>
  103. * @since 01.08.2011
  104. */
  105. public function ErrorMessages() {
  106. return new DataObjectSet($this->errorMessages);
  107. }
  108. /**
  109. * Handles the transfer of the sent product data to a valid shopping cart
  110. * via key-value pairs.
  111. *
  112. * @param SilvercartInboundShoppingCartTransfer $inboundShoppingCartTransfer The transfer object that handles this referer
  113. * @param SS_HTTPRequest $request The request parameters
  114. *
  115. * @return string
  116. *
  117. * @author Sascha Koehler <skoehler@pixeltricks.de>
  118. * @since 01.08.2011
  119. */
  120. protected function handleKeyValueShoppingCartTransferWith(SilvercartInboundShoppingCartTransfer $inboundShoppingCartTransfer, SS_HTTPRequest $request) {
  121. $error = false;
  122. $requestVars = $request->requestVars();
  123. $identifierIdx = 0;
  124. if (!array_key_exists($inboundShoppingCartTransfer->keyValueProductIdentifier, $requestVars)) {
  125. return $this->keyValueProductIdentifierNotFound();
  126. }
  127. if (!array_key_exists($inboundShoppingCartTransfer->keyValueQuantityIdentifier, $requestVars)) {
  128. return $this->keyValueQuantityIdentifierNotFound();
  129. }
  130. $identifierCount = count($requestVars[$inboundShoppingCartTransfer->keyValueProductIdentifier]);
  131. for ($identifierIdx = 0; $identifierIdx < $identifierCount; $identifierIdx++) {
  132. if (array_key_exists($identifierIdx, $requestVars[$inboundShoppingCartTransfer->keyValueQuantityIdentifier])) {
  133. $productQuantity = $requestVars[$inboundShoppingCartTransfer->keyValueQuantityIdentifier][$identifierIdx];
  134. } else {
  135. $productQuantity = 1;
  136. }
  137. $product = DataObject::get_one(
  138. 'SilvercartProduct',
  139. sprintf(
  140. $inboundShoppingCartTransfer->productMatchingField." = '%s'",
  141. Convert::raw2sql($requestVars[$inboundShoppingCartTransfer->keyValueProductIdentifier][$identifierIdx])
  142. )
  143. );
  144. if ($product) {
  145. $this->addProduct($product, $productQuantity);
  146. }
  147. }
  148. if (!$error) {
  149. Director::redirect(SilvercartPage_controller::PageByIdentifierCodeLink('SilvercartCartPage'));
  150. }
  151. }
  152. /**
  153. * Handles the transfer of the sent product data to a valid shopping cart
  154. * via one string with separators.
  155. *
  156. * @param SilvercartInboundShoppingCartTransfer $inboundShoppingCartTransfer The transfer object that handles this referer
  157. * @param SS_HTTPRequest $request The request parameters
  158. *
  159. * @return string
  160. *
  161. * @author Sascha Koehler <skoehler@pixeltricks.de>
  162. * @since 01.08.2011
  163. */
  164. protected function handleCombinedStringShoppingCartTransferWith(SilvercartInboundShoppingCartTransfer $inboundShoppingCartTransfer, SS_HTTPRequest $request) {
  165. $error = false;
  166. $requestVars = $request->requestVars();
  167. if (!array_key_exists($inboundShoppingCartTransfer->combinedStringKey, $requestVars)) {
  168. $action = $this->urlParams['ID'];
  169. $actionElements = explode('&', $action);
  170. $validCombinedKeyFound = false;
  171. foreach ($actionElements as $actionElement) {
  172. if (strpos($actionElement, '=') === false) {
  173. continue;
  174. }
  175. list($combinedStringKey, $combinedStringEntities) = explode('=', $actionElement);
  176. if ($combinedStringKey == $inboundShoppingCartTransfer->combinedStringKey) {
  177. $validCombinedKeyFound = true;
  178. $combinedString = Convert::raw2sql($combinedStringKey);
  179. $entities = explode($inboundShoppingCartTransfer->combinedStringEntitySeparator, $combinedStringEntities);
  180. }
  181. }
  182. if (!$validCombinedKeyFound) {
  183. return $this->combinedStringKeyNotFound();
  184. }
  185. } else {
  186. $combinedString = Convert::raw2sql($requestVars[$inboundShoppingCartTransfer->combinedStringKey]);
  187. $entities = explode($inboundShoppingCartTransfer->combinedStringEntitySeparator, $combinedString);
  188. }
  189. if (is_array($entities)) {
  190. foreach ($entities as $entity) {
  191. if (empty($entity)) {
  192. continue;
  193. }
  194. list($productIdentifier, $productQuantity) = explode($inboundShoppingCartTransfer->combinedStringQuantitySeparator, $entity);
  195. $product = DataObject::get_one(
  196. 'SilvercartProduct',
  197. sprintf(
  198. $inboundShoppingCartTransfer->productMatchingField." = '%s'",
  199. $productIdentifier
  200. )
  201. );
  202. if ($product) {
  203. $this->addProduct($product, $productQuantity);
  204. }
  205. }
  206. }
  207. if (!$error) {
  208. Director::redirect(SilvercartPage_controller::PageByIdentifierCodeLink('SilvercartCartPage'));
  209. }
  210. }
  211. /**
  212. * Add a product to the shopping cart.
  213. *
  214. * @param SilvercartProduct $product The product object to add to the shopping cart
  215. * @param int $productQuantity The quantity of the product to add
  216. *
  217. * @return boolean
  218. *
  219. * @author Sascha Koehler <skoehler@pixeltricks.de>
  220. * @since 01.08.2011
  221. */
  222. protected function addProduct(SilvercartProduct $product, $productQuantity) {
  223. $productAdded = false;
  224. if ($product->isActive &&
  225. $product->SilvercartProductGroupID > 0 &&
  226. $product->isBuyableDueToStockManagementSettings()) {
  227. $productData = array(
  228. 'productID' => $product->ID,
  229. 'productQuantity' => $productQuantity
  230. );
  231. $productAdded = SilvercartShoppingCart::addProduct($productData);
  232. }
  233. return $productAdded;
  234. }
  235. /**
  236. * Check if a shared secret was sent and is valid for this transfer type.
  237. *
  238. * @param SilvercartInboundShoppingCartTransfer $inboundShoppingCartTransfer The transfer object that handles this referer
  239. * @param SS_HTTPRequest $request The request parameters
  240. *
  241. * @return boolean
  242. *
  243. * @author Sascha Koehler <skoehler@pixeltricks.de>
  244. * @since 01.08.2011
  245. */
  246. protected function checkSharedSecretFor(SilvercartInboundShoppingCartTransfer $inboundShoppingCartTransfer, SS_HTTPRequest $request) {
  247. $isValid = false;
  248. $requestVars = $request->requestVars();
  249. if (array_key_exists($inboundShoppingCartTransfer->sharedSecretIdentifier, $requestVars) &&
  250. sha1($inboundShoppingCartTransfer->sharedSecret) === urldecode($requestVars[$inboundShoppingCartTransfer->sharedSecretIdentifier])) {
  251. $isValid = true;
  252. }
  253. return $isValid;
  254. }
  255. /**
  256. * Displays an error output since the referer could not be found.
  257. *
  258. * @return string
  259. *
  260. * @author Sascha Koehler <skoehler@pixeltricks.de>
  261. * @since 01.08.2011
  262. */
  263. protected function refererNotFound() {
  264. $this->errorMessages[] = array(
  265. 'Error' => _t('SilvercartInboundShoppingCartTransferPage.ERROR_REFERER_NOT_FOUND')
  266. );
  267. return $this;
  268. }
  269. /**
  270. * Displays an error output since the key-value product identifier is
  271. * missing.
  272. *
  273. * @return string
  274. *
  275. * @author Sascha Koehler <skoehler@pixeltricks.de>
  276. * @since 01.08.2011
  277. */
  278. protected function keyValueProductIdentifierNotFound() {
  279. $this->errorMessages[] = array(
  280. 'Error' => _t('SilvercartInboundShoppingCartTransferPage.ERROR_KEY_VALUE_PRODUCT_IDENTIFIER_NOT_FOUND')
  281. );
  282. return $this;
  283. }
  284. /**
  285. * Displays an error output since the key-value quantity identifier is
  286. * missing.
  287. *
  288. * @return string
  289. *
  290. * @author Sascha Koehler <skoehler@pixeltricks.de>
  291. * @since 01.08.2011
  292. */
  293. protected function keyValueQuantityIdentifierNotFound() {
  294. $this->errorMessages[] = array(
  295. 'Error' => _t('SilvercartInboundShoppingCartTransferPage.ERROR_KEY_VALUE_QUANTITY_IDENTIFIER_NOT_FOUND')
  296. );
  297. return $this;
  298. }
  299. /**
  300. * Displays an error output since the combined string key is missing.
  301. *
  302. * @return string
  303. *
  304. * @author Sascha Koehler <skoehler@pixeltricks.de>
  305. * @since 01.08.2011
  306. */
  307. protected function combinedStringKeyNotFound() {
  308. $this->errorMessages[] = array(
  309. 'Error' => _t('SilvercartInboundShoppingCartTransferPage.ERROR_COMBINED_STRING_KEY_NOT_FOUND')
  310. );
  311. return $this;
  312. }
  313. /**
  314. * Displays an error output since the sent shared secret is invalid.
  315. *
  316. * @return string
  317. *
  318. * @author Sascha Koehler <skoehler@pixeltricks.de>
  319. * @since 01.08.2011
  320. */
  321. protected function sharedSecretInvalid() {
  322. $this->errorMessages[] = array(
  323. 'Error' => _t('SilvercartInboundShoppingCartTransferPage.ERROR_SHARED_SECRET_INVALID')
  324. );
  325. return $this;
  326. }
  327. }