Dashboard.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Block\Account;
  7. use Magento\Customer\Api\AccountManagementInterface;
  8. use Magento\Customer\Api\CustomerRepositoryInterface;
  9. /**
  10. * Customer dashboard block
  11. *
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Dashboard extends \Magento\Framework\View\Element\Template
  16. {
  17. /**
  18. * @var \Magento\Newsletter\Model\Subscriber
  19. */
  20. protected $subscription;
  21. /**
  22. * @var \Magento\Customer\Model\Session
  23. */
  24. protected $customerSession;
  25. /**
  26. * @var \Magento\Newsletter\Model\SubscriberFactory
  27. */
  28. protected $subscriberFactory;
  29. /**
  30. * @var CustomerRepositoryInterface
  31. */
  32. protected $customerRepository;
  33. /**
  34. * @var AccountManagementInterface
  35. */
  36. protected $customerAccountManagement;
  37. /**
  38. * Constructor
  39. *
  40. * @param \Magento\Framework\View\Element\Template\Context $context
  41. * @param \Magento\Customer\Model\Session $customerSession
  42. * @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
  43. * @param CustomerRepositoryInterface $customerRepository
  44. * @param AccountManagementInterface $customerAccountManagement
  45. * @param array $data
  46. */
  47. public function __construct(
  48. \Magento\Framework\View\Element\Template\Context $context,
  49. \Magento\Customer\Model\Session $customerSession,
  50. \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
  51. CustomerRepositoryInterface $customerRepository,
  52. AccountManagementInterface $customerAccountManagement,
  53. array $data = []
  54. ) {
  55. $this->customerSession = $customerSession;
  56. $this->subscriberFactory = $subscriberFactory;
  57. $this->customerRepository = $customerRepository;
  58. $this->customerAccountManagement = $customerAccountManagement;
  59. parent::__construct($context, $data);
  60. }
  61. /**
  62. * Return the Customer given the customer Id stored in the session.
  63. *
  64. * @return \Magento\Customer\Api\Data\CustomerInterface
  65. */
  66. public function getCustomer()
  67. {
  68. return $this->customerRepository->getById($this->customerSession->getCustomerId());
  69. }
  70. /**
  71. * Retrieve the Url for editing the customer's account.
  72. *
  73. * @return string
  74. */
  75. public function getAccountUrl()
  76. {
  77. return $this->_urlBuilder->getUrl('customer/account/edit', ['_secure' => true]);
  78. }
  79. /**
  80. * Retrieve the Url for customer addresses.
  81. *
  82. * @return string
  83. */
  84. public function getAddressesUrl()
  85. {
  86. return $this->_urlBuilder->getUrl('customer/address/index', ['_secure' => true]);
  87. }
  88. /**
  89. * Retrieve the Url for editing the specified address.
  90. *
  91. * @param \Magento\Customer\Api\Data\AddressInterface $address
  92. * @return string
  93. */
  94. public function getAddressEditUrl($address)
  95. {
  96. return $this->_urlBuilder->getUrl(
  97. 'customer/address/edit',
  98. ['_secure' => true, 'id' => $address->getId()]
  99. );
  100. }
  101. /**
  102. * Retrieve the Url for customer orders.
  103. *
  104. * @return string
  105. */
  106. public function getOrdersUrl()
  107. {
  108. return $this->_urlBuilder->getUrl('customer/order/index', ['_secure' => true]);
  109. }
  110. /**
  111. * Retrieve the Url for customer reviews.
  112. *
  113. * @return string
  114. */
  115. public function getReviewsUrl()
  116. {
  117. return $this->_urlBuilder->getUrl('review/customer/index', ['_secure' => true]);
  118. }
  119. /**
  120. * Retrieve the Url for managing customer wishlist.
  121. *
  122. * @return string
  123. */
  124. public function getWishlistUrl()
  125. {
  126. return $this->_urlBuilder->getUrl('customer/wishlist/index', ['_secure' => true]);
  127. }
  128. /**
  129. * Retrieve the subscription object (i.e. the subscriber).
  130. *
  131. * @return \Magento\Newsletter\Model\Subscriber
  132. */
  133. public function getSubscriptionObject()
  134. {
  135. if ($this->subscription === null) {
  136. $this->subscription =
  137. $this->_createSubscriber()->loadByCustomerId($this->customerSession->getCustomerId());
  138. }
  139. return $this->subscription;
  140. }
  141. /**
  142. * Retrieve the Url for managing newsletter subscriptions.
  143. *
  144. * @return string
  145. */
  146. public function getManageNewsletterUrl()
  147. {
  148. return $this->getUrl('newsletter/manage');
  149. }
  150. /**
  151. * Retrieve subscription text, either subscribed or not.
  152. *
  153. * @return \Magento\Framework\Phrase
  154. */
  155. public function getSubscriptionText()
  156. {
  157. if ($this->getSubscriptionObject()->isSubscribed()) {
  158. return __('You are subscribed to our newsletter.');
  159. }
  160. return __('You aren\'t subscribed to our newsletter.');
  161. }
  162. /**
  163. * Retrieve the customer's primary addresses (i.e. default billing and shipping).
  164. *
  165. * @return \Magento\Customer\Api\Data\AddressInterface[]|bool
  166. */
  167. public function getPrimaryAddresses()
  168. {
  169. $addresses = [];
  170. $customerId = $this->getCustomer()->getId();
  171. if ($defaultBilling = $this->customerAccountManagement->getDefaultBillingAddress($customerId)) {
  172. $addresses[] = $defaultBilling;
  173. }
  174. if ($defaultShipping = $this->customerAccountManagement->getDefaultShippingAddress($customerId)) {
  175. if ($defaultBilling) {
  176. if ($defaultBilling->getId() != $defaultShipping->getId()) {
  177. $addresses[] = $defaultShipping;
  178. }
  179. } else {
  180. $addresses[] = $defaultShipping;
  181. }
  182. }
  183. return empty($addresses) ? false : $addresses;
  184. }
  185. /**
  186. * Get back Url in account dashboard.
  187. *
  188. * This method is copy/pasted in:
  189. * \Magento\Wishlist\Block\Customer\Wishlist - Because of strange inheritance
  190. * \Magento\Customer\Block\Address\Book - Because of secure Url
  191. *
  192. * @return string
  193. */
  194. public function getBackUrl()
  195. {
  196. // the RefererUrl must be set in appropriate controller
  197. if ($this->getRefererUrl()) {
  198. return $this->getRefererUrl();
  199. }
  200. return $this->getUrl('customer/account/');
  201. }
  202. /**
  203. * Create an instance of a subscriber.
  204. *
  205. * @return \Magento\Newsletter\Model\Subscriber
  206. */
  207. protected function _createSubscriber()
  208. {
  209. return $this->subscriberFactory->create();
  210. }
  211. }