PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/code/pages/SilvercartAddressHolder.php

https://bitbucket.org/silvercart/silvercart/
PHP | 310 lines | 204 code | 11 blank | 95 comment | 8 complexity | 6b6e928de228a8f67abb052ed80a8cd0 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 Pages
  22. */
  23. /**
  24. * Child of customer area; overview of all addresses;
  25. *
  26. * @package Silvercart
  27. * @subpackage Pages
  28. * @copyright 2010 pixeltricks GmbH
  29. * @author Roland Lehmann <rlehmann@pixeltricks.de>
  30. * @since 16.02.2011
  31. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  32. */
  33. class SilvercartAddressHolder extends SilvercartMyAccountHolder {
  34. public static $singular_name = "";
  35. public static $can_be_root = false;
  36. public static $allowed_children = array(
  37. "SilvercartAddressPage"
  38. );
  39. /**
  40. * The icon to use for this page in the storeadmin sitetree.
  41. *
  42. * @var string
  43. *
  44. * @author Sascha Koehler <skoehler@pixeltricks.de>
  45. * @since 27.10.2011
  46. */
  47. public static $icon = "silvercart/images/page_icons/my_account_holder";
  48. /**
  49. * Return all fields of the backend
  50. *
  51. * @return FieldSet Fields of the CMS
  52. */
  53. public function getCMSFields() {
  54. $fields = parent::getCMSFields();
  55. return $fields;
  56. }
  57. /**
  58. * configure the class name of the DataObjects to be shown on this page
  59. * this is needed to show correct breadcrumbs
  60. *
  61. * @return string class name of the DataObject to be shown on this page
  62. * @author Sascha Koehler <skoehler@pixeltricks.de>
  63. * @since 3.11.2010
  64. */
  65. public function getSection() {
  66. return 'SilvercartAddress';
  67. }
  68. /**
  69. * Adds the part for 'Add new address' to the breadcrumbs. Sets the link for
  70. * The default action in breadcrumbs.
  71. *
  72. * @param int $maxDepth maximum levels
  73. * @param bool $unlinked link breadcrumbs elements
  74. * @param bool $stopAtPageType name of PageType to stop at
  75. * @param bool $showHidden show pages that will not show in menus
  76. *
  77. * @return string
  78. *
  79. * @author Sebastian Diel <sdiel@pixeltricks.de>
  80. * @since 27.06.2011
  81. */
  82. public function Breadcrumbs($maxDepth = 20, $unlinked = false, $stopAtPageType = false, $showHidden = false) {
  83. $breadcrumbs = parent::Breadcrumbs($maxDepth, $unlinked, $stopAtPageType, $showHidden);
  84. if (Controller::curr()->getAction() == 'addNewAddress') {
  85. $parts = explode(self::$breadcrumbs_delimiter, $breadcrumbs);
  86. $addressHolder = array_pop($parts);
  87. $parts[] = ("<a href=\"" . $this->Link() . "\">" . $addressHolder . "</a>");
  88. $parts[] = _t('SilvercartAddressHolder.ADD', 'Add new address');
  89. $breadcrumbs = implode(self::$breadcrumbs_delimiter, $parts);
  90. }
  91. return $breadcrumbs;
  92. }
  93. }
  94. /**
  95. * Controller Class
  96. *
  97. * @package Silvercart
  98. * @subpackage Pages
  99. * @author Roland Lehmann <rlehmann@pixeltricks.de>
  100. * @since 16.02.2011
  101. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  102. * @copyright 2010 pixeltricks GmbH
  103. */
  104. class SilvercartAddressHolder_Controller extends SilvercartMyAccountHolder_Controller {
  105. public static $allowed_actions = array (
  106. 'deleteAddress',
  107. 'setInvoiceAddress',
  108. 'setShippingAddress',
  109. 'addNewAddress',
  110. );
  111. /**
  112. * execute these statements on object call
  113. *
  114. * @return void
  115. * @author Roland Lehmann <rlehmann@pixeltricks.de>
  116. * @since 18.10.2010
  117. */
  118. public function init() {
  119. parent::init();
  120. $preferences = array();
  121. $urlParams = $this->getURLParams();
  122. if ($urlParams['Action'] == 'addNewAddress') {
  123. $preferences['submitAction'] = 'addNewAddress/customHtmlFormSubmit';
  124. }
  125. $this->registerCustomHtmlForm('SilvercartAddAddressForm', new SilvercartAddAddressForm($this, null, $preferences));
  126. }
  127. /**
  128. * Action to delete an address. Checks, whether the given address is related
  129. * to the logged in customer and deletes it.
  130. *
  131. * @param SS_HTTPRequest $request The given request
  132. *
  133. * @return void
  134. *
  135. * @author Sebastian Diel <sdiel@pixeltricks.de>
  136. * @since 27.06.2011
  137. */
  138. public function deleteAddress(SS_HTTPRequest $request) {
  139. $params = $request->allParams();
  140. if (array_key_exists('ID', $params)
  141. && !empty ($params['ID'])) {
  142. $addressID = (int) $params['ID'];
  143. if (Member::currentUser()->SilvercartAddresses()->Count() == 1) {
  144. // address can't be deleted because it's the only one
  145. $this->setErrorMessage(_t('SilvercartAddressHolder.ADDRESS_CANT_BE_DELETED', "Sorry, but you can't delete your only address."));
  146. } elseif (Member::currentUser()->SilvercartAddresses()->containsIDs(array($addressID))) {
  147. // Address contains to logged in user - delete it
  148. if (Member::currentUser()->SilvercartInvoiceAddress()->ID == $addressID) {
  149. // set shipping address as users invoice address
  150. Member::currentUser()->SilvercartInvoiceAddressID = Member::currentUser()->SilvercartShippingAddress()->ID;
  151. Member::currentUser()->write();
  152. }
  153. if (Member::currentUser()->SilvercartShippingAddress()->ID == $addressID) {
  154. // set invoice address as users shipping address
  155. Member::currentUser()->SilvercartShippingAddressID = Member::currentUser()->SilvercartInvoiceAddress()->ID;
  156. Member::currentUser()->write();
  157. }
  158. DataObject::get_by_id('SilvercartAddress', $addressID)->delete();
  159. $this->setSuccessMessage(_t('SilvercartAddressHolder.ADDRESS_SUCCESSFULLY_DELETED', 'Your address was successfully deleted.'));
  160. } else {
  161. // possible break in attempt!
  162. $this->setErrorMessage(_t('SilvercartAddressHolder.ADDRESS_NOT_FOUND', 'Sorry, but the given address was not found.'));
  163. }
  164. }
  165. Director::redirectBack();
  166. }
  167. /**
  168. * Action to set an address as invoice address.
  169. *
  170. * @param SS_HTTPRequest $request The given request
  171. *
  172. * @return void
  173. *
  174. * @author Sebastian Diel <sdiel@pixeltricks.de>
  175. * @since 27.06.2011
  176. */
  177. public function setInvoiceAddress(SS_HTTPRequest $request) {
  178. $params = $request->allParams();
  179. if (array_key_exists('ID', $params)
  180. && !empty ($params['ID'])) {
  181. $addressID = (int) $params['ID'];
  182. if (Member::currentUser()->SilvercartAddresses()->containsIDs(array($addressID))) {
  183. // Address contains to logged in user - set as invoice address
  184. Member::currentUser()->SilvercartInvoiceAddressID = $addressID;
  185. Member::currentUser()->write();
  186. $this->setSuccessMessage(_t('SilvercartAddressHolder.UPDATED_INVOICE_ADDRESS', 'Your invoice addres was successfully updated.'));
  187. } else {
  188. // possible break in attempt!
  189. $this->setErrorMessage(_t('SilvercartAddressHolder.ADDRESS_NOT_FOUND', 'Sorry, but the given address was not found.'));
  190. }
  191. }
  192. Director::redirectBack();
  193. }
  194. /**
  195. * Action to set an address as shipping address.
  196. *
  197. * @param SS_HTTPRequest $request The given request
  198. *
  199. * @return void
  200. *
  201. * @author Sebastian Diel <sdiel@pixeltricks.de>
  202. * @since 27.06.2011
  203. */
  204. public function setShippingAddress(SS_HTTPRequest $request) {
  205. $params = $request->allParams();
  206. if (array_key_exists('ID', $params)
  207. && !empty ($params['ID'])) {
  208. $addressID = (int) $params['ID'];
  209. if (Member::currentUser()->SilvercartAddresses()->containsIDs(array($addressID))) {
  210. // Address contains to logged in user - set as invoice address
  211. Member::currentUser()->SilvercartShippingAddressID = $addressID;
  212. Member::currentUser()->write();
  213. $this->setSuccessMessage(_t('SilvercartAddressHolder.UPDATED_SHIPPING_ADDRESS', 'Your shipping addres was successfully updated.'));
  214. } else {
  215. // possible break in attempt!
  216. $this->setErrorMessage(_t('SilvercartAddressHolder.ADDRESS_NOT_FOUND', 'Sorry, but the given address was not found.'));
  217. }
  218. }
  219. Director::redirectBack();
  220. }
  221. /**
  222. * Action to add a new address.
  223. *
  224. * @param SS_HTTPRequest $request The given request
  225. *
  226. * @return void
  227. *
  228. * @author Sebastian Diel <sdiel@pixeltricks.de>
  229. * @since 27.06.2011
  230. */
  231. public function addNewAddress(SS_HTTPRequest $request) {
  232. $params = $request->allParams();
  233. if (strtolower($params['ID']) == 'customhtmlformsubmit') {
  234. $result = $this->CustomHtmlFormSubmit($request);
  235. $form = $this->getRegisteredCustomHtmlForm('SilvercartAddAddressForm');
  236. if ($form->submitSuccess) {
  237. $this->setSuccessMessage(_t('SilvercartAddressHolder.ADDED_ADDRESS_SUCCESS', 'Your address was successfully saved.'));
  238. return;
  239. } else {
  240. $this->setErrorMessage(_t('SilvercartAddressHolder.ADDED_ADDRESS_FAILURE', 'Your address could not be saved.'));
  241. }
  242. }
  243. return $this->renderWith(array('SilvercartAddNewAddress', 'Page'));
  244. }
  245. /**
  246. * Get the error message out of session and delete it (from session).
  247. *
  248. * @return string
  249. */
  250. public function getErrorMessage() {
  251. $errorMessage = Session::get('SilvercartAddressHolder.errorMessage');
  252. Session::clear('SilvercartAddressHolder.errorMessage');
  253. Session::save();
  254. return $errorMessage;
  255. }
  256. /**
  257. * Set the error message into the session.
  258. *
  259. * @param string $errorMessage Error message
  260. *
  261. * @return void
  262. */
  263. public function setErrorMessage($errorMessage) {
  264. Session::set('SilvercartAddressHolder.errorMessage', $errorMessage);
  265. Session::save();
  266. }
  267. /**
  268. * Get the success message out of session and delete it (from session).
  269. *
  270. * @return string
  271. */
  272. public function getSuccessMessage() {
  273. $successMessage = Session::get('SilvercartAddressHolder.successMessage');
  274. Session::clear('SilvercartAddressHolder.successMessage');
  275. Session::save();
  276. return $successMessage;
  277. }
  278. /**
  279. * Set the success message into the session.
  280. *
  281. * @param string $successMessage Success message
  282. *
  283. * @return void
  284. */
  285. public function setSuccessMessage($successMessage) {
  286. Session::set('SilvercartAddressHolder.successMessage', $successMessage);
  287. Session::save();
  288. }
  289. }