PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/code/custom_forms/checkout/SilvercartCheckoutFormStep5.php

https://bitbucket.org/silvercart/silvercart/
PHP | 338 lines | 168 code | 37 blank | 133 comment | 21 complexity | 2aa799818b36ce606bc0bf6980af4dc3 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2010, 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 Forms Checkout
  22. */
  23. /**
  24. * checkout step for order confirmation
  25. *
  26. * @package Silvercart
  27. * @subpackage Forms Checkout
  28. * @author Roland Lehmann <rlehmann@pixeltricks.de>
  29. * @copyright Pixeltricks GmbH
  30. * @since 03.01.2011
  31. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  32. */
  33. class SilvercartCheckoutFormStep5 extends SilvercartCheckoutFormStepPaymentInit {
  34. /**
  35. * The form field definitions.
  36. *
  37. * @var array
  38. *
  39. * @author Sascha Koehler <skoehler@pixeltricks.de>
  40. * @since 31.03.2011
  41. */
  42. protected $formFields = array(
  43. 'ChosenShippingMethod' => array(
  44. 'type' => 'ReadonlyField',
  45. 'title' => 'gewählte Versandart'
  46. ),
  47. 'ChosenPaymentMethod' => array(
  48. 'type' => 'ReadonlyField',
  49. 'title' => 'gewählte Bezahlart'
  50. ),
  51. 'Note' => array(
  52. 'type' => 'TextareaField'
  53. ),
  54. 'HasAcceptedTermsAndConditions' => array(
  55. 'type' => 'CheckboxField',
  56. 'title' => 'Ich akzeptiere die allgemeinen Geschäftsbedingungen',
  57. 'checkRequirements' => array(
  58. 'isFilledIn' => true
  59. )
  60. ),
  61. 'HasAcceptedRevocationInstruction' => array(
  62. 'type' => 'CheckboxField',
  63. 'title' => 'Ich habe die Widerrufsbelehrung gelesen',
  64. 'checkRequirements' => array(
  65. 'isFilledIn' => true
  66. )
  67. ),
  68. 'SubscribedToNewsletter' => array(
  69. 'type' => 'CheckboxField',
  70. 'title' => 'Ich möchte den Newsletter abonnieren'
  71. )
  72. );
  73. /**
  74. * constructor
  75. *
  76. * @param Controller $controller the controller object
  77. * @param array $params additional parameters
  78. * @param array $preferences array with preferences
  79. * @param bool $barebone is the form initialized completely?
  80. *
  81. * @return void
  82. *
  83. * @author Sascha Koehler <skoehler@pixeltricks.de>
  84. * @copyright 2011 pixeltricks GmbH
  85. * @since 07.01.2011
  86. */
  87. public function __construct($controller, $params = null, $preferences = null, $barebone = false) {
  88. parent::__construct($controller, $params, $preferences, $barebone);
  89. if (!$barebone) {
  90. /*
  91. * redirect a user if his cart is empty and no order exists
  92. */
  93. $checkoutData = $this->controller->getCombinedStepData();
  94. if (!Member::currentUser() ||
  95. (!Member::currentUser()->SilvercartShoppingCart()->isFilled() &&
  96. !array_key_exists('orderId', $checkoutData))) {
  97. $frontPage = SilvercartPage_Controller::PageByIdentifierCode();
  98. Director::redirect($frontPage->RelativeLink());
  99. }
  100. }
  101. }
  102. /**
  103. * Here we set some preferences.
  104. *
  105. * @return void
  106. *
  107. * @author Sascha Koehler <skoehler@pixeltricks.de>
  108. * @copyright 2011 pixeltricks GmbH
  109. * @since 31.03.2011
  110. */
  111. public function preferences() {
  112. parent::preferences();
  113. $this->preferences['stepIsVisible'] = true;
  114. $this->preferences['stepTitle'] = _t('SilvercartCheckoutFormStep5.TITLE', 'Overview');
  115. $this->preferences['submitButtonTitle'] = _t('SilvercartCheckoutFormStep.ORDER_NOW', 'Order now');
  116. $this->preferences['fillInRequestValues'] = true;
  117. $this->preferences['createShoppingcartForms'] = false;
  118. $checkoutData = $this->controller->getCombinedStepData();
  119. if (isset($checkoutData['PaymentMethod'])) {
  120. $this->paymentMethodObj = DataObject::get_by_id(
  121. 'SilvercartPaymentMethod',
  122. $checkoutData['PaymentMethod']
  123. );
  124. if ($this->paymentMethodObj &&
  125. $this->paymentMethodObj->hasMethod('getOrderConfirmationSubmitButtonTitle')) {
  126. $this->preferences['submitButtonTitle'] = $this->paymentMethodObj->getOrderConfirmationSubmitButtonTitle();
  127. }
  128. }
  129. }
  130. /**
  131. * Set initial form values
  132. *
  133. * @return void
  134. *
  135. * @author Sascha Koehler <skoehler@pixeltricks.de>
  136. * @copyright 2010 pixeltricks GmbH
  137. * @since 09.11.2010
  138. */
  139. protected function fillInFieldValues() {
  140. $this->controller->fillFormFields($this->formFields);
  141. $this->formFields['ChosenShippingMethod']['title'] = _t('SilvercartCheckoutFormStep.CHOOSEN_SHIPPING', 'choosen shipping method');
  142. $this->formFields['ChosenPaymentMethod']['title'] = _t('SilvercartCheckoutFormStep.CHOOSEN_PAYMENT', 'choosen payment method');
  143. $this->formFields['HasAcceptedTermsAndConditions']['title'] = _t('SilvercartCheckoutFormStep.I_ACCEPT_TERMS', 'I accept the terms and conditions.');
  144. $this->formFields['HasAcceptedRevocationInstruction']['title'] = _t('SilvercartCheckoutFormStep.I_ACCEPT_REVOCATION', 'I accept the revocation instructions');
  145. $this->formFields['SubscribedToNewsletter']['title'] = _t('SilvercartCheckoutFormStep.I_SUBSCRIBE_NEWSLETTER', 'I subscribe to the newsletter');
  146. $stepData = $this->controller->getCombinedStepData();
  147. if ($stepData &&
  148. isset($stepData['ShippingMethod']) &&
  149. isset($stepData['PaymentMethod'])) {
  150. $chosenShippingMethod = DataObject::get_by_id('SilvercartShippingMethod', $stepData['ShippingMethod']);
  151. if ($chosenShippingMethod) {
  152. $this->formFields['ChosenShippingMethod']['value'] = $chosenShippingMethod->Title;
  153. }
  154. $chosenPaymentMethod = DataObject::get_by_id('SilvercartPaymentMethod', $stepData['PaymentMethod']);
  155. if ($chosenPaymentMethod) {
  156. $this->formFields['ChosenPaymentMethod']['value'] = $chosenPaymentMethod->Name;
  157. }
  158. }
  159. }
  160. /**
  161. * returns address data as ArrayData
  162. *
  163. * @return ArrayData
  164. *
  165. * @author Sascha Koehler <skoehler@pixeltricks.de>
  166. * @copyright 2011 pixeltricks GmbH
  167. * @since 07.01.2011
  168. */
  169. public function AddressData() {
  170. $checkoutData = $this->controller->getCombinedStepData();
  171. $shippingAddress = $this->controller->extractAddressDataFrom('Shipping', $checkoutData);
  172. $invoiceAddress = $this->controller->extractAddressDataFrom('Invoice', $checkoutData);
  173. $shippingCountry = DataObject::get_by_id(
  174. 'SilvercartCountry',
  175. $shippingAddress['CountryID']
  176. );
  177. if ($shippingCountry) {
  178. $shippingAddress['country'] = $shippingCountry;
  179. $shippingAddress['SilvercartCountry'] = $shippingCountry;
  180. $shippingAddress['isShippingAddress'] = true;
  181. $shippingAddress['hasAddressData'] = true;
  182. }
  183. $invoiceCountry = DataObject::get_by_id(
  184. 'SilvercartCountry',
  185. $invoiceAddress['CountryID']
  186. );
  187. if ($invoiceCountry) {
  188. $invoiceAddress['country'] = $invoiceCountry;
  189. $invoiceAddress['SilvercartCountry'] = $invoiceCountry;
  190. $invoiceAddress['isInvoiceAddress'] = true;
  191. $invoiceAddress['hasAddressData'] = true;
  192. }
  193. if (array_key_exists('InvoiceAddress',$checkoutData) &&
  194. array_key_exists('ShippingAddress',$checkoutData)) {
  195. if ($checkoutData['InvoiceAddress'] === $checkoutData['ShippingAddress']) {
  196. $invoiceAddress['isInvoiceAndShippingAddress'] = true;
  197. }
  198. }
  199. if (!empty($shippingAddress['TaxIdNumber']) &&
  200. !empty($shippingAddress['Company'])) {
  201. $shippingAddress['isCompanyAddress'] = true;
  202. } else {
  203. $shippingAddress['isCompanyAddress'] = false;
  204. }
  205. if (!empty($invoiceAddress['TaxIdNumber']) &&
  206. !empty($invoiceAddress['Company'])) {
  207. $invoiceAddress['isCompanyAddress'] = true;
  208. } else {
  209. $invoiceAddress['isCompanyAddress'] = false;
  210. }
  211. $addressData = new ArrayData(
  212. array(
  213. 'SilvercartShippingAddress' => $shippingAddress,
  214. 'SilvercartInvoiceAddress' => $invoiceAddress
  215. )
  216. );
  217. return $addressData;
  218. }
  219. /**
  220. * Indicates wether the invoice and shipping address are the same
  221. * SilvercartAddress object.
  222. *
  223. * @return boolean
  224. *
  225. * @author Sascha Koehler <skoehler@pixeltricks.de>
  226. * @since 12.10.2011
  227. */
  228. public function hasOnlyOneStandardAddress() {
  229. $hasOnlyOneStandardAddress = false;
  230. $checkoutData = $this->controller->getCombinedStepData();
  231. if (array_key_exists('InvoiceAddress',$checkoutData) &&
  232. array_key_exists('ShippingAddress',$checkoutData)) {
  233. if ($checkoutData['InvoiceAddress'] === $checkoutData['ShippingAddress']) {
  234. $hasOnlyOneStandardAddress = true;
  235. }
  236. }
  237. return $hasOnlyOneStandardAddress;
  238. }
  239. /**
  240. * executed if there are no valdation errors on submit
  241. * Form data is saved in session
  242. *
  243. * @param SS_HTTPRequest $data contains the frameworks form data
  244. * @param Form $form not used
  245. * @param array $formData contains the modules form data
  246. *
  247. * @return void
  248. *
  249. * @author Sascha Koehler <skoehler@pixeltricks.de>
  250. * @copyright 2010 pixeltricks GmbH
  251. * @since 4.1.2011
  252. */
  253. public function submitSuccess($data, $form, $formData) {
  254. $this->controller->setStepData($formData);
  255. $this->controller->addCompletedStep();
  256. $this->controller->NextStep();
  257. }
  258. /**
  259. * Wrapper for the pages method to deside to display prices gross or net.
  260. *
  261. * @return bool
  262. *
  263. * @author Sebastian Diel <sdiel@pixeltricks.de>
  264. * @since 30.03.2011
  265. */
  266. public function showPricesGross() {
  267. return $this->controller->showPricesGross();
  268. }
  269. /**
  270. * Due to a but we had to render the template here. If we would have included
  271. * it in the *.ss file it would have been rendered twice and our logic would
  272. * not work propery.
  273. *
  274. * @return string Rendered html code
  275. *
  276. * @author Roland Lehmann <rlehmann@pixeltricks.de>, Sascha Köher <skoehler@pixeltricks.de>
  277. * @since 19.7.2011
  278. */
  279. public function getSilvercartShoppingCartFull() {
  280. $member = Member::currentUser();
  281. if ($member) {
  282. return $this->customise($member->SilvercartShoppingCart())->renderWith('SilvercartShoppingCartFull');
  283. }
  284. }
  285. /**
  286. * The newsletter checkbox should not be shown if a registered customer has
  287. * already subscribed to the newsletter.
  288. *
  289. * @return boolean answer
  290. *
  291. * @author Roland Lehmann <rlehmann@pixeltricks.de>
  292. * @since 22.7.2011
  293. */
  294. public function showNewsletterCheckbox() {
  295. $customer = SilvercartCustomer::currentRegisteredCustomer();
  296. if ($customer && $customer->SubscribedToNewsletter == 1) {
  297. return false;
  298. }
  299. return true;
  300. }
  301. }